Use case
On Linux endpoints, there's no built-in mechanism to detect malicious files at the point of write or execution. Two high-value detection moments are currently unaddressed:
- Webshell drop — a PHP/JSP/ASPX file written to a web-accessible directory (e.g.
/var/www, /srv/http) by a process that shouldn't be writing there (web server, CGI handler, compromised app).
- Webshell execution — the interpreter (
php, python, perl) executing a file that matches a known-malicious YARA rule.
Without on-access scanning, detection depends entirely on scheduled scans or log-based correlation, which introduces latency and misses fileless variants dropped and deleted quickly.
Proposed solution
Implement YARA on-access scanning using fanotify, which is the standard Linux kernel interface for filesystem event interception. Two event types are relevant:
FAN_CLOSE_WRITE — fires when a file opened for writing is closed; use this to scan newly written files before they become available for execution.
FAN_OPEN_EXEC — fires when a file is about to be executed; use this as a secondary gate.
To minimize overhead:
- Apply scanning only on
FAN_CLOSE_WRITE by default; exec scanning is optional/configurable since it fires on every binary invocation system-wide.
- Scope fanotify mounts to configurable path prefixes (e.g.
/var/www, /tmp, /dev/shm) rather than watching the full filesystem.
- Add a file extension filter before invoking YARA (e.g. only scan
.php, .py, .pl, .sh, .so, .elf — configurable).
- Add an inode/hash cache for recently scanned, unmodified files to avoid rescanning on repeated opens.
- Run YARA evaluation in a dedicated thread pool with a bounded queue; drop or log-only if the queue is saturated (non-blocking mode).
- Expose a blocking mode toggle (via fanotify's
FAN_ACCESS_PERM) for environments that can accept the latency tradeoff in exchange for active prevention.
Alternatives considered
- Scheduled YARA scans — already implementable externally; doesn't catch short-lived drops or act at execution time.
- inotify — cannot intercept or block, only notify after the fact; also less suitable for exec events.
- eBPF-based hooks — more flexible but significantly more complex to implement and maintain; kernel version dependencies. Could be a follow-up.
- Hash-based detection — low value for webshells since they're trivially modified; YARA string/regex matching is more resilient.
Additional context
- fanotify docs: man7.org/linux/man-pages/man7/fanotify.7.html —
FAN_OPEN_EXEC requires Linux ≥ 5.0; FAN_CLOSE_WRITE is available from 3.x.
- ClamAV's
clamonacc uses fanotify in a similar pattern and is a useful implementation reference.
Use case
On Linux endpoints, there's no built-in mechanism to detect malicious files at the point of write or execution. Two high-value detection moments are currently unaddressed:
/var/www,/srv/http) by a process that shouldn't be writing there (web server, CGI handler, compromised app).php,python,perl) executing a file that matches a known-malicious YARA rule.Without on-access scanning, detection depends entirely on scheduled scans or log-based correlation, which introduces latency and misses fileless variants dropped and deleted quickly.
Proposed solution
Implement YARA on-access scanning using fanotify, which is the standard Linux kernel interface for filesystem event interception. Two event types are relevant:
FAN_CLOSE_WRITE— fires when a file opened for writing is closed; use this to scan newly written files before they become available for execution.FAN_OPEN_EXEC— fires when a file is about to be executed; use this as a secondary gate.To minimize overhead:
FAN_CLOSE_WRITEby default; exec scanning is optional/configurable since it fires on every binary invocation system-wide./var/www,/tmp,/dev/shm) rather than watching the full filesystem..php,.py,.pl,.sh,.so,.elf— configurable).FAN_ACCESS_PERM) for environments that can accept the latency tradeoff in exchange for active prevention.Alternatives considered
Additional context
FAN_OPEN_EXECrequires Linux ≥ 5.0;FAN_CLOSE_WRITEis available from 3.x.clamonaccuses fanotify in a similar pattern and is a useful implementation reference.