shellcode缓冲区溢出-SegFault

时间:2017-11-06 19:24:14

标签: c gcc segmentation-fault buffer-overflow shellcode

我正在尝试运行此shellcode,但我一直在分段错误

/* call_shellcode.c */
/*A program that creates a file containing code for launching shell*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const char code[] =
   "\x31\xc0" /* Line 1: xorl %eax,%eax */
   "\x50" /* Line 2: pushl %eax */
   "\x68""//sh" /* Line 3: pushl $0x68732f2f */
   "\x68""/bin" /* Line 4: pushl $0x6e69622f */
   "\x89\xe3" /* Line 5: movl %esp,%ebx */
   "\x50" /* Line 6: pushl %eax */
   "\x53" /* Line 7: pushl %ebx */
   "\x89\xe1" /* Line 8: movl %esp,%ecx */
   "\x99" /* Line 9: cdq */
   "\xb0\x0b" /* Line 10: movb $0x0b,%al */
   "\xcd\x80" /* Line 11: int $0x80 */
   ;
int main(int argc, char **argv)
{
   char buf[sizeof(code)];
   strcpy(buf, code);
   ((void(*)( ))buf)( );
}

我使用以下方法编译它:

 gcc -z execstack -o call_shellcode call_shellcode.c

 gcc -fno-stack-protector -z execstack -o call_shellcode call_shellcode.c

但我一直在分段错误

另外,我正在运行64位Linux系统(ubuntu)

2 个答案:

答案 0 :(得分:2)

您在x86-64系统上使用32位汇编代码。所以,这是你的问题,你必须为x86-64系统创建shellcode。

E.g。

  400078:   48 31 c0                xor    rax,rax
  40007b:   48 bf 2f 2f 62 69 6e    movabs rdi,0x68732f6e69622f2f
  400082:   2f 73 68 
  400085:   48 31 f6                xor    rsi,rsi
  400088:   56                      push   rsi
  400089:   57                      push   rdi
  40008a:   48 89 e7                mov    rdi,rsp
  40008d:   48 31 d2                xor    rdx,rdx
  400090:   b0 3b                   mov    al,0x3b
  400092:   0f 05                   syscall 

32位汇编的主要区别之一是如何使用系统调用。在此链接Linux Syscalls x86-64中,您可以看到需要调用 sys_execve

的寄存器
  

int execve(const char * filename,char * const argv [],                     char * const envp []);

  • const char * filename - &gt; RDI
  • char * const argv [] - &gt; RSI
  • char * const envp [] - &gt; RDX

E.g。

  #include <stdlib.h>
  #include <stdio.h>
  #include <string.h>

  const char code[] = "\x48\x31\xc0\x48\xbf\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x48\x31\xf6\x56\x57\x48\x89\xe7\x48\x31\xd2\xb0\x3b\x0f\x05";
  int main(int argc, char **argv)
  {
       char buf[sizeof(code)];
       strcpy(buf, code);
       ((void(*)( ))buf)( );
  }

编译并测试它。

$ gcc -fno-stack-protector -z execstack shellcode.c -o shellcode
$ ./shellcode 
$ uname -a
 Linux foobar 4.4.0-97-generic #120-Ubuntu SMP Tue Sep 19 17:28:18 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

答案 1 :(得分:1)

code没有尾随空字节,因此您无法使用strcpy()进行复制。使用memcpy()

memcpy(buf, code, sizeof(code));

如评论中所述,您拥有的shellcode适用于32位Linux,但您尝试在64位系统上运行它。这可能解释了修复此错误后的错误。

相关问题