How to add/remove x86 instruction in linux executables without spoiling the alignment

徘徊边缘 提交于 2019-12-11 01:25:37

问题


I'm new to binary and assembly, and I'm curious about how to directly edit binary executables. I tried to remove an instruction from a binary file (according to disassembled instructions provided by objdump), but after doing that the "executable" seems no longer in an executable format (segmentation fault when running; gdb cannot recognize). I heard that this is due to instruction alignment issue. (Is it?)

So, is it possible to add/remove single x86 instructions directly in linux executables? If so, how? Thanks in advance.


回答1:


If you remove a chunk of binary file without adjusting file headers accordingly, it will become invalid.

Fortunately, you can replace instructions with NOP without actually removing them. File size remains the same, and if there is no checksum or signature (or if it's not actually checked), there is nothing more to do.

There is no universal way to insert the instructions, but generally you overwrite the original code with a JMP to another location, where you reproduce what the original code did, do your own things as you wanted, then JMP back. Finding room for your new code might be impossible without changing the size of the binary, so I would instead patch the code after executable is loaded (perhaps using a special LD_PRELOADed library).




回答2:


Yes. Just replace it with a NOP instruction (0x90) - or multiple ones if the instruction spans across multiple bytes. This is an old trick.



来源:https://stackoverflow.com/questions/14903823/how-to-add-remove-x86-instruction-in-linux-executables-without-spoiling-the-alig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!