Magic bytes - the first 4 bytes define the format
Before delving into the structure of an ELF file header or the structure of a PE file, it's worth understanding what kind of binary you're dealing with. fileIn Linux, it does this in milliseconds—reading magic bytes and producing a summary: Read more in our article on binary vulnerability analysis .
$ file ./challenge
challenge: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, not stripped
$ file ./task.exe
task.exe: PE32+ executable (console) x86-64, for MS Windows
One line and it's clear: architecture, bit depth, link type, presence of a symbol table. Behind this are specific bytes. ELF opens with the sequence 7F 45 4C 46(byte \x7F + ASCII ELF). PE starts with 4D 5A (ASCII MZ— the initials of Mark Zbikowski, one of the developers of MS-DOS). Through xxd ./binary | head -4They are visible with bare hands.
Why check manually if fileDoes it work? At CTFs, the authors regularly submit a binary file without an extension, with a substituted or truncated header. In malware analysis, attackers use file type substitution— Masquerade File Type (T1036.008, Defense Evasion according to MITRE ATT&CK) : extension .jpg or .pdf, and inside is executable code. The extension is file system metadata, while magic bytes are part of the file itself. The former can be renamed with a single command, while the latter—if changed, the OS bootloader will refuse to work with the binary.
ELF file header structure
The ELF header for a 64-bit binary takes up exactly 64 bytes, while for a 32-bit binary it takes up 52 bytes. Each field has a fixed offset, so it can be parsed in a hex editor without the need for additional utilities.
ELF Header: readelf -h and what to read in it
The most obvious examples of using readelf are to start with a key -hHere's a typical result on a CTF binary:
$ readelf -h ./challenge
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Entry point address: 0x400720
Start of program headers: 64 (bytes into file)
Start of section headers: 6440 (bytes into file)
Number of section headers: 29
What to look for in 30 seconds during static analysis of binary files:
Magic (16 bytes, offset 0x00). First 4 bytes 7F 45 4C 46confirm the format. The fifth byte is the class: 01= ELF32, 02= ELF64. Sixth-order byte: 01 = little-endian, 02= big-endian. For x86/x86-64 always 01You see. 02— MIPS big-endian or other exotic ones, and the disassembler settings change immediately.
Type (e_type, offset 0x10). What's in front of you: - ET_REL(1) - object file ( .o), not linked - ET_EXEC(2) - executable file with a fixed base address - ET_DYN(3) - position-independent code. The nuance that people stumble on is that modern compilers generate by default ET_DYNeven for regular executable files (PIE). This is not a library, it's a PIE binary. - ET_CORE (4) — core dump
Machine (e_machine, offset 0x12). Binary file architecture definition: EM_386(3) = i386, EM_X86_64 (62) = x86-64, EM_ARM (40) = ARM, EM_AARCH64(183) = AArch64. In CTF, this field determines which processor module to select in the disassembler.
Entry point (e_entry, offset 0x18 for ELF64). The virtual address of the entry point. The normal entry point is located inside .textIf he points in .data, .bssor a section with a non-standard name - a red flag: packer, obfuscator, or self-modifying code.
Sections and segments of a binary file
One of the most common mistakes when reading ELF executable file headers is confusing sections with segments. These are two parallel representations of a single file.
Segments (Program Headers) describe how the kernel loads a file into memory. readelf -lDisplays a complete list of them. Segment types critical for triage:
Segment type Purpose What to look at
PT_LOAD Downloadable segment R/W/X flags: code (R+X), data (R+W)
PT_INTERP Path to the dynamic linker No PT_INTERP= static linking
PT_DYNAMIC Information for dynamic linking Dependencies, PLT/GOT
PT_GNU_STACK Stack flags Flag X= executable stack (NX off)
PT_GNU_RELRO Read-Only relocations GOT is protected from overwriting
In CTF/pwn tasks PT_GNU_STACKwith a flag X— a direct hint at a shellcode solution. PT_GNU_RELROwithout Full RELRO means that GOT-overwrite is still in play.
Section Headers are a logical structure for the linker and debugger. readelf -Sdisplays a table. Main features:
Section Content Flags
.text Machine code AX (allocatable + executable)
.data Initialized variables WA (writable + allocatable)
.rodata Constants, strings (read-only) A
.bss Uninitialized data OF
.plt / .got Dynamic linking tables
.symtab / .strtab Table of symbols and strings
The principle is simple: sections are used for analysis, segments are used for loading. One segment combines several sections. Sections can be deleted using the command strip— the binary will run (the kernel uses segments), but the function names will disappear in the disassembler. This is the Stripped Payloads technique (T1027.008, Defense Evasion according to MITRE ATT&CK) , and it's common in CTFs.
Defense mechanisms are visible from the headings
The headers can be used to determine compiler protections before execution. checksec:
NX: PT_GNU_STACKwithout a flag X— the stack is not executable
RELRO: availability PT_GNU_RELRO— GOT is partially or fully protected
Stack Canaries: Symbol __stack_chk_failin the symbol table (visible through readelf -s | grep stack_chk)
PIE: type ET_DYNinstead of ET_EXEC— addresses are randomized at each launch
FORTIFY_SOURCE: symbol __memcpy_chk, __strcpy_chkin imports
For pwn tasks, the combination NX off + no canaries = ret2shellcode. PIE off = fixed addresses for ROP chains. This is actually why we read the headers before loading them into the disassembler.
Before delving into the structure of an ELF file header or the structure of a PE file, it's worth understanding what kind of binary you're dealing with. fileIn Linux, it does this in milliseconds—reading magic bytes and producing a summary: Read more in our article on binary vulnerability analysis .
$ file ./challenge
challenge: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, not stripped
$ file ./task.exe
task.exe: PE32+ executable (console) x86-64, for MS Windows
One line and it's clear: architecture, bit depth, link type, presence of a symbol table. Behind this are specific bytes. ELF opens with the sequence 7F 45 4C 46(byte \x7F + ASCII ELF). PE starts with 4D 5A (ASCII MZ— the initials of Mark Zbikowski, one of the developers of MS-DOS). Through xxd ./binary | head -4They are visible with bare hands.
Why check manually if fileDoes it work? At CTFs, the authors regularly submit a binary file without an extension, with a substituted or truncated header. In malware analysis, attackers use file type substitution— Masquerade File Type (T1036.008, Defense Evasion according to MITRE ATT&CK) : extension .jpg or .pdf, and inside is executable code. The extension is file system metadata, while magic bytes are part of the file itself. The former can be renamed with a single command, while the latter—if changed, the OS bootloader will refuse to work with the binary.
ELF file header structure
The ELF header for a 64-bit binary takes up exactly 64 bytes, while for a 32-bit binary it takes up 52 bytes. Each field has a fixed offset, so it can be parsed in a hex editor without the need for additional utilities.
ELF Header: readelf -h and what to read in it
The most obvious examples of using readelf are to start with a key -hHere's a typical result on a CTF binary:
$ readelf -h ./challenge
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Entry point address: 0x400720
Start of program headers: 64 (bytes into file)
Start of section headers: 6440 (bytes into file)
Number of section headers: 29
What to look for in 30 seconds during static analysis of binary files:
Magic (16 bytes, offset 0x00). First 4 bytes 7F 45 4C 46confirm the format. The fifth byte is the class: 01= ELF32, 02= ELF64. Sixth-order byte: 01 = little-endian, 02= big-endian. For x86/x86-64 always 01You see. 02— MIPS big-endian or other exotic ones, and the disassembler settings change immediately.
Type (e_type, offset 0x10). What's in front of you: - ET_REL(1) - object file ( .o), not linked - ET_EXEC(2) - executable file with a fixed base address - ET_DYN(3) - position-independent code. The nuance that people stumble on is that modern compilers generate by default ET_DYNeven for regular executable files (PIE). This is not a library, it's a PIE binary. - ET_CORE (4) — core dump
Machine (e_machine, offset 0x12). Binary file architecture definition: EM_386(3) = i386, EM_X86_64 (62) = x86-64, EM_ARM (40) = ARM, EM_AARCH64(183) = AArch64. In CTF, this field determines which processor module to select in the disassembler.
Entry point (e_entry, offset 0x18 for ELF64). The virtual address of the entry point. The normal entry point is located inside .textIf he points in .data, .bssor a section with a non-standard name - a red flag: packer, obfuscator, or self-modifying code.
Sections and segments of a binary file
One of the most common mistakes when reading ELF executable file headers is confusing sections with segments. These are two parallel representations of a single file.
Segments (Program Headers) describe how the kernel loads a file into memory. readelf -lDisplays a complete list of them. Segment types critical for triage:
Segment type Purpose What to look at
PT_LOAD Downloadable segment R/W/X flags: code (R+X), data (R+W)
PT_INTERP Path to the dynamic linker No PT_INTERP= static linking
PT_DYNAMIC Information for dynamic linking Dependencies, PLT/GOT
PT_GNU_STACK Stack flags Flag X= executable stack (NX off)
PT_GNU_RELRO Read-Only relocations GOT is protected from overwriting
In CTF/pwn tasks PT_GNU_STACKwith a flag X— a direct hint at a shellcode solution. PT_GNU_RELROwithout Full RELRO means that GOT-overwrite is still in play.
Section Headers are a logical structure for the linker and debugger. readelf -Sdisplays a table. Main features:
Section Content Flags
.text Machine code AX (allocatable + executable)
.data Initialized variables WA (writable + allocatable)
.rodata Constants, strings (read-only) A
.bss Uninitialized data OF
.plt / .got Dynamic linking tables
.symtab / .strtab Table of symbols and strings
The principle is simple: sections are used for analysis, segments are used for loading. One segment combines several sections. Sections can be deleted using the command strip— the binary will run (the kernel uses segments), but the function names will disappear in the disassembler. This is the Stripped Payloads technique (T1027.008, Defense Evasion according to MITRE ATT&CK) , and it's common in CTFs.
Defense mechanisms are visible from the headings
The headers can be used to determine compiler protections before execution. checksec:
NX: PT_GNU_STACKwithout a flag X— the stack is not executable
RELRO: availability PT_GNU_RELRO— GOT is partially or fully protected
Stack Canaries: Symbol __stack_chk_failin the symbol table (visible through readelf -s | grep stack_chk)
PIE: type ET_DYNinstead of ET_EXEC— addresses are randomized at each launch
FORTIFY_SOURCE: symbol __memcpy_chk, __strcpy_chkin imports
For pwn tasks, the combination NX off + no canaries = ret2shellcode. PIE off = fixed addresses for ROP chains. This is actually why we read the headers before loading them into the disassembler.