diff --git a/core/main.py b/core/main.py index c8a7a9e..13e2569 100644 --- a/core/main.py +++ b/core/main.py @@ -6,23 +6,42 @@ from core.postfix import Postfix from core.report import generateHtmlReport from core.config import AUDIT_SYSTEM, AUDIT_APPLICATION, generateConfig, parsingConfigFile from core.dispatcher import Dispatcher +from utils import getHostname, getKernelVersion, identifySystem, getCodeName, getRelease +from os import listdir +from os.path import isdir def checkArguments(): args = ArgumentParser(description="Check Gitlab repositories") args.add_argument('-a', '--audit', help="Kind of audit", choices=['system', 'application']) + args.add_argument('-p', '--plugins', help="Get all plugins", choices=['system', 'application']) args.add_argument('-c', '--config', help="Config file") return args.parse_args() +def getAllPlugins(audit): + print(f"List all plugins for {audit}") + path = str() + if audit == "system": + path = "audit/system/plugins/" + else: + path = "audit/applications/" + + for directory in listdir(path): + if isdir(f"{path}/{directory}"): + print(directory) def main(): args = checkArguments() + + if args.plugins is not None: + getAllPlugins(args.plugins) + exit(0) # If audit is not specified if args.audit is None: print("Please, you must specify the audit type") exit(1) - + # If config file is specified configs = generateConfig() if args.config is not None: @@ -32,19 +51,29 @@ def main(): report = dict() report['system'] = dict() + # Get the hostname of the VM + report['hostname'] = getHostname() + + # Get system informations + report['kernel'] = getKernelVersion() + report['release'] = f"{identifySystem()} {getRelease()} ({getCodeName()}) " + # Create our dispatcher dispatcher = Dispatcher() - print(configs) + #print(configs) if args.audit == "system": + print("Auditing the system...") for audit in AUDIT_SYSTEM: if audit not in configs["system"]["exclude_plugins"]: report["system"][audit] = dispatcher.runPlugin(audit, configs["system"][audit]) if args.audit == "application": + print("Auditing the application...") pass + print("End of the audit. Generating the report") generateHtmlReport(report) @Dispatcher.register_plugins diff --git a/core/report.py b/core/report.py index b5524e6..3e6dcca 100644 --- a/core/report.py +++ b/core/report.py @@ -6,9 +6,6 @@ import jinja2 def generateHtmlReport(data): today = datetime.now().isoformat()[0:10].replace("-", "_") - html = _getHeader() - html += "" \ - f"

Reports of {today}

" dataJinja2 = dict() dataJinja2['title'] = 'Report check system' dataJinja2['plugins'] = list() @@ -23,7 +20,7 @@ def generateHtmlReport(data): body = str() for plugin in data['system']: - print(plugin) + #print(plugin) dataJinja2['plugins'].append(f"{plugin}.html.j2") if 'postfix' in dataJinja2['plugins']: @@ -40,44 +37,15 @@ def generateHtmlReport(data): for sysctl in dataJinja2['sysctl']['file']['sysctl']: dataJinja2['sysctl']['file']['sysctl'][sysctl]['accordion-id'] = f"accordion-{index}" index += 1 - #print(data['system']['sysctl']['file'][sysctl]) - #for entry in data['system']['sysctl']['file']['sysctl']: - # print(entry) - # print(data['system']['sysctl']['file']['sysctl'][entry]) - #print(dataJinja2) dataJinja2['year'] = '2023' + dataJinja2['hostname'] = data['hostname'] + dataJinja2['kernel'] = data['kernel'] + dataJinja2['release'] = data['release'] rdr = tmplIndex.render(data=dataJinja2) - # For sysctl - #for entry in data['sysctl']: - # body += f"

Sysctl

" - - # # For file - # body += f"

File

" - #for f in data['sysctl']['file']: - # body += f"

{data['sysctl']['file']['filename']}

" - # for vul in data['sysctl']['file']['sysctl']: - # #print(data['sysctl']['file']['sysctl'][vul]) - # body += f"
{vul}
" - # body += f"

" - # body += f"Results:
" - #for result in data['sysctl']['file']['sysctl'][vul]: - # print(result) - # body += f"Line: {result['lineNumber']}
" - # body += f"Line: {result['line']}
" - # body += f"Level: {result['level']}
" - # body += f"Description: {result['description']}

" - #body += f"

" - - #print(body) with open(f"reports/reports_{today}.html", "w") as f: f.write(rdr) -def _getHeader() -> str: - header = "" \ - "" \ - "" \ - "" \ - - return header + print("The report is generated at this location: " \ + f"reports/reports_{today}.html") diff --git a/reports/templates/index.html.j2 b/reports/templates/index.html.j2 index b686e4f..0eb3da5 100644 --- a/reports/templates/index.html.j2 +++ b/reports/templates/index.html.j2 @@ -22,7 +22,10 @@

Report

-

Report of the day... blablabla

+

+ Audit of the system {{ data['hostname'] }} ({{ data['release'] }}).
+ Kernel version: {{ data['kernel'] }}
+

diff --git a/reports/templates/sysctl.html.j2 b/reports/templates/sysctl.html.j2 index 2fd7fd5..c5b92da 100644 --- a/reports/templates/sysctl.html.j2 +++ b/reports/templates/sysctl.html.j2 @@ -4,13 +4,26 @@

-

-
+
- {{ data['sysctl']['file']['sysctl'][item]['description'] }} + {{ data['sysctl']['file']['sysctl'][item]['description'] }}.
+ For resolving the issue, add this line in the {{ data['sysctl']['file']['filename'] }} file: +
+
+

+ 	    {{ data['sysctl']['file']['sysctl'][item]['flag'] }} = {{ data['sysctl']['file']['sysctl'][item]['value'] }}
+	    
+
+
diff --git a/utils.py b/utils.py index 92b214c..10aa0ef 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,7 @@ import re from subprocess import run +from socket import gethostname class ConfigError(Exception): @@ -13,23 +14,40 @@ class ConfigError(Exception): super().__init__(self.message) def identifySystem(): - os = None - with open('/etc/issue', 'r') as f: - line = f.readline() - if re.search('Arch Linux', line): - os = 'ARCHLINUX' - elif re.search('Ubuntu', line): - os = 'UBUNTU' - elif re.search('Debian', line): - os = 'DEBIAN' - else: - os = 'UNKNOWN' - - return os + #os = None + #with open('/etc/issue', 'r') as f: + # line = f.readline() + # if re.search('Arch Linux', line): + # os = 'ARCHLINUX' + # elif re.search('Ubuntu', line): + # os = 'UBUNTU' + # elif re.search('Debian', line): + # os = 'DEBIAN' + # else: + # os = 'UNKNOWN' + kernelVers = run(['/usr/bin/lsb_release', '-is'], capture_output=True) + return kernelVers.stdout.decode('utf-8') def getKernelVersion(): """ This function get the kernel version Linux """ - kernelVers = run(['/usr/bin/uname', '-r']) - return kernelVers.stdout + kernelVers = run(['/usr/bin/uname', '-r'], capture_output=True) + return kernelVers.stdout.decode('utf-8') + +def getCodeName(): + """ + This function get the code name of the system + """ + kernelVers = run(['/usr/bin/lsb_release', '-cs'], capture_output=True) + return kernelVers.stdout.decode('utf-8') + +def getRelease(): + """ + This function get the release name of the system + """ + kernelVers = run(['/usr/bin/lsb_release', '-rs'], capture_output=True) + return kernelVers.stdout.decode('utf-8') + +def getHostname(): + return gethostname()