hacktricks/pentesting-web/integer-overflow.md
carlospolop 63bd9641c0 f
2023-06-05 20:33:24 +02:00

4.3 KiB

Desbordamiento de enteros

☁️ HackTricks Cloud ☁️ -🐦 Twitter 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥

{% tabs %} {% tab title="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 title="Python" %}

Python

import struct

def pack_signed_int(i):
    return struct.pack("<i", i)

def pack_unsigned_int(i):
    return struct.pack("<I", i)

def unpack_signed_int(b):
    return struct.unpack("<i", b)[0]

def unpack_unsigned_int(b):
    return struct.unpack("<I", b)[0]

Example

>>> pack_signed_int(-1)
b'\xff\xff\xff\xff'
>>> unpack_signed_int(b'\xff\xff\xff\xff')
-1
>>> pack_unsigned_int(0xdeadbeef)
b'\xef\xbe\xad\xde'
>>> unpack_unsigned_int(b'\xef\xbe\xad\xde')
3735928559

References

```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 %}

☁️ HackTricks Cloud ☁️ -🐦 Twitter 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