n0blinder

Simple XoR Encryption

Introduction to XOR Encryption

XOR encryption is a very simple algorithm to hide data. The idea is that you take some data you want to protect and use a secret key to transform it into something that only someone with the same key can understand.

A good analogy is a locked box: to see what’s inside, you need the right key. Without the key, the contents stay hidden.

For example, if our data is the letter A, and our secret key is the letter K, then applying the XOR operation transforms A into \n. Here, \n is the encrypted data, and A is the original plain text.

XOR is also called a symmetric encryption method, because the same process is used both to encrypt and to decrypt. If you apply XOR with the same key again, you recover the original data. In other words:

XOR at the Bit Level

XOR encryption works at the bit level. A byte is simply a group of 8 bits, so when we XOR two bytes, the operation is applied bit by bit.

Another way to say it: if the bits are the same, the result is 0. If the bits are different, the result is 1.

For example, let’s XOR the letter 'A' with the letter 'K':

Plain byte:  01000001   ('A')
Key byte:    01001011   ('K')
XOR result:  00001010   ('\n')

This shows that XOR is applied bit by bit, and each byte is just 8 XOR operations happening at once.

Now if we take the result 00001010 and apply XOR with the same key again:

00001010 ^ 01001011 = 01000001 (A)

We get back the original. That’s why XOR is called symmetric: the same operation with the same key both encrypts and decrypts.

Simple XoR Encryption

XoR Encryption in C

#include <stdio.h>

// encrypt "test"

unsigned char code[] = "test";  

int main() {
	char key = 's';
	int i = 0;
	for (i; i < sizeof(code); i++){
		printf("\\x%02x", code[i] ^ key);
	}
}

1. THE ARRAY - unsigned char code[] = "test";

0x74 0x65 0x73 0x74 0x00  't'   'e'   's'   't'  '\0' (string terminator)

So "sizeof(code) = 5", not "4" (because of the extra "\0").

2. The Encryption Key - char key = 's';

3. The loop

for (i; i < sizeof(code); i++){     
	printf("\\x%02x", code[i] ^ key); 
}

4. The output

Let’s actually work it out:

\x07\x16\x00\x07\x73

Simple XoR Decryption

XoR Decryption in C

#include <stdio.h>

// decrypt an encrypted "test"
unsigned char code[] = "\x07\x16\x00\x07\x73";
int main() {

	char key = 's';
	int i = 0;
	for (i; i < sizeof(code) - 1; i++) {

		code[i] = code[i] ^ key;
	}
	printf("Decrypted Code is: %s", code);
}

1. The data is already encrypted

unsigned char code[] = "\x07\x16\x00\x07\x73";

2. The loop “undoes” the XOR

for (i; i < sizeof(code) - 1; i++) {     
	code[i] = code[i] ^ key; 
}

3. Print as a string

printf("Decrypted Code is: %s", code);

Executing the Encrypted Payload

Same Encryption code and the a function to execute

#include <stdio.h>
#include <windows.h>

// encrypt "test"

unsigned char code[] = "test";  

int main() {
	char key = 's';
	int i = 0;
	for (i; i < sizeof(code); i++){
		code[i] = code[i]^key;
	}

	void *exec = VirtualAlloc(0, sizeof code, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
	memcpy(exec, code, sizeof code);
	((void(*)())exec)();
	return 0;
}

1. VOID *exec

void *exec

2. Allocating Memory Region

void *exec = VirtualAlloc(0, sizeof(code), MEM_COMMIT, PAGE_EXECUTE_READWRITE);

3. Next, we copy our encrypted code into that memory buffer

memcpy(exec, code, sizeof(code));

Copy the contents of your code array into that executable buffer.

4. VOID

((void (*)(void))exec)();

So, in one line: cast the allocated address to a callable function pointer, then call it.