博客
关于我
缓冲区溢出基本C程序的控制台和Win32版本
阅读量:110 次
发布时间:2019-02-26

本文共 2980 字,大约阅读时间需要 9 分钟。

#include 
#include
int main(void){ char buff[8] = {0}; char *p = "012345678912345"; strcpy(buff,p); printf("%s\n",buff); return 0;}

这是缓冲区溢出原理演示的基本C程序;

p所指向的字符串长度大于buff的长度,拷贝时发生缓冲区溢出;程序崩溃;

 

如果p指向字符串的长度不超过,就不会溢出;

 

对于Win32来说,使用Windows提供的Win32字符串函数,字符串拷贝使用lstrcpy;

#include 
#include
int main(void){ char buff[8] = {0}; char *p = "0123456789"; lstrcpy(buff,p); printf("%s\n",buff); return 0;}

一样的,长度超过,运行程序,崩溃;

 

如果使用安全版的字符串拷贝函数,strncpy(buff,p,sizeof(buff));

就不会溢出;

 

下面来作一个Win32窗口版本溢出程序;单击鼠标左键执行拷贝并输出;

/* buffer over demo,by bobo,2020-01-19 */#include 
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ static TCHAR szAppName[] = TEXT ("buffer over demo") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = szAppName ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName,TEXT ("buffer over demo"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hdc ; PAINTSTRUCT ps ; char buff[8] = {0}; char *p = "012345678912345"; switch (message) { case WM_CREATE: return 0 ; case WM_LBUTTONDOWN : hdc = GetDC(hwnd); lstrcpy(buff,p); TextOut(hdc, 100, 100, buff, 15); return 0; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ;}

buff长度8,p指向内容长度15;

按理程序应该崩溃;但是Win32窗口程序真的没崩溃;还输出了15个长度的字符串;

啥情况?

以上是Win10,VC++6;

 

此程序仅演示缓冲区溢出最基本原理;

如果真能干这事的人,其目的并不是引起程序崩溃,而是通过覆盖程序返回地址等手段,改变程序的执行流程;

 

转载地址:http://jouy.baihongyu.com/

你可能感兴趣的文章
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>