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.

3.238.157.198 US, United States 25/Apr/2024:15:11:24 /open-source/raspberry-pi/openvas.html?s=mc
40.77.167.14 US, United States 25/Apr/2024:15:11:22 /travel/uk/bletchley-park/Index.html
40.77.167.14 US, United States 25/Apr/2024:15:11:22 /travel/greece/folegandros/Index.html
52.167.144.236 US, United States 25/Apr/2024:15:11:18 /travel/france/pierre-tourneresse/Index.html
192.133.77.17 US, United States 25/Apr/2024:15:11:11 /travel/poland/?s=tb
34.206.217.125 US, United States 25/Apr/2024:15:10:58 /travel/japan/kofun/konabe.html
93.93.135.84 GB, United Kingdom 25/Apr/2024:15:10:29 /travel/iceland/hali-hofn/?s=mb
132.145.78.154 US, United States 25/Apr/2024:15:10:20 /blog/2023/01/03-whats-up-with-my-social-media.html
40.77.167.14 US, United States 25/Apr/2024:15:10:20 /radio/nc2030-qrp-transceiver/Index.html
2.59.165.195 Unknown 25/Apr/2024:15:10:12 /open-source/performance-tuning/nfs.html
74.136.183.80 US, United States 25/Apr/2024:15:10:09 /cybersecurity/comptia/domain-4-answers.html
183.192.118.26 CN, China 25/Apr/2024:15:09:54 /travel/turkey/hans/
183.192.118.26 CN, China 25/Apr/2024:15:09:53 /robots.txt
183.192.118.18 CN, China 25/Apr/2024:15:09:48 /travel/uk/scotland-isle-of-iona/
183.192.118.18 CN, China 25/Apr/2024:15:09:48 /robots.txt
40.77.167.143 US, United States 25/Apr/2024:15:09:43 /travel/france/lavomat/
106.14.89.110 CN, China 25/Apr/2024:15:09:40 /fun/brilliant-movie-ideas/hick-school.html
65.108.255.89 US, United States 25/Apr/2024:15:09:22 /travel/mexico/mexico-city-tepoztlan.html?s=mb
103.131.71.207 Unknown 25/Apr/2024:15:09:16 /robots.txt
51.91.220.155 FR, France 25/Apr/2024:15:08:44 /travel/iceland/hali-hofn/?s=mb
66.249.79.6 US, United States 25/Apr/2024:15:08:41 /travel/greece/orthodox-shrines.html
66.249.79.7 US, United States 25/Apr/2024:15:08:38 /travel/replace-sim-card/
66.249.79.5 US, United States 25/Apr/2024:15:08:37 /travel/france/avignon/corrupt-popes.html
66.249.79.5 US, United States 25/Apr/2024:15:08:36 /travel/greece/patmos/ferry-to-patmos.html
217.58.122.215 IT, Italy 25/Apr/2024:15:08:36 /travel/iceland/hali-hofn/?s=mb
217.160.10.52 DE, Germany 25/Apr/2024:15:08:28 /travel/turkey/gallipoli/?s=mb
84.152.123.114 DE, Germany 25/Apr/2024:15:08:09 /cybersecurity/history/bwi.html
72.212.121.100 US, United States 25/Apr/2024:15:07:46 /travel/turkey/gallipoli/?s=mb
52.167.144.236 US, United States 25/Apr/2024:15:07:27 /travel/belgium/bastogne-ardennes/
37.82.215.184 DE, Germany 25/Apr/2024:15:06:45 /oliver-cromwell/crom-fu-fighting.html?s=mb
194.163.166.161 DE, Germany 25/Apr/2024:15:06:27 /travel/latvia/?s=mb
135.181.176.244 CA, Canada 25/Apr/2024:15:06:25 /travel/iceland/hali-hofn/?s=mb
3.142.200.226 US, United States 25/Apr/2024:15:06:01 /open-source/
62.210.209.158 FR, France 25/Apr/2024:15:06:01 /oliver-cromwell/?s=mb
138.197.120.39 US, United States 25/Apr/2024:15:05:57 /blog/2023/01/03-whats-up-with-my-social-media.html
198.91.54.245 US, United States 25/Apr/2024:15:05:55 /open-source/raspberry-pi/openvas.html
52.167.144.221 US, United States 25/Apr/2024:15:05:39 /travel/france/budget-hotels/
159.138.102.200 SG, Singapore 25/Apr/2024:15:05:37 /open-source/?s=mc
195.221.21.169 FR, France 25/Apr/2024:15:05:17 /blog/2023/01/03-whats-up-with-my-social-media.html
62.113.99.128 RU, Russian Federation 25/Apr/2024:15:05:10 /travel/turkey/selcuk/
132.145.78.154 US, United States 25/Apr/2024:15:05:04 /travel/iceland/hali-hofn/?s=mb
146.190.252.12 US, United States 25/Apr/2024:15:04:58 /travel/turkey/gallipoli/?s=mb
52.167.144.236 US, United States 25/Apr/2024:15:04:57 /travel/greece/mycenae.html
51.222.253.18 FR, France 25/Apr/2024:15:04:52 /travel/france/normandy/brecourt-manor.html
17.241.227.182 US, United States 25/Apr/2024:15:04:48 /travel/greece/greek-food.html
217.122.55.84 NL, Netherlands 25/Apr/2024:15:04:37 /travel/canada/ottawa/?s=mb
69.164.195.165 US, United States 25/Apr/2024:15:04:27 /travel/canada/ottawa/?s=mb
66.196.39.69 CA, Canada 25/Apr/2024:15:04:20 /travel/iceland/hali-hofn/?s=mb
52.167.144.194 US, United States 25/Apr/2024:15:03:49 /travel/france/flaneur-10eme-arrondissement/
183.192.118.26 CN, China 25/Apr/2024:15:03:07 /robots.txt
3.144.97.189 US, United States 25/Apr/2024:15:03:06 /robots.txt
45.79.248.212 US, United States 25/Apr/2024:15:03:04 /blog/2023/01/03-whats-up-with-my-social-media.html
104.28.227.187 US, United States 25/Apr/2024:15:03:03 /blog/2023/01/03-whats-up-with-my-social-media.html
5.161.178.12 IR, Iran, Islamic Republic of 25/Apr/2024:15:02:57 /blog/2023/01/03-whats-up-with-my-social-media.html
34.199.145.87 US, United States 25/Apr/2024:15:02:57 /ads.txt
185.191.171.17 MD, Moldova, Republic of 25/Apr/2024:15:02:55 /robots.txt
91.107.131.102 IR, Iran, Islamic Republic of 25/Apr/2024:15:02:53 /blog/2023/01/03-whats-up-with-my-social-media.html
185.191.171.9 MD, Moldova, Republic of 25/Apr/2024:15:02:51 /robots.txt
81.187.48.147 GB, United Kingdom 25/Apr/2024:15:02:49 /blog/2023/01/03-whats-up-with-my-social-media.html
86.9.76.176 GB, United Kingdom 25/Apr/2024:15:02:49 /blog/2023/01/03-whats-up-with-my-social-media.html
40.77.167.52 US, United States 25/Apr/2024:15:02:47 /travel/belgium/mons/
40.77.167.23 US, United States 25/Apr/2024:15:02:31 /open-source/rhel-oracle-centos-6-7-8-9/booting.html
89.58.41.236 DE, Germany 25/Apr/2024:15:01:26 /blog/2023/01/03-whats-up-with-my-social-media.html
88.198.161.239 DE, Germany 25/Apr/2024:15:01:20 /blog/2023/01/03-whats-up-with-my-social-media.html
51.91.220.155 FR, France 25/Apr/2024:15:01:19 /travel/syria/damascus.html?s=mb
185.170.114.165 DE, Germany 25/Apr/2024:15:01:17 /blog/2023/01/03-whats-up-with-my-social-media.html
136.55.229.122 US, United States 25/Apr/2024:15:01:16 /blog/2023/01/03-whats-up-with-my-social-media.html
89.58.36.157 DE, Germany 25/Apr/2024:15:01:16 /blog/2023/01/03-whats-up-with-my-social-media.html
51.75.147.37 FR, France 25/Apr/2024:15:01:16 /blog/2023/01/03-whats-up-with-my-social-media.html
185.250.56.35 CH, Switzerland 25/Apr/2024:15:01:16 /blog/2023/01/03-whats-up-with-my-social-media.html
49.12.191.254 IN, India 25/Apr/2024:15:01:16 /blog/2023/01/03-whats-up-with-my-social-media.html

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