lab 5 - archie-archana/securecodinglab GitHub Wiki
1.. Figure out why "passwd", "chsh", "su", and "sudo" commands need to be Set-UID programs. What will happen if they are not? If you are not familiar with these programs, you should first learn what they can do by reading their manuals. Please copy these commands to your own directory; the copies will not be Set-UID programs. Run the copied programs, and observe what happens.
//We find that when copying passwd to /tmp/,it lost root's privileges. As for chsh, su and sudo, they are the same.
a. Run Set-UID shell programs in Linux, and describe and explain your observations. (a) Login as root, copy /bin/zsh to /rmp, and make it a set-root-uid program with permission 4755. Then login as a normal user, and run /tmp/zsh. Will you get root privilege? Please describe your observation.
So now normal user get root privilege.
2b.b) Instead of copying /bin/zsh, this time, copy /bin/bash to /tmp, make it a set-root-uid program. Run /tmp/bash as a normal user. will you get root privilege? Please describe and explain your observation.
Since we do the same operating, zsh can get root privilege, but bash can't.
3.(Setup for the rest of the tasks) As you can find out from the previous task, /bin/bash has certain built-in protection that prevent the abuse of the Set-UID mechanism. To see the life before such a protection scheme was implemented, we are going to use a different shell program called /bin/zsh. In some Linux distributions(such as Fedora and Ubuntu), /bin/sh is actually a symbolic link to /bin/bash. To use zsh, we need to link /bin/sh to /bin/zsh. The following instructions describe how to change the default shell to zsh.
4.. The PATH environment variable. The system(const char *cmd) library function can be used to execute a command within a program. The way system(cmd) works is to invoke the /bin/sh program, and then let the shell program to execute cmd. Because of the shell program invoked, calling system() within a Set-UID program is extremely dangerous. This is because the actual behavior of the shell program can be affected by environment variables, such as PATH; these environment variables are under user's control. By changing these variables, malicious users can control the behavior of the Set-UID program. The Set-UID program below is supposed to execute the /bin/ls command; however, the programmer only uses the relative path for the ls command, rather than the absolute path:
a.Can you let this Set-UID program(owned by root) run your code instead of /bin/ls? If you can, is your code running with the root privilege? Describe and explain your observations.
It can have root previlege, copy /bin/sh to /tmp with new name ls.(make sure sh -> zsh). Then set PATH to current directory /tmp, compile and run system program and we will get root previlege.
b.Now, change /bin/sh so it points back to /bin/bash, and repeat the above attack. Can you still get the root privilege? Describe and explain your observations.
We can't get root privilege.
5.The difference between system() and execve(). Before you work on this task, please make sure that /bin/sh is point to /bin/zsh. Background: Bob works for an auditing agency, and he needs to investigete a company for a suspected fraud. For the investigation purpose, Bob needs to be able to read all the files in the company's Unix system; on the other hand, to protect the integrity of the system, Bob should not be able to modify any file. To achieve this goal, Vince, the superuser of the system, wrote a special set-root-uid program(see below), and then gave the executable permission to Bob. This program requires Bob to type a file name at the command line, and then it will run /bin/cat to display the specified file. Since the program is running as a root, it can display any file Bob specifies. However, since the program has no write operations, Vince is very sure that Bob cannot use this special program to modify any file.
a) Set q = 0 in the program. This way, the program will use system() to invoke the command. Is this program safe? If you were Bob, can you compromise the integrity of the system? For example, can you remove any file that is not writable to you?
The SEC file is not safe, Bob can read, write or move files which only root user can run.
b) Set q = 1 in the program. This way, the program will use execve() to invoke the command. Do your attacks in task (a) still work? Please describe and explain your observations.
When modify q to 1, the attack can't make sense. The reason why the before attack effectively is becausesystem() call /bin/sh, which links zsh. After running cat file with root privilege, it runs mv file file_new. But when q = 1, execve() will regard file;mv file file_new2 as a folder name, so system will prompt there have no the file.