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

/* PACISE26 CTF - Stack Overflow (modified Lab2)
 * Exploit it by overflowing array[] to overwrite the saved return address
 * so execution jumps to hacked(), which will read and print flag.txt.
 */

void hacked()
{
    char flag[256];
    FILE *f = fopen("flag.txt", "r");
    if (!f) {
        puts("hacked(), but flag.txt is missing :(");
        fflush(stdout);
        exit(1);
    }
    if (!fgets(flag, sizeof(flag), f)) {
        puts("hacked(), but flag.txt is empty :(");
        fclose(f);
        fflush(stdout);
        exit(1);
    }
    fclose(f);
    printf("Hacked!! Here is your flag: %s\n", flag);
    fflush(stdout);
    exit(0);
}

void return_input(void)
{
    char array[50];
    gets(array);
    printf("You said: %s\n", array);
    fflush(stdout);
}

int main(void)
{
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stdin,  NULL, _IONBF, 0);
    puts("=== PACISE26 CTF - Stack Overflow ===");
    puts("Enter your input: ");
    fflush(stdout);
    return_input();
    puts("Bye!");
    return 0;
}
