Advertisement
xerpi

nidattack OpenMP

Jul 16th, 2018
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.72 KB | None | 0 0
  1. /*
  2. SHA1 dictionary attack program v2.0 (c) 2005 adresd
  3. based on original by djhuevo
  4.  
  5. hash searchtable and other mods by adresd
  6. performance checked by gorim
  7.  
  8. No License, public domain, do as you want, no warranties
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <stdlib.h>
  15.  
  16. typedef char s8;
  17. typedef short s16;
  18. typedef int s32;
  19. typedef unsigned char u8;
  20. typedef unsigned short u16;
  21. typedef unsigned int u32;
  22.  
  23. /*
  24. SHA-1 based on Steve Reid SHA1 code <steve@edmweb.com>
  25. */
  26.  
  27. // this was originally 64, but since ps3 adds 16 bytes
  28. // to every hash buffer it may need to be increased.
  29. // Note: The '128' size does not currently work -
  30. // would anyone like to fix it? :)
  31. #define MAX_HASH_BUFF       64
  32. //#define MAX_HASH_BUFF     128
  33.  
  34. #define LITTLE_ENDIAN
  35.  
  36. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  37.  
  38. /* blk0() and blk() perform the initial expand. */
  39. /* I got the idea of expanding during the round function from SSLeay */
  40. #ifdef LITTLE_ENDIAN
  41. #define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \
  42.     |(rol(block[i],8)&0x00FF00FF))
  43. #else
  44. #define blk0(i) block[i]
  45. #endif
  46. #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
  47.     ^block[(i+2)&15]^block[i&15],1))
  48.  
  49. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  50. #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  51. #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  52. #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  53. #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  54. #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  55.  
  56. u32 fastSHA1(unsigned char *buffer,int len) {
  57.     u8 buf[MAX_HASH_BUFF];
  58.     s32 i;
  59.     u32 a, b, c, d, e;
  60.     u32 *block;
  61.  
  62.     memcpy(buf, buffer, len);
  63.     block=(u32 *)buf;
  64.  
  65.     for(i=len+1; i<MAX_HASH_BUFF-1; i++) {
  66.       buf[i]=0x00;
  67.     }
  68.  
  69.     buf[len]=0x80;
  70.     buf[MAX_HASH_BUFF-2]=(len*8)>>8;
  71.     buf[MAX_HASH_BUFF-1]=(len*8)&0xff;
  72.  
  73.     a = 0x67452301;
  74.     b = 0xEFCDAB89;
  75.     c = 0x98BADCFE;
  76.     d = 0x10325476;
  77.     e = 0xC3D2E1F0;
  78.  
  79.     /* 4 rounds of 20 operations each. Loop unrolled. */
  80.     R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  81.     R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  82.     R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  83.     R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  84.     R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  85.     R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  86.     R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  87.     R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  88.     R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  89.     R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  90.     R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  91.     R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  92.     R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  93.     R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  94.     R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  95.     R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  96.     R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  97.     R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  98.     R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  99.     R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  100.  
  101.     a+=0x67452301;
  102.     return (a>>24)|((a>>8)&0xff00)|((a<<8)&0xff0000)|((a<<24));
  103. }
  104.  
  105. #define CUARTO_ORDEN
  106.  
  107. char **dict;
  108. char *dict_member_length;
  109. int dict_count;
  110. unsigned int *hash;
  111. int hash_count;
  112.  
  113. int load_hash_list(char *filename)
  114. {
  115.     int i;
  116.     unsigned int t;
  117.     FILE *fp;
  118.     printf("Reading hash file '%s'\n", filename);
  119.     if ((fp = fopen(filename, "rt")) == NULL)
  120.         return -1;
  121.     hash_count = 0;
  122.     while (fscanf(fp, "0x%x\n", &t) == 1)
  123.         hash_count++;
  124. printf("hash_count = %d\n", hash_count);
  125.     fseek(fp, 0, SEEK_SET);
  126.     hash = malloc(hash_count * sizeof(unsigned int));
  127.     i = 0;
  128.     while (fscanf(fp, "0x%x\n", &hash[i++]) == 1);
  129.     fclose(fp);
  130.     return 0;
  131. }
  132.  
  133. int load_dictionary(char *filename)
  134. {
  135.     int i;
  136.     char buffer[0x200];
  137.     FILE *fp;
  138.     printf("Reading dictionary file '%s'\n", filename);
  139.     if ((fp = fopen(filename, "rt")) == NULL)
  140.         return -1;
  141.     dict_count = 0;
  142.     while (fscanf(fp, "%s\n", buffer) != EOF)
  143.         dict_count++;
  144. printf("dict_count = %d\n", dict_count);
  145.     fseek(fp, 0, SEEK_SET);
  146.     dict = (char **) malloc(dict_count * sizeof(char *));
  147.     dict_member_length = malloc(dict_count);
  148.     i = 0;
  149.     while (fscanf(fp, "%s\n", buffer) == 1) {
  150.         if ((buffer[0] == '-') || (buffer[0] == '/')) { // This handles comments in the dic file
  151.         } else {                // Not a comment, so do duplicate check
  152.             int count;
  153.             int found = 0;
  154.             for (count = 0; count < (i - 1); count++)
  155.                 if (strcmp(buffer, dict[count]) == 0)
  156.                     found = 1;
  157.             if (found == 0) {   // If not already in dictionary, then add
  158.                 dict_member_length[i] = strlen(buffer);
  159.                 dict[i] = malloc(dict_member_length[i] + 1);
  160.                 strcpy(dict[i], buffer);
  161.                 i++;
  162.             }
  163.         }
  164.     }
  165.     dict_count = i;
  166.     fclose(fp);
  167.     return 0;
  168. }
  169.  
  170. FILE *fout;
  171.  
  172. // Hash seems to be where the program spends most of its time, so we need to speedup the
  173. // search somehow
  174. // 32 bit value, 4 bytes
  175. // what we do, is pre-sort the hashlist, so they are in order AABBCCDD
  176. // Then index each byte into the list, hence cutting down the search space considerably
  177. #define VER2_SIZE  256
  178. // midsearch should hopefully fit in cache, so will
  179. // help quite a lot with rejection testing.
  180. int midsearch[VER2_SIZE*VER2_SIZE];
  181. // bigsearch is really a cop out, will not fit in cache, but still quicker
  182. // than checking many elements.
  183. int bigsearch[VER2_SIZE*VER2_SIZE*VER2_SIZE];
  184.  
  185. void fillsearchtable()
  186. {
  187.   unsigned int searchval;
  188.   int hashpos,hashpos2,hashpos3;
  189.   int count,count2,count3;
  190.   // Firstly Sort the hashlist, order AABBCCDD
  191.   int found = 1;
  192.   printf("Sorting Hashlist\n");
  193.   while (found == 1)
  194.   {
  195.     found = 0;
  196.     for (count=0;count<(hash_count-1);count++)
  197.     {
  198.       if (hash[count] > hash[count+1])
  199.       { //  swap entries if wrong way around
  200.         unsigned int temp = hash[count+1];
  201.         hash[count+1] = hash[count];
  202.         hash[count] = temp;
  203.         found = 1;
  204.       }
  205.     }
  206.   }
  207.   printf("Clearing Search Tree\n");
  208.   // Really lazy slow clear array, but who cares only done once
  209.   for (count=0;count<(VER2_SIZE*VER2_SIZE);count++)
  210.   {
  211.     midsearch[count] = -1;
  212.   }
  213.   for (count=0;count<(VER2_SIZE*VER2_SIZE*VER2_SIZE);count++)
  214.   {
  215.     bigsearch[count] = -1;
  216.   }
  217.   printf("Building Search Tree\n");
  218.   // Now build the toplevel (first) byte
  219.   for (count=0;count<256;count++)
  220.   { //  Find the first firstbyte in the hashlist that matches this value
  221.     hashpos = 0;
  222.     searchval = (count<<24);
  223.     while ((hash[hashpos]&0xff000000) < searchval &&
  224.        hashpos < hash_count - 1) hashpos++;
  225.     if ((hash[hashpos]&0xff000000) == searchval)
  226.     {
  227.       // Now Search for a twobyte combo
  228.       for (count2=0;count2<256;count2++)
  229.       { //  Find the first secondbyte in the hashlist from this pos
  230.         hashpos2 = hashpos;
  231.         searchval = (count<<24) | (count2<<16);
  232.         while ((hash[hashpos2]&0xffff0000) < searchval &&
  233.            hashpos2 < hash_count - 1) hashpos2++;
  234.         if ((hash[hashpos2]&0xffff0000) == searchval)
  235.         { //  Add this entry
  236.           midsearch[(count<<8)+count2] = hashpos2;
  237.  
  238.           // Now Search for a threebyte combo
  239.           for (count3=0;count3<256;count3++)
  240.           { //  Find the first thirdbyte in the hashlist from this pos
  241.             hashpos3 = hashpos2;
  242.             searchval = (count<<24) | (count2<<16) | (count3<<8);
  243.             while ((hash[hashpos3]&0xffffff00) < searchval &&
  244.            hashpos3 < hash_count - 1) hashpos3++;
  245.             if ((hash[hashpos3]&0xffffff00) == searchval)
  246.             { //  Add this entry
  247.               bigsearch[(count<<16)+(count2<<8)+count3] = hashpos2;
  248.             }
  249.           }
  250.         }
  251.       }
  252.     }
  253.   }
  254. }
  255.  
  256. int findhash(char *buffer, int size, int isPS3)
  257. {
  258.   int h;
  259.   unsigned int hashvalue;
  260.   unsigned int index1;
  261.   unsigned int index2;
  262.   unsigned int index3;
  263.   u8 int_buff[MAX_HASH_BUFF];
  264.   u8* buff_ptr;
  265.   const u8 ps3_key[16] = {
  266.     0x67, 0x59, 0x65, 0x99, 0x04, 0x25, 0x04, 0x90,
  267.     0x56, 0x64, 0x27, 0x49, 0x94, 0x89, 0x74, 0x1A };
  268.  
  269.   if(isPS3)
  270.   {
  271.     buff_ptr = int_buff;
  272.     memcpy(int_buff, buffer, size);
  273.     memcpy(int_buff+size, ps3_key, 16);
  274.     size += 16;
  275.   }
  276.   else
  277.   {
  278.     buff_ptr = buffer;
  279.   }
  280.  
  281.   //fastSHA1 doesn't support strings larger than MAX_HASH_BUFF-1 bytes
  282.   //and chances are unlikely that function names are that
  283.   //long anyway so reject them before doing processing
  284.   if (size > MAX_HASH_BUFF-1)
  285.     return 0;
  286.   hashvalue = fastSHA1(buff_ptr, size);
  287.   index1 = (hashvalue & 0xff000000)>>24;
  288.   index2 = (hashvalue & 0x00ff0000)>>16;
  289.   index3 = (hashvalue & 0x0000ff00)>>8;
  290.   int pos;
  291.   //  Not sure if this really helps or not, should do.. on early rejection
  292.   //  memory accesses if nothing else
  293.   pos = midsearch[(index1<<8)+index2];
  294.   if (pos != -1)
  295.   {
  296.     // Get threebyte position
  297.     pos = bigsearch[(index1<<16)+(index2<<8)+index3];
  298.     if (pos != -1)
  299.     { // Found a position, so search from here
  300.       for(h=pos; h<hash_count; h++)
  301.       {
  302.         if (hashvalue >= hash[h])
  303.           if(hashvalue == hash[h])
  304.           { //  If equal, found
  305.        #pragma omp critical
  306.        {
  307.         printf("Found : 0x%08x  %s  \n",hashvalue,buffer);
  308.         fprintf(fout,"<FUNC><NID>0x%08x</NID><NAME>%s</NAME></FUNC>\n",hashvalue,buffer);
  309.        }
  310.             return 1;
  311.           }
  312.           else  //  If not, reject as above
  313.             return 0;
  314.       }
  315.     }
  316.   }
  317.   return 0;
  318. }
  319.  
  320. int main(int argc, char **argv)
  321. {
  322.     time_t start, end;
  323.     int is_ps3_flag = 0;
  324.  
  325.     char gbuffer[0x200];
  326.  
  327.     char *prefix = "";
  328.     int prefixlen = 0;
  329.  
  330.     printf("SHA1 hash dictionary attack v2.1 by adresd\n");
  331.     printf("based on the original by djhuevo\n");
  332.     printf("PS3 support added by xorloser\n");
  333.  
  334.     if (argc < 3) {
  335.         printf("usage:\n\t%s <hash_list> <dictionary> [-ps3] [prefix]\n", argv[0]);
  336.         return 1;
  337.     }
  338.  
  339.     if (load_hash_list(argv[1]) < 0) {
  340.         fprintf(stderr, "can't open the hash file %s\n", argv[1]);
  341.         return 2;
  342.     }
  343.  
  344.     if (load_dictionary(argv[2]) < 0) {
  345.         fprintf(stderr, "can't open the dictionary file %s\n", argv[2]);
  346.         return 3;
  347.     }
  348.  
  349.     if (argc > 3) {
  350.         if( !strcmp("-ps3", argv[3]) ) {
  351.             is_ps3_flag = 1;
  352.             if (argc > 4) {
  353.                 prefix = argv[4];
  354.                 strcpy(gbuffer, prefix);
  355.                 prefixlen = strlen(prefix);
  356.             }
  357.         }
  358.         else
  359.         {
  360.             prefix = argv[3];
  361.             strcpy(gbuffer, prefix);
  362.             prefixlen = strlen(prefix);
  363.         }
  364.     }
  365.  
  366.     if (hash_count < 1 || dict_count < 1) {
  367.         fprintf(stderr, "error on input data.\n");
  368.         fprintf(stderr, "hash_count = %d, dict_count = %d\n", hash_count, dict_count);
  369.         return 4;
  370.     }
  371.  
  372.     if ((fout = fopen("results.xml", "wba")) <= 0) {
  373.         printf("Failed to open output file\n");
  374.         exit(1);
  375.     }
  376.  
  377.     printf("\nprefix : '%s'\n", prefix != "" ? prefix : "<None>");
  378.     printf("hash count : %d\n", hash_count);
  379.     printf("dictionary words: %d\n\n", dict_count);
  380.  
  381.     fillsearchtable();
  382.  
  383.     printf("\nsearching...\n\n");
  384.     fflush(stdout);
  385.     time(&start);
  386.  
  387.     #pragma omp parallel
  388.     {
  389.         int i; // , j, h;
  390.         int x, y, z, zz; // , z2;
  391.         char *ptr, *ptr0, *ptr1, *ptr2, *ptr3; // , *ptr4;
  392.  
  393.         char buffer[0x200];
  394.         memcpy(buffer, gbuffer, sizeof(buffer));
  395.  
  396.         ptr = buffer + prefixlen;
  397.  
  398.         #pragma omp for
  399.         for (x = 0; x < dict_count; x++) {  // First Word
  400.             ptr0 = ptr;
  401.             for (i = 0; i < dict_member_length[x]; i++) {
  402.                 *(ptr0++) = dict[x][i];
  403.             }
  404.             *(ptr0) = 0;
  405.             #pragma omp critical
  406.             {
  407.                 printf("// processing word: %s\n", buffer);
  408.                 fflush(stdout);
  409.             }
  410.             // Second word
  411.             for (y = 0; y < dict_count; y++) {
  412.                 ptr1 = ptr0;
  413.                 for (i = 0; i < dict_member_length[y]; i++) {
  414.                     *(ptr1++) = dict[y][i];
  415.                 }
  416.                 *(ptr1) = 0;
  417.                 //printf("// processing word: %s\n",buffer);
  418.  
  419.                 // Only do next loop if first and second dont match
  420.                 if (strcmp(dict[x], dict[y]) != 0) {
  421.                     // Third Word
  422.                     for (z = 0; z < dict_count; z++) {
  423.                         ptr2 = ptr1;
  424.                         for (i = 0; i < dict_member_length[z]; i++) {
  425.                             *(ptr2++) = dict[z][i];
  426.                         }
  427.                         // Only do next loop if second and third dont match
  428.                         if (strcmp(dict[y], dict[z]) != 0) {
  429.                             // Fourth Word
  430.                             for (zz = 0; zz < dict_count; zz++) {
  431.                                 ptr3 = ptr2;
  432.                                 for (i = 0; i < dict_member_length[zz]; i++) {
  433.                                     *(ptr3++) = dict[zz][i];
  434.                                 }
  435. #if 0
  436.                                 // Only do next loop if third and fourth dont match
  437.                                 if (strcmp(dict[z], dict[zz]) != 0) {
  438.                                     // Fifth Word
  439.                                     for (z2 = 0; z2 < dict_count; z2++) {
  440.                                         ptr4 = ptr3;
  441.                                         for (i = 0; i < dict_member_length[z2]; i++) {
  442.                                             *(ptr4++) = dict[z2][i];
  443.                                         }
  444.                                         *(ptr4) = 0x00;
  445.                                         findhash(buffer, ptr4 - buffer, is_ps3_flag);
  446.                                     }
  447.                                 }
  448. #endif
  449.                                 *(ptr3) = 0x00;
  450.                                 findhash(buffer, ptr3 - buffer, is_ps3_flag);
  451.                             }
  452.                         }
  453.                         *(ptr2) = 0x00;
  454.                         findhash(buffer, ptr2 - buffer, is_ps3_flag);
  455.                     }
  456.                 }
  457.                 *(ptr1) = 0x00;
  458.                 findhash(buffer, ptr1 - buffer, is_ps3_flag);
  459.             }
  460.             *(ptr0) = 0x00;
  461.             findhash(buffer, ptr0 - buffer, is_ps3_flag);
  462.         }
  463.     }
  464.     time(&end);
  465.     printf("\n\nhash count : %d\n", hash_count);
  466.     printf("dictionary words: %d\n\n", dict_count);
  467.     printf("\n\ntime : %f seconds.\n", difftime(end, start));
  468.  
  469.     fclose(fout);
  470.     return 0;
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement