栈溢出案例

实验环境
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.大端存储
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值