【发布时间】:2021-02-25 19:56:19
【问题描述】:
前言:如果 jq 无法做到以下几点,那么我完全接受它作为答案,并将尝试用 bash 强制执行此操作。
我有两个文件,其中包含一些 ID,通过一些按摩,应该能够将它们组合成一个文件。我也会添加一些内容(如输出所示)。本质上,“mitre_test”应该与“sys_id”进行比较。比较时,in2.json 中的“mitreid”在输出中变成了 technology_ID(通常是每个输出对象的统一字段)。
注意事项:
-
在 in1.json 中放置了一些垃圾“desc”值,以确保尽可能编程,并且在我使用的真实输入文件中实际上有许多垃圾输入。
-
一些 mitre_test 值具有对并且不在真实数组中。我可以拆分这些并将它们分开,但发现自己丢失了 in1.json 中的其他信息。
-
注意输出的“元数据”中包含来自 in1.json 的“数字”值,并以奇怪的方式存储(但接收工具需要的方式)。
in1.json
[
{
"test": "Execution",
"mitreid": "T1204.001",
"mitre_test": "90b"
},
{
"test": "Defense Evasion",
"mitreid": "T1070.001",
"mitre_test": "afa"
},
{
"test": "Credential Access",
"mitreid": "T1556.004",
"mitre_test": "14b"
},
{
"test": "Initial Access",
"mitreid": "T1200",
"mitre_test": "f22"
},
{
"test": "Impact",
"mitreid": "T1489",
"mitre_test": "fa2"
}
]
in2.json
[
{
"number": "REL0001346",
"desc": "apple",
"mitre_test": "afa"
},
{
"number": "REL0001343",
"desc": "pear",
"mitre_test": "90b"
},
{
"number": "REL0001366",
"desc": "orange",
"mitre_test": "14b,f22"
},
{
"number": "REL0001378",
"desc": "pineapple",
"mitre_test": "90b"
}
]
输出:
[{
"techniqueID": "T1070.001",
"tactic": "defense-evasion",
"score": 1,
"color": "",
"comment": "",
"enabled": true,
"metadata": [{
"name": "DET_ID",
"value": "REL0001346"
}],
"showSubtechniques": true
},
{
"techniqueID": "T1204.001",
"tactic": "execution",
"score": 1,
"color": "",
"comment": "",
"enabled": true,
"metadata": [{
"name": "DET_ID",
"value": "REL0001343"
},
{
"name": "DET_ID",
"value": "REL0001378"
}],
"showSubtechniques": true
},
{
"techniqueID": "T1556.004",
"tactic": "credential-access",
"score": 1,
"color": "",
"comment": "",
"enabled": true,
"metadata": [{
"name": "DET_ID",
"value": "REL0001366"
}],
"showSubtechniques": true
},
{
"techniqueID": "T1200",
"tactic": "initial-access",
"score": 1,
"color": "",
"comment": "",
"enabled": true,
"metadata": [{
"name": "DET_ID",
"value": "REL0001366"
}],
"showSubtechniques": true
}
]
我假设我需要在 mitre_test 上使用.mitre_test |= split(",")) 之类的东西进行一些拆分,并且我假设有一些连接,但这样做会导致数据丢失或数据混淆。您会注意到输出中的静态数据也存在,但可能很容易放入,因此问题不大。
编辑:减少了一些匹配 ID,以便在分析 in1 和 in2 文件时更容易查看。还简化了两个输入以具有相似的结构,以便以后更容易理解答案。
【问题讨论】:
-
我假设我需要首先将相似的值重命名为相同的值,然后将它们重命名为相同的值,然后
jq . in1.json in2.json并首先将类似的项目组合在一起。我将继续研究这个问题,并希望能回答我自己的问题,因为我知道这个问题比一般人更喜欢的问题更深入。 -
我越来越接近以下
jq . in4.json in3.json | jq '.[] |{number: .number, test: .test, mitreid: .mitreid, mitre_test: .mitre_test}' | jq -s '. |map(try(.mitre_test |= split(",")) // .)| .[] | [.number,.test,.mitreid] as $h | .mitre_test[] |$h + [.] | {DET_ID: .[0], tactic: .[1], techniqueID: .[2], mitre_test: .[3]}'