
“我的小鸟无处不在,即使在北方,它们也会向我低语最奇怪的故事。” —— 瓦里斯大人
Whispers 是一款静态代码分析工具,旨在解析各种常见数据格式,查找硬编码凭据和危险函数。Whispers 可以在命令行中运行,也可以集成到你的 CI/CD 流水线中。
Whispers 被设计为 结构化文本 解析器,而非代码解析器。
当前支持以下常用格式:
由于原生语言支持,Python3 文件被解析为抽象语法树(AST)。
以下语言文件被作为文本解析,并检查常见的变量声明和赋值模式:
pip3 install whispers
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
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 提供了多种配置选项。可以根据文件路径、键或值来包含/排除结果。文件路径规范被解释为 glob 模式。键和值支持正则表达式及其他参数。内置了一个默认配置文件,如果你不提供自定义文件,则会使用该默认配置。
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
规则指定了应从键值对中提取的具体内容。内置了多个常见规则,例如 AWS 密钥和密码,但该工具易于通过新规则扩展。
rules: 下定义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)
所有解析功能均通过插件实现。每个插件实现一个带有 pairs() 方法的类,该方法遍历文件并返回需要根据规则检查的键值对。
class PluginName:
def pairs(self, file):
yield "key", "value"