C-Language and Python read methods for disk mirroring and native MBR

Source: Internet
Author: User
Tags fread

Source of Self Blog

The http://www.yingzinanfei.com/2016/09/21/cipanjingxianghebenjimbrdecyuyanhepythonduqufangfa/disk image format is an IMG format, The content is consistent with the physical disk. The first 512 bytes are MBR, the feature is 33c0 start, the 55AA ends up using the C language to read the MBR

#include <stdio.h>
void print (char c) {
    //convert character C to a two-bit 16-binary representation
    int m = c;
    int high = 0x000000f0, low = 0x0000000f;
    High &= m;
    High >>= 4;
    Low &= m;
    printf ("%1x%1x", high, Low);
}
int main () {
    file* fd = fopen ("/lab/vm01.img", "rb+");
    if (fd = NULL) {
        printf ("Failed to open img!\n");
        return 0;
    }
    Fseek (FD, 0, Seek_set); Seek_set indicates file header
    Char buffer[512] = {0};
    Fread (buffer, 1, FD);  Reads 512 bytes
    int i = 1;
    for (; I <= i++) {
        print (buffer[i-1]);   Call function Format output
        //increase spacing improve readability
        if (i = = 0) {
            printf ("\ n");
        }
        else if (i% 8 = 0) {
            printf ("    ");
        }
        else if (i% 4 = 0) {
            printf ("  ");
        }
        else if (i% 2 = 0) {
            printf ("");
        }
    }
    Fclose (FD);
    return 0;
}
using Python to read the MBR
FD = open ("\lab\vm01.img", "RB")
ans = fd.read (for
I, C in enumerate (ANS):
    print "%02x" Ord (c),    #将字符 Convert to 16 uppercase output
    if (i+1)% = = 0:
        print "\ n",
    elif (i+1)% 8 = 0:
        print "    ",
    elif (i+1)% 4 = 0:
  print "  ",
    elif (i+1)% 2 = 0:
        print "",
methods for direct reading of native MBR
#include <stdio.h>
#include <windows.h>

void print (char c) {
    int m = c;
    int high = 0x000000f0, low = 0x0000000f;
    High &= m;
    High >>= 4;
    Low &= m;
    printf ("%1x%1x", high, Low);
}


int main () {
    file* fd = fopen ("\\\\.\\physicaldrive0", "rb+");   This machine's disk address
    char buffer[512] = {0};
    Fseek (FD, 0, seek_set);
    Char buffer2[512] = {0};
    Fread (buffer2, 1, FD);
    int i = 1;
    for (; I <= i++) {
        print (buffer2[i-1]);
        if (i% = = 0) {
            printf ("\ n");
        }
        else if (i% 8 = 0) {
            printf ("    ");
        }
    }
    Fclose (FD);
    return 0;
}
The output sample is as follows:
33c0 8ed0 BC00 7CFB 5007 501F fcbe 1b7c bf1b 0650 57b9 E501 f3a4 cbbd BE07 B104 386E 007C 0975 1383 C510 e2f 4 CD18 8bf5 83c6 1049 7419 382C 74f6 a0b5 07b4 078B f0ac 3c00 74FC BB07 00b4 0ECD 10EB F288 4E10 E846 0073 2AF E 4610 807E 040B 740B 807E 040C 7405 a0b6 0775 D280 4602 0683 4608 0683 560A 00E8 2100 7305 a0b6 07EB BC81 3 EFE 7d55 AA74 0b80 7E10 0074 c8a0 B707 EBA9 8BFC 1E57 8bf5 cbbf 0500 8a56 00b4 08CD 1372 238A C124 3f98 8AD E 8AFC 43f7 e38b D186 d6b1 06d2 EE42 f7e2 3956 0a77 2372 0539 4608 731C B801 02BB 007C 8b4e 028B 5600 CD13 7 351 4f74 4E32 e48a 5600 CD13 EBE4 8a56 0060 Bbaa 55b4 41CD 1372 3681 FB55 AA75 30f6 C101 742B 6160 6a00 6a00 F 
F76 0AFF 7608 6a00 6800 7c6a 016A 10b4 428B f4cd 1361 6173 0e4f 740B 32E4 8a56 00CD 13EB D661 f9c3 496E 7661 6c69 6420 7061 7274 6974 696F 6E20 7461 626C 6500 4572 726F 7220 6c6f 6164 696E 6720 6f70 6572 6174 696E 67 7379 7374 656D 004D 6973 7369 6E67 206F 7065 7261 7469 6E67 2073 7973 7465 6d00 0000 0000 0000 0000 0000 0000 0000 0000 0000 00 00
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 002C 4463 4C6A 4c6a 0000 8001 0100 07FE FFFF 3f00 0000 14AC FF00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 55AA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.