how to access and operate a binarry file ?

本文介绍如何使用Vim在Linux环境下打开并修改二进制文件。通过使用%xxd命令将二进制转换为十六进制表示,便于查看和编辑。修改完毕后,再用%xxd -r命令将其还原为二进制格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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.



出现了这样的问题: PS G:\DockerDemo> docker build -t ntpd-container . [+] Building 1.5s (7/10) => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 32B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/centos:7 0.0s => CACHED [1/6] FROM docker.io/library/centos:7 0.0s => [internal] load build context 0.0s => => transferring context: 1.42kB 0.0s => [2/6] RUN rm -f /etc/yum.repos.d/*.repo && curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/C 0.9s => ERROR [3/6] RUN yum install -y ntp && yum clean all && rm -rf /var/cache/yum 0.5s ------ > [3/6] RUN yum install -y ntp && yum clean all && rm -rf /var/cache/yum: #5 0.415 Loaded plugins: fastestmirror, ovl #5 0.481 #5 0.481 #5 0.481 File contains no section headers. #5 0.481 file: file:///etc/yum.repos.d/CentOS-Base.repo, line: 1 #5 0.481 '<!DOCTYPE html>\n' ------ executor failed running [/bin/sh -c yum install -y ntp && yum clean all && rm -rf /var/cache/yum]: exit code: 1 我的ntp.conf代码是: # NTP Network Time Protocol # **** ATTENTION ****: *You have to restart the NTP service when you change this file to activate the changes* # PLEASE CHECK THIS FILE CAREFULLY AND MODIFY IT IF REQUIRED # Configuration File created by Windows Binary Distribution Installer Rev.: 1.27 mbg # please check http://www.ntp.org for additional documentation and background information # restrict access to avoid abuse of NTP for traffic amplification attacks # see http://news.meinberg.de/244 for details restrict default noquery nopeer nomodify notrap restrict -6 default noquery nopeer nomodify notrap # allow status queries and everything else from localhost restrict 127.0.0.1 restrict -6 ::1 # if you need to allow access from a remote host, you can add lines like this: # restrict <IP OF REMOTE HOST> # Use drift file driftfile "D:\NTP\etc\ntp.drift" # your local system clock, could be used as a backup # (this is only useful if you need to distribute time no matter how good or bad it is) #server 127.127.1.0 # but it should operate at a high stratum level to let the clients know and force them to # use any other timesource they may have. #fudge 127.127.1.0 stratum 12 # End of generated ntp.conf --- Please edit this to suite your needs # 添加自定义NTP服务器(指定端口5323) server 47.104.243.93 port 5323 iburst
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值