Log Analysis
Log analysis is the systematic examination of log files to identify security incidents, unauthorized access, system anomalies, and attack patterns
The 5 W's of Log Analysis
Who: Which user, service, or system generated the event?What: What action or event occurred?When: At what date and time did it happen?Where: On which system, service, or component?Why: What was the context or trigger for the event?
Analysis Approach
Baseline Normal: Understand what normal looks likeIdentify Anomalies: Detect deviations from baselineCorrelate Events: Connect related activities across logsBuild Timeline: Reconstruct sequence of eventsValidate Findings: Confirm suspicions with multiple sources
Essential Log Analysis Commands
Basic Log Viewing
less /var/log/logfile
more /var/log/logfiletail -n 50 /var/log/logfile
head -n 50 /var/log/logfiletail -f /var/log/logfilesed -n '100,200p' /var/log/logfile
awk 'NR>=100 && NR<=200' /var/log/logfilePattern Searching
grep "pattern" /var/log/logfilegrep -i "pattern" /var/log/logfilegrep -i "pattern1\|pattern2\|pattern3" /var/log/logfile
grep -iE "pattern1|pattern2|pattern3" /var/log/logfile # Extended regexgrep -i -A 5 "pattern" /var/log/logfile # 5 lines After match
grep -i -B 5 "pattern" /var/log/logfile # 5 lines Before match
grep -i -C 5 "pattern" /var/log/logfile # 5 lines of Context (both)grep -v "pattern" /var/log/logfile
grep -iv "INFO\|DEBUG" /var/log/logfilegrep -i "pattern" /var/log/logfile | wc -lgrep -l "pattern" /var/log/*.loggrep -n "pattern" /var/log/logfilegrep -r "pattern" /var/log/ 2>/dev/null
grep -ri "pattern" /var/log/ 2>/dev/nullAdvanced Filtering
awk '{print $1, $2, $3}' /var/log/logfileawk -F':' '{print $1, $3}' /var/log/logfile
awk -F',' '{print $2, $5}' /var/log/logfileawk '$3 == "FAILED"' /var/log/logfile
awk '$1 ~ /192.168/ {print $0}' /var/log/logfile # Pattern match in column 1awk '$5 > 100' /var/log/logfile # Column 5 greater than 100
awk '$2 >= 500 && $2 < 600' /var/log/logfile # Status codes 500-599grep "pattern" /var/log/logfile | awk '{print $1, $7}'awk '{print $1}' /var/log/logfile | sort | uniq -c | sort -rnawk '$1 == "2025-12-29" && $2 >= "05:00:00" && $2 <= "06:00:00"' /var/log/logfileSorting and Uniqueness
sort /var/log/logfilesort -n /var/log/logfilesort -r /var/log/logfilesort -k 3 /var/log/logfile # Sort by 3rd column
sort -t',' -k2 /var/log/logfile # Sort by 2nd column, comma delimitersort /var/log/logfile | uniqsort /var/log/logfile | uniq -c | sort -rnsort /var/log/logfile | uniq -usort /var/log/logfile | uniq -dCounting and Statistics
wc -l /var/log/logfilewc -w /var/log/logfilewc -c /var/log/logfilegrep -c "pattern" /var/log/logfile
grep -i "pattern" /var/log/logfile | wc -l # Alternativecut -d' ' -f1 /var/log/logfile | sort | uniq -c | sort -rn | head -20Authentication Log Analysis
Common Authentication Events
grep -i "Accepted password\|Accepted publickey" /var/log/auth.loggrep -i "Failed password" /var/log/auth.loggrep -i "Invalid user" /var/log/auth.loggrep -i "Failed password for root" /var/log/auth.loggrep -i "session opened\|session closed" /var/log/auth.loggrep -i "sudo.*COMMAND" /var/log/auth.log
grep -i "sudo.*root" /var/log/auth.loggrep -i "su\[" /var/log/auth.logSession Analysis
grep "sshd\[12345\]" /var/log/auth.loggrep "192.168.1.100" /var/log/auth.log | grep "Accepted" | awk '{print $NF}' | grep -oP 'sshd\[\K[0-9]+' | sort -uSESSION_PID="12345"
grep "sshd\[$SESSION_PID\]" /var/log/auth.loggrep "sshd\[12345\]" /var/log/auth.log | grep "session opened"
grep "sshd\[12345\]" /var/log/auth.log | grep "session closed"Automated vs Manual Detection
Rapid succession of attempts (seconds apart)
Successful login immediately followed by logout
Session duration of < 10 seconds
Dictionary attack pattern (sequential common usernames)Session duration of minutes/hours
Commands executed during session
Multiple failed attempts with breaks in between
Successful login with sustained connectionCheck Brute Force Analysisfor more details.
Last updated