I would love if someone could explain me exactly what the code does. I know there's a buffer overflow and bash command execution vulnerability - but since I'm a network guy and not a programmer, I really could use some help to understand the entire code. Thanks in advance!
int main () {
int status;
char t[1024]="ps -eo lstart,cmd | grep ";
cout << "Content-type:text/html\r\n\r\n"<<endl;
char *value = getenv("QUERY_STRING");
strcat(t,value);
status = system(strcat(t," | grep -v grep | head -n 1 | awk '{ print $1\" \"$3\" \"$2\" \"$5\" \"$4}'"));
return 0;
}
tl;dr: This is what your code does, as a shell script:
Now for the longer answer.
Rewriting the code
First, let's make that thing into C++ rather than C (like your tag suggests you're asking about) with a bit of error handling, then talk about what's going on:
What are we doing here?
So, we're creating a command-line here which we then execute using the
system()
function. It's an invocation of aps
command with some switches, followed by some text processing withgrep
,head
andawk
- using the pipe mechanism to move the output of each command to the next. They key part is that we use the environment variableQUERY_STRING
to filter theps
results, i.e. we list processes which match some phrase. If we compile this program, set the environment variable and run, this is what it looks like:What this has given us is the start time of the first process whose command-line doesn't include the phrase "init". So now you can guess my system has been up since yesterday...
Finally, as a network guy, you probably realize the "Content-type" mumbo-jumbo and the double-newline is a MIME header, so this output is probably intended to be used as an HTTP response. Probably this is intended as some sort of CGI script.
Security vulnerabilities
The second vulnerability has to do with the
system
command. We're injecting an arbitrary string into the string we're creating; and there's nothing preventing someone from settingin which case you would run:
and this would delete everything under the effective user's home directory. Or it could be any command, including compilation of a custom C/C++ program to run some arbitrary code on your system. Very bad.