This project should be performed on machines in the CSE Linux Lab: cse-p204instXX.cse.psu.edu, which XX is a 2-digit number from 01 to at least 40. We will test your results on those machines.
We continue our study of the lucky program in the last homework. For the convenience of performing a format string attack, we have modified the program slightly, as shown below.
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
char Password[100]; // Memory storage for the password
char goodPassword() {
int good='N';
int *p = &good;
fgets(Password, sizeof(Password), stdin); // Get input from keyboard
printf("Password=");
printf(Password);
printf("\n");
return (char)(*p);
}
int main() {
struct timeval t;
printf("Enter your password:");
char r = goodPassword();
printf("r=%c\n", r);
if (r == 'Y') {
gettimeofday(&t, 0);
srand((unsigned int) t.tv_usec);
printf("Your lucky number today is %d!\n", rand()%100);
}
else {
printf("No lucky number for you today.\n");
exit(-1);
}
return 0;
}
Similar to the previous version, the program refuses to generate
a lucky number. What is different is that it uses fgets to get
the user password instead of the insecure gets. The fgets
function takes the size of the Password buffer as the bound of
the number of characters to read (more accurately, if the
buffer size is n, fgets will read at most n-1 characters and
will terminate the buffer with a null byte). Therefore, there is no
possibility of overflowing the Password buffer through the fgets
function.
However, there is a format-string vulnerability in the program. The program tries to echo the password in the goodPassword function, but it uses printf in an insecure way. We'd like to take advantage of this vulnerability so that the program generates lucky numbers for us.
Do this homework in the following steps:
For this project, you may not have to use gdb; manually inserting printf into the program might be sufficient to figure out which stack slot holds variable p and other information.
Author: Gang Tan, Penn State University, 2019. All rights reserved.