News for hackers. Stuff that matters.
Re:Finding an arbitrary file upload vulnerability in a files... by fake-name on Sunday May 14, 2017 @03:20AM (#14334649)
> I thought about a possible fix for a while, but in the end decided that the quickest and easiest fix would be to adjust the regular expressio

..... WHAT?

Python has a suite of facilities exactly for this very kind of problem.

Literally, the solution is "os.path.abspath(filename).startswith(os.path.abspath(dlfolder))"

This should, in all cases, return true if the filename is within the download folder directory, and false for any other case.

Re:Finding an arbitrary file upload vulnerability in a files... by jwilk on Sunday May 14, 2017 @07:16AM (#14335135)
It doesn't do the right thing if dlfolder doesn't end with a slash.
Re:Finding an arbitrary file upload vulnerability in a files... by jwilk on Sunday May 14, 2017 @07:11AM (#14335122)
So I was curious what translate_path does:

    def translate_path(self, path):
        """Translate a /-separated PATH to the local filename syntax.

        Components that mean special things to the local file system
        (e.g. drive or directory names) are ignored.  (XXX They should
        probably be diagnosed.)

        """
        # abandon query parameters
        path = path.split('?',1)[0]
        path = path.split('#',1)[0]
        path = posixpath.normpath(urllib.unquote(path))
        words = path.split('/')
        words = filter(None, words)
        path = os.getcwd()
        for word in words:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir): continue
            path = os.path.join(path, word)
        return path
That... that doesn't make any sense.
Re:Finding an arbitrary file upload vulnerability in a files... by jwilk on Sunday May 14, 2017 @07:19AM (#14335141)
Oh, and thanks to urllib.unquote, I can disguise slashes as "%2f", so the tightened regexp doesn't help.
Re:Finding an arbitrary file upload vulnerability in a files... by jwilk on Sunday May 14, 2017 @10:24AM (#14335751)
Never mind me, I misread how this function is used.

Apparently this code was copied from stdlib, including a different directory traversal bug on Windows:

https://bugs.python.org/issue26657

Re:Finding an arbitrary file upload vulnerability in a files... by jwilk on Sunday May 14, 2017 @07:03AM (#14335105)
Apparently the upload directory is the same as the script directory, which is not good. If you upload a file named "cgi.py", it will be imported next time someone runs the script. No directory traversal needed to exploit this.
Re:Finding an arbitrary file upload vulnerability in a files... by gehaxelt on Sunday May 14, 2017 @03:44PM (#14337219)
Hi,

I asssumed that the script would not be restarted, but you're totally right.

Thanks for the comment :)

Re:Finding an arbitrary file upload vulnerability in a files... by based2 on Sunday May 14, 2017 @05:58AM (#14334949)
Re:Finding an arbitrary file upload vulnerability in a files... by netsec_burn on Saturday May 13, 2017 @08:48PM (#14333700)
Nice find, I commented on your blog a method you can use to extend this vulnerability into RCE without admin interactivity.
Re:Finding an arbitrary file upload vulnerability in a files... by gruez on Saturday May 13, 2017 @08:56PM (#14333724)
>Upload a .py file to the untrusted directory that has the same name of one of the imports used in the Python script. It will be executed the next time Python is called by the script, so you don't need to wait for interactivity on the admins part

I have a feeling that only works on python2 because of absolute imports on python3.

Re:Finding an arbitrary file upload vulnerability in a files... by gcr on Sunday May 14, 2017 @11:07AM (#14335897)
Upload a script that replaces the old file source and wait for gunicorn or whatever to reload the app server.
Re:Finding an arbitrary file upload vulnerability in a files... by gehaxelt on Sunday May 14, 2017 @03:46PM (#14337229)
The script seems to ship with a rundimentary webserver by default, so I don't see a reason to wrap gunicorn et al. around it?

However, you're right that overwriting the source is a possible attack vector.

Re:Finding an arbitrary file upload vulnerability in a files... by internetwache on Saturday May 13, 2017 @08:57PM (#14333725)
Hi,

thanks for commenting!

I had cronjobs in mind, but as you said, they are not writeable.

I also thought about uploading a .py file with an import's name, but wouldn't that require a __init__.py (in a subdirectory?) to be regarded as a valid module/import by python?

Re:Finding an arbitrary file upload vulnerability in a files... by ecdh on Sunday May 14, 2017 @09:41AM (#14335577)
This is hilarious -- I found this exact script and noted the exact same vulnerability last year. Good on you for posting about it.
Re:Finding an arbitrary file upload vulnerability in a files... by gehaxelt on Sunday May 14, 2017 @03:52PM (#14337252)
Nice catch :)

I thought the the amount of forks and stars warrants a short writeup.