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_activitiesA shared condition, not a detection, so it is not indexed on this site. -
composes · Falco macro
open_writeA shared condition, not a detection, so it is not indexed on this site. -
composes · Falco macro
python_customize_fileA shared condition, not a detection, so it is not indexed on this site. -
composes · Falco macro
python_pth_fileA shared condition, not a detection, so it is not indexed on this site. -
composes · Falco macro
python_pth_installersA shared condition, not a detection, so it is not indexed on this site. -
composes · Falco macro
user_known_python_pth_modifiersA 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