Perlã§root権éã§åä½ãããå ´åã®æ³¨æç¹
root権éã§åä½ãããã常é§ããã°ã©ã ãä½ã£ã¦åä½ããã¦ã¿ããã®ã®ããã¾ãããå ´åã¨ãã¾ããããªãå ´åããã£ã¦ãããªãã¯ã¾ã£ã¦ãã¾ã£ãã
ã·ã§ã«ä¸ã§rootã¦ã¼ã¶ã§perlããã°ã©ã ãå®è¡ããå ´åã¨ããªãããã®ããªã¬ã¼ï¼ã¡ã¼ã«ããxmlrpcã¨ãã§ã³ãã³ããåãåã£ã¦ï¼ãçµã¦èªåã§å®è¡ãããã¨ãã®éãã調ã¹ã¦ã¿ãã
perlsec - Perl ã®ã»ãã¥ãªãã£
http://perldoc.jp/docs/perl/5.6.1/perlsec.pod
Perl ã¯ããã®ããã°ã©ã ãç°ãªãå®ã¦ã¼ã¶ã¼ IDãå®å¹ã¦ã¼ã¶ã¼ IDãå®ã°ã«ã¼ã IDãå®å¹ã°ã«ã¼ã ID ã使ã£ã¦å®è¡ããããã¨ãæ¤åºããã¨ãã«ãèªåçã« æ±æã¢ã¼ã (taint mode) ã¨å¼ã°ããç¹å¥ãªã»ãã¥ãªãã£ãã§ãã¯ã®ã»ãããæå¹ã«ãã¾ãã UNIX ãã¼ããã·ã§ã³ã«ããã setuid ãããã¯ã¢ã¼ã 04000 ã§ã setgid ãããã¯ã¢ã¼ã 02000 ã§ãããããã¯éè¤ãã¦ã»ãããããã¨ãã§ãã¾ããæ±æã¢ã¼ãã¯ãã³ãã³ãã©ã¤ã³ãã©ã° -T ã使ã£ã¦é½ã«æå¹ã«ãããã¨ãã§ãã¾ãããã®ãã©ã°ã¯ãµã¼ãã¼ããã°ã©ã ã§ããã¨ãã CGI ã¹ã¯ãªããã®ãããªãä»ã®èª°ãã«ããããã£ã¦å®è¡ãããããã°ã©ã ã«ä½¿ããã¨ã å¼·ã å§ãã¾ãã
ããããroot権éã§åä½ããªãå ´åã«ã¯æ±æã¢ã¼ãã«ãªã£ã¦ãã¹ã ã¨ãããããããªãã§ãã¯ã«å¼ã£ããã£ã¦ãããã ããã¨äºæ³ã
èªåã®ããã°ã©ã ã®å¤å´ããæ¥ããã¼ã¿ãããã°ã©ã ã®å¤ã®ä½ãã«å½±é¿ãåã¼ãããã«ä½¿ããã¨ã¯ãå°ãªãã¨ãã¢ã¯ã·ãã³ãã§ã§ããªããã°ãã§ãã¾ããããã¹ã¦ã®ã³ãã³ãã©ã¤ã³å¼æ°ãç°å¢å¤æ°ããã±ã¼ã«æ å ±(perllocale ãåç §)ãå¹¾ã¤ãã®ã·ã¹ãã ã³ã¼ã«ã®çµæ(readdir(), readlink(), shmread() ã®å¤æ°ã msgrcv() ãè¿ããã¡ãã»ã¼ã¸ããã¹ã¯ã¼ããgetpwxxx() å¼ã³åºããè¿ãã gecos ãã£ã¼ã«ãã¨ã·ã§ã«ãã£ã¼ã«ã)ããã¹ã¦ã®ãã¡ã¤ã«å ¥åã¨ãã£ããã®ã¯ âæ±æãããâ(tainted) ã¨ç®å°ãä»ãããã¾ããæ±æããããã¼ã¿ã¯ç´æ¥ãéæ¥ãåãããµãã·ã§ã«ãèµ·åããã³ãã³ãã«ä½¿ããã¨ãããã¡ã¤ã«ããã£ã¬ã¯ããªãããã»ã¹ã«å¤æ´ãå ãããããªã³ãã³ãã«ä½¿ããã¨ãã§ãã¾ãããä½ã 以ä¸ã®ä¾å¤ ãããã¾ãã
- system ããã㯠exec ã«å¯¾ããå¼æ°ãªã¹ãã®è¦ç´ ã¨ãã¦æ¸¡ããå ´åã«ã¯ããã®è¦ç´ ã«å¯¾ããæ±ææ¤æ»ã¯ è¡ããã¾ãããï¼ï½ï½ãå«ãï¼
- print 㨠syswrite ã®å¼æ°ã«å¯¾ããæ±ææ¤æ»ã¯ è¡ããã¾ããã
ä¾ã示ãã¾ã:
$arg = shift; # $arg ã¯æ±æããã $hid = $arg, 'bar'; # $hid ãæ±æããã $line = <>; # æ±æããã $line = <STDIN>; # ãããæ±æããã open FOO, "/home/me/bar" or die $!; $line = <FOO>; # ã¾ã æ±æããã¦ãã $path = $ENV{'PATH'}; # æ±æããã¦ããããä¸è¨ãåç §ã®ã㨠$data = 'abc'; # æ±æããã¦ããªã system "echo $arg"; # å®å ¨ã§ã¯ãªã system "/bin/echo", $arg; # å®å ¨ (shã使ãã¾ãã) system "echo $hid"; # å®å ¨ã§ã¯ãªã system "echo $data"; # PATHãè¨å®ããã¾ã§ã¯å®å ¨ã§ã¯ãªã $path = $ENV{'PATH'}; # $path ãæ±æããã $ENV{'PATH'} = '/bin:/usr/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; $path = $ENV{'PATH'}; # $path ã¯æ±æããã¦ããªã system "echo $data"; # ããã§å®å ¨! open(FOO, "< $arg"); # OK - èªã¿è¾¼ã¿ã®ã¿ã®ãã¡ã¤ã« open(FOO, "> $arg"); # Not OK - æ¸ãè¾¼ã¿ãããã¨ãã¦ãã open(FOO,"echo $arg|"); # Not OK, but... open(FOO,"-|") or exec 'echo', $arg; # OK $shout = `echo $arg`; # å®å ¨ã§ãªãã$shoutã¯æ±æãããã unlink $data, $arg; # å®å ¨ã§ãªã umask $arg; # å®å ¨ã§ãªã exec "echo $arg"; # å®å ¨ã§ãªã exec "echo", $arg; # å®å ¨ (ã·ã§ã«ã使ãã¾ãã) exec "sh", '-c', $arg; # å®å ¨ã¨è§£éãããããã! @files = <*.c>; # å®å ¨ã§ãªã (readdir() ã®ãããªãã®ã使ã) @files = glob('*.c'); # å®å ¨ã§ãªã (reã£ï½addir() ã®ãããªãã®ã使ã)
ã©ãæ±æããã¦ãããã調ã¹ãã«ã¯
(æ±æããããã¼ã¿ã®æ¤åºã¨æ´æµ)
ããå¤æ°ãæ±æããããã¼ã¿ãä¿æãã¦ãããã©ãããæ¤æ»ãããããããã¦ã "Insecure dependency" ã¡ãã»ã¼ã¸ã®å¼ãéã«ãªãå¯è½æ§ããããã©ãããæ¤æ»ããããã«ãããªãã®æã身è¿ã«ãã CPAN ã®ãã©ã¼ãµã¤ã㧠Taint.pm ã¢ã¸ã¥ã¼ã«ãæ¢ãã¦ã¿ã¦ãã ããããã㯠1997 å¹´ã® 11 æã«å ¥æã§ããããã«ãªãã§ãããããããã¯ã以ä¸ã®ãããªé¢æ° is_tainted() ã使ããã¨ãã§ãã¾ãã
sub is_tainted { return ! eval { join('',@_), kill 0; 1; }; }
ã³ãã³ãå®è¡ã¯æ³¨æãã¦æ¸ãã®ã¯å½ããåãªãã ãã©ãã¯ã¾ã£ãé¨åã¯ãã¡ã¤ã«ä¸æ¸ãã追å ãªã©ã®ãã¡ã¤ã«æä½ã§ããã
ã¨ãããåã«IO::Allã§ãã¡ã¤ã«ã追å ãã¦ãããã ãã©ãããã§ã¯ã ãã£ã½ãï¼å°ãªãã¨ãæ±æã¢ã¼ãã§ã¯ï¼ã
ã§æ®éã«openãã訳ãªãã ãã©ããããã調ã¹ã¦ã¿ãã
åèã«ãªã£ããµã¤ã
perl - open my $fh, "comand |"; # ã¯ã¢ãã³ãããªã
http://blog.livedoor.jp/dankogai/archives/51176081.html
ã¢ãã³Perlå ¥éã«ããè¨è¿°ã§
open(my $fh, '| cat -v');
ä¸è¨ã®é¨åãªãã ãã©ãä¸è¨ã®ããã«æ¸ã
open my $fh, '|-', qw/cat -v/ or die $!;
http://www.bioinfo.jp/tips.html#setuid
ä¸è¨ã¯ã¨ã¦ãããããããæ¸ãã¦ããã¦ãã¾ãããå¼æ°
以ä¸ã¯perlsecã«ããä¾ã§ã
exec "echo $arg"; # å®å ¨ã§ãªã exec "echo", $arg; # å®å ¨ (ã·ã§ã«ã使ãã¾ãã)
systemãopenãªããã使ãã¨ãã¯å¼æ°ã§ä¸ããã®ããã¤ã³ãããã»ãã¥ãªãã£çã«ãå®å¿ã ããã¢ãã³ã ãï¼ã£ã¦äºã®ããã ã
ä½è«ï¼perlsecã¯5.6ã®æã®ãã®ã¿ããã ãã©ãã ãã¶åããã¢ãã³ãªï¼ãªãã ããï¼ï¼æ¸ãæ¹ã¯ã¡ããã¨ç¤ºããã¦ããããã ï¼
http://www.ipa.go.jp/security/awareness/vendor/programmingv1/a04_03.html
ã«ãperlsecã®ã½ã¼ã¹ãæ¸ãã¦ããã¾ãã
æ±æé¤å»
$ENV{'PATH'} = '/bin:/usr/bin'; â PATH ç°å¢å¤æ°ãè¨å® delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; â IFS,CDPATH,ENV,BASH_ENVç°å¢ å¤æ°ã空ã«ãã
ãã®æ±æãã§ãã¯ã«ã¤ãã¦èª¿ã¹ãçç±ã¯ãã¡ã¤ã«ã®è¿½å ããã¾ããããªãã£ãäºã
å
·ä½çã«ã¯perlã§ãã¹ãããã°ã©ã ãæ¸ãã¦ã·ã§ã«ä¸ã§perl test_01.tãªãããå®è¡ããå ´åã«ã¯ãã¾ããããã ãã©ãxmlrpcãµã¼ãã¨ãã«ã³ãã³ããæãã¦ããã°ã©ã ãå®è¡ãããã¨ãããããªãããã¾ããããªãã»ã»ã»ããªãã§ã ã¼ã¨ã ãã¶æ©ã¿ã¾ããã
ãã¾ããããªãã£ãã®ã¯Perlãããæ°ãå©ããã¦æ±æã¢ã¼ããã§ãã¯ããã¦ããã¦ããããããå±éºãªç¶æ
ã ã£ããããã¾ãå®è¡ã§ããªããã¦ããã¦ããããããªããæ±æã¢ã¼ãèªä½ã«ã¤ãã¦ã¯ä½ã¨ãªãç¥ã£ã¦ã¯ãããã®ã®ãã¡ããã¨èª¿ã¹ãã¨ããããã£ã¦ããªãã£ãããã§ããç°å¢å¤æ°ã®äºã¨ããã¢ãã³ãªopenãªæ¸ãæ¹ã¨ãã£ã¦ãæå³ããããã ãªãã¨è¨ãã®ãããã£ãããåããæã£ã¦ããã¢ãã³ã£ã¦ä½ï¼ã®æå³ãå°ãã§ã¯ãããã©èº«ã«ã¤ããæ°ãããã
ãã¨perlã®ããã¥ã¡ã³ãã¯ã¨ã¦ãããæç« ã ãªã¨æã£ãã®ã¨ãæ¥æ¬èªè¨³ãã¦ããã¦ã人ãã¡ã«ã¯å度æè¬ã§ããã¼