#include <stdio.h>
int main(void){
int len;
char input[40]="";
printf("input length : \n");
scanf("%d", &len);
if(len > 40){
return 0;
}
read(0, input, len);
printf("%s", input);
return 0;
}
This code is vulnerable to a buffer overflow attack, and I am trying to figure out why. I tried a lot, but every attack code is failed to bypass 'if' statements.
How do I exploit this code?
inputis not a nul-terminated string. You initialize as all 0 bytes, but if you read 40 bytes from the file, the nul-terminator is replaced with whatever comes from the file. But I wouldn't call that a buffer overflow.read? I'm guessing the standard libc function, but then the appropriate header file appears to be missing (even if stdio.h includes e.g. unistd.h, it's not clear wherereadcomes from).scanf. If I give invalid input, such asa, then program behave randomly becauselenis not initialized and will have random value.