XOR strings for C code
This python code is useful for strings that you need to work with in C code but if Microsoft Defender sees them, your tool might get flagged. List each string in input.txt, one per line. Then run this python script. After that, output.txt will contain each XOR-encoded string. Those strings can then be defined in your C code. You will need a decode function in C. I included it below the Python code here.
# Set the key
key = 0xDD
# Set the input file containing the strings (one per line)
input_file = 'input.txt'
# Set the output file that will store the encoded strings
output_file = 'output.txt'
# Read in a list of strings to encode
with open(input_file, 'r', encoding='utf-8') as file:
strings = [string.strip() for string in file]
# Open the output file for writing
with open(output_file, 'w') as file:
# Read in each string and encode them saving the results to an output file
for string in strings:
encoded_string = [ord(character) ^ key for character in string]
hex_string = ', '.join([hex(byte) for byte in encoded_string])
file.write(f'// {string}\n')
file.write(f'{hex_string}, 0x00\n')
// Simple XOR function to encode and decode strings in memory
void xor_string(unsigned char* data, unsigned char key)
{
while (data && *data)
{
*data ^= key;
data++;
}
}
Don’t forget that you must match keys in C code with the key used in your Python code.
// Must match our offline encoding key
unsigned char key = 0xDD;