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

void init() {
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);
    setvbuf(stdin, NULL, _IONBF, 0);
}

int generate_key() {
    // Generate a "random" key for demonstration
    srand(time(NULL));
    return rand() ^ 0xdeadbeef;
}

void vulnerable_func(int secret_key) {
    char buffer[128];
    printf("Try to leak the key!\n");
    printf("Input your string: ");
    fgets(buffer, sizeof(buffer), stdin);
    
    printf("Echo: ");
    printf(buffer);  // Format string vulnerability here
}

int main() {
    init();
    
    int secret_key = generate_key();
    printf("Key address: %p\n", &secret_key);
    
    vulnerable_func(secret_key);
    
    // Check if leak was successful
    printf("\nReal key value: 0x%x\n", secret_key);
    
    return 0;
}
