标签:
$sudo su
Password:
#cd /bin
#rm sh
#ln -s zsh sh
将sh链接到bin/zsh
int main()
{
system("ls");
return 0;
}
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *v[3];
if(argc < 2)
{
printf("Please type a file name.\n");
return 1;
}
v[0] = "/bin/cat"; v[1] = argv[1]; v[2] = 0;
//Set q = 0 for Question a, and q = 1 for Question b
int q = 0;
if (q == 0)
{
char *command = malloc(strlen(v[0]) + strlen(v[1]) + 2);
sprintf(command, "%s %s", v[0], v[1]);
system(command);
}
else execve(v[0], v, 0);
return 0 ;
}
#include <stdio.h>
void sleep (int s)
{
printf("I am not sleeping!\n");
}
gcc -fPIC -g -c mylib.c
gcc -shared -Wl,-soname,libmylib.so.1 -o libmylib.so.1.0.1 mylib.o –lc
int main()
{
sleep(1);
return 0;
}
由以上四种情况可见:只有用户自己创建的程序自己去运行,才会使用LD_PRELOAD环境变量,重载sleep函数,否则的话忽略LD_PRELOAD环境变量,不会重载sleep函数。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void main()
{
int fd;
//Assume that /tmp/zzz is an important system file,
//and it is owned by root with permission 0644
fd = open("/tmp/zzz", O_RDWR | O_APPEND);
// Simulate the tasks conducted by the program
sleep(1);
// After the task, the root privileges are no longer needed,
//it’s time to relinquish the root privileges permanently.
setuid(getuid()); // getuid() returns the real uid
if (fork())
{ // In the parent process
close (fd);
exit(0);
}
else
{ // in the child process
//Now, assume that the child process is compromised, malicious
//attackers have injected the following statements
//into this process
write (fd, "shiyanlou!", 10);
close (fd);
}
}
如图所示文件被修改了,原因在于设置uid前,zzz文件就已经被打开了。只要将语句setuid(getuid())移至调用open函数之前,就能避免这个问题。
3.总结:
本次实验将近做了5个小时左右才做完,之所以费了这么多时间是因为网速太慢,总是刷新,还有我自己也不太会做,所以边上网查边做。标签:
原文地址:http://www.cnblogs.com/wangba/p/4485740.html