实验环境
Visual C++6.0
实验代码
//万能密码:
//1、输入的密码长度为8;
//2、确保输入的密码与实际密码比较的结果为1,即strcpy(buffer,password)等于1;
//StackOverflow.cpp 源代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#define PASSWORD "1234567"
int verify_password (char *password)
{
int authenticated;
char buffer[8]; // add local buff to be overflowed
authenticated=strcmp(password,PASSWORD);
strcpy(buffer,password); //overflowed here!
return authenticated;
}
void main()
{
int valid_flag=0;
char password[1024];
while(1)
{
printf("please input password:");
scanf("%s",password);
valid_flag = verify_password(password);
if(valid_flag)
{
printf("incorrect password!\n\n");
}
else
{
printf("Congratulation! You have passed the verification!\n");
break;
}
}
}
Visual Studio 2012运行结果
Visual C++6.0运行结果
从以上图片中可以看出问题:
输入的密码错误后也能通过验证
栈帧分析
当是大端存储时,根据代码画出如图栈帧结构可知,栈溢出,会覆盖其它存储的变量值。
总结
满足以下条件时,可以绕过密码验证
1.调用strcmp函数时结果为1
2.输入的密码长度为缓冲数组buffer的数组长,即8个字节
(输入8个字节后系统会自动补充“\0”结束标志,其ASCII为000)
3.大端存储