|
1 |
| -# Integer Overflow |
| 1 | +# Integer Overflow (Web Applications) |
2 | 2 |
|
3 | 3 | {{#include ../../banners/hacktricks-training.md}}
|
4 | 4 |
|
5 |
| -Check: |
| 5 | +> This page focuses on how **integer overflows/truncations can be abused in web applications and browsers**. For exploitation primitives inside native binaries you can continue reading the dedicated page: |
| 6 | +> |
| 7 | +> {{#ref}} |
| 8 | +> ../../binary-exploitation/integer-overflow.md |
| 9 | +> {{#endref}} |
6 | 10 |
|
7 |
| -{{#ref}} |
8 |
| -../../binary-exploitation/integer-overflow.md |
9 |
| -{{#endref}} |
| 11 | +--- |
10 | 12 |
|
11 |
| -{{#include ../../banners/hacktricks-training.md}} |
| 13 | +## 1. Why integer math still matters on the web |
| 14 | + |
| 15 | +Even though most business-logic in modern stacks is written in *memory-safe* languages, the underlying runtime (or third-party libraries) is eventually implemented in C/C++. Whenever user-controlled numbers are used to allocate buffers, compute offsets, or perform length checks, **a 32-bit or 64-bit wrap-around may transform an apparently harmless parameter into an out-of-bounds read/write, a logic bypass or a DoS**. |
| 16 | + |
| 17 | +Typical attack surface: |
| 18 | + |
| 19 | +1. **Numeric request parameters** – classic `id`, `offset`, or `count` fields. |
| 20 | +2. **Length / size headers** – `Content-Length`, WebSocket frame length, HTTP/2 `continuation_len`, etc. |
| 21 | +3. **File-format metadata parsed server-side or client-side** – image dimensions, chunk sizes, font tables. |
| 22 | +4. **Language-level conversions** – signed↔unsigned casts in PHP/Go/Rust FFI, JS `Number` → `int32` truncations inside V8. |
| 23 | +5. **Authentication & business logic** – coupon value, price, or balance calculations that silently overflow. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## 2. Recent real-world vulnerabilities (2023-2025) |
| 28 | + |
| 29 | +| Year | Component | Root cause | Impact | |
| 30 | +|------|-----------|-----------|--------| |
| 31 | +| 2023 | **libwebp – CVE-2023-4863** | 32-bit multiplication overflow when computing decoded pixel size | Triggered a Chrome 0-day (`BLASTPASS` on iOS), allowed *remote code execution* inside the renderer sandbox. | |
| 32 | +| 2024 | **V8 – CVE-2024-0519** | Truncation to 32-bit when growing a `JSArray` leads to OOB write on the backing store | Remote code execution after a single visit. | |
| 33 | +| 2025 | **Apollo GraphQL Server** (unreleased patch) | 32-bit signed integer used for `first/last` pagination args; negative values wrap to huge positives | Logic bypass & memory exhaustion (DoS). | |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## 3. Testing strategy |
| 38 | + |
| 39 | +### 3.1 Boundary-value cheat-sheet |
| 40 | + |
| 41 | +Send **extreme signed/unsigned values** wherever an integer is expected: |
| 42 | + |
| 43 | +``` |
| 44 | +-1, 0, 1, |
| 45 | +127, 128, 255, 256, |
| 46 | +32767, 32768, 65535, 65536, |
| 47 | +2147483647, 2147483648, 4294967295, |
| 48 | +9223372036854775807, 9223372036854775808, |
| 49 | +0x7fffffff, 0x80000000, 0xffffffff |
| 50 | +``` |
| 51 | + |
| 52 | +Other useful formats: |
| 53 | +* Hex (`0x100`), octal (`0377`), scientific (`1e10`), JSON big-int (`9999999999999999999`). |
| 54 | +* Very long digit strings (>1kB) to hit custom parsers. |
| 55 | + |
| 56 | +### 3.2 Burp Intruder template |
12 | 57 |
|
| 58 | +``` |
| 59 | +§INTEGER§ |
| 60 | +Payload type: Numbers |
| 61 | +From: -10 To: 4294967300 Step: 1 |
| 62 | +Pad to length: 10, Enable hex prefix 0x |
| 63 | +``` |
13 | 64 |
|
| 65 | +### 3.3 Fuzzing libraries & runtimes |
14 | 66 |
|
| 67 | +* **AFL++/Honggfuzz** with `libFuzzer` harness around the parser (e.g., WebP, PNG, protobuf). |
| 68 | +* **Fuzzilli** – grammar-aware fuzzing of JavaScript engines to hit V8/JSC integer truncations. |
| 69 | +* **boofuzz** – network-protocol fuzzing (WebSocket, HTTP/2) focusing on length fields. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## 4. Exploitation patterns |
| 74 | + |
| 75 | +### 4.1 Logic bypass in server-side code (PHP example) |
| 76 | +```php |
| 77 | +$price = (int)$_POST['price']; // expecting cents (0-10000) |
| 78 | +$total = $price * 100; // ← 32-bit overflow possible |
| 79 | +if($total > 1000000){ |
| 80 | + die('Too expensive'); |
| 81 | +} |
| 82 | +/* Sending price=21474850 → $total wraps to ‑2147483648 and check is bypassed */ |
| 83 | +``` |
| 84 | + |
| 85 | +### 4.2 Heap overflow via image decoder (libwebp 0-day) |
| 86 | +The WebP lossless decoder multiplied image width × height × 4 (RGBA) inside a 32-bit `int`. A crafted file with dimensions `16384 × 16384` overflows the multiplication, allocates a short buffer and subsequently writes **~1GB** of decompressed data past the heap – leading to RCE in every Chromium-based browser before 116.0.5845.187. |
| 87 | + |
| 88 | +### 4.3 Browser-based XSS/RCE chain |
| 89 | +1. **Integer overflow** in V8 gives arbitrary read/write. |
| 90 | +2. Escape the sandbox with a second bug or call native APIs to drop a payload. |
| 91 | +3. The payload then injects a malicious script into the origin context → stored XSS. |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## 5. Defensive guidelines |
| 96 | + |
| 97 | +1. **Use wide types or checked math** – e.g., `size_t`, Rust `checked_add`, Go `math/bits.Add64`. |
| 98 | +2. **Validate ranges early**: reject any value outside business ___domain before arithmetic. |
| 99 | +3. **Enable compiler sanitizers**: `-fsanitize=integer`, UBSan, Go race detector. |
| 100 | +4. **Adopt fuzzing in CI/CD** – combine coverage feedback with boundary corpora. |
| 101 | +5. **Stay patched** – browser integer overflow bugs are frequently weaponised within weeks. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +## References |
| 108 | + |
| 109 | +* [NVD CVE-2023-4863 – libwebp Heap Buffer Overflow](https://nvd.nist.gov/vuln/detail/CVE-2023-4863) |
| 110 | +* [Google Project Zero – "Understanding V8 CVE-2024-0519"](https://googleprojectzero.github.io/) |
| 111 | +{{#include ../../banners/hacktricks-training.md}} |
0 commit comments