1

一些不太容易注意到的 Python 安全性問題

 2 years ago
source link: https://blog.gslin.org/archives/2022/02/03/10525/%e4%b8%80%e4%ba%9b%e4%b8%8d%e5%a4%aa%e5%ae%b9%e6%98%93%e6%b3%a8%e6%84%8f%e5%88%b0%e7%9a%84-python-%e5%ae%89%e5%85%a8%e6%80%a7%e5%95%8f%e9%a1%8c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一些不太容易注意到的 Python 安全性問題

前幾天看到「10 Unknown Security Pitfalls for Python」這篇,講 Python ecosystem 裡面的一些設計造成的安全性問題,裡面很多東西都很有趣,而且有些算是共通的,其他程式語言也會中獎...

第一個是開發者用 assert() 確認權限,但 assert() 的設計是 debug 用的功能,所以可以預期在 optimization 後會被拿掉 (其他程式語言也有類似的功能),而導致權限沒被檢查而產生 security incident。

第二個是 os.makedirs() 所指定的 mode,依照說明看起來是 Python 改變行為了,變成只有最終的那個目錄照著設定:

In Python < 3.6, the folders A, B and C are each created with permission 700. However, in Python > 3.6, only the last folder C has permission 700 and the other folders A and B are created with the default permission 755.

這個設計就有點討厭了,我測了一下 umask 077 的情況下在 shell 下執行 mkdir a/b/c,所有的目錄都會是 700

第三個是串檔名的 os.path.join(),遇到 / 開頭的部份會把前面的部份都拔掉,也就是這樣:(嗯... 這樣搞的嗎 XDDD)

>>> os.path.join('var', 'lib', 'tmp', 'abc.png')
'var/lib/tmp/abc.png'
>>> os.path.join('var', 'lib', 'tmp', '/etc/shadow')
'/etc/shadow'

第四個就是蠻標準的 variable injection 了。

第五個算是每次處理 .zip 檔都很頭痛的問題,不限於 Python,因為壓縮檔裡面可能會有 ../../ 開頭這種看起來就很邪惡的檔案名稱,只要有安全意識的工程師都覺得處理起來很麻煩的東西...

第六個一看就是壞東西,想要用黑名單擋 injection 一定會有更多有創意的方法打進來。比較特別的是這邊有提到在 Python 裡 re.search()re.match() 的差異,這點真的是寫起來有時後會忽略掉的...

第七與第八個都是 Unicode 相關的問題,也是個超級麻煩的東西,大原則是先 normalize 再 escape,但還是有點見招拆招去看...

第九個也是類似的問題,沒有先 normalize 再確認,不過一般機器都會有 private ip address,還是可以試著打... 這個有機會用架構面去解決,像是用 MQ 架設 proxy service,隔出另外一區來跑;或是已經有 zero trust 的架構,內部網路也是要帶 token 跑來跑去,可以減少一些傷害...

第十個算是 Python 自己的歷史因素,早期同時吃 &; 當作 separator,而非只吃 &x-www-form-urlencoded (HTML spec 內定義的標準):

In Python < 3.7 the function urllib.parse.parse_qsl allows the use of the ; and & characters as separators for URL query variables.

查了一下官方文件可以看到 3.10 之後才變成 x-www-form-urlencoded

Changed in version 3.10: Added separator parameter with the default value of &. Python versions earlier than Python 3.10 allowed using both ; and & as query parameter separator. This has been changed to allow only a single separator key, with & as the default separator.

有些應該是有安全意識時會去翻 spec 看看就可以避開的,但有些就真的是蠻雷的 XDDD

Related

KKBOX 徵人:軟體開發中心 (i.e. Client Team)

索引: KKBOX 徵人 (Overview) KKBOX 徵人:平台營運處 (API Team) KKBOX 徵人:軟體開發中心 (i.e. Client Team) 在寫自家的介紹時,特地跑去跟軟體開發中心的主管要 Client Team 的介紹,人家交稿的速度快多了... Q_Q Anyway,這篇是由 Client Team 的主管所寫的介紹,一樣是所有的部門都有職缺 (人力銀行上未必有開),有興趣的可以提供 resume 到 recruit at kkbox.com 這個信箱。 軟體開發中心 (Application Develop) 在 KKBOX 裡頭,我們還蠻習慣以老派的 Client/Server Side 來稱呼不同技術背景的開發人員,Client Side 說穿了就是開發 App 的那群人,只要你喊得出來的主要平台,大概就是我們負責的。 軟體開發隨著功能的演進,程式碼就會變得又肥又大,自然免不了些壞味道,面臨設計架構的難題,我們希望內部開發者能夠清楚三件事情: Design Pattern,Unit Test,和 Refactoring。上述觀念應當不用多說什麼,幾乎都變成顯學了。我們期盼透過一些原則和流程來讓開發工作變得不會那麼難以維護。 Client Side 目前共有四個…

April 21, 2015

In "Computer"

Perl on Google App Engine

Python 是第一個可以在 Google App Engine (GAE) 上執行的程式語言,而下一個很有可能是 Perl。 Brad Fitzpatrick 在他的 Blog 上說,他被 GAE team 允許對外公佈「我可以使用 20% 的時間開發 Perl on GAE」:Perl on App Engine。 這是 GAE 支援其他程式語言的消息中,第一個被正式公開的。在 Brad Fitzpatrick 的文章裡面有一些藍圖,關於他大概會怎麼做的想法。我們應該可以期待他認真起來的戰鬥力 :p PS:Brad Fitzpatrick 是 LiveJournal 的創辦人、memcached 的作者、OpenID 的制定人。

July 23, 2008

In "Cloud"

在 VirtualBox 內安裝 ArchLinux 的紀錄

Debian 用一陣子後,發現 Python 2.6 在 lenny 的 backports 也沒有,一定得到 squeeze (目前的 testing) 找出來用,但如此一來就會有很多問題 (像是 testing 的 python library 使用 stable 的 C library),所以一直在覺得很煩... 另外,自己架設 APT 的熟練程度一直卡在沒有自動化工具,所以一直想要試試 Linux 上其他的 package system,但又不想要用 RPM,所以就挑上 ArchLinux 了... 不過在 VirtualBox 內裝 ArchLinux 遇到一些怪問題,總算是弄的差不多,紀錄起來讓其他人有機會少踩一些雷... 首先是抓 ISO image,最新的版本可以在 http://ftp.tku.edu.tw/Linux/ArchLinux/iso/latest/ (這是淡江大學的 mirror) 這邊抓到,我在寫這篇時是抓 archlinux-2009.08-netinstall-i686.iso,抓完之後先用工具確認檔案的 md5 與 sha1 有沒有問題,避免檔案壞掉造成安裝過程或結果異常。…

March 22, 2010

In "Computer"

a611ee8db44c8d03a20edf0bf5a71d80?s=49&d=identicon&r=gAuthor Gea-Suan LinPosted on February 3, 2022Categories Computer, Library, Murmuring, Programming, Security, SoftwareTags practice, programming, python, security

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK