├── README.md └── strfile-encryptor.py /README.md: -------------------------------------------------------------------------------- 1 | # String/File Encryptor 2 | This script has the ability to encrypt both text, file and shellcode using AES and XOR. 3 | ## Usage 4 | ``` 5 | strfile-encryptor.py [-h] -t {text,file,shellcode} -e {xor,aes} -i INPUTS [-l KEY_LENGTH] 6 | 7 | options: 8 | -h, --help show this help message and exit 9 | -t {text,file,shellcode}, --input-type {text,file,shellcode} 10 | 'text', 'file' or 'shellcode'. 11 | -e {xor,aes}, --encryption-type {xor,aes} 12 | 'xor' or 'aes'. 13 | -i INPUTS, --inputs INPUTS 14 | input filenames/texts separated by ','. 15 | -l KEY_LENGTH, --key-length KEY_LENGTH 16 | XOR key length. default is '10'. 17 | ``` 18 | **Note:** If `-t` is `shellcode` it must be in a file in raw format. 19 | ## Decryption functions 20 | XOR 21 | ```cpp 22 | void XOR(unsigned char data[], int dataSize, char key[], int keySize) { 23 | for (int i = 0; i < (dataSize / sizeof(unsigned char)); i++) { 24 | char currentKey = key[i % (keySize - 1)]; 25 | data[i] ^= currentKey; 26 | } 27 | } 28 | ``` 29 | AES 30 | ```cpp 31 | int AESDecrypt(unsigned char* payload, unsigned long payload_len, char* key, size_t keylen) { 32 | HCRYPTPROV hProv; 33 | HCRYPTHASH hHash; 34 | HCRYPTKEY hKey; 35 | 36 | if (!CryptAcquireContextW(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) { 37 | return -1; 38 | } 39 | if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash)) { 40 | return -1; 41 | } 42 | if (!CryptHashData(hHash, (const BYTE*)key, (DWORD)keylen, 0)) { 43 | return -1; 44 | } 45 | if (!CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey)) { 46 | return -1; 47 | } 48 | 49 | if (!CryptDecrypt(hKey, (HCRYPTHASH)NULL, 0, 0, payload, &payload_len)) { 50 | return -1; 51 | } 52 | 53 | CryptReleaseContext(hProv, 0); 54 | CryptDestroyHash(hHash); 55 | CryptDestroyKey(hKey); 56 | return 0; 57 | } 58 | ``` -------------------------------------------------------------------------------- /strfile-encryptor.py: -------------------------------------------------------------------------------- 1 | import string 2 | from random import choice 3 | from os import urandom 4 | import hashlib 5 | from Crypto.Cipher import AES 6 | from Crypto.Util.Padding import pad 7 | import argparse 8 | 9 | 10 | 11 | class Encryption: 12 | def __init__(self,input_type, enc_type,length=10): 13 | self.letters = string.ascii_lowercase+string.ascii_uppercase+string.digits 14 | self.length = length 15 | self.input_type = input_type 16 | self.enc_type = enc_type 17 | def __XOR_key_generator(self,length): 18 | "generate random XOR key" 19 | key = "" 20 | for i in range(length): 21 | key += choice(self.letters) 22 | return key 23 | def XOR(self,data): 24 | "XOR text/file" 25 | key = self.__XOR_key_generator(self.length) 26 | encoded_data = data if self.input_type == "file" or self.input_type == "shellcode" else data.encode()+b'\x00' 27 | data_array = bytearray(encoded_data) #modifable when bytearray 28 | for i in range(len(data_array)): 29 | current_key = key[i % len(key)] 30 | data_array[i] ^= ord(current_key) 31 | 32 | encrypted = bytes(data_array) 33 | key_var = "XORKey" if self.input_type == "file" or self.input_type == "shellcode" else "k"+data[0].upper()+data[1:] 34 | key_value = "{" + ", ".join(hex(x) for x in (key.encode()+b"\x00")) + "}" 35 | key_info = f"char {key_var}[] = {key_value};\n" 36 | if(self.input_type == "file"): 37 | return [encrypted,key_info] 38 | elif(self.input_type == "text" or self.input_type == "shellcode"): 39 | ciphertext_value = "{" + ", ".join(hex(x) for x in encrypted) + "}" 40 | varname = "Shellcode" if self.input_type == "shellcode" else data[0].upper()+data[1:] 41 | ciphertext_info = f"unsigned char s{varname}[] = {ciphertext_value};\n" 42 | return [ciphertext_info, key_info] 43 | 44 | def AES(self, data): 45 | "AES encrypt text/file" 46 | key = urandom(16) 47 | aes_key = hashlib.sha256(key).digest() 48 | 49 | iv = b'\x00' * 16 50 | encoded_data = data if self.input_type == "file" or self.input_type == "shellcode" else data.encode()+b'\x00' 51 | padded_data = pad(encoded_data, AES.block_size) 52 | cipher = AES.new(aes_key, AES.MODE_CBC, iv) 53 | 54 | key_var = "AESKey" if self.input_type == "file" or self.input_type == "shellcode" else "k"+data[0].upper()+data[1:] 55 | key_info = f"char {key_var}[] ="+ "{ 0x" + ", 0x".join(hex(ord(chr(x)))[2:] for x in key) + "};\n" 56 | encrypted = cipher.encrypt(padded_data) 57 | if(self.input_type == "file"): return [encrypted,key_info] 58 | elif(self.input_type == "shellcode" or self.input_type == "text"): 59 | ciphertext_value = "{" + ", ".join(hex(x) for x in encrypted ) + "}" 60 | varname = "Shellcode" if self.input_type == "shellcode" else data[0].upper()+data[1:] 61 | ciphertext_info = f"unsigned char s{varname}[] = {ciphertext_value};\n" 62 | return [ciphertext_info, key_info] 63 | def encrypt(self,data): 64 | if self.input_type != "file": 65 | data_var = "Shellcode" if self.input_type == "shellcode" else data[0].upper()+data[1:] 66 | decryption_function_call = f"(s{data_var}, sizeof(s{data_var}), k{data_var}, sizeof(k{data_var}));\n" 67 | if(enc_type == "aes"): 68 | result = self.AES(data) 69 | if self.input_type != "file": result.append("AESDecrypt"+decryption_function_call) 70 | elif(enc_type == "xor"): 71 | result = self.XOR(data) 72 | if self.input_type != "file": result.append("XOR"+decryption_function_call) 73 | return result 74 | def parse_args(): 75 | parser = argparse.ArgumentParser() 76 | parser.add_argument("-t", "--input-type", help="'text', 'file' or 'shellcode'.",choices=['text','file',"shellcode"],required=True,dest="input_type") 77 | parser.add_argument("-e","--encryption-type",help="'xor' or 'aes'.",dest="enc_type",required=True,choices=['xor','aes']) 78 | parser.add_argument("-i","--inputs",help="input filenames/texts separated by ','.",dest="inputs",required=True) 79 | parser.add_argument("-l","--key-length",help="XOR key length. default is '10'.",dest="key_length",default=10) 80 | return parser.parse_args() 81 | 82 | if __name__ == "__main__": 83 | 84 | options = parse_args() 85 | input_type = options.input_type 86 | key_length = int(options.key_length) 87 | inputs = options.inputs 88 | enc_type = options.enc_type 89 | 90 | encryption = Encryption(input_type,enc_type,key_length) 91 | 92 | output_obj = {"ciphertexts":[],"keys":[],"decryption_function_calls":[]} 93 | 94 | if(input_type == "text"): 95 | for text in inputs.split(','): 96 | result = encryption.encrypt(text) 97 | 98 | output_obj["ciphertexts"].append(result[0]) 99 | output_obj["keys"].append(result[1]) 100 | output_obj["decryption_function_calls"].append(result[2]) 101 | 102 | for key in list(output_obj.keys()): 103 | for item in output_obj[key]: 104 | with open("all.txt","a") as f: 105 | f.write(item) 106 | f.close() 107 | with open("all.txt","a") as f: 108 | f.write("\n") 109 | f.close() 110 | 111 | elif(input_type =="file" or input_type == "shellcode"): 112 | for file in inputs.split(','): 113 | with open(file,"rb") as f: 114 | result = encryption.encrypt(f.read()) 115 | if(input_type == "file"): 116 | print(f"file {file}.enc key:\n{result[1]}") 117 | with open(file+".enc","wb") as out_file: 118 | out_file.write(result[0]) 119 | else: 120 | print(result[0]) 121 | print(result[1]) 122 | print("\n") 123 | 124 | 125 | 126 | 127 | 128 | --------------------------------------------------------------------------------