用 Bandit 靜態掃描工具,掃描 Python 專案中的安全性問題
Posted on Mar 24, 2019 in Python 模組/套件推薦 by Amo Chen ‐ 2 min read
這陣子看了 10 common security gotchas in Python and how to avoid them , 該文章主要介紹幾種撰寫 Python 程式時需注意的安全問題,例如處理來自外部的 XML, YAML 檔案等可能會面臨的安全性問題,相當值得一讀,保證精彩。
該文的最後也介紹一款 Python 的靜態掃描工具 Bandit , 幫助我們找出程式內可能的漏洞或問題。
Bandit 是由 PyCQA(Python Code Quality Authority) 所開發的專案,有用過 Pylint 的朋友們應該都會知道 PyCQA
。
接下來就一起試試如何使用 Bandit 找出程式的淺在問題吧!
本文環境
- Python 3.6.5
- Bandit 1.5.1
安裝 Bandit 的指令如下:
$ pip install bandit
下載 repo 作為掃描目標
首先,下載 [requests](git clone https://github.com/kennethreitz/requests) 作為試刀對象吧!
$ git clone https://github.com/kennethreitz/requests
接著使用以下指令掃描 requests/requests/
, 也就是 requests 套件主要程式碼的資料夾:
$ bandit -r requests/requests -f html -o bandit.report.html
上述指令參數的部分解說:
-r requests/requests
掃描requests/requests
內的所有資料夾及檔案-f html
將掃描報告的格式設定為html
-o bandit.report.html
將報告輸出至檔案bandit.report.html
執行完畢之後,以瀏覽器打開 bandit.report.html
即可看到報告內容:
報告中會顯示每個問題的嚴重程度(Severity), 可信度(Confidence), 檔案及有問題的部分程式碼作為參考,比較方便我們一一排除是否真有問題。
以 requests
套件為例,其報告顯示該套件使用到 MD5
弱安全性的雜湊(hash)演算法,並非是真正有安全性問題,因為該套件使用 MD5
演算法是為了實作 Digest access authentication ,所以並沒有問題。如果 MD5
雜湊演算法被運用在密碼的雜湊上,那麼就會有相當大的安全問題。
以上就是這次關於 Bandit 的簡單介紹,除了上述介紹到的參數外,歡迎大家翻閱 Bandit 文件 學習更多關於 Bandit 的用法!
References
https://hackernoon.com/10-common-security-gotchas-in-python-and-how-to-avoid-them-e19fbe265e03
https://github.com/PyCQA/bandit