So I hope I'm phrasing this right. I'm trying to exploit a piece of c code which you can see below.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int user_age;
void secretClub(){
if(user_age<18){
printf("Come back when your old enough!!");
}
else{
printf("Come on in.");
}
}
int main(){
char name[30];
user_age = 17;
gets(name);
printf("Hello there ");
printf(name);
}
What I'm trying to do here is call the secretClub function and to print "Come on in". So I know if I wanted to just call the secretClub function, I could just overflow the buffer enough with the memory address of the function at the end. And I know that I can use this programs string format vulnerability to modify the variable's value in memory.
What I'm wondering is how to do both in one line? Sorry if this seems like a stupid and obvious question, upon searching I couldn't find much. Any guidance would be greatly appreciated!
printf
on an arbitrary string or write out of bounds of a buffer using a format string. In this case, this hardly seems necessary though, unless you need to write0xa
(\n
) bytes onto the stack, which is not possible with justgets
. In that case, you could abuse the format string vulnerability later to write those0xa
bytes.printf("Come on in.");
line in code?user_age
can be set with just the buffer overflow depends on the stack layout imposed by the compiler. This is implementation-specific behaviour. Finally, using the buffer overflow to get arbitrary code execution may be possible using shellocde or ROP, depending on mitigations and ROP gadgets.pwntools
is popular) so you can always do everything in one line withpython exploit.py