Rack of Ethernet switches.

Visualizing Log Patterns with Color

Nginx and Apache Logs in Color

Web server logs reveal patterns of activity by web crawlers. Some are indexing crawlers operated by search engines, some are mysterious. Another pattern is systematic blind searches for vulnerable server-side executables or other configuration problems. The pattern you want to see is the interested user who follows some path through the hyperlinks on your site, taking time to read the pages.

Maybe we could use color to help spot these patterns?

Maybe...

First, let's look at the result, then the explanation comes later. Here are the most recent client requests, starting most recent first. Your request for this page won't appear there as it isn't complete by the time this page was automatically generated with PHP. But if you reload the page you should see your initial request near the top.

44.200.27.215 US, United States 12/Apr/2024:13:25:48 /robots.txt
17.241.75.102 US, United States 12/Apr/2024:13:25:41 /travel/uk/edinburgh/
51.222.253.13 FR, France 12/Apr/2024:13:24:50 /travel/usa/venice-california/
128.124.121.110 UA, Ukraine 12/Apr/2024:13:24:20 /travel/japan/katakana-hiragana/
24.173.48.2 US, United States 12/Apr/2024:13:24:10 /cybersecurity/
193.135.10.216 CH, Switzerland 12/Apr/2024:13:24:09 /travel/usa/new-york-hp-lovecraft/?s=mb
91.107.203.134 IR, Iran, Islamic Republic of 12/Apr/2024:13:23:47 /travel/usa/new-york-hp-lovecraft/?s=mb
138.201.48.250 DE, Germany 12/Apr/2024:13:23:46 /travel/usa/new-york-hp-lovecraft/?s=mb
88.198.208.91 DE, Germany 12/Apr/2024:13:23:36 /travel/belgium/art-nouveau-architecture/?s=mb
49.13.224.169 IN, India 12/Apr/2024:13:22:57 /travel/turkey/istanbul/?s=mb
85.208.98.198 Unknown 12/Apr/2024:13:22:39 /travel/usa/new-york-jewish-les/synagogues.html
85.208.98.192 Unknown 12/Apr/2024:13:22:38 /robots.txt
47.102.219.201 CN, China 12/Apr/2024:13:22:37 /travel/japan/tokyo-yasukuni-shrine/
104.28.243.105 US, United States 12/Apr/2024:13:22:28 /cybersecurity/crypto/?s=mb
40.77.167.52 US, United States 12/Apr/2024:13:22:25 /travel/usa/earthworks/merom-site/
49.13.74.63 IN, India 12/Apr/2024:13:22:11 /travel/usa/new-york-marvel/?s=mb
66.249.72.70 US, United States 12/Apr/2024:13:22:05 /travel/usa/poe-in-new-york/
66.249.72.69 US, United States 12/Apr/2024:13:22:02 /travel/uk/avebury/?s=mb
69.164.195.165 US, United States 12/Apr/2024:13:21:50 /travel/usa/new-york-roosevelts/?s=mb
24.159.133.152 US, United States 12/Apr/2024:13:21:15 /travel/france/school-lunch-menus/
103.136.147.41 Unknown 12/Apr/2024:13:20:57 /travel/usa/north-carolina/?s=mb
213.109.138.15 UA, Ukraine 12/Apr/2024:13:20:56 /travel/japan/katakana-hiragana/
176.116.117.68 SK, Slovakia 12/Apr/2024:13:20:54 /travel/usa/new-york-roosevelts/?s=mb
149.154.161.250 GB, United Kingdom 12/Apr/2024:13:20:49 /travel/japan/katakana-hiragana/
163.182.118.136 US, United States 12/Apr/2024:13:20:45 /travel/usa/new-york-roosevelts/?s=mb
213.109.138.15 UA, Ukraine 12/Apr/2024:13:20:20 /travel/japan/katakana-hiragana/
207.46.13.127 US, United States 12/Apr/2024:13:20:14 /travel/belgium/bastogne-ardennes/bastogne.html
66.249.72.69 US, United States 12/Apr/2024:13:20:08 /travel/usa/poe-in-new-york/
199.16.157.180 US, United States 12/Apr/2024:13:20:02 /travel/usa/north-carolina/?s=tb
199.16.157.180 US, United States 12/Apr/2024:13:20:02 /robots.txt
199.16.157.180 US, United States 12/Apr/2024:13:20:02 /robots.txt
167.235.141.230 US, United States 12/Apr/2024:13:19:45 /cybersecurity/availability/cloud-archiving.html?s=mc
64.227.98.185 US, United States 12/Apr/2024:13:19:44 /travel/usa/new-york-roosevelts/?s=mb
101.44.251.127 CN, China 12/Apr/2024:13:19:31 /open-source/
51.222.253.6 FR, France 12/Apr/2024:13:19:20 /travel/uk/ben-nevis/
149.28.233.82 US, United States 12/Apr/2024:13:19:11 /travel/uk/glen-nevis/?s=mb
195.221.21.169 FR, France 12/Apr/2024:13:18:42 /travel/usa/new-york-roosevelts/?s=mb
46.22.99.207 IS, Iceland 12/Apr/2024:13:18:32 /travel/usa/new-york-hp-lovecraft/?s=mb
54.224.89.68 US, United States 12/Apr/2024:13:18:13 /open-source/torrent-magnet-links.html
66.249.72.70 US, United States 12/Apr/2024:13:18:10 /travel/usa/poe-in-new-york/
217.58.122.215 IT, Italy 12/Apr/2024:13:18:10 /travel/usa/new-york-roosevelts/?s=mb
49.13.224.169 IN, India 12/Apr/2024:13:18:04 /travel/japan/kofun/emperor-kaika.html?s=mb
74.194.226.131 US, United States 12/Apr/2024:13:17:44 /travel/usa/us-wash-masonic.html?s=mb
207.46.13.127 US, United States 12/Apr/2024:13:17:34 /travel/japan/tokyo-asakusa/hoppy-street.html?s=mb
3.81.78.61 US, United States 12/Apr/2024:13:17:19 /open-source/sendmail-ssl.html
223.25.78.49 SG, Singapore 12/Apr/2024:13:17:16 /travel/usa/new-york-roosevelts/?s=mb
72.212.121.100 US, United States 12/Apr/2024:13:17:11 /travel/usa/new-york-roosevelts/?s=mb
64.233.173.204 AP, Asia/Pacific Region 12/Apr/2024:13:17:09 /travel/uk/Index.html
135.181.176.244 CA, Canada 12/Apr/2024:13:17:07 /travel/usa/new-york-roosevelts/?s=mb
24.199.82.195 US, United States 12/Apr/2024:13:17:07 /travel/usa/new-york-roosevelts/?s=mb
45.76.14.84 US, United States 12/Apr/2024:13:17:04 /travel/usa/new-york-roosevelts/?s=mb
197.86.221.32 ZA, South Africa 12/Apr/2024:13:17:04 /travel/usa/new-york-roosevelts/?s=mb
66.76.189.5 US, United States 12/Apr/2024:13:17:02 /travel/usa/new-york-roosevelts/?s=mb
185.216.161.89 NL, Netherlands 12/Apr/2024:13:17:00 /travel/usa/new-york-roosevelts/?s=mb
51.91.220.155 FR, France 12/Apr/2024:13:17:00 /travel/usa/new-york-roosevelts/?s=mb

