使用gdb修改二进制文件 [英] Use gdb to Modify Binary
问题描述
我试图修改gdb下的可执行文件。即使内存已更改,但相应的可执行文件不会更改,因此下次运行该程序时,修改已不存在。
I tried to modify executable file under gdb. Even though memory has been changed, but corresponding executable does not change, so next time run the program the modification is gone.
我使用-write选项启动了gdb。
我也尝试设置写入然后重新加载exec-file
我改变了设置{unsigned char} addr = 0xf;
I started gdb with -write option. I also tried set write on and then reload exec-file I changed the memory with set {unsigned char}addr = 0xf;
的内存相应的文件没有改变。
but the corresponding file is not changed.
推荐答案
但相应的文件没有改变。
but the corresponding file is not changed.
很难说你实际修改了什么地址,所以你的修改是否应该修改二进制文件。
It's hard to say what address you are actually modifying, and so whether your change should actually modify the binary or not.
过去,我发现在修改二进制文件之后,我需要立即 退出
。如果我做了退出
(例如 run
)的任何其他操作,那么GDB会放弃我的更改,但是如果我 quit
,那么这个更改就会取。
In the past, I've found that after modifying the binary, I need to immediately quit
. If I do anything other than quit
(e.g. run
), then GDB would discard my change, but if I quit
, then the change would "take".
示例:
Example:
$ cat t.c
int main()
{
return 42;
}
$ gcc t.c && ./a.out; echo $?
42
$ gdb --write -q ./a.out
(gdb) disas/r main
Dump of assembler code for function main:
0x00000000004004b4 <+0>: 55 push %rbp
0x00000000004004b5 <+1>: 48 89 e5 mov %rsp,%rbp
0x00000000004004b8 <+4>: b8 2a 00 00 00 mov $0x2a,%eax
0x00000000004004bd <+9>: 5d pop %rbp
0x00000000004004be <+10>: c3 retq
End of assembler dump.
(gdb) set {unsigned char}0x00000000004004b9 = 22
(gdb) disas/r main
Dump of assembler code for function main:
0x00000000004004b4 <+0>: 55 push %rbp
0x00000000004004b5 <+1>: 48 89 e5 mov %rsp,%rbp
0x00000000004004b8 <+4>: b8 16 00 00 00 mov $0x16,%eax <<< ---changed
0x00000000004004bd <+9>: 5d pop %rbp
0x00000000004004be <+10>: c3 retq
End of assembler dump.
(gdb) q
$ ./a.out; echo $?
22 <<<--- Just as desired
这篇关于使用gdb修改二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!