Title here
Summary here

August 10, 20252 minutes
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fptr;
char c;
char input[16];
int num = 64;
printf("Enter a string: ");
fflush(stdout);
gets(input);
printf("\n");
printf("num is %d\n", num);
fflush(stdout);
if( num == 65 ){
printf("You win!\n");
fflush(stdout);
// Open file
fptr = fopen("flag.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file.\n");
fflush(stdout);
exit(0);
}
// Read contents from file
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fflush(stdout);
printf("\n");
fflush(stdout);
fclose(fptr);
exit(0);
}
printf("Bye!\n");
fflush(stdout);
}
Hay un claro buffer overflow, ya que se usa la función gets, y luego hay un int que se inicializa con el valor 64, y si se consigue cambiar a 65 el programa da la flag. El objetivo es desde input hacer un overflow y llegar a sobreescribir num
Probé mandando 17 “A” y poniendo un breakpoint justo donde se ejecuta el gets()
pwndbg> b *0x0000000000401275
Breakpoint 1 at 0x401275
pwndbg>
Desde pwndbg vi esta instrucción
0x4012a9 <main+115> cmp dword ptr [rbp - 8], 0x41 0x40 - 0x41 EFLAGS => 0x297 [ CF PF AF zf SF IF df of ac ]
Se está comparando los primeros 32 bits de rbp-8 con 0x41, esta instrucción equivaldría en c a:
if( num == 65 ){
Así que el valor que hay que modificar está en rbp-8

Desde el telescope se puede calcular que hay que poner 25 “A”

El 0x40 (64) ha cambiado a 0x41 (65)!