-
Notifications
You must be signed in to change notification settings - Fork 36
/
sensor-util.py
executable file
·190 lines (154 loc) · 6.13 KB
/
sensor-util.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
#!/usr/bin/env python
"""
OVERVIEW
Extract selected sensor information from Cb Response.
"""
import argparse
import csv
import os
import sys
from cbapi.response import CbEnterpriseResponseAPI
from cbapi.response.models import Process, Sensor
if sys.version_info.major >= 3:
_python3 = True
else:
_python3 = False
def log_err(msg):
"""Format msg as an ERROR and print to stderr.
"""
msg = 'ERROR: {0}\n'.format(msg)
sys.stderr.write(msg)
def log_info(msg):
"""Format msg and print to stdout.
"""
msg = '{0}\n'.format(msg)
sys.stdout.write(msg)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--profile", type=str, action="store",
help="The credentials.response profile to use.")
# File output
parser.add_argument("--prefix", type=str, action="store",
help="Output filename prefix.")
# Cb Response Sensor query paramaters
s = parser.add_mutually_exclusive_group(required=False)
s.add_argument("--group-id", type=int, action="store",
help="Target sensor group based on numeric ID.")
s.add_argument("--hostname", type=str, action="store",
help="Target sensor matching hostname.")
s.add_argument("--ip", type=str, action="store",
help="Target sensor matching IP address (dotted quad).")
# Health checking
parser.add_argument("--process-count", action="store_true",
help="Count processes associated with this sensor.")
parser.add_argument("--tamper-count", action="store_true",
help="Count tamper events associated with this sensor.")
parser.add_argument("--checkin-ip", action="store_true",
help="Return the latest public IP associated with the sensor.")
args = parser.parse_args()
if args.prefix:
output_filename = '%s-sensors.csv' % args.prefix
else:
output_filename = 'sensors.csv'
if args.profile:
cb = CbEnterpriseResponseAPI(profile=args.profile)
else:
cb = CbEnterpriseResponseAPI()
output_file = open(output_filename, 'w')
writer = csv.writer(output_file, quoting=csv.QUOTE_ALL)
header_row = ['computer_name',
'computer_dns_name',
'sensor_group_id',
'os',
'os_type',
'computer_sid',
'last_checkin_time',
'registration_time',
'network_adapters',
'id',
'group_id',
'group_name',
'num_eventlog_mb',
'num_storefiles_mb',
'systemvolume_free_size',
'systemvolume_total_size',
'health',
'commit_charge_mb',
'build_version_string',
'process_count',
'tamper_count',
'clock_delta',
'checkin_ip']
writer.writerow(header_row)
query_base = None
if args.group_id:
query_base = 'groupid:{0}'.format(args.group_id)
elif args.hostname:
query_base = 'hostname:{0}'.format(args.hostname)
elif args.ip:
query_base = 'ip:{0}'.format(args.ip)
if query_base is None:
sensors = cb.select(Sensor)
else:
sensors = cb.select(Sensor).where(query_base)
num_sensors = len(sensors)
log_info("Found {0} sensors".format(num_sensors))
counter = 1
for sensor in sensors:
if counter % 10 == 0:
print("{0} of {1}".format(counter, num_sensors))
if len(sensor.resource_status) > 0:
commit_charge = "{0:.2f}".format(float(sensor.resource_status[0]['commit_charge'])/1024/1024)
else:
commit_charge = ''
num_eventlog_mb = "{0:.2f}".format(float(sensor.num_eventlog_bytes)/1024/1024)
num_storefiles_mb = "{0:.2f}".format(float(sensor.num_storefiles_bytes)/1024/1024)
systemvolume_free_size = "{0:.2f}".format(float(sensor.systemvolume_free_size)/1024/1024)
systemvolume_total_size = "{0:.2f}".format(float(sensor.systemvolume_total_size)/1024/1024)
if args.process_count == True:
process_count = len(cb.select(Process).where('sensor_id:{0}'.format(sensor.id)))
else:
process_count = ''
if args.checkin_ip == True:
try:
checkin_ip = cb.select(Process).where('sensor_id:{0}'.format(sensor.id)).first().comms_ip
except AttributeError:
checkin_ip = ''
else:
checkin_ip = ''
if args.tamper_count == True:
tamper_count = len(cb.select(Process).where('tampered:true AND sensor_id:{0}'.format(sensor.id)))
else:
tamper_count = ''
output_fields = [sensor.computer_name.lower(),
sensor.computer_dns_name.lower(),
sensor.group_id,
sensor.os,
sensor.os_type,
sensor.computer_sid,
sensor.last_checkin_time,
sensor.registration_time,
sensor.network_adapters,
sensor.id,
sensor.group_id,
sensor.group.name,
num_eventlog_mb,
num_storefiles_mb,
systemvolume_free_size,
systemvolume_total_size,
sensor.sensor_health_message,
commit_charge,
sensor.build_version_string,
process_count,
tamper_count,
sensor.clock_delta,
checkin_ip]
if _python3 == False:
row = [col.encode('utf8') if isinstance(col, unicode) else col for col in output_fields]
else:
row = output_fields
writer.writerow(row)
counter += 1
output_file.close()
if __name__ == '__main__':
sys.exit(main())