Here's what's going on.

Each line above is a request from a client, extracted from Nginx's /var/www/logs/httpd-access.log file. The client IP address, timestamp, and requested path were selected with awk and the client IP address converted to a country if possible with geoiplookup.

Geolocate IP

You can use a service such as Abstract's IP geolocation to check if the conversion was successful, or if the client IP address is the exit portal of a VPN.

The first 3 octets or first 24 bits of the IP address are used to specify the hue, with chroma at 75% and intensity at 100%. The resulting red, green, and blue values are scaled to the range of 0-255 and printed as two-character hexadecimal in an HTML style string.

Low-numbered /8 networks appear as red, 20.0.0.0/8 through 40.0.0.0/8 are orange shifting to yellow, 50.0.0.0/8 through 110.0.0.0/8 are shades of green, the /16 networks 130.0.0.0/16 through about 180.0.0.0/16 are shades of blue, then it's shades of purple into magenta for the /24 networks 192.0.0.0/24 and up through 223.255.255.0/24.

The HTML file on the server has a line where PHP uses passthru() to call the following shell script:

#!/bin/sh

# Initial pipeline:
# tail		Just the last 200 (or slightly less after the grep)
# grep		... just the requests out of that
# cat | sort	... put into reverse order
# sed		... remove the quotes and square brackets
# awk		... print the IP address twice, timestamp, and requested path
# sed		... remove the first 3 dots to split first version of IP
#			address into octets, and remove any characters that
#			could cause trouble when inserted into this page
# I need to use the client IP address, field #5 at that point, to call
# geoiplookup.  So, send the initial pipeline into a while loop that
# assigns variables, sets a new variable, and then echoes the resulting
# collection into awk.
tail -200 /var/www/logs/access_log |
	grep 'GET.*200' |
	cat -n | sort -nr |
	sed -e 's/"/ /g' -e 's/\[//g' -e 's/\]//g' |
	awk '{print $2, $2, $5, $8}' |
	sed -e 's/\./ /' -e 's/\./ /' -e 's/\./ /' -e 's/[<>]//g' |
	while read IP1 IP2 IP3 IP4 CLIENTIP TIMESTAMP URL
	do
		COUNTRY=$( geoiplookup $CLIENTIP |
				sed 's/.*Edition: //' |
				sed 's/IP Address not found/Unknown/' )
		echo $IP1 $IP2 $IP3 $IP4 $CLIENTIP $COUNTRY $TIMESTAMP $URL |
		awk '{
			ip1 = $1;
			ip2 = $2;
			ip3 = $3;
			chroma = 0.75;
			hue = 6*(ip1*255*255 + ip2*255 + ip3)/(255*255*255);
			if (hue%2 > 1) {
				x = chroma*(1.0 - (hue%2 - 1));
			} else {
				x = chroma*(1.0 - (1 - hue%2));
			}
			if (hue < 1.0) {
				r = chroma;
				g = x;
				b = 0;
			} else if (hue < 2.0) {
				r = x;
				g = chroma;
				b = 0;
			} else if (hue < 3.0) {
				r = 0;
				g = chroma;
				b = x;
			} else if (hue < 4.0) {
				r = 0;
				g = x;
				b = chroma;
			} else if (hue < 5.0) {
				r = x;
				g = 0;
				b = chroma;
			} else {
				r = chroma;
				g = 0;
				b = x;
			}
			r = (r + 0.25)*255;
			g = (g + 0.25)*255;
			b = (b + 0.25)*255;

			printf("<div class=\"col-12 textleft\" ");
			printf("style=\"color:#000; background:#%02x%02x%02x;\"> ", r, g, b);
			for (i = 5; i <= NF; i++) {
				printf("%s ", $i);
			}
			printf("</div>\n");
		}'
	done 

Other Pages