[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void show_ids(void)
{
printf ("real uid: %d\n", getuid());
printf ("effective uid: %d\n", geteuid());
}
int main(int argc, char *argv[])
{
int uid;
show_ids();
uid = atoi(argv[1]);
if (setuid (uid) < 0)
perror ("setuid error");
show_ids();
return (0);
}
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
void show_ids (void)
{
printf ("The real user ID is: %d\n", getuid());
printf ("The effective user ID is :%d\n", geteuid());
}
int main(void)
{
const char *file = "/root/rootfile3.txt";
show_ids();
if (!unlink (file)) {
printf ("Ok, I am root, and I can delete the file which in /root directory.\n");
system ("echo hello world > /root/rootfile3.txt");
printf ("Now, drop the root privileges.\n");
if (setuid (1000) < 0) {
perror ("setuid");
exit (EXIT_FAILURE);
}
show_ids();
if (unlink (file) < 0) {
printf ("Ok, we have no privilege to delete rootfile.txt.\n");
}
printf ("try to regain root power again...\n");
if (seteuid (0)) {
perror ("seteuid");
show_ids();
exit (EXIT_FAILURE);
}
}