Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Publishes to PyPI when a GitHub release is published, using Trusted Publishing
# (OIDC), so no API token is stored in this repository.
name: Release

on:
release:
types: [published]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.13"
- run: python -m pip install "build>=1.2,<2" "twine>=6,<7"
- run: python -m build
- run: python -m twine check --strict dist/*

- name: Check the release tag matches the package version
env:
TAG: ${{ github.event.release.tag_name }}
run: |
version="${TAG#v}"
test -f "dist/loghunt-${version}.tar.gz" || {
echo "::error::Tag $TAG does not match the version in src/loghunt/__init__.py"
ls dist
exit 1
}

- name: Smoke-test the wheel
run: |
python -m venv /tmp/smoke
/tmp/smoke/bin/pip install dist/*.whl
/tmp/smoke/bin/loghunt --version
/tmp/smoke/bin/loghunt samples/access.log.gz --top 3 --fail || test $? -eq 1

- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: dist
path: dist/
if-no-files-found: error

publish:
needs: build
runs-on: ubuntu-latest
timeout-minutes: 10
environment:
name: pypi
url: https://pypi.org/project/loghunt/
permissions:
id-token: write # Trusted Publishing and attestations
steps:
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: dist
path: dist/
- uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
51 changes: 45 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# loghunt

[![CI](https://github.com/winxtxrgit/loghunt/actions/workflows/ci.yml/badge.svg)](https://github.com/winxtxrgit/loghunt/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/loghunt?color=0e75b6)](https://pypi.org/project/loghunt/)
![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-0e75b6)
![No dependencies](https://img.shields.io/badge/dependencies-none-2ea44f)
[![MIT License](https://img.shields.io/badge/license-MIT-0e75b6)](LICENSE)
[![MIT License](https://img.shields.io/badge/license-MIT-0e75b6)](https://github.com/winxtxrgit/loghunt/blob/main/LICENSE)

**Hunt bots, scanners and botnets in nginx/apache access logs. Every flagged IP comes with the reasons it was flagged.**

Expand All @@ -12,7 +13,7 @@ secret-file probers, login brute-forcers, floods, and the hard one, **botnets th
brute-force across many IPs so each one stays under every threshold.**

<p align="center">
<img src="docs/terminal.png" alt="loghunt in a terminal: a table of suspicious IPs with a score, request count, peak per minute and the reason each was flagged, followed by a 19-IP campaign" width="820">
<img src="https://raw.githubusercontent.com/winxtxrgit/loghunt/main/docs/terminal.png" alt="loghunt in a terminal: a table of suspicious IPs with a score, request count, peak per minute and the reason each was flagged, followed by a 19-IP campaign" width="820">
</p>

## Why
Expand All @@ -25,7 +26,7 @@ explained**, so you can check it before you block anyone.
## Try it in 10 seconds

```bash
pipx install git+https://github.com/winxtxrgit/loghunt
pipx install loghunt # or: pip install loghunt
git clone https://github.com/winxtxrgit/loghunt && cd loghunt
loghunt samples/access.log.gz -o report.html
```
Expand All @@ -41,12 +42,20 @@ loghunt /var/log/nginx/access.log # summary in the terminal
loghunt access.log access.log.1 access.log.2.gz # rotated logs, gzip detected by content
loghunt access.log -o report.html # single-file HTML report
loghunt access.log --blocklist block.txt # suspicious IPs, one valid address per line
loghunt access.log --blocklist deny.conf --blocklist-format nginx # or apache, ipset, nftables
loghunt access.log --json - | jq '.suspects[0]' # machine-readable findings
zcat old/*.gz | loghunt - # read stdin
loghunt access.log --fail # exit 1 if anything is found (cron, CI)
loghunt access.log --min-score 70 # only the worst offenders
loghunt access.log --allow 198.51.100.0/24 # never flag your own monitors (or @allow.txt)
loghunt access.log --since "2026-10-20 08:00" --until "2026-10-20 09:00" # one incident window
```

`--since` is inclusive and `--until` is exclusive, so `--since 2026-10-20 --until 2026-10-21` is
the whole of 20 October. Times are UTC, like every loghunt output, unless you add an offset
(`2026-10-20T15:00+07:00`). Counts are per window: 30 login attempts split 15/15 across the
edge of the window are only 15 inside it.

Works with the nginx and apache **combined** format (the default on both) and the **common**
format. The common format has no User-Agent, so the two User-Agent rules and campaign grouping
are skipped there and everything else still runs.
Expand All @@ -67,6 +76,15 @@ weak signal alone is never enough.
| `script-agent` | 15 | No User-Agent, or an HTTP library (`python-requests`, `Go-http-client`, `curl`, …) |
| `campaign` | 40 | Member of a campaign (below) |

The defaults suit a small-to-medium site. Change them with `--burst`, `--flood`,
`--login-posts` and `--probes`; campaigns use the same login and probe numbers for the group
total. The HTML report and the JSON output both record the thresholds a run used.

```bash
loghunt access.log --burst 300 --flood 1500 # a busy shop or an API
loghunt access.log --login-posts 50 # WordPress with many real logins
```

### Campaigns: catching low-and-slow botnets

Per-IP rules miss an attacker who rents 19 machines and sends eight login attempts from each.
Expand All @@ -92,7 +110,7 @@ suspicious share highlighted, campaigns with all their member IPs, and a filtera
suspicious IP with its reasons. Light and dark themes follow your system.

<p align="center">
<img src="docs/report.png" alt="The HTML report: summary cards, a traffic-over-time chart with a highlighted flood, a 19-IP campaign, and the table of suspicious IPs" width="820">
<img src="https://raw.githubusercontent.com/winxtxrgit/loghunt/main/docs/report.png" alt="The HTML report: summary cards, a traffic-over-time chart with a highlighted flood, a 19-IP campaign, and the table of suspicious IPs" width="820">
</p>

**Safe to open.** Everything in an access log is written by whoever sent the request, including
Expand All @@ -114,10 +132,31 @@ One pass over the log, no dependencies outside the Python standard library.
Measured on an Apple M2 with Python 3.12. Memory grows with the number of distinct IPs, not the
size of the log. To reproduce: `python samples/generate.py big.log --visitors 60000`.

## Loading the blocklist

`--blocklist-format` writes a file the server or firewall loads as it is. Every format contains
only well-formed IP addresses, and an IPv4 client logged as `::ffff:203.0.113.9` is written as
`203.0.113.9`.

| Format | Load it with |
| :--- | :--- |
| `plain` (default) | anything that reads one IP per line |
| `nginx` | `include /etc/nginx/loghunt-deny.conf;` in `http`, `server` or `location`, then `nginx -s reload` |
| `apache` | `Include` it inside a `<Directory>` or `<Location>` (Apache 2.4 `Require` rules) |
| `ipset` | `ipset restore < file`, then once: `iptables -I INPUT -m set --match-set loghunt4 src -j DROP` (and `ip6tables` with `loghunt6`) |
| `nftables` | `nft -f file`: a `loghunt` table that drops both sets |

The `ipset` and `nftables` files replace their previous contents when you load them again, so a
cron job that reruns loghunt never piles up stale entries.

## Before you block anyone

- Put your own uptime checks, office NAT and scanners on the allowlist (`--allow IP`, `--allow
CIDR` or `--allow @file` with one entry per line). They still count toward traffic totals,
but are never flagged, grouped into a campaign or written to the blocklist.
- Thresholds suit a typical small-to-medium site. A WordPress site sees real `POST
/wp-login.php` traffic from its own users, so read the reasons before blocking.
/wp-login.php` traffic from its own users, so read the reasons before blocking, or raise
`--login-posts`.
- Several people behind one NAT or proxy share an IP. If your site sits behind a load balancer
or CDN, log the real client IP (nginx `real_ip_header`, apache `mod_remoteip`), or every
request will look like it came from the proxy.
Expand All @@ -141,4 +180,4 @@ NAT (RFC 5737, RFC 2544, RFC 6598), so no real address is labelled an attacker.

## License

[MIT](LICENSE) © Thanut Jongteerathanachote
[MIT](https://github.com/winxtxrgit/loghunt/blob/main/LICENSE) © Thanut Jongteerathanachote
52 changes: 52 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Roadmap

loghunt finds attackers in a log you already have. The next releases make it fit a real
server (your own monitoring, your own log format, your own firewall), then let it watch a log as
it grows. The rule stays the same in every release: **rules decide, and every decision is
explained.**

## 0.2.0: fit your server, and the first PyPI release

0.1.0 was never published, so 0.2.0 is the first version on PyPI.

- [x] Release workflow: a published GitHub release builds, checks and uploads to PyPI
- [ ] Add loghunt as a Trusted Publisher on PyPI (environment `pypi`, workflow `release.yml`)
- [ ] Publish GitHub release `v0.2.0`, which uploads to PyPI

Things every operator needs before trusting the output enough to block anyone.

- [x] `--allow IP|CIDR|@FILE`: IPs that are never reported or blocked (uptime checks, office NAT,
your own scanners). They still count toward traffic totals.
- [x] Threshold flags (`--login-posts`, `--burst`, `--flood`, `--probes`): a busy shop and a
personal blog need different numbers.
- [x] `--since` / `--until`: look at one incident window inside a long log.
- [x] `--blocklist-format plain|nginx|apache|ipset|nftables`: write something you can load
directly instead of hand-converting a list. (fail2ban was dropped: it has no file to
load, only commands to run, and nftables covers current Linux firewalls.)

## 0.3.0: read more logs

- [ ] `--format` taking an nginx `log_format` string, so custom formats (request time, upstream,
`$http_x_forwarded_for` in the middle) parse without regex editing.
- [ ] JSON-lines logs (nginx `escape=json`, Caddy, Traefik).
- [ ] Report the parse rate per file, and warn when most lines are skipped (wrong format).

## 0.4.0: catch smarter botnets

- [ ] Campaigns that rotate User-Agents: group by request set alone when the set is unusual
enough (attacker-only paths or login endpoints), not by User-Agent.
- [ ] Network view: many IPs in one /24 (IPv4) or /48 (IPv6) that together cross a threshold.
- [ ] Collapse a blocklist into CIDR ranges when a whole range is flagged.

## 0.5.0: watch live

- [ ] `--follow`: tail a growing log and print new suspects as they cross the threshold, with a
sliding window so memory stays bounded.
- [ ] Systemd unit and cron examples in the README.

## Not planned

- Blocking by itself. loghunt writes the list; you decide to load it.
- Reverse DNS or any network lookup during analysis. A log analysis should not contact the
attacker's infrastructure, and results must not depend on the network.
- Machine-learned scores. They cannot explain themselves.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "loghunt"
version = "0.1.0"
dynamic = ["version"]
description = "Hunt bots, scanners and botnets in nginx/apache access logs. Every flag comes with a reason."
readme = "README.md"
license = "MIT"
Expand Down Expand Up @@ -33,6 +33,9 @@ loghunt = "loghunt.cli:main"
Homepage = "https://github.com/winxtxrgit/loghunt"
Issues = "https://github.com/winxtxrgit/loghunt/issues"

[tool.hatch.version]
path = "src/loghunt/__init__.py"

[tool.hatch.build.targets.sdist]
include = ["src", "tests", "samples", "README.md", "LICENSE"]

Expand Down
2 changes: 1 addition & 1 deletion src/loghunt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Hunt bots, scanners and botnets in web server access logs."""

__version__ = "0.1.0"
__version__ = "0.2.0"
Loading
Loading