forked from CrowdStrike/zscaler-FalconX-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intel_pull.py
150 lines (128 loc) · 5.83 KB
/
intel_pull.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
from app._util.intel_format import IntelFormat
from app._util.logger import Logger
from requests.exceptions import HTTPError
import config as config
import requests
import json
import os
class IntelPull():
def __init__(self, auth):
self.logger = Logger()
self.auth = auth
self.token = auth.get_token()
self.base_url = config.cs_base_url
self.limit = 10000
self.indicators_remaining = 10000
self.payload = {}
self.indicators_api = "/intel/queries/indicators/v1"
self.indicators_params = "?limit=" + \
str(self.limit) + \
"&filter=type:'url'%2Bmalicious_confidence:'high'&include_deleted=false"
self.deleted_indicators_params = "?limit=" + \
str(self.limit) + \
"&filter=deleted:true%2Btype:'url'%2Bmalicious_confidence:'high'"
self.headers = {'Authorization': 'Bearer ' + str(self.token)}
self.indicators_url = self.base_url + \
self.indicators_api + self.indicators_params
self.deleted_indicators_url = self.base_url + \
self.indicators_api + self.deleted_indicators_params
self.next_page_url = self.check_next_page_url('../queuing/intel.json')
self.deleted_next_page_url = self.check_next_page_url(
'../queuing/intel_deleted.json')
def check_next_page_url(self, file):
working_dir = os.path.dirname(os.path.abspath(__file__))
file_loc = os.path.join(working_dir, file)
try:
with open(file_loc) as f:
data = json.load(f)
return data['next_page']
except:
return ""
def reauthenticate(self):
self.auth.refresh_token()
self.token = self.auth.get_token()
self.headers = {'Authorization': 'Bearer ' + str(self.token)}
def get_indicators(self):
self.logger.info(
"Querying CrowdStrike Intel Indicators API for new indicators.")
if self.next_page_url:
self.holding = self.next_page_url
self.logger.info(
"CrowdStrike Intel Indicators API: next_page_url found - continuing paginated requests.")
response = requests.request(
"GET", self.next_page_url, headers=self.headers, data=self.payload)
else:
self.logger.info(
"CrowdStrike Intel Indicators API: next_page_url not found - initiating paginated requests.")
response = requests.request(
"GET", self.indicators_url, headers=self.headers, data=self.payload)
intel_results = str(response.status_code)
if intel_results == '401':
self.reauthenticate()
return self.get_indicators()
indicator_json = response.json()
response_headers = response.headers._store
self.indicators_remaining = response_headers['x-ratelimit-remaining'][1]
if 'next-page' in response_headers:
self.next_page_url = self.base_url + \
response_headers['next-page'][1]
else:
self.next_page_url = ""
indicator_json['meta']
intel = indicator_json['resources']
# format before writing
self.intel_format = IntelFormat()
self.intel_format.format_intel(intel)
intel = self.intel_format.get_lookup_ready_urls()
self.write_intel(intel, 'intel.json', self.next_page_url)
return intel
def get_deleted_indicators(self):
self.logger.info(
"Querying CrowdStrike Intel Indicators API for deleted indicators.")
if self.deleted_next_page_url:
self.holding_deleted = self.deleted_next_page_url
self.logger.info(
"CrowdStrike Intel Indicators API: next_page_url found for deleted indicators - continuing paginated requests.")
response = requests.request(
"GET", self.deleted_next_page_url, headers=self.headers, data=self.payload)
else:
self.logger.info(
"CrowdStrike Intel Indicators API: next_page_url not found for deleted indicators - initiating paginated requests.")
response = requests.request(
"GET", self.deleted_indicators_url, headers=self.headers, data=self.payload)
intel_results = str(response.status_code)
if intel_results == '401':
self.reauthenticate()
return self.get_deleted_indicators()
indicator_json = response.json()
response_headers = response.headers._store
self.indicators_remaining = response_headers['x-ratelimit-remaining'][1]
if 'next-page' in response_headers:
self.deleted_next_page_url = self.base_url + \
response_headers['next-page'][1]
else:
self.deleted_next_page_url = ''
indicator_json['meta']
intel = indicator_json['resources']
# format before writing
self.deleted_intel_format = IntelFormat()
self.deleted_intel_format.format_intel(intel)
intel = self.deleted_intel_format.get_lookup_ready_urls()
self.write_intel(intel, 'intel_deleted.json',
self.deleted_next_page_url)
return intel
def write_intel(self, intel, file, next_page):
content = {
'next_page': str(next_page),
'urls': intel
}
with open("app/crowdstrike/queuing/" + file, 'w', encoding='utf-8') as f:
json.dump(content, f, ensure_ascii=False, indent=4)
def revert_next_page(self):
self.logger.info("Reverting to previous Intel ")
self.next_page_url = self.holding
self.deleted_next_page_url = self.holding_deleted
def get_lookup_ready_urls(self):
return self.intel_format.get_lookup_ready_urls()
def get_deleted_lookup_ready_urls(self):
return self.deleted_intel_format.get_lookup_ready_urls()