当前位置:网站首页>游戏安全03:缓冲区溢出攻击简单解释
游戏安全03:缓冲区溢出攻击简单解释
2022-07-31 22:50:00 【谢白羽】
文章目录
一、举例具体体现
void add()
{
}
int32_t verify(const char* password)
{
add();
}
int main()
{
char password[1024];
int valid;
while(1)
{
scanf("%s",password);
valid = veryfy(password)
if(valid)
{
}
else
{
}
}
}
假设密码设置为1234567,当传入的字符串长度大于strlen (1234567)时,都会返回成功匹配密码的结果(比如传入8个8:)
二、缓冲区溢出攻击原理
备注:
缓冲区既可以是栈也可以是堆危害
多余缓冲区长度的数据可能会把其他区域的数据给覆盖掉(内存越界)
(1)栈帧基础原理


1)栈区(stack):由编译器自动分配与释放,存放为运行时函数
分配的局部变量、函数参数、返回数据、返回地址等。其操作类
似于数据结构中的栈。
2)堆区(heap):一般由程序员自动分配,如果程序员没有释
放,程序结束时可能有OS回收。其分配类似于链表。
3)全局区(静态区static):数据段,程序结束后由系统释放。全
局区分为已初始化全局区(data),用来存放保存全局的和静态
的已初始化变量和未初始化全局区(bss),用来保存全局的和静
态的未初始化变量。
4)常量区(文字常量区):数据段,存放常量字符串,程序结束
后有系统释放。
5)代码区:存放函数体(类成员函数和全局区)的二进制代码,
这个段在内存中一般被标记为只读,任何对该区的写操作都会导
致段错误(Segmentation Fault)。
- 栈的特点:
先进后出
- 基础知识
1)用OD查看堆栈
寄存器传送门(我总结的一点些汇编知识)
ESP :栈指针,指向栈顶
EBP :帧指针,指向当前活动的底部(若在add函数里面,就只想add和verify之前的接壤处)
EIP:指令寄存器:执行指令的地址(EIP就是程序的执行流程)
函数:调用的时候,系统都会为这个函数开辟一个栈帧(函数作用域)
系统栈:整个程序调用的栈

每个小格子就是特定函数的栈帧
(2)函数调用约定
//函数参数入栈的顺序是什么?a,b,c,d
int test(int a,int b, int c, int d)
{
return a + b + c + d ;
}
int main()
{
int a = 10;
printf("%d ,%d,%d,%d",a =a +1,a = a+ 1,a= a+1,a=a + 1);
}
//打印结果是14 13 12 11(栈的先进后出)
(3)溢出攻击原理
当程序写入超过缓冲区的边界时,就会产生所谓的“缓冲区溢出”。发生缓冲区溢出时,就会覆盖下一个相邻的内存块(覆盖相邻函数的栈帧),导致程序发生一些不可预料的结果:也许程序可以继续,也许程序的执行出现奇怪现象,也许程序完全失败或者崩溃等。
(4)缓冲区溢出攻击分类和举例
栈溢出、堆溢出、BSS溢出与格式化串溢出。其中,栈溢出是最简单,也是最为常见的一种溢出方式。
1)没有保证足够的存储空间存储复制过来的数据
void function(char *str)
{
char buffer[10];
strcpy(buffer,str);
}
上面的strcpy()将直接把str中的内容copy到buffer中。这样只要str的长度大于 10 ,就会造成buffer的溢出,使程序运行出错。存在象strcpy这样的问题的标准函数还有strcat(),sprintf(),vsprintf(),gets(),scanf()等。对应的有更加安全的函数,即在函数名后加上_s,如scanf_s()函数。
2)整数溢出
(1)整数溢出
把一个宽度较大的操作数赋给宽度较小的操作数,就有可能发生数据截断或符号位丢失
#include<stdio.h>
int main()
{
signed int value1 = 10;
usigned int value2 = (unsigned int)value1;
}
(1)算术溢出
该程序即使在接受用户输入的时候对a、b的赋值做安全性检查,a+b依旧可能溢出:
#include<stdio.h>
int main()
{
int a;
int b;
int c=a*b;
return 0;
}
3)数组索引不在合法范围内
enum {TABLESIZE = 100};
int *table = NULL;
int insert_in_table(int pos, int value) {
if(!table) {
table = (int *)malloc(sizeof(int) *TABLESIZE);
}
if(pos >= TABLESIZE) {
return -1;
}
table[pos] = value;
return 0;
}
其中:pos为int类型,可能为负数,这会导致在数组所引用的内存边界之外进行写入,可以将pos类型改为size_t避免
4)空字符错误
//错误
char array[]={
'0','1','2','3','4','5','6','7','8'};
//正确的写法应为:
char array[]={
'0','1','2','3','4','5','6','7','8',’\0’};
//或者
char array[11]={
'0','1','2','3','4','5','6','7','8','9’};
边栏推荐
- SQL注入 Less46(order by后的注入+rand()布尔盲注)
- 面试突击69:TCP 可靠吗?为什么?
- 二叉树非递归遍历
- Write a database document management tool based on WPF repeating the wheel (1)
- 一款国外开发的高质量WordPress下载站模板主题
- 「SDOI2016」征途 题解
- 基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
- #yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
- How to debug TestCafe
- Unity - LineRenderer show a line
猜你喜欢

Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic
SQL27 View user details of different age groups

逐步手撕轮播图3(保姆级教程)

【Acwing】第62场周赛 题解

A high-quality WordPress download site template theme developed abroad

Pytest first experience

数据分析(一)——matplotlib
Dry goods | 10 tips for MySQL add, delete, change query performance optimization

ECCV 2022 Huake & ETH propose OSFormer, the first one-stage Transformer framework for camouflaging instance segmentation!The code is open source!...

Recognize anomalies (you will understand after reading this)
随机推荐
BOW/DOM (top)
Niuke.com brush questions (1)
Talking about the algorithm security of network security
一款国外开发的高质量WordPress下载站模板主题
Flink_CDC construction and simple use
Several methods of mysql backup table
【Acwing】第62场周赛 题解
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
高等代数_证明_任何矩阵都相似于一个上三角矩阵
(26)Blender源码分析之顶层菜单的关于菜单
Summary of the classic drawing method of histogram
[NLP] What is the memory of the model!
VOT2021比赛简介
Write a database document management tool based on WPF repeating the wheel (1)
The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
[QNX Hypervisor 2.2用户手册]9.15 suppress
新产品如何进行网络推广?
iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools
useragent online lookup
C程序设计-方法与实践(清华大学出版社)习题解析