IamLupo

2013-CRYPTO-150 | Draft

Nov 5th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.53 KB | None | 0 0
  1. /*
  2. Made by IamLupo!
  3.  
  4. Challange: https://backdoor.sdslabs.co/challenges/2013-CRYPTO-150
  5. */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  12.  
  13. /* Struct */
  14. typedef struct {
  15.     char*       data;
  16.     long int    data_length;
  17.    
  18.     int         positions[2];
  19. } pattern;
  20.  
  21. typedef struct {
  22.     char        name[50];
  23.     char        directory[50];
  24.    
  25.     char*       data;
  26.     long int    data_length;
  27.    
  28.     pattern*    patterns;
  29.     int         patterns_total;
  30.    
  31.     int*        keys_length;
  32.     int         keys_length_total;
  33.    
  34.     unsigned int*   key;
  35. } file;
  36.  
  37. void readData(file* cfile) {
  38.     FILE *fp;
  39.     int i;
  40.     char ch, filename[100];
  41.    
  42.     strcpy(filename, "");
  43.     strcat(filename, cfile->directory);
  44.     strcat(filename, cfile->name);
  45.    
  46.     fp = fopen(filename, "r"); // read mode
  47.    
  48.     if( fp == NULL ) {
  49.         printf("Error: could not open file!\n");
  50.         exit(EXIT_FAILURE);
  51.     }
  52.    
  53.     fseek(fp, 0, SEEK_END);
  54.     cfile->data_length = ftell(fp);
  55.     fseek(fp, 0, SEEK_SET);
  56.    
  57.     cfile->data = malloc(cfile->data_length * sizeof(char));
  58.    
  59.     i = 0;
  60.     while((ch = fgetc(fp) ) != EOF) {
  61.         cfile->data[i] = (char)ch;
  62.         i++;
  63.     }
  64.    
  65.     fclose(fp);
  66. }
  67.  
  68. /* Pattern */
  69. void addPattern(file* cfile, int data_length) {
  70.     cfile->patterns_total++;
  71.     cfile->patterns = realloc(cfile->patterns, cfile->patterns_total * sizeof(pattern));
  72.  
  73.     cfile->patterns[cfile->patterns_total - 1].data = malloc(data_length * sizeof(char));
  74. }
  75.  
  76. void findPatterns(file* cfile, int length) {
  77.     int i, j, id;
  78.    
  79.     //Init
  80.     cfile->patterns = malloc(0);
  81.     cfile->patterns_total = 0;
  82.    
  83.     for(i = 0; i < cfile->data_length - (length * 2); i++) {
  84.         for(j = i + length; j < cfile->data_length - length; j++) {
  85.             if(memcmp(&(cfile->data[i]), &(cfile->data[j]), length * sizeof(char)) == 0) {
  86.                 id = cfile->patterns_total;
  87.                
  88.                 //Create
  89.                 addPattern(cfile, length);
  90.                
  91.                 memcpy(cfile->patterns[id].data, &(cfile->data[i]), length);
  92.                 cfile->patterns[id].data_length = length;
  93.                 cfile->patterns[id].positions[0] = i;
  94.                 cfile->patterns[id].positions[1] = j;
  95.             }
  96.         }
  97.     }
  98. }
  99.  
  100.  
  101. /*
  102.     He also forgot which algorithm but was able to remember reading something like
  103.     0(func)0 = 1
  104.     0(func)1 = 0
  105.     1(func)0 = 0
  106.     1(func)1 = 1
  107. */
  108. unsigned int algorithem(unsigned int data, unsigned int key) {
  109.     int i;
  110.     unsigned int value = 0;
  111.    
  112.     for(i = 0; i < 8; i++) {
  113.         value += !(((data >> i) % 2) ^ ((key >> i) % 2)) << i;
  114.     }
  115.    
  116.     return value;
  117. }
  118.  
  119. void findKeyLength(file* cfile) {
  120.     int i, j, k;
  121.     pattern p;
  122.    
  123.     //Init
  124.     cfile->keys_length = malloc(0);
  125.     cfile->keys_length_total = 0;
  126.    
  127.     //Generate a list of key length by first pattern
  128.     p = cfile->patterns[0];
  129.     for(i = p.data_length; i < p.positions[1]; i++) {
  130.         if((p.positions[0] % i) == (p.positions[1] % i)) {
  131.             cfile->keys_length_total++;
  132.             cfile->keys_length = realloc(cfile->keys_length, cfile->keys_length_total * sizeof(int));
  133.            
  134.             cfile->keys_length[cfile->keys_length_total - 1] = i;
  135.         }
  136.     }
  137.    
  138.     //Check if other patterns also fits the key length
  139.     for(i = 1; i < cfile->patterns_total; i++) {
  140.         p = cfile->patterns[i];
  141.         for(j = 0; j < cfile->keys_length_total; j++) {
  142.             if((p.positions[0] % cfile->keys_length[j]) != (p.positions[1] % cfile->keys_length[j])) {
  143.                 //Remove Key Length
  144.                 for(k = j; k < cfile->keys_length_total - 1; k++) {
  145.                     cfile->keys_length[k] = cfile->keys_length[k + 1];
  146.                 }
  147.                 cfile->keys_length_total--;
  148.                 j--;
  149.             }
  150.         }
  151.     }
  152. }
  153.  
  154. void findFirstPartKey(file* cfile) {
  155.     char filename[8];
  156.     cfile->key = malloc(26 * sizeof(int));
  157.    
  158.     cfile->key[0] = algorithem(0x50, cfile->data[0]);
  159.     cfile->key[1] = algorithem(0x4b, cfile->data[1]);
  160.     cfile->key[2] = algorithem(0x03, cfile->data[2]);
  161.     cfile->key[3] = algorithem(0x04, cfile->data[3]);
  162.    
  163.    
  164.     printf("data[26] = %d\n", algorithem(cfile->data[26], cfile->key[0]));
  165.     printf("filename = %d\n", algorithem(cfile->data[27], cfile->key[1]));
  166.     printf("filename = %d\n", algorithem(cfile->data[28], cfile->key[2]));
  167.     printf("filename = %d\n", algorithem(cfile->data[29], cfile->key[3]));
  168.    
  169.     /*
  170.     filename[0] = algorithem(cfile->data[51], cfile->key[0]);
  171.     filename[1] = algorithem(cfile->data[52], cfile->key[1]);
  172.     filename[2] = algorithem(cfile->data[53], cfile->key[2]);
  173.     filename[3] = algorithem(cfile->data[54], cfile->key[3]);
  174.     filename[4] = '\0';
  175.    
  176.     printf("filename = %s\n", filename);
  177.     */
  178. }
  179.  
  180. void test(file* cfile) {
  181.     int g, h, i, j, finished, found, values;
  182.     unsigned int value;
  183.     char text[10000];
  184.    
  185.     for(g = 0; g < cfile->keys_length_total; g++) { //Length key
  186.         finished = 0;
  187.         for(h = 0; h <= cfile->keys_length[g]; h++) { //key position
  188.             for(i = 0; i < 0x255; i++) { //Key[h] = i
  189.                 found = 1;
  190.                 values = 0;
  191.                
  192.                 for(j = h; j < cfile->data_length; j += cfile->keys_length[g]) { //Encrypted data position
  193.                     value = algorithem(cfile->data[j], i);
  194.                     text[values] = value;
  195.                     values++;
  196.                 }
  197.                 text[values] = '\0';
  198.                
  199.                 if(found == 1 && h == 0) {
  200.                     printf("%d\n", i);
  201.                     finished++;
  202.                 }
  203.             }
  204.         }
  205.         if(cfile->keys_length[g] == finished)
  206.             printf("Finished = %d\n", finished);
  207.     }
  208. }
  209.  
  210. void test2(file* cfile) {
  211.     int i, j;
  212.     char key[2];
  213.    
  214.     int nbytes = 100;
  215.     char* my_string;
  216.    
  217.     key[1] = '\0';
  218.     my_string = (char *) malloc (nbytes + 1);
  219.    
  220.     /*
  221.         0   - 127 Empty
  222.         128 - 159 Lower case
  223.         160 - 191 Higher case
  224.         192 - 223 symboles
  225.         224 - 256 big resemblance as file
  226.     */
  227.    
  228.     for(i = 224; i < 256; i++) { // Decrypted Data
  229.         printf("Data %d\n", i);
  230.         for(j = 0; j < cfile->data_length; j++) {
  231.             key[0] = algorithem(cfile->data[j], i);
  232.            
  233.             if(key[0] >= 0x20 && key[0] <= 0x7A)
  234.                 printf("%s", key);
  235.             else
  236.                 printf(".");
  237.            
  238.             //if(j % 26 == 0)
  239.             //  printf("\n");
  240.         }
  241.         printf("\n------------------------------------------\n");
  242.         //printf("\n");
  243.         getline (&my_string, &nbytes, stdin);
  244.     }
  245. }
  246.  
  247. int main(int argc, char *argv[]) {
  248.     file cfile;
  249.    
  250.     //Step 1: Get the Data
  251.     strcpy(cfile.name,      "enc.enc");
  252.     strcpy(cfile.directory, "../Challange/");
  253.     readData(&cfile);
  254.    
  255.     //Step 2: Find Patterns in the data
  256.     findPatterns(&cfile, 4);
  257.    
  258.     //Step 3: Based on the Patterns we generate possible key lengths
  259.     findKeyLength(&cfile);
  260.    
  261.     /*
  262.         Step 4: We found that the key is possible 13 or 26 length.
  263.         With bit of research on file header we figure out that zip file has a filename length on 26e byte.
  264.         Followed with 28e byte the filename. We can brutefore the possebilities :D
  265.         File header: 0x04034b50
  266.        
  267.         NOT WORKING :( File name length is to long...
  268.     */
  269.     //findFirstPartKey(&cfile);
  270.    
  271.    
  272.     //Test
  273.     //test(&cfile);
  274.     test2(&cfile);
  275.    
  276.     return 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment