【问题标题】:Why this little shellcode works in a C program,but not alone?为什么这个小 shellcode 在 C 程序中工作,但不是单独的?
【发布时间】:2019-12-20 14:38:47
【问题描述】:

这个shellcode在组装后不起作用

 Section    .text

    global _start

_start:

    jmp       GotoCall

shellcode:

     pop             edi               
     xor             eax, eax          
     mov byte        [edi + 7], al     
     lea             ebx, [edi]        
     mov long        [edi + 8], ebx    
     mov long        [edi + 12], eax   
     mov byte        al, 0x0b         
     mov             ebx, edi      
     lea             ecx, [edi + 8]    
     lea             edx, [edi + 12]   
     int             0x80

GotoCall:

     Call             shellcode
     db              '/bin/shJAAAAKKKK'

这个小 shellcode 可以在这个名为“Shellcode tester”的 C 程序中工作。

#shellcode tester

char shellcode[] = "\xe9\x1a\x00\x00\x00\x5f\x31\xc0\x88\x47\x07\x8d\x1f\x89\x5f\x08\x89\x47\x0c\xb0\x0b\x89\xfb\x8d\x4f\x08\x8d\x57\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x4a\x41\x41\x41\x41\x4b\x4b\x4b\x4b";               

int main(int argc, char *argv[])
{
    int (*ret)();              /* ret is a function pointer */
        ret = (int(*)())shellcode; /* ret points to our shellcode */
                                   /* shellcode is type caste as a function */
        (int)(*ret)();             /* execute, as a function, shellcode[] */
        exit(0);                   /* exit() */
}

但是,当我组装和链接它时,它不起作用,究竟是为什么?
当我用 GDB 调试它时,问题是这一行中的 EDI RegisterEDI 正好指向堆栈中存在的字符串,我的意思是ascii 中的字节数。

mov byte        [edi + 7], al

这个shellcode在组装后确实可以工作
我发现的另一个shellcode是这个

Section .text

global _start

_start:

    jmp    GotoCall

shellcode:

 xor eax, eax     ;zero out eax
 push eax         ;push 00000000 on to the stack
 push 'n/sh'      ;push hex //bin/sh on to the stack
 push '//bi'
                  ;at this point the stack contains //bin/sh0x00000000
 mov ebx, esp     ;this satisfies the requirements for *filename (first argument 
                   of execve)
 push eax         ;push 00000000 on to the stack
                  ;at this point the stack contains 0x00000000//bin/sh0x00000000
 mov edx, esp
 push ebx         ;ebx contains the memory address of the stack where 
                   //bin/sh0x00000000 is.
 mov ecx, esp     ;this satisfies the requirements for argv (second argument of 
                     execve)
 mov al, 11       ;execve syscall number, 0xb works also.
 int 0x80         ;initiate


GotoCall:

     Call             shellcode

这很有趣,因为当我将操作码放在那里时,这个 shellcode 程序完全可以与 C 中的“Shellcode Test”程序一起工作,并且也完全可以单独工作

请告诉我为什么第一个“shellcode”不能单独工作而第二个可以?

【问题讨论】:

  • 您能详细说明“不起作用”是什么意思吗?
  • @ikegami 对不起,我不明白你的意思。
  • @AjayBrahmakshatriya 如果您从“objdump -d 文件”获取操作码并将其放在 C shellcode 数组中,它会给您一个 shell。但是当您组装、链接并运行它时,它会赢'不要给你一个外壳,它会因“分段错误”错误而失败。
  • @ikegami 究竟是哪个代码?以及什么功能?
  • 听起来你已经明白了:它是 EDI 寄存器。有什么问题吗?

标签: c assembly callstack shellcode


【解决方案1】:

char shellcode[] 定义了一个可变静态数组。

db 定义程序的.text 部分中的存储。在 GNU/Linux 上,这是不可写的;程序文本被映射到标记为只读的虚拟内存页面中。

解决方法是将空字节粘贴到 db 定义中,而不是尝试在运行时将其放入其中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    相关资源
    最近更新 更多