Introduction to Executable File Formats
Executable file formats define how compiled code, data, and metadata are organized to enable execution on specific operating systems. This tutorial covers the PE, ELF, and Mach-O formats, which are the core formats for Windows, Linux/Unix, and macOS/iOS ecosystems, respectively. Understanding these formats is crucial for reverse engineering, debugging, and system-level software development. This tutorial will dissect the components of each format, explain their functions, and provide Python-based code for parsing and analyzing real files.
Before starting, ensure the following tools are installed:
- Python 3.x
- Relevant libraries:
pefile,pyelftools,macho_parser(install viapip install pefile pyelftools macho_parser) - Hex editor (such as HxD or Bless) for manual file inspection
- Sample executable files (e.g., a simple
.exefor PE analysis, a Linux binary for ELF analysis, a macOS binary for Mach-O analysis)
PE File Format (Portable Executable)
The PE file format is the standard format in Windows operating systems for executable files (such as .exe, .dll). Its design is flexible, supporting both 32-bit and 64-bit architectures, and is widely used in applications, dynamic link libraries, and drivers. The PE file structure consists of multiple parts, including headers, section tables, import tables, etc. This section will explain each component of the PE file in detail and parse a real PE file step-by-step using Python code.
PE File Structure Overview
The logical structure of a PE file can be divided into the following main parts:
- DOS Header: Ensures backward compatibility with MS-DOS, containing the offset to the PE header.
- PE Header: Contains basic file information, such as target architecture and entry point.
- Optional Header: Provides runtime environment information, such as base address and subsystem type.
- Section Table: Describes the sections in the file (such as code sections, data sections).
- Import Table: Lists external functions and dynamic link libraries that the program depends on.
- Other Tables: Such as export table, relocation table, etc., depending on file requirements.
The following is a typical layout of a PE file:
+--------------------+
| DOS Header |
+--------------------+
| DOS Stub |
+--------------------+
| PE Header |
+--------------------+
| Optional Header |
+--------------------+
| Section Table |
+--------------------+
| Section 1 (.text)|
+--------------------+
| Section 2 (.data)|
+--------------------+
| Section 3 (.rsrc)|
+--------------------+
| Import Table |
+--------------------+
| Export Table |
+--------------------+
DOS Header
The DOS header is the first part of a PE file, occupying 64 bytes, primarily for compatibility with old MS-DOS systems. Its key fields are e_magic (magic number, value 0x5A4D, representing “MZ”) and e_lfanew (offset to the PE header).
The following is the definition of some fields in the DOS header:
typedef struct _IMAGE_DOS_HEADER {
WORD e_magic; // Magic number, usually 0x5A4D
WORD e_cblp; // Bytes on last page
WORD e_cp; // Pages in file
WORD e_crlc; // Relocations
WORD e_cparhdr; // Size of header in paragraphs
WORD e_minalloc; // Minimum extra paragraphs
WORD e_maxalloc; // Maximum extra paragraphs
WORD e_ss; // Initial SS value
WORD e_sp; // Initial SP value
WORD e_csum; // Checksum
WORD e_ip; // Initial IP value
WORD e_cs; // Initial CS value
WORD e_lfarlc; // Relocation table offset
WORD e_ovno; // Overlay number
WORD e_res[4]; // Reserved words
WORD e_oemid; // OEM identifier
WORD e_oeminfo; // OEM information
WORD e_res2[10]; // Reserved words
LONG e_lfanew; // PE header offset
} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
Code Example: Parsing the DOS Header
We use Python’s pefile library to parse the DOS header of a PE file:
import pefile
# Load PE file
pe = pefile.PE("sample.exe")
# Get DOS header
dos_header = pe.DOS_HEADER
# Print key fields
print(f"DOS Magic: 0x{dos_header.e_magic:04X}")
print(f"PE Header Offset: 0x{dos_header.e_lfanew:08X}")
Running the above code will output something like:
DOS Magic: 0x5A4D
PE Header Offset: 0x000000E0
Here, e_magic value 0x5A4D confirms a valid PE file; e_lfanew points to the file offset of the PE header (e.g., 0xE0).
PE Header
The PE header (also known as the NT header) follows the DOS stub (a simple MS-DOS compatible code). The PE header is defined by the IMAGE_NT_HEADERS structure, containing a signature and two substructures: FileHeader and OptionalHeader.
The PE header signature is 0x50450000 (ASCII “PE\0\0”). FileHeader contains file metadata, such as target architecture and number of sections; OptionalHeader contains runtime information, such as entry point address and image base address.
The following is the C language definition of the PE header:
typedef struct _IMAGE_NT_HEADERS {
DWORD Signature; // Signature, value 0x50450000
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER OptionalHeader;
} IMAGE_NT_HEADERS;
typedef struct _IMAGE_FILE_HEADER {
WORD Machine; // Target architecture (e.g., 0x014C for x86)
WORD NumberOfSections; // Number of sections
DWORD TimeDateStamp; // File creation timestamp
DWORD PointerToSymbolTable; // Symbol table offset
DWORD NumberOfSymbols; // Number of symbols
WORD SizeOfOptionalHeader; // Optional header size
WORD Characteristics; // File attributes
} IMAGE_FILE_HEADER;
typedef struct _IMAGE_OPTIONAL_HEADER {
WORD Magic; // Magic number (0x10B for PE32, 0x20B for PE32+)
BYTE MajorLinkerVersion; // Linker major version
BYTE MinorLinkerVersion; // Linker minor version
DWORD SizeOfCode; // Code section size
DWORD SizeOfInitializedData; // Initialized data size
DWORD SizeOfUninitializedData; // Uninitialized data size
DWORD AddressOfEntryPoint; // Entry point RVA
DWORD BaseOfCode; // Code base address
DWORD BaseOfData; // Data base address
DWORD ImageBase; // Image base address
DWORD SectionAlignment; // Section alignment in memory
DWORD FileAlignment; // Section alignment in file
// ... other fields
} IMAGE_OPTIONAL_HEADER;
Code Example: Parsing the PE Header
Use pefile to parse the PE header:
# Get NT headers
nt_headers = pe.NT_HEADERS
# Print signature and FileHeader fields
print(f"PE Signature: 0x{nt_headers.Signature:08X}")
print(f"Machine: 0x{nt_headers.FileHeader.Machine:04X}")
print(f"Number of Sections: {nt_headers.FileHeader.NumberOfSections}")
print(f"Time Date Stamp: 0x{nt_headers.FileHeader.TimeDateStamp:08X}")
# Print OptionalHeader fields
print(f"Entry Point: 0x{nt_headers.OptionalHeader.AddressOfEntryPoint:08X}")
print(f"Image Base: 0x{nt_headers.OptionalHeader.ImageBase:08X}")
Sample output:
PE Signature: 0x50450000
Machine: 0x014C
Number of Sections: 6
Time Date Stamp: 0x5F7B1C2A
Entry Point: 0x00001234
Image Base: 0x00400000
Here, Machine value 0x014C indicates x86 architecture, NumberOfSections indicates 6 sections, AddressOfEntryPoint is the relative virtual address (RVA) of the program entry point.
Section Table
The section table (Section Table) follows the optional header and describes the sections in the PE file (such as .text, .data, .rsrc). Each section is represented by the IMAGE_SECTION_HEADER structure, containing the section name, size, address, and attributes.
The following is the definition of the section header structure:
typedef struct _IMAGE_SECTION_HEADER {
BYTE Name[8]; // Section name (e.g., ".text")
union {
DWORD PhysicalAddress;
DWORD VirtualSize; // Virtual size of the section
} Misc;
DWORD VirtualAddress; // Section RVA
DWORD SizeOfRawData; // Section size in file
DWORD PointerToRawData; // Section offset in file
DWORD PointerToRelocations; // Relocation table offset
DWORD PointerToLinenumbers; // Line number table offset
WORD NumberOfRelocations; // Number of relocation entries
WORD NumberOfLinenumbers; // Number of line numbers
DWORD Characteristics; // Section attributes (e.g., executable, writable)
} IMAGE_SECTION_HEADER;
Common sections include:
.text: Contains executable code, usually read-only and executable..data: Contains initialized data, readable and writable..bss: Contains uninitialized data, only occupies virtual space..rsrc: Contains resources, such as icons and dialogs..idata: Contains import table..edata: Contains export table.
Code Example: Parsing the Section Table
# Iterate through the section table
for section in pe.sections:
print(f"Section Name: {section.Name.decode().rstrip('\x00')}")
print(f"Virtual Address: 0x{section.VirtualAddress:08X}")
print(f"Virtual Size: 0x{section.Misc_VirtualSize:08X}")
print(f"Raw Size: 0x{section.SizeOfRawData:08X}")
print(f"Characteristics: 0x{section.Characteristics:08X}")
print("-" * 40)
Sample output:
Section Name: .text
Virtual Address: 0x00001000
Virtual Size: 0x0000A123
Raw Size: 0x0000A200
Characteristics: 0x60000020
----------------------------------------
Section Name: .data
Virtual Address: 0x0000C000
Virtual Size: 0x00001234
Raw Size: 0x00001400
Characteristics: 0xC0000040
----------------------------------------
Section Name: .rsrc
Virtual Address: 0x0000E000
Virtual Size: 0x00000500
Raw Size: 0x00000600
Characteristics: 0x40000040
----------------------------------------
The Characteristics field defines section attributes, for example:
0x60000020: Indicates code section, executable, read-only.0xC0000040: Indicates data section, readable and writable.
Import Table
The import table (Import Table) records the dynamic link libraries (DLLs) and external functions that the program depends on at runtime. The import table is located in the IMAGE_DIRECTORY_ENTRY_IMPORT entry of the data directory (OptionalHeader.DataDirectory), pointing to an array of IMAGE_IMPORT_DESCRIPTOR structures.
Each IMAGE_IMPORT_DESCRIPTOR describes a DLL, containing the following fields:
typedef struct _IMAGE_IMPORT_DESCRIPTOR {
union {
DWORD Characteristics;
DWORD OriginalFirstThunk; // RVA pointing to INT (Import Name Table)
};
DWORD TimeDateStamp; // Timestamp
DWORD ForwarderChain; // Forwarder chain
DWORD Name; // RVA of DLL name
DWORD FirstThunk; // RVA pointing to IAT (Import Address Table)
} IMAGE_IMPORT_DESCRIPTOR;
How the import table works:
OriginalFirstThunkpoints to the Import Name Table (INT), containing function names or ordinals.FirstThunkpoints to the Import Address Table (IAT), filled with actual function addresses by the loader when the program is loaded.Namepoints to the DLL name string.
Code Example: Parsing the Import Table
# Iterate through the import table
for entry in pe.DIRECTORY_ENTRY_IMPORT:
print(f"DLL Name: {entry.dll.decode()}")
for imp in entry.imports:
if imp.name:
print(f" Function: {imp.name.decode()} @ 0x{imp.address:08X}")
else:
print(f" Ordinal: {imp.ordinal} @ 0x{imp.address:08X}")
Sample output:
DLL Name: KERNEL32.dll
Function: GetModuleHandleA @ 0x0040A000
Function: ExitProcess @ 0x0040A004
Ordinal: 123 @ 0x0040A008
----------------------------------------
DLL Name: USER32.dll
Function: MessageBoxA @ 0x0040A100
----------------------------------------
This code lists each DLL and its imported function names or ordinals, along with the corresponding IAT addresses.
Comprehensive Analysis: Parsing a Complete PE File
The following is a comprehensive example combining the above steps to fully parse the key structures of a PE file:
import pefile
def analyze_pe(file_path):
pe = pefile.PE(file_path)
# DOS Header
print("=== DOS Header ===")
print(f"Magic: 0x{pe.DOS_HEADER.e_magic:04X}")
print(f"PE Offset: 0x{pe.DOS_HEADER.e_lfanew:08X}")
# PE Header
print("\n=== NT Headers ===")
print(f"Signature: 0x{pe.NT_HEADERS.Signature:08X}")
print(f"Machine: 0x{pe.FileHeader.Machine:04X}")
print(f"Number of Sections: {pe.FileHeader.NumberOfSections}")
print(f"Entry Point: 0x{pe.OptionalHeader.AddressOfEntryPoint:08X}")
print(f"Image Base: 0x{pe.OptionalHeader.ImageBase:08X}")
# Section Table
print("\n=== Sections ===")
for section in pe.sections:
print(f"Name: {section.Name.decode().rstrip('\x00')}")
print(f" Virtual Address: 0x{section.VirtualAddress:08X}")
print(f" Virtual Size: 0x{section.Misc_VirtualSize:08X}")
print(f" Raw Size: 0x{section.SizeOfRawData:08X}")
print(f" Characteristics: 0x{section.Characteristics:08X}")
# Import Table
print("\n=== Import Table ===")
for entry in pe.DIRECTORY_ENTRY_IMPORT:
print(f"DLL: {entry.dll.decode()}")
for imp in entry.imports:
if imp.name:
print(f" Function: {imp.name.decode()} @ 0x{imp.address:08X}")
else:
print(f" Ordinal: {imp.ordinal} @ 0x{imp.address:08X}")
# Run analysis
analyze_pe("sample.exe")
This code outputs the complete structural information of a PE file, covering the DOS header, PE header, section table, and import table.
Notes for PE File Analysis
- RVA to File Offset Conversion: RVA (Relative Virtual Address) is an address in memory, which needs to be converted to a file offset via the section table. For example, if the RVA falls in the
.textsection, file offset = RVA –VirtualAddress+PointerToRawData. - 64-bit PE Files: 64-bit PE files use
IMAGE_OPTIONAL_HEADER64, with slightly different field sizes and layouts, and fields likeImageBaseare 64-bit. - Corrupted or Obfuscated Files: Malware may intentionally modify header fields, requiring manual verification with a hex editor.
- Data Directory: Other tables in the PE file (such as export table, resource table) are located in
OptionalHeader.DataDirectoryand can be parsed as needed.
ELF File Format (Executable and Linkable Format)
The ELF file format is widely used in Linux, Unix, and embedded systems for executable files, supporting multiple architectures (such as x86, ARM, RISC-V). ELF files serve various purposes, including executable files, shared libraries (.so), object files (.o), and core dumps. This section will explain the ELF file structure in detail, including headers, segments, symbol tables, and relocation entries, and provide Python code for parsing.
ELF File Structure Overview
An ELF file consists of the following main parts:
- ELF Header: Describes the file’s architecture, byte order, and type.
- Program Header Table: Defines segments required at runtime (such as code segments, data segments).
- Section Header Table: Describes the file’s sections (such as
.text,.data,.symtab). - Segment and Section Content: Actual code, data, and metadata.
- Symbol Table: Records symbol information for functions and variables.
- Relocation Table: Records addresses that need adjustment during linking.
The typical layout of an ELF file is as follows:
+--------------------+
| ELF Header |
+--------------------+
| Program Header Table |
+--------------------+
| Section 1 (.text) |
+--------------------+
| Section 2 (.data) |
+--------------------+
| Section 3 (.bss) |
+--------------------+
| Symbol Table |
+--------------------+
| Relocation Table |
+--------------------+
| Section Header Table |
+--------------------+
ELF Header
The ELF header (ElfN_Ehdr, where N is 32 or 64) is at the beginning of the file, defining global file attributes. The following is the C language definition of a 32-bit ELF header:
typedef struct {
unsigned char e_ident[16]; // Magic number and identification information
Elf32_Half e_type; // File type (e.g., executable, shared library)
Elf32_Half e_machine; // Target architecture
Elf32_Word e_version; // File version
Elf32_Addr e_entry; // Entry point address
Elf32_Off e_phoff; // Program header table offset
Elf32_Off e_shoff; // Section header table offset
Elf32_Word e_flags; // Processor-specific flags
Elf32_Half e_ehsize; // ELF header size
Elf32_Half e_phentsize; // Program header entry size
Elf32_Half e_phnum; // Number of program header entries
Elf32_Half e_shentsize; // Section header entry size
Elf32_Half e_shnum; // Number of section header entries
Elf32_Half e_shstrndx; // Section name string table index
} Elf32_Ehdr;
The first few bytes of e_ident define basic file attributes:
e_ident[0-3]: Magic number0x7F454C46(ASCII0x7F ELF).e_ident[4]: 1 for 32-bit, 2 for 64-bit.e_ident[5]: 1 for little-endian, 2 for big-endian.
Code Example: Parsing the ELF Header
Use pyelftools to parse the ELF header:
from elftools.elf.elffile import ELFFile
with open("sample.elf", "rb") as f:
elf = ELFFile(f)
# Get ELF header
header = elf.header
print("=== ELF Header ===")
print(f"Magic: {' '.join(f'{b:02X}' for b in header['e_ident'][:4])}")
print(f"Class: {'32-bit' if header['e_ident'][4] == 1 else '64-bit'}")
print(f"Endian: {'Little' if header['e_ident'][5] == 1 else 'Big'}")
print(f"Type: {header['e_type']}")
print(f"Machine: {header['e_machine']}")
print(f"Entry Point: 0x{header['e_entry']:08X}")
print(f"Program Header Offset: 0x{header['e_phoff']:08X}")
print(f"Section Header Offset: 0x{header['e_shoff']:08X}")
Sample output:
=== ELF Header ===
Magic: 7F 45 4C 46
Class: 64-bit
Endian: Little
Type: ET_EXEC
Machine: EM_X86_64
Entry Point: 0x000000400080
Program Header Offset: 0x000000000040
Section Header Offset: 0x000000001234
Program Header Table
The program header table (Program Header Table) defines segments required at runtime, each described by the ElfN_Phdr structure. Segments are logical units in memory, such as code segments (PT_LOAD), dynamic linking information (PT_DYNAMIC), etc.
The following is the definition of a program header:
typedef struct {
Elf32_Word p_type; // Segment type (e.g., PT_LOAD)
Elf32_Off p_offset; // Segment offset in file
Elf32_Addr p_vaddr; // Segment virtual address
Elf32_Addr p_paddr; // Segment physical address
Elf32_Word p_filesz; // Segment size in file
Elf32_Word p_memsz; // Segment size in memory
Elf32_Word p_flags; // Segment flags (readable, writable, executable)
Elf32_Word p_align; // Alignment requirement
} Elf32_Phdr;
Common segment types include:
PT_LOAD: Loadable segments (such as code or data).PT_DYNAMIC: Dynamic linking information.PT_INTERP: Specifies interpreter path (e.g.,/lib/ld-linux.so.2).
Code Example: Parsing the Program Header Table
print("\n=== Program Headers ===")
for segment in elf.iter_segments():
print(f"Type: {segment.header['p_type']}")
print(f" Offset: 0x{segment.header['p_offset']:08X}")
print(f" Virtual Address: 0x{segment.header['p_vaddr']:08X}")
print(f" File Size: 0x{segment.header['p_filesz']:08X}")
print(f" Memory Size: 0x{segment.header['p_memsz']:08X}")
print(f" Flags: {segment.header['p_flags']}")
Sample output:
=== Program Headers ===
Type: PT_LOAD
Offset: 0x00000000
Virtual Address: 0x000000400000
File Size: 0x00000890
Memory Size: 0x00000890
Flags: 5 (Read + Execute)
Type: PT_LOAD
Offset: 0x00001000
Virtual Address: 0x000000600000
File Size: 0x00000200
Memory Size: 0x00000400
Flags: 6 (Read + Write)
Type: PT_INTERP
Offset: 0x00000238
Virtual Address: 0x000000400238
File Size: 0x0000001C
Memory Size: 0x0000001C
Flags: 4 (Read)
Section Header Table
The section header table (Section Header Table) describes the file’s sections, each defined by the ElfN_Shdr structure. Sections are logical units for linking and debugging, such as .text (code), .data (data), .symtab (symbol table), etc.
The following is the definition of a section header:
typedef struct {
Elf32_Word sh_name; // Section name (index in string table)
Elf32_Word sh_type; // Section type
Elf32_Word sh_flags; // Section flags
Elf32_Addr sh_addr; // Section virtual address
Elf32_Off sh_offset; // Section offset in file
Elf32_Word sh_size; // Section size
Elf32_Word sh_link; // Associated section index
Elf32_Word sh_info; // Additional information
Elf32_Word sh_addralign; // Address alignment
Elf32_Word sh_entsize; // Entry size (if section contains fixed-size entries)
} Elf32_Shdr;
Common sections include:
.text: Executable code..data: Initialized data..bss: Uninitialized data..symtab: Symbol table..strtab: String table..rela.text: Relocation entries.
Code Example: Parsing the Section Header Table
print("\n=== Sections ===")
for section in elf.iter_sections():
print(f"Name: {section.name}")
print(f" Type: {section.header['sh_type']}")
print(f" Address: 0x{section.header['sh_addr']:08X}")
print(f" Offset: 0x{section.header['sh_offset']:08X}")
print(f" Size: 0x{section.header['sh_size']:08X}")
Sample output:
=== Sections ===
Name: .text
Type: SHT_PROGBITS
Address: 0x000000400080
Offset: 0x00000080
Size: 0x00000123
Name: .data
Type: SHT_PROGBITS
Address: 0x000000600000
Offset: 0x00001000
Size: 0x00000200
Name: .symtab
Type: SHT_SYMTAB
Address: 0x000000000000
Offset: 0x00002000
Size: 0x00000300
Symbol Table
The symbol table (.symtab or .dynsym) records symbol information for functions, variables, etc., in the program, each represented by the ElfN_Sym structure. The symbol table is crucial for linking and debugging.
The following is the definition of a symbol structure:
typedef struct {
Elf32_Word st_name; // Symbol name (index in string table)
Elf32_Addr st_value; // Symbol value (e.g., address)
Elf32_Word st_size; // Symbol size
unsigned char st_info; // Symbol type and binding
unsigned char st_other; // Symbol visibility
Elf32_Half st_shndx; // Section index where symbol resides
} Elf32_Sym;
The st_info field defines symbol binding (e.g., STB_GLOBAL) and type (e.g., STT_FUNC).
Code Example: Parsing the Symbol Table
from elftools.elf.sections import SymbolTableSection
print("\n=== Symbol Table ===")
for section in elf.iter_sections():
if isinstance(section, SymbolTableSection):
for symbol in section.iter_symbols():
print(f"Name: {symbol.name}")
print(f" Value: 0x{symbol['st_value']:08X}")
print(f" Size: {symbol['st_size']}")
print(f" Type: {symbol['st_info']['type']}")
print(f" Bind: {symbol['st_info']['bind']}")
Sample output:
=== Symbol Table ===
Name: main
Value: 0x000000400080
Size: 32
Type: STT_FUNC
Bind: STB_GLOBAL
Name: global_var
Value: 0x000000600000
Size: 4
Type: STT_OBJECT
Bind: STB_GLOBAL
Relocation Table
The relocation table (.rel or .rela sections) records address references that need adjustment during linking. Each relocation entry is represented by the ElfN_Rel or ElfN_Rela structure.
The following is the definition of relocation structures:
typedef struct {
Elf32_Addr r_offset; // Address needing relocation
Elf32_Word r_info; // Symbol index and relocation type
} Elf32_Rel;
typedef struct {
Elf32_Addr r_offset; // Address needing relocation
Elf32_Word r_info; // Symbol index and relocation type
Elf32_Sword r_addend;// Addend
} Elf32_Rela;
The r_info field contains the symbol index and relocation type (e.g., R_X86_64_PC32).
Code Example: Parsing the Relocation Table
from elftools.elf.relocation import RelocationSection
print("\n=== Relocations ===")
for section in elf.iter_sections():
if isinstance(section, RelocationSection):
print(f"Relocation Section: {section.name}")
for reloc in section.iter_relocations():
print(f" Offset: 0x{reloc['r_offset']:08X}")
print(f" Type: {reloc['r_info_type']}")
if 'r_addend' in reloc:
print(f" Addend: {reloc['r_addend']}")
Sample output:
=== Relocations ===
Relocation Section: .rela.text
Offset: 0x000000400085
Type: R_X86_64_PC32
Addend: -4
Comprehensive Analysis: Parsing a Complete ELF File
The following is a comprehensive example for parsing an ELF file:
from elftools.elf.elffile import ELFFile
from elftools.elf.sections import SymbolTableSection
from elftools.elf.relocation import RelocationSection
def analyze_elf(file_path):
with open(file_path, "rb") as f:
elf = ELFFile(f)
# ELF Header
print("=== ELF Header ===")
header = elf.header
print(f"Magic: {' '.join(f'{b:02X}' for b in header['e_ident'][:4])}")
print(f"Class: {'32-bit' if header['e_ident'][4] == 1 else '64-bit'}")
print(f"Entry Point: 0x{header['e_entry']:08X}")
# Program Header Table
print("\n=== Program Headers ===")
for segment in elf.iter_segments():
print(f"Type: {segment.header['p_type']}")
print(f" Virtual Address: 0x{segment.header['p_vaddr']:08X}")
# Section Header Table
print("\n=== Sections ===")
for section in elf.iter_sections():
print(f"Name: {section.name}")
print(f" Address: 0x{section.header['sh_addr']:08X}")
# Symbol Table
print("\n=== Symbol Table ===")
for section in elf.iter_sections():
if isinstance(section, SymbolTableSection):
for symbol in section.iter_symbols():
print(f"Name: {symbol.name}")
print(f" Value: 0x{symbol['st_value']:08X}")
# Relocation Table
print("\n=== Relocations ===")
for section in elf.iter_sections():
if isinstance(section, RelocationSection):
print(f"Section: {section.name}")
for reloc in section.iter_relocations():
print(f" Offset: 0x{reloc['r_offset']:08X}")
# Run analysis
analyze_elf("sample.elf")
This code parses the complete structure of an ELF file, covering headers, segments, sections, symbol tables, and relocation tables.
Notes for ELF File Analysis
- 32-bit vs 64-bit: 64-bit ELF files use
Elf64_*structures, with larger field sizes, requiring attention to address and offset handling. - Dynamic Linking: Dynamic ELF files rely on the
.dynamicsection andPT_DYNAMICsegment, requiring parsing of the dynamic symbol table (.dynsym) and dynamic string table (.dynstr). - Section vs Segment Relationship: Segments (Program Header) are for runtime, sections (Section Header) are for linking and debugging, and they may overlap.
- Obfuscated Files: Malware may remove symbol tables or modify header fields, requiring analysis with disassembly tools (such as IDA Pro).
Mach-O File Format
Mach-O (Mach Object) is the executable file format used in macOS and iOS systems, supporting multi-architecture (Fat Binary), executable files, dynamic libraries (.dylib), and object files. Mach-O’s design is complex but efficient, widely used in the Apple ecosystem. This section will explain the Mach-O structure in detail, including headers, load commands, and dynamic linking mechanisms, and provide Python code for parsing.
Mach-O File Structure Overview
A Mach-O file consists of the following main parts:
- Mach-O Header: Defines file type, architecture, and number of load commands.
- Load Commands: Describe segments, symbol tables, dynamic linking information, etc.
- Segments and Sections: Contain code, data, and metadata.
- Symbol Table: Records symbol information for functions and variables.
- Dynamic Linking Information: Defines shared libraries and relocation requirements.
The typical layout of a Mach-O file is as follows:
+--------------------+
| Mach-O Header |
+--------------------+
| Load Commands |
+--------------------+
| Segment 1 (__TEXT)|
+--------------------+
| Segment 2 (__DATA)|
+--------------------+
| Symbol Table |
+--------------------+
| Dynamic Linker Info |
+--------------------+
Mach-O Header
The Mach-O header (mach_header or mach_header_64) is at the beginning of the file, defining global file attributes. The following is the C language definition of a 32-bit Mach-O header:
struct mach_header {
uint32_t magic; // Magic number (e.g., 0xFEEDFACE)
int32_t cputype; // CPU type (e.g., CPU_TYPE_X86)
int32_t cpusubtype; // CPU subtype
uint32_t filetype; // File type (e.g., MH_EXECUTE)
uint32_t ncmds; // Number of load commands
uint32_t sizeofcmds; // Total size of load commands
uint32_t flags; // File flags
};
The 64-bit header (mach_header_64) includes an additional reserved field. Common magic numbers include:
0xFEEDFACE: 32-bit Mach-O.0xFEEDFACF: 64-bit Mach-O.0xCAFEBABE: Fat Binary (multi-architecture file).
Code Example: Parsing the Mach-O Header
Use macho_parser to parse the Mach-O header:
from macho_parser.macho import MachO
def parse_macho_header(file_path):
macho = MachO(file_path)
header = macho.header
print("=== Mach-O Header ===")
print(f"Magic: 0x{header.magic:08X}")
print(f"CPU Type: {header.cputype}")
print(f"File Type: {header.filetype}")
print(f"Number of Commands: {header.ncmds}")
print(f"Size of Commands: {header.sizeofcmds}")
parse_macho_header("sample.macho")
Sample output:
=== Mach-O Header ===
Magic: 0xFEEDFACF
CPU Type: CPU_TYPE_X86_64
File Type: MH_EXECUTE
Number of Commands: 12
Size of Commands: 672
Load Commands
Load commands (Load Commands) follow the header, describing segments, symbol tables, dynamic linkers, etc. Each load command starts with the load_command structure, with the specific type determined by the cmd field.
The following is the basic structure of a load command:
struct load_command {
uint32_t cmd; // Command type (e.g., LC_SEGMENT_64)
uint32_t cmdsize; // Command size
};
Common load commands include:
LC_SEGMENT/LC_SEGMENT_64: Defines segments and sections.LC_SYMTAB: Specifies symbol table.LC_DYLD_INFO: Dynamic linking information.LC_LOAD_DYLIB: Loads shared library.LC_MAIN: Specifies entry point.
A segment (Segment) is defined by segment_command_64, containing multiple sections (Section). The following is the definition of a segment structure:
struct segment_command_64 {
uint32_t cmd; // LC_SEGMENT_64
uint32_t cmdsize; // Command size
char segname[16]; // Segment name (e.g., __TEXT)
uint64_t vmaddr; // Virtual address
uint64_t vmsize; // Virtual size
uint64_t fileoff; // File offset
uint64_t filesize; // File size
uint32_t maxprot; // Maximum protection permissions
uint32_t initprot; // Initial protection permissions
uint32_t nsects; // Number of sections
uint32_t flags; // Flags
};
Sections (Section) follow the segment structure, defining subregions of code or data, such as __text, __data.
Code Example: Parsing Load Commands and Segments
def parse_load_commands(file_path):
macho = MachO(file_path)
print("\n=== Load Commands ===")
for cmd in macho.load_commands:
print(f"Command: {cmd.cmd}")
if cmd.cmd == "LC_SEGMENT_64":
print(f" Segment Name: {cmd.segname}")
print(f" Virtual Address: 0x{cmd.vmaddr:016X}")
print(f" File Offset: 0x{cmd.fileoff:016X}")
print(f" Sections:")
for section in cmd.sections:
print(f" Section: {section.sectname} ({section.segname})")
print(f" Address: 0x{section.addr:016X}")
print(f" Size: 0x{section.size:016X}")
parse_load_commands("sample.macho")
Sample output:
=== Load Commands ===
Command: LC_SEGMENT_64
Segment Name: __TEXT
Virtual Address: 0x0000000100000000
File Offset: 0x0000000000000000
Sections:
Section: __text (__TEXT)
Address: 0x0000000100001000
Size: 0x0000000000000123
Command: LC_SEGMENT_64
Segment Name: __DATA
Virtual Address: 0x0000000100002000
File Offset: 0x0000000000002000
Sections:
Section: __data (__DATA)
Address: 0x0000000100002000
Size: 0x0000000000000200
Command: LC_SYMTAB
Command: LC_LOAD_DYLIB
Symbol Table
The symbol table is specified by the LC_SYMTAB load command, containing symbol information for functions and variables. Each symbol is represented by the nlist or nlist_64 structure.
The following is the definition of a 64-bit symbol structure:
struct nlist_64 {
uint32_t n_strx; // String table index
uint8_t n_type; // Symbol type
uint8_t n_sect; // Section index
uint16_t n_desc; // Descriptor
uint64_t n_value; // Symbol value (e.g., address)
};
Code Example: Parsing the Symbol Table
def parse_symbol_table(file_path):
macho = MachO(file_path)
print("\n=== Symbol Table ===")
for cmd in macho.load_commands:
if cmd.cmd == "LC_SYMTAB":
for symbol in cmd.symbols:
print(f"Name: {symbol.name}")
print(f" Value: 0x{symbol.n_value:016X}")
print(f" Type: 0x{symbol.n_type:02X}")
parse_symbol_table("sample.macho")
Sample output:
=== Symbol Table ===
Name: _main
Value: 0x0000000100001000
Type: 0x0F
Name: _global_var
Value: 0x0000000100002000
Type: 0x01
Dynamic Linking
Dynamic linking information is specified by the LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command, containing relocation, binding, and shared library information. The LC_LOAD_DYLIB command specifies the dynamic libraries to load.
Code Example: Parsing Dynamic Linking Information
def parse_dynamic_info(file_path):
macho = MachO(file_path)
print("\n=== Dynamic Link Info ===")
for cmd in macho.load_commands:
if cmd.cmd == "LC_LOAD_DYLIB":
print(f"Dynamic Library: {cmd.name}")
elif cmd.cmd == "LC_DYLD_INFO_ONLY":
print(f"Rebase Offset: 0x{cmd.rebase_off:08X}")
print(f"Bind Offset: 0x{cmd.bind_off:08X}")
parse_dynamic_info("sample.macho")
Sample output:
=== Dynamic Link Info ===
Dynamic Library: /usr/lib/libc.dylib
Rebase Offset: 0x00003000
Bind Offset: 0x00004000
Comprehensive Analysis: Parsing a Complete Mach-O File
The following is a comprehensive example for parsing a Mach-O file:
from macho_parser.macho import MachO
def analyze_macho(file_path):
macho = MachO(file_path)
# Header
print("=== Mach-O Header ===")
header = macho.header
print(f"Magic: 0x{header.magic:08X}")
print(f"CPU Type: {header.cputype}")
# Load Commands and Segments
print("\n=== Load Commands ===")
for cmd in macho.load_commands:
print(f"Command: {cmd.cmd}")
if cmd.cmd == "LC_SEGMENT_64":
print(f" Segment: {cmd.segname}")
for section in cmd.sections:
print(f" Section: {section.sectname}")
# Symbol Table
print("\n=== Symbol Table ===")
for cmd in macho.load_commands:
if cmd.cmd == "LC_SYMTAB":
for symbol in cmd.symbols:
print(f"Name: {symbol.name}")
print(f" Value: 0x{symbol.n_value:016X}")
# Dynamic Linking
print("\n=== Dynamic Link Info ===")
for cmd in macho.load_commands:
if cmd.cmd == "LC_LOAD_DYLIB":
print(f"Dynamic Library: {cmd.name}")
# Run analysis
analyze_macho("sample.macho")
This code parses the complete structure of a Mach-O file, covering headers, load commands, segments, symbol tables, and dynamic linking information.
Notes for Mach-O File Analysis
- Fat Binary: Multi-architecture Mach-O files start with
fat_header, requiring parsing of multiplemach_header. - Address Space Layout Randomization (ASLR): Mach-O files may use randomized base addresses, requiring parsing of relocation information in
LC_DYLD_INFO. - Code Signing: macOS requires executable file signatures, requiring checking the
LC_CODE_SIGNATUREcommand. - Encrypted Segments: Some segments in iOS applications may be encrypted, requiring tools (such as
dumpdecrypted) for decryption.
Comprehensive Comparison and Reverse Engineering Practices
Comparison of PE, ELF, and Mach-O
| Feature | PE (Windows) | ELF (Linux/Unix) | Mach-O (macOS/iOS) |
|---|---|---|---|
| Operating System | Windows | Linux, Unix, embedded systems | macOS, iOS |
| Magic Number | 0x5A4D (MZ), 0x50450000 (PE) | 0x7F454C46 (0x7F ELF) | 0xFEEDFACF (64-bit) |
| Header Structure | DOS Header, NT Headers | ELF Header | Mach-O Header |
| Segment/Section Organization | Sections | Segments and Sections | Segments and Sections |
| Import/Dynamic Linking | Import Table | Dynamic Segment (PT_DYNAMIC) | Load Commands (LC_LOAD_DYLIB) |
| Symbol Table | Optional, usually in debug information | .symtab, .dynsym | LC_SYMTAB |
| Relocation | Relocation Table | .rel, .rela sections | LC_DYLD_INFO |
Reverse Engineering Practices
In actual reverse engineering, analyzing PE, ELF, or Mach-O files typically involves the following steps:
- File Validation: Check magic numbers and header integrity to confirm file format and architecture.
- Structure Parsing: Extract headers, sections/segments, symbol tables, and dynamic linking information.
- Code Extraction: Locate the
.textor__textsection and extract machine code. - Symbol Analysis: Check symbol tables to identify key functions and variables.
- Dynamic Analysis: Run the program, monitor imported function calls and dynamic linking behavior.
- Disassembly and Debugging: Use tools like IDA Pro, Ghidra, or Radare2 to disassemble code and set breakpoints for debugging.
The following is a comprehensive example showing how to combine parsing logic for all three formats:
import pefile
from elftools.elf.elffile import ELFFile
from macho_parser.macho import MachO
def analyze_file(file_path):
try:
# Try parsing PE file
pe = pefile.PE(file_path)
print("Detected PE File")
print(f"Entry Point: 0x{pe.OptionalHeader.AddressOfEntryPoint:08X}")
for section in pe.sections:
print(f"Section: {section.Name.decode().rstrip('\x00')}")
return
except:
pass
try:
# Try parsing ELF file
with open(file_path, "rb") as f:
elf = ELFFile(f)
print("Detected ELF File")
print(f"Entry Point: 0x{elf.header['e_entry']:08X}")
for section in elf.iter_sections():
print(f"Section: {section.name}")
return
except:
pass
try:
# Try parsing Mach-O file
macho = MachO(file_path)
print("Detected Mach-O File")
print(f"CPU Type: {macho.header.cputype}")
for cmd in macho.load_commands:
if cmd.cmd == "LC_SEGMENT_64":
print(f"Segment: {cmd.segname}")
return
except:
pass
print("Unknown file format")
# Test different files
analyze_file("sample.exe")
analyze_file("sample.elf")
analyze_file("sample.macho")
This code attempts to parse PE, ELF, and Mach-O files sequentially, outputting basic information, suitable for quick format detection.
Recommended Reverse Engineering Tools
- PE Analysis: PEiD, CFF Explorer, pefile
- ELF Analysis: readelf, objdump, pyelftools
- Mach-O Analysis: otool, MachOView, macho_parser
- Disassembly and Debugging: IDA Pro, Ghidra, Radare2, x64dbg
- Dynamic Analysis: OllyDbg, GDB, LLDB, Frida
Introduction to Disassembly
Disassembly is the process of converting machine code (binary instructions) into human-readable assembly language, serving as a core technique in reverse engineering and debugging. Through disassembly, analysts can understand program logic, behavior, and potential vulnerabilities. Disassembly involves the following key aspects:
- Instruction Set Parsing: Understanding instruction formats and semantics for different architectures (such as x86, x64, ARM).
- Control Flow Analysis: Identifying program jumps, loops, and function calls to construct a control flow graph (CFG).
- Data Flow Analysis: Tracking data movement in registers, stack, and memory to understand program state.
This tutorial will explain these aspects step by step, provide Python code (using the Capstone disassembly engine) to parse real binary code, and combine tools like Radare2 and Ghidra for practical analysis.
Preparation
Before starting, ensure the following tools are installed:
- Python 3.x: For writing parsing scripts.
- Capstone: Disassembly engine (
pip install capstone). - Radare2: Open-source reverse engineering framework (install from the official website).
- Ghidra: NSA’s open-source reverse engineering tool.
- Sample binary files: Simple x86, x64, and ARM executables (can be obtained from open-source projects or compiled yourself).
- Hex editor: Such as HxD, for manual binary inspection.
Instruction Set Parsing
An instruction set is the collection of basic operations a processor can execute, with different architectures (such as x86, x64, ARM) having unique instruction set architectures (ISA). This section introduces the instruction formats, common instructions, and disassembly methods for x86, x64, and ARM.
x86 Instruction Set
x86 is the 32-bit instruction set architecture used by Intel and AMD processors, widely applied in desktop and server systems. The x86 instruction set is complex, supporting various addressing modes and instruction lengths (1 to 15 bytes).
x86 Instruction Format
x86 instructions typically consist of the following parts:
- Prefix (optional): Modifies instruction behavior, such as lock (
lock) or repeat (rep). - Opcode: Specifies the instruction type, such as
mov,add. - Addressing Mode: Specifies operands (e.g., registers, memory addresses).
- Immediate Value: A constant embedded directly in the instruction.
Example instruction (mov eax, 0x1234) machine code:
B8 34 12 00 00
B8: Opcode, indicatingmovtoeax.34 12 00 00: Immediate value0x1234(little-endian).
Common x86 Instructions
The following are common x86 instruction categories and examples:
- Data Transfer:
mov eax, ebx: Copies the value ofebxtoeax.push eax: Pusheseaxonto the stack.pop ebx: Pops from the stack intoebx.
- Arithmetic Operations:
add eax, 0x10:eax += 0x10.sub ebx, ecx:ebx -= ecx.imul eax, ebx:eax *= ebx(signed multiplication).
- Logical Operations:
and eax, 0xFF:eax &= 0xFF.or ebx, ecx:ebx |= ecx.xor eax, eax:eax = 0(zeroing).
- Control Flow:
jmp 0x1000: Unconditional jump to address0x1000.call 0x2000: Calls a function, saving the return address.ret: Returns from a function.
- Comparison and Conditional Jumps:
cmp eax, ebx: Compareseaxandebx, setting flags.je 0x3000: Jumps to0x3000if equal (ZF=1).jne 0x3000: Jumps to0x3000if not equal (ZF=0).
Code Example: Disassembling x86 Code
Use Capstone to disassemble x86 machine code:
from capstone import *
# x86 machine code (mov eax, 0x1234; add eax, 0x10; jmp 0x1000)
CODE = b"\xB8\x34\x12\x00\x00\x83\xC0\x10\xE9\xF8\xEF\xFF\xFF"
# Initialize Capstone (x86, 32-bit)
md = Cs(CS_ARCH_X86, CS_MODE_32)
# Disassemble
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: mov eax, 0x1234
0x1005: add eax, 0x10
0x1008: jmp 0xff
x64 Instruction Set
x64 (or x86-64) is the 64-bit extension of x86, supporting larger address spaces and more registers. x64 is compatible with x86 in instruction encoding but introduces new features:
- 64-bit Registers:
rax,rbx, etc., extended to 64 bits, with new registersr8tor15. - RIP-Relative Addressing: Instruction pointer (RIP) used for position-independent code.
- REX Prefix: Used to access 64-bit registers or extended operands.
x64 Instruction Format
x64 instructions are similar to x86 but add the REX prefix (0x40-0x4F) to specify 64-bit operations or new registers. For example:
48 89 C8 // mov rax, rcx
48: REX prefix, indicating 64-bit operation.89:movopcode.C8: Addressing mode, specifyingraxandrcx.
Common x64 Instructions
x64 instructions are similar to x86, but operands are typically 64-bit. For example:
mov rax, 0x1234567890: Loads a 64-bit immediate intorax.lea rsi, [rip + 0x1000]: Loads a RIP-relative address intorsi.syscall: Triggers a system call (common on Linux).
Code Example: Disassembling x64 Code
from capstone import *
# x64 machine code (mov rax, 0x1234; syscall)
CODE = b"\x48\xC7\xC0\x34\x12\x00\x00\x0F\x05"
# Initialize Capstone (x64)
md = Cs(CS_ARCH_X86, CS_MODE_64)
# Disassemble
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: mov rax, 0x1234
0x1007: syscall
ARM Instruction Set
ARM is a RISC (Reduced Instruction Set Computing) architecture commonly used in embedded devices (such as phones, IoT devices). ARM instructions have a fixed length (32 bits, 16 bits in Thumb mode), with a concise instruction set emphasizing low power consumption.
ARM Instruction Format
ARM instructions typically include:
- Condition Code: Each instruction can have conditional execution (e.g.,
EQ,NE). - Opcode: Specifies the instruction type, such as
mov,ldr. - Operands: Registers or immediate values.
ARM has two modes:
- ARM Mode: 32-bit instructions, 4-byte aligned.
- Thumb Mode: 16-bit instructions, 2-byte aligned, higher code density.
Example instruction (mov r0, #0x12):
E3A00012 // ARM mode
2012 // Thumb mode
Common ARM Instructions
- Data Transfer:
mov r0, #0x12: Loads immediate0x12intor0.ldr r1, [r2]: Loads from memory pointed to byr2intor1.str r1, [r2]: Storesr1into memory pointed to byr2.
- Arithmetic Operations:
add r0, r1, #4:r0 = r1 + 4.sub r2, r3, r4:r2 = r3 - r4.
- Branching:
b label: Unconditional branch tolabel.bl label: Calls a function, saving return address tolr(link register).bx lr: Returns from a function.
Code Example: Disassembling ARM Code
from capstone import *
# ARM machine code (mov r0, #0x12; add r1, r2, #4; bl 0x1000)
CODE = b"\x12\x00\xA0\xE3\x04\x10\x82\xE2\x00\xF0\x9F\xE5"
# Initialize Capstone (ARM, 32-bit)
md = Cs(CS_ARCH_ARM, CS_MODE_ARM)
# Disassemble
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: mov r0, #0x12
0x1004: add r1, r2, #4
0x1008: bl 0x1000
ARM64 (AArch64)
ARM64 is the 64-bit extension of ARM, supporting larger registers (31 general-purpose registers x0-x30) and address space. Instruction format is similar to ARM but adds new instructions, such as ldr x0, [x1].
Code Example: Disassembling ARM64 Code
# ARM64 machine code (mov x0, #0x1234; ret)
CODE = b"\xE0\x88\x00\xD2\xC0\x03\x5F\xD6"
# Initialize Capstone (ARM64)
md = Cs(CS_ARCH_ARM64, CS_MODE_ARM)
# Disassemble
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: mov x0, #0x1234
0x1004: ret
Notes for Instruction Set Parsing
- Architecture Differences: x86/x64 is CISC (Complex Instruction Set Computing) with variable-length instructions; ARM is RISC with fixed-length instructions, requiring selection of the appropriate disassembly mode based on architecture.
- Byte Order: x86/x64 typically uses little-endian; ARM supports both little and big-endian (check file header).
- Obfuscated Code: Malware may use invalid instructions or jump obfuscation, requiring dynamic analysis.
- Tool Selection: Capstone is suitable for programmatic disassembly; Radare2 and Ghidra are suitable for interactive analysis.
Control Flow Analysis
Control flow analysis (Control Flow Analysis) studies the execution paths of a program, including jumps, loops, and function calls. The goal is to construct a control flow graph (CFG) representing the jump relationships between instructions. Control flow analysis is central to disassembly and reverse engineering, used to understand program logic and identify key code blocks.
Control Flow Basics
Control flow is driven by the following elements:
- Sequential Execution: Instructions execute in address order.
- Jumps: Change the program counter (PC) to jump to a non-sequential address.
- Function Calls: Call subroutines, saving the return address.
- Exception Handling: Handle interrupts or errors.
A control flow graph (CFG) divides the program into basic blocks, each a sequence of consecutive instructions entered only at the start and exited only at the end. Basic blocks are connected via jumps or calls.
Jump Analysis
Jump instructions alter program execution flow, divided into:
- Unconditional Jumps: Such as x86
jmp, ARMb. - Conditional Jumps: Such as x86
je,jne, ARMbeq,bne, depending on flag bits. - Indirect Jumps: Target address stored in a register or memory, such as
jmp eax.
Example: x86 Conditional Jump
The following code checks if eax is zero and decides whether to jump:
cmp eax, 0
je zero_label
mov ebx, 1
jmp end_label
zero_label:
mov ebx, 0
end_label:
Disassemble the machine code:
from capstone import *
CODE = b"\x83\xF8\x00\x74\x05\xBB\x01\x00\x00\x00\xEB\x02\xBB\x00\x00\x00\x00"
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: cmp eax, 0x0
0x1003: je 0x100a
0x1005: mov ebx, 0x1
0x100a: jmp 0x100e
0x100c: mov ebx, 0x0
Analysis:
cmp eax, 0: Compareseaxwith 0, setting the zero flag (ZF).je 0x100a: Jumps to0x100aif ZF=1 (eax==0).- Otherwise, executes
mov ebx, 1, then jumps toend_label. - If jumped to
zero_label, executesmov ebx, 0.
Loop Analysis
Loops are implemented via conditional jumps, typically involving the following structures:
- Pre-test Loop (while): Checks condition at the start of the loop.
- Post-test Loop (do-while): Checks condition at the end of the loop.
Example: x86 Loop
The following code implements a counting loop (ecx decrements from 10 to 0):
mov ecx, 10
loop_start:
dec ecx
cmp ecx, 0
jne loop_start
Disassemble:
CODE = b"\xB9\x0A\x00\x00\x00\xFF\xC9\x83\xF9\x00\x75\xF8"
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: mov ecx, 0xa
0x1005: dec ecx
0x1007: cmp ecx, 0x0
0x100a: jne 0x1004
Analysis:
mov ecx, 10: Initializes the counter.dec ecx:ecx--.cmp ecx, 0: Checks ifecxis 0.jne loop_start: Jumps toloop_startifecx!=0.
Function Call Analysis
Function calls are implemented via call (x86/x64), bl (ARM), etc.:
- Save the return address (x86 pushes to stack, ARM stores in
lr). - Jump to the function entry.
- After function execution, return via
ret(x86) orbx lr(ARM).
Example: x86 Function Call
call my_function
nop
my_function:
mov eax, 0x1234
ret
Disassemble:
CODE = b"\xE8\x03\x00\x00\x00\x90\xB8\x34\x12\x00\x00\xC3"
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: call 0x1008
0x1005: nop
0x1006: mov eax, 0x1234
0x100b: ret
Analysis:
call 0x1008: Saves return address (0x1005) to the stack and jumps to0x1008.mov eax, 0x1234: Function body.ret: Pops the return address from the stack and jumps to0x1005.
Control Flow Graph (CFG) Construction
A CFG represents basic blocks and their jump relationships. The following are steps to manually construct a CFG:
- Identify Basic Blocks: Starting from the entry point, end a basic block when encountering a jump or call.
- Determine Edges: Connect basic blocks based on jumps or calls.
- Label Conditions: Label conditional jumps as true/false branches.
Example: Constructing a CFG
Tools like Ghidra or Radare2 can automatically generate CFGs. For example, using Radare2:
r2 sample.bin
> aaa # Auto analysis
> agf # Generate control flow graph
Notes for Control Flow Analysis
- Indirect Jumps: Such as
jmp eax, where the target address is determined at runtime, requiring dynamic analysis. - Exception Handling: x86
intor ARMsvcmay trigger exceptions, requiring checking the interrupt vector table. - Optimized Code: Compilers may inline functions or unroll loops, increasing analysis difficulty.
- Obfuscation Techniques: Malware may use fake jumps or dynamically computed addresses, requiring combined disassembly and debugging.
Data Flow Analysis
Data flow analysis (Data Flow Analysis) tracks the movement of data in a program, including the use of registers, stack, and memory. The goal is to understand variable lifetimes, function parameter passing, and memory access patterns.
Register Analysis
Registers are high-speed storage units in the processor used for temporary data storage. Register usage in different architectures is as follows:
- x86/x64:
eax/rax: Accumulator, commonly used for return values.ebx/rbx: Base register, general-purpose.esp/rsp: Stack pointer.eip/rip: Instruction pointer.r8-r15(x64): Additional general-purpose registers.
- ARM:
r0-r3: Parameter passing and return values.r4-r11: General-purpose registers.sp: Stack pointer.lr: Link register, saves return address.pc: Program counter.
- ARM64:
x0-x7: Parameter passing and return values.x8-x28: General-purpose registers.sp: Stack pointer.lr(x30): Link register.
Example: x86 Register Usage
mov eax, 0x10
add ebx, eax
Analysis:
eaxstores0x10.ebxis increased by the value ofeax(ebx += 0x10).
Stack Analysis
The stack is used for function calls, local variables, and parameter passing. The stack is managed via the stack pointer (esp/rsp/sp), typically growing downward (from high to low addresses).
Stack Operations
- Push:
push(x86) orstr(ARM), decreases the stack pointer. - Pop:
pop(x86) orldr(ARM), increases the stack pointer. - Stack Frame: Functions allocate stack space for local variables and return addresses.
Example: x86 Stack Frame
push ebp
mov ebp, esp
sub esp, 0x10 ; Allocate 16 bytes for local variables
mov [ebp-4], 0x1234
call my_function
mov esp, ebp
pop ebp
ret
Analysis:
push ebp: Saves the caller’s base pointer.mov ebp, esp: Sets the current stack frame.sub esp, 0x10: Allocates 16 bytes of local variable space.[ebp-4]: Accesses a local variable.- Restores the stack frame and returns.
Code Example: Parsing Stack Operations
CODE = b"\x55\x89\xE5\x83\xEC\x10\xC7\x45\xFC\x34\x12\x00\x00\xE8\x00\x00\x00\x00\x89\xEC\x5D\xC3"
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: push ebp
0x1001: mov ebp, esp
0x1003: sub esp, 0x10
0x1006: mov dword ptr [ebp - 0x4], 0x1234
0x100d: call 0x1012
0x1012: mov esp, ebp
0x1014: pop ebp
0x1015: ret
Memory Analysis
Memory stores global variables, heap, and code. Memory access is performed via:
- Direct Addressing: Such as
mov eax, [0x1000]. - Indirect Addressing: Such as
mov eax, [ebx + 0x4]. - RIP-Relative Addressing (x64): Such as
mov rax, [rip + 0x1000].
Example: x86 Memory Access
mov eax, [ebx + 0x10]
mov [0x1000], eax
Analysis:
- Loads from memory address
ebx + 0x10intoeax. - Stores the value of
eaxinto address0x1000.
Data Flow Tracking such as:
Data flow analysis tracks the source and destination of variables, commonly using:
- Forward Analysis: Starts from assignment, tracking data usage.
- Backward Analysis: Traces back from usage to data source.
- Symbolic Execution: Simulates instructions to infer possible variable values.
Example: Data Flow Tracking
The following code computes eax = ebx + ecx:
mov eax, ebx
add eax, ecx
Forward Analysis:
eaxvalue comes fromebx.eaxis then added withecx.
Use Ghidra or Radare2 to automatically track data flow. For example, in Radare2:
r2 sample.bin
> aaa
> pdf @ main # Print main function disassembly
> ax # Show data flow references
Notes for Data Flow Analysis
- Calling Conventions: Different architectures and operating systems use different calling conventions (e.g., x86
cdecl,stdcall, ARM64 AAPCS). - Stack Overflow: Malware may intentionally corrupt the stack, requiring checking for abnormal access.
- Pointer Obfuscation: Indirect addressing may hide real addresses, requiring dynamic debugging.
- Optimization Impact: Compiler optimizations may reuse registers or inline variables, increasing analysis complexity.
Comprehensive Example: Parsing a Complete Function
The following is an x86 function containing control flow and data flow:
push ebp
mov ebp, esp
sub esp, 0x8
mov eax, [ebp + 8] ; First parameter
cmp eax, 0
je zero_label
mov [ebp - 4], eax
add eax, [ebp + 12] ; Second parameter
mov [ebp - 8], eax
jmp end_label
zero_label:
mov [ebp - 4], 0
mov [ebp - 8], 0
end_label:
mov eax, [ebp - 8]
mov esp, ebp
pop ebp
ret
Disassembly and analysis:
CODE = b"\x55\x89\xE5\x83\xEC\x08\x8B\x45\x08\x83\xF8\x00\x74\x0A\x89\x45\xFC\x03\x45\x0C\x89\x45\xF8\xEB\x06\xC7\x45\xFC\x00\x00\x00\x00\xC7\x45\xF8\x00\x00\x00\x00\x8B\x45\xF8\x89\xEC\x5D\xC3"
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
Output:
0x1000: push ebp
0x1001: mov ebp, esp
0x1003: sub esp, 0x8
0x1006: mov eax, dword ptr [ebp + 0x8]
0x1009: cmp eax, 0x0
0x100c: je 0x1018
0x100e: mov dword ptr [ebp - 0x4], eax
0x1011: add eax, dword ptr [ebp + 0xc]
0x1014: mov dword ptr [ebp - 0x8], eax
0x1017: jmp 0x101f
0x1019: mov dword ptr [ebp - 0x4], 0x0
0x1020: mov dword ptr [ebp - 0x8], 0x0
0x1027: mov eax, dword ptr [ebp - 0x8]
0x102a: mov esp, ebp
0x102c: pop ebp
0x102d: ret
Control Flow Analysis:
- Basic Blocks:
- Block 1:
0x1000-0x100c(stack frame setup and comparison). - Block 2:
0x100e-0x1017(non-zero branch). - Block 3:
0x1019-0x1020(zero branch). - Block 4:
0x1027-0x102d(return).
- Block 1:
- Jumps:
je 0x1018: Conditional jump to zero branch.jmp 0x101f: Unconditional jump to end.
Data Flow Analysis:
- Parameters:
[ebp + 8](first parameter),[ebp + 12](second parameter). - Local Variables:
[ebp - 4],[ebp - 8]. eax: Stores parameters, computation results, and return value.
Interactive Analysis Using Radare2
- Load the file:
r2 sample.bin - Analyze functions:
aaa # Auto analysis pdf @ main # Print main function - View control flow graph:
agf - Track data flow:
ax @ 0x1000 # Check data references at 0x1000
Reverse Engineering Practices
- Extract Code: Extract machine code from the
.textsection of PE/ELF/Mach-O files. - Disassemble: Use Capstone or Radare2 to disassemble the code.
- Build CFG: Manually or use Ghidra to generate a control flow graph.
- Track Data Flow: Identify key variables and function parameters.
- Dynamic Debugging: Use GDB, x64dbg, or LLDB to verify analysis results.
Introduction to Code Structure Recognition
Code structure recognition is a key step in reverse engineering, aimed at recovering high-level program structures from binary files, including functions, data structures, and resources. Recognizing these structures helps understand program logic, reconstruct source code, or discover hidden functionality. Code structure recognition involves the following:
- Function Recognition and Calling Conventions: Locate function boundaries, analyze parameter passing and return mechanisms.
- Data Structure Reconstruction: Infer the layout of structs and arrays, recover variable definitions.
- String and Resource Extraction: Extract strings, icons, and other embedded resources in the program.
This tutorial will explain these aspects step by step, provide Python code to parse real binary files, and combine tools like Radare2 and Ghidra for practical analysis.
Preparation
Before starting, ensure the following tools are installed:
- Python 3.x: For writing parsing scripts.
- Capstone: Disassembly engine (
pip install capstone). - pefile, pyelftools, macho_parser: Parse PE, ELF, Mach-O files (
pip install pefile pyelftools macho_parser). - Radare2: Open-source reverse engineering framework (install from the official website).
- Ghidra: NSA’s open-source reverse engineering tool.
- Sample binary files: PE, ELF, or Mach-O files for x86/x64/ARM architectures (can be obtained from open-source projects or compiled yourself).
- Hex editor: Such as HxD, for manual binary inspection.
Function Recognition and Calling Conventions
Functions are the core logical units of a program, and recognizing functions and their calling conventions is the starting point for understanding program behavior. Function recognition involves locating function entries, exits, and call relationships, while calling conventions define rules for parameter passing, return values, and stack management.
Function Recognition
Functions typically have the following characteristics:
- Prologue: Function start sets up the stack frame, such as
push ebp; mov ebp, esp(x86). - Epilogue: Function end restores the stack frame, such as
mov esp, ebp; pop ebp; ret. - Call Instructions: Other code calls functions via
call(x86/x64),bl(ARM). - Control Flow: Functions contain jumps, loops, and conditional branches internally.
- Symbol Table: Dynamically linked files may contain function names (e.g., ELF
.symtab).
Function Recognition Steps
- Locate Prologue and Epilogue: Search for common stack frame setup and restoration patterns.
- Track Call Instructions: Identify function entries from reverse references of
callorblinstructions. - Analyze Control Flow: Construct the function’s control flow graph (CFG) to confirm function boundaries.
- Check Symbol Table: Extract exported functions or debug symbols.
Example: x86 Function Prologue and Epilogue
The following is a typical x86 function:
push ebp
mov ebp, esp
sub esp, 0x10 ; Allocate space for local variables
; Function body
mov esp, ebp
pop ebp
ret
Code Example: Using Capstone to Recognize Functions
The following code disassembles binary code and detects function prologue and epilogue:
from capstone import *
# Sample code (containing one function)
CODE = b"\x55\x89\xE5\x83\xEC\x10\xB8\x34\x12\x00\x00\x89\xEC\x5D\xC3"
# Initialize Capstone (x86, 32-bit)
md = Cs(CS_ARCH_X86, CS_MODE_32)
# Disassemble and detect functions
print("=== Function Analysis ===")
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
if instr.mnemonic == "push" and instr.op_str == "ebp":
print(" -> Function Prologue Detected")
elif instr.mnemonic == "ret":
print(" -> Function Epilogue Detected")
Output:
=== Function Analysis ===
0x1000: push ebp
-> Function Prologue Detected
0x1001: mov ebp, esp
0x1003: sub esp, 0x10
0x1006: mov eax, 0x1234
0x100b: mov esp, ebp
0x100d: pop ebp
0x100e: ret
-> Function Epilogue Detected
Using Radare2 to Recognize Functions
Radare2 can automatically recognize functions:
r2 sample.bin
> aaa # Auto analysis
> afl # List all functions
> pdf @ main # Disassemble main function
Sample output:
[0x00401000]> afl
0x00401000 1 32 main
0x00401234 3 64 sub.my_function
Calling Conventions
Calling conventions define rules for parameter passing, return values, and stack management during function calls. Common calling conventions include:
- x86:
- cdecl: Parameters pushed from right to left, caller cleans stack, return value in
eax. - stdcall: Parameters pushed from right to left, callee cleans stack, return value in
eax(common in Win32 API). - fastcall: First two parameters via
ecxandedx, rest pushed, caller cleans stack.
- cdecl: Parameters pushed from right to left, caller cleans stack, return value in
- x64:
- Microsoft x64: First four parameters via
rcx,rdx,r8,r9, rest pushed, caller cleans stack, return value inrax. - System V AMD64 (Linux): First six parameters via
rdi,rsi,rdx,rcx,r8,r9, rest pushed, return value inrax.
- Microsoft x64: First four parameters via
- ARM:
- AAPCS: First four parameters via
r0-r3, rest pushed, return value inr0.
- AAPCS: First four parameters via
- ARM64:
- AAPCS64: First eight parameters via
x0-x7, rest pushed, return value inx0.
- AAPCS64: First eight parameters via
Example: x86 cdecl Call
The following code calls a function with two parameters:
push 0x2
push 0x1
call my_function
add esp, 0x8 ; Caller cleans stack
Analysis:
- Parameters
0x1and0x2pushed from right to left. callsaves return address and jumps.add esp, 0x8cleans 8 bytes of stack space (two parameters).
Code Example: Parsing Calling Convention
from capstone import *
CODE = b"\x6A\x02\x6A\x01\xE8\x00\x00\x00\x00\x83\xC4\x08"
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
if instr.mnemonic == "push":
print(" -> Parameter Push")
elif instr.mnemonic == "call":
print(" -> Function Call")
elif instr.mnemonic == "add" and "esp" in instr.op_str:
print(" -> Stack Cleanup (cdecl)")
Output:
0x1000: push 0x2
-> Parameter Push
0x1002: push 0x1
-> Parameter Push
0x1004: call 0x1009
-> Function Call
0x1009: add esp, 0x8
-> Stack Cleanup (cdecl)
Example: x64 Microsoft Call
mov rcx, 0x1
mov rdx, 0x2
call my_function
Analysis:
- Parameters
0x1and0x2passed viarcxandrdx. - No stack cleanup needed (caller responsible).
Notes for Function Recognition and Calling Conventions
- Inline Functions: Compilers may inline functions, eliminating
callinstructions, requiring control flow analysis to identify. - Non-standard Calls: Malware may use custom calling conventions, requiring manual tracking of registers and stack.
- Symbol Information: Debug symbols or export tables can simplify function recognition.
- Tool Support: Ghidra and IDA Pro can automatically infer calling conventions; Radare2 requires manual verification.
Data Structure Reconstruction
Data structure reconstruction aims to recover high-level data structures from binary code, such as structs and arrays. These structures typically manifest as memory access patterns, loop processing, or function parameters.
Struct Recognition
A struct is a group of contiguous fields stored in memory, accessed using a base address plus offset. Key features of struct recognition include:
- Fixed Offset Access: Such as
mov eax, [ebx + 0x4], indicating access to a struct field. - Contiguous Memory Allocation: Struct fields are stored in consecutive addresses.
- Function Parameters: Structs are often passed to functions as pointers.
Example: x86 Struct Access
The following code accesses a struct:
mov ebx, [ebp + 8] ; Struct pointer
mov eax, [ebx] ; Field 1
mov ecx, [ebx + 4] ; Field 2
add eax, ecx
mov [ebx + 8], eax ; Field 3
Analysis:
ebxholds the struct pointer.- Offsets
0,4,8indicate three fields, possiblystruct { int field1; int field2; int field3; }.
Code Example: Recognizing Struct Access
from capstone import *
CODE = b"\x8B\x5D\x08\x8B\x03\x8B\x4B\x04\x01\xC8\x89\x43\x08"
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
if instr.mnemonic == "mov" and "[" in instr.op_str:
print(f" -> Struct Access (Offset: {instr.op_str.split('+')[-1].strip(']')})")
Output:
0x1000: mov ebx, dword ptr [ebp + 0x8]
0x1003: mov eax, dword ptr [ebx]
-> Struct Access (Offset: 0)
0x1005: mov ecx, dword ptr [ebx + 0x4]
-> Struct Access (Offset: 0x4)
0x1008: add eax, ecx
0x100a: mov dword ptr [ebx + 0x8], eax
-> Struct Access (Offset: 0x8)
Inferred Struct:
struct MyStruct {
int field1; // offset 0
int field2; // offset 4
int field3; // offset 8
};
Using Ghidra to Reconstruct Structs
Ghidra can automatically recognize structs:
- Open the binary file and analyze the function.
- In the disassembly window, right-click the offset access (e.g.,
[ebx + 0x4]), select “Create Structure”. - Edit the struct fields, name them, and specify types.
Array Recognition
Arrays are contiguous storage of elements of the same type, accessed using index calculation for offsets. Features of array recognition include:
- Loop Access: Such as
mov eax, [ebx + ecx * 4], indicating access to anintarray. - Index Calculation: Using a register (e.g.,
ecx) as the index. - Fixed Element Size: Such as 4 bytes (
int), 1 byte (char).
Example: x86 Array Loop
The following code traverses an int array:
mov ebx, [ebp + 8] ; Array pointer
mov ecx, 0 ; Index
loop_start:
cmp ecx, 5
jge loop_end
mov eax, [ebx + ecx * 4]
add eax, 1
mov [ebx + ecx * 4], eax
inc ecx
jmp loop_start
loop_end:
Analysis:
ebxholds the array base address.ecxis the index,[ebx + ecx * 4]accessesintarray elements.- The loop processes 5 elements.
Code Example: Recognizing Array Access
CODE = b"\x8B\x5D\x08\xB9\x00\x00\x00\x00\x83\xF9\x05\x7D\x0A\x8B\x04\x8B\x83\xC0\x01\x89\x04\x8B\x41\xEB\xF0"
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(CODE, 0x1000):
print(f"0x{instr.address:04x}: {instr.mnemonic} {instr.op_str}")
if instr.mnemonic == "mov" and "*" in instr.op_str:
print(" -> Array Access")
Output:
0x1000: mov ebx, dword ptr [ebp + 0x8]
0x1003: mov ecx, 0x0
0x1008: cmp ecx, 0x5
0x100b: jge 0x1017
0x100d: mov eax, dword ptr [ebx + ecx * 4]
-> Array Access
0x1010: add eax, 0x1
0x1013: mov dword ptr [ebx + ecx * 4], eax
-> Array Access
0x1016: inc ecx
0x1017: jmp 0x1008
Inferred Array:
int array[5];
Data Structure Reconstruction Notes
- Alignment Issues: Structs may contain padding bytes (e.g., 4-byte alignment), need to check offsets.
- Nested Structures: Complex structs may contain pointers or sub-structs, requiring recursive analysis.
- Obfuscated Data: Malware may encrypt or disperse data structures, need to combine with dynamic analysis.
- Tool Support: Ghidra and IDA Pro provide struct editors, Radare2 requires manual inference.
String and Resource Extraction
Strings and resources (such as icons, dialogs) are important components of programs, often used for user interfaces, logs, or configurations. Extracting these resources helps understand program functionality or discover hidden information.
String Extraction
Strings are typically stored in .data, .rodata, or .rdata sections, appearing as consecutive ASCII or Unicode characters, terminated with a null byte (0x00).
String Extraction Steps
- Locate String Table: Check data sections or symbol tables (e.g., ELF’s
.strtab). - Scan Memory: Search for consecutive ASCII/Unicode characters.
- Cross-References: Track string references to confirm their usage.
Example: PE File String Extraction
The following code uses pefile to extract strings from a PE file:
import pefile
import re
def extract_strings_pe(file_path, min_len=4):
pe = pefile.PE(file_path)
strings = []
# Traverse data sections
for section in pe.sections:
if b".rdata" in section.Name or b".data" in section.Name:
data = section.get_data()
# Find ASCII strings (length >= min_len)
ascii_strings = re.findall(b"[ -~]{%d,}\x00" % min_len, data)
strings.extend(s.decode("ascii")[:-1] for s in ascii_strings)
# Find Unicode strings
unicode_strings = re.findall(b"(?:[ -~]\x00){%d,}\x00\x00" % min_len, data)
strings.extend(s.decode("utf-16le")[:-1] for s in unicode_strings)
return strings
# Extract strings
strings = extract_strings_pe("sample.exe")
print("=== Strings ===")
for s in strings:
print(s)
Output Example:
=== Strings ===
Hello, World!
Error: Invalid input
KERNEL32.dll
MessageBoxA
Example: ELF File String Extraction
from elftools.elf.elffile import ELFFile
def extract_strings_elf(file_path, min_len=4):
with open(file_path, "rb") as f:
elf = ELFFile(f)
strings = []
# Traverse .rodata and .data sections
for section in elf.iter_sections():
if section.name in (".rodata", ".data"):
data = section.data()
ascii_strings = re.findall(b"[ -~]{%d,}\x00" % min_len, data)
strings.extend(s.decode("ascii")[:-1] for s in ascii_strings)
return strings
strings = extract_strings_elf("sample.elf")
print("=== Strings ===")
for s in strings:
print(s)
Output Example:
=== Strings ===
/bin/sh
Error: Failed to open file
Success
Resource Extraction
Resources (such as icons, bitmaps, dialogs) are typically stored in the .rsrc section of PE files or the __DATA segment of Mach-O. Resources are organized in a tree structure, containing type, name, and data.
Example: PE File Resource Extraction
The following code extracts resources from a PE file:
def extract_resources_pe(file_path):
pe = pefile.PE(file_path)
resources = []
if hasattr(pe, "DIRECTORY_ENTRY_RESOURCE"):
for resource_type in pe.DIRECTORY_ENTRY_RESOURCE.entries:
type_name = resource_type.name if resource_type.name else pefile.RESOURCE_TYPE.get(resource_type.struct.Id, "Unknown")
for resource_name in resource_type.directory.entries:
for resource_lang in resource_name.directory.entries:
data = pe.get_data(resource_lang.data.struct.OffsetToData, resource_lang.data.struct.Size)
resources.append({
"Type": type_name,
"Name": resource_name.name if resource_name.name else resource_name.id,
"Size": resource_lang.data.struct.Size,
"Data": data
})
return resources
resources = extract_resources_pe("sample.exe")
print("=== Resources ===")
for res in resources:
print(f"Type: {res['Type']}, Name: {res['Name']}, Size: {res['Size']} bytes")
Output Example:
=== Resources ===
Type: RT_ICON, Name: 1, Size: 1024 bytes
Type: RT_STRING, Name: 100, Size: 256 bytes
Example: Mach-O Resource Extraction
Resources in Mach-O files are usually embedded in the __DATA segment or referenced via LC_SYMTAB. The following is code to extract string resources:
from macho_parser.macho import MachO
def extract_strings_macho(file_path, min_len=4):
macho = MachO(file_path)
strings = []
for cmd in macho.load_commands:
if cmd.cmd == "LC_SEGMENT_64" and cmd.segname == "__DATA":
for section in cmd.sections:
if section.sectname == "__const" or section.sectname == "__cstring":
data = section.data
ascii_strings = re.findall(b"[ -~]{%d,}\x00" % min_len, data)
strings.extend(s.decode("ascii")[:-1] for s in ascii_strings)
return strings
strings = extract_strings_macho("sample.macho")
print("=== Strings ===")
for s in strings:
print(s)
Output Example:
=== Strings ===
Welcome to macOS
Error: Invalid operation
String and Resource Extraction Notes
- Encoding Issues: Strings may be ASCII, UTF-16, or other encodings, need to check byte order.
- Encrypted Resources: Malware may encrypt strings or resources, need dynamic decryption.
- Cross-References: String references (e.g.,
lea eax, [string]) can reveal their usage. - Tool Support: Resource Hacker (PE), otool (Mach-O), and the strings command can assist in extraction.
Comprehensive Example: Parsing a Complete Binary
The following code combines function recognition, data structure reconstruction, and string extraction to analyze a PE file:
import pefile
from capstone import *
import re
def analyze_binary(file_path):
# Load PE file
pe = pefile.PE(file_path)
# Extract strings
print("=== Strings ===")
strings = []
for section in pe.sections:
if b".rdata" in section.Name or b".data" in section.Name:
data = section.get_data()
ascii_strings = re.findall(b"[ -~]{4,}\x00", data)
strings.extend(s.decode("ascii")[:-1] for s in ascii_strings)
for s in strings:
print(s)
# Disassemble .text section
print("\n=== Functions ===")
for section in pe.sections:
if b".text" in section.Name:
code = section.get_data()
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(code, section.VirtualAddress):
if instr.mnemonic == "push" and instr.op_str == "ebp":
print(f"Function Prologue at 0x{instr.address:08x}")
elif instr.mnemonic == "ret":
print(f"Function Epilogue at 0x{instr.address:08x}")
elif instr.mnemonic == "mov" and "[" in instr.op_str and "+" in instr.op_str:
print(f"Struct Access at 0x{instr.address:08x}: {instr.op_str}")
# Run analysis
analyze_binary("sample.exe")
Output Example:
=== Strings ===
Hello, World!
KERNEL32.dll
MessageBoxA
=== Functions ===
Function Prologue at 0x00401000
Struct Access at 0x00401005: mov eax, dword ptr [ebx + 0x4]
Function Epilogue at 0x00401010
Using Radare2 for Interactive Analysis
- Load the file:
r2 sample.bin - Analyze functions and strings:
aaa # Automatic analysis iz # List strings afl # List functions - Check data structures:
pds @ main # Display struct access in main function
Reverse Engineering Practice
- Function Analysis: Locate key functions from symbol tables or
callinstructions, infer calling conventions. - Data Structure Derivation: Track memory access, reconstruct structs and arrays.
- Resource Extraction: Extract strings and resources, analyze their usage.
- Dynamic Debugging: Use GDB or x64dbg to verify function and data structure behavior.
Advanced Static Analysis Introduction
Static analysis is the process of examining binary files (without execution) to understand program logic and behavior. Advanced static analysis requires handling obfuscated code, complex control flows, and protection mechanisms to recover the program’s true functionality. This tutorial covers three core topics:
- Deobfuscation Techniques: Handling symbol-stripped code and encrypted strings to recover original logic.
- Control Flow Flattening Analysis: Analyzing deliberately complicated control flows to reconstruct program structure.
- Code Signing and Checksum Bypass: Understanding signing mechanisms and bypassing integrity checks.
This tutorial will explain these techniques one by one, providing Python code to parse real binary files, and combining tools like Radare2, Ghidra, and IDA Pro for practical analysis.
Preparation
Before starting, ensure the following tools are installed:
- Python 3.x: For writing parsing scripts.
- Capstone: Disassembly engine (
pip install capstone). - pefile, pyelftools, macho_parser: For parsing PE, ELF, Mach-O files (
pip install pefile pyelftools macho_parser). - Radare2: Open-source reverse engineering framework (install from official website).
- Ghidra: NSA open-source reverse engineering tool.
- IDA Pro: Commercial reverse engineering tool (optional, free version available).
- Sample Binary Files: Obfuscated PE, ELF, or Mach-O files (obtainable from malware samples or CTF challenges).
- Hex Editor: Such as HxD, for manual binary modification.
Deobfuscation Techniques
Obfuscation is a common protection technique in malware and commercial software to increase reverse engineering difficulty. Common obfuscations include symbol stripping (removing function and variable names), string encryption, and code encryption. This section explains how to identify and deobfuscate these techniques.
Symbol Stripping
Symbol Stripping removes symbol information from binary files (such as function names, variable names), making disassembled code hard to understand. For example, ELF files may lack .symtab and .strtab, and PE files may have empty export tables.
Symbol Stripping Identification
Features of symbol stripping include:
- Empty symbol table (ELF’s
.symtabmissing, PE’s export table empty). - Function names replaced with random strings or addresses (e.g.,
sub_00401000). - Debugging information (such as DWARF, PDB) removed.
Symbol Stripping Recovery
Methods to recover symbol-stripped code include:
- Control Flow Analysis: Locate functions via
callinstructions and function prologues. - Cross-References: Track function calls and data references to infer functionality.
- Pattern Matching: Recognize standard library function signatures (e.g.,
memcpyloop patterns). - Dynamic Analysis: Run the program and capture symbol information (e.g., import table).
Example: Recovering Symbol-Stripped Function
The following is a symbol-stripped x86 function, disassembled as sub_00401000:
sub_00401000:
push ebp
mov ebp, esp
mov eax, [ebp + 8]
add eax, [ebp + 12]
mov esp, ebp
pop ebp
ret
Analysis:
- The function takes two parameters (
[ebp + 8]and[ebp + 12]), returns their sum (eax). - Inferred functionality: Likely an addition function, rename to
add_numbers.
Code Example: Using Capstone to Recognize Functions
from capstone import *
# Machine code of symbol-stripped function
CODE = b"\x55\x89\xE5\x8B\x45\x08\x03\x45\x0C\x89\xEC\x5D\xC3"
# Initialize Capstone (x86, 32-bit)
md = Cs(CS_ARCH_X86, CS_MODE_32)
print("=== Function Analysis ===")
for instr in md.disasm(CODE, 0x401000):
print(f"0x{instr.address:08x}: {instr.mnemonic} {instr.op_str}")
if instr.mnemonic == "push" and instr.op_str == "ebp":
print(" -> Function Prologue")
elif instr.mnemonic == "ret":
print(" -> Function Epilogue")
elif instr.mnemonic == "mov" and "[ebp + " in instr.op_str:
print(" -> Parameter Access")
Output:
=== Function Analysis ===
0x00401000: push ebp
-> Function Prologue
0x00401001: mov ebp, esp
0x00401003: mov eax, dword ptr [ebp + 0x8]
-> Parameter Access
0x00401006: add eax, dword ptr [ebp + 0xc]
-> Parameter Access
0x00401009: mov esp, ebp
0x0040100b: pop ebp
0x0040100c: ret
-> Function Epilogue
Recovery:
- Rename the function to
add_numbers. - Inferred parameters are two integers, C prototype:
int add_numbers(int a, int b).
Using Radare2 to Recover Symbols
r2 sample.bin
> aaa # Automatic analysis
> afl # List functions
> fr sub_00401000 add_numbers # Rename function
> pdf @ add_numbers # View disassembly
String Decryption
String encryption is a common obfuscation technique where programs dynamically decrypt strings at runtime to hide key information (such as API names, URLs). Decryption logic typically appears as loops, bitwise operations (like XOR), or custom algorithms.
String Decryption Identification
Features include:
- Encrypted strings stored in data sections, appearing as unreadable byte sequences.
- Decryption functions using loops, bitwise operations (like XOR), or lookup tables.
- Decrypted strings passed to API calls via
leaormov.
String Decryption Recovery
Recovery methods include:
- Locate Decryption Function: Track functions referencing strings, analyze their logic.
- Simulate Decryption: Extract the decryption algorithm, write a script to reproduce the process.
- Dynamic Debugging: Capture strings after decryption.
Example: XOR String Decryption
The following code decrypts an XOR-encrypted string:
mov esi, encrypted_data
mov edi, decrypted_buffer
mov ecx, 0x10 ; Length
mov edx, 0x5A ; Key
decrypt_loop:
lodsb ; Load byte into al
xor al, dl ; Decrypt
stosb ; Store to edi
loop decrypt_loop
Analysis:
encrypted_datastores the encrypted string.- Each byte is XORed with
0x5Afor decryption. - Result stored in
decrypted_buffer.
Code Example: Simulating XOR Decryption
def decrypt_xor_string(data, key):
return bytes(b ^ key for b in data)
# Encrypted data (example)
encrypted = b"\x31\x28\x3F\x3F\x34\x2C\x7B\x26\x20\x35\x3A\x3F\x32\x2A\x3B\x30"
key = 0x5A
# Decrypt
decrypted = decrypt_xor_string(encrypted, key)
print("=== Decrypted String ===")
print(decrypted.decode("ascii"))
Output:
=== Decrypted String ===
Hello, World!
Using Ghidra to Analyze Decryption Functions
- Open the binary, locate string references.
- Track to the decryption function, analyze loops and operations.
- In Ghidra’s script editor, write a Python script to simulate decryption:
# Ghidra Python script
data = getBytes(toAddr(0x1000), 16) # Encrypted data
key = 0x5A
decrypted = [b ^ key for b in data]
print("".join(chr(b) for b in decrypted))
Deobfuscation Notes
- Multi-Layer Obfuscation: Code may be encrypted multiple times, need to peel layers one by one.
- Dynamic Generation: Strings or functions may be generated at runtime, need to combine with dynamic analysis.
- Anti-Debugging: Obfuscated code may contain anti-debugging logic, need to disable or bypass.
- Tool Support: IDA Pro’s FLAIR (Fast Library Identification and Recognition) can recognize standard library functions.
Control Flow Flattening Analysis
Control Flow Flattening is an advanced obfuscation technique that breaks a function’s control flow into multiple basic blocks, using a dispatcher to dynamically decide the execution path. This technique makes the control flow graph (CFG) complex and difficult to analyze manually.
Control Flow Flattening Principle
The core of control flow flattening is to split function logic into multiple basic blocks, each jumping through a dispatcher. The dispatcher uses a state variable (like a switch table or state register) to decide the next block. Typical structure:
entry:
mov state, 0
jmp dispatcher
dispatcher:
cmp state, 0
je block1
cmp state, 1
je block2
...
block1:
; Logic
mov state, 1
jmp dispatcher
block2:
; Logic
mov state, 2
jmp dispatcher
Control Flow Flattening Identification
Features include:
- Numerous unconditional jumps (
jmp) to the same dispatcher. - Frequent state variable updates (e.g.,
mov eax, <const>). - Conditional jumps or
switchtables controlling block selection. - CFG appears “flat”, with highly dispersed basic blocks.
Control Flow Flattening Analysis
Analysis steps:
- Locate Dispatcher: Identify frequently jumped-to target addresses.
- Track State Variable: Analyze state variable initialization and updates.
- Reconstruct CFG: Reconnect basic blocks to recover original control flow.
- Deflattening: Merge basic blocks to generate readable code.
Example: x86 Control Flow Flattening
The following is a flattened function:
mov eax, 0 ; State variable
jmp dispatcher
dispatcher:
cmp eax, 0
je block1
cmp eax, 1
je block2
jmp exit
block1:
mov ebx, [ebp + 8]
add ebx, 1
mov eax, 1
jmp dispatcher
block2:
mov [ebp + 8], ebx
mov eax, 2
jmp dispatcher
exit:
ret
Analysis:
eaxis the state variable controlling block selection.- Dispatcher jumps to
block1orblock2viacmpandje. - Original logic: Increment input parameter by 1 and store it.
Code Example: Analyzing Flattened Code
from capstone import *
CODE = b"\xB8\x00\x00\x00\x00\xEB\x0E\x83\xF8\x00\x74\x05\x83\xF8\x01\x74\x0A\xEB\x10\x8B\x5D\x08\x83\xC3\x01\xB8\x01\x00\x00\x00\xEB\xEC\x89\x5D\x08\xB8\x02\x00\x00\x00\xEB\xE2\xC3"
md = Cs(CS_ARCH_X86, CS_MODE_32)
print("=== Flattened Code ===")
for instr in md.disasm(CODE, 0x401000):
print(f"0x{instr.address:08x}: {instr.mnemonic} {instr.op_str}")
if instr.mnemonic == "cmp" and "eax" in instr.op_str:
print(" -> Dispatcher Condition")
elif instr.mnemonic == "jmp" and instr.address < int(instr.op_str, 16):
print(" -> Back to Dispatcher")
Output:
=== Flattened Code ===
0x00401000: mov eax, 0x0
0x00401005: jmp 0x401015
0x00401007: cmp eax, 0x0
-> Dispatcher Condition
0x0040100a: je 0x401011
0x0040100c: cmp eax, 0x1
-> Dispatcher Condition
0x0040100f: je 0x40101b
0x00401011: jmp 0x401023
0x00401013: mov ebx, dword ptr [ebp + 0x8]
0x00401016: add ebx, 0x1
0x00401019: mov eax, 0x1
0x0040101e: jmp 0x40100c
-> Back to Dispatcher
0x00401020: mov dword ptr [ebp + 0x8], ebx
0x00401023: mov eax, 0x2
0x00401028: jmp 0x40100c
-> Back to Dispatcher
0x0040102a: ret
Recovery:
- Merge
block1andblock2, reconstruct as:
mov ebx, [ebp + 8]
add ebx, 1
mov [ebp + 8], ebx
ret
Using Ghidra for Deflattening
Ghidra’s Decompiler can attempt deflattening:
- Open the binary, locate the flattened function.
- Use the “Decompile” view, check the generated pseudocode.
- If failed, manually track the state variable and merge basic blocks.
Control Flow Flattening Notes
- Multi-Layer Flattening: Complex programs may nest multiple dispatchers, requiring recursive analysis.
- Indirect Jumps: Dispatchers may use jump tables, need to parse table contents.
- Dynamic State: State variables may be computed at runtime, need symbolic execution.
- Tool Support: IDA Pro’s “Deobfuscator” plugin or Ghidra scripts can automate deflattening.
Code Signing and Checksum Bypass
Code signing and integrity checks are mechanisms to protect binary files from tampering, common in PE (Windows), ELF (Linux), and Mach-O (macOS/iOS) files. Bypassing these mechanisms is an advanced task in reverse engineering, usually for research or testing.
Code Signing Mechanism
Code signing verifies the file’s origin and integrity via digital certificates:
- PE Files: Signature stored in
.rsrcsection orIMAGE_DIRECTORY_ENTRY_SECURITY. - Mach-O Files: Signature specified by
LC_CODE_SIGNATUREload command. - ELF Files: Signatures usually managed by external tools (like
signelf) or via hash checks.
Checksum mechanisms include:
- Hash Check: Compute file or section hash (e.g., MD5, SHA256), compare with expected value.
- Digital Signature: Verify signature using public key.
- Runtime Check: Program dynamically checks its own integrity.
Code Signing Analysis
Analysis steps:
- Locate Signature: Check PE’s
DataDirectory, Mach-O’s load commands, or ELF’s custom sections. - Extract Hash: Identify checksum algorithm and expected hash value.
- Track Check Logic: Locate functions performing the check.
Example: PE File Signature Check
The following code checks a PE file’s hash:
call compute_hash
cmp eax, 0x12345678 ; Expected hash
jne error
jmp continue
error:
; Error handling
continue:
; Normal execution
Code Example: Extracting PE Signature
import pefile
def analyze_pe_signature(file_path):
pe = pefile.PE(file_path)
if pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress != 0:
print("=== Code Signature Found ===")
offset = pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress
size = pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].Size
print(f"Signature Offset: 0x{offset:08x}")
print(f"Signature Size: {size} bytes")
else:
print("No Code Signature")
analyze_pe_signature("sample.exe")
Output Example:
=== Code Signature Found ===
Signature Offset: 0x00012000
Signature Size: 8192 bytes
Checksum Bypass
Bypass methods include:
- Modify Check Logic: Replace conditional jumps (e.g.,
jne) with unconditional jumps (jmp). - Patch Hash: Modify expected hash value to match tampered file.
- Disable Check: Replace check function with
nopor direct return. - Simulate Signature: Forge signature data to fool verification.
Example: Bypassing x86 Check
Assume check logic:
call compute_hash
cmp eax, 0x12345678
jne error
Bypass Method:
- Replace
jne errorwithjmp continue.
Code Example: Patching Binary
def patch_binary(file_path, offset, new_bytes):
with open(file_path, "r+b") as f:
f.seek(offset)
f.write(new_bytes)
# Replace jne (0x75) with jmp (0xEB)
patch_binary("sample.exe", 0x401008, b"\xEB")
Verification:
- Use Radare2 to check patch result:
r2 sample.bin
> s 0x401008
> pd 1
0x00401008 jmp 0x40100a
Mach-O Signature Bypass
macOS/iOS Mach-O files enforce signing, bypass methods include:
- Remove LC_CODE_SIGNATURE: Modify load command table to disable signature check.
- Patch dyld: Modify dynamic linker to skip signature verification.
- Jailbroken Environment: Run on jailbroken device to bypass system restrictions.
Example: Removing Mach-O Signature
from macho_parser.macho import MachO
def remove_macho_signature(file_path):
macho = MachO(file_path)
for cmd in macho.load_commands:
if cmd.cmd == "LC_CODE_SIGNATURE":
print("Removing LC_CODE_SIGNATURE")
cmd.cmd = 0 # Nullify command
macho.write_file(file_path + ".patched")
remove_macho_signature("sample.macho")
Code Signing and Checksum Bypass Notes
- Legal Risks: Bypassing signatures may violate laws, perform only in legal contexts (e.g., CTF or authorized testing).
- Runtime Checks: Dynamic checks may require debugger intervention.
- Backup Files: Backup binaries before patching to avoid damage.
- Tool Support: Codesign (macOS), Signtool (Windows) can verify signatures.
Comprehensive Example: Analyzing Obfuscated Binary
The following code combines deobfuscation, control flow flattening, and checksum bypass to analyze a PE file:
import pefile
from capstone import *
import re
def analyze_obfuscated_binary(file_path):
# Load PE file
pe = pefile.PE(file_path)
# Extract encrypted strings
print("=== Encrypted Strings ===")
for section in pe.sections:
if b".data" in section.Name:
data = section.get_data()
# Assume XOR encryption
encrypted = data[:16]
key = 0x5A
decrypted = bytes(b ^ key for b in encrypted)
print(f"Decrypted: {decrypted.decode('ascii', errors='ignore')}")
# Analyze .text section (search for flattening and checks)
print("\n=== Code Analysis ===")
for section in pe.sections:
if b".text" in section.Name:
code = section.get_data()
md = Cs(CS_ARCH_X86, CS_MODE_32)
for instr in md.disasm(code, section.VirtualAddress):
if instr.mnemonic == "cmp" and "eax" in instr.op_str:
print(f"0x{instr.address:08x}: {instr.mnemonic} {instr.op_str} (Dispatcher or Checksum)")
elif instr.mnemonic == "jne":
print(f"0x{instr.address:08x}: {instr.mnemonic} {instr.op_str} (Possible Checksum Branch)")
# Check signature
print("\n=== Signature ===")
if pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']].VirtualAddress != 0:
print("Signature Found")
analyze_obfuscated_binary("sample.exe")
Output Example:
=== Encrypted Strings ===
Decrypted: Hello, World!
=== Code Analysis ===
0x00401007: cmp eax, 0x0 (Dispatcher or Checksum)
0x0040100c: jne 0x401020 (Possible Checksum Branch)
=== Signature ===
Signature Found
Using Radare2 for Interactive Analysis
- Load the file:
r2 sample.bin - Analyze obfuscation and checks:
aaa # Automatic analysis iz # List strings agf # View control flow graph s 0x401007; pd 10 # Check checksum logic - Patch check:
s 0x40100c w \xEB # Replace jne with jmp
Reverse Engineering Practice
- Deobfuscation: Extract encrypted strings, simulate decryption logic.
- Control Flow Analysis: Identify dispatcher, reconstruct flattened code.
- Checksum Bypass: Patch binary, disable signature or hash checks.
- Dynamic Verification: Use GDB or x64dbg to verify modification effects.



