{"id":11067,"date":"2021-12-09T22:42:02","date_gmt":"2021-12-09T19:42:02","guid":{"rendered":"https:\/\/kifarunix.com\/?p=11067"},"modified":"2024-03-18T07:50:29","modified_gmt":"2024-03-18T04:50:29","slug":"detect-changes-to-critical-files-in-linux-using-auditbeat-and-elk","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/detect-changes-to-critical-files-in-linux-using-auditbeat-and-elk\/","title":{"rendered":"Detect Changes to Critical Files in Linux using Auditbeat and ELK"},"content":{"rendered":"\n
In this tutorial, you will learn how to detect changes to critical files in Linux using Auditbeat and ELK. Auditbeat is one of the elastic beats that according to Elastic page<\/a>, collects Linux audit framework data and monitor the integrity of the files. It ships these events in real time to the rest of the Elastic Stack for further analysis<\/em>. It enables you to find out who was the actor?<\/em> What action did they perform and when?<\/em>.<\/p>\n\n\n\n To begin with, you need to have a running ELK stack. We use Debian 11 in this setup. Hence, you can follow the link below setup ELK;<\/p>\n\n\n\n Install ELK Stack on Debian 11<\/a><\/p>\n\n\n\n In this tutorial, we will use two hosts, Ubuntu and Rocky Linux vms as our remote hosts to monitor for any changes in the critical files.<\/p>\n\n\n\n Run the commands below to install Auditbeat on Ubuntu\/Debian based systems;<\/p>\n\n\n\n Run the commands below to install Auditbeat on RHEL based systems;<\/p>\n\n\n\n Once the installation is done, open the Auditbeat configuration file for editing;<\/p>\n\n\n\n <\/p>\n\n\n\n Navigate to Elasticsearch Output configuration section;<\/p>\n\n\n\n Set the output configurations depending on how your Elasticsearch is configured. In my setup, i just need to update the Elasticsearch IP address and port by replacing Save the changes and exit the configuration file.<\/p>\n\n\n\n Ensure the Elasticsearch port is opened and you can connect to it;<\/p>\n\n\n\n Next, test the Auditbeat connection to Elasticsearch (Ensure auditbeat service is not running before you ran this command<\/em>);<\/p>\n\n\n\n Next, you need to configure Auditbeat to collect system audit data that needs to be shipped to Elasticsearch.<\/p>\n\n\n\n In the default Auditbeat configuration file, Auditbeat supports different modules that simplifies the collection, parsing, and visualization of various audit data.<\/p>\n\n\n\n These modules include:<\/p>\n\n\n\n In this setup, we will go with the default configurations.<\/p>\n\n\n\n If you want to monitor another file\/directory, for integrity changes, add it under the file_integrity module.<\/p>\n\n\n\n Whenever you make changes to Auditbeat configuration, ensure that you validate the config.<\/p>\n\n\n\n If the output is Run the command below to load Auditbeat index template:<\/p>\n\n\n\n Output;<\/p>\n\n\n\n Run this command to load visualization dashboards to Elasticsearch;<\/p>\n\n\n\n You can run these command only once on a single system. No need to run on other hosts running audibeat.<\/p>\n\n\n\n Ensure both Elasticsearch and Kibana are reachable from the host.<\/p>\n\n\n\n Run the command below to update Auditbeat logging;<\/p>\n\n\n\n Update the Limit on the size of the process executable that will be hashed. Default is \"100 MiB\". With the default value, you may see a warning like:<\/p>\n\n\n\n You can now start and enable Auditbeat service;<\/p>\n\n\n\n Checking the status;<\/p>\n\n\n\n Go to Kibana web interface and navigate to the Menu > Management > Stack Management > Data > Index management<\/strong>.<\/p>\n\n\n\n You should see an Create Auditbeat Kibana Index pattern by navigating to Menu > Management > Stack Management > Kibana > Index Pattern > Create Index Pattern<\/strong>.<\/p>\n\n\n\n Define the name of the index and select the timestamp field.<\/p>\n\n\n\n Then click Create index pattern<\/strong>.<\/p>\n\n\n\n If you navigate to Discover<\/strong> tab and selecting the index pattern just created, auditbeat-*<\/strong>, you will the events.<\/p>\n\n\n\n When you navigate to Dashboards, you should see multiple Auditbeat dashboards;<\/p>\n\n\n\n Take for example, File integrity dashboards;<\/p>\n\n\n\n In our previous tutorial, we learnt how to Find out who Edited Files in Linux<\/a> with auditd.<\/p>\n\n\n\n Auditbeat auditd module can be configured the same way.<\/p>\n\n\n\n Taking an example from the above guide, where SSH server configuration file is being monitored, using the Auditd rule;<\/p>\n\n\n\n then you can configure Auditbeat auditd module by inserting the above rule;<\/p>\n\n\n\n The Restart Auditd;<\/p>\n\n\n\n Next,navigate to Kibana Auditd dashboards and select Auditd overview dashboard. With some filters applied, you can see who edited the SSH server configuration file.<\/p>\n\n\n\n Getting Started with Auditbeat<\/a><\/p>\n\n\n\n Detecting Malicious Files with Wazuh and VirusTotal<\/a><\/p>\n\n\n\nDetecting Changes to Critical Files in Linux using Auditbeat and ELK<\/h2>\n\n\n\n
Install and Setup ELK Stack<\/h3>\n\n\n\n
Install Auditbeat on the Host<\/h3>\n\n\n\n
Installing Auditbeat on Ubuntu\/Debian based sytems<\/h4>\n\n\n\n
apt install gnupg2 apt-transport-https -y<\/code><\/pre>\n\n\n\n
wget -qO - https:\/\/artifacts.elastic.co\/GPG-KEY-elasticsearch | gpg --dearmor > \/etc\/apt\/trusted.gpg.d\/elastic.gpg<\/code><\/pre>\n\n\n\n
echo \"deb https:\/\/artifacts.elastic.co\/packages\/7.x\/apt stable main\" > \/etc\/apt\/sources.list.d\/elastic-7.x.list<\/code><\/pre>\n\n\n\n
apt update<\/code><\/pre>\n\n\n\n
apt install auditbeat -y<\/code><\/pre>\n\n\n\n
Installing Auditbeat on RHEL based sytems<\/h5>\n\n\n\n
rpm --import https:\/\/packages.elastic.co\/GPG-KEY-elasticsearch<\/code><\/pre>\n\n\n\n
\ncat > \/etc\/yum.repos.d\/elastic.repo << EOL\n[elastic-7.x]\nname=Elastic repository for 7.x packages\nbaseurl=https:\/\/artifacts.elastic.co\/packages\/7.x\/yum\ngpgcheck=1\ngpgkey=https:\/\/artifacts.elastic.co\/GPG-KEY-elasticsearch\nenabled=0\nautorefresh=1\ntype=rpm-md\nEOL\n<\/code><\/pre>\n\n\n\n
yum --enablerepo=elastic-7.x install auditbeat -y<\/code><\/pre>\n\n\n\n
Configure Auditbeat Elasticsearch connection<\/h3>\n\n\n\n
...\n...\n# ---------------------------- Elasticsearch Output ----------------------------\noutput.elasticsearch:\n # Array of hosts to connect to.\n hosts: [\"localhost:9200\"]\n\n # Protocol - either `http` (default) or `https`.\n #protocol: \"https\"\n\n # Authentication credentials - either API key or username\/password.\n #api_key: \"id:api_key\"\n #username: \"elastic\"\n #password: \"changeme\"\n...\n<\/code><\/pre>\n\n\n\n
localhost:9200<\/code><\/strong> with my Elasticsearch connection details;
192.168.58.22:9200<\/code><\/strong>.<\/p>\n\n\n\n
...\n...\n# ---------------------------- Elasticsearch Output ----------------------------\noutput.elasticsearch:\n # Array of hosts to connect to.\n hosts: [\"192.168.58.22:9200\"]<\/strong>\n\n # Protocol - either `http` (default) or `https`.\n #protocol: \"https\"\n\n # Authentication credentials - either API key or username\/password.\n #api_key: \"id:api_key\"\n #username: \"elastic\"\n #password: \"changeme\"\n...\n<\/code><\/pre>\n\n\n\n
nc -nvz 192.168.58.22 9200<\/code><\/pre>\n\n\n\n
auditbeat test output<\/code><\/pre>\n\n\n\n
elasticsearch: http:\/\/192.168.58.22:9200...\n parse url... OK\n connection...\n parse host... OK\n dns lookup... OK\n addresses: 192.168.58.22\n dial up... OK\n TLS... WARN secure connection disabled\n talk to server... OK\n version: 7.16.0\n\n<\/code><\/pre>\n\n\n\n
Configure Auditbeat to Collect Audit Data<\/h3>\n\n\n\n
\/etc\/auditbeat\/auditbeat.yml<\/strong><\/code>, the default configurations for Auditbeat is as shown below;<\/p>\n\n\n\n
# =========================== Modules configuration ============================\nauditbeat.modules:\n\n- module: auditd\n # Load audit rules from separate files. Same format as audit.rules(7).\n audit_rule_files: [ '${path.config}\/audit.rules.d\/*.conf' ]\n audit_rules: |\n ## Define audit rules here.\n ## Create file watches (-w) or syscall audits (-a or -A). Uncomment these\n ## examples or add your own rules.\n\n ## If you are on a 64 bit platform, everything should be running\n ## in 64 bit mode. This rule will detect any use of the 32 bit syscalls\n ## because this might be a sign of someone exploiting a hole in the 32\n ## bit API.\n #-a always,exit -F arch=b32 -S all -F key=32bit-abi\n\n ## Executions.\n #-a always,exit -F arch=b64 -S execve,execveat -k exec\n\n ## External access (warning: these can be expensive to audit).\n #-a always,exit -F arch=b64 -S accept,bind,connect -F key=external-access\n\n ## Identity changes.\n #-w \/etc\/group -p wa -k identity\n #-w \/etc\/passwd -p wa -k identity\n #-w \/etc\/gshadow -p wa -k identity\n\n ## Unauthorized access attempts.\n #-a always,exit -F arch=b64 -S open,creat,truncate,ftruncate,openat,open_by_handle_at -F exit=-EACCES -k access\n #-a always,exit -F arch=b64 -S open,creat,truncate,ftruncate,openat,open_by_handle_at -F exit=-EPERM -k access\n\n- module: file_integrity\n paths:\n - \/bin\n - \/usr\/bin\n - \/sbin\n - \/usr\/sbin\n - \/etc\n\n- module: system\n datasets:\n - package # Installed, updated, and removed packages\n\n period: 2m # The frequency at which the datasets check for changes\n\n- module: system\n datasets:\n - host # General host information, e.g. uptime, IPs\n - login # User logins, logouts, and system boots.\n - process # Started and stopped processes\n - socket # Opened and closed sockets\n - user # User information\n\n # How often datasets send state updates with the\n # current state of the system (e.g. all currently\n # running processes, all open sockets).\n state.period: 12h\n\n # Enabled by default. Auditbeat will read password fields in\n # \/etc\/passwd and \/etc\/shadow and store a hash locally to\n # detect any changes.\n user.detect_password_changes: true\n\n # File patterns of the login record files.\n login.wtmp_file_pattern: \/var\/log\/wtmp*\n login.btmp_file_pattern: \/var\/log\/btmp*\n<\/code><\/pre>\n\n\n\n
\n
- module: auditd<\/code>. The module receives audit events from the Linux Audit Framework that is a part of the Linux kernel<\/em>.<\/li>\n\n\n\n
- module: file_integrity<\/code><\/strong> <\/em>in the Auditbeat config file. This module sends events when a file is changed (created, updated, or deleted) on disk. The events contain file metadata and hashes<\/em>. By default, it monitors the following directories;\n
\n
- module: system<\/code><\/strong> in the config file. This module collects various security related information about a system<\/em>.<\/li>\n<\/ul>\n\n\n\n
Validate Auditbeat Configuration<\/h4>\n\n\n\n
auditbeat test config<\/code><\/pre>\n\n\n\n
Config OK<\/code><\/strong>, then all is fine.<\/p>\n\n\n\n
Load Auditbeat Index Template and Visualization Dashboards to Elasticsearch<\/h3>\n\n\n\n
auditbeat setup --index-management -E output.logstash.enabled=false -E 'output.elasticsearch.hosts=[\"192.168.58.22:9200\"]' -E setup.kibana.host=192.168.58.22:5601<\/code><\/pre>\n\n\n\n
Index setup finished.<\/code><\/pre>\n\n\n\n
auditbeat setup -e -E output.logstash.enabled=false -E output.elasticsearch.hosts=['192.168.58.22:9200'] -E setup.kibana.host=192.168.58.22:5601<\/code><\/pre>\n\n\n\n
Configure Auditbeat Logging<\/h3>\n\n\n\n
\ncat >> \/etc\/auditbeat\/auditbeat.yml << EOL\nlogging.level: info\nlogging.to_files: true\nlogging.files:\n path: \/var\/log\/auditbeat\n name: auditbeat\n keepfiles: 7\n permissions: 0644\nEOL\n<\/code><\/pre>\n\n\n\n
failed to hash executable \/usr\/share\/auditbeat\/bin\/auditbeat for PID 5888: failed to hash file \/usr\/share\/auditbeat\/bin\/auditbeat: hasher: file size 111924496 exceeds max file size<\/strong><\/code><\/pre>\n\n\n\n
sed -i '\/ state.period:\/a\\ process.hash.scan_rate_per_sec: 50 MiB\\n process.hash.max_file_size: 250 MiB\\n process.hash.hash_types: [sha1]' \/etc\/auditbeat\/auditbeat.yml<\/code><\/pre>\n\n\n\n
Running Auditbeat<\/h3>\n\n\n\n
systemctl enable --now auditbeat<\/code><\/pre>\n\n\n\n
systemctl status auditbeat<\/code><\/pre>\n\n\n\n
\u25cf auditbeat.service - Audit the activities of users and processes on your system.\n Loaded: loaded (\/lib\/systemd\/system\/auditbeat.service; enabled; vendor preset: enabled)\n Active: active (running) since Thu 2021-12-09 20:46:09 EAT; 3s ago\n Docs: https:\/\/www.elastic.co\/beats\/auditbeat\n Main PID: 2621 (auditbeat)\n Tasks: 5 (limit: 1133)\n Memory: 41.8M\n CPU: 109ms\n CGroup: \/system.slice\/auditbeat.service\n \u2514\u25002621 \/usr\/share\/auditbeat\/bin\/auditbeat --environment systemd -c \/etc\/auditbeat\/auditbeat.yml --path.home \/usr\/share\/auditbeat --path.config \/etc\/auditbeat >\n\nDec 09 20:46:09 debian11 systemd[1]: Started Audit the activities of users and processes on your system..\n<\/code><\/pre>\n\n\n\n
Verify Audit Data Reception on Kibana<\/h3>\n\n\n\n
auditbeat-<\/code><\/strong> index created.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
Visualize Auditbeat Data in Kibana<\/h3>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
auditctl -w \/etc\/ssh\/sshd_config -p wax -k monitor_sshd_conf<\/strong><\/code><\/pre>\n\n\n\n
sed -i '\/audit_rules:\/a\\ -w \/etc\/ssh\/sshd_config -p wax -k monitor_sshd_conf' \/etc\/auditbeat\/auditbeat.yml<\/code><\/pre>\n\n\n\n
\/etc\/auditbeat\/auditbeat.yml<\/code> Auditd module config now look like;<\/p>\n\n\n\n
auditbeat.modules:\n\n- module: auditd\n # Load audit rules from separate files. Same format as audit.rules(7).\n audit_rule_files: [ '${path.config}\/audit.rules.d\/*.conf' ]\n audit_rules: |\n -w \/etc\/ssh\/sshd_config -p wax -k monitor_sshd_conf<\/strong><\/code><\/pre>\n\n\n\n
systemctl restart auditd<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
Further Reading<\/h4>\n\n\n\n
Other Tutorials<\/h4>\n\n\n\n