Linux Security Monitoring
Linux security monitoring focuses on collecting and analyzing logs, process activity, file changes, authentication events, and command execution to detect compromise.
Linux servers are common targets because they host web applications, databases, cloud workloads, and infrastructure services.
Linux Logging Basics
Linux logs are usually plain text files.
Common location:
Typical log format:
Unlike Windows logs, Linux logs usually do not have universal event IDs.
Key Linux Log Files
| File | Purpose |
|---|---|
/var/log/syslog |
General system log on Debian/Ubuntu |
/var/log/messages |
General system log on RHEL/CentOS |
/var/log/auth.log |
Authentication logs on Debian/Ubuntu |
/var/log/secure |
Authentication logs on RHEL/CentOS |
/var/log/kern.log |
Kernel logs |
/var/log/dpkg.log |
Debian package install/removal logs |
/var/log/yum.log |
RHEL/CentOS package logs |
/var/log/cron |
Scheduled task logs |
/var/log/nginx/access.log |
Nginx web access |
/var/log/apache2/access.log |
Apache web access |
Working with Logs
Read a log:
First lines:
Last lines:
Follow live:
Filtering with Grep
Filter for a keyword:
Exclude a keyword:
Recursive search:
Search compressed rotated logs:
Log Rotation
Logs may rotate into older files.
Examples:
Use tools like:
Authentication Logs
Authentication logs are critical for detecting:
- SSH brute force
- Successful compromise
- Sudo usage
- User creation
- Group modification
- Password changes
- Privilege escalation attempts
Debian/Ubuntu:
RHEL/CentOS:
SSH Monitoring
Successful SSH logins:
Failed SSH logins:
Invalid users:
Brute-force pattern:
Session Events
Find opened and closed sessions:
Common sources:
| Source | Meaning |
|---|---|
sshd |
Remote SSH login |
login |
Local login |
sudo |
Elevated command |
su |
Switch user |
cron |
Scheduled task |
User and Group Changes
Find account changes:
Red flags:
- Unexpected user creation
- User added to
sudo - Root password changed
- Disabled account re-enabled
- Service-looking user created after compromise
Sudo Command Auditing
Sudo logs the command executed.
Search:
Suspicious examples:
COMMAND=/usr/bin/systemctl stop ufw
COMMAND=/usr/bin/rm -rf /var/log/auth.log
COMMAND=/usr/bin/chmod +s /bin/bash
Web Server Logs
Web logs help detect:
- SQL injection
- Path traversal
- Web shells
- Directory brute forcing
- Authentication attacks
- DoS/DDoS
- Suspicious uploads
Nginx access log example:
Fields:
| Field | Meaning |
|---|---|
| Source IP | Requester |
| Timestamp | Time of request |
| Method/path | Requested resource |
| Status code | Result |
| Size | Bytes returned |
Bash History
Bash history records interactive commands.
View saved history:
View current shell history:
Limitations:
- Commands with leading spaces may not be logged.
- Commands inside scripts are not individually logged.
- Attackers can clear history.
- Other shells may not log the same way.
- History writes on session close, not always instantly.
Warning
Bash history is useful but not reliable as a complete forensic record.
Auditd
Auditd is the Linux audit daemon.
It monitors kernel-level activity through syscalls.
Useful for:
- Process execution
- File access
- File modification
- Privileged actions
- Sensitive file monitoring
- Runtime detection
- File integrity monitoring
Audit logs:
Auditd Rules
Rules define what to watch.
Common monitoring targets:
/etc/passwd/etc/shadow/etc/sudoers/etc/ssh/sshd_config/home/*/.ssh/authorized_keys/etc/crontab/etc/cron.d//etc/systemd/system//var/www/- Execution of
wget,curl,nc,bash,python
Searching Audit Logs
Use ausearch.
Search by key:
Search by executable:
Search by file:
Search by PID:
Search by parent PID:
Important Audit Fields
| Field | Meaning |
|---|---|
pid |
Process ID |
ppid |
Parent process ID |
auid |
Original login user |
uid |
Effective user |
exe |
Executable path |
proctitle |
Full command |
key |
Rule tag |
syscall |
Kernel action |
auid is very important because it can preserve the original user even after sudo.
Linux Discovery Detection
After gaining access, attackers usually run discovery commands.
Common commands:
| Category | Commands |
|---|---|
| Identity | whoami, id, w, last |
| System | uname -a, hostname, lsb_release -a, env |
| Users | cat /etc/passwd, cat /etc/sudoers |
| Processes | ps aux, top |
| Network | ip a, netstat -tnlp, ss -tnlp |
| Virtualization | systemd-detect-virt, lsmod |
High-confidence signal:
Web server users rarely need to spawn shells and run discovery commands.
Process Tree Investigation
When investigating suspicious command execution:
- Find the suspicious child process.
- Get the parent process ID.
- Investigate the parent.
- Investigate sibling processes from the same parent.
Example:
Suspicious patterns:
Privilege Escalation Monitoring
Attackers often land as low-privilege users and attempt to become root.
Common phases:
| Phase | Indicators |
|---|---|
| Discovery | whoami, id, uname -r |
| Staging | wget, curl, files in /tmp |
| Compilation | gcc exploit.c |
| Execution | Running exploit binary |
| Impact | UID changes to root |
Reliable indicator:
Privilege Escalation Investigation
Find exploit execution:
Trace parent:
Trace child:
Look for root shell spawned by suspicious process.
Linux Persistence
Common Linux persistence mechanisms:
| Technique | Location |
|---|---|
| Cron job | /etc/crontab, /etc/cron.d/, /var/spool/cron/ |
| Systemd service | /etc/systemd/system/, /lib/systemd/system/ |
| SSH key | ~/.ssh/authorized_keys |
| New user | /etc/passwd, /etc/shadow |
| Web shell | /var/www/, app upload directories |
| Shell profile | .bashrc, .profile, /etc/profile |
Cron Persistence
Attackers may add jobs like:
Hunt:
Systemd Persistence
Attackers may create fake services.
Suspicious paths:
Hunt:
Watch for:
- Odd service names
- Services running from
/tmp - Services running curl/bash pipelines
- Newly enabled services
- Masquerading as cloud or update services
Account Persistence
Attackers may create users or add SSH keys.
Find user creation:
Check suspicious users:
Find sudo-capable users:
SSH Key Backdoors
Attackers may append their public key to:
Auditd search:
Manual hunt:
find /home -name authorized_keys -type f -exec ls -la {} \;
find /home -name authorized_keys -type f -exec cat {} \;
Web Shell Persistence
Web shells provide application-level persistence.
Hunt:
find /var/www -type f \( -name "*.php" -o -name "*.jsp" -o -name "*.aspx" \)
grep -R "system(" /var/www
grep -R "shell_exec" /var/www
grep -R "base64_decode" /var/www
Look for:
- New PHP files in upload folders
- Random names
- Double extensions
- Dangerous functions
- Recent modifications
Auditd Alternatives
| Tool | Best For |
|---|---|
| Sysmon for Linux | Teams using Sysmon-style telemetry |
| Falco | Containers and Kubernetes runtime detection |
| Osquery | Querying system state like SQL |
| EDR | Enterprise endpoint detection and response |
Quick Reference
| Goal | Command |
|---|---|
| Successful SSH | grep "Accepted" /var/log/auth.log |
| Failed SSH | grep "Failed password" /var/log/auth.log |
| Invalid users | grep "Invalid user" /var/log/auth.log |
| Sudo commands | grep "COMMAND=" /var/log/auth.log |
| User changes | grep -E "useradd|usermod|userdel" /var/log/auth.log |
| Search all logs | grep -R -E "keyword1|keyword2" /var/log |
| Audit by executable | ausearch -i -x <binary> |
| Audit by file | ausearch -i -f <path> |
| Audit by key | ausearch -i -k <key> |
| Trace parent | ausearch -i --pid <PPID> |
| Trace siblings | ausearch -i --ppid <PPID> |
Notes to Remember
- Linux logs are mostly plain text.
/var/log/auth.logor/var/log/secureis critical for authentication.- Bash history is helpful but easy to evade.
- Auditd gives syscall-level visibility.
- Process tree context is essential.
www-datarunningwhoami,id,bash, orcurlis highly suspicious.- Persistence often uses cron, systemd, SSH keys, new users, or web shells.