I'm trying to use buffer overflow to overwrite two local variables, so that I can call the hidden function. Here is the C code.
#include <stdio.h>
#include <stdlib.h>
static void hidden_function(void)
{
puts("I laugh in the face of danger. Ha ha ha ha!");
}
static void visible_function(void)
{
puts("Knock, knock! Who's there? Recursion. Recursion who? Knock, knock!");
}
static void helper_function(void)
{
void (*f_ptr)(void) = visible_function;
unsigned int dumb_number = 0x12345678;
char buffer[32];
printf("Provide buffer input: ");
fgets(buffer, 64, stdin);
printf("Dumb number value is 0x%08x.\n", dumb_number);
printf("Buffer is %s\n", buffer);
f_ptr();
}
int main(void)
{
helper_function();
return 0;
}
This is the Makefile that I use.
CC = gcc
CFLAGS = -m32 -Wall -Wextra -Wno-unused-function -g -O0 -fno-stack-protector -no-pie
LDFLAGS = -m32
.PHONY: all clean
all: overflow_ptr
overflow_ptr: overflow_ptr.o
$(CC) $(CFLAGS) -o $@ $<
overflow_ptr.o: overflow_ptr.c
clean:
-rm -f overflow_ptr.o overflow_ptr
-rm -f *~
Running nm overflow_ptr shows me that the address of the hidden function is the following:
080484a6 t hidden_function
So I created the following payload:
python3 -c 'print(32*"A" + "\x21\x43\x65\x87" + "\xa6\x84\x04\x08")'
This is supposed to make dump_number = 0x87654321 and f_ptr = 0x080484a6. However, when I run this program the output is:
Provide buffer input: Dumb number value is 0xc2654321.
Which makes me wonder why was that c2 inserted? I'm assumming it's some kind of protection measure. If so, is there any way to prevent it? I'm using a 64-bit virtual machine with Ubuntu.
python3 -c 'print(32*"A" + "\x21\x43\x65\x87" + "\xa6\x84\x04\x08")' | od -xthe values do not correspond. But it seems like a python problem. Maybe you can try to fix it or add the python tag.