Skip to main content

Simple Encryptor

On our regular checkups of our secret flag storage server we found out that we were hit by ransomware! The original flag data is nowhere to be found, but luckily we not only have the encrypted file but also the encryption program itself.

Solution:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>

void decrypt()
{
FILE *fp;
char *flag;
unsigned char *flag_enc;
uint32_t seed;
uint32_t rand_num;
uint32_t rand_num2;

fp = fopen("flag.enc", "rb");
fseek(fp, 0, SEEK_END);
int size = ftell(fp);
fseek(fp, 0, SEEK_SET);
flag_enc = malloc(size);
fread(flag_enc, size, 1, fp);
fclose(fp);

// first 4 bytes is seed
memcpy(&seed, flag_enc, 4);
srand(seed);

// decrypt
for (int i = 4; i < size; i++)
{
rand_num = rand();
rand_num2 = rand() & 7;

flag_enc[i] = flag_enc[i] >> (rand_num2) | flag_enc[i] << (8 - rand_num2);
flag_enc[i] = flag_enc[i] ^ rand_num;
}

printf("\n%s\n", flag_enc + 4);
}

int main()
{
decrypt();
return 0;
}

After running the code you will get the flag HTB{vRy_s1MplE_F1LE3nCryp0r}