Rack of Ethernet switches.

Visualizing Log Patterns with Color

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.

54.145.83.79 US, United States 23/Oct/2018:02:42:21 /robots.txt
68.150.93.138 CA, Canada 23/Oct/2018:02:42:12 /open-source/linux-break-in-howto.html
68.150.93.138 CA, Canada 23/Oct/2018:02:42:05 /open-source/linux-break-in-howto.html
68.150.93.138 CA, Canada 23/Oct/2018:02:41:57 /open-source/linux-break-in-howto.html
184.3.248.96 US, United States 23/Oct/2018:02:41:41 /technical/dsl/
68.150.93.138 CA, Canada 23/Oct/2018:02:41:28 /robots.txt
68.150.93.138 CA, Canada 23/Oct/2018:02:41:28 /open-source/linux-break-in-howto.html
184.3.248.96 US, United States 23/Oct/2018:02:41:05 /technical/dsl/
111.78.208.141 CN, China 23/Oct/2018:02:40:57 /open-source/package-management.html
111.78.208.141 CN, China 23/Oct/2018:02:40:48 /open-source/package-management.html
162.210.196.98 US, United States 23/Oct/2018:02:39:57 /
162.210.196.98 US, United States 23/Oct/2018:02:39:54 /robots.txt
144.76.81.72 DE, Germany 23/Oct/2018:02:39:44 /cybersecurity/cyberwar.html
207.46.13.2 US, United States 23/Oct/2018:02:39:24 /fun/teaching/turn-the-page.html
144.76.81.72 DE, Germany 23/Oct/2018:02:38:57 /robots.txt
110.151.130.228 AU, Australia 23/Oct/2018:02:38:47 /technical/dsl/
144.76.81.72 DE, Germany 23/Oct/2018:02:38:40 /networking/
66.249.79.135 US, United States 23/Oct/2018:02:38:33 /open-source/linux-break-in-howto.html
144.76.81.72 DE, Germany 23/Oct/2018:02:38:26 /robots.txt
54.36.148.87 FR, France 23/Oct/2018:02:37:58 /travel/japan/tokyo-asakusa/riverfront.html
220.176.233.94 CN, China 23/Oct/2018:02:36:57 /open-source/sendmail-ssl.html
66.249.79.137 US, United States 23/Oct/2018:02:36:00 /open-source/keyboard-mouse.html
220.176.233.94 CN, China 23/Oct/2018:02:35:01 /open-source/sendmail-ssl.html
34.201.101.2 US, United States 23/Oct/2018:02:34:29 /cybersecurity/password-breaking.html
220.176.233.94 CN, China 23/Oct/2018:02:34:19 /open-source/sendmail-ssl.html
212.125.24.108 TR, Turkey 23/Oct/2018:02:34:17 /turkish/turkish-verbs.pdf
34.228.223.239 US, United States 23/Oct/2018:02:33:58 /travel/usa/new-york-jewish-les/houston-clinton-norfolk.html
71.10.224.133 US, United States 23/Oct/2018:02:33:42 /travel/usa/new-york-jewish-les/houston-clinton-norfolk.html
66.249.79.139 US, United States 23/Oct/2018:02:33:36 /open-source/biblical-unix.html
212.125.24.108 TR, Turkey 23/Oct/2018:02:33:34 /turkish/turkish-verbs.pdf
157.55.39.53 US, United States 23/Oct/2018:02:33:30 /open-source/dev-random.html
66.249.79.135 US, United States 23/Oct/2018:02:32:43 /open-source/windows-path.html
66.249.79.139 US, United States 23/Oct/2018:02:32:20 /open-source/raspberry-pi/hardware.html
66.249.79.139 US, United States 23/Oct/2018:02:31:39 /networking/logs-in-color.html
207.46.13.2 US, United States 23/Oct/2018:02:31:34 /open-source/stig-compliance.html
54.157.158.173 US, United States 23/Oct/2018:02:31:33 /technical/dsl/
180.76.15.32 CN, China 23/Oct/2018:02:31:09 /travel/trinidad/
110.151.130.228 AU, Australia 23/Oct/2018:02:31:05 /technical/dsl/
66.249.79.135 US, United States 23/Oct/2018:02:30:45 /open-source/tux-gospel.html
151.80.31.152 FR, France 23/Oct/2018:02:30:45 /robots.txt
66.249.79.137 US, United States 23/Oct/2018:02:29:49 /open-source/qemu-unix.html
213.239.216.194 DE, Germany 23/Oct/2018:02:29:43 /open-source/linux-kernel-details.html
213.239.216.194 DE, Germany 23/Oct/2018:02:29:36 /technical/samsung-galaxy/sim-unlocking.html
213.239.216.194 DE, Germany 23/Oct/2018:02:29:29 /technical/samsung-galaxy/secret-code.html
46.229.168.130 US, United States 23/Oct/2018:02:29:18 /open-source/raspberry-pi/openvas.html
52.25.157.255 US, United States 23/Oct/2018:02:28:50 /travel/uk/glastonbury/king-arthur.html
35.192.84.7 US, United States 23/Oct/2018:02:28:40 /robots.txt
92.40.249.0 GB, United Kingdom 23/Oct/2018:02:27:17 /open-source/openbsd-wireless-wpa2.html
191.85.168.161 AR, Argentina 23/Oct/2018:02:26:17 /open-source/openbsd-qemu-windows-howto.html
66.249.79.137 US, United States 23/Oct/2018:02:26:08 /travel/uk/lee-ho-fook/
72.14.199.71 US, United States 23/Oct/2018:02:26:00 /turkish/verbs.html
88.99.27.172 DE, Germany 23/Oct/2018:02:25:52 /travel/usa/washington-yardley.html
101.184.10.31 AU, Australia 23/Oct/2018:02:25:46 /technical/samsung-galaxy/linux.html
75.84.165.234 US, United States 23/Oct/2018:02:25:22 /travel/belgium/bastogne-ardennes/ardennes-forest.html
157.55.39.72 US, United States 23/Oct/2018:02:25:02 /Index.html
173.219.50.231 US, United States 23/Oct/2018:02:24:48 /technical/dsl/
113.23.240.11 MY, Malaysia 23/Oct/2018:02:24:46 /cybersecurity/verify-digital-signature.html
116.226.156.130 CN, China 23/Oct/2018:02:22:55 /open-source/performance-tuning/disks.html
46.229.168.132 US, United States 23/Oct/2018:02:22:26 /travel/uk/bletchley-park/
148.64.56.67 GB, United Kingdom 23/Oct/2018:02:21:53 /turkish/orthography.html
46.229.168.133 US, United States 23/Oct/2018:02:21:45 /radio/frequencies.html
148.64.56.68 GB, United Kingdom 23/Oct/2018:02:20:46 /technical/samsung-galaxy/block-calls.html
110.145.22.167 AU, Australia 23/Oct/2018:02:20:22 /open-source/linux-break-in-howto.html
49.205.12.163 IN, India 23/Oct/2018:02:20:13 /
177.182.2.1 BR, Brazil 23/Oct/2018:02:20:09 /turkish/word-order.html
148.64.56.75 GB, United Kingdom 23/Oct/2018:02:20:07 /travel/egypt/luxor.html
66.249.79.135 US, United States 23/Oct/2018:02:19:55 /travel/france/normandy/
18.212.121.38 US, United States 23/Oct/2018:02:19:25 /technical/hdmi.html
68.40.59.230 US, United States 23/Oct/2018:02:19:23 /technical/hdmi.html
66.102.6.153 US, United States 23/Oct/2018:02:19:18 /travel/
201.252.63.218 AR, Argentina 23/Oct/2018:02:19:12 /radio/j-poles.html
75.1.198.48 US, United States 23/Oct/2018:02:18:26 /open-source/performance-tuning/disks.html
175.157.153.237 LK, Sri Lanka 23/Oct/2018:02:18:20 /turkish/turkish-suffixes.html
175.157.153.237 LK, Sri Lanka 23/Oct/2018:02:18:13 /turkish/verbs.html
90.118.64.201 FR, France 23/Oct/2018:02:17:37 /travel/usa/new-york-andy-warhol/
99.203.15.238 US, United States 23/Oct/2018:02:16:39 /technical/dsl/
180.76.15.156 CN, China 23/Oct/2018:02:16:26 /travel/trinidad/
207.237.83.16 US, United States 23/Oct/2018:02:16:13 /travel/usa/new-york-les-rivington/

Here's what's going on.

Each line above is a request from a client, extracted from Apache's /var/www/logs/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.

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