免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 思一克
打印 上一主题 下一主题

一个所谓的全局变量地址改变的例子 [复制链接]

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
41 [报告]
发表于 2010-01-30 19:08 |只看该作者
原帖由 swordfish.cn 于 2010-1-30 18:37 发表


“让代码的行为依赖于编译参数”不是好办法。但是在这个例子的情况下,我认为是最好的选择。既然anchor也无法完全确保程序的行为,我也就不再需要它了。当然,这是个别情况。




实际调用的时候做了转 ...

等一下,我正在写一个例子

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
42 [报告]
发表于 2010-01-30 19:25 |只看该作者

  1. linux-0gt0:~/weiqi # cat /etc/issue

  2. Welcome to SUSE Linux Enterprise Server 10 (i586) - Kernel \r (\l).


  3. linux-0gt0:~/weiqi # uname -a
  4. Linux linux-0gt0 2.6.16.21-0.8-default #1 Mon Jul 3 18:25:39 UTC 2006 i686 i686 i386 GNU/Linux
  5. linux-0gt0:~/weiqi # cat 1.c
  6. #include <sys/mman.h>
  7. #include <stdio.h>
  8. #include <asm/unistd.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. int main()
  12. {
  13.         unsigned int i,j,k;
  14.         FILE* f;
  15.         char *p;
  16.         char cmd[200];
  17.         char s[]={0x55,0x89,0xe5,0x83,0xec,0x38,0xc6,0x45,0xf8,0x2f,0xc6,0x45,0xf9,0x62,0xc6,0x45,0xfa,0x69,0xc6,0x45,0xfb,0x6e,0xc6,0x45,0xfc,0x2f,0xc6,0x45,0xfd,0x6c,0xc6,0x45,0xfe,0x73,0xc6,0x45,0xff,0x00,0xc6,0x45,0xf4,0x2f,0xc6,0x45,0xf5,0x6c,0xc6,0x45,0xf6,0x73,0xc6,0x45,0xf7,0x00,0x8d,0x45,0xf4,0x89,0x45,0xec,0xc7,0x45,0xf0,0x00,0x00,0x00,0x00,0xc7,0x45,0xe8,0x00,0x00,0x00,0x00,0x8d,0x45,0xe8,0x8d,0x55,0xec,0x8d,0x4d,0xf8,0x89,0xcb,0x89,0xd1,0x89,0xc2,0xb8,0x0b,0x00,0x00,0x00,0xcd,0x80};
  18.         i=(unsigned)puts;
  19.         sprintf(cmd,"awk '{x=$1;y=x;sub(/.*-/,\"\",x);sub(/-.*/,\"\",y);if(strtonum(\"0x\"x)>%#x&&strtonum(\"0x\"y<%#x)){print y,x-y;exit}}' /proc/%d/maps",i,i,getpid());
  20.         f=popen(cmd,"r");
  21.         fscanf(f,"%x%x",&i,&j);
  22.         fclose(f);

  23.         if(mprotect((void*)i,j,PROT_WRITE|PROT_READ|PROT_EXEC) < 0)
  24.                 perror("mprotect");
  25.         p=(char*)puts;
  26.         for(i=0;i<sizeof(s);i++)
  27.                 p[i]=s[i];

  28.         puts("have a try\n");
  29.         return 0;
  30. }
  31. linux-0gt0:~/weiqi # gcc 1.c
  32. linux-0gt0:~/weiqi # ./a.out
  33. 1  1.c  1.s  2  2.s  a.out  rules.c  x
  34. linux-0gt0:~/weiqi # ls
  35. 1  1.c  1.s  2  2.s  a.out  rules.c  x

复制代码

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
43 [报告]
发表于 2010-01-30 19:33 |只看该作者
另外,术语栈(stack)的确是一个硬件相关的东西,C语言标准里是不会提到这个的。
并且栈的增长方面如同大端小端一样,并没有一个标准,C语言编译器是用栈传还是寄存器传,也并没有一个标准。
所以我说我开始写的那个代码能起作用完全是“硬件/编译相关”的

