-
Notifications
You must be signed in to change notification settings - Fork 29
/
attack2jira.py
282 lines (229 loc) · 10.7 KB
/
attack2jira.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
from attackcti import attack_client
import json, sys, argparse, traceback
from getpass import getpass
from lib.jirahandler import JiraHandler
from argparse import RawTextHelpFormatter
class Attack2Jira:
jirahandler = None
def __init__(self, url, username, password):
jirahandler = JiraHandler(url, username, password)
self.jirahandler = jirahandler
def get_attack_techniques(self):
# Deprecated, keeping just in case
try:
print ("[*] Obtaining ATT&CK's techniques...")
client = attack_client()
all_enterprise = client.get_enterprise()
techniques = []
for technique in all_enterprise['techniques']:
tech= json.loads(technique.serialize())
# avoid bringing in the revoked techniques
if not 'revoked' in tech.keys():
techniques.append(tech)
print ("[!] Done!")
return techniques
except:
traceback.print_exc(file=sys.stdout)
print ("[!] Error connecting to Att&ck's API !")
return
def create_attack_techniques(self, key):
techniques = self.get_attack_techniques()
jiraclient = self.jirahandler
print ("[*] Creating Jira issues for ATT&CK's techniques...")
for technique in techniques:
try:
custom_fields=self.jirahandler.get_custom_fields()
name = technique['name']
id = technique['external_references'][0]['external_id']
url = technique['external_references'][0]['url']
tactic = technique['kill_chain_phases'][0]['phase_name']
description = technique['description']
# some techniques dont have the field populated
if 'x_mitre_data_sources' in technique.keys(): datasources = technique['x_mitre_data_sources']
else: datasources = []
ds_payload=[]
for ds in datasources: ds_payload.append({'value':ds.title()})
issue_dict = {
"fields": {
"project": {"key": key},
"summary": name + " (" + id + ")",
#"summary": name,
"description": description,
"issuetype": {"name": "Task"},
custom_fields['id']: id,
custom_fields['tactic']: {'value': tactic},
custom_fields['maturity']: {'value':'Not Tracked'},
custom_fields['url']: url,
custom_fields['datasources']: ds_payload,
# "customfield_11050": "Value that we're putting into a Free Text Field."
}
}
jiraclient.create_issue(issue_dict,id)
except Exception as ex:
print ("\t[*] Could not create ticket for " + id)
print(ex)
traceback.print_exc(file=sys.stdout)
pass
#print(ex)
#sys.exit()
print ("[*] Done!")
def create_attack_techniques_and_subtechniques(self, key):
# this creates each sub-technique as a SubTask
# no issue links are created
jiraclient = self.jirahandler
techniques = self.get_attack_techniques()
sorted_techniques = sorted(techniques, key=lambda k: k['external_references'][0]['external_id'])
print ("[*] Creating Jira issues for ATT&CK's techniques...")
for technique in sorted_techniques:
try:
custom_fields = self.jirahandler.get_custom_fields()
name = technique['name']
id = technique['external_references'][0]['external_id']
url = technique['external_references'][0]['url']
tactic = technique['kill_chain_phases'][0]['phase_name']
description = technique['description']
# some techniques dont have the field populated
if 'x_mitre_data_sources' in technique.keys():
datasources = technique['x_mitre_data_sources']
else:
datasources = []
ds_payload = []
for ds in datasources: ds_payload.append({'value':ds.title()})
if not technique ['x_mitre_is_subtechnique']:
# Not a sub-technique
issue_dict = {
"fields": {
"project": {"key": key },
#"summary": name + " (" + id + ")",
"summary": name,
"description": description,
"issuetype": {"name": "Task"},
custom_fields['Id']: id,
custom_fields['Tactic']: {'value': tactic},
custom_fields['Maturity']: {'value': 'Not Tracked'},
custom_fields['Url']: url,
custom_fields['Datasources']: ds_payload,
# "customfield_11050": "Value that we're putting into a Free Text Field."
}
}
#print("Creating Technique")
parent_id= jiraclient.create_issue(issue_dict, id)
#print (parent_id)
#print("Created Technique with id : "+ str(parent_id))
else:
# Sub-technique
issue_dict = {
"fields": {
"parent": {"id": parent_id['id']},
"project": {"key": key},
#"summary": name + " (" + id + ")",
"summary": name,
"description": description,
"issuetype": {"name": "Sub-task"},
custom_fields['Id']: id,
custom_fields['Tactic']: {'value': tactic},
custom_fields['Maturity']: {'value': 'Not Tracked'},
custom_fields['Url']: url,
custom_fields['Datasources']: ds_payload,
custom_fields['Sub-Technique of']: jiraclient.url +"/browse/"+parent_id['key'],
}
}
#print("Creating sub Technique under parent " + str(parent_id))
ret_id= jiraclient.create_issue(issue_dict, id)
#print("Created sub Technique with id : "+ str(ret_id))
except Exception as ex:
print("\t[*] Could not create ticket for " + id)
print(ex)
traceback.print_exc(file=sys.stdout)
pass
# print(ex)
# sys.exit()
print("[*] Done!")
def generate_json_layer(self, hideDisabled):
VERSION = "2.2"
NAME = "Attack2Jira"
DESCRIPTION = "Attack2Jira"
DOMAIN = "mitre-enterprise"
GRADIENT = {
"colors": [
"#DCDCDC",
"#03ad03"],
}
layer_json = {
"domain": DOMAIN,
"name": NAME,
"description": DESCRIPTION,
"gradient": GRADIENT,
"version": VERSION,
"hideDisabled": hideDisabled,
"techniques": [ ]
}
# Define your colors here
not_tracked_color = "#DCDCDC"
shade_0_color = "#e1fce1" # lightest green
shade_1_color = "#81fc81" # lighter green
shade_2_color = "#49fc49" # green
shade_3_color = "#03ad03" # darker green
res_dict=self.jirahandler.get_technique_maturity()
for key in res_dict.keys():
enabled = True
#print (key +" "+ res_dict[key]['value'])
if res_dict[key]['value'] == "Not Tracked":
enabled=False
color = not_tracked_color
elif res_dict[key]['value'] == "Initial":
color = shade_0_color
elif res_dict[key]['value'] == "Defined":
color = shade_1_color
elif res_dict[key]['value'] == "Resilient":
color = shade_2_color
elif res_dict[key]['value'] == "Optimized":
color = shade_3_color
technique = {
"techniqueID": key,
"enabled": enabled,
"color": color
}
layer_json["techniques"].append(technique)
print ("[*] Outputting JSON layer attack2jira.json")
with open('attack2jira.json', 'w', encoding='utf-8') as f:
json.dump(layer_json, f, ensure_ascii=False, indent=4)
def set_up_jira_automated(self, project, key):
self.jirahandler.create_project(project, key)
self.jirahandler.create_custom_fields()
self.jirahandler.add_custom_field_options()
self.jirahandler.add_custom_fields_to_screen(key)
self.jirahandler.hide_unwanted_fields(key)
self.create_attack_techniques_and_subtechniques(key)
def main():
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter)
parser.add_argument('-url', dest = 'url', type=str, help= 'Url of Jira instance', default="")
parser.add_argument('-u', dest = 'user', type=str, help='Username', default="")
parser.add_argument('-a', dest='action', type=str, default="", help='action to execute\nTwo supported:\n\'initialize\' will create the JIRA entities. \n\'export\' will export the JSON layer.')
parser.add_argument('-p', dest = 'project', type=str, help='Name of the Jira project to create.', default="Mitre Attack Framework")
parser.add_argument('-k', dest = 'key', type=str, help='Project Key.(default=\'ATTACK\')', default="ATTACK")
parser.add_argument('-hide', help='If set, \'Not Tracked\' techniques will be hidden',action='store_true')
results = parser.parse_args()
url= results.url
user= results.user
action = results.action
hideDisabled = results.hide
project = results.project
key = results.key
if (url and user and action):
pswd = getpass('Jira API Token for '+user+":")
if (action == "initialize"):
attack2jira = Attack2Jira(url, user, pswd)
attack2jira.set_up_jira_automated(project, key)
if (action == "export"):
attack2jira = Attack2Jira(url, user, pswd)
attack2jira.generate_json_layer(hideDisabled)
else:
parser.print_help()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("\n")
print ("[!] Exiting attack2jira")
sys.exit()