MalDev Study
Core Module 17beginner4 code files

Payload Encryption - XOR

Demonstrating multiple ways of using XOR to encrypt a payload.

Jump to Section (4)

Learning Objectives

0/3 done

Introduction

XOR encryption is the simplest to use and the lightest to implement, making it a popular choice for malware. It is faster than AES and RC4 and does not require any additional libraries or the usage of Windows APIs. Additionally, it is a bidirectional encryption algorithm that allows the same function to be used for both encryption and decryption.

XOR Encryption

The code snippet below shows a basic XOR encryption function. The function simply XORs each byte of the shellcode with a 1-byte key.

CPP
/*
	- pShellcode : Base address of the payload to encrypt 
	- sShellcodeSize : The size of the payload 
	- bKey : A single arbitrary byte representing the key for encrypting the payload
*/
VOID XorByOneKey(IN PBYTE pShellcode, IN SIZE_T sShellcodeSize, IN BYTE bKey) {
	for (size_t i = 0; i < sShellcodeSize; i++){
		pShellcode[i] = pShellcode[i] ^ bKey;
	}
}

Securing The Encryption Key

Some tools and security solutions can brute force the key which will expose the decrypted shellcode. To make the process of guessing the key more difficult for these tools, the code below performs a minor change and increases the keyspace of the key by making i a part of the key. With keyspace much larger now, it's more difficult to brute force the key.

CPP
/*
	- pShellcode : Base address of the payload to encrypt 
	- sShellcodeSize : The size of the payload 
	- bKey : A single arbitrary byte representing the key for encrypting the payload
*/
VOID XorByiKeys(IN PBYTE pShellcode, IN SIZE_T sShellcodeSize, IN BYTE bKey) {
	for (size_t i = 0; i < sShellcodeSize; i++) {
		pShellcode[i] = pShellcode[i] ^ (bKey + i);
	}
}

The code snippet above can still be hardened further. The snippet below performs the encryption process with a key, using every byte of the key repeatedly making it harder to crack the key.

CPP
/*
	- pShellcode : Base address of the payload to encrypt 
	- sShellcodeSize : The size of the payload 
	- bKey : A random array of bytes of specific size
	- sKeySize : The size of the key
*/
VOID XorByInputKey(IN PBYTE pShellcode, IN SIZE_T sShellcodeSize, IN PBYTE bKey, IN SIZE_T sKeySize) {
	for (size_t i = 0, j = 0; i < sShellcodeSize; i++, j++) {
		if (j > sKeySize){
			j = 0;
		}
		pShellcode[i] = pShellcode[i] ^ bKey[j];
	}
}

Conclusion

It is recommended to utilize XOR encryption for small tasks, such as obscuring strings. However, for larger payloads, it is advised to use more secure encryption methods such as AES.

Diagram

Companion Project & Source Code

4 readable source/config files extracted directly from the archive

Download 17.Xor.zip
2.2 KB
// @NUL0x4C | @mrd0x : MalDevAcademy

#include <Windows.h>
#include <stdio.h>



// Example invocation: XorByOneKey(shellcode, sizeof(shellcode), 0xFA);
// Here the shellcode is a char array and 0xFA is the one byte key used
VOID XorByOneKey(IN PBYTE pShellcode, IN SIZE_T sShellcodeSize, IN BYTE bKey) {
	
	for (size_t i = 0; i < sShellcodeSize; i++){
		
		pShellcode[i] = pShellcode[i] ^ bKey;
	}

}


// Example invocation: XorByOneKey(shellcode, sizeof(shellcode), 0xFA);
// Here the shellcode is a char array and 0xFA is the one byte key used
VOID XorByiKeys(IN PBYTE pShellcode, IN SIZE_T sShellcodeSize, IN BYTE bKey) {

	for (size_t i = 0; i < sShellcodeSize; i++) {

		pShellcode[i] = pShellcode[i] ^ (bKey + i);
	}

}


// Example invocation: Check the main function
VOID XorByInputKey(IN PBYTE pShellcode, IN SIZE_T sShellcodeSize, IN PBYTE bKey, IN SIZE_T sKeySize) {

	for (size_t i = 0, j = 0; i < sShellcodeSize; i++, j++) {
		// if end of the key, start again 
		if (j > sKeySize)
		{
			j = 0;
		}
		pShellcode[i] = pShellcode[i] ^ bKey[j];
		
	}

}


unsigned char shellcode[] = { 
	"This is very spooky stuff" 
};

unsigned char key[] = { 
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05
};

int main() {
	// Printing some data
	printf("[i] shellcode : 0x%p \n", shellcode);

	// Encryption
	XorByInputKey(shellcode, sizeof(shellcode), key, sizeof(key));

	printf("[#] Press <Enter> To Decrypt ...");
	getchar();

	// Decryption
	XorByInputKey(shellcode, sizeof(shellcode), key, sizeof(key));

	// Printing the shellcode's string
	printf("[i] shellcode : \"%s\" \n", (char*)shellcode);


	// Exit
	printf("[#] Press <Enter> To Quit ...");
	getchar();
	return 0;

}

/*

// "XorByiKeys" usage example, the same thing goes for "XorByOneKey" usage

int main() {
	// Printing some data
	printf("[i] shellcode : 0x%p \n", shellcode);

	// Encryption, 0xF4 is the key
	XorByiKeys(shellcode, sizeof(shellcode), 0xF4);

	printf("[#] Press <Enter> To Decrypt ...");
	getchar();

	// Decryption, 0xF4 is the key
	XorByiKeys(shellcode, sizeof(shellcode), 0xF4);

	// Printing the shellcode's string
	printf("[i] shellcode : \"%s\" \n", (char*)shellcode);


	// Exit
	printf("[#] Press <Enter> To Quit ...");
	getchar();
	return 0;

}

*/
Full Archive Manifest (5 files inside .zip)
  • Xor/Xor.sln
  • Xor/Xor/XOR.c
  • Xor/Xor/Xor.vcxproj
  • Xor/Xor/Xor.vcxproj.filters
  • Xor/Xor/Xor.vcxproj.user

Search all modules and companion code

No matching modules or source files found for “”.
    Covers all 91 Core + 38 Advanced modules + source archives

    Reader Settings & Progress

    Color Theme

    Reading Font Size

    Study Progress & Cross-Device Backup

    0/129
    Completed
    0
    Bookmarked
    0
    Module Notes

    Progress is saved locally in your browser. Export a JSON backup to move your progress between laptop and phone.