table = table_func; scanf("%s", ptr); if( !over->table ){ return 0; } over->table(); return 0; }"> table = table_func; scanf("%s", ptr); if( !over->table ){ return 0; } over->table(); return 0; }"> table = table_func; scanf("%s", ptr); if( !over->table ){ return 0; } over->table(); return 0; }">
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

struct over {
    void (*table)();
};

void alarm_handler() {
    puts("TIME OUT");
    exit(-1);
}

void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);

    signal(SIGALRM, alarm_handler);
    alarm(30);
}

void get_shell() {
    system("/bin/sh");
}

void table_func() {
    printf("overwrite_me!");
}

int main() {
    char *ptr = malloc(0x20);

    struct over *over = malloc(0x20);

    initialize();

    over->table = table_func;

    scanf("%s", ptr);

    if( !over->table ){
        return 0;
    }

    over->table();
    return 0;
}

문제에 소스코드는 위와 같다.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

struct over {
    void (*table)();
};

void alarm_handler() {
    puts("TIME OUT");
    exit(-1);
}

void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);

    signal(SIGALRM, alarm_handler);
    alarm(30);
}

void get_shell() {
    system("/bin/sh");
}

void table_func() {
    printf("overwrite_me!");
}

int main() {
    char *ptr = malloc(0x20);

    struct over *over = malloc(0x20);

    initialize();

    over->table = table_func;

    scanf("%s", ptr);

    if( !over->table ){
        return 0;
    }

    over->table();
    return 0;
}

취약점 분석

struct over {
    void (*table)();
};
    char *ptr = malloc(0x20);
    struct over *over = malloc(0x20);
    initialize();
    over->table = table_func;

		scanf("%s", ptr);

scanf를 통해 OverFlow가 발생한다.

ptr값을 통해 Over영역에서 OverFlow 또한 발생할 수 있기때문에 이는 Heap-Over-Flow 취약점 발생으로 이어진다.

Over에 할당되는 0x20에 추가로 Overwrite하면 희망하는 주소로 이동할 수 있다.