使用 vim 和 xxd 编辑二进制文件

xxd

xxd [options] [files]

xxd 可以转储 [dump] 出文件的数据,反之亦可。例如:

xxd -l 0x30 /bin/ls
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
00000010: 0300 3e00 0100 0000 5058 0000 0000 0000  ..>.....PX......
00000020: 4000 0000 0000 0000 a003 0200 0000 0000  @...............

上面的命令 dumpls 命令二进制文件的前 0x30 个字节的数据,第一列是偏移,默认每行两字节一组,8组共16字节,最后一列是数据代表的字符,只显示可打印字符。

optionmeaning
-l N只显示前 N 个字节的数据
-s N / -s -N跳过前 N 个字节的数据,之后开始显示,-N 则表示只显示文件末尾的 N 个数据
-c N每行显示的字节数,默认是16
-g N将每行的字节数据打包成 N 个字节的 8位元组,默认是2
-ps输出连续的字节串

因此下面的命令将逐字节显示文件 /bin/ls 前 0x30 个字节的数据。

xxd -g 1 -l 0x30 /bin/ls

00000000: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00  .ELF............
00000010: 03 00 3e 00 01 00 00 00 50 58 00 00 00 00 00 00  ..>.....PX......
00000020: 40 00 00 00 00 00 00 00 a0 03 02 00 00 00 00 00  @...............

how-to-edit-binary-files-with-vim1

可以使用 xxd 工具配合 vim 来实现一个16进制编辑器。一个使用场景是更改二进制文件的数据,这样可以在不重新编译可执行文件的情况下,直接更改二进制中的某些指令用于调试。

// open file as binary mode.
vim -b file

:set binary

// : enters command-line mode,
// % matches whole file as a range, 
// ! filters that range through an external command
// xxd is that external shell command
:%!xxd

// edit the file
...
// Once you make the changes (in the hex part), you can go back to text with -r command on xxd
:%!xxd -r

// rembember you can also use syntax highlighting for hex editing in vim with that command:
:set ft=xxd

armeb code dump: 0x11223344 and 0xe3a01001 mov r1, #1

00000030: 00 08 00 07 11 22 33 44 e3 a0 10 01 41 00 00 00  ....."3D....A...

arm code dump: 0x11223344 and 0xe3a01001 mov r1, #1

00000030: 08 00 07 00 44 33 22 11 01 10 a0 e3 41 1c 00 00  ....D3".....A...

  1. https://vi.stackexchange.com/questions/343/how-to-edit-binary-files-with-vim ↩︎

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值