-
Notifications
You must be signed in to change notification settings - Fork 1
/
AZADBASGER.py
262 lines (192 loc) · 10.5 KB
/
AZADBASGER.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
"""
"""
import phantom.rules as phantom
import json
from datetime import datetime, timedelta
@phantom.playbook_block()
def on_start(container):
phantom.debug('on_start() called')
# call 'debug_1' block
debug_1(container=container)
return
@phantom.playbook_block()
def debug_1(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
phantom.debug("debug_1() called")
data_summary_requestid_value = container.get("data", {}).get("summary", {}).get("requestId", None)
data_summary_userid_value = container.get("data", {}).get("summary", {}).get("userId", None)
data_disposition_value = container.get("data", {}).get("disposition", None)
parameters = []
parameters.append({
"input_1": data_summary_requestid_value,
"input_2": data_summary_userid_value,
"input_3": None,
"input_4": None,
"input_5": None,
"input_6": None,
"input_7": None,
"input_8": None,
"input_9": None,
"input_10": None,
})
################################################################################
## Custom Code Start
################################################################################
phantom.debug("DEBUG: Custom Code Start")
# replace:
# with: phantom.error
import json
import requests
import time
import array
import yaml
# for async
import asyncio
import logging
# for command line args
import sys
# get the byte size of a vars
from sys import getsizeof
# for file mamagment backup of JSON output etc..
import shutil
# for skipping cert errors in proxy
import ssl
# hiding warnings for ssl
import urllib3
# for setting OS env vars for proxy
import os
# for checking for files existing
import pathlib
# This is set to False when we enable proxy support later
VAR_SSL_FLAG = True
# regex
import re
# to check dates on last seen key
import datetime
# import dateutil.parser
#import pytz
#Sep 19, 12:22:26 : data_summary_userid_value: 36688b1d-1916-4b80-9627-218567ddec1b
#Sep 19, 12:22:26 : data_summary_requestid_value: b57f2a9e-a70e-4171-9be2-2b4f6c004300
#global data_summary_requestid_value
#global data_summary_userid_value
# WORKING !!!!
#data_summary_requestid_value="1747fb72-23f3-43ed-b763-798cd31fc100"
global headers
global access_token
#data_summary_requestid_value="c0234c93-80f3-42d2-9cf9-dbca84eaa000"
#data_summary_userid_value="36688b1d-1916-4b80-9627-218567ddec1b"
############################################ DANGER SECURTY ############
############################################ DANGER SECURTY ############
############################################ DANGER SECURTY ############
############################################ DANGER SECURTY ############
with open("config.yml", "w") as file:
lines = [
'{\r "app_id":"66f2XXXXXXXXXXXXXXXXXXX30496f98",\r "app_id_secret":"gKXXXXXXXXXXXXXXXXXXXXXXXXXXXmyv8yc_a",\r "tenant_id":"66631XXXXXXXXXXXXXXXXXXXXX802154"\r}']
file.writelines(lines)
file.close()
############################################ DANGER SECURTY ############
############################################ DANGER SECURTY ############
############################################ DANGER SECURTY ############
############################################ DANGER SECURTY ############
############################################################ FUNCTIONS
def LOADCONFIG():
phantom.debug("DEBUG: Running LOADCONFIG")
yaml.warnings({'YAMLLoadWarning': False})
Loader = yaml.FullLoader
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
global app_id
global app_id_secret
global tenant_id
app_id = (cfg['app_id'])
app_id_secret = (cfg['app_id_secret'])
tenant_id = (cfg['tenant_id'])
def PROXY():
phantom.error("DEBUG: Running PROXY")
os.environ["HTTP_PROXY"] = "http://127.0.0.1:8080"
os.environ["HTTPS_PROXY"] = "http://127.0.0.1:8080"
global VAR_SSL_FLAG
VAR_SSL_FLAG = False
# supress warnings for ssl
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
################################################################################ AUTH
def FUNC_AUTH():
phantom.debug("Running FUNC_AUTH")
r = requests.post('https://login.microsoftonline.com/' + tenant_id + '/oauth2/v2.0/token',
data={'grant_type': 'client_credentials', 'client_id': app_id, 'client_secret': app_id_secret, 'scope': 'https://graph.microsoft.com/.default'}, verify=VAR_SSL_FLAG)
phantom.debug("data_disposition_value: " + data_disposition_value)
phantom.debug("data_summary_userid_value: " + data_summary_userid_value)
############################################ TEST TEST TEST requestId ############
#data_summary_requestid_value=['c0234c93-80f3-42d2-9cf9-dbca84eaa000']
############################################ TEST TEST TEST requestId ############
phantom.debug("data_summary_requestid_value: " + str(data_summary_requestid_value))
phantom.debug("tenant_id: " + tenant_id)
phantom.debug("app_id: " + app_id)
phantom.debug("app_id_secret: XXXXXXXXXXXXXX" + app_id_secret[-10:])
if r.status_code in [200]:
# print("DEBUG: Status 200 OK!")
tok_dict = json.loads(r.text)
access_token = tok_dict["access_token"]
headers = {"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"}
#phantom.debug("r.status_code:" + str(r.status_code))
#phantom.debug("r.url:" + str(r.url))
#phantom.debug("r.text:" + str(r.text))
#phantom.debug("r.content:" + str(r.content))
phantom.debug("DEBUG: AUTH COMPLETE GOT 200 STATUS CODE")
phantom.debug("access_token: XXXXXXXXXXXXXX" + access_token[-10:])
else:
phantom.error("r.status_code:" + str(r.status_code))
phantom.error("r.url:" + str(r.url))
phantom.error("r.text:" + str(r.text))
phantom.error("r.content:" + str(r.content))
phantom.error("data_summary_userid_value: " + data_summary_userid_value)
phantom.error("DEBUG: AUTH ERROR DID NOT GET 200 STATUS CODE")
# set confirmFlag based on data_disposition_value if not disposition:7 then set to safe else flag as unsafe
if data_disposition_value in ['disposition:7']:
confirmFlag = 'ConfirmCompromised'
else:
confirmFlag = 'confirmSafe'
################################ POST confirmFlag as confirmSafe or ConfirmCompromised
phantom.debug("DEBUG: Waiting 20 seconds to POST reqestid: " + data_summary_requestid_value + " data_disposition_value: " + str(confirmFlag) )
headers = {"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"}
r = requests.post('https://graph.microsoft.com/beta/auditLogs/signIns/' + confirmFlag, data=json.dumps(
{'requestIds': [data_summary_requestid_value]}),headers=headers, timeout=20, verify=VAR_SSL_FLAG)
if r.status_code in [204]:
phantom.debug("DEBUG: COMPLETE POST reqestid: " + data_summary_requestid_value)
phantom.debug("r.status_code: " + str(r.status_code))
phantom.debug("r.url: " + str(r.url))
phantom.debug("r.text: " + str(r.text[-10:]) + "XXXXXXXXXX")
phantom.debug("r.content: " + str(r.content[-10:]) + "XXXXXXXXXX")
phantom.debug("confirmFlag: " + str(confirmFlag))
phantom.debug("data_disposition_value: " + str(data_disposition_value))
phantom.debug("data_summary_requestid_value: " + data_summary_requestid_value)
else:
phantom.error("DEBUG: ERROR DID NOT GET 204 STATUS CODE for requestid: " + data_summary_requestid_value)
phantom.error("r.status_code: " + str(r.status_code))
phantom.error("r.url: " + str(r.url))
phantom.error("r.text: " + str(r.text))
phantom.error("r.content: " + str(r.content))
phantom.error("confirmFlag: " + str(confirmFlag))
phantom.error("data_disposition_value: " + str(data_disposition_value))
phantom.error("data_summary_requestid_value: " + data_summary_requestid_value)
############################################################ MAIN
phantom.debug("DEBUG: MAIN")
LOADCONFIG()
#PROXY()
FUNC_AUTH()
phantom.debug("DEBUG: Custom Code End")
################################################################################
## Custom Code End
################################################################################
#phantom.custom_function(custom_function="community/debug", parameters=parameters, name="debug_1")
return
@phantom.playbook_block()
def on_finish(container, summary):
phantom.debug("on_finish() called")
################################################################################
## Custom Code Start
################################################################################
# Write your custom code here...
################################################################################
## Custom Code End
################################################################################
return