-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
348 lines (224 loc) · 8.68 KB
/
main.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import requests
import json
import configparser
from requests.utils import requote_uri
import sys
import os
requests.packages.urllib3.disable_warnings()
config_filename = 'config.ini'
qradar_domain_id = {}
open_ticketid_dict = {}
redmine_custom_fields_dict = {}
qradar_redmine_mapping_dict = {}
custom_fields_is_list_dict = {}
# {offense_id:ticket_id}
maxt = 0
maxq= 0
if os.path.isfile(config_filename):
config = configparser.ConfigParser()
config.sections()
config.read(config_filename)
filestore = config['FILESTORE']
qradar = config['QRADAR']
redmine = config['REDMINE']
filename_to_store_vars = filestore['filename_to_store_vars']
qradar_api_key = qradar['qradar_api_key']
if qradar_api_key == "":
print("[+] Check your settings... QRadar API key is missing...")
sys.exit()
qradar_protocol = qradar['qradar_protocol']
qradar_host = qradar['qradar_host']
if qradar_host == "":
print("[+] Check your settings... QRadar Host value is missing...")
reason_id = int(qradar['reason_id'])
redmine_api_key = redmine['redmine_api_key']
if redmine_api_key == "":
print("[+] Check your settings... Script is terminating...")
sys.exit()
redmine_protocol = redmine['redmine_protocol']
redmine_host = redmine['redmine_host']
if redmine_host == "":
print("[+] Check your settings... Redmine Host value is missing...")
project_id = int(redmine['project_id'])
redmine_custom_fields = config['REDMINE_CUSTOM_FIELDS']
qradar_redmine_mapping = config['QRADAR_REDMINE_MAPPING']
custom_fields_is_list = config['CUSTOM_FIELDS_IS_LIST']
map_qradar_domain_id_to_tracker = config['MAP_DOMAIN_TO_TRACKER']
# one_dict must all be strings
one_dict_to_rule_them_all = {}
for key in qradar_redmine_mapping:
qradar_field = key
redmine_field = qradar_redmine_mapping[key]
one_dict_to_rule_them_all[qradar_field] = {'map': redmine_field, 'attributes':{'isCustom':'False','isList':'False'}}
for custom_id,custom_name in redmine_custom_fields.items():
#is custom field?
if custom_name == redmine_field:
one_dict_to_rule_them_all[qradar_field]['attributes']['isCustom'] = 'True'
one_dict_to_rule_them_all[qradar_field]['attributes']['custom_id'] = custom_id
if custom_id in custom_fields_is_list.keys():
one_dict_to_rule_them_all[qradar_field]['attributes']['isList'] = 'True'
section_name = custom_fields_is_list[custom_id]
custom_field_list_dict = config[section_name]
tmp = {}
for k,v in custom_field_list_dict.items():
tmp[k] = v
one_dict_to_rule_them_all[qradar_field]['attributes']['listValues'] = tmp
tmp = {}
if qradar_field == 'domain_id':
for k,v in map_qradar_domain_id_to_tracker.items():
tmp[k] = v
one_dict_to_rule_them_all[qradar_field]['attributes']['map_qdomain_tracker'] = tmp
custom_field_offense = one_dict_to_rule_them_all['id']['map']
#print(one_dict_to_rule_them_all)
else:
print("[+] The configuration file DOES NOT Exists!!!")
sys.exit()
# Get max offense_id:ticket_id
def initialize():
global open_ticketid_dict
global maxt
global maxq
open_ticketid_dict = {}
maxt = 0
maxq= 0
try:
with open(filename_to_store_vars,'r') as f:
line = f.readline()
line = line.replace('\n','')
maxq,maxt = line.split(':')
maxq=int(maxq)
maxt=int(maxt)
#print("Maxq:%d ,Maxt:%d"%(maxq,maxt))
f.close()
except FileNotFoundError:
print("[+] File Not Found. Creating...")
file = open(filename_to_store_vars,'w+')
file.write('0:0')
file.close()
"""
Redmine API Requests
"""
def printer(x):
print(x)
print(x.status_code)
print(x.headers)
print(x.request)
print(x.request.body)
print(x.text)
def get_redmine_ticket_offense_ids(project_id):
total_tickets_count = 0
headers = {'X-Redmine-API-Key':redmine_api_key,'Accept':'application/json'}
try:
r = requests.get(redmine_protocol+'://'+redmine_host+'/issues.json?project_id='+str(project_id),headers=headers)
response = json.loads(r.text)
total_tickets_count = response['total_count']
except Exception as e:
# print("[+] No Tickets Found")
print("Error Found: %s "%e)
print("[+] Total Tickets Count:%s"%total_tickets_count)
if total_tickets_count > 0:
for issue in response['issues']:
ticket_id = issue['id']
for custom_field in issue['custom_fields']:
if custom_field['name'] == custom_field_offense:
if custom_field['value'] != "":
val = int(custom_field['value'])
offense_id = custom_field['value']
# print(ticket_id,offense_id)
open_ticketid_dict[int(offense_id)] = int(ticket_id)
else:
print("[+] Ticket %d : Not a QRadar related issue."%ticket_id)
# print(open_ticketid_dict)
return(open_ticketid_dict)
def do_payload(offense_reply_dict):
payload_dict = {}
payload_dict['issue'] = {'project_id':project_id}
payload_dict['issue']['custom_fields'] = []
for key in one_dict_to_rule_them_all.keys():
if one_dict_to_rule_them_all[key]['attributes']['isCustom'] == 'True':
#custom field
#print(key,one_dict_to_rule_them_all[key]['map'],offense_reply_dict[key])
tmp_val = offense_reply_dict[key]
#check if is list and get the value
if one_dict_to_rule_them_all[key]['attributes']['isList'] == 'True':
tmp_val = one_dict_to_rule_them_all[key]['attributes']['listValues'][str(offense_reply_dict[key])]
payload_dict['issue']['custom_fields'].append({'id': one_dict_to_rule_them_all[key]['attributes']['custom_id'] ,'name': one_dict_to_rule_them_all[key]['map'] ,'value': tmp_val })
else:
#not custom field
payload_dict['issue'][one_dict_to_rule_them_all[key]['map']] = offense_reply_dict[key].replace('\n','')
#print(payload_dict)
return(payload_dict)
def post_redmine_new_issue(offense_dict):
#returning ticket_id
headers = {'X-Redmine-API-Key':redmine_api_key,'Content-Type':'application/json','Accept':'application/json'}
payload = do_payload(offense_dict)
r = requests.post(redmine_protocol+'://'+redmine_host+'/issues.json',headers=headers,json=payload)
response = json.loads(r.text)
# printer(r)
return(response['issue']['id'])
def close_ticket(ticket_id):
headers = {'X-Redmine-API-Key':redmine_api_key,'Content-Type':'application/json','Accept':'application/json'}
payload = {'issue':{'status_id':4}}
r = requests.put(redmine_protocol+'://'+redmine_host+'/issues/'+str(ticket_id)+'.json',headers=headers,json=payload)
"""
QRadar API Requests
"""
def get_offenses_in_open_offense_list(filter):
headers = {'SEC':qradar_api_key,'Accept': 'application/json'}
url = qradar_protocol+'://'+qradar_host+'/api/siem/offenses?filter=status%3D%22OPEN%22'
r = requests.get(url,headers=headers,verify=False)
return(r.text)
def close_offense(offense_id,reason_id):
headers = {'SEC':qradar_api_key,'Accept': 'application/json'}
url = qradar_protocol+'://'+qradar_host+'/api/siem/offenses/'+str(offense_id)+'?closing_reason_id='+str(reason_id)+'&status=CLOSED'
r = requests.post(url,headers=headers,verify=False)
#####################################################################################################################################
def check_for_new_offenses():
global maxq
global maxt
qradar_open = {}
qradar_offenses_list = []
open_ticketid_dict = {}
try:
# this dict takes offense_id:ticket_id from redmine
open_ticketid_dict = get_redmine_ticket_offense_ids(project_id)
#Get Qradar IDs in Open status
x = get_offenses_in_open_offense_list(open_ticketid_dict.keys())
qradar_offenses_dict = json.loads(x)
except Exception as e:
print("Exception: %s . Terminating..."%e)
for x in qradar_offenses_dict:
qradar_offenses_list.append(x['id'])
qradar_open[x['id']] = x
tickets = open_ticketid_dict.keys()
qids = qradar_offenses_list
#exists in redmine
d = tickets - qids
#must me in order
for i in d:
print("[+] Offense %d exists in Redmine and not in QRadar. Closing Ticket %d to Redmine"%(i,open_ticketid_dict[i]))
#close ticket offense
close_ticket(open_ticketid_dict[i])
#exists in qradar
e = qids - tickets
#must be in order
for offense_id in e:
print("[+] Offense %d exists in Qradar"%offense_id)
#is new?
if offense_id > maxq:
print("[+] New Offense. Sending to Redmine...")
ticket_id = post_redmine_new_issue(qradar_open[offense_id])
maxt = ticket_id
maxq = offense_id
else:
print("[+] Old Offense. Closing it...")
#delete qradar offense
close_offense(offense_id=offense_id,reason_id=reason_id)
with open(filename_to_store_vars,"w") as f:
f.write(str(maxq)+":"+str(maxt))
f.close()
#####################################################################################################################################
print("[+] Start Syncing...")
initialize()
check_for_new_offenses()
print("[+] Synced...")