Task: Apply targeted binary patches in IDA Pro based on the user's natural language description. Analyze the function, identify the minimal set of instructions to change, assemble new instructions, write them, and verify the result.
Workflow
Read the target function's disassembly (read_function_disassembly) and decompiled pseudocode (decompile_function) to understand its current behavior.
Identify which specific instructions implement the behavior the user wants to change. Use get_instruction_info to get exact byte sizes and encodings for the target instructions.
Back up the original bytes before patching. Use read_bytes at the target address for the instruction length, and print them so the user has a record:
Original bytes at 0x{addr:x}: {hex_bytes}
Plan the minimal patch:
- Determine what new instruction(s) achieve the desired behavior.
- Ensure the new instructions fit within the original byte boundaries.
- If new instructions are shorter, the remaining bytes MUST be filled with NOPs.
- Verify branch targets and relative offsets are correct for the patch address.
Patch using execute_python with IDA's byte-patching API:
import ida_bytes, idc
# Option A: manual opcode (for simple patches like branch inversion)
ida_bytes.patch_bytes(0xADDR, bytes([0x75])) # JNZ instead of JZ
# Option B: use keystone assembler (if installed)
import keystone
ks = keystone.Ks(keystone.KS_ARCH_X86, keystone.KS_MODE_64)
encoding, _ = ks.asm("jg 0x401300", 0x401248)
ida_bytes.patch_bytes(0x401248, bytes(encoding))
# NOP padding
remaining = original_size - len(encoding)
if remaining > 0:
ida_bytes.patch_bytes(0x401248 + len(encoding), bytes([0x90] * remaining))
print(f"Patched at 0x401248")
Verify with redecompile_function — confirm the decompiled output reflects the desired behavior change. If it doesn't match, revert by writing back the original bytes and try a different approach.
Report — If called from /modify, you MUST call:
exploration_report(category="patch_result", address=..., summary="Patched X: old → new", original_hex="...", new_hex="...", evidence="redecompile confirms...")
Annotate each patched address with set_comment explaining what was changed and why.
Safety Rules
- IDB only. Patches are applied to the IDB analysis database, not the binary file on disk. The binary file is only modified when the user does: File → Produce file → Create patched file.
- When called from /modify, do NOT save to file — the Phase 4 save gate handles this.
- Never exceed original boundaries. New instructions must not be larger than the instructions they replace.
- NOP padding is mandatory. If new instructions are shorter, fill remaining bytes with NOPs (
0x90).
- Always back up first. Print original bytes before writing any patch.
- Always verify after. Redecompile and confirm the change matches the user's intent.
- Revert on failure. If verification shows the patch didn't work:
ida_bytes.patch_bytes(addr, original_bytes).
- Minimal changes only. Patch the fewest bytes possible.
NOP via Microcode
For obfuscation cleanup, prefer nop_microcode to suppress instructions at the Hex-Rays IR level without touching bytes — useful when byte-level NOP would affect alignment or when the goal is to remove a check from the decompiler output only.
Common Patch Patterns
Changing a conditional branch
Replace jl with jg, je with jne, etc. Same instruction size, just a different opcode byte.
Inverting a condition
Change test eax, eax + je to test eax, eax + jne, or patch the comparison operand.
Forcing a branch (always/never taken)
Replace conditional jump with jmp (always) or NOP out the jump (never).
Changing an immediate operand
Reassemble the instruction with a new immediate value, e.g., cmp eax, 0xa → cmp eax, 0x14.
Removing a check entirely
NOP out the comparison and conditional jump instructions using nop_microcode.
1---2name: smart-patch-ida-pro3description: Patch binary code in IDA Pro using natural language — read, assemble, write, verify4---5Task: Apply targeted binary patches in IDA Pro based on the user's natural language description. Analyze the function, identify the minimal set of instructions to change, assemble new instructions, write them, and verify the result.67## Workflow891. **Read** the target function's disassembly (`read_function_disassembly`) and decompiled pseudocode (`decompile_function`) to understand its current behavior.10112. **Identify** which specific instructions implement the behavior the user wants to change. Use `get_instruction_info` to get exact byte sizes and encodings for the target instructions.12133. **Back up** the original bytes before patching. Use `read_bytes` at the target address for the instruction length, and print them so the user has a record:14 ```15 Original bytes at 0x{addr:x}: {hex_bytes}16 ```17184. **Plan** the minimal patch:19 - Determine what new instruction(s) achieve the desired behavior.20 - Ensure the new instructions fit within the original byte boundaries.21 - If new instructions are shorter, the remaining bytes MUST be filled with NOPs.22 - Verify branch targets and relative offsets are correct for the patch address.23245. **Patch** using `execute_python` with IDA's byte-patching API:25 ```python26 import ida_bytes, idc2728 # Option A: manual opcode (for simple patches like branch inversion)29 ida_bytes.patch_bytes(0xADDR, bytes([0x75])) # JNZ instead of JZ3031 # Option B: use keystone assembler (if installed)32 import keystone33 ks = keystone.Ks(keystone.KS_ARCH_X86, keystone.KS_MODE_64)34 encoding, _ = ks.asm("jg 0x401300", 0x401248)35 ida_bytes.patch_bytes(0x401248, bytes(encoding))3637 # NOP padding38 remaining = original_size - len(encoding)39 if remaining > 0:40 ida_bytes.patch_bytes(0x401248 + len(encoding), bytes([0x90] * remaining))41 print(f"Patched at 0x401248")42 ```43446. **Verify** with `redecompile_function` — confirm the decompiled output reflects the desired behavior change. If it doesn't match, revert by writing back the original bytes and try a different approach.45467. **Report** — If called from `/modify`, you MUST call:47 ```48 exploration_report(category="patch_result", address=..., summary="Patched X: old → new", original_hex="...", new_hex="...", evidence="redecompile confirms...")49 ```50518. **Annotate** each patched address with `set_comment` explaining what was changed and why.5253## Safety Rules5455- **IDB only.** Patches are applied to the IDB analysis database, not the binary file on disk. The binary file is only modified when the user does: File → Produce file → Create patched file.56- **When called from /modify**, do NOT save to file — the Phase 4 save gate handles this.57- **Never exceed original boundaries.** New instructions must not be larger than the instructions they replace.58- **NOP padding is mandatory.** If new instructions are shorter, fill remaining bytes with NOPs (`0x90`).59- **Always back up first.** Print original bytes before writing any patch.60- **Always verify after.** Redecompile and confirm the change matches the user's intent.61- **Revert on failure.** If verification shows the patch didn't work: `ida_bytes.patch_bytes(addr, original_bytes)`.62- **Minimal changes only.** Patch the fewest bytes possible.6364## NOP via Microcode6566For obfuscation cleanup, prefer `nop_microcode` to suppress instructions at the Hex-Rays IR level without touching bytes — useful when byte-level NOP would affect alignment or when the goal is to remove a check from the decompiler output only.6768## Common Patch Patterns6970### Changing a conditional branch71Replace `jl` with `jg`, `je` with `jne`, etc. Same instruction size, just a different opcode byte.7273### Inverting a condition74Change `test eax, eax` + `je` to `test eax, eax` + `jne`, or patch the comparison operand.7576### Forcing a branch (always/never taken)77Replace conditional jump with `jmp` (always) or NOP out the jump (never).7879### Changing an immediate operand80Reassemble the instruction with a new immediate value, e.g., `cmp eax, 0xa` → `cmp eax, 0x14`.8182### Removing a check entirely83NOP out the comparison and conditional jump instructions using `nop_microcode`.