Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add RuleTitle to ttp-summary command output #83

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/takajopkg/ttpSummary.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ proc readJsonFromFile(filename: string): JsonNode =
file.close()
result = parseJson(content)

proc compareArrays(a, b: array[4, string]): int =
proc compareArrays(a, b: array[5, string]): int =
for i in 0..<4:
if a[i] < b[i]:
return -1
Expand Down Expand Up @@ -58,7 +58,7 @@ proc ttpSummary(output: string = "", quiet: bool = false, timeline: string) =
let attack = readJsonFromFile("mitre-attack.json")
var
bar: SuruBar = initSuruBar()
seqOfResultsTables: seq[array[4, string]]
seqOfResultsTables: seq[array[5, string]]

bar[0].total = totalLines
bar.setup()
Expand All @@ -77,15 +77,16 @@ proc ttpSummary(output: string = "", quiet: bool = false, timeline: string) =
let tac = tac_no[dat] & dat
let tec = res["Technique"].getStr()
let sub = res["Sub-Technique"].getStr()
seqOfResultsTables.add([com, tac, tec, sub])
let rul = jsonLine["RuleTitle"].getStr()
seqOfResultsTables.add([com, tac, tec, sub, rul])
except CatchableError:
continue
seqOfResultsTables.sort(compareArrays)
bar.finish()

let header = ["Computer", "Tactic", "Technique", "Sub-Technique", "Count"]
var prev = ["","","",""]
let header = ["Computer", "Tactic", "Technique", "Sub-Technique", "RuleTitle", "Count"]
var prev = ["","","","",""]
var count = 1
var ruleStr = initHashSet[string]()
if output != "":
# Open file to save results
var outputFile = open(output, fmWrite)
Expand All @@ -95,14 +96,17 @@ proc ttpSummary(output: string = "", quiet: bool = false, timeline: string) =

## Write contents
for arr in seqOfResultsTables:
if arr == prev:
ruleStr.incl(arr[4])
if arr[0..<4] == prev[0..<4]:
count += 1
continue
for i, val in enumerate(arr):
for i, val in enumerate(arr[0..<4]):
outputFile.write(escapeCsvField(val) & ",")
outputFile.write(escapeCsvField(ruleStr.mapIt($it).join(", ")) & ",")
outputFile.write(escapeCsvField(intToStr(count)))
prev = arr
count = 1
ruleStr = initHashSet[string]()
outputFile.write("\p")
outputFile.close()
let fileSize = getFileSize(output)
Expand All @@ -113,12 +117,14 @@ proc ttpSummary(output: string = "", quiet: bool = false, timeline: string) =
var table: TerminalTable
table.add header
for arr in seqOfResultsTables:
if arr == prev:
ruleStr.incl(arr[4])
if arr[0..<4] == prev[0..<4]:
count += 1
continue
table.add arr[0], arr[1], arr[2], arr[3], intToStr(count)
table.add arr[0], arr[1], arr[2], arr[3], ruleStr.mapIt($it).join(", "), intToStr(count)
prev = arr
count = 1
ruleStr = initHashSet[string]()
table.echoTableSepsWithStyled(seps = boxSeps)

echo ""
Expand Down