Zip Slip
zipファイルを十分に検証せずに解凍するときに起こる脆弱性のこと
例えば../../../../etc/passwdというファイルを含んだzipをそのまま解凍してしまうと、/etc/passwdが上書きされる
tarファイルも同様の攻撃ができる (Tar Slip)
Synk社のセキュリティチームが発見した
例:
code:terminal
$ pwd
/tmp/zipslip
$ ls -alR
.:
internal target.txt
./internal:
evil.zip
$ cat target.txt # 書き換えるファイル
WRITE ME
$ cd internal
$ zipinfo evil.zip # 細工したzipファイル
Archive: evil.zip
Zip file size: 182 bytes, number of entries: 1
-rw-r--r-- 3.0 unx 6 tx stor 22-Sep-30 11:47 ../target.txt
1 file, 6 bytes uncompressed, 6 bytes compressed: 0.0%
$ unzip -: evil.zip # zipslipのチェックを無効化するために-:オプションを加える
Archive: evil.zip
replace ../target.txt? yes, no, All, None, rename: yes
extracting: ../target.txt
$ cat ../target.txt # 親ディレクトリのファイルが書き換えられている
PWNED
対策
ソフトウェアをアップデートする
現在は多くのライブラリで修正されている
ファイルパスを検証する
..を含んでいるパスや絶対パスを弾く
場合によってはシンボリックリンクも弾く
zipを参照
ツール
evilarc https://github.com/ptoomey3/evilarc
簡単にzipslipができるzip/tarファイルを作成できるツール
テクニック
Python tarfile.extractall
PythonのtarfileはZip Slipの対策をしていない
これはドキュメントで警告している https://docs.python.org/ja/3/library/tarfile.html#tarfile.TarFile.extractall
最近この問題が掘り返されている https://gigazine.net/news/20220922-python-old-bug-unpatched-15-years/
extarct()のようなAPIを提供していないライブラリ
自分でファイル抽出処理を書く必要があるライブラリは、自分で十分な検証をしてなければzipslipが可能
Golang archive/tar archive/zip など
シンボリックリンクによるディレクトリのジャンプ
root -> /というシンボリックリンクを先に作っておいて、
root/tmp/slipに書き込むことで/tmp/slipに書き込むことができる
チェックが甘い場合はこれで通る
資料
ZipSlipの影響を受けるソフトウェアまとめ https://github.com/snyk/zip-slip-vulnerability
Cloudflareのcdnjsにおける任意コード実行 https://blog.ryotak.me/post/cdnjs-remote-code-execution/