博客
关于我
缓冲区溢出基本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与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>