博客
关于我
缓冲区溢出基本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、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>