2017-04-12 27 views
0

是否有可能在不編寫代碼的情況下構建一個shellcode?構造一個shellcode而不會搞亂程序集

我試圖編譯下面的簡單應用:

#include <stdlib.h> 

int main(int argc, char** argv) 
{ 
     char* args[] = { "/bin/bash", NULL }; 
     execve("bin/bash", &args, NULL); 
     return 0; 
} 

然後我編譯它,並用gdb得到如下:

(gdb) x/15i main 
    0x400506 <main>:  push %rbp 
    0x400507 <main+1>: mov %rsp,%rbp 
=> 0x40050a <main+4>: sub $0x20,%rsp 
    0x40050e <main+8>: mov %edi,-0x14(%rbp) 
    0x400511 <main+11>: mov %rsi,-0x20(%rbp) 
    0x400515 <main+15>: movq $0x4005d4,-0x10(%rbp) 
    0x40051d <main+23>: movq $0x0,-0x8(%rbp) 
    0x400525 <main+31>: lea -0x10(%rbp),%rax 
    0x400529 <main+35>: mov $0x0,%edx 
    0x40052e <main+40>: mov %rax,%rsi 
    0x400531 <main+43>: mov $0x4005de,%edi 
    0x400536 <main+48>: callq 0x4003f0 <[email protected]> 
    0x40053b <main+53>: mov $0x0,%eax 
    0x400540 <main+58>: leaveq 
    0x400541 <main+59>: retq 

可惜我不能用它來生成的shellcode,因爲有一個使用全局偏移表[main + 48]。

我該如何修復這種方法來生成shellcode而不用返回程序集。

+0

我不認爲你可以讓編譯器產生的代碼可以直接用作shell代碼。 – fuz

回答

1

你不能。

C編譯器不知道如何直接生成系統調用。它知道如何做的是調用函數 - 它使用GOT。

(您可以解決此通過使用內聯彙編,但是這仍然是「用匯編寫代碼」,你說過你希望避免的。)

除此之外,你還發現, C編譯器將生成偶然包含空字節的程序集。這在許多應用程序中將失敗。

瞭解如何編寫程序集或使用其他人的shellcode。

0

下面是使用/ bin/sh和堆棧方法使用execve打開shell的示例。

我目前正在通過Security Tube的Linux程序集64位課程以及閱讀Hacking,開發藝術Jon Erickson;它使用x86彙編。

; David @InfinitelyManic 
; execute_shellcode_stack.s 
; nasm -felf64 -g -F dwarf execute_shellcode_stack.s -o execute_shellcode_stack.o && ld execute_shellcode_stack.o -o execute_shellcode_stack 
; code from course: http://www.securitytube-training.com/online-courses/x8664-assembly-and-shellcoding-on-linux/ 
; 
section .bss 
section .data 
section .text 
     global _start 
_start: 
     ; <syscall name="execve" number="59"/> 
     ; int execve(const char *filename, char *const argv[], char *const envp[]); 
     ; x86_64     rdi     rsi     rdx  r10 r8 r9 

     xor rax, rax       ; null 
     push rax        ; push 0x00 

     ; we need /bin//sh in hex in reverse 
     ; rev <<< "/bin//sh" | xxd -g4 
     ; 00000000: 68732f2f 6e69622f 0a     hs//nib/. 

     mov rbx, 0x68732f2f6e69622f  ; hs//nib/ ascii 

     push rbx      ; push '/bin//sh' to stack 

     mov rdi, rsp     ; _filename = '/bin//sh'0000 

     push rax      ; second null 0x00 "" 

     mov rdx, rsp     ; envp = array of strings == null 

     push rdi      ; push _filename 

     mov rsi, rsp     ; argv = array of argument strings = _filename 

     add rax, 59      ; syscall # for execve 

     syscall 

objdump的:

execute_shellcode_stack:  file format elf64-x86-64 


Disassembly of section .text: 

0000000000400080 <_start>: 
    400080:  48 31 c0    xor rax,rax 
    400083:  50      push rax 
    400084:  48 bb 2f 62 69 6e 2f movabs rbx,0x68732f2f6e69622f 
    40008b:  2f 73 68 
    40008e:  53      push rbx 
    40008f:  48 89 e7    mov rdi,rsp 
    400092:  50      push rax 
    400093:  48 89 e2    mov rdx,rsp 
    400096:  57      push rdi 
    400097:  48 89 e6    mov rsi,rsp 
    40009a:  48 83 c0 3b    add rax,0x3b 
    40009e:  0f 05     syscall 

生成對C前衛的shellcode:

$ for i in $(objdump -d execute_shellcode_stack | grep "^ " |cut -f2); do echo -n "\x"$i; done; echo 
\x48\x31\xc0\x50\x48\xbb\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x53\x48\x89\xe7\x50\x48\x89\xe2\x57\x48\x89\xe6\x48\x83\xc0\x3b\x0f\x05 

輸出:

$ ./shellcode 
Shellcode Length: 32 
$ 
+0

也許你錯過了它,但問的問題是「_是否有可能構建一個shellcode而無需在程序集中編寫代碼?_」。您的答案只使用匯編語言。 –

+0

是的,我意識到這個問題,並且我的答案不是四肢。 OP和其他人可以選擇不允許答案或將其標出。只需在「學習如何編寫程序集或使用其他人的shellcode」中提供一些幫助。 – InfinitelyManic