Popular Searches

File Integrity Monitoring (FIM)

CybersecurityBeginner
Practice Now

Introduction

File Integrity Monitoring (FIM) is a critical security control that validates the integrity of operating system and application software files. It works by comparing the current state of files against a known, good baseline. If files are altered, added, or deleted unexpectedly, it could indicate a system compromise or unauthorized changes.

FIM is valuable because many important attacks eventually become file changes. Malware may drop a new executable, a persistence mechanism may alter a startup file, and a careless administrator may unintentionally modify a configuration. Watching for these changes does not tell you why they happened, but it gives you a dependable signal that something on the host is no longer in its expected state.

In this lab, you will use AIDE (Advanced Intrusion Detection Environment), a popular open-source FIM tool for Linux. You will learn how to install AIDE, generate a cryptographic baseline database of your system, simulate a malicious file modification, and finally run an integrity check to detect the unauthorized changes.

Install and Configure AIDE

In this step, you will install the AIDE package, configure it to monitor a specific directory for speed, and initialize its database. AIDE works by creating a database of file hashes (like SHA-256) and metadata (like permissions and ownership) for critical system files. This initial database serves as your "known good" baseline.

The phrase "known good" matters. FIM only works if the baseline is created before compromise or from a trusted source. If you build the baseline after an attacker has already modified the system, the malicious state becomes part of what AIDE treats as normal. That is why defenders care so much about secure provisioning and trusted golden images.

First, ensure you are in your default working directory.

cd ~/project

Install the AIDE package using the APT package manager. The -y flag automatically answers yes to any prompts during the installation process.

sudo apt-get install aide -y

By default, AIDE is configured to scan the entire filesystem, which can take a very long time. For this lab, we will configure AIDE to only monitor the /etc directory (where system configurations are stored). Overwrite the default configuration with a minimal setup:

cat << 'EOF' | sudo tee /etc/aide/aide.conf
database_in=file:/var/lib/aide/aide.db
database_out=file:/var/lib/aide/aide.db.new
database_new=file:/var/lib/aide/aide.db.new
gzip_dbout=yes
CustomRule = p+i+n+u+g+s+m+c+xattrs+sha256
/etc CustomRule
EOF

Now, initialize the AIDE database using the aide --init command. We use the --config flag to explicitly point to our newly created configuration file. This process calculates cryptographic hashes for all the files in /etc.

sudo aide --init --config /etc/aide/aide.conf

When the initialization finishes, you will see an output similar to the following, indicating that a new database has been generated.

AIDE, version 0.17.3

#### AIDE database at /var/lib/aide/aide.db.new initialized.

The newly created database is saved as aide.db.new in the /var/lib/aide/ directory. This prevents accidentally overwriting an existing active database.

Backup the Known Good Database

In this step, you will configure AIDE to use the newly generated database.

Backing up the baseline is not just housekeeping. It is a trust issue. If an attacker gains enough privileges, they may try to replace the baseline so future integrity checks appear clean. In real environments, defenders often protect baseline data separately from the monitored host for exactly this reason.

By default, when AIDE performs an integrity check, it looks for a database file named aide.db. However, as you saw in the previous step, the initialization process creates a file named aide.db.new.

To make your new baseline active, you need to copy or rename the .new file to remove the .new extension. It is a best practice in production environments to copy this database to a secure, read-only location (like an external drive or a secure log server) so attackers cannot tamper with the baseline. For this lab, we will simply copy it within the same directory.

Use the cp command to copy the newly generated database to the active database filename.

sudo cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db

You can verify that the file has been copied successfully by listing the contents of the AIDE library directory.

ls -l /var/lib/aide/

You should see both aide.db and aide.db.new in the output.

total 5800
-rw------- 1 root root 2966881 Oct 25 10:00 aide.db
-rw------- 1 root root 2966881 Oct 25 09:58 aide.db.new

Your system now has an active cryptographic baseline. Any subsequent changes to monitored files will be detected by comparing them against this aide.db file.

Modify Critical System Files

In this step, you will simulate a system compromise by modifying existing configuration files and creating new unauthorized files in critical system directories.

The point of this simulation is to show that integrity monitoring can detect different classes of change. A modified file suggests tampering with an existing asset. A newly added file suggests a dropped tool or persistence artifact. A removed file can suggest cleanup or sabotage. Good host monitoring pays attention to all three.

AIDE is configured by default to monitor the /etc directory, which contains system-wide configuration files. Attackers often target this directory to establish persistence, alter system behavior, or add malicious users.

First, let's simulate an attacker adding a malicious entry to the /etc/hosts file. The /etc/hosts file is used to map IP addresses to hostnames. We will append a comment to the end of this file.

echo "## malicious entry" | sudo tee -a /etc/hosts

Next, let's simulate an attacker dropping a malicious script into the /etc directory. We will use the touch command to create an empty file named malicious_script.sh.

sudo touch /etc/malicious_script.sh

You have now altered the state of the filesystem. The /etc/hosts file has been modified (its hash and size have changed), and a completely new file (/etc/malicious_script.sh) has been added. Because these changes occurred after you created the AIDE baseline, AIDE should flag them as anomalies during the next check.

Run Integrity Check and Analyze

In this final step, you will run an AIDE integrity check to detect the unauthorized changes you made in the previous step.

An integrity check is best thought of as a comparison report, not an automatic verdict. AIDE will tell you what changed, but an analyst still needs to decide whether the change was expected maintenance, a packaging update, or something suspicious. FIM produces high-value evidence, but human interpretation still matters.

The integrity check process recalculates the hashes and metadata for all monitored files and compares them against the values stored in the active database (/var/lib/aide/aide.db).

Execute the AIDE check command, pointing it to our custom configuration file. This process may take a few seconds to complete.

sudo aide --check --config /etc/aide/aide.conf

Once the check is complete, AIDE will output a detailed report to the terminal. The report summarizes the differences found between the current filesystem and the baseline database.

Look closely at the output. You should see a summary indicating that files were added and changed.

AIDE 0.17.3 found differences between database and filesystem!!
Start timestamp: 2026-10-25 10:05:00

Summary:
  Total number of files:        15234
  Added files:                  1
  Removed files:                0
  Changed files:                1

---------------------------------------------------
Added files:
---------------------------------------------------

f++++++++++++++++: /etc/malicious_script.sh

---------------------------------------------------
Changed files:
---------------------------------------------------

f   ...    .  .. : /etc/hosts

The report clearly identifies /etc/malicious_script.sh as an added file and /etc/hosts as a changed file. In a real-world scenario, a system administrator would review this report, investigate the unauthorized changes, and take appropriate incident response actions.

After reviewing the report, if the changes were legitimate (e.g., you intentionally updated a configuration file), you would need to update the AIDE database by running sudo aide --update and replacing the old database with the new one, just as you did in Step 2.

Summary

In this lab, you successfully implemented File Integrity Monitoring (FIM) on a Linux server using AIDE. You learned how to install the AIDE package, initialize a cryptographic baseline database, and activate it. By simulating a system compromise through file modifications and additions, you demonstrated how AIDE detects unauthorized changes by comparing the current filesystem state against the known good baseline. This foundational security skill is essential for detecting intrusions and maintaining the integrity of critical system configurations.