Advertisement
Guest User

3DS CFW - New3DS signature check patchs

a guest
Mar 17th, 2015
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int slotmagic() {
  6.     return 0; // insert 7.X KEYX
  7. }
  8.  
  9. // this is not super stable but works, just a few things to check
  10. // i won't release it by the way as i don't want more piracy
  11. // to be used with ninjhax for now
  12. int main(int argc, char *argv[]) {
  13.     FILE *f;
  14.     u8 *save_buf, *out_buf, *xorpad_buf = NULL;
  15.     u8 zerobuf[0x10];
  16.     unsigned int size=0, xorpad_size = 0x0;
  17.     int i;
  18.     int enable_wearlevel = 1, enable_xorpad = 1;
  19.     int load_xorpad = 0;
  20.  
  21.     int fargc = 1, argi = 1;
  22.     char **fargv;
  23.     struct stat filestat;
  24.     char xorpad_path[256];
  25.  
  26.     // overwriting firm
  27.     f = fopen(argv[1], "rb");
  28.     if(f == NULL) {
  29.         fprintf(stderr, "error: failed to open %s\n", argv[1]);
  30.         return -1;
  31.     }
  32.  
  33.     fseek(f, 0, SEEK_END);
  34.     size = ftell(f);
  35.     fseek(f, 0, SEEK_SET);
  36.     save_buf = malloc(size);
  37.     out_buf = malloc(size);
  38.     fread(save_buf, size, 1, f);
  39.  
  40.     fclose(f);
  41.  
  42.     memset(xorpad_path, 0, 256);
  43.  
  44.     for(i = 1; i < argc - 1; i++) {
  45.         if(strncmp(argv[i + 1], "--", 2))fargc++;
  46.     }
  47.  
  48.     fargv = (char **) malloc(fargc * sizeof(char *));
  49.  
  50.     fargv[0] = argv[0];
  51.  
  52.     for(i = 1; i < argc - 1; i++) {
  53. #ifdef DEBUG
  54.         printf("arg: '%s'\n", argv[i + 1]);
  55. #endif
  56.         if(strncmp(argv[i + 1], "--nowear", 8)==0) {
  57.             enable_wearlevel = 0;
  58.         }
  59.         else if(strncmp(argv[i + 1], "--xorpad=", 9)==0) {
  60.             load_xorpad = 1;
  61.             strncpy(xorpad_path, &argv[i + 1][9], 255);
  62.         }
  63.         else {
  64.             fargv[argi] = argv[i + 1];
  65.             argi++;
  66.         }
  67.     }
  68.  
  69.     if(enable_wearlevel) {
  70.         if(rearrange(save_buf, out_buf, size) != 0) {
  71.             free(save_buf);
  72.             free(out_buf);
  73.             return -2;
  74.         }
  75.         else {
  76.             size -= 0x2000;
  77.         }
  78.     }
  79.     else {
  80.         memcpy(out_buf, save_buf, size);
  81.     }
  82.  
  83.     memset(zerobuf, 0, 0x10);
  84.     if(memcmp(&out_buf[0x10], zerobuf, 0x10)==0)enable_xorpad = 0;
  85.  
  86.     f = fopen("rawimage.bin", "wb");
  87.     fwrite(out_buf, 1, size, f);
  88.     fclose(f);
  89.  
  90.     // patch rsa check
  91.     if(enable_xorpad) {
  92.         if(load_xorpad) {
  93.             if(stat(xorpad_path, &filestat)==-1) {
  94.                 fprintf(stderr, "error: failed to stat %s\n", xorpad_path);
  95.                 free(save_buf);
  96.                 free(out_buf);
  97.                 return -1;
  98.             }
  99.             xorpad_size = (unsigned int)filestat.st_size;
  100.  
  101.             xorpad_buf = (u8*)malloc(xorpad_size);
  102.             if(xorpad_buf==NULL) {
  103.                 fprintf(stderr, "error: failed to allocate xorpad buffer.\n");
  104.                 free(save_buf);
  105.                 free(out_buf);
  106.                 return -1;
  107.             }
  108.             memset(xorpad_buf, 0, xorpad_size);
  109.  
  110.             f = fopen(xorpad_path, "rb");
  111.             fread(xorpad_buf, 1, xorpad_size, f);
  112.             fclose(f);
  113.         }
  114.         else
  115.         {
  116.             xorpad_size = 0x200;
  117.             xorpad_buf = (u8*)malloc(xorpad_size);
  118.             if(xorpad_buf==NULL) {
  119.                 fprintf(stderr, "error: failed to allocate xorpad buffer.\n");
  120.                 free(save_buf);
  121.                 free(out_buf);
  122.                 return -1;
  123.             }
  124.             memset(xorpad_buf, 0, xorpad_size);
  125.  
  126.             if(find_key(out_buf, size, xorpad_buf) == -1) {
  127.                 fprintf(stderr, "error: could not find xorpad block :(\n");
  128.                 free(save_buf);
  129.                 free(out_buf);
  130.                 return -1;
  131.             }
  132.         }
  133.  
  134.         xor(out_buf, size, NULL, xorpad_buf, xorpad_size);
  135.        
  136.     }
  137.     else
  138.     {
  139.         xorpad_size = 0x200;
  140.         xorpad_buf = (u8*)malloc(xorpad_size);
  141.         if(xorpad_buf==NULL) {
  142.             fprintf(stderr, "error: failed to allocate xorpad buffer.\n");
  143.             free(save_buf);
  144.             free(out_buf);
  145.             return -1;
  146.         }
  147.         memset(xorpad_buf, 0, xorpad_size);
  148.     }
  149.  
  150.     f = fopen("logical.bin", "wb");
  151.     fwrite(out_buf, 1, size, f);
  152.     fclose(f);
  153.  
  154. #ifdef DEBUG
  155.     printf("** FUSE GO! **\n");
  156. #endif
  157.  
  158.     return fuse_sav_init(out_buf, size, xorpad_buf, xorpad_size, fargc, fargv);
  159. }
  160.  
  161. int find_key(u8 *buf, size_t len, u8 *out) {
  162.     int i, j, count=0, found=0, rec_idx=0, rec_count=0;
  163.     hash_entry **hash_list;
  164.     u8 hash[16];
  165.  
  166.     u8 ff_hash[16]="\xde\x03\xfe\x65\xa6\x76\x5c\xaa\x8c\x91\x34\x3a\xcc\x62\xcf\xfc";
  167.  
  168.     hash_list = malloc(sizeof(hash_entry*) * ((len / 0x200)+1));
  169.     memset(hash_list, 0, sizeof(hash_entry*) * ((len / 0x200)+1));
  170.  
  171.     for(i = 0; i < (len / 0x200); i++) {
  172.         md5_buf(buf + (i*0x200), hash, 0x200);
  173.  
  174.         if(memcmp(hash, ff_hash, 16) == 0)
  175.             continue;
  176.  
  177.         found = 0;
  178.  
  179.         for(j = 0; j < count; j++) {
  180.             if (memcmp(hash_list[j]->hash, hash, 16) == 0) {
  181.                 hash_list[j]->count++;
  182.                 found = 1;
  183.                 break;
  184.             }
  185.         }
  186.  
  187.         // push new hashlist entry
  188.         if(found == 0) {
  189.             hash_list[count] = malloc(sizeof(hash_entry));
  190.             memcpy(hash_list[count]->hash, hash, 16);
  191.             hash_list[count]->count = 1;
  192.             hash_list[count]->block_idx = i;
  193.             count++;
  194.         }
  195.     }
  196.  
  197.     // this is a troll by the way
  198.     // thanks to yellows8 for the random code
  199.     for(i = 0; i < count; i++) {
  200.         if (hash_list[i]->count > rec_count) {
  201.             rec_count = hash_list[i]->count;
  202.             rec_idx = i;
  203.         }
  204.     }
  205.  
  206.     if (rec_count == 0)
  207.         return -1;
  208.  
  209. #ifdef DEBUG
  210.     printf("key hash: "); md5_print(hash_list[rec_idx]->hash); printf("\n");
  211. #endif
  212.  
  213.     memcpy(out, buf + (hash_list[rec_idx]->block_idx * 0x200), 0x200);
  214.  
  215.     for(i = 0; i < count; i++)free(hash_list[i]);
  216.     free(hash_list);
  217.  
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement