x64dbg conditional breakpoints and hardware breakpoints practical quick review: don’t set breakpoints too wide
The usage and trade-offs of conditional breakpoints/hardware breakpoints/memory breakpoints, with a comparison table of typical scenarios and several pitfalls that are easy to avoid.
The biggest waste of time during debugging is "setting breakpoints too wide". This article will record the usage and trade-offs of conditional breakpoints, hardware breakpoints, and memory breakpoints.
1. Problems with common breakpoints
above and below large functions bp, it takes dozens of hits to reach the target branch. Three more precise methods: conditional breakpoints, hardware breakpoints, and memory breakpoints.
2. Conditional breakpoints
Directly download x64dbg from the command bar:
bp 0x401000, "eax==0x1234"
bp 0x401000, "[[esp+4]]==0x1000"
- The condition is evaluated on each hit. Writing too complex will significantly slow down the process.
- Don’t use string comparison
strcmp, changing to comparing pointers or lengths is faster - Script variables for counting requirements:
$count = 0
bp 0x401000, "$count++; $count==5"
It is broken only after the 5th hit, and the front is automatically released.
3. Hardware breakpoint (do not move instruction byte)
bph 0x401000, r ; 读
bph 0x401000, w ; 写
bph 0x401000, x ; 执行
advantage:
- Do not modify the original bytes and avoid CRC/byte comparison self-checking
- Only 4 debug registers are available, up to 4 simultaneously
- Suitable for questions such as "Who changed this value" and "Who read this global situation"
Disadvantages: It is easy to miss hits when switching between multiple threads, and it is not sensitive to cross-page access.
4. Memory breakpoints
Coarse-grained, but covering the entire page:
bpm 0x7FF000, r
bpm 0x7FF000, w
- Used for "When will this decrypted code be executed?"
- After hitting, confirm whether it falls on the target instruction. The page-level breakpoint will also hit the adjacent code.
- It may not be triggered when the memory is modified in kernel mode, and single-stepping is required.
5. Typical scene comparison
| need | First choice |
|---|---|
| Calls that only care about a certain parameter value | conditional breakpoint |
| Find out who modified a global variable | Hardware write breakpoint |
| After shell decryption, jump to OEP | Hardware execution breakpoints |
| Who will execute first in a large decryption area? | memory breakpoint |
| Nth time in loop | Conditional breakpoint + count variable |
6. Several pits
- If the conditional expression is written incorrectly, no error will be reported, but it will never hit the target; first use the constant true condition to verify the syntax
- Hardware breakpoints are maintained by thread and will be overwritten by other threads under multi-threading. Be careful when switching threads.
- If the breakpoint is left at the hooked address, it will fight with the hook. Make sure there is no inline hook first.
- The address will change when ASLR is turned on. Use "module base address + RVA", or break at the entrance and calculate again.
# 调试时关掉目标进程的 ASLR
setdllcharacteristics -d sample.exe
7. Efficiency Suggestions
- First
bpGo to the module entrance to get the base address, and then use "base address + RVA" to set a precise breakpoint. - Log breakpoint (
logcommand) saves time than interrupts, especially when observing in batches - For complex logic, write scripts first, which is much faster than writing by hand.
The above are debugger usage tips and are only used for local sample analysis.
please Log in or Register an account Participate in discussions, download exclusive add-ons or unlock level posts.