Python Path File (pth) Creation
This rule detects the creation of .pth files in system-wide and user-specific Python package directories, which can be abused for persistent code execution. .pth files automatically execute Python code when the interpreter starts, making them a stealthy persistence mechanism. Monitoring these paths helps identify unauthorized modifications that could indicate persistence by an attacker or malicious package injection.
Elastic rule (View on GitHub)
1[metadata]
2creation_date = "2025/02/26"
3integration = ["endpoint"]
4maturity = "production"
5updated_date = "2025/02/26"
6
7[rule]
8author = ["Elastic"]
9description = """
10This rule detects the creation of .pth files in system-wide and user-specific Python package
11directories, which can be abused for persistent code execution. .pth files automatically
12execute Python code when the interpreter starts, making them a stealthy persistence mechanism.
13Monitoring these paths helps identify unauthorized modifications that could indicate
14persistence by an attacker or malicious package injection.
15"""
16from = "now-9m"
17index = ["logs-endpoint.events.file*"]
18language = "eql"
19license = "Elastic License v2"
20name = "Python Path File (pth) Creation"
21references = [
22 "https://dfir.ch/posts/publish_python_pth_extension/",
23 "https://www.volexity.com/blog/2024/04/12/zero-day-exploitation-of-unauthenticated-remote-code-execution-vulnerability-in-globalprotect-cve-2024-3400/",
24]
25risk_score = 21
26rule_id = "7f65f984-5642-4291-a0a0-2bbefce4c617"
27setup = """## Setup
28
29This rule requires data coming in from Elastic Defend.
30
31### Elastic Defend Integration Setup
32Elastic Defend is integrated into the Elastic Agent using Fleet. Upon configuration, the integration allows the Elastic Agent to monitor events on your host and send data to the Elastic Security app.
33
34#### Prerequisite Requirements:
35- Fleet is required for Elastic Defend.
36- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
37
38#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
39- Go to the Kibana home page and click "Add integrations".
40- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
41- Click "Add Elastic Defend".
42- Configure the integration name and optionally add a description.
43- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
44- Select a configuration preset. Each preset comes with different default settings for Elastic Agent, you can further customize these later by configuring the Elastic Defend integration policy. [Helper guide](https://www.elastic.co/guide/en/security/current/configure-endpoint-integration-policy.html).
45- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
46- Enter a name for the agent policy in "New agent policy name". If other agent policies already exist, you can click the "Existing hosts" tab and select an existing policy instead.
47For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
48- Click "Save and Continue".
49- To complete the integration, select "Add Elastic Agent to your hosts" and continue to the next section to install the Elastic Agent on your hosts.
50For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
51"""
52severity = "low"
53tags = [
54 "Domain: Endpoint",
55 "OS: Linux",
56 "Use Case: Threat Detection",
57 "Tactic: Persistence",
58 "Tactic: Execution",
59 "Tactic: Defense Evasion",
60 "Data Source: Elastic Defend"
61]
62timestamp_override = "event.ingested"
63type = "eql"
64query = '''
65file where host.os.type == "linux" and event.action in ("creation", "rename") and file.extension == "pth" and
66file.path like~ (
67 "/usr/local/lib/python*/dist-packages/*",
68 "/usr/lib/python*/dist-packages/*",
69 "/usr/local/lib/python*/site-packages/*",
70 "/usr/lib/python*/site-packages/*",
71 "/home/*/.local/lib/python*/site-packages/*",
72 "/opt/*/lib/python*/site-packages/*"
73) and process.executable != null and not (
74 process.executable in (
75 "/usr/local/bin/pip2", "/usr/bin/restic", "/usr/bin/pacman", "/usr/bin/dockerd", "/usr/local/bin/pip3",
76 "/usr/bin/pip3", "/usr/local/bin/pip", "/usr/bin/pip", "/usr/bin/podman", "/usr/local/bin/poetry",
77 "/usr/bin/poetry", "/usr/bin/pamac-daemon", "/opt/venv/bin/pip", "/usr/bin/dnf", "./venv/bin/pip",
78 "/usr/bin/dnf5", "/bin/dnf5", "/bin/pip", "/bin/podman"
79 ) or
80 process.executable like~ (
81 "/usr/bin/python*", "/usr/local/bin/python*", "/opt/venv/bin/python*",
82 "/nix/store/*libexec/docker/dockerd", "/snap/docker/*dockerd"
83 )
84)
85'''
86
87[[rule.threat]]
88framework = "MITRE ATT&CK"
89
90[[rule.threat.technique]]
91id = "T1546"
92name = "Event Triggered Execution"
93reference = "https://attack.mitre.org/techniques/T1546/"
94
95[[rule.threat.technique]]
96id = "T1574"
97name = "Hijack Execution Flow"
98reference = "https://attack.mitre.org/techniques/T1574/"
99
100[rule.threat.tactic]
101id = "TA0003"
102name = "Persistence"
103reference = "https://attack.mitre.org/tactics/TA0003/"
104
105[[rule.threat]]
106framework = "MITRE ATT&CK"
107
108[[rule.threat.technique]]
109id = "T1059"
110name = "Command and Scripting Interpreter"
111reference = "https://attack.mitre.org/techniques/T1059/"
112
113[[rule.threat.technique.subtechnique]]
114id = "T1059.004"
115name = "Unix Shell"
116reference = "https://attack.mitre.org/techniques/T1059/004/"
117
118[rule.threat.tactic]
119id = "TA0002"
120name = "Execution"
121reference = "https://attack.mitre.org/tactics/TA0002/"
122
123[[rule.threat]]
124framework = "MITRE ATT&CK"
125
126[rule.threat.tactic]
127id = "TA0005"
128name = "Defense Evasion"
129reference = "https://attack.mitre.org/tactics/TA0005/"
References
Related rules
- Git Hook Egress Network Connection
- Base64 Decoded Payload Piped to Interpreter
- Dynamic Linker Creation or Modification
- Suspicious APT Package Manager Network Connection
- Suspicious Network Connection via systemd