diff --git a/implants/imix/install_scripts/install_service/main.eldritch b/implants/imix/install_scripts/install_service/main.eldritch index 70c223ef4..edfcb0c7a 100644 --- a/implants/imix/install_scripts/install_service/main.eldritch +++ b/implants/imix/install_scripts/install_service/main.eldritch @@ -138,6 +138,72 @@ launch_daemon_template = """ """ +bsdinit_template = """ +#!/bin/sh +# +# PROVIDE: {{ service_name }} +# REQUIRE: LOGIN FILESYSTEMS +# KEYWORD: shutdown + +. /etc/rc.subr + +name="{{ service_name }}" +rcvar="{{ service_name }}_enable" + +# The command to start the service +command="{{ service_start_cmd }}" +# Additional command arguments if any +command_args="" + +# Load the rc.subr script +load_rc_config $name +: ${name}_enable:=no } + +# Define the function to start the service +start_cmd="${name}_start" + +# Start function +{{ service_name }}_start() { + echo "Starting {{ service_name }}." + # Execute the command to start the service + ${command} ${command_args} & +} + +# Define the function to stop the service +stop_cmd="${name}_stop" + +# Stop function +{{ service_name }}_stop() { + echo "Stopping {{ service_name }}." + # Command to stop the service if required + # For example, if {{ service_name }} supports graceful shutdown: + # killall -SIGTERM {{ service_name }} +} + +# Define the function to check if the service is running +status_cmd="${name}_status" + +# Status function +{{ service_name }}_status() { + # Check if the service is running + # For example, check if the process exists + if pgrep -q -x "{{ service_name }}"; then + echo "{{ service_name }} is not running." + else + echo "{{ service_name }} is not running." + fi +} + +# Define command line arguments to control the service +# e.g., {{ service_name }}_enable="YES" to enable the service + +# Start the service automatically during system startup +{{ service_name }}_enable="YES" + +# Call the rc.subr functions to handle the service +run_rc_command "$1" +""" + def is_using_systemd(): command_get_res = sys.shell("command -v systemctl") if command_get_res['status'] == 0 and file.is_file(command_get_res['stdout'].strip()): @@ -152,6 +218,13 @@ def is_using_sysvinit(): return True return False +def is_using_bsdinit(): + # Lol this is how ansible does it too :shrug: + # https://github.com/ansible/ansible/blob/386edc666ec2a053b4d576fc4b2deeb46fe492b8/lib/ansible/module_utils/facts/system/service_mgr.py#L124 + if sys.get_os()['platform'] == "BSD": + return True + return False + def systemd(service_name, service_desc, executable_path, executable_args): # assets.copy("persist_service/files/systemd.service.j2","/tmp/systemd.service.j2") file.write("/tmp/systemd.service.j2", systemd_service_template) @@ -190,6 +263,27 @@ def sysvinit(service_name, service_desc, executable_path, executable_args): sys.shell("service "+service_name+" start") print("sysvinit installed") +def bsdinit(service_name, service_desc, executable_path, executable_args): + startup_dir = "/usr/local/etc/rc.d/" + if not file.is_dir(startup_dir): + print(startup_dir+" not found") + return + + file.write("/tmp/svc.sh.j2", bsdinit_template) + args = { + "service_name":service_name, + "service_desc":service_desc, + "service_start_cmd":executable_path+" "+executable_args + } + file.template("/tmp/svc.sh.j2",startup_dir+service_name+".sh", args, False) + file.remove("/tmp/svc.sh.j2") + + sys.shell("chmod +x "+startup_dir+service_name+".sh") + sys.shell("chmod +x "+executable_path) + sys.shell("service "+service_name+".sh start") + + print("bsdinit installed") + def launch_daemon(service_name, executable_path, executable_args): # assets.copy("persist_service/files/launch_daemon.plist.j2","/tmp/plist.j2") file.write("/tmp/plist.j2",launch_daemon_template) @@ -236,6 +330,11 @@ def persist_service(service_name, service_desc, executable_name, executable_args executable_path = "C:\\ProgramData\\"+executable_name+".exe" file.copy(src_path, executable_path) windows_service_manager(service_name, service_name, service_desc, executable_path) + elif sys.get_os()['platform'] == "BSD": + executable_path = "/bin/"+executable_name + file.copy(src_path, executable_path) + if is_using_bsdinit(): + bsdinit(service_name, service_desc, executable_path, executable_args) else: print("OS not supported") diff --git a/implants/lib/eldritch/src/sys/shell_impl.rs b/implants/lib/eldritch/src/sys/shell_impl.rs index 3bdbb2a3a..0a78d3b07 100644 --- a/implants/lib/eldritch/src/sys/shell_impl.rs +++ b/implants/lib/eldritch/src/sys/shell_impl.rs @@ -33,7 +33,7 @@ fn handle_shell(cmd: String) -> Result { command_args = ["/c", cmd.as_str()].to_vec(); } else { // linux and such - command_string = "bash"; + command_string = "sh"; command_args = ["-c", cmd.as_str()].to_vec(); }