SUID and SGID ? Why don't they work ?
I've read somewhere that when you run a script file with SUID, all the commands in the script file will have the owner's permissions. The same with SGID. So I have a test:
Script file: Code:
-rwsr-xr-x myacc myacc usertest Code:
whoami Text file: Code:
-rw------- myacc myacc secret.txt I login as user "youracc", execute the script, and this is the result: Code:
youracc@mydesktop:/home/myacc$ ./usertest Any helps will be appreciated :). |
It depends on the shell you use. Some will change to the effective uid before they execute so what you want to do will not work. Try other shells.
|
Quote:
The problem is that the linux kernel prohibits setuid/setgid shell scripts. Write a program using a language such as C or C++. Setuid/setgid shell scripts have been a source of security problems, and have been strongly discouraged for years. See: http://www.tuxation.com/setuid-on-shell-scripts.html http://www.samag.com/documents/s=114...106a/0106a.htm |
@ Mr. C.: Thank you. That's what I need :D.
@ normscherer: Thanks for your quick reply, the problem does not really come from shell-kind. :) |
Following the second link from Mr. C., I found this line of script:
cp /bin/sh /tmp/sh; chown root /tmp/sh; chmod 4755 /tmp/sh If I get it right, it means: copy a shell to /tmp, set the shell's owner to be root, set SUID for the shell. Now we have a shell which can run as root . I've tried running the script and "chown: Operation not permitted" is what I've received. It makes me confused: to do a chown, we must have root privileges (actually I don't know the differences between "to be root" and "to have root privileges" ), using sudo or something like that. But if we can "sudo", or have root privileges, why do we want to have a shell which "runs as root" ? :D |
A user and a group are simple concepts in Unix/Linux. All processes have a UID/GID/EUID/EGID. The acronyms should be obvious, with the exception being the E, meaning "effective".
To be root is layman's terms to be logged in as root, or to have a shell that has been created with su or sudo to be a root shell. This is nothing more than a process running (shell) with UID=0. To have root privileges means the process is running as UID=0. I'll ignore GID/EGID, as these group counterparts apply in similar fashion. It should be clear that UID=0 is what determines permission to access a resource for a given mode (read/write/execute/examine). Names like "root", "toor", "admin" are all just decorative, and administrative candy above UIDs. Same for groups. The line of code: Code:
cp /bin/sh /tmp/sh; chown root /tmp/sh; chmod 4755 /tmp/sh As an aside, /tmp is often mounted with the nosetuid flag to DISABLE setuid, and often the noexec flags to deny running an executable from /tmp. chown requires the process running it to have the UID of either the owner of the object itself, or UID=0. And chmod requires UID=0 if the running process is not the owner, or belong to one of the object's groups if group write is enabled. |
- Thanks for reminding me of EUID :D. Btw, my /tmp is not mount separately, it stays inside / partition.
- The code is a way to get a root shell by exploiting SUID - After the "cp" command, I am the owner of /tmp/sh: Code:
-rwxr-xr-x 1 myacc myacc 79988 2008-08-26 09:04 /tmp/sh |
A non-root user can NEVER chown files to another user (eg. you can't force files onto another user; consider how this would be a DoS attack on a user with quota's enabled). Only root can do this.
The code above looks like code I've seen from a piece of scripted malware, try to gain a root shell, and planting a trojan horse. |
One more interesting issue I've encountered:
This is my program used to test SUID: Code:
#include <stdlib.h> Code:
g++ source -o binary Code:
-rwsr-xr-x 1 root myacc 9588 2008-08-28 22:16 binary There's a difference of results between RHEL 4 and ubuntu 8.04: Result in Ubuntu 8.04: Quote:
Quote:
Please explain what happened to my knowledge :(. Thanks in advance. |
One word: SELinux.
|
@Mr. C.: Thanks for giving a clue, but it didn't help much, because for avoiding troubles related to SELinux (I've heard SELinux is quite difficult to config), I've disabled SELinux from installing steps of RHEL 4. I've just checked it again, and its state was currently disabled.
Please give me more details (I hope this time you'll leave me more than one word :)). Thanks. |
Does the mounted file system allow setuid programs? Check the mount options with mount.
|
I think my file system allows suid.
Quote:
Quote:
PS: by the way, please pay attention to my above result in RedHat: EUID = 0 but `whoami` still calls me 'myacc', and I cannot read /etc/shadow file. Could it be RedHat user authentication based on RUID, not EUID ? |
There's no explain up to now.
Please help. Thanks. |
There are only three things I know of that affect root setuid for binary executables: SELinux/AppArmor, file system mount options, and ACLs.
What are the ACLs for the binary and containing directory? If this lends no clues, I'm out of options. |
All times are GMT -5. The time now is 02:07 PM. |