REF:
man chmod
Supplements (Mon Jan 7 00:33:37 CST 2013)
1. setuid只对二进制文件或者perl脚本起作用(suid可以加在shell脚本上,但是不起作用)
Note: For security reasons the s-bit works only when used on binaries
(compiled code) and not on scripts (an exception are perl scripts).
Scripts,i.e. programs that cannot be executed by the kernel directory
but need an interpreter such as the Bourne shell or Java,can have
their setuid bit set, but it doesn't have any effect. There are some
platforms that honor the s bits even on scripts ( some System V vari-
ants, for example), but most systems don't because it has proven such
a security headache - most interpreters simply aren't written with
much security in mind. Set the SUID bit on shell script is useless,
that's why I am using perl script here.
http://www.bashguru.com/2010/03/unixlinux-advanced-file-permissions.html
2. 在运行有suid的perl时如果碰到以下错误,在pl脚本本机中加入$ENV{"PATH"} = "/usr/bin";这行代码即可,/usr/bin为pl可执行文件所在的目录。
Insecure $ENV{PATH} while running setuid at
http://chrisjean.com/2011/06/06/fix-insecure-envpath-while-running-setuid/
示例perl代码:
#!/usr/bin/perl $ENV{"PATH"} = "/bin"; system('ls','/root'); system('touch','/root/root'); system('ls','/test'); system('touch','/test/test');
20140227补充:
分享一篇关于Effective ID和Real ID的文章
我们不能直接用vim编辑/etc/shadow文件,因为kernel会检查我们的effectvie ID,进而发现vim没有权限去修改root的文件
但是我们却可以通过passwd程序来修改/etc/shadow文件,因为passwd有setuid位,调用它时,我们的effectvie ID被设置成了0,即root的uid
不过我们却不能用passwd程序修改别人的密码,这是因为passwd会检查我们的real ID,如果real ID不匹配则拒绝修改
初始状态时real ID和effective ID一样,都等于/etc/passwd文件里的uid.不过effective ID主要用来进行权限验证并可通过带有suid位的程序改变,而real ID不能改变
Real and Effective IDs
http://www.lst.de/~okir/blackhats/node23.html