Chinaunix首页 | 论坛 | 博客
  • 博客访问: 296538
  • 博文数量: 94
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 202
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-08 20:07
文章分类

全部博文(94)

文章存档

2017年(19)

2016年(30)

2015年(12)

2014年(33)

我的朋友

分类: C/C++

2016-07-12 21:06:04

用C语言写一个程序,访问一个进程的所有地址空间,并返回可读内存区域。大致思路为按页扫描所有地址空间,当访问无读权限内存区域时,将触发段错误,在段错误处理程序中longjmp回主循环并继续....,按此思路,大家可以试试扫描写权限区域。

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <setjmp.h>

  4. #define PAGE_SIZE    0x1000
  5. #define ADDR_END     0xfffff000

  6. static volatile int addr;
  7. static volatile int flag;
  8. static jmp_buf jmpbuf;

  9. void handler(int signo)
  10. {
  11.     longjmp(jmpbuf, 1);
  12. }

  13. int main(void)
  14. {
  15.     for (; addr < ADDR_END; addr += PAGE_SIZE) {
  16.         signal(SIGSEGV, handler);
  17.         if (!setjmp(jmpbuf)) {
  18.             int c = *(int *)addr;
  19.             if (!flag) {
  20.                 printf("\n0x%x ~ ", addr);
  21.                 flag = 1;
  22.             }
  23.         }
  24.         else if (flag) {
  25.             printf("0x%x", addr - 1);
  26.             flag = 0;
  27.         }
  28.     }
  29.     return 0;
  30. }

 


 

阅读(1418) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~