Lesson 13-Reverse Engineering

In-Depth Analysis of Disassembly Techniques

Comparison of Mainstream Disassembly Tools

ToolArchitecture SupportGUICore AdvantagesTypical Use Cases
IDA Prox86/ARM/MIPS, etc.YesPowerful disassembly engine, cross-referencesCommercial reverse engineering, vulnerability analysis
GhidraMulti-architectureYesNSA open-source, free, script supportSecurity research, malware analysis
Radare2Multi-architectureOptionalCLI-driven, highly extensibleEmbedded analysis, automated reverse engineering

IDA Pro Advanced Features:

  • FLIRT signature for identifying library functions
  • Interactive disassembly navigation
  • Python/API automation scripting
  • Binary diffing

Ghidra Signature Features:

# Ghidra Python script example: batch marking strings
from ghidra.app.decompiler import DecompInterface

def mark_strings():
    current_program = getCurrentProgram()
    listing = current_program.getListing()
    for addr in listing.getAddresses(True):
        if listing.getCodeUnitAt(addr).isString():
            createBookmark(addr, "String", "Identified string")

Key Points of Disassembly Techniques

Instruction Decoding Essentials:

  • Opcode identification and classification
  • Operand parsing (registers/memory/immediate values)
  • Prefix byte handling (e.g., 66h/67h)
  • Branch target calculation

Pseudocode Generation Principles:

  • Control Flow Graph (CFG) construction
  • Data flow analysis
  • Type inference
  • Intermediate Representation (IR) transformation

Dynamic Analysis Techniques

Dynamic Debugging Tools

OllyDbg Core Features:

  • Memory breakpoints (access/write)
  • Hardware breakpoints (DR0–DR7)
  • Script debugging (OLLYScript)
  • API call tracing

x64dbg Modern Features:

  • Multi-threaded debugging support
  • Memory search highlighting
  • Plugin system (Python/Lua)
  • Symbol server integration

Dynamic Analysis Tips

; Example: setting a hardware breakpoint in a debugger
mov eax, [target_address]   ; Target address
mov dr0, eax                ; Set DR0 register
mov dr7, 0x00000001         ; Enable execution breakpoint (DR7[0]=1)

Runtime Behavior Analysis

API Call Monitoring Techniques:

  • SSDT Hook (System Service Descriptor Table)
  • Inline Hook (instruction replacement)
  • EAT Hook (Export Address Table)

Memory Monitoring Methods:

  • Page table permission modification (PAGE_GUARD)
  • Write breakpoints (DR0–DR3)
  • Heap allocation tracking (RtlAllocateHeap)

Code Injection Techniques

Shellcode Injection Techniques

Classic Injection Workflow:

  1. Allocate executable memory (VirtualAlloc/VirtualAllocEx)
  2. Write shellcode
  3. Create remote thread (CreateRemoteThread)
  4. Execute and clean up

Shellcode Development Principles:

  • Position-independent code (PIC)
  • Avoid null bytes (0x00)
  • Direct system calls
  • Compatibility with initial register states

Shellcode Example (x86):

; Windows MessageBox Shellcode
mov eax, 0x77D507E0     ; MessageBoxA address
push 0                  ; uType
push 0x74736577         ; "wset"
push 0x6F6C6C63         ; "cllo"
push 0                  ; hWnd
mov ebx, esp
push 0
call eax

DLL Injection Techniques

Comparison of Mainstream Injection Methods:

MethodPrincipleDetection Difficulty
CreateRemoteThreadRemotely create thread to load DLLLow
SetWindowsHookExInstall global hookMedium
Reflective DLLLoad DLL in memory without file dropHigh
APC InjectionAsynchronous Procedure CallMedium

Reflective DLL Injection Key Code:

// Core steps of reflective loading
void* mem = VirtualAlloc(NULL, dll_size, MEM_COMMIT, PAGE_READWRITE);
memcpy(mem, dll_buffer, dll_size);
relocate(mem);           // Fix relocation table
resolve_imports();       // Resolve import table
call_entrypoint();       // Invoke DllMain

Software Cracking Techniques

Patching Techniques Classification

  • Instruction replacement (e.g., JNZ → JMP)
  • Data modification (jump table alteration)
  • Structure patching (vtable hijacking)
  • Import Table Hook

Keygen Algorithm Reversing:

; Typical serial validation logic
mov eax, [user_input]
call crc32_hash
cmp eax, [stored_hash]
jne show_error_message

Bypassing Protection Mechanisms

  • Anti-debugging detection (IsDebuggerPresent)
  • VM detection (CPUID instruction analysis)
  • Environment checks (registry/filesystem)

Advanced Cracking Techniques

Memory Patching Techniques:

  • Runtime code modification
  • Import Address Table (IAT) Hook
  • Exception handler hijacking

Example: IAT Hook Implementation:

; Modify IAT entry to point to our function
mov eax, [iat_entry]        ; Original function address
mov [new_iat_entry], eax
mov [iat_entry], our_function

Anti-Debugging and Anti-Reverse Engineering Techniques

Common Anti-Debugging Methods

  • Debugger flag detection (IsDebuggerPresent)
  • Time-difference detection (RDTSC)
  • Exception handling detection (SEH chain inspection)
  • Process enumeration checks

Advanced Anti-Debugging Example:

; Detect hardware breakpoints
mov eax, dr7
test eax, 0x0F              ; Check DR0–DR3
jnz debugger_detected

; Detect memory breakpoints
mov eax, [target_address]
mov ebx, [eax]
; Attempting to write will trigger an exception

Code Obfuscation Techniques

Control Flow Flattening:

; Flattened control flow
dispatch_loop:
    mov eax, [state_var]
    jmp [dispatch_table + eax*4]

handler1:
    ; ... code ...
    mov [state_var], 2
    jmp dispatch_loop

handler2:
    ; ... code ...
    mov [state_var], 1
    jmp dispatch_loop

Data Encryption Techniques:

; Runtime string decryption
mov esi, encrypted_string
mov edi, decrypted_buffer
mov ecx, string_length

decrypt_loop:
    lodsb
    xor al, key
    stosb
    loop decrypt_loop

Comprehensive Practical Case Studies

Cracking Example: Serial Validation

Reverse Engineering Steps:

  1. Locate validation function (via string cross-references)
  2. Analyze algorithm logic (CRC32/MD5, etc.)
  3. Identify critical comparison instruction
  4. Modify jump condition (JNZ → JMP)

Patch Process:

; Original code
cmp eax, ebx
jne show_error

; After patch
cmp eax, ebx
jmp skip_error      ; Skip error message directly

Shellcode Development in Practice

Principles for AV-Evading Shellcode:

  • Dynamic API resolution (GetProcAddress)
  • Direct system calls
  • Instruction randomization
  • Polymorphic code generation

Example: Dynamic API Resolution:

; Resolve API via NTDLL
mov eax, [ntdll_base]
mov eax, [eax + GetProcAddress_offset]
call eax

Anti-Debugging Countermeasure Drill

Detection and Bypass Example:

; Anti-debugging check
mov eax, 0x77D507E0     ; IsDebuggerPresent address
call eax
test eax, eax
jnz exit_program

; Bypass method: NOP out or modify return value
; nop
; mov eax, 0

By mastering these techniques, security researchers can:

  • Analyze malware behavior
  • Assess software security vulnerabilities
  • Develop security protection solutions
  • Conduct authorized penetration testing
  • Understand the principles behind software protection mechanisms

Legal Disclaimer:
The techniques described in this document are intended solely for legitimate security research purposes. Unauthorized reverse engineering may violate laws and regulations such as the Regulations on Computer Software Protection.

Share your love