Parse sysctl

This commit is contained in:
gbucchino
2023-06-07 16:49:12 +02:00
parent 8b2e9cbd29
commit 882cdf805f
9 changed files with 328 additions and 46 deletions
+51
View File
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
import re
from json import dumps
from parsing.base import ParsingBase
class Parsing(ParsingBase):
def __init__(self, objects):
self._parsing = dict()
self._reports = dict()
self._objects = objects
def runParsing(self):
# Generate report
self._constructReports()
print(self._reports)
def _parseFile(self):
pass
def _generateReport(self, objects):
# We can generate the report
for postfix in self._reports['postfix']:
self._reports['postfix'][postfix] = objects[postfix]
def _parsingFile(self, line, obj, vulnerabilityFound) -> bool:
"""
This function parse the line and try to find the item in it
"""
result = bool()
return result
def _constructReports(self):
"""
Construct dictionary for result of the tests
Each entry contains:
Key:
- filename: filename of the test
- line: line of the test
- parse: Display the line where the vulnerabilites has been found
- description: description of the vulnerability
- level: high, medium or low
"""
self._reports['postfix'] = dict()
def getResults(self) -> dict:
return self._reports
+39 -25
View File
@@ -1,3 +1,5 @@
#!/usr/bin/env python3
import re
from json import dumps
from parsing.base import ParsingBase
@@ -10,38 +12,37 @@ class Parsing(ParsingBase):
self._audit = audit
def runParsing(self):
# Generate report
self._constructReports()
for audit in self._audit:
if audit['audit'] == 'file':
with open(audit['value'], 'rb') as fdata:
self._parseFile(fdata)
if audit['audit'] == 'process':
pass
self._parseProcess()
def _parseFile(self, fdata):
data = fdata.read()
lines = data.splitlines()
numLines = 1
self._constructReports(filename='/etc/sysctl.conf')
vulnerabilityFound = dict()
# I create an array which contains all flag we need to find
# After that, for each data, I put the number of occurence I found.
# If the array is empty, no entry found for a flag, otherwise, we check the value
for obj in self._objects['sysctl']:
for obj in self._objects:
vulnerabilityFound[obj['flag']] = dict()
vulnerabilityFound[obj['flag']]['recommand_value'] = obj['value']
vulnerabilityFound[obj['flag']]['occurence'] = 0
for item in obj:
vulnerabilityFound[obj['flag']][item] = obj[item]
print("")
for line in lines:
line = line.decode("utf-8")
for obj in self._objects['sysctl']:
for obj in self._objects:
result = self._parsingFile(line, obj, vulnerabilityFound)
if result:
vulnerabilityFound[obj['flag']]['lineNumber'] = numLines
@@ -53,26 +54,32 @@ class Parsing(ParsingBase):
# And check if the flag is specified and need to put on the sysctl config
for entry in vulnerabilityFound:
obj = vulnerabilityFound[entry]
self._reports[entry]['result'] = dict()
vulnerabilityFound[entry]['result'] = dict()
if obj['occurence'] > 0:
#print(entry)
#print(obj)
if obj['current_value'] != obj['recommand_value']:
self._reports[entry]['result']['result'] = "failed"
#self._reports[entry]['message'] = \
# f"You specify this value {obj['current_value']}" \
# ", you should use this value {obj['recommand_value']}"
vulnerabilityFound[entry]['result']['result'] = "failed"
else:
self._reports[entry]['result']['result'] = "failed"
vulnerabilityFound[entry]['result']['result'] = "success"
else:
# No find the flag, we recommand to enable it
self._reports[entry]['result']['result'] = "failed"
vulnerabilityFound[entry]['result']['result'] = "failed"
# Generate report
self._generateReport(vulnerabilityFound)
def _parseProcess(self):
vulnerabilityFound = dict()
# Generate report
#self._generateReport(vulnerabilityFound)
def _generateReport(self, objects):
# We can generate the report
print(self._reports)
print("")
from json import dumps
print(dumps(vulnerabilityFound, indent=4))
for sysctl in self._reports['file']['sysctl']:
#self._reports['file']['sysctl'][sysctl] = vulnerabilityFound[sysctl]
self._reports['file']['sysctl'][sysctl] = objects[sysctl]
def _parsingFile(self, line, obj, vulnerabilityFound) -> bool:
"""
@@ -94,7 +101,7 @@ class Parsing(ParsingBase):
return result
def _constructReports(self, filename):
def _constructReports(self):
"""
Construct dictionary for result of the tests
Each entry contains:
@@ -105,12 +112,19 @@ class Parsing(ParsingBase):
- description: description of the vulnerability
- level: high, medium or low
"""
self._reports['filename'] = filename
# For file
self._reports['file'] = dict()
self._reports['file']['filename'] = self._audit[0]['value']
self._reports['file']['sysctl'] = dict()
for sysctl in self._objects['sysctl']:
self._reports[sysctl['flag']] = dict()
# For process
self._reports['process'] = dict()
self._reports['process']['sysctl'] = dict()
for sysctl in self._objects:
self._reports['file']['sysctl'][sysctl['flag']] = dict()
self._reports['process']['sysctl'][sysctl['flag']] = dict()
def getResults(self) -> dict:
result = dict()
return result
return self._reports