# Integer Overflow

<details>

<summary><strong>从零开始学习AWS黑客技术，成为</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[NFT收藏品](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

## 基本信息

**整数溢出**的核心是计算机编程中数据类型的**大小**限制和数据的**解释**。

例如，一个**8位无符号整数**可以表示**0到255**的值。如果尝试将值256存储在8位无符号整数中，由于其存储容量的限制，它会回绕到0。同样，对于**16位无符号整数**，它可以保存**0到65,535**的值，将65,535加1会将值回绕到0。

此外，一个**8位有符号整数**可以表示\*\*-128到127**的值。这是因为一个比特用于表示符号（正或负），剩下的7位用于表示大小。最负数表示为**-128\*\*（二进制`10000000`），最正数为**127**（二进制`01111111`）。

### 最大值

对于潜在的**网络漏洞**，了解最大支持的值非常有趣：

```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);
}
```

整数溢出是一种常见的安全漏洞，发生在对整数进行算术运算时，结果超出了该整数类型所能表示的范围。这可能导致未预期的行为，如数据损坏、程序崩溃或甚至远程代码执行。要防止整数溢出，应该在进行算术运算前检查操作数的范围，并采取适当的措施，如使用更大的数据类型或添加边界检查。 \`\`\`c #include #include

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; }

````
## 例子

### 纯溢出

打印结果将为0，因为我们溢出了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;
}
````

#### 有符号转无符号转换

考虑这样一种情况：从用户输入中读取一个有符号整数，然后在一个将其视为无符号整数的上下文中使用，但没有进行适当的验证：

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

在这个例子中，如果用户输入一个负数，由于二进制值的解释方式，它将被解释为一个大的无符号整数，可能导致意外行为。

#### 其他示例

* <https://guyinatuxedo.github.io/35-integer_exploitation/int_overflow_post/index.html>
* 只使用1B来存储密码的大小，因此可能会发生溢出，并使其认为长度为4，而实际上长度为260，以绕过长度检查保护
* <https://guyinatuxedo.github.io/35-integer_exploitation/puzzle/index.html>
* 给定一对数字，使用z3找出一个新数字，使其乘以第一个数字得到第二个数字：

```
(((argv[1] * 0x1064deadbeef4601) & 0xffffffffffffffff) == 0xD1038D2E07B42569)
```

* <https://8ksec.io/arm64-reversing-and-exploitation-part-8-exploiting-an-integer-overflow-vulnerability/>
* 只使用1B来存储密码的大小，因此可能会发生溢出，并使其认为长度为4，而实际上长度为260，以绕过长度检查保护并覆盖堆栈中的下一个局部变量，并绕过两个保护

### ARM64

这在**ARM64中没有改变**，如您可以在[**这篇博客文章**](https://8ksec.io/arm64-reversing-and-exploitation-part-8-exploiting-an-integer-overflow-vulnerability/)中看到的。

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**上关注**我们。
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
