2023-07-25 15:21:38 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from subprocess import *
|
|
|
|
import hashlib
|
|
|
|
import sys
|
2023-07-26 15:28:44 +02:00
|
|
|
import os
|
2023-07-27 15:53:10 +02:00
|
|
|
from .general import now,set_dict_key_value
|
2023-07-26 11:03:55 +02:00
|
|
|
from .modules import print_module,print_log_module
|
2023-07-27 15:53:10 +02:00
|
|
|
from .transfer import write_xml
|
|
|
|
|
|
|
|
####
|
|
|
|
# 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-07-25 15:21:38 +02:00
|
|
|
value
|
|
|
|
):
|
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-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,
|
|
|
|
modules_def: list = []
|
|
|
|
):
|
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-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-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-07-27 15:53:10 +02:00
|
|
|
"agent_mode" : "1",
|
|
|
|
}
|
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).
|
|
|
|
"""
|
|
|
|
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:
|
|
|
|
print(xml)
|
|
|
|
|
|
|
|
return xml
|