Directory Traversal via ".tar" file

2024. 4. 12. 22:47·💻 Security

1. Concept

  • Directory Traversal via .tar file
  • CVE-2007-4559
  • Bypass Patch code and advanced exploit 

 

2. Exploit Principle before code was patched

The vulnerability occurs when extracting files with the ".tar" extension via the extract, extractall functions of the tarfile Python library.

 

https://github.com/python/cpython/blob/3.10/Lib/tarfile.py#L2327

When a file is saved, the filename is saved using the path and the tar filename.

e.g) path: ~/Desktop/   | filename : flag.tar
>> result: ~/Destkop/flag.tar

 

https://github.com/python/cpython/blob/3.10/Lib/tarfile.py#L2854

 

The extract and extractall functions extract specific files via a saved filename. But there is no validation of filename during the upload process. So attacker can upload the filename like "../../flag.tar". If Web application has the functions of extracting and reading file contents, the attacker can read files in the parent directory.

 

3. Exploit principle after code was patched

I'm not sure this patch code is for preventing directory traversal vulnerability. Whatever, let's check it out.

https://github.com/python/cpython/blob/3.10/Lib/tarfile.py#L2256

You can see _get_extract_tarinfo function is added recently.

Once inside, you can find the getmember function among the code related to filename.

https://github.com/python/cpython/blob/3.10/Lib/tarfile.py#L1977

When defining filename, specify the string from the first character before the slash as the filename. So we can't exploit this with traditional method.

 

4. How to Bypass?

  • We can't directly input slash in filename
  • To bypass this logic, use Symbolic link. Upload a zipped file that points to another file in the parent directory.
  • If web application extracts the uploaded file and read it, the file points another file in the parent directory. Due to this process, the attacker can read other files.

5. Validation

This description simplifies the structure for better understanding. In real world, a web service should have the ability to upload a compressed file and ability to extract and display the compressed file to the user.

 

First, make a symoblic link file.

The flag file is in parent directory of test. ( relative path: ../ )

 

This is PoC Code. If run this code, flag file can be read.

 

• Side Note

  • The above vulnerabilities exist not only in tar files, but also in popular compressed file formats (.zip, .jar, etc.)
  • Not only Python(Flask, Django), but also NodeJS and Spring has this vulnerability.
  • Mitigation : In my opinion, there are ways to strictly set access to directory folders to prevent access to parent directories.
    (  I googled mitigation, but there was nothing. So I write my private opinion :(  )

 

Reference :

tarrible-storage : https://cronuse.tistory.com/283

 

 

 

저작자표시 비영리 변경금지 (새창열림)
'💻 Security' 카테고리의 다른 글
  • CVE-2023-27224 (NginxProxyManager)
  • CVE-2024-4367 (PDF.js - Arbitrary JS Execution)
  • Apache OFBIZ Vulnerability (CVE-2023-49070)
  • [ CVE-2022-1582 ] Wordpress External Links in New Window Analysis
Cronus
Cronus
Offensive Security Researcher
  • Cronus
    Cronus
    Striving to be the best.
    • 분류 전체보기 (251)
      • AboutMe (1)
      • Portfolio (1)
        • Things (1)
      • Bug Report (1)
      • 🚩 CTF (23)
        • Former Doc (9)
        • 2023 (9)
      • 💻 Security (5)
      • 🖌️ Theory (22)
        • WEB (9)
        • PWN (13)
      • 📄 Project (6)
        • Edu_Siri (6)
      • Dreamhack (156)
        • WEB (95)
        • PWN (41)
        • Crypto (14)
        • ETC (6)
      • Wargame (22)
        • HackCTF (22)
      • Bug Bounty (1)
        • Hacking Zone (1)
      • Tips (7)
      • Development (2)
        • Machine Learning & Deep Lea.. (1)
      • Offensive Tools (1)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Ubuntu 기초 셋팅
    bug report
    Deep learning
    cache poisoning
    Remote Code Execution
    cache
    GPNCTF
    ubuntu 명령어
    TFCCTF2022
    Ubuntu 기초
    Crypto
    TsukuCTF2022
    RCE
    pwntools
    python
    Machine Learning
    justCTF
    Text Summarization
    bug hunter
    sqli
  • 최근 댓글

  • 최근 글

Cronus
Directory Traversal via ".tar" file
상단으로

티스토리툴바