博客
关于我
缓冲区溢出基本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/

你可能感兴趣的文章
Linux下的系统监控与性能调优:从入门到精通
查看>>
LiveGBS user/save 逻辑缺陷漏洞复现(CNVD-2023-72138)
查看>>
localhost:5000在MacOS V12(蒙特利)中不可用
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
Moment.js常见用法总结
查看>>