I'm trying to learn binary exploitation, and thought of using online samples to train myself. Here's one that I've found, and I can't see to figure out how to exploit it.
int main(int argc, char** argv[])
{
uint32_t number = 0;
uint32_t guess = 0;
char input[8] = {0};
FILE* devRand = fopen("/dev/urandom", "rb");
if(devRand == NULL)
{
printf("I can't think of a number");
return EXIT_FAILURE;
}
fread(&number, 1, 4, devRand);
fclose(devRand);
printf("What number am I thinking of?\n");
fflush(stdout);
bool correct = false;
do
{
fgets(&input[0], 28, stdin);
guess = strtol(&input[0], NULL, 16);
if (number == guess)
{
correct = true;
printf("Yes!\n");
fflush(stdout);
system("/bin/sh");
}
else
{
printf("No\n");
fflush(stdout);
}
}
while(!correct);
return EXIT_SUCCESS;
}
It would be appreciated if someone can give me a detailed explanation of the exploit, how it works, and how it can be mitigated.
Thanks!