check_sys/core/main.py
2023-09-18 14:51:01 +02:00

113 lines
3.1 KiB
Python

#!/usr/bin/env python
from argparse import ArgumentParser
from core.plugins.sysctl import Sysctl
from core.plugins.postfix import Postfix
from core.plugins.apache import Apache
from core.plugins.localaccount import LocalAccount
from core.plugins.grub import Grub
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
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}")
if audit == "system":
dis = Dispatcher()
plugins = dis.get_plugins()
for plugin in plugins:
print(plugin)
elif audit == "application":
pass
def main(path):
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:
parsingConfigFile(args.config, configs)
# Report
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().strip()}"
# Create our dispatcher
dispatcher = Dispatcher()
#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(path, report)
@Dispatcher.register_plugins
def sysctl(*args) -> dict:
sysctl = Sysctl(args[1])
sysctl.runAudit()
return sysctl.getReports()
@Dispatcher.register_plugins
def postfix(*args) -> dict:
postfix = Postfix(args[1])
postfix.runAudit()
return postfix.getReports()
@Dispatcher.register_plugins
def apache(*args) -> dict:
apache = Apache(args[1])
apache.runAudit()
return apache.getReports()
@Dispatcher.register_plugins
def localaccount(*args) -> dict:
account = LocalAccount(args[1])
account.runAudit()
return account.getReports()
@Dispatcher.register_plugins
def grub(*args) -> dict:
grub = Grub(args[1])
grub.runAudit()
return grub.getReports()
if __name__ == "__main__":
main()