-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredvar.py
209 lines (163 loc) · 7.4 KB
/
credvar.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
import requests
import xlsxwriter
from datetime import datetime
from pathlib import Path
import time
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
center_port = 443
center_base_url = "api/3.0"
def get_component(center_token, center_ip):
try:
headers = { "x-token-id": center_token }
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/components",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
#raw JSON data response
devices = r_get.json()
variables = dict()
credentials = dict()
for device in devices:
if device['variablesCount'] > 0:
id = device['id']
v_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/components/{id}/variables",headers=headers,verify=False)
v_get.raise_for_status() #if there are any request errors
vars = v_get.json()
variables[device['label']] = vars
if device['credentialsCount'] > 0:
id = device['id']
c_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/components/{id}/credentials",headers=headers,verify=False)
c_get.raise_for_status() #if there are any request errors
creds = c_get.json()
credentials[device['label']] = creds
return [variables, credentials]
except Exception as e:
return f"Error when connecting: {e}"
def get_device(center_token, center_ip):
try:
headers = { "x-token-id": center_token }
r_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/devices",headers=headers,verify=False)
r_get.raise_for_status() #if there are any request errors
#raw JSON data response
devices = r_get.json()
variables = dict()
credentials = dict()
for device in devices:
if device['variablesCount'] > 0:
id = device['id']
v_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/devices/{id}/variables",headers=headers,verify=False)
v_get.raise_for_status() #if there are any request errors
vars = v_get.json()
variables[device['label']] = vars
if device['credentialsCount'] > 0:
id = device['id']
c_get = requests.get(f"https://{center_ip}:{center_port}/{center_base_url}/devices/{id}/credentials",headers=headers,verify=False)
c_get.raise_for_status() #if there are any request errors
creds = c_get.json()
credentials[device['label']] = creds
# Note: maybe look for IP address of accessed by devices/components instead of label
return [variables, credentials]
except Exception as e:
return f"Error when connecting: {e}"
def get_creds_and_vars(center_token, center_ip):
device_data = get_device(center_token, center_ip)
component_data = get_component(center_token, center_ip)
now = datetime.now()
current_time = now.strftime("%m-%d-%Y_%H-%M-%S")
file_name = "Cyber Vision Variables and Credentials_" + current_time + ".xlsx"
workbook = xlsxwriter.Workbook(file_name)
sheet1 = workbook.add_worksheet("Variables")
sheet1.write(0, 0, 'Device')
sheet1.write(0, 1, 'Variable')
sheet1.write(0, 2, 'Protocol')
sheet1.write(0, 3, 'Details')
sheet1.write(0, 4, 'Types')
sheet1.write(0, 5, 'Accessed By')
sheet1.write(0, 6, 'First Access')
sheet1.write(0, 7, 'Last Access')
index = 1
lastIndex = 0
for device in device_data[0].keys():
for var in device_data[0][device]:
sheet1.write(index, 0, device)
sheet1.write(index, 1, var['label'])
sheet1.write(index, 2, var['protocol'])
sheet1.write(index, 3, var['category'])
types = ""
for type in var['types']:
types += type + ", "
types = types[0:len(types)-2]
sheet1.write(index, 4, types)
accesses = ""
for access in var['accesses']:
if 'device' in access:
accesses += access['device']['label'] + ", "
else:
accesses += access['component']['label'] + ", "
accesses = accesses[0:len(accesses)-2]
sheet1.write(index, 5, accesses)
sheet1.write(index, 6, datetime.fromtimestamp(int(var['firstAccess'])//1000).strftime("%m-%d-%Y, %H:%M:%S"))
sheet1.write(index, 7, datetime.fromtimestamp(int(var['lastAccess'])//1000).strftime("%m-%d-%Y, %H:%M:%S"))
index += 1
lastIndex = index
lastIndex += 2
sheet1.write(lastIndex, 0, 'Component')
sheet1.write(lastIndex, 1, 'Variable')
sheet1.write(lastIndex, 2, 'Protocol')
sheet1.write(lastIndex, 3, 'Details')
sheet1.write(lastIndex, 4, 'Types')
sheet1.write(lastIndex, 5, 'Accessed By')
sheet1.write(lastIndex, 6, 'First Access')
sheet1.write(lastIndex, 7, 'Last Access')
index = lastIndex + 1
for component in component_data[0].keys():
for var in component_data[0][component]:
sheet1.write(index, 0, component)
sheet1.write(index, 1, var['label'])
sheet1.write(index, 2, var['protocol'])
sheet1.write(index, 3, var['category'])
types = ""
for type in var['types']:
types += type + ", "
types = types[0:len(types)-2]
sheet1.write(index, 4, types)
accesses = ""
for access in var['accesses']:
if 'device' in access:
accesses += access['device']['label'] + ", "
else:
accesses += access['component']['label'] + ", "
accesses = accesses[0:len(accesses)-2]
sheet1.write(index, 5, accesses)
sheet1.write(index, 6, datetime.fromtimestamp(int(var['firstAccess'])//1000).strftime("%m-%d-%Y, %H:%M:%S"))
sheet1.write(index, 7, datetime.fromtimestamp(int(var['lastAccess'])//1000).strftime("%m-%d-%Y, %H:%M:%S"))
index += 1
lastIndex = index
sheet2 = workbook.add_worksheet("Credentials")
sheet2.write(0, 0, 'Device')
sheet2.write(0, 1, 'Username')
sheet2.write(0, 2, 'Password')
index = 1
lastIndex = 0
for device in device_data[1]:
for cred in device_data[1][device]:
sheet2.write(index, 0, device)
sheet2.write(index, 1, cred['username'])
sheet2.write(index, 2, cred['password'])
index += 1
lastIndex = index
lastIndex += 2
sheet2.write(lastIndex, 0, 'Component')
sheet2.write(lastIndex, 1, 'Username')
sheet2.write(lastIndex, 2, 'Password')
index = lastIndex + 1
for component in component_data[1]:
for cred in component_data[1][component]:
sheet2.write(index, 0, component)
if 'username' in cred:
sheet2.write(index, 1, cred['username'])
if 'password' in cred:
sheet2.write(index, 2, cred['password'])
index += 1
lastIndex = index
workbook.close()
print("Variables and Credentials Report in this directory: " + str(Path.cwd()))