Guest User

inotifywait

a guest
Dec 16th, 2025
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. r3j3ct
  2.  
  3. Of course. This is an excellent setup for real-time monitoring. I'll guide you through configuring `inotifywait` to act as a bridge between the filesystem and the Linux Malware Detect (LMD) `clamav` integration.
  4.  
  5. The goal is to have `inotifywait` watch for file changes (creates, closes) in key directories and then trigger an `lmd` scan on that specific file.
  6.  
  7. ### Prerequisites (Verify First)
  8.  
  9. 1. **ClamAV is installed and the daemon (`clamav-daemon`) is running:**
  10. ```bash
  11. sudo systemctl status clamav-daemon
  12. # or
  13. sudo systemctl status clamav-freshclam
  14. ```
  15.  
  16. 2. **Linux Malware Detect (LMD) is installed and integrated with ClamAV:**
  17. This is usually done during LMD's configuration. Check that the `scan_clamscan` option is enabled in `/usr/local/maldetect/conf.maldet`.
  18. ```bash
  19. sudo grep scan_clamscan /usr/local/maldetect/conf.maldet
  20. ```
  21. It should return: `scan_clamscan="1"`.
  22.  
  23. ### Step 1: Create the Monitoring Script
  24.  
  25. We'll create a script that uses `inotifywait` to monitor directories and call `maldet` to scan any new or modified files.
  26.  
  27. 1. Open a text editor with sudo privileges to create the script. A good place for system scripts is `/usr/local/sbin/`.
  28. ```bash
  29. sudo nano /usr/local/sbin/inotify-maldet.sh
  30. ```
  31.  
  32. 2. **Copy and paste the following script.** I'll explain the key parts below.
  33.  
  34. ```bash
  35. #!/bin/bash
  36.  
  37. # Directories to monitor. Add more with a space separator.
  38. # e.g., MONITOR_DIR="/home /www /var/www"
  39. MONITOR_DIR="/home /tmp /var/www"
  40.  
  41. # ClamAV/LMD log file
  42. LOG_FILE="/var/log/inotify-maldet.log"
  43.  
  44. # Inotifywait pattern. We watch for 'close_write' (file saved) and 'moved_to'
  45. INOTIFY_EVENTS="close_write,moved_to"
  46.  
  47. # Maldet path
  48. MALDET_SCAN="/usr/local/maldetect/maldet"
  49.  
  50. # Init log
  51. echo "$(date) - Starting inotifywait monitoring for LMD on: $MONITOR_DIR" >> "$LOG_FILE"
  52.  
  53. # Start monitoring
  54. inotifywait -r -m -e "$INOTIFY_EVENTS" $MONITOR_DIR --format '%w%f' | while read FILE
  55. do
  56. # Check if the file exists (to avoid errors on moved/deleted files)
  57. if [ -f "$FILE" ]; then
  58. # Avoid scanning files that are obviously not threats to reduce CPU load
  59. # Example: skip .log, .tmp files. Add or remove extensions as needed.
  60. case "$FILE" in
  61. *.log|*.tmp)
  62. # Skip these file types
  63. continue
  64. ;;
  65. esac
  66.  
  67. # Run maldet on the specific file.
  68. # The '--background' flag is CRUCIAL to avoid fork bombs.
  69. # It tells maldet to run the scan as a background process.
  70. echo "$(date) - Scanning file: $FILE" >> "$LOG_FILE"
  71. $MALDET_SCAN --background -s "$FILE" >> "$LOG_FILE" 2>&1
  72. fi
  73. done
  74. ```
  75.  
  76. **Script Explanation:**
  77. * `MONITOR_DIR`: Define the directories you want to watch. **This is the most important setting.** Start with `/home` and `/tmp`. Add web directories like `/var/www` if you run a server.
  78. * `LOG_FILE`: Where all output from the script and `maldet` will go. Essential for debugging.
  79. * `INOTIFY_EVENTS`: `close_write,moved_to` is efficient. It triggers when a file is finished being written to or is moved into the directory.
  80. * The `case` statement: This is a simple filter to skip scanning certain harmless file types, reducing unnecessary CPU usage.
  81. * `--background`: **This is critical.** Without it, if a process creates many files quickly, each `maldet` scan would run in the foreground and could quickly consume all system resources, creating a fork bomb scenario. `--background` prevents this.
  82.  
  83. 3. **Make the script executable:**
  84. ```bash
  85. sudo chmod +x /usr/local/sbin/inotify-maldet.sh
  86. ```
  87.  
  88. ### Step 2: Create a Systemd Service (Recommended)
  89.  
  90. The best way to manage a long-running script like this is with a systemd service. It ensures the script starts on boot and can be easily controlled.
  91.  
  92. 1. Create a service file:
  93. ```bash
  94. sudo nano /etc/systemd/system/inotify-maldet.service
  95. ```
  96.  
  97. 2. Paste the following configuration:
  98. ```ini
  99. [Unit]
  100. Description=Inotifywait monitoring with Linux Malware Detect
  101. After=network.target clamav-daemon.service maldet.service
  102. Wants=clamav-daemon.service maldet.service
  103.  
  104. [Service]
  105. Type=simple
  106. ExecStart=/usr/local/sbin/inotify-maldet.sh
  107. Restart=on-failure
  108. RestartSec=5s
  109.  
  110. [Install]
  111. WantedBy=multi-user.target
  112. ```
  113. * `After=...`: Ensures this service starts *after* ClamAV and LMD are ready.
  114. * `Restart=on-failure`: Automatically restarts the script if it crashes.
  115.  
  116. 3. Reload systemd to recognize the new service, then start and enable it:
  117. ```bash
  118. sudo systemctl daemon-reload
  119. sudo systemctl start inotify-maldet.service
  120. sudo systemctl enable inotify-maldet.service
  121. ```
  122.  
  123. ### Step 3: Verify and Monitor
  124.  
  125. 1. **Check if the service is running:**
  126. ```bash
  127. sudo systemctl status inotify-maldet.service
  128. ```
  129. Look for `active (running)`.
  130.  
  131. 2. **Check the log file for initial messages:**
  132. ```bash
  133. sudo tail -f /var/log/inotify-maldet.log
  134. ```
  135. You should see the "Starting inotifywait..." message.
  136.  
  137. 3. **Test it!** Create a test file in one of your monitored directories (e.g., `/tmp`):
  138. ```bash
  139. touch /tmp/test_file.txt
  140. ```
  141. Immediately check the log again. You should see a new line indicating the file was scanned.
  142. ```bash
  143. sudo tail -f /var/log/inotify-maldet.log
  144. # Output should look like:
  145. # Sun Mar 2 12:00:00 UTC 2025 - Scanning file: /tmp/test_file.txt
  146. ```
  147.  
  148. ### Important Considerations and Tips
  149.  
  150. * **Performance:** Real-time scanning has a CPU and I/O cost. The `case` statement in the script helps. Monitor your system load after enabling this. Avoid monitoring very large, volatile directories (e.g., a database storage location).
  151. * **Testing EICAR:** To *really* test it, download the EICAR test virus file. The scan should detect it immediately.
  152. ```bash
  153. wget https://secure.eicar.org/eicar.com.txt -P /tmp/
  154. ```
  155. * **Updating Malware Definitions:** Remember, this real-time scanner is only as good as its definitions. Ensure the `maldet` and `clamav` update services are working:
  156. ```bash
  157. sudo systemctl status maldet.service
  158. sudo systemctl status clamav-freshclam.service
  159. ```
  160. * **Troubleshooting:** If it doesn't work, the log file (`/var/log/inotify-maldet.log`) is your first place to look for errors.
  161.  
  162. This configuration provides a robust, real-time shield that leverages the combined power of ClamAV's engine and LMD's specialized ruleset.
Advertisement
Add Comment
Please, Sign In to add comment