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

#define KEY 'X'I';

#include <stdio.h>
#include <stdlib.h>
/*#include TODO:<string.h>


- Dev this in C++ with a class "shellcode". 
          - Handle the nullbyte.
*/unsigned char*shellcodeEncXor(shellcodeXor(unsigned char*shellcode, char key){

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

char*shellcodeDecXor(unsigned char*shellcodeEnc, char key){

    char*shellcode = (char*)malloc(sizeof(shellcodeEnc));
    for(int i=0;i<sizeof(shellcodeEnc);i++)  shellcode[i] = shellcodeEnc[i] ^ key;
    return shellcode;shellcodeXored;
}

void*printShellcode(unsigned char*shellcode, int shellcodeLen)shellcode){
    for// (Print each char until the nullbyte
    for(int i=0;i<shellcodeLen;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 = 4;
    int scLen = sizeof(strlen(sc);

    // EncXOR process
    unsigned char*scEncscXor = shellcodeEncXor(shellcodeXor(sc, key);
    printShellcode(scEnc, scLen)scXor);

    // DecXOR processagain (to unxor)
    unsigned char*scDec = shellcodeDecXor(scEnc,shellcodeXor(scXor, key);
    printShellcode(scDec, scLen)scDec);

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

    free(scEnc);
    free(scDec);

    return 0;
}