ret2libc

ret2libc

ret2libc 这种攻击方式主要是针对 动态链接(Dynamic linking) 编译的程序,通过控制执行libc中的函数(通常为‘sysytem(“/bin/sh”)’)。

通常分为:

  1. 程序中自身就含有system函数和“/bin/sh”字符串
  2. 程序中自身就有system函数,但是没有“/bin/sh”字符串
  3. 程序中自身就没有system函数和“/bin/sh”字符串,但给出了libc.so文件
  4. 程序中自身就没有system函数和“/bin/sh”字符串,并且没有给出libc.so文件

解题思路

劫持binary执行system(/bin/sh),一般有一个溢出点需要两次劫持

一是泄露函数地址:控制eip指向,寻找output函数(如puts和write)和待泄露函数

二是控制binary返回到libc执行sysem(/bin/sh)

例一:ctfwiki-ret2libc1

题目地址

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char *shell = "/bin/sh";
char buf2[100];

void secure(void)
{
    int secretcode, input;
    srand(time(NULL));

    secretcode = rand();
    scanf("%d", &input);
    if(input == secretcode)
        system("shell!?");
}

int main(void)
{
    setvbuf(stdout, 0LL, 2, 0LL);
    setvbuf(stdin, 0LL, 1, 0LL);

    char buf1[100];

    printf("RET2LIBC >_<\\n");
    gets(buf1);

    return 0;
}

shift+f12,发现system和/bin/sh,与ret2text不同,这里system中并不是/bin/sh

shell!?并不是一个真正的shell命令,所以我们需要将其替换为/bin/sh