You encounter a bot meditating in the park. He opens his cameras and begins to speak.
“Hear the word of RNGesus. Complexity is the enemy of security. Let your encryption be as simple as possible, as to secure it, thusly”. He hands you a flyer with a snippet of code. “Secure every message you have with it. Only those who see can enter.”
What do you think? Is it simple enough to be secure?
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain() {
char secret[] ="brck{not_the_flag}";
char*key = NULL;
size_t read_length, buffer_length =0;
// Read One Time Key
FILE *random_bytes =fopen("/dev/urandom", "r"); //incassable comme d'hab
read_length =getline(&key, &buffer_length, random_bytes);
fclose(random_bytes);
// Encrypt
for (int i =0; i <strlen(secret); i++)
secret[i] = secret[i] ^ key[i%read_length];
// Return encrypted secret
printf("%s", secret);
free(key);
return0;
}
Recon
Reading man for getline() (thanks to admin adivce), we understand that getline take bytes from a file until a \n or 0x0A bytes is encoutered.
So each time we connect to the server we got a ciphertext xored with a key from /dev/urandom and it’s length can vary it only depends of the presence of 0x0A bytes. So the key can have a length of 1 or even 500.
The solution is to request a server until the key is short enoguh, (1 or 2 bytes).
And we can know that by xoring 1st character of the known plaintext which is 'b' from 'brck{' and comparing it with the xor of the 2nd known character with the 2nd character of the ciphertext.