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 @...............
上面的命令 dump
了 ls
命令二进制文件的前 0x30 个字节的数据,第一列是偏移,默认每行两字节一组,8组共16字节,最后一列是数据代表的字符,只显示可打印字符。
option | meaning |
---|---|
-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...
https://vi.stackexchange.com/questions/343/how-to-edit-binary-files-with-vim ↩︎