Skip to content

Instantly share code, notes, and snippets.

@jachermocilla
Created August 19, 2018 12:25
Show Gist options
  • Save jachermocilla/c06916cbedd7f391fb5a5955a8d45208 to your computer and use it in GitHub Desktop.
Save jachermocilla/c06916cbedd7f391fb5a5955a8d45208 to your computer and use it in GitHub Desktop.
/*
* setuid example by [email protected]
*
* $gcc -o mysuid.exe mysuid.c
* $sudo chown root.root mysuid.exe
* $sudo chmod 4755 mysuid.exe
*
* Note: must be run in a filesystem mounted with no 'nosuid'
* ex: /tmp
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
uid_t current_uid;
printf("RUID=%d, EUID=%d, GID=%d\n", getuid(), geteuid(), getgid());
system("cat /etc/shadow");
if (setuid(0)){
perror("setuid");
return 1;
}
printf("RUID=%d, EUID=%d, GID=%d\n", getuid(), geteuid(), getgid());
system("cat /etc/shadow");
setuid(current_uid);
printf("RUID=%d, EUID=%d, GID=%d\n", getuid(), geteuid(), getgid());
system("cat /etc/shadow");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment