免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3317 | 回复: 4
打印 上一主题 下一主题

james foster写的 缓冲区溢出区攻击 一书中s-proc.c例子为何运行不了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-13 16:09 |只看该作者 |倒序浏览
本帖最后由 oatt 于 2011-01-13 16:18 编辑

在James C foster的书buffer overflow attacks -  detect, exploit, prevent当中第三章中有个例子s-proc.c,书中它是能执行的,并且有正确的执行结果,书中的代码及执行结果如下面两个段代码所示。
   但是我在运行书上面的例子时,出现段错误,我认为调用fread(code, 1, flen, fp)时,是把代码(write.s编译后生产的)写入数据段,数据段是不可执行的,所以会出现段错误。
   为什么写书的作者能运行下面的代码,我却实现不了??????
  1. Example 3.1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <errno.h>

  8. /*
  9. * Print message function
  10. */
  11. static void croak(const char *msg) {
  12.         fprintf(stderr, "%s\n", msg);
  13.         fflush(stderr);
  14. }
  15. /*
  16. * Usage function
  17. */
  18. static void usage(const char *prgnam) {
  19.         fprintf(stderr, "\nExecute code : %s -e <file-containingshellcode>\n", prgnam);
  20.         fprintf(stderr, "Convert code : %s -p <file-containing-shellcode>\n\n", prgnam);
  21.         fflush(stderr);
  22.         exit(1);
  23. }
  24. /*
  25. * Signal error and bail out.
  26. */
  27. static void barf(const char *msg) {
  28.         perror(msg);
  29.         exit(1);
  30. }

  31. /*
  32. * Main code starts here
  33. */

  34. int main(int argc, char **argv) {
  35. FILE *fp;
  36. void *code;
  37. int arg;
  38. int i;
  39. int l;
  40. int m = 15; /* max # of bytes to print on one line */

  41. struct stat sbuf;
  42. long flen; /* Note: assume files are < ** bytes long;-) */
  43. void (*fptr)(void);

  44. if(argc < 3)
  45.         usage(argv[0]);
  46. if(stat(argv[], &sbuf))
  47.         barf("failed to stat file");
  48. flen = (long) sbuf.st_size;
  49. if(!(code = malloc(flen)))
  50.         barf("failed to grab required memory");
  51. if(!(fp = fopen(argv[2], "rb")))
  52.         barf("failed to open file");
  53. if(fread(code, 1, flen, fp) != flen)
  54.         barf("failed to slurp file");
  55. if(fclose(fp))
  56.         barf("failed to close file");

  57. while ((arg = getopt (argc, argv, "e:p:")) != -1){
  58.         switch (arg){
  59.                 case 'e':
  60.                 croak("Calling code ...");
  61.                 fptr = (void (*)(void)) code;
  62.                 (*fptr)();
  63.                 break;
  64.                 case 'p':
  65.                 printf("\n/* The following shellcode is %d bytes long:*/\n",flen);
  66.                 printf("\nchar shellcode[] =\n");
  67.                 l = m;
  68.                 for(i = 0; i < flen; ++i) {
  69.                         if(l >= m) {
  70.                                 if(i) printf("\"\n");
  71.                                 printf( "\t\"");
  72.                                 l = 0;
  73.                         }
  74.                         ++l;
  75.                         printf("\\x%0x", ((unsigned char *)code)[i]);
  76.                  }
  77.                 printf("\";\n\n\n");
  78.                 break;
  79.                  default :
  80.                 usage(argv[0]);
  81.         }
  82. }
  83. return 0;
  84. }
复制代码
下面一行编译上面的代码:
gcc –o s-proc s-proc.c
  1. Example 3.3 Linux Shellcode for Hello, World!
  2. xor eax,eax
  3. xor ebx,ebx
  4. xor ecx,ecx
  5. xor edx,edx
  6. jmp short string
  7. code:
  8. pop ecx
  9. mov bl,1
  10. mov dl,13
  11. mov al,4
  12. int 0x80
  13. dec bl
  14. mov al,1
  15. int 0x80
  16. string:
  17. call code
  18. db 'Hello, world!'
复制代码

下面编译上面的代码,及执行的情况:
[root@gabriel]# nasm -o write write.S

[root@gabriel]# s-proc -e write
Calling code ...
Hello, world!

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
2 [报告]
发表于 2011-01-14 11:20 |只看该作者
看一下书中介绍的环境和你的测试环境有哪些区别。
溢出方面,好像是高版本的 gcc 会做一些相关的检查

论坛徽章:
0
3 [报告]
发表于 2011-01-20 16:31 |只看该作者
估计是编译器的问题(安全优化),以及系统问题(堆栈不可执行)

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
4 [报告]
发表于 2011-01-20 17:38 |只看该作者
你可以降低编译器版本试试

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
5 [报告]
发表于 2011-01-21 10:47 |只看该作者
对。高版本的编译器都会做一些安全检查了。所以不少缓冲区溢出的例子,之所以可能成功,一方面也是因为编译器没有做太多的检查
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP