Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Made by IamLupo!
- Challange: https://backdoor.sdslabs.co/challenges/2013-CRYPTO-150
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
- /* Struct */
- typedef struct {
- char* data;
- long int data_length;
- int positions[2];
- } pattern;
- typedef struct {
- char name[50];
- char directory[50];
- char* data;
- long int data_length;
- pattern* patterns;
- int patterns_total;
- int* keys_length;
- int keys_length_total;
- unsigned int* key;
- } file;
- void readData(file* cfile) {
- FILE *fp;
- int i;
- char ch, filename[100];
- strcpy(filename, "");
- strcat(filename, cfile->directory);
- strcat(filename, cfile->name);
- fp = fopen(filename, "r"); // read mode
- if( fp == NULL ) {
- printf("Error: could not open file!\n");
- exit(EXIT_FAILURE);
- }
- fseek(fp, 0, SEEK_END);
- cfile->data_length = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- cfile->data = malloc(cfile->data_length * sizeof(char));
- i = 0;
- while((ch = fgetc(fp) ) != EOF) {
- cfile->data[i] = (char)ch;
- i++;
- }
- fclose(fp);
- }
- /* Pattern */
- void addPattern(file* cfile, int data_length) {
- cfile->patterns_total++;
- cfile->patterns = realloc(cfile->patterns, cfile->patterns_total * sizeof(pattern));
- cfile->patterns[cfile->patterns_total - 1].data = malloc(data_length * sizeof(char));
- }
- void findPatterns(file* cfile, int length) {
- int i, j, id;
- //Init
- cfile->patterns = malloc(0);
- cfile->patterns_total = 0;
- for(i = 0; i < cfile->data_length - (length * 2); i++) {
- for(j = i + length; j < cfile->data_length - length; j++) {
- if(memcmp(&(cfile->data[i]), &(cfile->data[j]), length * sizeof(char)) == 0) {
- id = cfile->patterns_total;
- //Create
- addPattern(cfile, length);
- memcpy(cfile->patterns[id].data, &(cfile->data[i]), length);
- cfile->patterns[id].data_length = length;
- cfile->patterns[id].positions[0] = i;
- cfile->patterns[id].positions[1] = j;
- }
- }
- }
- }
- /*
- He also forgot which algorithm but was able to remember reading something like
- 0(func)0 = 1
- 0(func)1 = 0
- 1(func)0 = 0
- 1(func)1 = 1
- */
- unsigned int algorithem(unsigned int data, unsigned int key) {
- int i;
- unsigned int value = 0;
- for(i = 0; i < 8; i++) {
- value += !(((data >> i) % 2) ^ ((key >> i) % 2)) << i;
- }
- return value;
- }
- void findKeyLength(file* cfile) {
- int i, j, k;
- pattern p;
- //Init
- cfile->keys_length = malloc(0);
- cfile->keys_length_total = 0;
- //Generate a list of key length by first pattern
- p = cfile->patterns[0];
- for(i = p.data_length; i < p.positions[1]; i++) {
- if((p.positions[0] % i) == (p.positions[1] % i)) {
- cfile->keys_length_total++;
- cfile->keys_length = realloc(cfile->keys_length, cfile->keys_length_total * sizeof(int));
- cfile->keys_length[cfile->keys_length_total - 1] = i;
- }
- }
- //Check if other patterns also fits the key length
- for(i = 1; i < cfile->patterns_total; i++) {
- p = cfile->patterns[i];
- for(j = 0; j < cfile->keys_length_total; j++) {
- if((p.positions[0] % cfile->keys_length[j]) != (p.positions[1] % cfile->keys_length[j])) {
- //Remove Key Length
- for(k = j; k < cfile->keys_length_total - 1; k++) {
- cfile->keys_length[k] = cfile->keys_length[k + 1];
- }
- cfile->keys_length_total--;
- j--;
- }
- }
- }
- }
- void findFirstPartKey(file* cfile) {
- char filename[8];
- cfile->key = malloc(26 * sizeof(int));
- cfile->key[0] = algorithem(0x50, cfile->data[0]);
- cfile->key[1] = algorithem(0x4b, cfile->data[1]);
- cfile->key[2] = algorithem(0x03, cfile->data[2]);
- cfile->key[3] = algorithem(0x04, cfile->data[3]);
- printf("data[26] = %d\n", algorithem(cfile->data[26], cfile->key[0]));
- printf("filename = %d\n", algorithem(cfile->data[27], cfile->key[1]));
- printf("filename = %d\n", algorithem(cfile->data[28], cfile->key[2]));
- printf("filename = %d\n", algorithem(cfile->data[29], cfile->key[3]));
- /*
- filename[0] = algorithem(cfile->data[51], cfile->key[0]);
- filename[1] = algorithem(cfile->data[52], cfile->key[1]);
- filename[2] = algorithem(cfile->data[53], cfile->key[2]);
- filename[3] = algorithem(cfile->data[54], cfile->key[3]);
- filename[4] = '\0';
- printf("filename = %s\n", filename);
- */
- }
- void test(file* cfile) {
- int g, h, i, j, finished, found, values;
- unsigned int value;
- char text[10000];
- for(g = 0; g < cfile->keys_length_total; g++) { //Length key
- finished = 0;
- for(h = 0; h <= cfile->keys_length[g]; h++) { //key position
- for(i = 0; i < 0x255; i++) { //Key[h] = i
- found = 1;
- values = 0;
- for(j = h; j < cfile->data_length; j += cfile->keys_length[g]) { //Encrypted data position
- value = algorithem(cfile->data[j], i);
- text[values] = value;
- values++;
- }
- text[values] = '\0';
- if(found == 1 && h == 0) {
- printf("%d\n", i);
- finished++;
- }
- }
- }
- if(cfile->keys_length[g] == finished)
- printf("Finished = %d\n", finished);
- }
- }
- void test2(file* cfile) {
- int i, j;
- char key[2];
- int nbytes = 100;
- char* my_string;
- key[1] = '\0';
- my_string = (char *) malloc (nbytes + 1);
- /*
- 0 - 127 Empty
- 128 - 159 Lower case
- 160 - 191 Higher case
- 192 - 223 symboles
- 224 - 256 big resemblance as file
- */
- for(i = 224; i < 256; i++) { // Decrypted Data
- printf("Data %d\n", i);
- for(j = 0; j < cfile->data_length; j++) {
- key[0] = algorithem(cfile->data[j], i);
- if(key[0] >= 0x20 && key[0] <= 0x7A)
- printf("%s", key);
- else
- printf(".");
- //if(j % 26 == 0)
- // printf("\n");
- }
- printf("\n------------------------------------------\n");
- //printf("\n");
- getline (&my_string, &nbytes, stdin);
- }
- }
- int main(int argc, char *argv[]) {
- file cfile;
- //Step 1: Get the Data
- strcpy(cfile.name, "enc.enc");
- strcpy(cfile.directory, "../Challange/");
- readData(&cfile);
- //Step 2: Find Patterns in the data
- findPatterns(&cfile, 4);
- //Step 3: Based on the Patterns we generate possible key lengths
- findKeyLength(&cfile);
- /*
- Step 4: We found that the key is possible 13 or 26 length.
- With bit of research on file header we figure out that zip file has a filename length on 26e byte.
- Followed with 28e byte the filename. We can brutefore the possebilities :D
- File header: 0x04034b50
- NOT WORKING :( File name length is to long...
- */
- //findFirstPartKey(&cfile);
- //Test
- //test(&cfile);
- test2(&cfile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment