您的位置:首页 > 运维架构

how to access and operate a binarry file ?

2016-06-15 19:32 405 查看
In some exception case, we need to check and modified a binary file byte by byte , in a Windows-like OS, we could use some tools like UtralEdit and so on.

but in a Linux/Unix-like OS, how can we do this ?

I suggest VIM.

VIM is a smart and powerful and flexible tool , it can do too much more things than our cognition.

here I'll show you how to use Vim to read and modified a binary file.

first, open the binary file by vim :

       vim  -b  test.bin

    at this this moment,  you'll see some digital gibberish like below:

         QFI<fb>^@^@^@^C^@^@^@^@^@^@^A^H^@^@^@*^@^@^@^P^@^@^@^B@^@^@^@^@^@^@^@^@^@^@^R^@^@^@^@^@^C^@^@^@^@^@^@^@^A^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^    @^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^D^@^@^@hh^C<f8>W^@^@^@<90>^@^@dirty
bit^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^Acorrupt bit^@^@    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@lazy refcounts^@^@^@^

...

then, type "%!xxd" in Vim's command mode, you'll see the digital gibberish been converted to hex and showed like below:

    1 0000000: 5146 49fb 0000 0003 0000 0000 0000 0108  QFI.............

    2 0000010: 0000 002a 0000 0010 0000 0002 4000 0000  ...*........@...

    3 0000020: 0000 0000 0000 0012 0000 0000 0003 0000  ................

    4 0000030: 0000 0000 0001 0000 0000 0001 0000 0000  ................

    5 0000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................

    6 0000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................

    7 0000060: 0000 0004 0000 0068 6803 f857 0000 0090  .......hh..W....

    8 0000070: 0000 6469 7274 7920 6269 7400 0000 0000  ..dirty bit.....

    9 0000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   10 0000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   11 00000a0: 0001 636f 7272 7570 7420 6269 7400 0000  ..corrupt bit...

   12 00000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   13 00000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   14 00000d0: 0100 6c61 7a79 2072 6566 636f 756e 7473  ..lazy refcounts

   15 00000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   16 00000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   17 0000100: 0000 0000 0000 0000 2f72 6f6f 742f 7465  ......../root/te

   18 0000110: 7374 2f69 6d61 6765 2f69 6133 3265 5f72  st/image/ia32e_r

   19 0000120: 6865 6c37 7532 5f63 7075 3230 3036 2e69  hel7u2_cpu2006.i

   20 0000130: 6d67 0000 0000 0000 0000 0000 0000 0000  mg..............

now, you can modify the hex words in the Vim's edit mode, for example , here I will modify the red block of the above text.

after it , type "%!xxd   -r" to convert the hex type to binary type,

save this file like you always doing in Vim. Now , this binary file had been modified .

open this file again , you'll see like below:

    1 0000000: 0000 1400 0000 0000 0000 0000 0000 0000  ................

    2 0000010: 0000 002a 0000 0010 0000 0002 4000 0000  ...*........@...

    3 0000020: 0000 0000 0000 0012 0000 0000 0003 0000  ................

    4 0000030: 0000 0000 0001 0000 0000 0001 0000 0000  ................

    5 0000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................

    6 0000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................

    7 0000060: 0000 0004 0000 0068 6803 f857 0000 0090  .......hh..W....

    8 0000070: 0000 6469 7274 7920 6269 7400 0000 0000  ..dirty bit.....

    9 0000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   10 0000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   11 00000a0: 0001 636f 7272 7570 7420 6269 7400 0000  ..corrupt bit...

   12 00000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   13 00000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   14 00000d0: 0100 6c61 7a79 2072 6566 636f 756e 7473  ..lazy refcounts

   15 00000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
   16 00000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   17 0000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   18 0000110: 7374 2f69 6d61 6765 2f69 6133 3265 5f72  st/image/ia32e_r

   19 0000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   20 0000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   21 0000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................

   22 0000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................

see the red block , what difference you've found than before?

good job !

haha, thanks.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vim linux binary