Modify Python Startup Hook File


Description

Detect attempts to create or modify Python startup hook files, primarily aimed at establishing persistence: path configuration (.pth) files in a site-packages or dist-packages directory, and sitecustomize or usercustomize modules in a directory on the default sys.path. When Python's site module processes a .pth file in an active site directory, lines beginning with import followed by a space or tab are executed. The site module also imports sitecustomize and, if the user site directory is enabled, usercustomize. Both allow attacker code to run at startup for interpreters using that environment. This is the Python counterpart of the shell configuration persistence covered by "Modify Shell Configuration File". Package managers, common Python installers (pip in its various invocation forms, ensurepip/venv, virtualenv, pipx, uv) and container image unpacking legitimately write these files and are excluded. These exclusions are based on process name and command line and can therefore be imitated by an attacker, and a malicious package installed through a package manager is out of scope for this rule. Files moved into place with rename, and sitecustomize modules placed in a directory added through PYTHONPATH, are not detected. Use user_known_python_pth_modifiers to tune the rule for your environment.

Query · falco

open_write and (python_pth_file or python_customize_file) and not proc.name in (package_mgmt_binaries) and not python_pth_installers and not containerd_activities and not user_known_python_pth_modifiers

Rule dependencies

Depends on

  • composes · Falco macro containerd_activities
    A shared condition, not a detection, so it is not indexed on this site.
  • composes · Falco macro open_write
    A shared condition, not a detection, so it is not indexed on this site.
  • composes · Falco macro python_customize_file
    A shared condition, not a detection, so it is not indexed on this site.
  • composes · Falco macro python_pth_file
    A shared condition, not a detection, so it is not indexed on this site.
  • composes · Falco macro python_pth_installers
    A shared condition, not a detection, so it is not indexed on this site.
  • composes · Falco macro user_known_python_pth_modifiers
    A shared condition, not a detection, so it is not indexed on this site.

Analyst notes

A Python startup hook file has been modified | file=%fd.name pcmdline=%proc.pcmdline evt_type=%evt.type user=%user.name user_uid=%user.uid user_loginuid=%user.loginuid process=%proc.name proc_exepath=%proc.exepath parent=%proc.pname command=%proc.cmdline terminal=%proc.tty

Raw source Modify Python Startup Hook File · Falco YAML
Esc
Published by falcosecurity/rules ↗, licensed under Apache 2.0 ↗. Reproduced here unmodified.
rule: Modify Python Startup Hook File
desc: 'Detect attempts to create or modify Python startup hook files, primarily aimed
  at establishing persistence: path configuration (.pth) files in a site-packages
  or dist-packages directory, and sitecustomize or usercustomize modules in a directory
  on the default sys.path. When Python''s site module processes a .pth file in an
  active site directory, lines beginning with import followed by a space or tab are
  executed. The site module also imports sitecustomize and, if the user site directory
  is enabled, usercustomize. Both allow attacker code to run at startup for interpreters
  using that environment. This is the Python counterpart of the shell configuration
  persistence covered by "Modify Shell Configuration File". Package managers, common
  Python installers (pip in its various invocation forms, ensurepip/venv, virtualenv,
  pipx, uv) and container image unpacking legitimately write these files and are excluded.
  These exclusions are based on process name and command line and can therefore be
  imitated by an attacker, and a malicious package installed through a package manager
  is out of scope for this rule. Files moved into place with rename, and sitecustomize
  modules placed in a directory added through PYTHONPATH, are not detected. Use user_known_python_pth_modifiers
  to tune the rule for your environment.

  '
condition: 'open_write and (python_pth_file or python_customize_file) and not proc.name
  in (package_mgmt_binaries) and not python_pth_installers and not containerd_activities
  and not user_known_python_pth_modifiers

  '
output: A Python startup hook file has been modified | file=%fd.name pcmdline=%proc.pcmdline
  evt_type=%evt.type user=%user.name user_uid=%user.uid user_loginuid=%user.loginuid
  process=%proc.name proc_exepath=%proc.exepath parent=%proc.pname command=%proc.cmdline
  terminal=%proc.tty
priority: WARNING
tags:
- maturity_incubating
- host
- container
- filesystem
- mitre_persistence
- T1546.018

Detection rules belong to the projects that publish them and remain under their own licenses. This site indexes and links to them; it claims no rights in them.