Introduction
Linux has become the operating system of choice for cybersecurity professionals, serving as both a powerful platform for security operations and a critical component of modern infrastructure that requires protection. The command line interface provides unparalleled control and visibility into system operations, making it an indispensable tool for security analysis, incident response, penetration testing, and system hardening. This technical deep-dive explores the essential Linux commands that form the foundation of professional cybersecurity work, examining not just the basic syntax but the advanced usage patterns, security implications, and practical applications that distinguish expert practitioners from novices.
Understanding the Linux Security Command Ecosystem
The Security Professional’s Command Line Architecture
The Linux command line ecosystem for security professionals can be conceptualized as a layered architecture:
┌─────────────────────────────────────────────────────────────┐
│ Security Analysis Layer │
│ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
│ │ Network │ │ Process │ │ File System │ │
│ │ Analysis │ │ Analysis │ │ Analysis │ │
│ └───────────────┘ └───────────────┘ └───────────────┘ │
└───────────────────────────┬─────────────────────────────────┘
│
┌───────────────────────────┴─────────────────────────────────┐
│ Core System Utilities │
│ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
│ │ Text │ │ System │ │ User │ │
│ │ Processing │ │ Information │ │ Management │ │
│ └───────────────┘ └───────────────┘ └───────────────┘ │
└───────────────────────────┬─────────────────────────────────┘
│
┌───────────────────────────┴─────────────────────────────────┐
│ Kernel Interface │
│ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
│ │ /proc │ │ /sys │ │ System Calls │ │
│ │ Filesystem │ │ Filesystem │ │ │ │
│ └───────────────┘ └───────────────┘ └───────────────┘ │
└─────────────────────────────────────────────────────────────┘
Understanding this architecture helps security professionals select the appropriate tools for specific tasks and understand how different commands interact with system components at various levels.
Network Analysis and Security Commands
Advanced Network Connection Analysis with ss and netstat
The ss (socket statistics) command has largely replaced netstat in modern Linux distributions, offering faster performance and more detailed information about network connections. Security professionals use these tools to identify suspicious connections, verify service configurations, and detect potential compromises.
bash# Display all TCP connections with process information
ss -tnp
# Show listening ports with detailed socket information
ss -tlnp
# Display established connections with timer information
ss -o state established
# Show all UDP sockets with extended information
ss -uanpe
# Filter connections by specific criteria
ss -tn state established dst 192.168.1.0/24
# Display socket memory usage (useful for DoS detection)
ss -tm
# Show connections with specific destination port
ss -tn sport = :22
# Real-time monitoring of connection states
watch -n 1 'ss -s'
For deeper analysis, combining ss with other tools provides comprehensive network visibility:
bash# Identify processes making external connections
ss -tnp | grep -v "127.0.0.1" | awk '{print $6}' | sort | uniq -c | sort -rn
# Monitor connections to specific ports over time
while true; do
echo "=== $(date) ==="
ss -tn state established '( dport = :443 or dport = :80 )'
sleep 5
done
# Detect potential port scanning activity
ss -tn state syn-recv | awk '{print $4}' | cut -d':' -f1 | sort | uniq -c | sort -rn
# Advanced connection analysis with process context
ss -tnp | while read proto recv send local remote state pid; do
if [[ $pid =~ pid=([0-9]+) ]]; then
process_id=${BASH_REMATCH[1]}
cmdline=$(cat /proc/$process_id/cmdline 2>/dev/null | tr '\0' ' ')
echo "$local -> $remote [$state] PID:$process_id CMD:$cmdline"
fi
done
Network Traffic Analysis with tcpdump
The tcpdump command provides packet-level network analysis capabilities essential for security investigations:
bash# Capture traffic on specific interface with detailed packet info
tcpdump -i eth0 -nn -vvv -X
# Capture and save suspicious traffic for analysis
tcpdump -i any -w suspicious_traffic.pcap 'port 4444 or port 1337'
# Monitor DNS queries (often used in data exfiltration)
tcpdump -i any -nn port 53
# Detect potential ARP spoofing
tcpdump -i eth0 -nn arp
# Capture HTTP traffic with ASCII output
tcpdump -i any -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
# Advanced BPF filter for detecting SYN flood attacks
tcpdump -i any -nn 'tcp[tcpflags] & (tcp-syn) != 0 and tcp[tcpflags] & (tcp-ack) = 0'
# Capture traffic from specific IP excluding SSH
tcpdump -i any -nn src 192.168.1.100 and not port 22
# Real-time traffic analysis with rotation
tcpdump -i eth0 -w /var/log/tcpdump/capture-%Y%m%d%H%M%S.pcap -G 3600 -z gzip
For more sophisticated analysis, tcpdump can be combined with other tools:
bash# Extract unique IPs from packet capture
tcpdump -nn -r capture.pcap | awk '{print $3}' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | sort -u
# Analyze packet sizes for anomaly detection
tcpdump -nn -r capture.pcap -l | awk '{print $NF}' | grep -oE '[0-9]+' | sort | uniq -c | sort -rn
# Extract HTTP headers from traffic
tcpdump -A -s 0 'tcp port 80' | grep -E '^(GET|POST|Host:|User-Agent:|Cookie:)'
iptables for Traffic Analysis and Control
While primarily a firewall tool, iptables provides powerful traffic analysis capabilities:
bash# Log all incoming connections for analysis
iptables -I INPUT -j LOG --log-prefix "INPUT: " --log-level 4
# Track connection states for security monitoring
iptables -A INPUT -m conntrack --ctstate NEW -j LOG --log-prefix "NEW_CONN: "
# Monitor specific port access attempts
iptables -A INPUT -p tcp --dport 22 -j LOG --log-prefix "SSH_ATTEMPT: "
# Create honeypot rules to detect scanning
iptables -A INPUT -p tcp --dport 23 -j LOG --log-prefix "TELNET_HONEYPOT: "
iptables -A INPUT -p tcp --dport 23 -j DROP
# Rate limiting for DoS protection analysis
iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
# Track outbound connections to detect data exfiltration
iptables -A OUTPUT -m state --state NEW -j LOG --log-prefix "OUTBOUND_NEW: "
# Advanced packet marking for traffic analysis
iptables -t mangle -A PREROUTING -s 192.168.1.0/24 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -m mark --mark 1 -j LOG --log-prefix "MARKED_TRAFFIC: "
Process Analysis and System Monitoring
Advanced Process Investigation Techniques
Process analysis forms a critical component of security investigations, helping identify malicious activities and compromised systems:
bash# Comprehensive process listing with security-relevant information
ps auxwwf
# Show process tree with full command lines
ps -ejH
# Display processes with security contexts (SELinux)
ps -eZ
# Monitor real-time process creation
while true; do ps aux | tail -n +2 | awk '{print $2}' > /tmp/ps.new; diff /tmp/ps.old /tmp/ps.new 2>/dev/null | grep "^>" | awk '{print $2}' | while read pid; do ps -p $pid -o pid,ppid,user,cmd 2>/dev/null; done; mv /tmp/ps.new /tmp/ps.old; sleep 1; done
# Find processes hiding from ps (rootkit detection)
for pid in $(ls /proc | grep -E '^[0-9]+$'); do
if ! ps -p $pid > /dev/null 2>&1; then
echo "Hidden process found: PID $pid"
ls -la /proc/$pid/ 2>/dev/null
fi
done
# Analyze process memory maps for injection detection
for pid in $(ps aux | awk '{print $2}' | grep -E '^[0-9]+$'); do
if [ -r /proc/$pid/maps ]; then
echo "=== PID: $pid ==="
grep -E 'rwxp|r-xp.*\[' /proc/$pid/maps 2>/dev/null
fi
done
# Track process file descriptors for connection analysis
lsof -p $(pgrep -d, suspicious_process)
# Monitor process system calls in real-time
strace -p $(pgrep suspicious_process) -f -e trace=network,file
System Performance and Resource Monitoring
Understanding system performance is crucial for detecting resource-based attacks and system compromise:
bash# Real-time system performance monitoring
vmstat 1
# Detailed CPU usage by process
top -b -n 1 | head -20
# Memory usage analysis
free -m && echo "===" && ps aux --sort=-%mem | head -10
# Disk I/O monitoring for potential data exfiltration
iotop -b -n 1
# Network I/O by process
nethogs -t
# System call statistics
perf stat -a sleep 10
# Advanced performance analysis
sar -u -r -d -n DEV 1 10
# Check for CPU crypto mining indicators
top -b -n 1 | awk '$9 > 80.0 {print $0}' | grep -v "COMMAND"
File Descriptor and Open File Analysis
The lsof command provides comprehensive visibility into open files and network connections:
bash# List all open files by a specific user
lsof -u username
# Find processes using a specific file
lsof /path/to/file
# Display all network connections
lsof -i
# Show processes listening on specific ports
lsof -i :80,443
# Find deleted files still in use (potential malware indicator)
lsof | grep deleted
# Monitor file access in real-time
watch -n 1 'lsof +D /sensitive/directory'
# Identify processes with many open files (potential DoS)
lsof | awk '{print $2}' | sort | uniq -c | sort -rn | head -20
# Track network connections by process
lsof -i -n -P | grep ESTABLISHED
# Find processes using raw sockets (potential sniffer)
lsof | grep -E 'raw|packet'
File System Security and Analysis
Advanced File System Investigation
File system analysis is fundamental to security investigations, helping identify compromises, backdoors, and data breaches:
bash# Find recently modified files (potential indicators of compromise)
find / -type f -mtime -1 -ls 2>/dev/null | grep -v "/proc\|/sys"
# Locate SUID/SGID files (privilege escalation vectors)
find / -type f \( -perm -4000 -o -perm -2000 \) -ls 2>/dev/null
# Search for files with unusual permissions
find / -type f -perm -o+w -ls 2>/dev/null | grep -v "/proc\|/sys\|/dev"
# Detect files with multiple hard links (potential rootkit behavior)
find / -type f -links +1 -ls 2>/dev/null | grep -v "/proc\|/sys"
# Find hidden files and directories
find / -name ".*" -type f -ls 2>/dev/null | grep -v "/proc\|/sys"
# Locate files with spaces at the end (evasion technique)
find / -name "* " -o -name "*.." -ls 2>/dev/null
# Search for specific file content (credential hunting)
grep -r "password\|passwd\|pwd" /etc 2>/dev/null
# File integrity checking
find /bin /sbin /usr/bin /usr/sbin -type f -exec sha256sum {} \; > system_binaries.sha256
# Advanced file attribute analysis
find / -type f -exec lsattr {} \; 2>/dev/null | grep -E "^----i|^----a"
File System Timeline Analysis
Creating timelines helps reconstruct security incidents:
bash# Generate comprehensive file system timeline
find / -type f -printf "%T@ %Tc %p\n" 2>/dev/null | sort -n > filesystem_timeline.txt
# Create MAC time timeline
find / -printf "%A@ %Ay-%Am-%Ad %AH:%AM:%AS %p (atime)\n" -o \
-printf "%T@ %Ty-%Tm-%Td %TH:%TM:%TS %p (mtime)\n" -o \
-printf "%C@ %Cy-%Cm-%Cd %CH:%CM:%CS %p (ctime)\n" 2>/dev/null | \
sort -n > mac_timeline.txt
# Analyze file changes within specific timeframe
find / -type f -newermt "2024-01-01" ! -newermt "2024-01-02" -ls 2>/dev/null
# Track file system changes in real-time
inotifywait -m -r /etc --format '%w%f %e %T' --timefmt '%Y-%m-%d %H:%M:%S'
Permission and Ownership Analysis
Understanding file permissions is crucial for security hardening:
bash# Find world-writable directories
find / -type d -perm -0002 -ls 2>/dev/null | grep -v "/proc\|/sys"
# Locate files without proper ownership
find / -nouser -o -nogroup -ls 2>/dev/null
# Check for unusual executable permissions
find /home -type f -executable -ls 2>/dev/null
# Audit sudo permissions
cat /etc/sudoers /etc/sudoers.d/* 2>/dev/null | grep -v "^#\|^$"
# Analyze capability-enhanced binaries
getcap -r / 2>/dev/null
# Review ACL permissions
getfacl -R /sensitive/directory 2>/dev/null | grep -v "^#"
Log Analysis and Security Monitoring
System Log Analysis Commands
Effective log analysis is essential for detecting security incidents:
bash# Monitor authentication attempts
journalctl -u ssh.service -f
# Analyze failed login attempts
grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -rn
# Track sudo usage
grep sudo /var/log/auth.log | grep -v "session opened\|session closed"
# Monitor system messages in real-time
tail -f /var/log/syslog | grep -E "error|fail|denied|invalid"
# Analyze kernel messages for security events
dmesg | grep -E "denied|blocked|detected"
# Extract IP addresses from logs
grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" /var/log/auth.log | sort | uniq -c | sort -rn
# Timeline analysis of security events
grep -h "Failed password\|Accepted password\|Invalid user" /var/log/auth.log* | sort -k1,3
# Detect potential brute force attacks
awk '/Failed password/ {print $11}' /var/log/auth.log | sort | uniq -c | awk '$1 > 10 {print}'
Advanced Log Correlation and Analysis
Complex security investigations often require correlating events across multiple log sources:
bash# Correlate authentication with network connections
for ip in $(ss -tn state established | awk '{print $4}' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | sort -u); do
echo "=== Connections from $ip ==="
ss -tn | grep $ip
echo "=== Auth logs for $ip ==="
grep $ip /var/log/auth.log | tail -5
echo
done
# Create security event summary
{
echo "=== Failed Login Attempts ==="
grep "Failed password" /var/log/auth.log | wc -l
echo -e "\n=== Successful Logins ==="
grep "Accepted password" /var/log/auth.log | wc -l
echo -e "\n=== New User Sessions ==="
grep "session opened" /var/log/auth.log | wc -l
echo -e "\n=== Sudo Commands ==="
grep "COMMAND=" /var/log/auth.log | wc -l
} > security_summary.txt
# Monitor for specific security patterns
tail -f /var/log/syslog /var/log/auth.log | grep --line-buffered -E \
"Failed password|Invalid user|POSSIBLE BREAK-IN|authentication failure|Connection closed|Did not receive identification"
User and Access Management Security Commands
User Account Analysis and Auditing
User account security is fundamental to system protection:
bash# List all user accounts with details
getent passwd | awk -F: '$3 >= 1000 {print $1, $3, $6, $7}'
# Find users with empty passwords
awk -F: '($2 == "") {print $1}' /etc/shadow
# Identify users with UID 0 (root privileges)
awk -F: '($3 == "0") {print}' /etc/passwd
# Check password aging settings
for user in $(cut -d: -f1 /etc/passwd); do
echo -n "$user: "
chage -l $user 2>/dev/null | grep "Password expires" || echo "No aging info"
done
# Find recently created or modified users
ls -la /home | grep -v "total\|^d"
stat /etc/passwd /etc/shadow /etc/group
# Analyze user login history
last -F | head -20
lastlog | grep -v "Never logged in"
# Check for users without home directories
while IFS=: read -r user _ _ _ _ home _; do
[ ! -d "$home" ] && echo "User $user has no home directory: $home"
done < /etc/passwd
# Review user group memberships
for user in $(cut -d: -f1 /etc/passwd); do
groups $user 2>/dev/null
done | sort -u
SSH Security Configuration Analysis
SSH is a critical attack vector requiring careful security analysis:
bash# Check SSH configuration for security issues
grep -E "^[^#]" /etc/ssh/sshd_config | grep -E \
"PermitRootLogin|PasswordAuthentication|PubkeyAuthentication|PermitEmptyPasswords|X11Forwarding"
# Analyze SSH keys
find / -name "authorized_keys" -o -name "id_rsa*" -o -name "id_dsa*" 2>/dev/null
# Check for weak SSH host keys
ssh-keygen -l -f /etc/ssh/ssh_host_*key.pub
# Monitor SSH connection attempts
journalctl -u sshd -f
# Analyze SSH key fingerprints
for key in /etc/ssh/ssh_host_*_key.pub; do
echo "=== $key ==="
ssh-keygen -l -f "$key"
ssh-keygen -l -E md5 -f "$key"
done
# Check for SSH config issues
sshd -T | grep -E "permitrootlogin|passwordauthentication|pubkeyauthentication"
System Security Auditing Commands
Security Configuration Assessment
Comprehensive security auditing requires systematic configuration analysis:
bash# Check running services
systemctl list-units --type=service --state=running
# Analyze listening services
ss -tlnp | grep LISTEN
# Review firewall rules
iptables -L -n -v
ip6tables -L -n -v
# Check for unnecessary packages
dpkg -l | grep -E "telnet|rsh|rlogin|ftp" # Debian/Ubuntu
rpm -qa | grep -E "telnet|rsh|rlogin|ftp" # RedHat/CentOS
# Kernel parameter security check
sysctl -a 2>/dev/null | grep -E "forwarding|redirects|source_route|accept_ra|secure_redirects"
# SELinux/AppArmor status
sestatus # SELinux
aa-status # AppArmor
# Check for core dumps (information disclosure risk)
find / -name "core.*" -o -name "*.core" 2>/dev/null
# Review cron jobs for all users
for user in $(cut -d: -f1 /etc/passwd); do
echo "=== Crontab for $user ==="
crontab -l -u $user 2>/dev/null
done
# Analyze system startup scripts
systemctl list-unit-files --type=service | grep enabled
Package and Software Integrity Verification
Verifying system integrity helps detect compromises:
bash# Verify package integrity (Debian/Ubuntu)
debsums -c 2>/dev/null | grep -v "OK$"
# Verify package integrity (RedHat/CentOS)
rpm -Va | grep -E "^..5"
# Check for rootkits
chkrootkit
rkhunter --check --skip-keypress
# Generate software inventory
dpkg -l > installed_packages_$(date +%Y%m%d).txt # Debian/Ubuntu
rpm -qa > installed_packages_$(date +%Y%m%d).txt # RedHat/CentOS
# Find unusual libraries
ldd /bin/* /usr/bin/* 2>/dev/null | grep -v "linux-vdso\|ld-linux" | awk '{print $3}' | sort -u
# Check binary signatures
for binary in /bin/* /sbin/* /usr/bin/* /usr/sbin/*; do
file "$binary" | grep -v "ELF\|script"
done
Memory Analysis and Forensics Commands
Live Memory Analysis
Memory analysis can reveal hidden processes and rootkits:
bash# Dump process memory
gcore -o /tmp/process_dump $(pgrep suspicious_process)
# Analyze process memory strings
strings /proc/$(pgrep suspicious_process)/mem | grep -E "password|secret|key"
# Check kernel modules
lsmod | grep -v "Module"
modinfo $(lsmod | awk '{print $1}' | tail -n +2)
# Detect hidden kernel modules
cat /proc/modules > /tmp/modules_proc
lsmod | awk '{print $1}' | sort > /tmp/modules_lsmod
diff /tmp/modules_proc /tmp/modules_lsmod
# Memory usage by process
ps aux --sort=-%mem | head -20
# Shared memory analysis
ipcs -m
for i in $(ipcs -m | awk '/0x/ {print $2}'); do
echo "=== Shared Memory Segment $i ==="
ipcs -m -i $i
done
# Check for memory-resident malware
cat /proc/*/maps | grep -E "deleted|memfd:" | sort -u
Incident Response Command Workflows
Initial Incident Response Triage
When responding to a security incident, a structured approach using Linux commands is essential:
┌─────────────────────────────────────────────────────────────┐
│ Incident Response Workflow │
└─────────────────────────────────────────────────────────────┘
│
┌─────────────────────┼─────────────────────┐
▼ ▼ ▼
┌────────────────┐ ┌────────────────┐ ┌────────────────┐
│ Containment │ │ Investigation │ │ Eradication │
│ Commands │ │ Commands │ │ Commands │
└────────────────┘ └────────────────┘ └────────────────┘
│
┌─────────────────────────────────────────────────────────────┐
│ Recovery & Lessons │
└─────────────────────────────────────────────────────────────┘
Initial triage script example:
bash#!/bin/bash
# incident_triage.sh - Initial incident response triage
INCIDENT_DIR="/tmp/incident_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$INCIDENT_DIR"
echo "Starting incident response triage..."
# System information
{
echo "=== System Information ==="
uname -a
uptime
date
} > "$INCIDENT_DIR/system_info.txt"
# Network connections
{
echo "=== Network Connections ==="
ss -tunap
echo -e "\n=== Listening Services ==="
ss -tlnp
echo -e "\n=== Established Connections ==="
ss -tnp state established
} > "$INCIDENT_DIR/network_connections.txt"
# Running processes
{
echo "=== Process List ==="
ps auxwwf
echo -e "\n=== Process Tree ==="
pstree -p
} > "$INCIDENT_DIR/processes.txt"
# User activity
{
echo "=== Currently Logged In Users ==="
w
echo -e "\n=== Last Logins ==="
last -20
echo -e "\n=== Failed Login Attempts ==="
grep "Failed password" /var/log/auth.log | tail -50
} > "$INCIDENT_DIR/user_activity.txt"
# File system changes
{
echo "=== Recently Modified System Files ==="
find /etc /bin /sbin /usr/bin /usr/sbin -type f -mtime -1 -ls 2>/dev/null
echo -e "\n=== SUID/SGID Files ==="
find / -type f \( -perm -4000 -o -perm -2000 \) -ls 2>/dev/null
} > "$INCIDENT_DIR/filesystem_changes.txt"
# Create archive
tar czf "incident_triage_$(date +%Y%m%d_%H%M%S).tar.gz" -C /tmp "$(basename $INCIDENT_DIR)"
echo "Triage complete. Results saved to $INCIDENT_DIR"
Network Isolation Commands
During an incident, network isolation may be necessary:
bash# Block specific IP address
iptables -I INPUT -s suspicious_ip -j DROP
iptables -I OUTPUT -d suspicious_ip -j DROP
# Block all outbound traffic except DNS and established connections
iptables -P OUTPUT DROP
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
# Disable network interface
ip link set eth0 down
# Create network traffic capture for analysis
tcpdump -i any -s 0 -w /forensics/capture_$(date +%Y%m%d_%H%M%S).pcap &
# Monitor remaining network activity
watch -n 1 'ss -tn state established'
Security Automation and Scripting
Automated Security Monitoring Script
Creating automated security monitoring enhances detection capabilities:
bash#!/bin/bash
# security_monitor.sh - Continuous security monitoring
LOGFILE="/var/log/security_monitor.log"
ALERT_EMAIL="[email protected]"
log_event() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOGFILE"
}
check_failed_logins() {
local threshold=10
local failed_count=$(grep "Failed password" /var/log/auth.log | \
awk -v d="$(date -d '5 minutes ago' '+%b %d %H:%M')" '$0 > d' | wc -l)
if [ "$failed_count" -gt "$threshold" ]; then
log_event "ALERT: $failed_count failed login attempts in last 5 minutes"
echo "Failed login surge detected: $failed_count attempts" | \
mail -s "Security Alert: Failed Logins" "$ALERT_EMAIL"
fi
}
check_new_users() {
local current_users="/tmp/users_current.txt"
local previous_users="/tmp/users_previous.txt"
cut -d: -f1 /etc/passwd | sort > "$current_users"
if [ -f "$previous_users" ]; then
new_users=$(comm -13 "$previous_users" "$current_users")
if [ -n "$new_users" ]; then
log_event "ALERT: New users detected: $new_users"
echo "New users created: $new_users" | \
mail -s "Security Alert: New Users" "$ALERT_EMAIL"
fi
fi
mv "$current_users" "$previous_users"
}
check_listening_ports() {
local current_ports="/tmp/ports_current.txt"
local previous_ports="/tmp/ports_previous.txt"
ss -tlnp 2>/dev/null | awk '{print $4}' | grep -E ":[0-9]+" | sort -u > "$current_ports"
if [ -f "$previous_ports" ]; then
new_ports=$(comm -13 "$previous_ports" "$current_ports")
if [ -n "$new_ports" ]; then
log_event "ALERT: New listening ports detected: $new_ports"
echo "New ports opened: $new_ports" | \
mail -s "Security Alert: New Ports" "$ALERT_EMAIL"
fi
fi
mv "$current_ports" "$previous_ports"
}
check_outbound_connections() {
# Monitor for unusual outbound connections
local suspicious_ports="4444 31337 1337 6667"
for port in $suspicious_ports; do
if ss -tn state established | grep -q ":$port"; then
log_event "ALERT: Suspicious outbound connection on port $port"
ss -tnp | grep ":$port" | \
mail -s "Security Alert: Suspicious Connection" "$ALERT_EMAIL"
fi
done
}
# Main monitoring loop
while true; do
check_failed_logins
check_new_users
check_listening_ports
check_outbound_connections
sleep 300 # Check every 5 minutes
done
System Hardening Automation
Automating security hardening ensures consistent security configurations:
bash#!/bin/bash
# harden_system.sh - Automated system hardening
echo "Starting system hardening..."
# Kernel parameter hardening
cat >> /etc/sysctl.d/99-security.conf << EOF
# IP Spoofing protection
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# Ignore ICMP redirects
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# Ignore send redirects
net.ipv4.conf.all.send_redirects = 0
# Disable source packet routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# Log Martians
net.ipv4.conf.all.log_martians = 1
# Ignore ICMP ping requests
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Disable IPv6 if not needed
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
# Enable TCP/IP SYN cookies
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 5
# Increase system file descriptor limit
fs.file-max = 65535
# Enable ExecShield
kernel.exec-shield = 1
kernel.randomize_va_space = 2
EOF
sysctl -p /etc/sysctl.d/99-security.conf
# Secure shared memory
echo "tmpfs /run/shm tmpfs defaults,noexec,nosuid 0 0" >> /etc/fstab
# Disable unnecessary services
systemctl disable avahi-daemon
systemctl disable cups
systemctl disable bluetooth
# Set secure permissions
chmod 644 /etc/passwd
chmod 644 /etc/group
chmod 600 /etc/shadow
chmod 600 /etc/gshadow
# Configure password policies
sed -i 's/PASS_MAX_DAYS.*/PASS_MAX_DAYS 90/' /etc/login.defs
sed -i 's/PASS_MIN_DAYS.*/PASS_MIN_DAYS 7/' /etc/login.defs
sed -i 's/PASS_WARN_AGE.*/PASS_WARN_AGE 14/' /etc/login.defs
echo "System hardening complete."
Advanced Forensics and Investigation Commands
Timeline Creation and Analysis
Creating comprehensive timelines is crucial for incident reconstruction:
bash# Create master timeline
find / -printf "%T@ %Tc %11s %M %u %g %p\n" 2>/dev/null | sort -n > master_timeline.txt
# Extract specific timeframe
awk -v start="$(date -d '2024-01-01' +%s)" -v end="$(date -d '2024-01-31' +%s)" \
'$1 >= start && $1 <= end' master_timeline.txt
# Correlate file changes with log events
while read timestamp file; do
echo "=== File: $file ==="
log_time=$(date -d "@$timestamp" "+%b %d %H:%M")
grep "$log_time" /var/log/auth.log /var/log/syslog 2>/dev/null
done < <(find /etc -type f -mtime -1 -printf "%T@ %p\n")
# Process execution timeline from logs
journalctl --since "1 day ago" -o json | \
jq -r '. | select(.MESSAGE | contains("Exec")) | "\(.TIMESTAMP) \(.MESSAGE)"' | \
sort
Data Recovery and Analysis
When investigating incidents, data recovery capabilities are essential:
bash# Recover deleted files from filesystem
extundelete /dev/sda1 --restore-all
# Search for strings in unallocated space
dd if=/dev/sda1 | strings -n 10 | grep -i "password\|confidential"
# Create forensic image
dd if=/dev/sda of=/external/forensic_image.dd bs=4M conv=sync,noerror
# Calculate hash for integrity
sha256sum /external/forensic_image.dd > forensic_image.sha256
# Mount image for analysis
mkdir /mnt/forensic
mount -o loop,ro /external/forensic_image.dd /mnt/forensic
# Extract metadata from files
exiftool -r /suspicious/directory
# Analyze browser history
sqlite3 ~/.mozilla/firefox/*.default*/places.sqlite \
"SELECT datetime(visit_date/1000000,'unixepoch'), url FROM moz_places ORDER BY visit_date DESC LIMIT 100;"
Best Practices for Security Command Usage
Command Logging and Audit Trail
Maintaining comprehensive logs of security commands is essential:
bash# Enable comprehensive command logging
echo 'HISTTIMEFORMAT="%F %T "' >> /etc/profile
echo 'HISTSIZE=10000' >> /etc/profile
echo 'HISTFILESIZE=10000' >> /etc/profile
# Log all commands to syslog
echo 'PROMPT_COMMAND="history -a >(logger -t cmd -p local6.info)"' >> /etc/profile
# Create security command wrapper
cat > /usr/local/bin/sec_wrapper << 'EOF'
#!/bin/bash
COMMAND="$@"
LOGFILE="/var/log/security_commands.log"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] User: $(whoami) Command: $COMMAND" >> "$LOGFILE"
exec $COMMAND
EOF
chmod +x /usr/local/bin/sec_wrapper
# Configure sudo logging
echo "Defaults log_output" >> /etc/sudoers
echo "Defaults!/usr/bin/sudoreplay !log_output" >> /etc/sudoers
echo "Defaults!/sbin/reboot !log_output" >> /etc/sudoers
Performance and Resource Considerations
Security commands can be resource-intensive. Optimizing their usage is important:
bash# Limit find command resource usage
nice -n 19 ionice -c 3 find / -type f -mtime -1
# Use timeout to prevent hanging commands
timeout 300 tcpdump -i any -w capture.pcap
# Implement resource limits
ulimit -t 3600 # CPU time limit
ulimit -v 1048576 # Virtual memory limit
# Monitor command resource usage
/usr/bin/time -v command_to_monitor
# Background intensive tasks
nohup find / -type f -exec sha256sum {} \; > checksums.txt 2>&1 &
Conclusion
The Linux command line provides cybersecurity professionals with unparalleled power and flexibility for security analysis, incident response, and system hardening. Mastery of these commands requires not just memorization of syntax, but understanding of their security implications, performance characteristics, and integration into comprehensive security workflows.
The commands and techniques presented in this guide represent the foundation of professional cybersecurity work on Linux systems. From network analysis with tcpdump and ss, through process investigation with ps and lsof, to forensic analysis with find and custom scripts, each tool serves a specific purpose in the security professional’s arsenal.
As threats continue to evolve, the importance of command-line proficiency only increases. The ability to quickly analyze systems, detect anomalies, and respond to incidents using these fundamental tools remains a critical skill that distinguishes expert practitioners. Combined with automation through scripting and integration with modern security tools, these commands form the backbone of effective security operations.
Regular practice, continuous learning, and adaptation of these techniques to specific environments will ensure that security professionals can effectively protect systems against both current and emerging threats. The command line remains not just a tool, but a powerful ally in the ongoing battle for cybersecurity.
Frequently Asked Questions
What are the most critical Linux commands every cybersecurity professional should master first?
For cybersecurity professionals beginning their Linux journey, these commands form the essential foundation:
- Network Analysis Commands:
ssornetstat: Understanding network connections is fundamental to detecting compromisestcpdump: Packet-level analysis reveals attack patterns and data exfiltrationnmap: Network discovery and vulnerability identification (though not native to all distributions)
- Process Analysis Commands:
pswith various flags (aux, ejH, etc.): Process visibility is crucial for malware detectionlsof: Links processes to files and network connectionstop/htop: Real-time resource monitoring for detecting cryptominers and DoS
- File System Commands:
find: The Swiss Army knife of file system investigationgrep: Pattern matching across files and logslswith various flags: Understanding file permissions and attributes
- Log Analysis Commands:
tail,head,less: Basic log navigationjournalctl: Modern systemd log analysisawk,sed: Text processing for log analysis
- User and Permission Commands:
who,w,last: User activity monitoringchmod,chown: Permission managementsudo,su: Privilege escalation understanding
These commands provide the foundation for more advanced security operations and should be practiced regularly in various scenarios.
How can I effectively combine multiple Linux commands for comprehensive security analysis?
Effective command combination leverages pipes, redirection, and command substitution to create powerful analysis workflows:
- Pipeline Techniques: bash
# Combine network and process analysis ss -tnp | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -10This identifies top 10 IP addresses with established connections. - Command Substitution: bash
# Analyze files accessed by suspicious processes lsof -p $(ps aux | grep suspicious | grep -v grep | awk '{print $2}' | tr '\n' ',') - Loops for Comprehensive Analysis: bash
# Check all user crontabs for suspicious entries for user in $(cut -d: -f1 /etc/passwd); do echo "=== $user ===" crontab -l -u $user 2>/dev/null | grep -v "^#" done - Parallel Processing: bash
# Parallel file integrity checking find /bin /sbin -type f | parallel -j 4 sha256sum {} > integrity_check.txt - Complex Analysis Scripts: Create reusable scripts that combine multiple commands for specific security tasks, such as incident response triage or baseline creation.
The key is understanding each command’s output format and how to parse it for the next command in the pipeline.
What are the best practices for using Linux commands without disrupting production systems?
Using security commands on production systems requires careful consideration to avoid service disruption:
- Resource Management:
- Use
niceandioniceto lower command priority - Implement
ulimitrestrictions to prevent resource exhaustion - Use
timeoutto prevent commands from running indefinitely - Monitor system load with
uptimebefore running intensive commands
- Use
- Read-Only Operations:
- Always use read-only mount options when analyzing file systems
- Prefer non-invasive commands that don’t modify system state
- Use
--dry-runoptions when available - Test commands on development systems first
- Timing Considerations:
- Schedule intensive operations during maintenance windows
- Use incremental approaches for large-scale analysis
- Implement rate limiting in scripts
- Consider impact on backup and replication systems
- Output Management:
- Redirect output to avoid filling up system partitions
- Use dedicated forensics storage for large captures
- Implement log rotation for continuous monitoring scripts
- Compress output files to save space
- Safety Measures:
- Always have rollback plans
- Document all commands executed
- Use screen or tmux for long-running operations
- Implement confirmation prompts for destructive operations
How do Linux security commands differ across distributions, and how can I write portable scripts?
While core commands are similar across distributions, there are important differences to consider:
- Package Management Differences:
- Debian/Ubuntu:
dpkg,apt - RedHat/CentOS:
rpm,yum/dnf - Create wrapper functions that detect the distribution
- Debian/Ubuntu:
- Service Management:
- SystemD:
systemctl,journalctl - SysV Init:
service, traditional/var/logfiles - Check for command availability before use
- SystemD:
- File Locations:
- Log files may be in different locations
- Configuration files might have different paths
- Use variables for paths and detect locations dynamically
- Command Options:
- Some commands have different flags across distributions
- GNU vs BSD versions of commands differ
- Test for specific options before using them
- Portable Scripting Techniques: bash
# Distribution detection if [ -f /etc/debian_version ]; then DISTRO="debian" elif [ -f /etc/redhat-release ]; then DISTRO="redhat" fi # Command availability checking command -v systemctl >/dev/null 2>&1 && USE_SYSTEMD=true # Portable function example get_installed_packages() { if command -v dpkg >/dev/null; then dpkg -l elif command -v rpm >/dev/null; then rpm -qa fi }
What are the essential Linux commands for detecting and analyzing cryptocurrency mining malware?
Cryptocurrency mining malware has become a prevalent threat, requiring specific detection techniques:
- CPU Usage Analysis: bash
# Identify high CPU processes top -b -n 1 | awk '$9 > 80.0 {print $0}' # Monitor CPU usage over time sar -u 1 60 | grep -v Average | awk '$8 < 20 {print "High CPU: " $0}' - Process Name Analysis: bash
# Look for common miner process names ps aux | grep -E "xmrig|minerd|ccminer|xmr-stak|minergate|nanopool" # Find processes trying to hide ps aux | grep -E "^\[\]|^ *\[|^$" - Network Connection Analysis: bash
# Check for mining pool connections ss -tnp | grep -E ":3333|:4444|:5555|:7777|:8333|:9999" # Monitor for Stratum protocol tcpdump -i any -A -s 0 'port 3333 or port 4444' | grep -i "stratum" - File System Indicators: bash
# Find mining configuration files find / -name "*.json" -exec grep -l "pool\|wallet\|mining" {} \; 2>/dev/null # Look for hidden mining directories find / -type d -name ".*" -exec ls -la {} \; 2>/dev/null | grep -i "xmr\|mine" - Memory Analysis: bash
# Search process memory for mining indicators for pid in $(ps aux | awk '$3 > 80 {print $2}'); do strings /proc/$pid/cmdline 2>/dev/null | grep -i "pool\|stratum" done - Automated Detection Script: bash
#!/bin/bash # Detect potential mining activity echo "Checking for cryptocurrency mining indicators..." # High CPU processes echo "=== High CPU Usage ===" ps aux | awk '$3 > 80 {print $2, $3, $11}' | grep -v "COMMAND" # Suspicious network connections echo -e "\n=== Mining Pool Connections ===" ss -tnp 2>/dev/null | grep -E ":3333|:4444|:5555|:8333" || echo "None found" # Check cron jobs echo -e "\n=== Suspicious Cron Jobs ===" for user in $(cut -d: -f1 /etc/passwd); do crontab -l -u $user 2>/dev/null | grep -E "curl|wget|python|sh" | grep -v "^#" done
Regular monitoring using these commands helps identify mining malware before it significantly impacts system performance.
Related Articles
- SANS Linux Security Documentation
- Red Hat Security Guide
- CIS Linux Benchmarks
- Linux Kernel Security Documentation
- NIST Cybersecurity Framework for Linux
- Linux Security Wiki
- Offensive Security Linux Resources
- Digital Forensics with Linux – DFIR Training
- Kali Linux Documentation
- Linux Audit System Documentation
- OWASP Linux Security Cheat Sheet
- Ubuntu Security Documentation
- Arch Linux Security Wiki
- Linux Command Line Forensics – SANS
- The Linux Documentation Project – Security HOWTO
- MITRE ATT&CK Linux Techniques
Need Expert Linux Security Assessment?
Our security engineers specialize in Linux-based security operations, from penetration testing to incident response and forensic analysis. Contact our team for comprehensive security services tailored to your Linux infrastructure.
This technical deep-dive was prepared by the security research team at Secure Debug, specializing in Linux security, incident response, and advanced command-line techniques for cybersecurity professionals.


