Skip to main content

[C] XOR Encryption

Introduction

Le OU exclusif, ou le XOR, est très souvent utilisé dans les algorithmes de chiffrement et les logiciels malveillants.

C'est pourquoi je partage un fonction permettant de chiffrer en XOR.

image.png

Code

Voici un simple XOR avec un simple caractère :

#define KEY 'I';

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


unsigned char*shellcodeXor(unsigned char*shellcode, char key){

    char*shellcodeXored = (char*)malloc(sizeof(shellcode));
    // Xor each char until the nullbyte
    for(int i=0;i<shellcode[i];i++) shellcodeXored[i] = shellcode[i] ^ key;
    return shellcodeXored;
}

void*printShellcode(unsigned char*shellcode){
    // Print each char until the nullbyte
    for(int i=0;shellcode[i];i++) printf("\\x%x", shellcode[i]);
    printf("\n");
}

int main(){
    
    char key = KEY;
	unsigned char sc[] = {"\x48\x31\xc9\x48\x48"};
    int scLen = strlen(sc);

    // XOR process
    unsigned char*scXor = shellcodeXor(sc, key);
    printShellcode(scXor);

    // XOR again (to unxor)
    unsigned char*scDec = shellcodeXor(scXor, key);
    printShellcode(scDec);

    printf("Sizeof shellcode : [%d]\n", scLen);

    free(scDec);

    return 0;
}