216 lines
7.3 KiB
Go
216 lines
7.3 KiB
Go
package enforce
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
)
|
|
|
|
func TestDecideSlowRules(t *testing.T) {
|
|
p := Params{Threshold: 5}
|
|
cases := []struct {
|
|
name string
|
|
in Input
|
|
act string
|
|
why string
|
|
}{
|
|
{"10 probes in window = 5 fails", Input{SSHProbes: 10}, ActBan, ReasonSSHBrute},
|
|
{"9 probes", Input{SSHProbes: 9}, ActNone, ""},
|
|
{"fails and probes mix", Input{SSHFails: 3, SSHProbes: 4}, ActBan, ReasonSSHBrute},
|
|
{"ssh 7 days", Input{SSH7: 14}, ActBan, ReasonSSHBrute},
|
|
{"ssh 7 days below", Input{SSH7: 9}, ActNone, ""},
|
|
{"23.132.164.185 profile", Input{SSH7: 18, Probe7: 10}, ActBan, ReasonSSHBrute},
|
|
{"77.239.124.130 profile", Input{Probe7: 28}, ActBan, ReasonSSHBrute},
|
|
{"probes 24h", Input{Probe24: 20}, ActBan, ReasonSSHBrute},
|
|
{"web 7 days", Input{Web7: 10}, ActBan, ReasonScannerFlood},
|
|
{"web 7 days below", Input{Web7: 9}, ActNone, ""},
|
|
{"web 7 days with a legit visit", Input{Web7: 30, Legit: true}, ActNone, ""},
|
|
{"neighbor", Input{Neighbor: true}, ActBan, ReasonNeighbor},
|
|
{"neighbor legit", Input{Neighbor: true, Legit: true}, ActNone, ""},
|
|
{"neighbor trusted", Input{Neighbor: true, Exclude: SkipTrusted}, ActSkip, ""},
|
|
{"neighbor crawler", Input{Neighbor: true, Exclude: SkipCrawler}, ActSkip, ""},
|
|
{"neighbor operator", Input{Neighbor: true, Exclude: SkipOperator}, ActSkip, ""},
|
|
{"neighbor already banned", Input{Neighbor: true, Banned: true}, ActAlready, ""},
|
|
}
|
|
for _, c := range cases {
|
|
d := Decide(c.in, p)
|
|
if d.Action != c.act || (c.why != "" && d.Reason != c.why) {
|
|
t.Errorf("%s: %+v", c.name, d)
|
|
}
|
|
}
|
|
if !ValidReason(ReasonNeighbor) || ReasonNeighbor != "neighbor-campaign" {
|
|
t.Fatal("slug")
|
|
}
|
|
}
|
|
|
|
func probeEv(ip string, at time.Time) core.Event {
|
|
return core.Event{Time: at, IP: ip, Cat: core.SSHProbe, Source: "ssh"}
|
|
}
|
|
|
|
func TestSSHProbesBanLive(t *testing.T) {
|
|
r := histRig(t, nil)
|
|
for i := 0; i < 9; i++ {
|
|
r.e.handle(probeEv("203.0.114.70", r.now.Add(-time.Duration(i)*time.Second)), "")
|
|
}
|
|
if len(r.lines(t)) != 0 {
|
|
t.Fatal("9 probes must not ban")
|
|
}
|
|
r.e.handle(probeEv("203.0.114.70", r.now), "")
|
|
if l := r.lines(t); len(l) != 1 || !contains(l[0], "ssh-bruteforce") {
|
|
t.Fatalf("%v", l)
|
|
}
|
|
}
|
|
|
|
func TestSlowWeekLive(t *testing.T) {
|
|
r := histRig(t, func(c *Config) {
|
|
c.Tally = func(string, int64) Tally { return Tally{SSH: 2, SSH7: 13} } // 144.22.238.238: 14 in 7 days
|
|
})
|
|
r.e.handle(sshEv("203.0.114.71", r.now), "")
|
|
if l := r.lines(t); len(l) != 1 || !contains(l[0], "ssh-bruteforce") {
|
|
t.Fatalf("%v", l)
|
|
}
|
|
}
|
|
|
|
func TestSweepWeekAndNeighbors(t *testing.T) {
|
|
r := histRig(t, func(c *Config) {
|
|
c.Roster = func(from int64) []Seen {
|
|
if from > r0(t0).Add(-6*24*time.Hour).Unix() {
|
|
t.Errorf("sweep must look back 7 days, from=%d", from)
|
|
}
|
|
return []Seen{
|
|
{IP: "203.0.114.80", Score: 20, Level: core.LevelSuspicious, Events: 14, Last: t0.Add(-2 * 24 * time.Hour).Unix()},
|
|
{IP: "203.0.114.81", Score: 20, Level: core.LevelSuspicious, Events: 3, Last: t0.Add(-2 * 24 * time.Hour).Unix()},
|
|
}
|
|
}
|
|
c.Tallies = func(int64) map[string]Tally {
|
|
return map[string]Tally{"203.0.114.80": {SSH7: 14}, "203.0.114.81": {SSH7: 3}}
|
|
}
|
|
})
|
|
r.e.Sweep()
|
|
r.e.drain()
|
|
if l := r.lines(t); len(l) != 1 || !contains(l[0], "203.0.114.80") {
|
|
t.Fatalf("%v", l)
|
|
}
|
|
}
|
|
|
|
func r0(t time.Time) time.Time { return t }
|
|
|
|
func ban(ip string, at time.Time, jail string) core.Event {
|
|
return core.Event{Time: at, IP: ip, Cat: core.Ban, Source: "fail2ban", Jail: jail}
|
|
}
|
|
|
|
func TestNeighborCampaign(t *testing.T) {
|
|
r := histRig(t, nil)
|
|
for _, ip := range []string{"203.0.114.1", "203.0.114.2"} {
|
|
r.e.process(ban(ip, r.now.Add(-time.Hour), "sshd"), "")
|
|
}
|
|
r.e.process(atk("203.0.114.9", r.now, "/x", 404), "")
|
|
if len(r.lines(t)) != 0 {
|
|
t.Fatal("two banned neighbors are not a campaign")
|
|
}
|
|
r.e.process(ban("203.0.114.3", r.now.Add(-time.Hour), "sshd"), "")
|
|
r.e.process(atk("203.0.114.9", r.now, "/x", 404), "")
|
|
l := r.lines(t)
|
|
if len(l) != 1 || !contains(l[0], "ip=203.0.114.9") || !contains(l[0], "neighbor-campaign") {
|
|
t.Fatalf("%v", l)
|
|
}
|
|
// another /24 is untouched
|
|
r.e.process(atk("203.0.115.9", r.now, "/x", 404), "")
|
|
if len(r.lines(t)) != 1 {
|
|
t.Fatal("other prefix banned")
|
|
}
|
|
// bans older than 24 h expire
|
|
r.now = r.now.Add(25 * time.Hour)
|
|
r.e.process(atk("203.0.114.77", r.now, "/x", 404), "")
|
|
if len(r.lines(t)) != 1 {
|
|
t.Fatal("expired campaign banned")
|
|
}
|
|
}
|
|
|
|
func TestNeighborRespectsExclusions(t *testing.T) {
|
|
r := histRig(t, func(c *Config) { c.Crawler = func(ip string, _ []string) bool { return ip == "203.0.114.9" } })
|
|
for _, ip := range []string{"203.0.114.1", "203.0.114.2", "203.0.114.3"} {
|
|
r.e.process(ban(ip, r.now.Add(-time.Hour), "sshd"), "")
|
|
}
|
|
r.e.process(atk("203.0.114.9", r.now, "/x", 404), "") // verified crawler, no UA at all
|
|
if len(r.lines(t)) != 0 {
|
|
t.Fatalf("crawler banned: %v", r.lines(t))
|
|
}
|
|
r.e.process(atk("203.0.114.10", r.now, "/x", 404), "")
|
|
if len(r.lines(t)) != 1 {
|
|
t.Fatal("expected one ban")
|
|
}
|
|
// operator allowance
|
|
if err := r.e.Unban(mustAddr("203.0.114.11"), 3600, "t"); err != nil && err != ErrNoUnbanLog {
|
|
t.Fatal(err)
|
|
}
|
|
// trusted prefix inside the campaign
|
|
r.e.process(ban("168.196.85.1", r.now, "sshd"), "")
|
|
r.e.process(ban("168.196.85.2", r.now, "sshd"), "")
|
|
r.e.process(ban("168.196.85.3", r.now, "sshd"), "")
|
|
r.e.process(atk("168.196.85.9", r.now, "/x", 404), "")
|
|
for _, l := range r.lines(t) {
|
|
if contains(l, "168.196.85.9") {
|
|
t.Fatal("trusted banned")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNeighborV6AndPeers(t *testing.T) {
|
|
r := histRig(t, nil)
|
|
for _, ip := range []string{"2606:4701:1:2::1", "2606:4701:1:2::2", "2606:4701:1:2::3"} {
|
|
r.e.process(ban(ip, r.now.Add(-time.Hour), "sshd"), "")
|
|
}
|
|
r.e.process(atk("2606:4701:1:2::99", r.now, "/x", 404), "")
|
|
if l := r.lines(t); len(l) != 1 || !contains(l[0], "neighbor-campaign") {
|
|
t.Fatalf("%v", l)
|
|
}
|
|
// a peer may send the slug; this node must have seen the address attack
|
|
r.e.process(atk("203.0.114.200", r.now, "/x", 404), "")
|
|
acc, inv := r.e.AcceptPeer("hub", []Wire{{T: r.now.Unix(), IP: "203.0.114.200", Reason: ReasonNeighbor, Score: 70, TTL: 3600}})
|
|
if acc != 1 || inv != 0 {
|
|
t.Fatalf("accepted %d invalid %d", acc, inv)
|
|
}
|
|
}
|
|
|
|
func TestOwnJailBanDoesNotChangeAttackLogic(t *testing.T) {
|
|
r := histRig(t, nil)
|
|
r.e.process(ban("203.0.114.5", r.now, "urutau"), "")
|
|
if st := r.e.ips[mustAddr("203.0.114.5")]; st == nil || st.f2bAt != 0 || st.urutauAt == 0 {
|
|
t.Fatalf("%+v", st)
|
|
}
|
|
}
|
|
|
|
func TestCrawlerExcludedWithoutUA(t *testing.T) {
|
|
r := histRig(t, func(c *Config) { c.Crawler = func(string, []string) bool { return true } })
|
|
e := atk("203.0.114.40", r.now, "/.env", 404)
|
|
e.UA = ""
|
|
for i := 0; i < 10; i++ {
|
|
e.Time = r.now.Add(-time.Duration(i) * time.Second)
|
|
r.e.process(e, "env-credentials")
|
|
}
|
|
if len(r.lines(t)) != 0 || r.e.counts.Skipped[SkipCrawler] == 0 {
|
|
t.Fatalf("%v %v", r.lines(t), r.e.counts.Skipped)
|
|
}
|
|
}
|
|
|
|
func TestCoverageOneShot(t *testing.T) {
|
|
r := histRig(t, func(c *Config) {
|
|
c.Roster = func(int64) []Seen {
|
|
return []Seen{
|
|
{IP: "203.0.114.90", Events: 1},
|
|
{IP: "203.0.114.91", Events: 5},
|
|
{IP: "203.0.114.92", Events: 5, Jails: []string{"sshd"}},
|
|
}
|
|
}
|
|
})
|
|
cv := r.e.Report(0).Coverage
|
|
if cv.AttackIPs != 3 || cv.OneShot != 1 || cv.Repeat != 2 || cv.RepeatUnblocked != 1 || cv.Unblocked != 2 {
|
|
t.Fatalf("%+v", cv)
|
|
}
|
|
}
|
|
|
|
func mustAddr(s string) netip.Addr { return netip.MustParseAddr(s) }
|