Query Details

Nginx Worker Spawning Shells And Scripting Tools On Linux

Query

let lookback = 1d;
let nginxParents = dynamic(["nginx", "nginx-debug"]);
let shellNames = dynamic(["sh", "bash", "dash", "ash", "zsh", "ksh"]);
let suspiciousTools = dynamic(["python", "python3", "perl", "ruby", "php", "node", "lua", "awk", "gawk",
    "nc", "ncat", "netcat", "socat", "openssl", "telnet", "curl", "wget", "ssh", "sftp", "busybox"]);
let reverseShellPatterns = dynamic(["/dev/tcp", "bash -i", "mkfifo", "base64 -d",
    "-e /bin/sh", "-e /bin/bash", "| sh", "| bash"]);
union isfuzzy=true
(
    // Process level: shell or tool started by nginx, or nested under nginx
    DeviceProcessEvents
    | where Timestamp >= ago(lookback)
    | where InitiatingProcessFileName in~ (nginxParents)
        or (InitiatingProcessFileName in~ (shellNames) and InitiatingProcessParentFileName in~ (nginxParents))
    | where FileName in~ (shellNames) or FileName in~ (suspiciousTools)
    | extend AttackMethod = case(
        ProcessCommandLine has_any (reverseShellPatterns), "Reverse shell pattern in command line",
        InitiatingProcessFileName in~ (nginxParents), "Direct child process of nginx worker (code execution)",
        "Nested child process nginx -> shell -> tool")
    | project Timestamp, DeviceName, AttackMethod,
        Reason = strcat(
            "Initiator=", InitiatingProcessFileName,
            " Parent=", InitiatingProcessParentFileName,
            " Child=", FileName,
            iff(ProcessCommandLine has_any (reverseShellPatterns), " | cmdline matched reverse shell pattern", "")),
        Detail = tostring(ProcessCommandLine),
        Context = tostring(InitiatingProcessCommandLine)
),
(
    // File level: files written by nginx to temp locations
    DeviceFileEvents
    | where Timestamp >= ago(lookback)
    | where InitiatingProcessFileName in~ (nginxParents)
    | where FolderPath starts

About this query

Explanation

This query is designed to detect potentially malicious activities on Linux systems where the nginx web server might be exploited to execute unauthorized code. Here's a simplified breakdown:

  1. Purpose: The query checks for suspicious activities involving the nginx web server on Linux systems monitored by Microsoft Defender for Endpoint. It looks for signs that nginx is being used to execute code, which could indicate a security breach.

  2. Key Activities Monitored:

    • Process Monitoring: It examines processes started by nginx or its worker processes. Specifically, it looks for shell processes (like bash or sh) that are initiated by nginx and then start other scripting or networking tools (like python, perl, curl, wget, etc.). This behavior is typical of web shells or exploited web applications.
    • Reverse Shell Detection: The query identifies command lines that contain patterns commonly used in reverse shells, such as /dev/tcp, bash -i, or mkfifo. These are flagged as potential security threats.
    • File Monitoring: It checks for files written by nginx to temporary directories like /tmp, /dev/shm, or /var/tmp, which might indicate staging of malicious payloads.
  3. Time Frame: The query looks back over the past day to find these activities.

  4. Customization: The lists of shells, interpreters, and reverse shell patterns can be adjusted to fit specific environments.

  5. False Positives: The query might flag legitimate activities, such as nginx hooks or deployment scripts that use shells, or applications that store temporary files in the monitored directories. Therefore, results should be cross-referenced with web application logs and the affected host's context before taking action.

  6. Limitations: The query might miss payloads that execute entirely in memory without creating new processes, or those using embedded nginx modules or interpreters that don't appear as separate processes.

Overall, this query helps identify potential security threats by monitoring for unusual process and file activities associated with the nginx web server.