Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit 80ef20c

Browse files
committed
Working with new v2 deepsecurity-py SDK. Test all logical branches. Ready for AWS Lambda
1 parent 793c190 commit 80ef20c

File tree

4 files changed

+50
-44
lines changed

4 files changed

+50
-44
lines changed

lib/deepsecurity/computers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def get(self, name=None, group_id=None):
192192
class Computer(core.CoreObject):
193193
def __init__(self, manager=None, api_response=None, log_func=None):
194194
self.manager = manager
195-
self.recommened_rules = None
195+
self.recommended_rules = None
196196
if api_response: self._set_properties(api_response, log_func)
197197

198198
def send_events(self):
@@ -235,8 +235,8 @@ def get_recommended_rules(self):
235235
"""
236236
Recommend a set of rules to apply to the computer
237237
"""
238-
self.recommened_rules = self.manager.get_rule_recommendations_for_computer(self.id)
239-
return self.recommened_rules['total_recommedations']
238+
self.recommended_rules = self.manager.get_rule_recommendations_for_computer(self.id)
239+
return self.recommended_rules['total_recommedations']
240240

241241
class ComputerGroup(core.CoreObject):
242242
def __init__(self, manager=None, api_response=None, log_func=None):

lib/deepsecurity/core.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ def _request(self, request, auth_required=True):
256256
if result['raw']:
257257
result['data'] = json.loads(result['raw'])
258258
except Exception, json_err:
259-
self.log("Could not convert response from call {} to JSON".format(request['call']), err=json_err)
260-
259+
# report the exception as 'info' because it's not fatal and the data is
260+
# still captured in result['raw']
261+
self.log("Could not convert response from call {} to JSON. Threw exception:\n\t{}".format(request['call'], json_err), level='info')
261262
return result
262263

263264
def _prefix_keys(self, prefix, d):

lib/deepsecurity/dsm.py

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ def get_rule_recommendations_for_computer(self, computer_id):
357357
if response and response['status'] == 200:
358358
# response contains the internal rule ID
359359
for internal_rule_id in response['data']:
360+
if internal_rule_id == u'@xmlns': continue
360361
results[rule_key].append(internal_rule_id)
361362
results['total_recommedations'] += 1
362363

lib/sqli.py

