Frida anti-debugging countermeasures quick check: from ptrace to inline hook detection

Four common types of anti-debugging on Android (ptrace / TracerPid / port scanning / maps characteristics) and corresponding bypass ideas.

Anti-debugging surfaces often encountered during dynamic analysis on the Android side, as well as corresponding bypass ideas. Environment: frida-server 16.x/Android 12-14/arm64.

1. The four most common tests

1. ptrace self-attached

After the process starts itself ptrace(PTRACE_TRACEME) Occupying the debugging position, the second attach fails directly.

c
if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) { exit(1); }

Bypass ideas: hook ptrace Let the first call return 0 but not actually attach; or use frida-server's spawn mode to seize the startup opportunity.

2. TracerPid polling

read /proc/self/status of TracerPid Field, if it is not 0, it is judged to be debugged.

bash
adb shell "while true; do grep TracerPid /proc/<pid>/status; sleep 1; done"

Bypass: inline hook open/read combine, put TracerPid: Change the following number to 0; than hook directly strstr stable.

3. Port and process scanning

Scan port 27042/27043, or traverse /proc/*/cmdline try to find frida-server、gum-js-loop、gmain Characteristic thread name.

Bypass key points: change the default port (frida-server -l 0.0.0.0:6666), rename the server binary, use frida-gadget Injection rather than standalone server.

4. maps and so features

/proc/self/maps appear in frida-agent、linjector、gadget Waiting for the name.

Bypass: gadget rename + custom injector; or use frida-gadget The so is loaded directly with the legal name.

2. Confrontation with inline hook detection

Testers often do the following things:

  • Compare the first 8-16 bytes of the target function with the original bytes in disk so
  • scanning .text Does the segment appear to be unusually dense? b <offset>(arm64 jump)
  • check mprotect Will the page permissions change later? rwx

Corresponding processing: temporarily restore the original bytes before verification, and then call back the hook after comparison; more thoroughly, use hardware breakpoints to replace inline hooks, without changing the instruction bytes, and directly avoid byte comparison.

3. Several practical points on the debugger side

bash
# 以 spawn 方式启动,抢在反调试初始化之前
frida -U -f com.example.app --no-pause -l hook.js

# 只看目标模块的导出,减少噪音
frida-trace -U -f com.example.app -I "libtarget.so" -i "*"

# 枚举线程,找检测线程
frida -U com.example.app -e "Process.enumerateThreads().forEach(t => console.log(t.id, t.name))"
  • spawn is easier to win at the starting line than attach, but pay attention to multi-process (:remote) also hang up together
  • Detection threads usually have fixed names or fixed sleep periods, using Process.enumerateThreads + Time difference positioning is fast
  • meet SIGTRAP Frequently triggered, first confirm whether the shell itself is using it int3 Do integrity check

4. Suggestions on the order of investigation

  1. First, statically check if there is any in so ptrace、/proc/self/status、frida string
  2. Dynamically hang spawn and observe which detection branch the crash point falls on
  3. Remove the return value of the detection function one by one instead of patching all at once (easy to locate)
  4. Let’s talk about shelling/dumping after everything is passed.

The above only focuses on the analysis and debugging environment construction of the detection mechanism itself.

There are no messages yet. please Log in or Register an account Start this discussion.