大家好
这里是热(归)爱(心)工(似)作(箭)的超级牛
新的一年即将到来
在这里提前祝大家
身体健康,万事如意,新年快乐!
// 获取依赖名以 fastjson 结尾的依赖版本
__dependency__.*fastjson.version as $ver;
// 获取依赖名以 fastjson 结尾所在的依赖文件
__dependency__.*fastjson.filename as $file;
__dependency__.*fastjson.version as $ver;:筛选依赖名称以 fastjson 结尾的依赖版本,并将结果存储在变量 $ver 中。
// 检查版本是否在 1 < version <= 2 范围内
$version in (1,2]
// 检查版本是否在 1.0.0 < version <= 2.0.0 范围内
$version in (1.0.0,2.0.0]
// 检查版本是否在 1.2.3-beta < version <= 2.2.1-beta 范围内
$version in (1.2.3-beta,2.2.1-beta]
// 检查版本是否在 [1.1,1.3] 或 [2.2,2.3] 或 [3.2,3.3] 范围内
$version in [1.1,1.3] || [2.2,2.3] || [3.2,3.3]
$version ?{version_in:(1,2]} // 版本号是否在 1 < version <= 2 范围内
$version ?{version_in:(1.0.0,2.0.0]} // 版本号是否在 1.0.0 < version <= 2.0.0 范围内
$version ?{version_in:(1.2.3-beta,2.2.1-beta]} // 版本号是否在 1.2.3-beta < version <= 2.2.1-beta 范围内
$version ?{version_in:[1.1,1.3] || [2.2,2.3] || [3.2,3.3]} // 版本号是否在多个范围内
总结一下:version_in 语法
使用 in 关键字
版本范围表示:"]" 闭 ")"开
版本范围之间可以通过 "||" 并集多个不连续的版本范围。
__dependency__.*alibaba*fastjson.version as $ver;
$ver in (,1.2.68] as $vuln_1_2_68;
alert $vuln_1_2_68 for {
message: 'SCA: com.alibaba.fastjson <= 1.2.68 RCE Mid to exploit',
level: high,
}
下面是一个简单案例的使用
type CVE struct {
CVE string
CWE string
ProblemType []byte
References []byte
TitleZh string
Solution string
DescriptionMain string
DescriptionMainZh string
Descriptions []byte
Vendor string
Product string
CPEConfigurations []byte
...
}
CVE :CVE编号
for i in cve.QueryEx(cve.product("fastjson")){ // 搜索产品为 fastjson 的CVE
println(i.CVE)
println(string(i.CPEConfigurations))
}
/*
CVE-2017-18349
{
"CVE_data_version": "4.0",
"nodes": [
{
"operator": "OR",
"cpe_match": [
{
"vulnerable": true,
"cpe23Uri": "cpe:2.3:a:pippo:pippo:1.11.0:*:*:*:*:*:*:*",
"versionStartExcluding": "",
"versionEndExcluding": "",
"versionStartIncluding": "",
"versionEndIncluding": ""
},
{
"vulnerable": true,
"cpe23Uri": "cpe:2.3:a:alibaba:fastjson:*:*:*:*:*:*:*:*",
"versionStartExcluding": "",
"versionEndExcluding": "1.2.25",
"versionStartIncluding": "",
"versionEndIncluding": ""
}
],
"children": []
}
]
}
CVE-2022-25845
{
"CVE_data_version": "4.0",
"nodes": [
{
"operator": "OR",
"cpe_match": [
{
"vulnerable": true,
"cpe23Uri": "cpe:2.3:a:alibaba:fastjson:*:*:*:*:*:*:*:*",
"versionStartExcluding": "",
"versionEndExcluding": "1.2.83",
"versionStartIncluding": "",
"versionEndIncluding": ""
}
],
"children": []
},
{
"operator": "OR",
"cpe_match": [
{
"vulnerable": true,
"cpe23Uri": "cpe:2.3:a:oracle:communications_cloud_native_core_unified_data_repository:22.2.0:*:*:*:*:*:*:*",
"versionStartExcluding": "",
"versionEndExcluding": "",
"versionStartIncluding": "",
"versionEndIncluding": ""
}
],
"children": []
}
]
}
*/
product = cli.String("product",cli.setRequired(true),cli.setVerboseName("产品名"))
cli.check()
matchToVersionRange = func(match, wantproduct) {
/*
如果cpe_match的CPEuri有指定的版本,那么则代表其对应的版本是一个特定的指定版本。
若未指定版本则依据其他的versionStartExcluding等数据划定一个版本范围。
*/
cpeArray = str.Split(match["cpe23Uri"], ":")
if len(cpeArray) < 6 {
return
}
if cpeArray[4] != wantproduct {
return
}
version = cpeArray[5]
if version != "*"{
return f"[${version},${version}]"
}
versionStart = ""
startBoundary = "("
versionEnd = ""
endBounday = ")"
if match["versionStartExcluding"] != "" {
versionStart = match["versionStartExcluding"]
}else if match["versionStartIncluding"] != "" {
versionStart = match["versionStartIncluding"]
startBoundary = '['
}
if match["versionEndExcluding"] != "" {
versionEnd = match["versionEndExcluding"]
}else if match["versionEndIncluding"] != "" {
versionEnd = match["versionEndIncluding"]
endBounday = "]"
}
return f"${startBoundary}${versionStart},${versionEnd}${endBounday}"
}
cveInfoToAlert = func (cveInfo) {
serverity = str.ToLower(cveInfo.Severity)
return f`{
title: 'CVE Compliance Check ${cveInfo.CVE}',
cve: "${cveInfo.CVE}"
level: ${serverity},
}
`
}
rule = f"__dependency__.*${product}.version as \$ver;"
for i in cve.QueryEx(cve.product(product)){
alertMessage = cveInfoToAlert(i)
versionRangeList = []
cpeMap =json.loads(i.CPEConfigurations)
for node in cpeMap["nodes"] {
for match in node["cpe_match"] {
versionRange = matchToVersionRange(match, product)
if versionRange != "" {
versionRangeList = append(versionRangeList, versionRange)
}
}
}
if len(versionRangeList) < 1 {
continue
}
versionCollect = str.Join(versionRangeList, " || ")
alertVerName := "$"+str.Replace(i.CVE, "-", "_", -1)
rule +=f`\$ver in ${versionCollect} as ${alertVerName}
alert ${alertVerName} for ${alertMessage}
`
}
yakit.Text(rule)
生成一个fastjson的CVE合规规则
__dependency__.*fastjson.version as $ver;
$ver in (,1.2.25) as $CVE_2017_18349
alert $CVE_2017_18349 for {
title: 'CVE Compliance Check CVE-2017-18349',
cve: "CVE-2017-18349"
level: high,
}
$ver in (,1.2.83) as $CVE_2022_25845
alert $CVE_2022_25845 for {
title: 'CVE Compliance Check CVE-2022-25845',
cve: "CVE-2022-25845"
level: medium,
}
来到代码审计页面编译好项目,运行上述脚本获取到的规则。可以看到检查出了 CVE-2022-25845 ,此CVE命中条件版本低于 1.2.83,而Lab的版本为 1.2.37,符合预期。
__dependency__.*xstream.version as $ver;$ver in (,1.4.6] || [1.4.10,1.4.10] as $CVE_2013_7285
alert $CVE_2013_7285 for {
title: 'CVE Compliance Check CVE-2013-7285',
cve: "CVE-2013-7285"
level: high,
}
....
$ver in (,1.4.19] as $CVE_2022_40152
alert $CVE_2022_40152 for {
title: 'CVE Compliance Check CVE-2022-40152',
cve: "CVE-2022-40152"
level: high,
}
...
再在代码审计中使用生成的规则,可以看到产出了多个CVE检查的提示
END
YAK官方资源
Yak 语言官方教程:
https://yaklang.com/docs/intro/
Yakit 视频教程:
https://space.bilibili.com/437503777
Github下载地址:
https://github.com/yaklang/yakit
Yakit官网下载地址:
https://yaklang.com/
Yakit安装文档:
https://yaklang.com/products/download_and_install
Yakit使用文档:
https://yaklang.com/products/intro/
常见问题速查:
https://yaklang.com/products/FAQ