2023-07-25 15:21:38 +02:00
|
|
|
import sys
|
2023-07-26 15:28:44 +02:00
|
|
|
import os
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
####
|
|
|
|
# Define global variables dict, used in functions as default values.
|
|
|
|
# Its values can be changed.
|
|
|
|
#########################################################################################
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
GLOBAL_VARIABLES = {
|
2023-07-27 15:53:10 +02:00
|
|
|
'agents_group_name' : '',
|
|
|
|
'interval' : 300
|
2023-07-25 15:21:38 +02:00
|
|
|
}
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
####
|
|
|
|
# Define some global variables
|
2023-07-26 15:28:44 +02:00
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
POSIX = os.name == "posix"
|
|
|
|
WINDOWS = os.name == "nt"
|
|
|
|
LINUX = sys.platform.startswith("linux")
|
|
|
|
MACOS = sys.platform.startswith("darwin")
|
|
|
|
OSX = MACOS # deprecated alias
|
|
|
|
FREEBSD = sys.platform.startswith("freebsd")
|
|
|
|
OPENBSD = sys.platform.startswith("openbsd")
|
|
|
|
NETBSD = sys.platform.startswith("netbsd")
|
|
|
|
BSD = FREEBSD or OPENBSD or NETBSD
|
|
|
|
SUNOS = sys.platform.startswith(("sunos", "solaris"))
|
|
|
|
AIX = sys.platform.startswith("aix")
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
####
|
|
|
|
# Set a global variable with the specified name and assigns a value to it.
|
2023-07-27 15:53:10 +02:00
|
|
|
#########################################################################################
|
2023-07-25 15:21:38 +02:00
|
|
|
def set_global_variable(
|
2023-07-27 15:53:10 +02:00
|
|
|
variable_name: str = "",
|
2023-08-02 11:42:20 +02:00
|
|
|
value = None
|
2023-07-25 15:21:38 +02:00
|
|
|
):
|
2023-07-26 11:21:15 +02:00
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
Sets the value of a global variable in the 'GLOBAL_VARIABLES' dictionary.
|
2023-07-26 11:21:15 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
variable_name (str): Name of the variable to set.
|
|
|
|
value (any): Value to assign to the variable.
|
|
|
|
"""
|
2023-08-09 13:46:09 +02:00
|
|
|
from .general import set_dict_key_value
|
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
set_dict_key_value(GLOBAL_VARIABLES, variable_name, value)
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
####
|
2023-07-27 15:53:10 +02:00
|
|
|
# Agent class
|
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
class Agent:
|
2023-07-25 15:21:38 +02:00
|
|
|
"""
|
2023-07-27 15:53:10 +02:00
|
|
|
Basic agent class. Requires agent parameters (config {dictionary})
|
|
|
|
and module definition (modules_def [list of dictionaries])
|
|
|
|
"""
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config: dict = None,
|
2023-08-02 13:13:43 +02:00
|
|
|
modules_def: list = [],
|
|
|
|
log_modules_def: list = []
|
2023-07-27 15:53:10 +02:00
|
|
|
):
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
if config is None:
|
|
|
|
config = init_agent()
|
|
|
|
|
|
|
|
self.config = config
|
|
|
|
self.modules_def = modules_def
|
2023-08-02 13:13:43 +02:00
|
|
|
self.log_modules_def = log_modules_def
|
|
|
|
self.added_modules = []
|
|
|
|
|
|
|
|
def update_config(
|
|
|
|
self,
|
|
|
|
config: dict = {}
|
|
|
|
):
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
2023-08-02 13:13:43 +02:00
|
|
|
for key, value in config.items():
|
|
|
|
if key in self.config:
|
|
|
|
self.config[key] = value
|
|
|
|
|
|
|
|
def get_config(
|
|
|
|
self
|
|
|
|
) -> dict:
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
2023-08-02 13:13:43 +02:00
|
|
|
return self.config
|
|
|
|
|
|
|
|
def add_module(
|
|
|
|
self,
|
|
|
|
module: dict = {}
|
|
|
|
):
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
|
|
|
from .general import generate_md5
|
|
|
|
from .modules import init_module
|
2023-08-02 13:13:43 +02:00
|
|
|
|
|
|
|
if "name" in module and type(module["name"]) == str and len(module["name"].strip()) > 0:
|
|
|
|
self.modules_def.append(init_module(module))
|
|
|
|
self.added_modules.append(generate_md5(module["name"]))
|
|
|
|
|
|
|
|
def del_module(
|
|
|
|
self,
|
|
|
|
module_name: str = ""
|
|
|
|
):
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
|
|
|
from .general import generate_md5
|
2023-08-02 13:13:43 +02:00
|
|
|
|
|
|
|
if len(module_name.strip()) > 0:
|
|
|
|
try:
|
|
|
|
module_id = self.added_modules.index(generate_md5(module_name))
|
|
|
|
except:
|
|
|
|
module_id = None
|
|
|
|
|
|
|
|
if module_id is not None:
|
|
|
|
self.added_modules.pop(module_id)
|
|
|
|
self.modules_def.pop(module_id)
|
|
|
|
|
|
|
|
def update_module(
|
|
|
|
self,
|
|
|
|
module_name: str = "",
|
|
|
|
module: dict = {}
|
|
|
|
):
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
2023-08-02 13:13:43 +02:00
|
|
|
module_def = self.get_module(module_name)
|
|
|
|
|
|
|
|
if module_def:
|
|
|
|
if "name" not in module:
|
|
|
|
module["name"] = module_name
|
|
|
|
|
|
|
|
module_def.update(module)
|
|
|
|
|
|
|
|
self.del_module(module_name)
|
|
|
|
self.add_module(module_def)
|
|
|
|
|
|
|
|
def get_module(
|
|
|
|
self,
|
|
|
|
module_name: str = ""
|
|
|
|
) -> dict:
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
|
|
|
from .general import generate_md5
|
2023-08-02 13:13:43 +02:00
|
|
|
|
|
|
|
if len(module_name.strip()) > 0:
|
|
|
|
try:
|
|
|
|
module_id = self.added_modules.index(generate_md5(module_name))
|
|
|
|
except:
|
|
|
|
module_id = None
|
|
|
|
|
|
|
|
if module_id is not None:
|
|
|
|
return self.modules_def[module_id]
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def get_modules_def(
|
|
|
|
self
|
|
|
|
) -> dict:
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
2023-08-02 13:13:43 +02:00
|
|
|
return self.modules_def
|
|
|
|
|
|
|
|
def add_log_module(
|
|
|
|
self,
|
|
|
|
log_module: dict = {}
|
|
|
|
):
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
|
|
|
from .modules import init_log_module
|
2023-08-02 13:13:43 +02:00
|
|
|
|
|
|
|
if "source" in module and type(module["source"]) == str and len(module["source"].strip()) > 0:
|
|
|
|
self.log_modules_def.append(init_log_module(log_module))
|
|
|
|
|
|
|
|
def get_log_modules_def(
|
|
|
|
self
|
|
|
|
) -> dict:
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
2023-08-02 13:13:43 +02:00
|
|
|
return self.log_modules_def
|
|
|
|
|
|
|
|
def print_xml(
|
|
|
|
self,
|
|
|
|
print_flag: bool = False
|
|
|
|
) -> str:
|
2023-08-09 13:46:09 +02:00
|
|
|
'''
|
|
|
|
TODO: Add commnets
|
|
|
|
'''
|
2023-08-02 13:13:43 +02:00
|
|
|
return print_agent(self.get_config(), self.get_modules_def(), self.get_log_modules_def(), print_flag)
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
####
|
|
|
|
# Init agent template
|
2023-07-27 15:53:10 +02:00
|
|
|
#########################################################################################
|
2023-07-27 16:04:27 +02:00
|
|
|
def init_agent(
|
|
|
|
default_values: dict = {}
|
|
|
|
) -> dict:
|
2023-07-26 11:21:15 +02:00
|
|
|
"""
|
|
|
|
Initializes an agent template with default values.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: Dictionary representing the agent template with default values.
|
|
|
|
"""
|
2023-08-09 13:46:09 +02:00
|
|
|
from .general import now
|
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
agent = {
|
2023-07-27 15:53:10 +02:00
|
|
|
"agent_name" : "",
|
|
|
|
"agent_alias" : "",
|
2023-07-25 15:21:38 +02:00
|
|
|
"parent_agent_name" : "",
|
2023-07-27 15:53:10 +02:00
|
|
|
"description" : "",
|
|
|
|
"version" : "",
|
|
|
|
"os_name" : "",
|
|
|
|
"os_version" : "",
|
|
|
|
"timestamp" : now(),
|
|
|
|
"address" : "",
|
2023-08-02 11:32:34 +02:00
|
|
|
"group" : GLOBAL_VARIABLES['agents_group_name'],
|
|
|
|
"interval" : GLOBAL_VARIABLES['interval'],
|
2023-08-02 13:13:43 +02:00
|
|
|
"agent_mode" : "1"
|
2023-07-27 15:53:10 +02:00
|
|
|
}
|
2023-07-26 15:28:44 +02:00
|
|
|
|
2023-07-27 16:04:27 +02:00
|
|
|
for key, value in default_values.items():
|
|
|
|
if key in agent:
|
|
|
|
agent[key] = value
|
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
return agent
|
2023-07-26 15:28:44 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
####
|
|
|
|
# Prints agent XML. Requires agent conf (dict) and modules (list) as arguments.
|
2023-07-26 15:28:44 +02:00
|
|
|
#########################################################################################
|
2023-07-27 15:53:10 +02:00
|
|
|
def print_agent(
|
|
|
|
agent: dict = None,
|
|
|
|
modules: list = [],
|
|
|
|
log_modules: list = [],
|
|
|
|
print_flag: bool = False
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Prints agent XML. Requires agent conf (dict) and modules (list) as arguments.
|
|
|
|
- Use print_flag to show modules' XML in STDOUT.
|
|
|
|
- Returns xml (str).
|
|
|
|
"""
|
2023-08-09 13:46:09 +02:00
|
|
|
from .output import print_stdout
|
|
|
|
from .modules import print_module,print_log_module
|
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
xml = ""
|
|
|
|
data_file = None
|
2023-07-26 15:28:44 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
if agent is not None:
|
|
|
|
header = "<?xml version='1.0' encoding='UTF-8'?>\n"
|
|
|
|
header += "<agent_data"
|
|
|
|
for dato in agent:
|
|
|
|
header += " " + str(dato) + "='" + str(agent[dato]) + "'"
|
|
|
|
header += ">\n"
|
|
|
|
xml = header
|
|
|
|
|
|
|
|
for module in modules:
|
|
|
|
modules_xml = print_module(module)
|
|
|
|
xml += str(modules_xml)
|
|
|
|
|
|
|
|
for log_module in log_modules:
|
|
|
|
modules_xml = print_log_module(log_module)
|
|
|
|
xml += str(modules_xml)
|
|
|
|
|
|
|
|
xml += "</agent_data>"
|
|
|
|
|
|
|
|
if print_flag:
|
2023-08-09 13:46:09 +02:00
|
|
|
print_stdout(xml)
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
return xml
|