-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHEC_Blocklist_API.py
283 lines (243 loc) · 11.4 KB
/
HEC_Blocklist_API.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
import argparse
import requests
import uuid
from datetime import datetime, timedelta
import string
# Constants - Update these with your actual credentials
CLIENT_ID = ""
SECRET_KEY = ""
API_BASE_URL = "https://cloudinfra-gw.portal.checkpoint.com/app/hec-api/v1.0"
AUTH_URL = "https://cloudinfra-gw.portal.checkpoint.com/auth/external"
# Proxy settings - Update as needed
PROXY_SERVER = ""
PROXIES = {
"http": PROXY_SERVER,
"https": PROXY_SERVER
}
def get_access_token():
"""Fetches the API access token using Client ID and Secret Key."""
payload = {"clientId": CLIENT_ID, "accessKey": SECRET_KEY}
headers = {"Content-Type": "application/json", "Accept": "application/json"}
try:
response = requests.post(AUTH_URL, json=payload, headers=headers, proxies=PROXIES)
if response.status_code == 200:
return response.json().get("data", {}).get("token")
print("Error: Failed to get access token.")
return None
except Exception:
print("Error: Exception during authentication.")
return None
def get_exception_id(email, token):
"""Fetches exception ID based on the sender email from the blocklist."""
url = f"{API_BASE_URL}/exceptions/blacklist"
headers = {
"Authorization": f"Bearer {token}",
"x-av-req-id": str(uuid.uuid4()),
"Accept": "application/json"
}
try:
response = requests.get(url, headers=headers, proxies=PROXIES)
if response.status_code == 200:
data = response.json().get("responseData", [])
for item in data:
if item.get("senderEmail") == email:
return item.get("entityId")
print(f"No blocklist entry found for email '{email}'.")
return None
except Exception as e:
print(f"Exception while retrieving blocklist entry: {str(e)}")
return None
def delete_exception(email, token):
"""Deletes an email from the blocklist using its exception ID, with validation."""
if not email.strip() or email in [".", "@", "*"] or (len(email) == 1 and email in string.printable):
print("Error: Invalid email pattern. Cannot be empty, a single ASCII character, a single '.', '@', or '*'.")
return False
if any(c in email for c in " \t"):
print("Error: Email pattern cannot contain spaces or tab characters.")
return False
exception_id = get_exception_id(email, token)
if not exception_id:
return False
url = f"{API_BASE_URL}/exceptions/blacklist/delete/{exception_id}"
headers = {
"Authorization": f"Bearer {token}",
"x-av-req-id": str(uuid.uuid4()),
"Accept": "application/json"
}
try:
response = requests.post(url, headers=headers, proxies=PROXIES)
if response.status_code in [200, 204]:
print(f"Successfully deleted '{email}' from the blocklist.")
return True
print(f"Failed to delete '{email}' - Status Code: {response.status_code}")
return False
except Exception as e:
print(f"Exception during blocklist deletion: {str(e)}")
return False
def get_emails_by_sender(email, token):
"""Retrieves all emails from the specified sender within the last 3 days."""
url = f"{API_BASE_URL}/search/query"
headers = {
"Authorization": f"Bearer {token}",
"x-av-req-id": str(uuid.uuid4()),
"Content-Type": "application/json",
"Accept": "application/json"
}
end_date = datetime.utcnow()
start_date = end_date - timedelta(days=7) # Change this to howevever many days back you want the quarantine to pull emails from
payload = {
"requestData": {
"entityFilter": {
"saas": "office365_emails",
"saasEntity": "office365_emails_email",
"startDate": start_date.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
"endDate": end_date.strftime("%Y-%m-%dT%H:%M:%S.999Z")
},
"entityExtendedFilter": [
{
"saasAttrName": "entityPayload.fromEmail",
"saasAttrOp": "contains",
"saasAttrValue": email
}
]
}
}
# print(f"Sending email search request for sender: {email}") # Debugging Output
entity_ids = []
while True:
try:
response = requests.post(url, json=payload, headers=headers, proxies=PROXIES)
# print(f"Email search API Response: {response.status_code} - {response.text}") # Debugging Output
if response.status_code == 200:
data = response.json()
response_data = data.get("responseData", [])
for item in response_data:
entity_id = item.get("entityInfo", {}).get("entityId")
is_quarantined = item.get("entityPayload", {}).get("isQuarantined", False)
if entity_id and not is_quarantined:
entity_ids.append(entity_id)
scroll_id = data.get("responseEnvelope", {}).get("scrollId")
if not scroll_id:
break
payload["requestData"]["scrollId"] = scroll_id
else:
print(f"Error: No emails found for sender '{email}' - Status Code: {response.status_code}")
return []
except Exception as e:
print(f"Exception while retrieving emails: {str(e)}")
return []
# print(f"Retrieved entity IDs for sender '{email}': {entity_ids}") # Debugging Output
return entity_ids
def quarantine_emails(entity_ids, token):
"""Sends a quarantine request for a list of entity IDs and logs potential re-quarantine issues."""
if not entity_ids:
print("Error: No valid emails found to quarantine.")
return False
# print(f"Attempting to quarantine {len(entity_ids)} emails...") # Debugging Output
url = f"{API_BASE_URL}/action/entity"
headers = {
"Authorization": f"Bearer {token}",
"x-av-req-id": str(uuid.uuid4()),
"Content-Type": "application/json",
"Accept": "application/json"
}
payload = {
"requestData": {
"entityIds": entity_ids,
"entityType": "office365_emails_email",
"entityActionName": "quarantine",
"entityActionParam": ""
}
}
try:
response = requests.post(url, json=payload, headers=headers, proxies=PROXIES)
response_json = response.json()
# print(f"Quarantine API Response: {response.status_code} - {response_json}") # Debugging Output
if response.status_code == 200:
print(f"Successfully quarantined {len(entity_ids)} emails.")
print("If the email was previously released, it may not be re-quarantined due to HEC system restrictions.")
return True
print(f"Quarantine failed - Status Code: {response.status_code} - {response_json}")
return False
except Exception as e:
print(f"Exception during quarantine request: {str(e)}")
return False
def is_email_in_blocklist(email, token):
"""Checks if an email is already in the blocklist."""
url = f"{API_BASE_URL}/exceptions/blacklist"
headers = {
"Authorization": f"Bearer {token}",
"x-av-req-id": str(uuid.uuid4()),
"Accept": "application/json"
}
try:
response = requests.get(url, headers=headers, proxies=PROXIES)
if response.status_code == 200:
data = response.json().get("responseData", [])
for item in data:
if item.get("senderEmail") == email:
return True
return False
except Exception as e:
print(f"Exception while checking blocklist: {str(e)}")
return False
def add_to_blocklist(email_pattern, action_needed, token, quarantine_all=None):
"""Adds emails to the HEC blocklist with validation and duplicate check."""
if not email_pattern.strip() or email_pattern in [".", "@", "*"] or (len(email_pattern) == 1 and email_pattern in string.printable):
print("Error: Invalid email pattern. Cannot be empty, a single ASCII character, a single '.', '@', or '*'.")
return False
if any(c in email_pattern for c in " \t"):
print("Error: Email pattern cannot contain spaces or tab characters.")
return False
if is_email_in_blocklist(email_pattern, token):
print(f"Warning: Email '{email_pattern}' is already in the blocklist. No action taken.")
return False
url = f"{API_BASE_URL}/exceptions/blacklist"
headers = {
"Authorization": f"Bearer {token}",
"x-av-req-id": str(uuid.uuid4()),
"Content-Type": "application/json",
"Accept": "application/json"
}
comment = f"HEC API - {action_needed.capitalize()}"
payload = {
"requestData": {
"senderEmail": email_pattern,
"senderEmailMatching": "contains", # or "exact"
"actionNeeded": action_needed,
"comment": comment,
"matchOnlyFuture": "true",
"editedBy": ""
}
}
try:
response = requests.post(url, json=payload, headers=headers, proxies=PROXIES)
if response.status_code == 200:
print(f"Successfully added '{email_pattern}' to blocklist with comment: '{comment}'.")
return True
print(f"Error: Failed to add '{email_pattern}' - Status Code: {response.status_code}")
return False
except Exception as e:
print(f"Error: Exception during blocklist addition: {str(e)}")
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Manage exceptions in HEC blocklist or whitelist")
parser.add_argument("-e", "--email_pattern", help="Email pattern to match (will be added as contains, not exact)")
parser.add_argument("-a", "--action", choices=["phishing", "spam", "greymail"], help="Action needed - What to classify email as")
parser.add_argument("-q", "--quarantine", choices=["true", "false"], default=None, help="Set 'quarantineAll' flag (true or false) -> Will quarantine emails from 7 days ago")
parser.add_argument("-d", "--delete", metavar="EMAIL", help="Delete an email from the blocklist by providing the email address")
args = parser.parse_args()
access_token = get_access_token()
if not access_token:
exit(1)
if args.delete:
delete_exception(args.delete, access_token)
if args.email_pattern and args.action:
add_to_blocklist(args.email_pattern, args.action, access_token, args.quarantine)
if args.email_pattern and args.quarantine == "true":
# print(f"Fetching entity IDs for sender: {args.email_pattern}") # Debugging Output
entity_ids = get_emails_by_sender(args.email_pattern, access_token)
# print(f"Retrieved entity IDs: {entity_ids}") # Debugging Output
if entity_ids:
# print("Calling quarantine_emails function") # Debugging Output
quarantine_emails(entity_ids, access_token)