[ 本帖最后由 cjaizss 于 2010-1-30 19:36 编辑 ]

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
44 [报告]
发表于 2010-01-31 12:56 |只看该作者
再发一个

  1. #include <stdio.h>



  2. int gi = 1;



  3. f()

  4. {

  5. int i;



  6.   /**/
  7.           int j;
  8.         char cmd[200];
  9.         FILE* f;
  10.         char *p;
  11.         char s[]={
  12.                 0x55,0x89,0xe5,0x81,0xec,0x98,0x00,0x00,0x00,0xc7,0x45,0xfc,0x00,0x00,0x00,0x00,0xc7,0x45,0xf0,0x00,0x00,0x00,0x00,0xa1,0x90,0x98,0x04,0x08,0x83,0xe8,0x01,0xa3,0x90,0x98,0x04,0x08,0xa1,0x90,0x98,0x04,0x08,0x85,0xc0,0x75,0x08,0x83,0x45,0x0c,0x04,0x83,0x45,0x10,0x01,0xc7,0x45,0xf0,0x00,0x00,0x00,0x00,0xeb,0x15,0x8b,0x45,0xf0,0x8b,0x55,0x08,0x0f,0xb6,0x12,0x88,0x54,0x05,0x8c,0x83,0x45,0x08,0x01,0x83,0x45,0xf0,0x01,0x83,0x7d,0xf0,0x06,0x7e,0xe5,0x8b,0x45,0xf0,0xc6,0x44,0x05,0x8c,0x30,0x83,0x45,0xf0,0x01,0x8b,0x45,0xf0,0xc6,0x44,0x05,0x8c,0x78,0x83,0x45,0xf0,0x01,0xc7,0x45,0xf4,0x1c,0x00,0x00,0x00,0xeb,0x5a,0x8b,0x4d,0xf4,0xb8,0x0f,0x00,0x00,0x00,0xd3,0xe0,0x89,0xc2,0x8b,0x45,0x0c,0x21,0xd0,0x8b,0x4d,0xf4,0xd3,0xe8,0x89,0x45,0xf8,0x83,0x7d,0xf8,0x00,0x75,0x06,0x83,0x7d,0xfc,0x00,0x74,0x31,0xc7,0x45,0xfc,0x01,0x00,0x00,0x00,0x83,0x7d,0xf8,0x09,0x7f,0x13,0x8b,0x55,0xf0,0x8b,0x45,0xf8,0x83,0xc0,0x30,0x88,0x44,0x15,0x8c,0x83,0x45,0xf0,0x01,0xeb,0x11,0x8b,0x55,0xf0,0x8b,0x45,0xf8,0x83,0xc0,0x57,0x88,0x44,0x15,0x8c,0x83,0x45,0xf0,0x01,0x83,0x6d,0xf4,0x04,0x83,0x7d,0xf4,0x00,0x79,0xa0,0x83,0x45,0x08,0x02,0xc7,0x45,0xf4,0x00,0x00,0x00,0x00,0xeb,0x19,0x8b,0x45,0xf0,0x8b,0x55,0x08,0x0f,0xb6,0x12,0x88,0x54,0x05,0x8c,0x83,0x45,0xf0,0x01,0x83,0x45,0x08,0x01,0x83,0x45,0xf4,0x01,0x83,0x7d,0xf4,0x08,0x7e,0xe1,0x83,0x7d,0x10,0x01,0x75,0x0e,0x8b,0x45,0xf0,0xc6,0x44,0x05,0x8c,0x31,0x83,0x45,0xf0,0x01,0xeb,0x0c,0x8b,0x45,0xf0,0xc6,0x44,0x05,0x8c,0x32,0x83,0x45,0xf0,0x01,0x8b,0x45,0xf0,0xc6,0x44,0x05,0x8c,0x0a,0x83,0x45,0xf0,0x01,0x8b,0x55,0xf0,0x8d,0x4d,0x8c,0xbb,0x01,0x00,0x00,0x00,0xb8,0x04,0x00,0x00,0x00,0xcd,0x80,0xc9,0xc3,
  13.                 0x00,0x01,0x00,0x00
  14.         };
  15.         i=(unsigned)printf;
  16.         sprintf(cmd,"awk '{x=$1;y=x;sub(/.*-/,\"\",x);sub(/-.*/,\"\",y);if(strtonum(\"0x\"x)>%#x&&strtonum(\"0x\"y<%#x)){print y,x-y;exit}}' /proc/%d/maps",i,i,getpid());
  17.         f=popen(cmd,"r");
  18.         fscanf(f,"%x%x",&i,&j);
  19.         fclose(f);
  20. #include <sys/mman.h>
  21.         //int mprotect(void *addr, size_t len, int prot);
  22.         if(mprotect((void*)i,j,PROT_WRITE|PROT_READ|PROT_EXEC) < 0)
  23.                 perror("mprotect");
  24.         p=(char*)printf;
  25.         j = (unsigned)printf+sizeof(s)-4;
  26.         *(unsigned*)(s+24)=*(unsigned*)(s+32)=*(unsigned*)(s+37)=j;
  27.         for(i=0;i<sizeof(s);i++)
  28.                 p[i]=s[i];


  29.   /**/



  30.   for(i = 0; i < 256; i++) {

  31.     printf("addr = %p value = %d\n", &gi, gi);

  32.   }

  33. }



  34. main()

  35. {

  36.   f();

  37. }

复制代码

论坛徽章:
0
45 [报告]
发表于 2010-01-31 16:05 |只看该作者
回复 45# cjaizss

直接修改了printf函数?

论坛徽章:
0
46 [报告]
发表于 2010-01-31 19:16 |只看该作者
i == &gi?

论坛徽章:
0
47 [报告]
发表于 2010-02-01 11:50 |只看该作者
添加的什么代码啊?

论坛徽章:
0
48 [报告]
发表于 2010-02-01 11:51 |只看该作者
添加的什么代码啊?

论坛徽章:
0
49 [报告]
发表于 2010-02-01 16:45 |只看该作者
问题太高深了 我不懂

论坛徽章:
0
50 [报告]
发表于 2010-02-01 17:51 |只看该作者
来个简单点的:
  1. [bobo]$ cat gotover.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int evil_code(char *s)
  5. {
  6.         printf("hi, I am here!\n");
  7.         return 0;
  8. }

  9. int main(int argc, char *argv[])
  10. {
  11.         long addr = *(long *)((long)strlen + 2);

  12.         *(long *)addr = (long)evil_code;

  13.         int len = strlen(argv[0]);

  14.         printf("len = [%d]\n", len);

  15.         return 0;
  16. }

  17. [bobo]$ gcc gotover.c -o gotover
  18. [bobo]$ ./gotover
  19. hi, I am here!
  20. len = [0]
  21. [bobo]$
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP