So i was trying to do the Stripe CTF contest, but I know absolutely nothing about security, so I looked up problem 3 when i got stuck on it. I still don’t understand how it works. The goal of the hack is to access the password in a file of a different user, by using an application with the SUID bit set. Here is the (simplified) code of the application:
#define NUM_FNS 4
typedef int (*fn_ptr)(const char *);
int to_upper(const char *str)
int to_lower(const char *str)
int capitalize(const char *str)
int length(const char *str)
int run(const char *str)
{
// This function is now deprecated.
return system(str);
}
int truncate_and_call(fn_ptr *fns, int index, char *user_string)
{
char buf[64];
// Truncate supplied string
strncpy(buf, user_string, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
return fns[index](buf);
}
int main(int argc, char **argv)
{
int index;
fn_ptr fns[NUM_FNS] = {&to_upper, &to_lower, &capitalize, &length};
if (argc != 3) {
exit(-1);
}
// Parse supplied index
index = atoi(argv[1]);
if (index >= NUM_FNS) {
exit(-1);
}
return truncate_and_call(fns, index, argv[2]);
}
And here is a solution i found: http://pastebin.com/VJ4xpawq
I’m so confused as to why this works. If i run the code with something like ./level03 -28 “echo foo;” I get a segfault. Also, why is the memory address reversed in his printf function????
I’m lost, and would like to learn. Thank you in advance. 🙂
The goal of this code is to execute
since the executable has a UID of ‘level04’, the shell it spawned will also have a UID of ‘level04’.
This can be done by running the “deprecated”
runfunction:We notice that in the function
truncate_and_call, we will call a function, which is selected by user input:So, we try to create a memory location and make up an
index, such thatfns[index] == &run.The
indexis bounds-checked bywhich means the malicious
indexwe provide must be less than 4 — but it could be negative! Therefore, our goal becomes:fnswhich is writable&runinto itbufas something which starts the shell.To check the addresses, we run the program inside
gdband break attruncate_and_call:Notice that there is also a local variable
bufhere, which:has an address before
fns. So step 1 is done. We only need to check theindex, which isSo the next question is how to write the memory location of
&runintobuf. This is easy, becausebufis just astrcpyofuser_string, the second argument of the function. Check that the address ofrunisIn a little-endian system, this address is encoded as the string
"\xAC\x84\x04\x08". This string can be obtained from the shell using theprintfcommand, or$'...':So, the last step is to make it start the shell. Because if we assign
"\xac\x84\x04\x08"tobuf, what is actually called isbut we wanted
"/bin/sh", not"\xac\x84\x04\x08"! This could be easily worked around by linking/bin/shto a file with name"\xac\x84\x04\x08", and add the directory of that file to$PATH:The whole solution is therefore:
(Note: the numbers are a bit different, because I run them on my machine instead of Stripe’s.)
Also, you get a segfault in
./level03 -28 "echo foo;"because it will interpret the address to run as0x6f686365(the ASCII codes of the 4 bytes ‘echo’), which is an invalid address.