Who is reading my blog
Having recently created this page, I decided to try analyse my logs to see who is accessing (or trying to hack) my server. In particular, I am interesting if any humans are reading my posts.
I created logsqlifier as a tool that takes logs from multiple sources (eg. nginx and sshd) and ingests them into an sqlite database.
Some general log analysis
One thing that struck me immediately, is how many bots are querying non existing pages
| status_code | count |
|---|---|
| 200 | 2392 |
| 404 | 13551 |
This lead me the look up what these bots are actually trying to access
| request_path | count |
|---|---|
| /wp-admin/install.php?step=1 | 266 |
| /robots.txt | 186 |
| /.git/config | 96 |
| /.env | 93 |
| /app/.env | 58 |
| /api/.env | 57 |
| /.env.production | 57 |
| /wp-login.php | 56 |
| /.env.local | 56 |
| /.env.bak | 56 |
| /backend/.env | 55 |
| /.env.save | 55 |
| /.env.prod | 54 |
| /.env.backup | 54 |
| /.env.dev | 53 |
Looking at the valid requests, there are are also some shenanigans there like /?static=1&order=asc
| request_path | count |
|---|---|
| / | 1414 |
| /favicon.ico | 199 |
| /static/style.css | 105 |
| /about_me.html | 88 |
| /posts/remote_journald_logs.html | 62 |
| /posts/notes_on_journalctl.html | 60 |
| /posts/starting_a_blog.html | 56 |
| /posts/why_do_my_videos_play_so_slow.html | 48 |
| /posts/kitty_overlays.html | 44 |
| /posts/monitoring_internet_connection_state/monitoring_my_internet_connection_state.html | 42 |
| /atom.xml | 41 |
| /posts/editing_videos_with_ffmpeg.html | 38 |
| /index.html | 25 |
| /robots.txt | 15 |
| /?p=1 | 15 |
| /?rest_route=/ | 14 |
| /posts.html | 10 |
| /posts/monitoring_internet_connection_state/internet-state-grafana-example.webp | 7 |
| /?static=1&order=asc | 7 |
| /?rest_route=/wpadverts/v1/classifieds-types | 7 |
One assumption I had was that malicious bots would not query the css files (what possible information could they get from there?). It seems that search engine crawlers like googlebot fetch your css pages, but these can easily be identified via the User Agent.
Let's have a look at the User Agents of the requests that are fetch my css sheet.
| User Agent | Request Count |
|---|---|
| Mozilla/5.0 (X11; Linux x86_64; rv:157.0) Gecko/20100101 Firefox/157.0 | 38 |
| Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/125.0.6422.60 Safari/537.36 Edge/12.246 | 11 |
| Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 Chrome/153.0.8010.52 Mobile Safari/537.36 (compatible; Googlebot/2.1) | 6 |
| Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/117.0.5938.132 Safari/537.36 | 5 |
| Mozilla/5.0 (iPhone; CPU iPhone OS 18_7_8 like Mac OS X) AppleWebKit/605.1.15 Version/26.0 Mobile/15E148 Safari/604.1 | 3 |
| Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36 | 3 |
| Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:156.0) Gecko/20100101 Firefox/156.0 | 3 |
| Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/102.0.0.0 Safari/537.36 | 3 |
| Mozilla/5.0 (Android 15; Mobile; rv:158.0) Gecko/158.0 Firefox/158.0 | 3 |
| RootEvidence/1.0 | 2 |
Googlebot does show on there, but a quick search with AI tells me this is a spoofed version and probably a malicious actor, which invalidated my previous assumption.
Are any humans reading my blog?
Filtering the bots out of my traffic logs is challenging because
- Bots can just spoof any User Agent
- IP adresses can change overtime for a given machine
- Stylesheets and other assets get cached by the browser in different ways
I tried (with the help of AI) to come up with an SQL query to identify human readers
WITH marked AS (
SELECT client_ip, timestamp / 86400 AS session, request_path, status_code, user_agent,
request_path LIKE '%.css' OR request_path LIKE '%.ico'
OR request_path LIKE '%.webp' AS asset
FROM nginx_logs
),
human_sessions AS (
SELECT client_ip, session
FROM marked
GROUP BY client_ip, session
HAVING MAX(user_agent LIKE 'Mozilla/5.0%'
AND (user_agent LIKE '%Chrome/%' OR user_agent LIKE '%Firefox/%'
OR user_agent LIKE '%Safari/%' OR user_agent LIKE '%Edg/%')) = 1
AND SUM(status_code >= 400) = 0
AND SUM(asset) > 0
AND COUNT(DISTINCT CASE WHEN NOT asset THEN request_path END) >= 2
)
SELECT request_path, COUNT(*) AS views
FROM marked JOIN human_sessions USING (client_ip, session)
WHERE NOT asset
GROUP BY request_path
ORDER BY views DESC;A few things:
- marked creates a
sessioncolumn, which is used to account for the fact that IP addresses change. The same ip address in the same day is assumed to be in the same machine. - humansessions
And behold the result
| request_path | views |
|---|---|
| / | 65 |
| /about_me.html | 36 |
| /posts/notes_on_journalctl.html | 27 |
| /posts/starting_a_blog.html | 20 |
| /atom.xml | 14 |
| /posts/remote_journald_logs.html | 11 |
| /posts/why_do_my_videos_play_so_slow.html | 10 |
| /posts/monitoring_internet_connection_state/monitoring_my_internet_connection_state.html | 7 |
| /posts/kitty_overlays.html | 6 |
| /posts/editing_videos_with_ffmpeg.html | 6 |
| /index.html | 1 |
Notes
- The result seems valid, but I will keep improving this detection and logsqlifier as time goes by and I learn more
- I think this would be a pretty cool application for a “classic” machine learning model, provided I could come up with a large enough correctly labeled dataset