如何使用Whispers識別靜態(tài)結構化文本中的硬編碼敏感信息
關于Whispers
Whispers是一款功能強大的靜態(tài)代碼分析工具,該工具可以幫助廣大研究人員解析各種常見的數據格式,并搜索硬編碼憑證和危險函數。Whispers支持在命令行終端中運行,或者也可以將其集成到CI/CD管道中。
檢測功能
- 密碼
- API令牌
- AWS密鑰
- 私鑰
- 憑證哈希
- 身份認證令牌
- 危險函數
- 敏感文件
支持的格式
Whispers本質上來說是一款結構化的問版本解析工具,而不是一個代碼分析工具。
下面列出的是當前版本Whispers支持的數據格式:
- YAML
- JSON
- XML
- .npmrc
- .pypirc
- .htpasswd
- .properties
- pip.conf
- conf / ini
- Dockerfile
- Dockercfg
- Shell scripts
- Python3
Python3文件會以AST進行解析,因為這是原生語言支持。
聲明和賦值格式
該工具可以將下列語言文件解析為文本,并檢測常見的變量聲明和賦值模式:
- JavaScript
- Java
- Go
- PHP
特殊格式支持
- AWS憑證文件
- JDBC連接字符串
- Jenkins配置文件
- SpringFramework配置文件
- Java屬性文件
- Dockercfg注冊認證文件
- GitHub令牌
工具安裝
通過PyPI安裝:
- pip3 install whispers
GitHub安裝:
- git clone https://github.com/Skyscanner/whispers
- cd whispers
- make install
工具使用
命令行接口:
- whispers --help
- whispers --info
- whispers source/code/fileOrDir
- whispers --config config.yml source/code/fileOrDir
- whispers --output /tmp/secrets.yml source/code/fileOrDir
- whispers --rules aws-id,aws-secret source/code/fileOrDir
- whispers --severity BLOCKER,CRITICAL source/code/fileOrDir
- whispers --exitcode 7 source/code/fileOrDir
Python:
- from whispers.cli import parse_args
- from whispers.core import run
- src = "tests/fixtures"
- configfile = "whispers/config.yml"
- args = parse_args(["-c", configfile, src])
- for secret in run(args):
- print(secret)
工具配置
Whispers工具支持多種配置選項,我們可以根據需要來配置是否在結果中互毆文件路徑、密鑰或其他值等。config.yml的參考格式如下:
- include:
- files:
- - "**/*.yml"
- exclude:
- files:
- - "**/test/**/*"
- - "**/tests/**/*"
- keys:
- - ^foo
- values:
- - bar$
- rules:
- starks:
- message: Whispers from the North
- severity: CRITICAL
- value:
- regex: (Aria|Ned) Stark
- ignorecase: True
最快的配置方法就是將config.yml文件拷貝至一個新的文件中,然后直接將其以參數形式傳遞給Whispers:
- whispers --config config.yml --rules starks src/file/or/dir
自定義規(guī)則
我們可以通過下列方式,在whispers/rules文件中添加和編輯自己的自定義規(guī)則:
- rule-id: # unique rule name
- description: Values formatted like AWS Session Token
- message: AWS Session Token # report will show this message
- severity: BLOCKER # one of BLOCKER, CRITICAL, MAJOR, MINOR, INFO
- key: # specify key format
- regex: (aws.?session.?token)?
- ignorecase: True # case-insensitive matching
- value: # specify value format
- regex: ^(?=.*[a-z])(?=.*[A-Z])[A-Za-z0-9\+\/]{270,450}$
- ignorecase: False # case-sensitive matching
- minlen: 270 # value is at least this long
- isBase64: True # value is base64-encoded
- isAscii: False # value is binary data when decoded
- isUri: False # value is not formatted like a URI
- similar: 0.35 # maximum allowed similarity between key and value
- # (1.0 being exactly the same)
插件
Whispers中所有的解析功能都是通過插件實現的,每一個插件都會使用pairs()方法實現一個類,并返回匹配規(guī)則的鍵值對:
- class PluginName:
- def pairs(self, file):
- yield "key", "value"
項目地址
Whispers:【GitHub傳送門】