Rev Unicorn Debug

p4nda0s/reverse-skills/skills/rev-unicorn-debug

作者 p4nda0sa2baa31c58a3無授權條款2.2K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 個月前更新

Debug and emulate specific code fragments or functions using the Unicorn engine. Activate when the user wants to emulate a function with Unicorn, trace binary execution without running the full program, decrypt or decode data by emulating the algorithm, or bypass environment dependencies (JNI, syscalls, libc) during emulation.

AI 產生的概覽

指導使用 Unicorn 引擎模擬特定函式或程式碼片段,並透過掛鉤外部相依性進行除錯。

功能
此技能提供使用 Unicorn CPU 模擬器模擬單一函式或程式碼片段(而非執行整個二進位檔)的說明。內容涵蓋原始檔案載入、辨識 JNI、系統呼叫與 libc 等情境相依性,並透過 Unicorn 掛鉤加以模擬。它也列出回呼類型、迭代式崩潰診斷流程,以及各架構的暫存器與系統呼叫慣例。產出的是模擬設定與除錯指引,而非檔案或指令碼。
適用情境
當你想用 Unicorn 模擬某個函式、在不執行完整程式的情況下追蹤二進位檔執行、透過模擬演算法來解密或解碼資料,或在模擬過程中繞過 JNI、系統呼叫、libc 等環境相依性時,可使用此技能。
執行需求
需要 Unicorn 引擎與 Python 環境來撰寫模擬及掛鉤程式碼。技能未附帶指令碼,僅為說明性內容。

rev-unicorn-debug - Unicorn Emulation Debugger

Debug and emulate specific code fragments or functions using the Unicorn engine. Analyze context dependencies (JNI, syscalls, library functions) and simulate them through hook mechanisms to complete the user's debugging goal.


Core Principles

  1. Load file raw first — do NOT parse ELF/PE/Mach-O headers. Read the file as raw bytes and map directly into Unicorn memory. We only need to emulate specific functions, not the entire binary. If raw loading fails (code references segments at specific addresses), then parse minimally — only map the segments needed.
  2. Identify context dependencies — analyze the target code for external calls (JNI, syscalls, libc, imports) and hook them to provide simulated responses.
  3. Use callbacks extensively — leverage Unicorn's hook system for debugging, tracing, error recovery, and environment simulation.
  4. Iterative fix — when emulation crashes, use the callback info to diagnose and fix (map missing memory, hook unhandled calls, fix register state).
  5. Minimal trace output — prefer block-level tracing over instruction-level. Only enable instruction trace on small targeted ranges. Use counters and summaries instead of per-step logging.

Environment Simulation Strategy

Before emulating, read the target function and identify what it calls. Hook external dependencies by address and simulate in Python:

CategoryExamplesSimulation Strategy
libcmalloc, free, memcpy, strlen, printfHook address, implement logic in Python (bump allocator for malloc)
JNIGetStringUTFChars, FindClass, GetMethodIDBuild fake JNIEnv function table in UC memory, write RET stubs at each entry, hook stub addresses
Syscallsread, write, mmap, ioctlHook UC_HOOK_INTR, dispatch by syscall number
C++ runtimeoperator new, __cxa_throwHook and simulate
Library callspthread_mutex_lock, dlopenHook and return success/stub

Hook pattern: Register a UC_HOOK_CODE callback. When PC hits a known import address, execute the Python simulation, then set PC = LR to skip the original function.


Callback Types to Use

CallbackPurpose
UC_HOOK_CODEIntercept import calls by address; instruction-level trace (use sparingly, narrow range only)
UC_HOOK_BLOCKBlock-level trace (preferred over instruction trace)
UC_HOOK_MEM_UNMAPPEDAuto-map missing pages to recover from unmapped access errors
UC_HOOK_MEM_READ | UC_HOOK_MEM_WRITETrace memory access on targeted data ranges only
UC_HOOK_INTRIntercept SVC/INT for syscall simulation

Iterative Debugging Workflow

When emulation fails, follow this loop:

  1. Run — start emulation, let it crash
  2. Read callback output — which address faulted? What type (read/write/fetch)?
  3. Diagnose:
    • Unmapped memory fetch → missing code page, map it
    • Unmapped memory read/write → missing data section or uninitialized pointer, map or hook
    • Hitting an import stub → identify the function, add a simulation hook
    • Infinite loop → add a code hook with execution counter, stop after threshold
  4. Fix — add the hook / map the memory / adjust registers
  5. Re-run — repeat until the target function completes

Architecture Quick Reference

ArchUc ConstModeSPLRArgsReturnSyscall
ARM64UC_ARCH_ARM64UC_MODE_LITTLE_ENDIANSPX30X0-X7X0X8 + SVC #0
ARM32UC_ARCH_ARMUC_MODE_THUMB / UC_MODE_ARMSPLRR0-R3R0R7 + SVC #0
x86-64UC_ARCH_X86UC_MODE_64RSP(stack)RDI,RSI,RDX,RCX,R8,R9RAXRAX + syscall
x86-32UC_ARCH_X86UC_MODE_32ESP(stack)(stack)EAXEAX + int 0x80
MIPS32UC_ARCH_MIPSUC_MODE_MIPS32 + UC_MODE_BIG_ENDIAN$sp$ra$a0-$a3$v0$v0 + syscall

來源與署名

來源:p4nda0s/reverse-skills位於skills/rev-unicorn-debug提交a2baa31

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架