扫码领资料
获网安教程
来Track安全社区投稿~
千元稿费!还有保底奖励~(https://bbs.zkaq.cn)
什么是整数溢出?
整数溢出是指当算术运算的结果超出用于存储该结果的数据类型的容量时发生的现象。简单来说,就是当一个数值变得过大(对于有符号整数来说,也可能是过小),无法被正常存储,从而导致意外结果。
举个例子,假设有一个有符号的32位整数,其存储范围为 -2,147,483,648 到 2,147,483,647。如果你试图给最大值(2,147,483,647)加上 1
,数值会回绕到最小值(-2,147,483,648),这在关键系统中可能引发严重后果。
为什么会发生整数溢出?
存储限制:定长整数(如 int
、short
等)有固定的边界。例如:
#include <stdio.h>
int main() {
int a = 2147483647; // Maximum value for a 32-bit signed integer
int b = a + 1; // This causes an integer overflow
printf("Overflowed Value: %dn", b);
return 0;
}
Overflowed Value: -2147483648
1
加到最大整数会导致它回绕到最小的负值,这就是典型的溢出现象。整数溢出漏洞的历史回顾
int
和 short
之类的数据类型来节省内存。因此,特别是在涉及安全的关键软件中,溢出问题时有发生。与整数溢出相关的CVE数量(截至2024年9月)
为什么有这么多与整数溢出相关的CVE?
什么使应用程序容易遭受整数溢出攻击?
使应用程序易受攻击的因素:
int
、short
等)有上下限,如果应用程序没有正确处理大或小的输入值,就可能发生溢出。void copy_data(int size) {
char *buf = malloc(size + 10); // Potential overflow if size is close to INT_MAX
...
}
malloc()
、calloc()
)并基于整数运算计算内存大小的应用程序容易受到溢出攻击。计算错误的内存大小可能会分配比预期更少的内存,导致缓冲区溢出或内存损坏。易受攻击的应用程序示例:
导致漏洞的条件:
整数溢出漏洞的影响
实例分析:
实践演示
#include <iostream>
#include <cstring>
#include <limits>
#include <cstdlib>
using namespace std;
// Vulnerability: Integer overflow in a memory allocation calculation
void memory_allocation_overflow(int input) {
int size = input * sizeof(int); // Multiplication overflow
cout << "Allocating memory for: " << size << " bytesn";
int *buffer = (int *)malloc(size);
if (buffer == nullptr) {
cout << "Memory allocation failedn";
return;
}
// Simulate a memory write operation
for (int i = 0; i < input; i++) {
buffer[i] = i; // Buffer overflow if size is incorrectly allocated
}
cout << "Buffer populatedn";
free(buffer);
}
// Vulnerability: Integer overflow in security check leading to security bypass
void privilege_check(int user_id) {
int max_allowed_id = 1000; // Max allowed ID is 1000
if (user_id > max_allowed_id) {
cout << "Access denied for user ID: " << user_id << endl;
} else {
cout << "Access granted for user ID: " << user_id << endl;
}
}
// Vulnerability: Denial of service due to large integer value
void large_allocation(int size) {
// Large allocation with integer overflow
int *large_array = (int *)malloc(size * sizeof(int));
if (large_array == nullptr) {
cout << "Failed to allocate large arrayn";
return;
}
for (int i = 0; i < size; i++) {
large_array[i] = i; // Filling the array
}
cout << "Large array populatedn";
free(large_array);
}
// Vulnerability: Arithmetic overflow in logic operation
void logic_overflow(int count) {
int result = count * 1000; // Overflow in multiplication
cout << "Calculated result: " << result << endl;
if (result < 0) {
cout << "Overflow occurred! Unexpected negative value: " << result << endl;
} else {
cout << "Normal operation, result: " << result << endl;
}
}
// Simulating buffer overflow leading to potential code execution (simplified)
void buffer_overflow() {
char buffer[10];
cout << "Enter input for buffer (limit 10 characters): ";
cin >> buffer; // Buffer overflow if input exceeds 10 chars
cout << "You entered: " << buffer << endl;
}
// Main function to run tests
int main() {
cout << "n--- Memory Allocation Overflow ---n";
int large_input = std::numeric_limits<int>::max() / 2; // Large input to trigger overflow
memory_allocation_overflow(large_input);
cout << "n--- Privilege Bypass via Overflow ---n";
int user_id = 2147483647; // Overflowed value simulates bypass
privilege_check(user_id);
cout << "n--- Denial of Service ---n";
large_allocation(1073741824); // Large allocation leading to DoS
cout << "n--- Arithmetic Logic Overflow ---n";
logic_overflow(2147483); // Input causing arithmetic overflow
cout << "n--- Buffer Overflow Simulation ---n";
buffer_overflow();
return 0;
}
memory_allocation_overflow
),通过不当的用户ID检查实现的权限绕过(privilege_check
),以及由于过量内存分配导致的服务拒绝(large_allocation
)。logic_overflow
函数展示了未经检查的乘法引发的算术溢出,而buffer_overflow
则通过接受超过固定缓冲区大小的输入来模拟输入相关的缓冲区溢出。代码部分重点展示了动态内存管理、权限验证和算术运算中典型的溢出场景,这些都是软件安全中至关重要的。影响探讨
void memory_allocation_overflow(int input) {
int size = input * sizeof(int); // Multiplication overflow
cout << "Allocating memory for: " << size << " bytesn";
int *buffer = (int *)malloc(size);
if (buffer == nullptr) {
cout << "Memory allocation failedn";
return;
}
// Simulate a memory write operation
for (int i = 0; i < input; i++) {
buffer[i] = i; // Buffer overflow if size is incorrectly allocated
}
cout << "Buffer populatedn";
free(buffer);
}
2147483647 / 2
(即 1073741823
),当与 sizeof(int)
相乘时,可能会超过最大整数限制。size
由于整数溢出而回绕到负值或小的正值,从而导致内存分配不足。当代码尝试写入超出分配的缓冲区时,可能会覆盖相邻内存,导致潜在的崩溃、数据损坏或被攻击者利用。void privilege_check(int user_id) {
int max_allowed_id = 1000; // Max allowed ID is 1000
if (user_id > max_allowed_id) {
cout << "Access denied for user ID: " << user_id << endl;
} else {
cout << "Access granted for user ID: " << user_id << endl;
}
}
2147483647
(32 位有符号整数的最大值)。user_id
大于 max_allowed_id
,条件检查按预期工作。然而,如果应用程序有其他检查或使用不同的逻辑涉及溢出(例如,调整 user_id
),这可能导致意外的访问,允许未授权用户获得更高权限或绕过限制。void large_allocation(int size) {
int *large_array = (int *)malloc(size * sizeof(int));
if (large_array == nullptr) {
cout << "Failed to allocate large arrayn";
return;
}
for (int i = 0; i < size; i++) {
large_array[i] = i; // Filling the array
}
cout << "Large array populatedn";
free(large_array);
}
1000000
,用于乘法运算。logic_overflow
函数中,执行 value * value
时,结果会超出 32 位有符号整数的最大值。由于没有边界检查,这将导致溢出,使得 result
变为负值或小于预期的值,从而可能导致逻辑错误或应用程序异常行为。void logic_overflow(int count) {
int result = count * 1000; // Overflow in multiplication
cout << "Calculated result: " << result << endl;
if (result < 0) {
cout << "Overflow occurred! Unexpected negative value: " << result << endl;
} else {
cout << "Normal operation, result: " << result << endl;
}
}
2147483647 / 1000
(或 2147483
),在乘以 1000
时会导致溢出。result
变为负值,表明发生了溢出。根据该结果在程序后续使用的方式(例如,作为迭代或资源分配的限制),可能会导致程序行为不正确、崩溃,或由于处理了意外值而引发安全漏洞。void buffer_overflow() {
char buffer[10];
cout << "Enter input for buffer (limit 10 characters): ";
cin >> buffer; // Buffer overflow if input exceeds 10 chars
cout << "You entered: " << buffer << endl;
}
"ThisIsDefinitelyMoreThanTenCharacters"
。声明:⽂中所涉及的技术、思路和⼯具仅供以安全为⽬的的学习交流使⽤,任何⼈不得将其⽤于⾮法⽤途以及盈利等⽬的,否则后果⾃⾏承担。
原文始发于微信公众号(白帽子左一):二进制漏洞利用 | 整数溢出探究
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论