首页 > 其他 > 详细

OD:

时间:2014-03-21 23:02:04      阅读:515      评论:0      收藏:0      [点我收藏+]

实验代码:

bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define PASSWORD "1234567"
 4 
 5 int verify_password(char *password)
 6 {
 7     int authenticated;
 8     char buffer[8];     // add local buf to be overflowed
 9     authenticated=strcmp(password,PASSWORD);
10     strcpy(buffer, password);  // overflow here
11     return authenticated;
12 }
13 
14 int main()
15 {
16     int valid_flag=0;
17     char password[1024];
18     while(1){
19         printf("Please input password: ");
20         scanf("%s",password);
21         valid_flag=verify_password(password);
22         if(valid_flag){
23             printf("Incorrect password!\n\n");
24         }
25         else
26         {
27             printf("Congratulation! You have passed the verification!\n\n\n");
28             break;
29         }
30     }
31     return 0;
32 }
bubuko.com,布布扣

注意以上第 8 行和第 10 行的代码,对于猜测变量在内存的相对位置和溢出尝试有用。

 

 

程序执行到 int verify_password(char *password) 时的栈帧如图:

bubuko.com,布布扣

 

 

 

 

 

 

 

 

 

 

(变量在内存中的位置可能因编译优化而与上图不一致)

 

可见 authenticated (int 类型,内存中为 DWORD,占 4 字节)恰在 buffer 的 “下方”,如果 buffer 越界,那么 buffer[8..11] 刚好能覆盖 authenticated !

如果输入的字符超过 7 个字符(null 会占第 8 个字符),则越界字符会覆盖 authenticated。若 authenticated 被覆盖为 0,则溢出成功。

OD:,布布扣,bubuko.com

OD:

原文:http://www.cnblogs.com/exclm/p/3616752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!