格式化字符串溢出指的是调用类似sprintf函数时,没有限定长度而产生溢出。printf中的%s是直接在后面的参数栈中搜索字符串的,直到遇到’\0’。以下代码就可以产生简单的格式化溢出效果。
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#pragma runtime_checks( "[runtime_checks]", off)
void Overflow(char* pData, unsigned short cbData)
{
char szTest[10] = { 0 };
if (cbData > 10)
return;
sprintf(szTest, "%s", pData);
return;
}
int main()
{
int nLen = 0x010001;
char *strShoot = (char*)malloc(nLen);
memset(strShoot, 0x41, nLen);
*(strShoot + nLen - 1) = 0;
Overflow(strShoot, strlen(strShoot));
system("pause");
return 0;
}