SQLi via WebSockets
Inserting unsanitized user input from WebSocket connections into SQL queries can lead to SQL injection vulnerabilities. However, due to the lack of WebSocket support in many exploitation tools, abusing WebSocket SQLi can be more challenging.
Code Review - Identifying the Vulnerability
Application Overview
Web application displays messages for a given username via WebSocket connection.
Backend Analysis (server.py)
@sock.route('/dbconnector')
def dbconnector(sock):
while True:
response = {}
try:
data = sock.receive(timeout=1)
if not data:
continue
username = json.loads(data).get('username', '')
response["username"] = username
messages = query(username)
if not messages:
response['error'] = "No messages for this user!"
else:
response['messages'] = [msg[0] for msg in messages]
sock.send(json.dumps(response))
except Exception as e:
response['error'] = "An error occured!"
sock.send(json.dumps(response))Vulnerable Query Function
Vulnerability: Username directly interpolated into SQL query β SQLi!
Local Testing
Setup MySQL Docker
Confirm SQLi
Use UNION-based payload as username:
Result: 1 displayed β SQLi confirmed!
Exploitation with sqlmap
The Problem
sqlmap struggles with WebSocket connections directly.
Solution: HTTP-to-WebSocket Middleware
Create a Flask middleware that:
Receives SQLi payload from sqlmap via HTTP
Opens WebSocket connection to target
Forwards payload via WebSocket
Returns response to sqlmap
Middleware Code
Install Dependencies
Run Middleware
Run sqlmap
Results:
Question Walkthrough
Task: Exploit SQLi vulnerability to exfiltrate the flag from the database.
Step 1: Analyze Source Code
Vulnerable line in server.py:
Step 2: Create Middleware
Save middleware code to sqlmapMiddleware.py (replace STMIP with target IP):
Step 3: Run Middleware
Step 4: Confirm SQLi
Step 5: Enumerate Databases
Step 6: Enumerate Tables
Step 7: Dump Flag
Flag retrieved from secretdata table!
Summary
Key Points
WebSocket SQLi exploitable like HTTP SQLi
Use middleware to bridge sqlmap β WebSocket
--prefix='"'may be needed for proper injectionSame technique applies to other vulns (Command Injection, LFI)
Last updated