Cheat sheets / Defence

Log Analysis Cheat Sheet

Read web server and SSH logs, and use grep, awk, sort and uniq one-liners to find top IPs, failed logins and signs of attack. · Updated 2026-12-04

Logs tell you what happened, when and from where. This page shows the common log formats and the command-line one-liners analysts use every day.

Where the logs are

Log Usual location
Nginx access log /var/log/nginx/access.log
Nginx error log /var/log/nginx/error.log
Apache access log /var/log/apache2/access.log (Debian, Ubuntu) or /var/log/httpd/access_log (Red Hat family)
SSH and sudo (Debian, Ubuntu) /var/log/auth.log
SSH and sudo (Red Hat family) /var/log/secure
systemd journal journalctl (for example journalctl -u ssh)

Older logs are often rotated to access.log.1, access.log.2.gz and so on. Use zcat or zgrep to read .gz files without extracting them.

Reading a web access log line

Nginx and Apache both use the "combined" format by default:

203.0.113.45 - - [04/Dec/2026:10:15:32 +0500] "GET /login.php?user=admin HTTP/1.1" 200 1532 "-" "Mozilla/5.0"
Part Example awk field
Client IP 203.0.113.45 $1
Identity, user - - $2, $3
Date and time [04/Dec/2026:10:15:32 +0500] $4, $5
Method "GET $6
Path /login.php?user=admin $7
Protocol HTTP/1.1" $8
Status code 200 $9
Response size (bytes) 1532 $10
Referrer "-" $11
User agent "Mozilla/5.0" $12 onwards

The awk fields assume the default format with no extra spaces. Check one line first.

Reading auth.log

Dec  4 10:20:01 web1 sshd[2211]: Failed password for root from 198.51.100.23 port 51422 ssh2
Dec  4 10:20:05 web1 sshd[2213]: Failed password for invalid user oracle from 198.51.100.23 port 51430 ssh2
Dec  4 10:21:10 web1 sshd[2240]: Accepted password for ali from 192.0.2.15 port 50122 ssh2
Dec  4 10:22:00 web1 sudo:      ali : TTY=pts/0 ; PWD=/home/ali ; USER=root ; COMMAND=/usr/bin/apt update
Phrase Meaning
Failed password for <user> Wrong password for a real account
Failed password for invalid user <user> Someone tried a user name that does not exist
Invalid user <user> from <ip> Login attempt with an unknown user name
Accepted password for / Accepted publickey for Successful login
sudo: ... COMMAND= A user ran a command as root

Newer systems may log to the journal only. Use journalctl -u ssh or journalctl -u sshd.

One-liners for web logs

Goal Command
Top 10 client IPs awk '{print $1}' access.log | sort | uniq -c | sort -rn | head
Count of each status code awk '{print $9}' access.log | sort | uniq -c | sort -rn
Top requested paths awk '{print $7}' access.log | sort | uniq -c | sort -rn | head -20
All 404s awk '$9 == 404' access.log
IPs causing the most 404s awk '$9 == 404 {print $1}' access.log | sort | uniq -c | sort -rn | head
All 5xx errors awk '$9 ~ /^5/' access.log
Everything from one IP grep "^203.0.113.45 " access.log
Requests per hour awk '{print substr($4, 2, 14)}' access.log | sort | uniq -c
Top user agents awk -F'"' '{print $6}' access.log | sort | uniq -c | sort -rn | head
POST requests only grep '"POST ' access.log

One-liners for auth.log

Goal Command
All failed SSH logins grep "Failed password" /var/log/auth.log
Count failed logins grep -c "Failed password" /var/log/auth.log
Top IPs with failed logins grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head
User names attackers tried grep "Invalid user" /var/log/auth.log | awk '{print $8}' | sort | uniq -c | sort -rn | head
Successful logins grep "Accepted" /var/log/auth.log
Did a bad IP ever succeed? grep "Accepted" /var/log/auth.log | grep "198.51.100.23"
sudo commands grep "COMMAND=" /var/log/auth.log

$(NF-3) means "the fourth field from the end". It finds the IP because the line ends with from <ip> port <n> ssh2. The field numbers for Invalid user lines can change between versions, so check one line first.

Command building blocks

Command What it does
grep -i Case-insensitive match
grep -E "a|b" Match either pattern
awk '{print $1}' Print the first field (split on spaces)
awk -F'"' '{print $2}' Split on a double quote instead
sort Sort lines (needed before uniq)
uniq -c Count repeated lines next to each other
sort -rn Sort by number, largest first
head / tail First or last 10 lines
cut -d' ' -f1 Another way to take the first field
zgrep / zcat Search or read .gz rotated logs

URL decoding

Attackers often encode their payloads. Decode them before you judge a request.

Encoded Character
%20 or + space
%27 '
%22 "
%3C / %3E < / >
%2F /
%3D =
%2E .
%25 % (watch for double encoding like %252e)

Decode on the command line:

python3 -c "import sys, urllib.parse; print(urllib.parse.unquote_plus(sys.argv[1]))" "%27%20OR%201%3D1--"
# ' OR 1=1--

Signs of attacks in web logs

What you see Likely activity
Many 404s from one IP in a short time Directory or file brute forcing
User agents like sqlmap, Nikto, Nmap Scripting Engine, gobuster Automated scanning tools
', UNION SELECT, OR 1=1, SLEEP( in parameters SQL injection attempts
<script>, onerror=, javascript: in parameters XSS attempts
../ or ..%2F and paths like /etc/passwd Path traversal
Requests for /.env, /.git/, /wp-login.php, /phpmyadmin Scanning for common weak spots
Many POSTs to /login from one IP Password guessing
A 200 response after many failed attempts A possible successful attack. Investigate first

Signs of attacks in auth.log

What you see Likely activity
Hundreds of Failed password lines from one IP SSH brute force
Many Invalid user names like admin, test, oracle Password spraying with common user names
Failures from many IPs against one user Distributed attack on one account
Accepted from an IP that just failed many times Possible compromise. Treat as an incident
Logins at unusual hours or from unusual places Check with the account owner
New sudo commands such as useradd or changes to /etc/sudoers Possible persistence after a break-in

Tips

  • Note the time zone of the log before you compare it with other logs.
  • Copy logs before you work on them. Never edit the original evidence.
  • Write down every command you run and what it showed.

Spotted a mistake or something unclear? Report a mistake