+43-39
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ def get_deep_security_info(self):
220220
"""
221221
self._log("Requesting information from Deep Security about your deployment", priority=True)
222222
if self.dsm:
223-
self.dsm.get_all()
223+
self.dsm.policies.get()
224+
self.dsm.computer_groups.get()
224225
self._log("Requesting rules from the Deep Security manager. This will take a few seconds...")
225-
self.dsm.get_all_rules()
226+
self.dsm.rules.get()
226227
self._log("Requesting computers from the Deep Security manager. This will take a few seconds...")
227-
self.dsm.get_computers_with_details()
228+
self.dsm.computers.get()
228229
self._log("Requested information from the Deep Security manager cached locally")
229230

230231
def compare_ec2_to_deep_security(self):
@@ -236,7 +237,7 @@ def compare_ec2_to_deep_security(self):
236237
recommendations = {}
237238
if self.dsm and self.dsm.computers and self.instances:
238239
for computer_id, computer_details in self.dsm.computers.items():
239-
ds_instance_map[computer_details.cloud_object_instance_id] = computer_id
240+
ds_instance_map[computer_details.cloud_instance_id] = computer_id
240241

241242
for instance_id, instance_details in self.instances.items():
242243
if ds_instance_map.has_key(instance_id):
@@ -258,8 +259,8 @@ def does_rule_match_sqli(self, rule):
258259
sqli_recommended = True
259260

260261
if 'application_type_id' in dir(rule):
261-
if self.dsm.application_types.has_key(rule.application_type_id):
262-
if self.dsm.application_types[rule.application_type_id].tbuid in self.tbuids:
262+
if self.dsm.rules['application_types'].has_key(rule.application_type_id):
263+
if self.dsm.rules['application_types'][rule.application_type_id].tbuid in self.tbuids:
263264
sqli_recommended = True
264265

265266
for pattern in self.patterns:
@@ -278,43 +279,46 @@ def analyze_computer(self, ds_computer_id):
278279
Analyze the specified computer to determine if it should be
279280
protected by SQLi rules
280281
"""
281-
self._log("Analyzing computer {}:{}".format(ds_computer_id, self.dsm.computers[ds_computer_id].hostname))
282+
self._log("Analyzing computer {}:{}".format(ds_computer_id, self.dsm.computers[ds_computer_id].name))
282283
recommendation = False
283-
self.dsm.get_recommended_rules_for_computer(ds_computer_id)
284+
self.dsm.get_rule_recommendations_for_computer(ds_computer_id)
284285
computer = self.dsm.computers[ds_computer_id]
285286
sqli_recommendations = []
286-
287-
# check at the policy level
288-
if computer.policy_id:
289-
self._log("Computer is protected by Deep Security. Checking rules")
290-
for rule_type in [
291-
'integrity_monitoring_rules',
292-
'log_inspection_rules',
293-
'intrusion_prevention_rules'
294-
]:
295-
if self.dsm.policies.has_key(computer.policy_id):
296-
rule_set = getattr(self.dsm.policies[computer.policy_id], rule_type)
297-
if rule_set: # policy has these type of rules applied
298-
for rule_id in getattr(self.dsm.policies[computer.policy_id], rule_type)[-1]:
299-
rule = self.dsm.rules[rule_type.replace('_rules', '')][rule_id]
300-
if self.does_rule_match_sqli(rule): sqli_recommendations.append(rule)
287+
288+
if 'cloud_instance_id' in dir(computer) and computer.cloud_instance_id and computer.cloud_instance_id.startswith('i-'):
289+
# this is an AWS instance
290+
291+
# check at the policy level
292+
if computer.policy_id:
293+
self._log("Computer is protected by Deep Security. Checking rules")
294+
for rule_type in [
295+
'integrity_monitoring_rule_ids',
296+
'intrusion_prevention_rule_ids',
297+
'log_inspection_rule_ids'
298+
# application_types
299+
]:
300+
if self.dsm.policies.has_key(computer.policy_id):
301+
rule_set = getattr(self.dsm.policies[computer.policy_id], rule_type)
302+
if rule_set and rule_set.has_key('item'): # policy has these type of rules applied
303+
for rule_id in rule_set['item']:
304+
rule = self.dsm.rules[rule_type.replace('_rule_ids', '')][rule_id]
305+
if self.does_rule_match_sqli(rule): sqli_recommendations.append(rule)
306+
else:
307+
self._log("Instance {} has no rules of type {} applied".format(computer.cloud_instance_id, rule_type))
301308
else:
302-
self._log("Instance {} has no rules of type {} applied".format(computer.cloud_object_instance_id, rule_type))
303-
else:
304-
self._log("Policy {} is not available for analysis".format(computer.policy_id))
305-
else:
306-
self._log("Deep Security is aware of the instance but is not protecting it with a policy")
307-
recommendation = None
308-
309-
# now check for any recommendations to the computer
310-
for rule_type, rules in computer.recommended_rules.items():
311-
self._log("Checking for recommended {} rules".format(rule_type))
312-
for rule_id, rule in rules.items():
313-
if self.does_rule_match_sqli(rule): sqli_recommendations.append(rule)
314-
315-
for application_type_id, application_type in computer.application_types.items():
316-
if application_type.tbuid in self.tbuids:
317-
sqli_recommendations.append(application_type)
309+
self._log("Policy {} is not available for analysis".format(computer.policy_id))
310+
else:
311+
self._log("Deep Security is aware of the instance but is not protecting it with a policy")
312+
recommendation = None
313+
314+
# now check for any recommendations to the computer
315+
if computer.recommended_rules:
316+
for rule_type, rules in computer.recommended_rules.items():
317+
self._log("Checking for recommended {} rules".format(rule_type))
318+
for rule_id, rule in rules.items():
319+
if self.does_rule_match_sqli(rule): sqli_recommendations.append(rule)
320+
else:
321+
self._log("There are no rule recommendations for instance {}".format(computer.cloud_instance_id))
318322

319323
if len(sqli_recommendations) > 1:
320324
recommendation = True if len(sqli_recommendations) > 0 else False

0 commit comments

Comments
 (0)