Question: Buffer Overflow Your task is to exploit a buffer - overflow in function f of code provided to you in file ex 1 . c

Buffer Overflow
Your task is to exploit a buffer-overflow in function f of code provided to you in file ex1.c (use the
Makefile to compile it). You are not allowed to change the source code. The program receives three
arguments: your first and last name and the name of a file. The function f reads the content of that file
but doesn't check if there is enough space for all contents of the file.
Your task is to provide a file with proper content such that after running the executable you will be
dropped into a new shell instance (and the final message "This message should not be printed" is not
printed). Moreover, the program must display your name twice. For example, the following is an example
of an intended behavior:
The size of the buffer is dependent on the lengths of your names and the values of the initial letters of
both. Thus, each of you will have to produce a different file and each file produced will match a single
name.
Hint: Because the buffer in the function is allocated via alloca, you will have to also keep the address
of the buffer on the stack even after the attack. Use the address returned by the executable for this.
However, you don't need to insert the buffer's address exactly but an address at a constant offset (which
depends on the numbers m and n ).
To debug from GDB, run "set architecture i386" after launching it.
(PLEASE SOLVE AND SHOW STEPS FROM TERMINAL)
ex1.c:
#include
#include
#include
void f(int n, int m, char *str)
{
FILE *f = fopen(str,"r");
char buf[n];
size_t pos;
if (m >= n){
fprintf(stderr, "Invalid call of f
");
exit(EXIT_FAILURE);
}
if (f == NULL){
perror("Invalid file with buffer");
exit(EXIT_FAILURE);
}
printf("Reading file into buffer at %p
", buf);
fseek(f,0, SEEK_END);
pos = ftell(f);
fseek(f,0, SEEK_SET);
/* here's the bug: pos should be at most n for legit calls */
fread(buf, sizeof(buf[0]), pos, f);
buf[m]=0;
printf("Your name is: %s
", buf);
printf("First 32 hex values read from file (A=0x41, a=0x61):");
for (pos =0; pos 32; pos++){
if (pos %16==0)
printf("
\t");
printf("0x%02x ",(unsigned char)buf[pos]);
}
printf("
");
}
int main(int argc, char **argv)
{
int m, n;
if (argc !=4){
fprintf(stderr, "Usage: ./ex1 firstname lastname filename
");
exit(EXIT_FAILURE);
}
m =10*(argv[1][0]-'A'+1)+(argv[2][0]-'A'+1);
if (m 0) m =-m;
n = strlen(argv[1])+ strlen(argv[2])+1;
if (m 42* n +42) m +=42* n +42;
if (m >1024) m =1024;
if (n >50) n =50;
fprintf(stdout, "Numbers (m, n): %d %d
", m, n);
fprintf(stdout, "Provided name: %s %s
", argv[1], argv[2]);
f(m, n, argv[3]);
printf("This message should not be printed
");
return 0;
}
makefile:
.PHONY: all clean md5
TARGET =./ex1
CC = gcc
CFLAGS =-Wall -Wextra -g -fno-stack-protector -m32-z execstack
LDFLAGS =-fno-stack-protector -m32-z execstack
all: md5
md5: $(TARGET)
md5sum $(TARGET)
clean:
$(RM) $(TARGET)
Buffer Overflow Your task is to exploit a buffer

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!