Whether you choose to statically include your shellcode in the binary or choose to stage it, encrypting it is important. This is because anti-virus and EDR solutions have static detections and signatures for for known malware.
There are 3 easy encryption/decryption methods that will be covered including:
XOR
RC4
AES
The XOR operation is often performed with the ^ symbol in most languages. XOR is an abbreviation of “Exlusive OR” and is an equation given 2 logical statements. The XOR function returns TRUE if one statement is true (1) and the other statement is false (0) meaning that these statements are exclusive to eachother.
#include<stdio.h>#include<string.h>voidxor(charkey,char*data,intlength){for(intx=0;x<length;x++){data[x]^=key;}}intmain(){charkey='z';chardata[]="Hello World";printf("Before XOR: %s\n",data);// encrypting data[] with XOR
xor(key,data,strlen(data));printf("After XOR: %s\n",data);// final XOR to restore to original
xor(key,data,strlen(data));printf("Restored with XOR: %s\n",data);}
RC4 or Rivest Cipher 4 is a symmetric stream cipher that produces a pseudo-random keystream based on a variable length key. Its extremely fast and super easy to implement with the Win32 api.
AES or Advanced Encryption Standard is a widely trusted and used encryption algorithm. It can be a bit difficult to learn at first, but its the best encryption algorithm out of the other 2 we talked about. I utilize the tiny AES project so in order to program this out, you will have to pull some included files down.
#include<stdio.h>#include<stdint.h>#include<string.h>// https://github.com/kokke/tiny-AES-c
#include"aes.h"#include"aes.c"intmain(void){// Key and IV HAVE to be 16 bytes long or this wont work.
uint8_tkey[16]={0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81};uint8_tiv[16]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x01,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};structAES_ctxctx;charmessage[]="Hello World";size_tlen=strlen(message);printf("Plaintext: %s\n",message);// initialize the AES context with the key and IV and encrypt the message
AES_init_ctx_iv(&ctx,key,iv);AES_CTR_xcrypt_buffer(&ctx,message,len);printf("Ciphertext: %s\n",(char*)message);// decrypt the message by reinitializing the AES context with the same key and IV
AES_ctx_set_iv(&ctx,iv);AES_CTR_xcrypt_buffer(&ctx,message,len);printf("Decrypted: %s\n",(char*)message);return0;}