Advertisement
fishgop

Untitled

Jun 1st, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#include <aes.h>
  2.  
  3.  
  4. /*
  5.  * Verschlüsselungsverfahren zum entschlüsseln Camellia-128-CBC
  6.  * Byte Nummer 3
  7.  * Verschlüsselungsverfahren zum verschlüsseln MD5
  8.  */
  9.  
  10. static char* SRC_CIPHER_BIN_PATH = "s70307-src-cipher.bin";
  11. static char* CORRUPT_SRC_KEY_PATH = "s70307-corrupt-src-key.bin";
  12. static char* DEST_CIPHER_BIN_PATH = "s70307-dest-key.bin";
  13. static char* DECRYPT_FILE_PDF_PATH = "decrypted-s70307.pdf";
  14.  
  15. int CORRUPT_BYTE_POSITION = 3;
  16.  
  17. unsigned char* SRC_CIPHER_BIN_TEXT, CORRUPT_SRC_KEY_TEXT;
  18. char* DECRYPTED_CIPHER;
  19.  
  20. unsigned int getFileSize(FILE* data);
  21. unsigned char* readInFile(FILE *data, char* name);
  22. int decrypt(unsigned char* cipher, int clen, unsigned char iv[EVP_MAX_IV_LENGTH], unsigned char* key, FILE *decrypted_file);
  23.  
  24. int bruteForce(unsigned char* cipher,int clen, unsigned char iv[EVP_MAX_IV_LENGTH], unsigned char* key, FILE *decrypted_file);
  25.  
  26.  
  27.  
  28. int main(int argc, char* argv[]) {
  29.    
  30.     FILE *src_cipher, *cipher_dest, *corrupt_key, *decrypted_file;
  31.    
  32.     unsigned char* cipher;
  33.     int cipher_len;
  34.    
  35.    
  36.     unsigned char iv[EVP_MAX_IV_LENGTH];
  37.    
  38.     unsigned char key[EVP_MAX_KEY_LENGTH];
  39.     unsigned int key_len;
  40.  
  41.     /*  Check for correct number of values
  42.     if (argc < 5)
  43.     {
  44.         fprintf(stderr, "Usage: %s <snumber> <out>\n", argv[0]);
  45.         exit(EXIT_FAILURE);
  46.     }
  47.     */
  48.    
  49.     /*Opening files binary coded*/
  50.    
  51.     /* Open cipher file */
  52.     src_cipher = fopen(SRC_CIPHER_BIN_PATH, "rb");
  53.     if (!src_cipher)
  54.     {
  55.         fprintf(stderr, "Failed to load %s\n", SRC_CIPHER_BIN_PATH);
  56.         exit(EXIT_FAILURE);
  57.     }
  58.    
  59.     /* Saving cipher */
  60.     cipher = readInFile(src_cipher, SRC_CIPHER_BIN_PATH);
  61.     cipher_len = getFileSize(src_cipher);
  62.     //printf("Size of cipher: %i\n", cipher_len);
  63.    
  64.     /* Open cipher file */
  65.     corrupt_key = fopen(CORRUPT_SRC_KEY_PATH, "rb");
  66.     if (!corrupt_key)
  67.     {
  68.         fprintf(stderr, "Failed to load %s\n", CORRUPT_SRC_KEY_PATH);
  69.         exit(EXIT_FAILURE);
  70.     }
  71.    
  72.    
  73.    
  74.     /* Saving key for decrypting
  75.     key = malloc(EVP_CIPHER_key_length(EVP_camellia_128_cbc()));
  76.    
  77.     printf("%d", EVP_CIPHER_key_length(EVP_camellia_128_cbc()));*/
  78.    
  79.     /* Lese die Länge des Session Keys
  80.     if(fread(&key_len_ln, sizeof(key_len_ln), 1, corrupt_key) != 1)
  81.     {
  82.         fprintf(stderr, "Einlesen der Keylaenge.\n");
  83.     }
  84.     printf("DKey Len:: %u\n", key_len_ln);
  85.     key_len = ntohl(key_len_ln);*/
  86.    
  87.     if(fread(key, EVP_CIPHER_key_length(EVP_camellia_128_cbc()), 1, corrupt_key) != 1)
  88.     {
  89.         fprintf(stderr, "einlesen des keys");
  90.  
  91.     }
  92.    
  93.     if (sizeof(key) < EVP_CIPHER_key_length(EVP_camellia_128_cbc())) {
  94.         fprintf(stderr, "Decryption failed - wrong length of used key.\n");
  95.         exit(EXIT_FAILURE);
  96.  
  97.     }
  98.     key_len = sizeof(key);
  99.    
  100.    
  101.     if(fread(iv, EVP_CIPHER_iv_length(EVP_camellia_128_cbc()), 1, corrupt_key) != 1)
  102.     {
  103.         fprintf(stderr, "Lesen des Eingabestreams/-datei fehlgeschlagen.\n");
  104.     }
  105.    
  106.     printf("Initialization vector: %lu\n", sizeof(iv));
  107.  
  108.    
  109.    
  110.     decrypted_file = fopen(DECRYPT_FILE_PDF_PATH, "wb");
  111.     if (!decrypted_file)
  112.     {
  113.         fprintf(stderr, "Failed to load %s\n", DECRYPT_FILE_PDF_PATH);
  114.         exit(EXIT_FAILURE);
  115.     }
  116.    
  117.     //decrypt(cipher, cipher_len, iv, key, decrypted_file);
  118.    
  119.    
  120.    
  121.     bruteForce(cipher, cipher_len, iv, key, decrypted_file);
  122.    
  123.  
  124.     fclose(src_cipher);
  125.     fclose(corrupt_key);
  126.    
  127.    
  128.    
  129.    
  130.     printf("test: %d \n",  EVP_CIPHER_iv_length(EVP_camellia_128_cbc()));
  131.     return 1;
  132. }
  133.  
  134.  
  135.  
  136. int bruteForce(unsigned char* cipher,int clen, unsigned char iv[EVP_MAX_IV_LENGTH], unsigned char key[EVP_MAX_KEY_LENGTH], FILE *decrypted_file){
  137.    
  138.     int i;
  139.    
  140.     printf("Starting to bruteforce the coorect decrypting key...\n");
  141.     for (i = 0; i < 256; i++) {
  142.        
  143.         key[CORRUPT_BYTE_POSITION - 0] = i;
  144.         printf("%d = %c\n", i, i);
  145.         //printf("Current key is tested: %s", key);
  146.         if(decrypt(cipher, clen, iv, key, decrypted_file) == 1 ){
  147.             printf("[6] Bruteforcing was successfully\n");
  148.             printf("[7] Match for corrupted byte was found. Which is byte: %d - character: %c \n",key[i],key[i]);
  149.             return 1;
  150.         }
  151.        
  152.     }
  153.    
  154.     return 0;
  155. }
  156.  
  157. int decrypt(unsigned char* cipher, int clen, unsigned char iv[EVP_MAX_IV_LENGTH], unsigned char key[EVP_MAX_KEY_LENGTH], FILE *decrypted_file){
  158.    
  159.     unsigned char buffer[1024];
  160.     unsigned char* buffer_out;
  161.    
  162.     int length = 16;
  163.     int len_out = 0;
  164.     printf("---------processing---------\n");
  165.  
  166.     EVP_CIPHER_CTX ctx;
  167.     printf("[1] CTX struct initialized\n");
  168.  
  169.  
  170.     /* EVP Chiffre-Kontext initialisieren */
  171.     //EVP_CIPHER_CTX_init(&ctx);
  172.    
  173.    
  174.     if( EVP_DecryptInit_ex(&ctx, EVP_camellia_128_cbc(), NULL, key, iv) == 0){
  175.         EVP_CIPHER_CTX_cleanup(&ctx);
  176.         fprintf(stderr,"Error while initializing decryption framework\n");
  177.         return 0;
  178.     }
  179.     printf("[2] Decryption initated\n");
  180.    
  181.     if(!(buffer_out = malloc(clen*3+1))){
  182.         printf("tmp_out speicherreservierung hat ni geklappt!\n");
  183.     }
  184.      printf("%i",clen);
  185.     if( EVP_DecryptUpdate(&ctx, buffer_out, &len_out, cipher, clen) == 0){
  186.         EVP_CIPHER_CTX_cleanup(&ctx);
  187.         fprintf(stderr,"Encryption of the current block failed");
  188.         return 0;
  189.     }
  190.    
  191.    
  192.     printf("[3] Using DecryptUpdate and trying to decrypt.\n");
  193.    
  194.     if(len_out == 0 || EVP_DecryptFinal_ex(&ctx, buffer_out+len_out, &length) == 0){
  195.         EVP_CIPHER_CTX_cleanup(&ctx);
  196.         fprintf(stderr,"Encryption and writing to file failed.\n");
  197.         return 0;
  198.     }
  199.    
  200.     printf("[4] DecryptFinal: Decryption sucessfully with a plaintext length: %lu\n\tplaintext:", sizeof(buffer_out));
  201.    
  202.     /*int d;
  203.     for (d = 0; d < len_out+1000; ++d) {
  204.        printf("%i ", buffer_out[d]);
  205.     }
  206.     printf("\n%i", d);*/
  207.  
  208.  
  209.  
  210.    
  211.    // printf("The key for decryption is: %s\n", key);
  212.    
  213.    /* if (fwrite(buffer_out, len_out, 1, decrypted_file) != 1)
  214.     {
  215.         fprintf(stderr, "Writing to disk failed.\n");
  216.         EVP_CIPHER_CTX_cleanup(&ctx);
  217.         fclose(decrypted_file);
  218.     }*/
  219.    
  220.     int c;
  221.     for (c = 0; c < len_out; ++c) {
  222.         fputc(buffer_out[c], decrypted_file);
  223.     }
  224.  
  225.    
  226.     fclose(decrypted_file);
  227.     printf("[5] Created a .pdf file with a size of %i\n", len_out);
  228.  
  229.    
  230.     return 1;
  231.    
  232. }
  233.  
  234.  
  235. unsigned int getFileSize(FILE* data){
  236.    
  237.     int fileSize = 0;
  238.    
  239.     fseek(data, 0L, SEEK_END);
  240.     fileSize = ftell(data);
  241.     rewind(data);
  242.    
  243.     return fileSize;
  244. }
  245.  
  246. /*
  247.  *      Function to read in the file data
  248.  */
  249. unsigned char* readInFile(FILE *data, char* name){
  250.    
  251.     unsigned int data_length = 0;
  252.     data_length = getFileSize(data);
  253.    
  254.     unsigned char *buff = NULL;
  255.    
  256.     buff = malloc(sizeof(unsigned char)*data_length);
  257.     if(buff==NULL)
  258.     {
  259.         fprintf(stderr,"error reading file");
  260.     }
  261.    
  262.     fread(buff,sizeof(char),data_length,data);
  263.     fprintf(stderr,"File: %s\t - read succesfully\n",name );
  264.     return buff;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement