使用osquery追踪被盗代码签名证书
Mike Myers | 2017年10月10日
近期,270万台Windows电脑感染了通过CCleaner软件更新机制传播的恶意软件,该恶意软件使用了从CCleaner开发商处窃取的证书进行签名。得益于同事Alessandro Gario提交的PR(新增了Windows可执行文件代码签名验证功能),现在通过osquery可以轻松检测此类签名恶意软件。
代码签名恶意软件的威胁
代码签名本应防止可执行文件被恶意篡改,并允许用户选择是否运行来自非信任源的文件。但在Windows等通用计算平台上,第三方软件供应商需自行保护其代码签名证书。攻击者只需窃取一个证书就能为恶意软件披上合法外衣。从著名的Stuxnet事件开始,使用被盗证书签名的恶意软件已成为犯罪组织和国家级攻击的常规手段。
对防御者而言,代码签名证书可作为事件响应中的关键指标:当某证书确认为被盗后,可全网搜索使用该证书签名的文件。这种检测方式具有零误报率的优势。
osquery的Authenticode签名验证实现
osquery通过"表"的形式添加新功能,将系统信息抽象为SQL表。Alessandro的PR为Windows平台新增了authenticode
虚拟表,包含以下字段:
签名验证通过调用系统API WinVerifyTrust()
实现。验证结果可能为:
missing
:缺少签名
invalid
:无效签名
untrusted
:未经验证的签名
distrusted
:用户明确不信任的签名
valid
:有效但未明确信任的签名
trusted
:用户信任的有效签名
实战SQL查询示例
示例1:查找使用被盗证书签名的文件
1
2
3
4
5
6
7
8
9
10
|
SELECT files.path, authenticode.subject_name,
authenticode.serial_number,
authenticode.result AS status
FROM (
SELECT * FROM file
WHERE directory = "C:\Program Files\CCleaner"
) AS files
LEFT JOIN authenticode
ON authenticode.path = files.path
WHERE authenticode.serial_number == "4b48b27c8224fe37b17a6a2ed7a81c9f";
|
示例2:查找受影响厂商签发但未使用新证书的文件
1
2
3
4
5
6
7
8
9
10
11
|
SELECT files.path, authenticode.subject_name,
authenticode.serial_number,
authenticode.result AS status
FROM (
SELECT * FROM file
WHERE directory = "C:\Program Files\CCleaner"
) AS files
LEFT JOIN authenticode
ON authenticode.path = files.path
WHERE authenticode.subject_name LIKE "%Piriform%"
AND authenticode.serial_number != "52b6a81474e8048920f1909e454d7fc0";
|
示例3:结合代码签名与文件哈希的记录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
SELECT files.path AS path,
authenticode.subject_name AS subject_name,
authenticode.serial_number AS serial_number,
authenticode.result AS status,
hashes.sha256 AS sha256
FROM (
SELECT * FROM file
WHERE directory = "C:\Program Files\CCleaner"
) AS files
LEFT JOIN authenticode
ON authenticode.path = files.path
LEFT JOIN hash AS hashes
ON hashes.path = files.path
WHERE authenticode.subject_name LIKE "%Piriform%"
AND authenticode.serial_number != "52b6a81474e8048920f1909e454d7fc0";
|
实际应用建议
这些查询可在osquery交互式shell中运行(适合事件响应),也可通过osqueryd守护进程定时执行(用于持续检测)。建议将输出接入LogStash/ElasticSearch等日志分析系统。
osquery的SQL语法使其成为灵活的系统信息检索工具,能够快速定制符合特定需求的查询。除Authenticode验证外,它还可用于初始恶意软件检测和传播路径识别等场景。