Skip to content

Commit

Permalink
Overhaul logging mechanism for S28
Browse files Browse the repository at this point in the history
  • Loading branch information
B1TC0R3 committed Sep 25, 2024
1 parent e2f5308 commit cba0296
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
19 changes: 8 additions & 11 deletions modules/S28_python_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# Description: This is an experimental EMBA module. It is designed to run user-defined python
# scripts during the analysis.
#
# shellcheck disable=SC2004

S28_python_run() {
module_log_init "${FUNCNAME[0]}"
Expand All @@ -23,8 +22,8 @@ S28_python_run() {

local lSCRIPT_DIR="${MOD_DIR}/${FUNCNAME[0]}"
local lPYTHON_SCRIPT_COUNT=${#PYTHON_SCRIPTS[@]}
local lRESULTS=()
local lCOUNT_FINDINGS=0
local lCOUNT_SUBMODULE_FINDINGS=0
local lCOUNT_TOTAL_FINDINGS=0
local lSCRIPT=""

if [[ ${lPYTHON_SCRIPT_COUNT} -gt 0 ]]; then
Expand All @@ -33,21 +32,19 @@ S28_python_run() {
for lSCRIPT in "${PYTHON_SCRIPTS[@]}"; do
sub_module_title "Execution of Python runner for ${ORANGE}${lSCRIPT}${NC}"
print_output "[*] Executing: ${ORANGE}${lSCRIPT_DIR}/${lSCRIPT}.py${NC}"
mapfile -t lRESULTS < <(python3 "${lSCRIPT_DIR}/${lSCRIPT}.py")
lCOUNT_FINDINGS=$(("${lCOUNT_FINDINGS}" + "${#lRESULTS[@]}"))

print_output "[*] Python module ${ORANGE}${lSCRIPT}${NC} reported a total of ${ORANGE}${#lRESULTS[@]}${NC} findings."
for lRESULT in "${lRESULTS[@]}"; do
print_output "\t- ${lRESULT}"
done
lCOUNT_SUBMODULE_FINDINGS=$(python3 "${lSCRIPT_DIR}/${lSCRIPT}.py" | grep "FINDINGS" | sed "s/FINDINGS://")
lCOUNT_TOTAL_FINDINGS=$((lCOUNT_TOTAL_FINDINGS + lCOUNT_SUBMODULE_FINDINGS))

cat "${LOG_PATH_MODULE}/${lSCRIPT}.txt" >> "${LOG_FILE}"
print_output "[*] Python module ${ORANGE}${lSCRIPT}${NC} reported a total of ${ORANGE}${lCOUNT_SUBMODULE_FINDINGS}${NC} findings."
done

else
print_output "[*] No Python scripts queued for execution."
fi

sub_module_title "Final results for ${FUNCNAME[0]}"
print_output "Total results count: ${lCOUNT_FINDINGS}"
module_end_log "${FUNCNAME[0]}" "${lCOUNT_FINDINGS}"
print_output "Total results count: ${lCOUNT_TOTAL_FINDINGS}"
module_end_log "${FUNCNAME[0]}" "${lCOUNT_TOTAL_FINDINGS}"
}
33 changes: 26 additions & 7 deletions modules/S28_python_run/embamodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ class EmbaModule():
__del__:
Close module files and destroy the class instance.
__write_formatted_log:
Base method for logging. Should not be called by Python modules directly.
log:
Log a new message into the module log files.
add_finding:
Add a new finding to the module. This will later be used during report generation.
panic:
Ensures propper logging when throwing exceptions.
"""

def __init__(self, argv: list, env: _Environ):
Expand All @@ -43,22 +49,36 @@ def __init__(self, argv: list, env: _Environ):
try:
self.logfile_dir = env.get('LOG_PATH_MODULE')
except:
raise Exception(f"Unable to determine log path for python module '{self.filename}'.")
self.panic(f"Unable to determine log path for python module '{self.filename}'.", Exception)

try:
self.logfile = open(f"{self.logfile_dir}/{self.filename}.txt", "w")
except:
raise Exception("Unable to open log files for '{self.filename}'.")
self.panic("Unable to open log files for '{self.filename}'.", Exception)


def __del__(self):
self.logfile.close()
self.logfile.close()


def __write_formatted_log(self, operator: str, text: str):
lines = text.split('\n')
for line in lines:
self.logfile.write(f"[{operator}] {line}\n")


def log(self, text: str):
self.logfile.write(f"{text}\n");
self.__write_formatted_log("*", text)


def add_finding(self, description: str):
self.findings.append(description)
self.__write_formatted_log(f"F{len(self.findings)}", description)


def panic(self, description: str, except_type: type[Exception]):
self.__write_formatted_log("!", description)
raise except_type(description)


def setup_module(argv: list, env: _Environ):
Expand All @@ -78,14 +98,13 @@ def setup_module(argv: list, env: _Environ):
def shutdown_module(module: EmbaModule):
"""
Shut down an emba python module.
This will also print the amount of findings as an interger so EMBA can parse the number.
Parameters:
module (EmbaModule): A class instance of EmbaModule.
Returns:
none
"""
for finding in module.findings:
print(finding)

print(f"FINDINGS:{len(module.findings)}", end="")
del module
7 changes: 3 additions & 4 deletions modules/S28_python_run/example_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ def main():

# This is just some example code.
# The module logic would go here.
module.log("Received arguments:")
for key, value in enumerate(argv):
module.add_finding(f"Found argument: {key}:{value}")
module.log(f"\t- {key} :: {value}")
module.log("Received arguments a total of {len(environ)} environment variables.")
for key in environ.keys():
module.add_finding(f"Found envvar: {key}={environ[key]}")

# Shutdown module and report results.
# This line is required
Expand Down

0 comments on commit cba0296

Please sign in to comment.