-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbc-api.py
250 lines (233 loc) · 7.9 KB
/
cbc-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
import requests
import pandas as pd
import time
import argparse
import sys
from os.path import expanduser
start_time = time.time()
# Handle the parameters that are passed into the program
def getArgs(argv=None):
parser = argparse.ArgumentParser(
description="Use cbr-api.py to see all processes on a specific host or search for a process across the environment."
)
group1 = parser.add_mutually_exclusive_group(required=True)
group1.add_argument(
"--host",
type=str,
help="Enter in the name of the host you want to see all the processes on.",
)
group1.add_argument(
"--process",
type=str,
help="Enter in the name of the process you want to search.",
)
parser.add_argument(
"--window",
type=str,
required=False,
help="OPTIONAL: Default is 10h. Time window: “2w” where y=year, w=week, d=day, h=hour, m=minute, s=second",
)
parser.add_argument(
"--start",
type=str,
required=False,
help="OPTIONAL: Default is 10h Window. Enter start time in this format - 2020-01-18T18:34:04Z",
)
parser.add_argument(
"--end",
type=str,
required=False,
help="OPTIONAL: Default is 10h Window. Enter end time in this format - 2020-01-18T18:34:04Z",
)
args = parser.parse_args(argv)
return args
def getConfig():
"""
Function reads the contents of the credential file and returns the configuration data
to connect to the API of a specific org. Returns the headers for the api request, the
org id, and the domain name to connect to.
"""
if sys.platform == "Windows":
cred_file = "C:\\Windows\\carbonblack\\credentials.cbc"
else:
home = expanduser("~")
cred_file = f"{home}/.carbonblack/credentials.cbc"
with open(cred_file) as file:
datafile = file.readlines()
for line in datafile:
if "url" in line:
address = line.split("=")[1]
elif "token" in line:
auth_token = line.split("=")[1]
elif "org" in line:
org = line.split("=")[1]
auth_token = str(auth_token).strip("\n")
headers = {
"X-Auth-Token": auth_token,
"Content-Type": "application/json",
"accept": "application/json",
}
return (address, headers, org)
def get_job_id(
domain,
org_key,
headers,
hostname="*",
process="*",
window="10h",
start="0",
end="0",
):
"""
Function takes in the domain, org_key, headers, hostname, and timeframe to generate
the initial query an retrieve the job id of that query returns job_id
"""
url = f"{domain}/api/investigate/v2/orgs/{org_key}/processes/search_jobs"
if start != "0":
time_range = {"end": end, "start": start}
else:
time_range = {"window": "-" + window}
if hostname == "*":
query_payload = {
"query": "process_name:" + process,
"fields": [
"device_name",
"process_start_time",
"process_cmdline",
"process_name",
"process_pid",
"parent_pid",
],
"sort": [{"field": "device_timestamp", "order": "asc"}],
"start": 0,
"rows": 10000,
"time_range": time_range,
}
else:
query_payload = {
"criteria": {"device_name": [hostname]},
"query": "process_name:" + process,
"fields": [
"device_name",
"process_start_time",
"process_cmdline",
"process_name",
"process_pid",
"parent_pid",
],
"sort": [{"field": "device_timestamp", "order": "asc"}],
"start": 0,
"rows": 10000,
"time_range": time_range,
}
print("")
response = requests.post(url, headers=headers, json=query_payload).json()
job_id = response.get("job_id")
print("Query sent to Carbon Black Cloud")
return job_id
def check_status(domain, org_key, job_id, headers):
"""
Takes in the domain, org_key, job_id, and headers as input and generates a new request
that runs until "contacted" == "completed", this indicates that the query has finished
running and we can now retrieve results returns the bool True when complete
"""
url = f"{domain}/api/investigate/v1/orgs/{org_key}/processes/search_jobs/{job_id}"
contacted = ""
completed = "1"
print("Checking to see if query has completed...")
while contacted != completed:
response = requests.get(url, headers=headers).json()
contacted = response.get("contacted")
completed = response.get("completed")
print("Query has completed.")
return True
def get_results(domain, org_key, job_id, headers):
"""
Takes in domain, org_key, job_id, and headers as input and retrieves the reults in the
proper format. Function returns a dataframe.
"""
print("Retrieving the results. Please stand by...")
all_df = pd.DataFrame()
url = f"{domain}/api/investigate/v2/orgs/{org_key}/processes/search_jobs/{job_id}/results"
for i in range(0, 10000, 1000):
payload = {"start": i, "rows": 1000}
response = requests.get(url, headers=headers, params=payload).json()
results = response.get("results")
r_df = pd.DataFrame.from_dict(results)
all_df = all_df.append(r_df, ignore_index=True)
del r_df
return all_df
def df_to_csv(results, param):
"""
Takes in the dataframe and argument parameter input and writes it to a csv.
"""
output_file = f"{param}-process.csv"
if results.empty:
print("No results found. Try expanding your search.")
sys.exit(1)
results.to_csv(
output_file,
index=False,
columns=[
"device_name",
"process_start_time",
"process_cmdline",
"process_name",
"process_pid",
"parent_pid",
],
)
if __name__ == "__main__":
args = getArgs()
config = getConfig()
domain = str(config[0]).strip("\n")
headers = config[1]
org_key = str(config[2]).strip("\n")
if args.window and (args.start or args.end):
print("ERROR: Please select either Window or Start/End. Cannot specifiy both.")
sys.exit(1)
if args.start and not args.end:
print("ERROR: You must have both Start and End definied")
sys.exit(1)
if args.host:
hostname = args.host
param = args.host
if args.window:
window = args.window
job_id = get_job_id(
domain,
org_key,
headers,
hostname=hostname,
window=window,
)
elif args.start:
start = args.start
end = args.end
job_id = get_job_id(
domain, org_key, headers, hostname=hostname, start=start, end=end
)
else:
job_id = get_job_id(domain, org_key, headers, hostname=hostname)
elif args.process:
process = args.process
param = args.process
if args.window:
window = args.window
job_id = get_job_id(
domain, org_key, headers, process=process, window=window
)
elif args.start:
start = args.start
end = args.end
job_id = get_job_id(
domain, org_key, headers, process=process, start=start, end=end
)
else:
job_id = get_job_id(domain, org_key, headers, process=process)
status = check_status(domain, org_key, job_id, headers)
if status == True:
results = get_results(domain, org_key, job_id, headers)
df_to_csv(results, param)
speed = time.time() - start_time
print(f"--- {speed} seconds ---")