-
Notifications
You must be signed in to change notification settings - Fork 1
/
WhoIsWhoAPT.py
305 lines (277 loc) · 10.5 KB
/
WhoIsWhoAPT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from cgi import print_arguments
import json, math, requests, os, argparse
import pandas as pd
import matplotlib.pyplot as plt
from collections import Counter
from stix2 import MemoryStore, Filter
#WhoIsWhoAPT 1.0 19/09/2022. Credits: Javier Muñoz
colour1 = '#a1d99b'
colour2 = '#4597c8'
colour3 = '#a644b1'
aptnames = []
compRes = {}
datadirname = os.path.dirname(__file__) + "\\resources\\data"
def get_data_from_branch(domain):
"""get the ATT&CK STIX data from MITRE/CTI. Domain should be 'enterprise-attack', 'mobile-attack' or 'ics-attack'. Branch should typically be master."""
stix_json = requests.get(f"https://raw.githubusercontent.com/mitre-attack/attack-stix-data/master/{domain}/{domain}.json").json()
return MemoryStore(stix_data=stix_json["objects"])
def similarity(c1, c2):
terms = set(c1).union(c2)
dotprod = sum(c1.get(k, 0) * c2.get(k, 0) for k in terms)
magA = math.sqrt(sum(c1.get(k, 0)**2 for k in terms))
magB = math.sqrt(sum(c2.get(k, 0)**2 for k in terms))
return dotprod / (magA * magB)
def listTtpxApt(thesrc):
groups = thesrc.query([ Filter("type", "=", "intrusion-set") ])
result = {}
for element in groups:
aptnames.append(element.name)
tacticasapt = []
relationships = thesrc.query([ Filter("type", "=", "relationship"),
Filter("source_ref", "=", element.id)
])
for relation in relationships:
attackPattern = thesrc.query([ Filter("type", "=", "attack-pattern"),
Filter("id", "=", relation.target_ref),
])
if attackPattern:
if ('x_mitre_deprecated' in attackPattern[0]):
if not (attackPattern[0]['x_mitre_deprecated']== True):
ttptypetec = [(attackPattern[0]['external_references'][0]['external_id']), (attackPattern[0]['kill_chain_phases'][0]['phase_name']) ]
tacticasapt.append(ttptypetec)
else:
ttptypetec = [(attackPattern[0]['external_references'][0]['external_id']), (attackPattern[0]['kill_chain_phases'][0]['phase_name']) ]
tacticasapt.append(ttptypetec)
result[element.name] = tacticasapt
os.makedirs(datadirname, exist_ok=True)
with open (datadirname + '\\ttpsMitre.txt', "w+") as f:
f.write(json.dumps(result))
return result
def comparationJsonApts(group, compApt):
b0 = group.get(compApt)
unzippeda0 = list(zip(*b0))
b1 = Counter(set(unzippeda0[0]))
for apt in group:
if group[apt]:
a0 = group.get(apt)
unzippeda0 = list(zip(*a0))
a1 = Counter(set(unzippeda0[0]))
# Deprecated json load
# with open(os.path.dirname(__file__)+ '\\' + sys.argv[1]) as f:
# techdata = json.load(f) ['techniques']
# loadedTech = []
# for x in techdata:
# loadedTech.append(x['techniqueID'])
# b1 = Counter(set(loadedTech))
result = similarity(a1, b1)
compRes[apt] = round(result, 5)
sortedCompRes = sorted(compRes.items(), key=lambda x: x[1], reverse=True)
blankList = []
for x in sortedCompRes[1:16]:
blankList.append(list(x))
for idx, y in enumerate(blankList):
x0 = group.get(y[0])
unzippedx0 = list(zip(*x0))
x1 = len((set(unzippedx0[0])))
x2 = Counter(set(unzippedx0[0]))
terms = len(set(x2).intersection(b1))
fractionString = "{}/{}".format(terms, x1)
blankList[idx] = (y[0], y[1], fractionString)
print(pd.DataFrame(blankList,columns=['Group Name','Similarity','Ttps Covert']))
df = pd.DataFrame(list(sortedCompRes[1:16]),columns=['Group Name','Similarity'])
df.plot(x ='Group Name', y='Similarity', kind = 'bar')
plt.tight_layout()
plt.show()
exit()
def addCustomtoTtpsMitre(ttpsmitre):
customApt = {}
for filename in os.listdir(os.path.dirname(__file__) + "\\resources"):
if filename.endswith('.json'):
with open(os.path.dirname(__file__) + "\\resources\\" + filename) as f:
data = json.load(f)
techdata = data ['techniques']
loadedName = data ['name']
loadedTech = []
for x in techdata:
if x['color']:
ttptypetec = [x['techniqueID'], x['tactic']]
loadedTech.append(ttptypetec)
customApt[loadedName] = loadedTech
return (ttpsmitre | customApt)
tecnicas= []
data = {
"name": "placeholder",
"versions": {
"attack": "11",
"navigator": "4.6.5",
"layer": "4.3"
},
"domain": "enterprise-attack",
"description": "",
"filters": {
"platforms": [
"Linux",
"macOS",
"Windows",
"PRE",
"Containers",
"Network",
"Office 365",
"SaaS",
"Google Workspace",
"IaaS",
"Azure AD"
]
},
"sorting": 0,
"layout": {
"layout": "side",
"aggregateFunction": "average",
"showID": False,
"showName": True,
"showAggregateScores": False,
"countUnscored": False
},
"hideDisabled": False,
"techniques": tecnicas,
"gradient": {
"colors": [
"#ff6666ff",
"#ffe766ff",
"#8ec843ff"
],
"minValue": 0,
"maxValue": 100
},
"legendItems": [],
"metadata": [],
"links": [],
"showTacticRowBackground": False,
"tacticRowBackground": "#dddddd",
"selectTechniquesAcrossTactics": True,
"selectSubtechniquesWithParent": False
}
def creacionJson(group, apt, col1):
a1 = group.get(apt)
for x in a1:
var = {
"techniqueID": "placeholder",
"tactic": "placeholder",
"color": col1,
"comment": "",
"enabled": True,
"metadata": [],
"links": [],
"showSubtechniques": False
}
var["techniqueID"] = x[0]
var["tactic"] = x[1]
tecnicas.append(var)
docName = "{}".format(apt)
data['name'] = docName
with open(os.path.dirname(__file__) + "\\" + docName + ".json", "w") as write_file:
json.dump(data, write_file, indent=4)
def creacionJsoncomparenoJson(apt1, apt2, group, col1, col2, col3):
a1 = group.get(apt1)
a2 = group.get(apt2)
ttpsa1 = []
ttpsa2 = []
ttpsa3 = []
for i in a1:
if i in a2:
ttpsa3.append(i)
else:
ttpsa1.append(i)
for i in a2:
if i not in a1:
ttpsa2.append(i)
for x in ttpsa1:
var = {
"techniqueID": "placeholder",
"tactic": "placeholder",
"color": col1,
"comment": "",
"enabled": True,
"metadata": [],
"links": [],
"showSubtechniques": False
}
var["techniqueID"] = x[0]
var["tactic"] = x[1]
tecnicas.append(var)
for x in ttpsa2:
var = {
"techniqueID": "placeholder",
"tactic": "placeholder",
"color": col2,
"comment": "",
"enabled": True,
"metadata": [],
"links": [],
"showSubtechniques": False
}
var["techniqueID"] = x[0]
var["tactic"] = x[1]
tecnicas.append(var)
for x in ttpsa3:
var = {
"techniqueID": "placeholder",
"tactic": "placeholder",
"color": col3,
"comment": "",
"enabled": True,
"metadata": [],
"links": [],
"showSubtechniques": False
}
var["techniqueID"] = x[0]
var["tactic"] = x[1]
tecnicas.append(var)
docName= "{} vs {}".format(apt1, apt2)
data['name'] = docName
with open(os.path.dirname(__file__) + "\\" + docName + ".json", "w") as write_file:
json.dump(data, write_file, indent=4)
#****************************************************************************************************************************************
parser = argparse.ArgumentParser('WhoIsWhoAPT', description='WhoIsWhoAPT', usage= 'python whoiswhoapt.py [-c <APT>] | [-l <APT>] | [-v <APT1> <APT2>] [-col <APT1 Color> <APT2 Color> <Match Color>]', epilog='Credits: Javier Muñoz')
parser.add_argument('-c', '--compare', help='Compare an APT with all the others APTs', nargs=1, metavar= 'APT')
parser.add_argument('-v', '--versus', help='Compare two APTs and extract the comparison matrix', nargs=2, metavar= ('APT', 'APT2'))
parser.add_argument('-l', '--layer', help='Create a layer with selected APT\'s TTPs', nargs=1, metavar= 'APT')
parser.add_argument('-col', '--colours', help='Choose the colors with which the data will be represented in the layer. Most be a color hexcode.', nargs='+')
args = parser.parse_args()
if os.path.exists(datadirname + '\\ttpsMitre.txt'):
ttpsMitre = json.load(open(datadirname + '\\ttpsMitre.txt'))
else:
print('APT database not found. Downloading...')
src = get_data_from_branch("enterprise-attack")
print('Database downloaded')
ttpsMitre = listTtpxApt(src)
completeApt = addCustomtoTtpsMitre(ttpsMitre)
if args.versus and (args.compare or args.layer):
print('Options Versus, Compare and Layer are mutually exclusive, you can only choose one per execution')
elif args.compare and (args.versus or args.layer):
print('Options Versus, Compare and Layer are mutually exclusive, you can only choose one per execution')
elif args.compare:
comparationJsonApts(completeApt, args.compare[0])
elif args.layer:
if args.colours:
creacionJson(completeApt, args.layer[0], args.colours[0])
print(args.layer[0], 'layer created')
exit()
creacionJson(completeApt, args.layer[0], colour1)
print(args.layer[0], 'layer created')
exit()
elif args.versus:
if args.colours and (len(args.colours) == 3):
creacionJsoncomparenoJson(args.versus[0], args.versus[1], completeApt, args.colours[0], args.colours[1], args.colours[2])
print(args.versus[0], 'vs', args.versus[1], 'layer created')
exit()
elif args.colours:
print("With VERSUS you need to specify 3 colous")
exit()
creacionJsoncomparenoJson(args.versus[0], args.versus[1], completeApt, colour1, colour2, colour3)
print(args.versus[0], 'vs', args.versus[1], 'layer created')
exit()
#help
else:
parser.print_help()
exit()