|
| 1 | +# Persistence via Loadable Kernel Modules |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## Metadata |
| 6 | + |
| 7 | +- **Author:** Elastic |
| 8 | +- **Description:** This hunt identifies potential persistence mechanisms leveraging Loadable Kernel Modules (LKMs) on Linux systems. LKMs enable dynamic extension of kernel functionality but can be abused by attackers to load malicious code into the kernel, granting them high privileges or persistence. This hunt monitors suspicious kernel module file creations, LKM-related process executions, and access to kernel module configuration files. |
| 9 | + |
| 10 | +- **UUID:** `d667d328-fadc-4a52-9b46-f42b1a83181c` |
| 11 | +- **Integration:** [endpoint](https://docs.elastic.co/integrations/endpoint) |
| 12 | +- **Language:** `[ES|QL, SQL]` |
| 13 | +- **Source File:** [Persistence via Loadable Kernel Modules](../queries/persistence_via_loadable_kernel_modules.toml) |
| 14 | + |
| 15 | +## Query |
| 16 | + |
| 17 | +```sql |
| 18 | +from logs-endpoint.events.file-* |
| 19 | +| keep @timestamp, host.os.type, event.type, event.action, file.extension, file.path, process.executable, agent.id |
| 20 | +| where @timestamp > now() - 30 day |
| 21 | +| where host.os.type == "linux" and event.type == "creation" and file.extension == "ko" and not ( |
| 22 | + // Add your exclusions here |
| 23 | + file.path like "/run/initramfs/*" or |
| 24 | + file.path like "/var/tmp/mkinitramfs*" |
| 25 | +) |
| 26 | +| stats cc = count(), agent_count = count_distinct(agent.id) by file.path, process.executable |
| 27 | +| where agent_count <= 3 |
| 28 | +| sort cc asc |
| 29 | +| limit 100 |
| 30 | +``` |
| 31 | + |
| 32 | +```sql |
| 33 | +from logs-endpoint.events.process-* |
| 34 | +| keep @timestamp, host.os.type, event.type, event.action, process.name, agent.id, process.args, process.args_count |
| 35 | +| where @timestamp > now() - 30 days |
| 36 | +| where host.os.type == "linux" and event.type == "start" and event.action == "exec" and process.name in ("kmod", "modprobe", "insmod", "rmmod") |
| 37 | +| stats cc = count(), agent_count = count_distinct(agent.id) by process.args, process.args_count |
| 38 | +| where cc == 1 and agent_count == 1 and process.args_count <= 3 |
| 39 | +| sort cc asc |
| 40 | +| limit 100 |
| 41 | +``` |
| 42 | + |
| 43 | +```sql |
| 44 | +SELECT |
| 45 | + f.filename, |
| 46 | + f.path, |
| 47 | + u.username AS file_owner, |
| 48 | + g.groupname AS group_owner, |
| 49 | + datetime(f.atime, 'unixepoch') AS file_last_access_time, |
| 50 | + datetime(f.mtime, 'unixepoch') AS file_last_modified_time, |
| 51 | + datetime(f.ctime, 'unixepoch') AS file_last_status_change_time |
| 52 | + datetime(f.btime, 'unixepoch') AS file_created_time, |
| 53 | + f.size AS size_bytes |
| 54 | +FROM |
| 55 | + file f |
| 56 | +LEFT JOIN |
| 57 | + users u ON f.uid = u.uid |
| 58 | +LEFT JOIN |
| 59 | + groups g ON f.gid = g.gid |
| 60 | +WHERE |
| 61 | + f.path LIKE '/etc/modprobe.d/%' |
| 62 | + OR f.path LIKE '/usr/lib/modprobe.d/%' |
| 63 | + OR f.path LIKE '/usr/lib/security/%' |
| 64 | + OR f.path LIKE '/etc/modules-load.d/%' |
| 65 | + OR f.path LIKE '/run/modules-load.d/%' |
| 66 | + OR f.path LIKE '/usr/local/lib/modules-load.d/%' |
| 67 | + OR f.path like '/usr/lib/modules-load.d/%' |
| 68 | + OR f.path = '/etc/modules' |
| 69 | +``` |
| 70 | + |
| 71 | +```sql |
| 72 | +SELECT * FROM kernel_modules; |
| 73 | +``` |
| 74 | + |
| 75 | +## Notes |
| 76 | + |
| 77 | +- Tracks the creation of loadable kernel module files (.ko) in non-standard directories to identify potential malicious modules. |
| 78 | +- Monitors the execution of processes related to kernel module management, such as kmod, modprobe, insmod, and rmmod, to detect suspicious or unusual activity. |
| 79 | +- Identifies changes to critical kernel module configuration files, including /etc/modprobe.d/, /etc/modules, and related paths. |
| 80 | +- Uses OSQuery queries to gather detailed metadata on kernel modules currently loaded, supporting forensic analysis of potential persistence mechanisms. |
| 81 | +- Provides statistics and counts to help identify rare or anomalous kernel module-related events. |
| 82 | + |
| 83 | +## MITRE ATT&CK Techniques |
| 84 | + |
| 85 | +- [T1547.006](https://attack.mitre.org/techniques/T1547/006) |
| 86 | + |
| 87 | +## License |
| 88 | + |
| 89 | +- `Elastic License v2` |
0 commit comments