I just started doing CTFs not so long, and get struck with this pwn challenge. Here's the code:
#include<stdio.h>
#include<string.h>
void validAnswer(char *str){
printf("Congratulations!\n");
}
void wrongAnswer(char *str){
printf("Unfortunately!\n");
printf(str);
printf("is not the correct answer");
}
int main() {
char flag[40] = "This should be flag";
printf("What is the answer of 1+1\n");
char answer[64] = "";
fgets(answer, 64, stdin);
if(strcmp(answer, "2\n") == 0){
validAnswer(answer);
}else{
wrongAnswer(answer);
}
}
main();
I think I should overflow the fgets and call the flag, but cannot go so far because of strcmp prevent it. How can I escape it?