LoFP LoFP / developers and administrators may use php cli or phpdbg for legitimate automation and troubleshooting. review the command line, parent process, user, host, and execution context. tune approved automation by host or parent process as needed.

Techniques

Sample rules

Inline PHP Code Execution

Description

The following analytic detects PHP command-line interpreters executing code supplied directly on the command line with the CLI -r / –run options, per-line execution flags, non-empty auto-prepend or auto-append settings, or code supplied to PHPDBG through standard input. It covers common CLI executable names on Linux and Windows, including php.exe, php-win.exe, the legacy php-cli.exe name, versioned Linux php binaries, phpdbg, and FrankenPHP’s php-cli subcommand. PHP-CGI and PHP-FPM are excluded because they do not support the PHP CLI inline -r option. This behavior can be used legitimately for administration and automation, but unexpected or obfuscated inline PHP execution may indicate an attacker using an installed interpreter to run commands without dropping a script file.

Detection logic


| tstats `security_content_summariesonly`
  count min(_time) as firstTime
        max(_time) as lastTime

FROM datamodel=Endpoint.Processes WHERE
(
    (
        Processes.process_name IN (
            "php-cli.exe",
            "php-cli",
            "php-win.exe",
            "php.exe",
            "php",
            "php5*",
            "php7*",
            "php8*",
            "php9*"
        )
        NOT Processes.process_name IN ("*cgi*", "*fpm*")
        Processes.process IN (
            "* -r *", "* --run *", "* --run=*", "* -r\"*", "* -r'*", "* -r$*",
            "* -B *", "* -R *", "* -E *", "* -F *",
            "* --process-begin *", "* --process-code *", "* --process-file *", "* --process-end *",
            "*auto_prepend_file=*", "*auto_append_file=*", "*allow_url_include=*"
        )
    )
    OR
    (
        Processes.process_name IN ("phpdbg", "phpdbg.exe")
        Processes.process IN ("* -s", "* -s ", "* -s=*")
    )
    OR
    (
        Processes.process_name IN ("frankenphp", "frankenphp.exe")
        Processes.process="*php-cli*"
        Processes.process IN ("* -r *", "* -r\"*", "* -r'*", "* -r$*")
    )
)

BY Processes.action Processes.dest Processes.original_file_name
    Processes.parent_process Processes.parent_process_exec Processes.parent_process_guid
    Processes.parent_process_id Processes.parent_process_name Processes.parent_process_path
    Processes.process Processes.process_exec Processes.process_guid
    Processes.process_hash Processes.process_id Processes.process_integrity_level
    Processes.process_name Processes.process_path Processes.user
    Processes.user_id Processes.vendor_product


| `drop_dm_object_name(Processes)`


| eval php_inline_r=if(match(process, "(^
|\s)(-r
|--run)(\s
|=
|\"
|'
|\$)"), 1, 0)

| eval php_per_line=if(match(process, "(^
|\s)(-[BREF]
|--process-(begin
|code
|file
|end))(\s
|$)"), 1, 0)

| eval php_ini_inject=if(match(process, "(?i)-d\s*\"?(auto_prepend_file
|auto_append_file)\s*=\s*\"?[^\s\"']"), 1, 0)

| eval php_allow_url_include=if(match(process, "(?i)-d\s*\"?allow_url_include\s*=\s*\"?(?:1
|on
|true
|yes)(?=\s
|\"
|'
|$)"), 1, 0)

| eval phpdbg_or_franken=if(match(process_name, "(?i)^(phpdbg
|frankenphp)"), 1, 0)


| where (
    php_inline_r=1
    OR php_per_line=1
    OR php_ini_inject=1
    OR php_allow_url_include=1
    OR phpdbg_or_franken=1
)


| where NOT (
    lower(parent_process_name)="cmd.exe"
    AND like(lower(process), "%\\.config\\herd\\bin\\php8%\\php.exe%")
    AND like(lower(process), "% -r \"define('laravel_start'%")
    AND like(lower(process), "%vendor/autoload.php%")
    AND like(lower(process), "%bootstrap/app.php%")
)


| `security_content_ctime(firstTime)`

| `security_content_ctime(lastTime)`

| `inline_php_code_execution_filter`

Linux Auditd Inline PHP Code Execution

Description

The following analytic detects PHP command-line interpreters executing code supplied directly on the command line with CLI code-execution options such as -r / –run and -B / -R / -E / -F, or PHP runtime INI overrides that can load code. It also detects code supplied to PHPDBG through the -s option and FrankenPHP php-cli execution. It covers common Linux CLI executable names, including php, versioned PHP binaries, phpdbg, and FrankenPHP. PHP-CGI and PHP-FPM are excluded from the CLI option branch because they do not support the PHP CLI inline -r option. This behavior can be used legitimately for administration and automation, but unexpected or obfuscated inline PHP execution may indicate an attacker using an installed interpreter to run commands without dropping a script file.

Detection logic

`linux_auditd`
type=EXECVE
"*php*"


| eval process_name=lower(replace(coalesce(a0, ""), "^.*/", ""))

| eval process=coalesce(execve_command, _raw)

| rex "msg=audit\([^:]+:(?<audit_id>\d+)\)"


| eval php_code_option=if(
    match(process, "(^
|[[:space:]])(-r
|--run
|-B
|-R
|-E
|-F
|--process-begin
|--process-code
|--process-file
|--process-end)(=
|[$[:space:]]
|$)")
    OR match(_raw, "a[0-9]+=\"(-r
|--run
|-B
|-R
|-E
|-F
|--process-begin
|--process-code
|--process-file
|--process-end)(=
|[$[:space:]]
|\"
|$)"),
    1, 0
  )

| eval php_ini_override=if(
    match(process, "(^
|[[:space:]])(-d[[:space:]]+
|-d
|--define[[:space:]]+
|--define=)(auto_prepend_file
|auto_append_file
|allow_url_include)=")
    OR match(_raw, "a[0-9]+=\"(-d
|--define)\" a[0-9]+=\"(auto_prepend_file
|auto_append_file
|allow_url_include)=")
    OR match(_raw, "a[0-9]+=\"-d(auto_prepend_file
|auto_append_file
|allow_url_include)=")
    OR match(_raw, "a[0-9]+=\"--define=(auto_prepend_file
|auto_append_file
|allow_url_include)=")
    , 1, 0
  )


| where (
    match(process_name, "^php([5-9](\\.[0-9]+)*)?(-cli)?$")
    AND (php_code_option=1 OR php_ini_override=1)
    AND NOT match(process_name, "(cgi
|fpm)")
  )
  OR (
    process_name="phpdbg"
    AND (
      match(process, "(^
|[[:space:]])-s(=
|[$[:space:]]
|$)")
      OR match(_raw, "a[0-9]+=\"-s(=
|[$[:space:]]
|\"
|$)")
    )
  )
  OR (
    process_name="frankenphp"
    AND match(process, "php-cli")
    AND (php_code_option=1 OR php_ini_override=1)
  )


| rename host as dest


| stats count min(_time) as firstTime max(_time) as lastTime
    values(execve_command) as execve_command
    values(a0) as argv0
    values(auid) as auid
    values(uid) as uid
    values(audit_id) as audit_id
    by dest process_name process


| `security_content_ctime(firstTime)`

| `security_content_ctime(lastTime)`

| `linux_auditd_inline_php_code_execution_filter`