import re from json import dumps from parsing.base import ParsingBase class Parsing(ParsingBase): def __init__(self, objects, audit): self._parsing = dict() self._reports = dict() self._objects = objects self._audit = audit def runParsing(self): for audit in self._audit: if audit['audit'] == 'file': with open(audit['value'], 'rb') as fdata: self._parseFile(fdata) if audit['audit'] == 'process': pass def _parseFile(self, fdata): data = fdata.read() lines = data.splitlines() numLines = 1 self._constructResults(filename='/etc/sysctl.conf') resultsFlag = 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']: resultsFlag[obj['flag']] = dict() resultsFlag[obj['flag']]['recommand_value'] = obj['value'] resultsFlag[obj['flag']]['occurence'] = 0 for line in lines: line = line.decode("utf-8") for obj in self._objects['sysctl']: result = self._parsingFile(line, obj, resultsFlag) if result: resultsFlag[obj['flag']]['lineNumber'] = numLines resultsFlag[obj['flag']]['occurence'] += 1 numLines += 1 # Now, we can check if the value is specified or not # And check if the flag is specified and need to put on the sysctl config for entry in resultsFlag: obj = resultsFlag[entry] if obj['occurence'] > 0: print(entry) print(obj) if obj['current_value'] != obj['recommand_value']: self._reports[entry]['message'] = \ f"You specify this value {obj['current_value']}" \ ", you should use this value {obj['recommand_value']}" else: # No find the flag, we recommand to enable it self._reports[entry]['message'] = "" # We can generate the report print(self._reports) def _parsingFile(self, line, obj, resultsFlag) -> bool: """ This function parse the line and try to find the item in it """ result = bool() groupLine = re.search(obj['flag'], line) if groupLine: # Avoid the comment if not line.startswith('#'): sLine = line.split('=') flag = sLine[0].strip() value = int(sLine[1].strip()) resultsFlag[flag]['current_value'] = value result = True return result def _constructResults(self, filename): """ 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['filename'] = filename for sysctl in self._objects['sysctl']: self._reports[sysctl['flag']] = dict() def getResults(self) -> dict: result = dict() return result