HowTo :: Patching binary files in Linux

Introduction

Learn to patch binary files directly without source code using hex editing and binary diff tools; essential for fixing bugs, applying security patches, or modifying executables.

What is Binary Patching?

Binary patching is the process of modifying compiled executable files without access to their source code. This technique is useful for:

Instead of editing high-level source code and recompiling, you directly modify the machine code bytes in the executable file. This requires understanding hexadecimal representation and executable file formats.

What You’ll Need

Method 1: Using xxd (Hex Editing)

The xxd command creates hexadecimal representations of binary files that you can edit with text editors, then convert back to binary.

Step 1: Convert Binary to Hex

First, create hex dumps of the binary files you want to work with:

1
xxd prog1.bin > b1.hex

This converts prog1.bin into a human-readable hexadecimal format saved in b1.hex.

Example output format:

1
2
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
00000010: 0200 3e00 0100 0000 7010 4000 0000 0000  ..>.....p.@.....

Each line shows:

Step 2: Edit the Hex File

Now you can edit b1.hex with any text editor. Modify the hex values in the middle columns:

1
2
3
4
vim b1.hex

# Or use your preferred editor
nano b1.hex

Example modification: Change instruction opcodes, string values, or jump addresses by editing the hex bytes.

Step 3: Convert Hex Back to Binary

After editing, convert the hex file back to a binary:

1
xxd -r b1.hex new_prog1.bin

The -r flag tells xxd to reverse the conversion (hex to binary).

Step 4: Compare Binaries Using Hex Diffs

To see what changed between two versions, create hex dumps of both and compare:

1
2
3
xxd prog1.bin > original.hex
xxd new_prog1.bin > modified.hex
diff original.hex modified.hex

Or use vimdiff for visual comparison:

1
vimdiff original.hex modified.hex

This shows exactly which bytes changed, making it easy to document or reverse your modifications.

Method 2: Using bsdiff (Binary Patches)

The bsdiff tool creates and applies binary patch files, useful for distributing modifications without sharing entire binaries.

Step 1: Install bsdiff

If not already installed:

1
2
sudo apt install bsdiff      # Debian/Ubuntu
sudo yum install bsdiff      # Fedora/RHEL

Step 2: Create a Binary Patch

Generate a patch file from an old and new version of a binary:

1
bsdiff old_prog.bin new_prog.bin patch.bin

This creates patch.bin, which contains only the differences between the two files; much smaller than distributing the entire new binary.

What this does:

Step 3: Apply a Binary Patch

To apply a patch file to the original binary:

1
bspatch old_prog.bin output.bin patch.bin

This applies the modifications from patch.bin to old_prog.bin, creating output.bin which matches new_prog.bin.

Use cases:

Step 4: Verify the Result

Always verify the patched binary matches expectations:

1
2
3
4
5
6
7
8
# Compare file sizes
ls -lh old_prog.bin output.bin new_prog.bin

# Check file integrity if you have checksums
sha256sum old_prog.bin output.bin new_prog.bin

# Test functionality
./output.bin --version

Practical Example: Patching a Simple Binary

Here’s a complete workflow for patching a binary to change a string:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 1. Create backup
cp myprogram.bin myprogram.bin.backup

# 2. Convert to hex
xxd myprogram.bin > myprogram.hex

# 3. Search for the string you want to change
grep -n "Hello" myprogram.hex

# 4. Edit the hex file (change the bytes)
vim myprogram.hex
# Find the line with "Hello" and change it to "Hallo" (or whatever fits)

# 5. Convert back to binary
xxd -r myprogram.hex myprogram_patched.bin

# 6. Make executable
chmod +x myprogram_patched.bin

# 7. Test
./myprogram_patched.bin

Troubleshooting

Best Practices

Next Steps

Now that you understand binary patching:

  1. Learn assembly: Read Linux Syscalls in Assembly to understand what you’re modifying
  2. Master GDB: Use GDB to inspect binaries and find patch locations
  3. Study ELF format: Learn about executable file structure to patch safely
  4. Practice with CTFs: Capture The Flag challenges often require binary patching skills

See Also