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

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

void vulnerable_func(int *key_ptr) {
    char buffer[128];
    printf("Current key: 0x%x\n", *key_ptr);
    printf("Key address: %p\n", key_ptr);
    printf("Try to change the last byte to 0x22\n");
    
    printf("Input your string: ");
    fgets(buffer, sizeof(buffer), stdin);
    
    printf("Echo: ");
    printf(buffer);  // Format string vulnerability here
    
    printf("\nKey is now: 0x%x\n", *key_ptr);
    if ((*key_ptr & 0xff) == 0x22) {
        printf("Success! You've modified the last byte to 0x22!\n");
    }
}

int main() {
    init();
    
    int key = 0xaabbccdd;  // Fixed initial value
    printf("Initial key value: 0x%x\n", key);
    
    vulnerable_func(&key);
    
    return 0;
}
