Skip to content

Commit feb5f4b

Browse files
committed
refactor: service installation for isolation and security; add uninstaller
- Run Linux EDR under dedicated 'linux-edr' user and group - Install package into isolated uv virtual environment at /opt/linux-edr - Update systemd service to use non-root user, set custom PATH and working directory - Enable key syscall tracepoints on service start - Restrict file access and add necessary capabilities (CAP_DAC_OVERRIDE, CAP_SYS_ADMIN) - Ensure proper ownership and permissions for log and config directories - Add uninstall_service.sh script for clean removal of service and data
1 parent 94349e1 commit feb5f4b

3 files changed

Lines changed: 102 additions & 13 deletions

File tree

install_service.sh

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,40 @@ if [ ! -f /usr/bin/uv ]; then
2222
ln -sf $(which uv) /usr/bin/uv
2323
fi
2424

25-
# Install the Python package in development mode
26-
echo "Installing Linux EDR Python package..."
27-
uv pip install -e .
25+
# Create service user and group
26+
echo "Creating service user..."
27+
useradd -r -s /sbin/nologin linux-edr || true
2828

29-
# Create log directory
29+
# Create virtual environment directory and venv
30+
VENV_DIR="/opt/linux-edr"
31+
if [ ! -d "$VENV_DIR" ]; then
32+
echo "Creating virtual environment with uv..."
33+
mkdir -p "$VENV_DIR"
34+
uv venv "$VENV_DIR"
35+
fi
36+
37+
# copy this all to VENV_DIR
38+
cp -r . "$VENV_DIR"
39+
40+
# Install the Python package into the venv using its uv
41+
echo "Installing Linux EDR Python package into virtual environment..."
42+
uv pip install .
43+
44+
# Create log directory and set permissions
3045
echo "Creating log directory..."
3146
mkdir -p /var/log/linux-edr
47+
chown linux-edr:linux-edr /var/log/linux-edr
3248
chmod 750 /var/log/linux-edr
3349

34-
# Copy service file to systemd directory
35-
echo "Installing systemd service..."
36-
cp linux-edr.service /etc/systemd/system/
37-
3850
# Create default config directory if it doesn't exist
3951
mkdir -p /etc/linux_edr
52+
chown linux-edr:linux-edr /etc/linux_edr
4053

4154
# Copy default config and update with OpenAI API key
4255
echo "Installing configuration..."
4356
cp linux_edr/config.ini /etc/linux_edr/
57+
chown linux-edr:linux-edr /etc/linux_edr/config.ini
58+
chmod 600 /etc/linux_edr/config.ini
4459

4560
# Prompt for OpenAI API key
4661
echo ""
@@ -51,9 +66,12 @@ if [ ! -z "$api_key" ]; then
5166
# Update the config file with the API key
5267
echo "Setting OpenAI API key in config file..."
5368
sed -i "s/^api_key =.*/api_key = $api_key/" /etc/linux_edr/config.ini
54-
chmod 600 /etc/linux_edr/config.ini
5569
fi
5670

71+
# Copy service file to systemd directory
72+
echo "Installing systemd service..."
73+
cp linux-edr.service /etc/systemd/system/
74+
5775
# Reload systemd
5876
systemctl daemon-reload
5977

@@ -64,5 +82,8 @@ echo ""
6482
echo "To check status:"
6583
echo " systemctl status linux-edr.service"
6684
echo ""
85+
echo "To view logs:"
86+
echo " journalctl -u linux-edr.service -f"
87+
echo ""
6788
echo "Configuration file is at /etc/linux_edr/config.ini"
6889
echo "Note: The config file permissions are set to 600 to protect the API key"

linux-edr.service

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@ After=network.target
44

