This project should be performed on machines in the CSE Linux Lab: e5-cse-135-XX.cse.psu.edu, where XX is a number between 01 and about 28. You can either use those machines by going to the CSE Linux lab, or you can access them remotely. For remote access, you will need to first vpn into the PSU network before being able to access those machines. 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';
char *Prompt="Password=";
int *p = &good;
fgets(Password, sizeof(Password), stdin); // Get input from keyboard
printf(Prompt);
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:
Author: G. Gary Tan, Penn State University, 2026. All rights reserved.