ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:103.82KB ,
资源ID:17375521      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/17375521.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Playing with ptrace 2.docx)为本站会员(b****1)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Playing with ptrace 2.docx

1、Playing with ptrace 2In Part II of his series on ptrace, Pradeep tackles the more advanced topics of setting breakpoints and injecting code into running processes. In Part I of this article LJ, November 2002, we saw how ptrace can be used to trace system calls and change system call arguments. In th

2、is article, we investigate advanced techniques like setting breakpoints and injecting code into running programs. Debuggers use these methods to set up breakpoints and execute debugging handlers. As with Part I, all code in this article is i386 architecture-specific. Attaching to a Running ProcessIn

3、 Part I, we ran the process to be traced as a child after calling ptrace(PTRACE_TRACEME, .). If you simply wanted to see how the process is making system calls and trace the program, this would be sufficient. If you want to trace or debug a process already running, then ptrace(PTRACE_ATTACH, .) shou

4、ld be used.When a ptrace(PTRACE_ATTACH, .) is called with the pid to be traced, it is roughly equivalent to the process calling ptrace(PTRACE_TRACEME, .) and becoming a child of the tracing process. The traced process is sent a SIGSTOP, so we can examine and modify the process as usual. After we are

5、 done with modifications or tracing, we can let the traced process continue on its own by calling ptrace(PTRACE_DETACH, .).The following is the code for a small example tracing program:int main() int i; for(i = 0;i 10; +i) printf(My counter: %dn, i); sleep(2); return 0;Save the program as dummy2.c.

6、Compile and run it: gcc -o dummy2 dummy2.c./dummy2 &Now, we can attach to dummy2 by using the code below: #include #include #include #include #include /* For user_regs_struct etc. */int main(int argc, char *argv) pid_t traced_process; struct user_regs_struct regs; long ins; if(argc != 2) printf(Usag

7、e: %s n, argv0, argv1); exit(1); traced_process = atoi(argv1); ptrace(PTRACE_ATTACH, traced_process, NULL, NULL); wait(NULL); ptrace(PTRACE_GETREGS, traced_process, NULL, ®s); ins = ptrace(PTRACE_PEEKTEXT, traced_process, regs.eip, NULL); printf(EIP: %lx Instruction executed: %lxn, regs.eip, ins)

8、; ptrace(PTRACE_DETACH, traced_process, NULL, NULL); return 0;The above program simply attaches to a process, waits for it to stop, examines its eip (instruction pointer) and detaches. To inject code use ptrace(PTRACE_POKETEXT, .) and ptrace(PTRACE_POKEDATA, .) after the traced process has stopped.S

9、etting BreakpointsHow do debuggers set breakpoints? Generally, they replace the instruction to be executed with a trap instruction, so that when the traced program stops, the tracing program, the debugger, can examine it. It will replace the original instruction once the tracing program continues th

10、e traced process. Heres an example:#include #include #include #include #include const int long_size = sizeof(long);void getdata(pid_t child, long addr, char *str, int len) char *laddr; int i, j; union u long val; char charslong_size; data; i = 0; j = len / long_size; laddr = str; while(i j) data.val

11、 = ptrace(PTRACE_PEEKDATA, child, addr + i * 4, NULL); memcpy(laddr, data.chars, long_size); +i; laddr += long_size; j = len % long_size; if(j != 0) data.val = ptrace(PTRACE_PEEKDATA, child, addr + i * 4, NULL); memcpy(laddr, data.chars, j); strlen = 0;void putdata(pid_t child, long addr, char *str,

12、 int len) char *laddr; int i, j; union u long val; char charslong_size; data; i = 0; j = len / long_size; laddr = str; while(i j) memcpy(data.chars, laddr, long_size); ptrace(PTRACE_POKEDATA, child, addr + i * 4, data.val); +i; laddr += long_size; j = len % long_size; if(j != 0) memcpy(data.chars, l

13、addr, j); ptrace(PTRACE_POKEDATA, child, addr + i * 4, data.val); int main(int argc, char *argv) pid_t traced_process; struct user_regs_struct regs, newregs; long ins; /* int 0x80, int3 */ char code = 0xcd,0x80,0xcc,0; char backup4; if(argc != 2) printf(Usage: %s n, argv0, argv1); exit(1); traced_pr

14、ocess = atoi(argv1); ptrace(PTRACE_ATTACH, traced_process, NULL, NULL); wait(NULL); ptrace(PTRACE_GETREGS, traced_process, NULL, ®s); /* Copy instructions into a backup variable */ getdata(traced_process, regs.eip, backup, 3); /* Put the breakpoint */ putdata(traced_process, regs.eip, code, 3); /