55
[Service]
66
Type=simple
7-
User=root
7+
User=linux-edr
8+
Group=linux-edr
9+
Environment="PYTHONUNBUFFERED=1"
10+
Environment="PATH=/opt/linux-edr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
11+
WorkingDirectory=/opt/linux-edr
812
ExecStartPre=/bin/sh -c 'echo 1 > /sys/kernel/tracing/events/syscalls/sys_enter_execve/enable'
9-
ExecStart=/usr/bin/uv run python -m linux_edr.cli run --output /var/log/linux-edr/reports.jsonl
13+
ExecStartPre=/bin/sh -c 'echo 1 > /sys/kernel/tracing/events/syscalls/sys_exit_execve/enable'
14+
ExecStartPre=/bin/sh -c 'echo 1 > /sys/kernel/tracing/events/syscalls/sys_enter_fork/enable'
15+
ExecStartPre=/bin/sh -c 'echo 1 > /sys/kernel/tracing/events/syscalls/sys_exit_fork/enable'
16+
ExecStartPre=/bin/sh -c 'echo 1 > /sys/kernel/tracing/events/syscalls/sys_enter_clone/enable'
17+
ExecStartPre=/bin/sh -c 'echo 1 > /sys/kernel/tracing/events/syscalls/sys_exit_clone/enable'
18+
ExecStartPre=/bin/sh -c 'echo 1 > /sys/kernel/tracing/events/syscalls/sys_enter_connect/enable'
19+
ExecStartPre=/bin/sh -c 'echo 1 > /sys/kernel/tracing/events/syscalls/sys_exit_connect/enable'
20+
ExecStart=uv run python -m linux_edr.cli run --output /var/log/linux-edr/reports.jsonl
1021
Restart=on-failure
1122
RestartSec=5
1223
StandardOutput=journal
1324
StandardError=journal
1425

1526
# Security hardening
1627
ProtectSystem=full
17-
ReadWritePaths=/var/log/linux-edr
28+
ReadWritePaths=/var/log/linux-edr /etc/linux_edr /opt/linux-edr
1829
PrivateTmp=true
19-
NoNewPrivileges=true
30+
NoNewPrivileges=false
31+
CapabilityBoundingSet=CAP_DAC_OVERRIDE CAP_SYS_ADMIN
32+
AmbientCapabilities=CAP_DAC_OVERRIDE CAP_SYS_ADMIN
2033

2134
[Install]
2235
WantedBy=multi-user.target

uninstall_service.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Check if running as root
5+
if [ "$EUID" -ne 0 ]; then
6+
echo "Please run as root to uninstall the service"
7+
exit 1
8+
fi
9+
10+
echo "Uninstalling Linux EDR service..."
11+
12+
# Stop and disable the service if it's running
13+
if systemctl is-active --quiet linux-edr.service; then
14+
echo "Stopping Linux EDR service..."
15+
systemctl stop linux-edr.service
16+
fi
17+
18+
if systemctl is-enabled --quiet linux-edr.service 2>/dev/null; then
19+
echo "Disabling Linux EDR service..."
20+
systemctl disable linux-edr.service
21+
fi
22+
23+
# Remove the systemd service file
24+
if [ -f /etc/systemd/system/linux-edr.service ]; then
25+
echo "Removing systemd service file..."
26+
rm -f /etc/systemd/system/linux-edr.service
27+
systemctl daemon-reload
28+
fi
29+
30+
# Remove the virtual environment
31+
if [ -d "/opt/linux-edr" ]; then
32+
echo "Removing virtual environment..."
33+
rm -rf /opt/linux-edr
34+
fi
35+
36+
# Remove logs
37+
if [ -d "/var/log/linux-edr" ]; then
38+
echo "Removing log directory and all logs..."
39+
rm -rf /var/log/linux-edr
40+
fi
41+
42+
# Remove configuration
43+
if [ -d "/etc/linux_edr" ]; then
44+
echo "Removing configuration directory..."
45+
rm -rf /etc/linux_edr
46+
fi
47+
48+
# Remove the service user
49+
if id "linux-edr" &>/dev/null; then
50+
echo "Removing service user..."
51+
userdel linux-edr
52+
fi
53+
54+
echo "Linux EDR service has been completely uninstalled and all data has been removed."
55+
echo "If you installed any dependencies specifically for this service, you may remove them manually."

0 commit comments

Comments
 (0)