ã·ã§ã«ã¹ã¯ãªããã§SUIDã䜿çšããå±éºæ§
é£æ床ïŒã·ã³ãã«
ããã€ãã®èŠåã SUIDããã°ã©ã ãSUIDã¢ããªã±ãŒã·ã§ã³ã¯ãïŒ å®è¡å±æ§ã«å ããŠïŒ setuidå±æ§ãæã€å®è¡å¯èœãã¡ã€ã«ã§ãã
SUIDã¹ã¯ãªãããSUIDã¹ã¯ãªãã-åæ§ã«ãrunå±æ§ã«å ããŠsetuidå±æ§ãæã€ã·ã§ã«ã¹ã¯ãªããã
Unixã·ã¹ãã -UnixãŸãã¯Unixã©ã€ã¯ãªãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã
ãã®èšäºã§ã¯ã å®å šãªé瀺ãšãšã¯ã¹ããã€ãã®äœæã®éã®çŽ°ãç·ããã©ãããšããŸãã ç®æšã¯ãSUIDã¢ããªã±ãŒã·ã§ã³ãã©ã®ããã«åäœããŠãä»ã®äººãããã°ã©ã ãæžããšãã«ããããééããé¿ããã®ãå©ãããã説æããããšã§ãã ãã®èšäºã§æäŸãããŠããäŸã¯ããã¹ãŠã®å±éºãç解ããã®ã«åœ¹ç«ã€ããã«ååã«è©³çŽ°ã«èª¬æãããŠããŸãããæªæã®ãã䜿çšã®å Žåã«ç€ºãããŠãããšããã«ãã¹ãŠãæ©èœãããšããä¿èšŒã¯ãããŸããã
éåžžãUnixã¹ã¯ãªããããã³ããã°ã©ã ã¯ãããããå®è¡ãããŠãŒã¶ãŒã®æš©éã§å®è¡ãããŸãã ããããäžè¬ãŠãŒã¶ãŒã/ etc / passwdãã¡ã€ã«ãçŽæ¥ç·šéããŠãã¹ã¯ãŒããå€æŽã§ããªãçç±ã§ãïŒUnixã·ã¹ãã ã¯ãã®ãã¡ã€ã«ã«ãã¹ã¯ãŒããä¿åããªããªããŸããããã¢ã«ãŠã³ãæ å ±ã®ã¿- çŽ ïŒã / etc / passwdãžã®æžã蟌ã¿æš©éããªããå®è¡ãããåäžã®ã³ãã³ãããããè¡ãããšã¯ã§ããŸããã ãã ããSUIDããã°ã©ã ã¯éåžžã®ã¢ã¯ã»ã¹æš©ããªãŒããŒã©ã€ãããåžžã«ããã°ã©ã ææè ã®æš©éã§å®è¡ãããŸãã ãããã£ãŠããŠãŒã¶ãŒã¯/ usr / bin / passwdã³ãã³ãã䜿çšããŠãã¹ã¯ãŒããå€æŽã§ããŸãã ããã°ã©ã / usr / bin / passwdã«ã¯SUIDå±æ§ããããææè ãšããŠrootãŠãŒã¶ãŒãããŸãã åžžã«ã«ãŒããšããŠå®è¡ãããŸãïŒ
% ls -l `which passwd`
-rwsr-xr-x 1 root root 23688 Jan 6 2007 /usr/bin/passwd
åå¿è ã®ç®¡çè ãSUIDå±æ§ãçºèŠãããšããããäžèœè¬ãšèŠãªããããã«äœæ¥ãç°¡çŽ åããããã«ããã°ã©ã ãšã¹ã¯ãªããã«äœ¿çšãå§ããŸãã æ®å¿µãªãããéåžžã«é »ç¹ã«ééã£ãŠããŸãã
æè¿SUIDå±æ§ã«æ £ãã管çè ãšäœæ¥ããå Žåã次ã®ãããªã·ããªãªãããèŠãããŸãã
% ls change-pass
-rwsr-x--- 1 root helpdesk
37 Feb 26 16:35 change-pass
% cat change-pass
#!/bin/csh -b
set user = $1
passwd $user
ãã®åçŽãªã¹ã¯ãªããã¯ããã«ããã¹ã¯ ïŒãã«ããã¹ã¯ã°ã«ãŒãïŒããŠãŒã¶ãŒãã¹ã¯ãŒãããªã»ããã§ããããã«èšèšãããŠããŸããããã¯éåžžã«äžè¬çãªã¿ã¹ã¯ã§ãã SUIDå±æ§ãã¹ã¯ãªããã«å²ãåœãŠãããã¹ãŒããŒãŠãŒã¶ãŒãææè ãšããŠèšå®ãããŸãã ãã«ããã¹ã¯ã¡ã³ããŒã¯ããã®ã¹ã¯ãªãããèªãã§å®è¡ã§ããŸãã ãµããã®ãããªã»ãã¥ãªãã£ããŒã«ã§ãã£ã±ãã§ãã ãã®èšäºã§ã¯ããã®ãã¡ã®7ã€ãšãããããåé¿ããããã®ãªãã·ã§ã³ã«ã€ããŠæ€èšããŸãã
äž»ãªæžå¿µã¯ãCã·ã§ã«ã®äœ¿çšã§ãã ãã®ã·ã§ã«ã®ã¹ã¯ãªããã¯ãç°å¢å€æ°ã®æäœã«å¯ŸããŠè匱ã§ãã ãããå©çšããããã«ãæ»æè ã¯æ¬¡ã®ããã«ãµããŒãã¢ã«ãŠã³ãã䟵害ããã¹ãŒããŒãŠãŒã¶ãŒç¹æš©ãæã€ã·ã§ã«ãååŸã§ããŸãã
% env TERM='`cp /bin/sh /tmp/sh;chown root /tmp/sh;chmod 4755/tmp/sh`' change-pass
ã¬ãã¹ã³1-SUIDã¹ã¯ãªããã«Cã·ã§ã«ã䜿çšããªã
% cat change-pass
#!/bin/ksh
user=$1
passwd $user
Kornã·ã§ã«ã䜿çšããŠã¹ã¯ãªãããæžãæãããšãCã·ã§ã«åºæã®åé¡ãåé¿ã§ããŸãããã¹ã¯ãªããã¯PATHç°å¢å€æ°ã®æäœã«å¯ŸããŠè匱ã§ãã ããã°ã©ã ãžã®çžå¯Ÿãã¹ã䜿çšãããšãæ»æè ã¯æšæºã®/ usr / bin / passwdã®ä»£ããã«ã¢ããªã±ãŒã·ã§ã³ãå®è¡ã§ããŸãã
% export PATH='/tmp'
% echo "cp /bin/sh /tmp/sh;chown root /tmp/sh;chmod 4755/tmp/sh" > /tmp/passwd
% ./change-pass
PATHå€æ°ãå€æŽãããchange-passã³ãã³ãã/ usr / bin / passwdã®ä»£ããã«/ tmp / passwdãåŒã³åºãããã«ãªããŸããã
ã¬ãã¹ã³2-ç°å¢å€æ°PATHãåžžã«æåã§èšå®ãã絶察ãã¹ã䜿çšããå¿ èŠããããŸã
% cat change-pass
#!/bin/ksh
PATH='/bin:/usr/bin'
user=$1
/usr/bin/passwd $user
ããã§PATHã¯å®å šã«ãªãã絶察ãã¹ã䜿çšãããŸãããããèŠããšãã¹ã¯ãªãããrootãå«ããã¹ãŠã®ãŠãŒã¶ãŒã®ãã¹ã¯ãŒããå€æŽã§ããããšãããããŸãã ã¹ã¯ãªããã䜿çšããŠã¹ãŒããŒãŠãŒã¶ãŒã®ãã¹ã¯ãŒããå€æŽãããµããŒããµãŒãã¹ïŒãŸãã¯ã¯ã©ãã«ãŒïŒã®ãŠãŒã¶ãŒãæãŸãªãã
ã¬ãã¹ã³3-é¢ä¿ããããã°ã©ã ã®äœæ¥ãç解ããå¿ èŠããããŸã
% cat change-pass
#!/bin/ksh
PATH='/bin:/usr/bin'
user=$1
rm /tmp/.user
echo "$user" > /tmp/.user
isroot='/usr/bin/grep -c root /tmp/.user'
[ "$isroot" -gt 0 ] && echo "You Can't change root's password!" && exit
/usr/bin/passwd $user
誰ããrootãåŒæ°ãšããŠå ¥åãããšãã¹ã¯ãªããã¯ãã®èªç±ãçµäºããŸãã ããããåŒæ°ããŸã£ããæž¡ããªããšã©ããªããŸããïŒ passwdã¯ãããããåŒæ°ãªãã§åŒã³åºãããŸãã ãã®å Žåãããã°ã©ã ã¯çŸåšã®ãŠãŒã¶ãŒïŒèµ·åãéå§ããïŒã®ãã¹ã¯ãŒããã€ãŸãrootãå€æŽããŸãïŒSUIDã®éå§çŽåŸã«ãžã§ããå®è¡ããçŸåšã®ãŠãŒã¶ãŒãrootã«å€æŽããããšãå¿ããªãã§ãã ãã-ïŒã SUIDå±æ§ãæã€å®è¡å¯èœãã¡ã€ã«ã®ã³ã³ããã¹ãã§ã¯ãçŸåšã®ãŠãŒã¶ãŒã¯åžžã«ãã®ãã¡ã€ã«ã®ææè ã«ãªããŸãã ãããã£ãŠãchange-passãåŒã³åºããŠåäžã®åŒæ°ãæž¡ããã«ãã¹ãŒããŒãŠãŒã¶ãŒã®ãã¹ã¯ãŒãããªã»ããããããšã¯ãŸã å¯èœã§ãã ãããã£ãŠã3çªç®ã®ã¬ãã¹ã³ã®æ¹è¯-ããã°ã©ã ãã¹ã¯ãªããå ã§ã©ã®ããã«æ©èœããããç¹ã«åŒæ°ãåŠçããæ¹æ³ãç解ããŸãã
% cat change-pass
#!/bin/ksh
PATH='/bin:/usr/bin'
user=$1
[ -z $user ] && echo "Usage: change-pass username" && exit
rm /tmp/.user
echo "$user" > /tmp/.user
isroot='/usr/bin/grep -c root /tmp/.user'
[ "$isroot" -gt 0 ] && echo "You Can't change root's password!" && exit
/usr/bin/passwd $user
ããã§ã誰ãrootãŠãŒã¶ãŒã®ãã¹ã¯ãŒããå€æŽããããšã¯ã§ããªããªããŸãããäžæãã¡ã€ã«ã®äœ¿çšã«ã¯æ³šæãæã£ãŠãã ããïŒç§ã¯å人çã«ãã®å¿ èŠæ§ãç解ã§ããŸãã- çŽ ïŒã ã¹ã¯ãªããã¯äžæãã¡ã€ã«ãåé€ãããã¹ã¯ãŒãããªã»ããããå¿ èŠããããŠãŒã¶ãŒã®ååãå ¥åããŠäœæããæåŸã«ãã®ãŠãŒã¶ãŒãã«ãŒããã©ããã確èªããŸãã æ»æè ããã¡ã€ã«ãåé€ãããæ°ãããã¡ã€ã«ããŸã äœæãããŠããªãç¬éãéåžžã«æ£ç¢ºã«æž¬å®ãã空ã®/tmp/.userãã¡ã€ã«ãäœæãããšã©ããªããŸããïŒ äžæžããããŸããïŒ ãã¶ãã¯ããããããã¶ãããã§ã¯ãªã...ã·ã¹ãã èšå®ã«äŸå ã¯ã©ãã«ãŒã«ãã£ãŠäœæããã/tmp/.userãäžæžããããªãå Žåãã¹ã¯ãªããã®ãã§ãã¯ã«åæ Œããpasswdã¯ã¹ãŒããŒãŠãŒã¶ãŒã®ãã¹ã¯ãŒãã®å€æŽãææ¡ããŸãïŒåŒæ°ããªãå Žå- çŽ ïŒã ãã®ãããªæ»æã容æã«ããããã«ãæ»æè ã¯ç¹å¥ãªããã°ã©ã ãäœæããŠã¢ã¯ãã£ããã£ïŒãã®å Žåã¯/tmp/.userãã¡ã€ã«ã®å€èŠ³ïŒã远跡ããå¿ èŠãªãã¡ã€ã«ã眮ãæããããšãã§ããŸãã
ã泚æ ãããã®ã¿ã€ãã®æ»æã¯ãæéé 延ã«åºã¥ããŠããŸãïŒä»¥äžãåæ§ã®äŸããããŸãïŒã
ã¬ãã¹ã³4-äžæãã¡ã€ã«ã䜿çšããªãããŸãã¯ïŒäœ¿çšããå¿ èŠãããå Žåã¯ïŒäžæãã¡ã€ã«ãä»ã®æžã蟌ã¿å¯èœãªå Žæã«çœ®ããªã
% cat change-pass
#!/bin/ksh
PATH='/bin:/usr/bin'
user=$1
[ -z $user ] && echo "Usage: change-pass username" && exit
[ "$user" = root ] && echo "You can't change root's password!" && exit
/usr/bin/passwd $user
çŸåšã®ããŒãžã§ã³ã§ã¯ãäžæãã¡ã€ã«ã¯äœ¿çšãããŸããïŒãã®ããã«- çŽ ïŒããæ»æè ã¯ã»ãã³ãã³ïŒåºåãæåïŒã䜿çšããŠããªãã¯ã䜿çšã§ããŸãã ã;ãã䜿çšãããšãè€æ°ã®ã³ãã³ãã1è¡ã«èšè¿°ããŠãé çªã«å®è¡ã§ããŸãã ãããç¥ã£ãŠãã¯ã©ãã«ãŒã¯æ¬¡ã®ããã«æžãããšãã§ããŸãïŒ
% change-pass "user;cp /bin/sh /tmp/sh;chown root /tmp/sh;chmod 4755 /tmp/sh"
ã¹ã¯ãªããã¯ãã®å ¥åãåãå ¥ããŠå®è¡ããŸãã
/usr/bin/passwd user;cp /bin/sh /tmp/sh;chown root /tmp/sh;chmod 4755 /tmp/sh
ãããã®åã³ãã³ããå®è¡ãããã·ã§ã«ã«ã¹ãŒããŒãŠãŒã¶ãŒç¹æš©ãäžããããŸãã ãã®ãããªåé¡ãé²ãã«ã¯ããŠãŒã¶ãŒå ¥åã«ã»ãã³ãã³ãŸãã¯ãã®ä»ã®ã¡ã¿æåãå«ãŸããŠããªãããšã確èªããå¿ èŠããããŸãã
% cat change-pass
#!/bin/ksh
PATH='/bin:/usr/bin'
user=${1##*[ \\$/;()|\>\<& ]}
[ -z $user ] && echo "Usage: change-pass username" && exit
[ "$user" = root ] && "You can't change root's password!" && exit
/usr/bin/passwd $user
ããã§ãã¹ããŒã¹æåã\ã$ã/ã;ãïŒãïŒã|ã>ã<ãïŒãããã³ã¿ããå ¥åããåé€ãããŸãã
ã¬ãã¹ã³5-ãã¹ãŠã®ãŠãŒã¶ãŒå ¥åãä¿¡é Œããã³ãã§ãã¯ãããã¡ã¿æåãé€å€ããŸã
ãã1ã€ã®äžè¬çãªè匱æ§ã¯ã å éšãã£ãŒã«ãåé¢ïŒIFS ïŒã³ãã³ãã·ã§ã«ã«é¢é£ããŠããŸãã IFSã¯ãã³ãã³ããåºåãæåãå®çŸ©ããŸãã éåžžãããã¯ã¹ããŒã¹ãã¿ãããŸãã¯æ¹è¡æåã§ãã ã¹ã¯ãªããã¯ããã«ãã¹/ usr / bin / passwdã䜿çšããŠããã°ã©ã ãåŒã³åºããŸãã IFSãã/ãã³ãã³ãã«çœ®ãæãã
ã¹ã¯ãªããã/ usr / bin / passwdãåŒã³åºããã代ããã«usrãbinãpasswdãé çªã«å®è¡ããŸãã ããã§ãæ»æè ã¯usrãšããã¹ã¯ãªãããäœæããŠãrootæš©éã§ã·ã§ã«ãäœæã§ããSUIDã¹ã¯ãªããããããå®è¡ããŸãã% export IFS='/'
ã¬ãã¹ã³6-åžžã«IFSãæåã§å®çŸ©ãã
% cat change-pass
#!/bin/ksh
PATH='/bin:/usr/bin'
IFS=' '
user=${1##*[ \\$/;()|\>\<& ]}
[ -z $user ] && echo "Usage: change-pass username" && exit
[ "$user" = root ] && "You can't change root's password!" && exit
/usr/bin/passwd $user
æ®å¿µãªãããç§ãã¡ã¯ãŸã å®å šã§ã¯ãããŸããã ã·ã§ã«ããŒã ã®ã·ããªãªã«ã¯ãã人皮ãã®åºçŸã«å¯Ÿããåºæã®æ¡ä»¶ããããŸããããã¯ãé«å質ã®ã¹ã¯ãªãããèšè¿°ããããšã§ããå æããããšã¯ã§ããŸããã åé¡ã¯ãã¹ã¯ãªããã2段éã§å®è¡ãããããšã§ãã æåã«ãã·ã¹ãã ã¯å¥ã®ã·ã§ã«ã€ã³ã¹ã¿ã³ã¹ãèµ·åããŸãã 次ã«ãæ°ããããã»ã¹ãã¹ã¯ãªãããã¡ã€ã«ã®å 容ãèªã¿åããå®è¡ããŸãã 4çªç®ã®ã¬ãã¹ã³ã§äžæãã¡ã€ã«ã䜿çšããç¶æ³ãšåæ§ã«ãæ»æè ã¯æéé 延ãæªçšããæ°ããã·ã§ã«ãäœæããŠããã¹ã¯ãªãããèªã¿åããŸã§ã®ç¬éãå©çšã§ããŸãã SUIDã¹ã¯ãªãããžã®ã·ã³ããªãã¯ãªã³ã¯ãäœæãã
åç §ã«ããã¹ã¯ãªããã®åŒã³åºããšå 容ã®è¿ éãªçœ®æ% cd /tmp
% ln -s change-pass rootme
rootãšããŠäœã§ãå®è¡ã§ããŸãã ãã®ææ³ã䜿çšãããšãæåã®å¯èœæ§ã¯éåžžã«äœããªããŸãããæåã®å¯èœæ§ãé«ããããã»ã¹ãèªååããã®ã«åœ¹ç«ã€ææ³ãšããã°ã©ã ããããŸãã ãã®çš®ã®æ»æãã身ãå®ãã«ã¯2ã€ã®æ¹æ³ããããŸãã 1ã€ç®ã¯ãSUIDã·ã§ã«ã¹ã¯ãªããã䜿çšããªãããšã§ãã 2ã€ç®ã¯ãäžéšã®ã·ã¹ãã ïŒSolarisãªã©ïŒãææããŠããŸããããã¯ãéããŠããã¹ã¯ãªãããã¡ã€ã«ã®èšè¿°åãã³ãã³ãã·ã§ã«ã«æž¡ãããšã§ãã¬ãŒã¹ãç¶æ ã®çºçãé²ããSUIDã¹ã¯ãªãããã¡ã€ã«ãåæ€åºããŠèªã¿åãå¿ èŠããããŸããã% ./rootme &
% rm rootme && echo "cp /bin/sh /tmp/sh;chown root /tmp/sh;chmod 4755 /tmp/sh" > rootme
ã¬ãã¹ã³7-SUIDã¹ã¯ãªããã䜿çšããªã
ãã¹ãŠã®äœæ¥ãå®äºããåŸã§ããå®å šãªSUIDã·ã§ã«ã¹ã¯ãªãããèšè¿°ããããšã¯ã»ãšãã©äžå¯èœã§ãïŒããã¯ã»ãšãã©ã®ã·ã¹ãã ã§ã¯äžå¯èœã§ãïŒã äžèšã®åé¡ã«ãããäžéšã®ã·ã¹ãã ïŒLinuxãªã©ïŒã§ã¯ãã³ãã³ãã¹ã¯ãªããã«SUIDå±æ§ãã€ã³ã¹ããŒã«ããããšãæšå¥šããŠããŸããã SUIDæ©èœãååŸããã«ã¯ãCã©ãããŒãPerlã¹ã¯ãªããããŸãã¯sudoã®ãããªããã°ã©ã ã®3ã€ã®å®å šãªæ¹æ³ããããŸãã å®å šãªããã°ã©ãã³ã°ã®åå¿è ã¯ãsudoãŸãã¯Perlããã°ã©ã ã䜿çšããå¿ èŠããããŸãã Suidperlã«ã¯ããã®èšäºã§èª¬æããããã°ã©ããŒåãã®ãšã©ãŒä¿è·ã¡ã«ããºã ãçµã¿èŸŒãŸããŠããŸãã SUIDå±æ§ã䜿çšããå®å šãªããã°ã©ãã³ã°ã®è©³çŽ°ã«ã€ããŠã¯ãæžç±Practical UNIXïŒInternet SecurityïŒO'ReillyïŒAssociatesïŒãŸãã¯èšäºWriting Writing Safe Setuid Programsãåç §ããŠãã ããã
ããšããã SUIDã䜿çšããããã®ãããã¿ã€ãã©ãããŒïŒ
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
//
setuid(0); // id ( root, )
//setgid(0); // (id )
system("/path/to/script.sh"); //
return 0;
}