15、* Let the process continue and execute the int 3 instruction */ ptrace(PTRACE_CONT, traced_process, NULL, NULL); wait(NULL); printf(The process stopped, putting back the original instructionsn); printf(Press to continuen); getchar(); putdata(traced_process, regs.eip, backup, 3); /* Setting the eip b

16、ack to the original instruction to let the process continue */ ptrace(PTRACE_SETREGS, traced_process, NULL, ®s); ptrace(PTRACE_DETACH, traced_process, NULL, NULL); return 0;Here we replace the three bytes with the code for a trap instruction, and when the process stops, we replace the original in

17、structions and reset the eip to original location. Figures 1-4 clarify how the instruction stream looks when above program is executed. Figure 1. After the Process Is StoppedFigure 2. After the Trap Instruction Bytes Are SetFigure 3. Trap Is Hit and Control Is Given to the Tracing ProgramFigure 4. A

18、fter the Original Instructions Are Replaced and eip Is Reset to the Original LocationNow that we have a clear idea of how breakpoints are set, lets inject some code bytes into a running program. These code bytes will print “hello world”.The following program is a simple “hello world” program with mo

19、difications to fit our needs. Compile the following program with:gcc -o hello hello.cvoid main()_asm_( jmp forwardbackward: popl %esi # Get the address of # hello world string movl $4, %eax # Do write system call movl $2, %ebx movl %esi, %ecx movl $12, %edx int $0x80 int3 # Breakpoint. Here the # pr

20、ogram will stop and # give control back to # the parentforward: call backward .string Hello Worldn );The jumping backward and forward here is required to find the address of the “hello world” string. We can get the machine code for the above assembly from GDB. Fire up GDB and disassemble the program

21、:(gdb) disassemble mainDump of assembler code for function main:0x80483e0 : push %ebp0x80483e1 : mov %esp,%ebp0x80483e3 : jmp 0x80483fa End of assembler dump.(gdb) disassemble forwardDump of assembler code for function forward:0x80483fa : call 0x80483e5 0x80483ff : dec %eax0x8048400 : gs0x8048401 :

22、insb (%dx),%es:(%edi)0x8048402 : insb (%dx),%es:(%edi)0x8048403 : outsl %ds:(%esi),(%dx)0x8048404 : and %dl,0x6f(%edi)0x8048407 : jb 0x80484750x8048409 : or %fs:(%eax),%al0x804840c : mov %ebp,%esp0x804840e : pop %ebp0x804840f : retEnd of assembler dump.(gdb) disassemble backwardDump of assembler cod

23、e for function backward:0x80483e5 : pop %esi0x80483e6 : mov $0x4,%eax0x80483eb : mov $0x2,%ebx0x80483f0 : mov %esi,%ecx0x80483f2 : mov $0xc,%edx0x80483f7 : int $0x800x80483f9 : int3End of assembler dump.We need to take the machine code bytes from main+3 to backward+20, which is a total of 41 bytes.

24、The machine code can be seen with the x command in GDB: (gdb) x/40bx main+3: eb 15 5e b8 04 00 00 00: bb 02 00 00 00 89 f1 ba: 0c 00 00 00 cd 80 cc: e6 ff ff ff 48 65 6c 6c: 6f 20 57 6f 72 6c 64 0aNow we have the instruction bytes to be executed. Why wait? We can inject them using the same method as

25、 in the previous example. The following is the source code; only the main function is given here: int main(int argc, char *argv) pid_t traced_process; struct user_regs_struct regs, newregs; long ins; int len = 41; char insertcode =xebx15x5exb8x04x00 x00x00xbbx02x00x00x00x89xf1xba x0cx00x00x00xcdx80x

26、ccxe8xe6xff xffxffx48x65x6cx6cx6fx20x57x6f x72x6cx64x0ax00; char backuplen; if(argc != 2) printf(Usage: %s n, argv0, argv1); exit(1); traced_process = atoi(argv1); ptrace(PTRACE_ATTACH, traced_process, NULL, NULL); wait(NULL); ptrace(PTRACE_GETREGS, traced_process, NULL, ®s); getdata(traced_proce

27、ss, regs.eip, backup, len); putdata(traced_process, regs.eip, insertcode, len); ptrace(PTRACE_SETREGS, traced_process, NULL, ®s); ptrace(PTRACE_CONT, traced_process, NULL, NULL); wait(NULL); printf(The process stopped, Putting back the original instructionsn); putdata(traced_process, regs.eip, ba

28、ckup, len); ptrace(PTRACE_SETREGS, traced_process, NULL, ®s); printf(Letting it continue with original flown); ptrace(PTRACE_DETACH, traced_process, NULL, NULL); return 0;In Part II of his series on ptrace, Pradeep tackles the more advanced topics of setting breakpoints and injecting code into ru

29、nning processes. Injecting the Code into Free SpaceIn the previous example we injected the code directly into the executing instruction stream. However, debuggers can get confused with this kind of behaviour, so lets find the free space in the process and inject the code there. We can find free space by examining the /proc/pid/maps file of the tr

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1