Evan Allen @evallen, Melanie Verna @kverna
This repository contains a plugin for the GNU Debugger (GDB) that allows users to patch a binary on disk with granular control.
- Download the
patch.py
file to a directory of your choice such as~/scripts
. That's it!
- Open the executable you intend to patch in GDB.
- Make the intended changes to the executable in memory
as you normally would. For example, you might
use the
set
command to change a certain byte. - Import the patch script using GDB's
source
command, e.g.:
source ~/scripts/patch.py
- Run the patch command:
patch <filename> <start_addr> <end_addr>
where
<filename>
refers to the name of the original executable you would like to patch.<start_addr>
refers to the address of the first byte of memory in the GDB process that you would like to save to the patched file.<end_addr>
refers to the address of the last byte of memory in the GDB process that you would like to save to the patched file.
After running the command, a new file will be created with the same
name as the original executable with "_patch
" added to the end
of the filename. This file will be a copy of the original
executable file, but with the user's selected memory patched in.
This allows you to patch a binary without having to worry about
accidentally overwriting the original file.
However, if a file with "<executable>_patch
" already exists
(where <executable>
is the name of the original executable
file), then it will be overwritten when the patch
command
is executed.
set {int}0x0000555555555152 = 0x06
patch ../test 0x000055555555514d 0x000055555555515d
Our patch
command operates by completing the following steps
upon execution:
- It reads the memory the user wants to save a patch for from the process currently being debugged.
- It determines the equivalent file addresses of the memory the
user selected. For example, if the base address of the process
is
0x10000
, then the process memory address0x1BEEF
corresponds to the file address0x0BEEF
. - It copies the original executable and appends "
_patch
" to it. - It copies the memory it read from the user's process in GDB into the calculated equivalent file addresses of the copied executable on disk. This effectively saves the memory the user selected, allowing the user to save any patches they have made in GDB permanently.
The repository contains a simple test program, test.c
with
a compiled test
x86 executable to try this script on.
- GDB
set write on
command