Replace the tree with upstream TecharoHQ/anubis v1.28.2 (a1384fbf42e3). Masyunis customizations are re-applied in the following commits.
286 lines
7.1 KiB
Go
286 lines
7.1 KiB
Go
package naive
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
_ "embed"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"math"
|
|
"math/rand/v2"
|
|
"net/http"
|
|
"net/netip"
|
|
"os"
|
|
"slices"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/TecharoHQ/anubis/internal"
|
|
"github.com/TecharoHQ/anubis/internal/bundler"
|
|
"github.com/TecharoHQ/anubis/internal/honeypot"
|
|
"github.com/TecharoHQ/anubis/lib/config"
|
|
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
|
"github.com/TecharoHQ/anubis/lib/store"
|
|
"github.com/a-h/templ"
|
|
"github.com/google/uuid"
|
|
"github.com/nikandfor/spintax"
|
|
)
|
|
|
|
//go:generate go tool github.com/a-h/templ/cmd/templ generate
|
|
|
|
// XXX(Xe): All of this was generated by ChatGPT, GLM 4.6, and GPT-OSS 120b. This is pseudoprofound bullshit in spintax[1] format so that the bullshit generator can emit plausibly human-authored text while being very computationally cheap.
|
|
//
|
|
// It feels somewhat poetic to use spammer technology in Anubis.
|
|
//
|
|
// [1]: https://outboundly.ai/blogs/what-is-spintax-and-how-to-use-it/
|
|
//
|
|
//go:embed spintext.txt
|
|
var spintext string
|
|
|
|
//go:embed titles.txt
|
|
var titles string
|
|
|
|
//go:embed affirmations.txt
|
|
var affirmations string
|
|
|
|
func New(cfg *config.Honeypot, st store.Interface, lg *slog.Logger) (*Impl, error) {
|
|
affirmation, err := spintax.Parse(affirmations)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("can't parse affirmations: %w", err)
|
|
}
|
|
|
|
body, err := spintax.Parse(spintext)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("can't parse bodies: %w", err)
|
|
}
|
|
|
|
title, err := spintax.Parse(titles)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("can't parse titles: %w", err)
|
|
}
|
|
|
|
var fout *os.File
|
|
var foutBundler *bundler.Bundler[string]
|
|
|
|
if cfg.IPLogFile != "" {
|
|
lg.InfoContext(context.Background(), "logging honeypot IP addresses", "foutName", cfg.IPLogFile)
|
|
|
|
fout, err = os.OpenFile(cfg.IPLogFile, os.O_CREATE|os.O_RDWR, 0600)
|
|
if err == nil {
|
|
err = fout.Chmod(0600)
|
|
if err != nil {
|
|
_ = fout.Close()
|
|
}
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("can't open ip log file %q: %w", cfg.IPLogFile, err)
|
|
}
|
|
|
|
if _, err := fout.Seek(0, 0); err != nil {
|
|
return nil, fmt.Errorf("can't rewind ip log file %q: %w", cfg.IPLogFile, err)
|
|
}
|
|
|
|
if err := fout.Truncate(0); err != nil {
|
|
return nil, fmt.Errorf("can't truncate ip log file %q: %w", cfg.IPLogFile, err)
|
|
}
|
|
|
|
lg := lg.With("in", "foutBundler")
|
|
|
|
foutBundler = bundler.New(func(ctx context.Context, ips []string) {
|
|
st, err := fout.Stat()
|
|
if err != nil {
|
|
lg.ErrorContext(ctx, "can't stat ip log file", "foutName", cfg.IPLogFile, "err", err)
|
|
return
|
|
}
|
|
|
|
if st.Size() >= 65536 {
|
|
if _, err := fout.Seek(0, 0); err != nil {
|
|
lg.ErrorContext(ctx, "can't rewind ip log file", "foutName", cfg.IPLogFile, "err", err)
|
|
return
|
|
}
|
|
|
|
if err := fout.Truncate(0); err != nil {
|
|
lg.ErrorContext(ctx, "can't truncate ip log file", "foutName", cfg.IPLogFile, "err", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
sort.Strings(ips)
|
|
ips = slices.Compact(ips)
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
for _, ip := range ips {
|
|
fmt.Fprintln(buf, ip)
|
|
}
|
|
|
|
if _, err := io.Copy(fout, buf); err != nil {
|
|
lg.ErrorContext(ctx, "can't write buffered IP addresses to output file", "foutName", cfg.IPLogFile, "err", err)
|
|
return
|
|
}
|
|
|
|
if err := fout.Sync(); err != nil {
|
|
lg.ErrorContext(ctx, "can't sync output file", "foutName", cfg.IPLogFile, "err", err)
|
|
}
|
|
})
|
|
|
|
foutBundler.BundleByteLimit = 32768
|
|
foutBundler.DelayThreshold = time.Minute
|
|
}
|
|
|
|
lg.Debug("initialized basic bullshit generator", "affirmations", affirmation.Count(), "bodies", body.Count(), "titles", title.Count())
|
|
|
|
return &Impl{
|
|
st: st,
|
|
infos: store.JSON[honeypot.Info]{Underlying: st, Prefix: "honeypot:info"},
|
|
uaWeight: store.JSON[int]{Underlying: st, Prefix: "honeypot:user-agent"},
|
|
networkWeight: store.JSON[int]{Underlying: st, Prefix: "honeypot:network"},
|
|
affirmation: affirmation,
|
|
body: body,
|
|
title: title,
|
|
lg: lg.With("component", "honeypot/naive"),
|
|
fout: fout,
|
|
foutBundler: foutBundler,
|
|
}, nil
|
|
}
|
|
|
|
type Impl struct {
|
|
st store.Interface
|
|
infos store.JSON[honeypot.Info]
|
|
uaWeight store.JSON[int]
|
|
networkWeight store.JSON[int]
|
|
lg *slog.Logger
|
|
fout *os.File
|
|
foutBundler *bundler.Bundler[string]
|
|
|
|
affirmation, body, title spintax.Spintax
|
|
}
|
|
|
|
func (i *Impl) incrementNetwork(ctx context.Context, network string) int {
|
|
key := internal.SHA256sum(network)
|
|
result, _ := i.networkWeight.Get(ctx, key)
|
|
result++
|
|
_ = i.networkWeight.Set(ctx, key, result, time.Hour)
|
|
return result
|
|
}
|
|
|
|
func (i *Impl) CheckNetwork() checker.Impl {
|
|
return checker.Func(func(r *http.Request) (bool, error) {
|
|
realIP, _ := internal.RealIP(r)
|
|
if !realIP.IsValid() {
|
|
realIP, _ = netip.ParseAddr(r.Header.Get("X-Real-IP"))
|
|
}
|
|
|
|
network, ok := internal.ClampIP(realIP)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
|
|
result, _ := i.networkWeight.Get(r.Context(), internal.SHA256sum(network.String()))
|
|
if result >= 25 {
|
|
return true, nil
|
|
}
|
|
|
|
return false, nil
|
|
})
|
|
}
|
|
|
|
func (i *Impl) Hash() string {
|
|
return internal.SHA256sum("naive honeypot")
|
|
}
|
|
|
|
func (i *Impl) makeAffirmations() []string {
|
|
count := rand.IntN(5) + 1
|
|
|
|
var result []string
|
|
for range count {
|
|
result = append(result, i.affirmation.Spin())
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (i *Impl) makeSpins() []string {
|
|
count := rand.IntN(5) + 1
|
|
|
|
var result []string
|
|
for range count {
|
|
result = append(result, i.body.Spin())
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (i *Impl) makeTitle() string {
|
|
return i.title.Spin()
|
|
}
|
|
|
|
func (i *Impl) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
t0 := time.Now()
|
|
lg := internal.GetRequestLogger(i.lg, r)
|
|
|
|
id := r.PathValue("id")
|
|
if id == "" {
|
|
id = uuid.NewString()
|
|
}
|
|
|
|
realIP, _ := internal.RealIP(r)
|
|
if !realIP.IsValid() {
|
|
realIP, _ = netip.ParseAddr(r.Header.Get("X-Real-IP"))
|
|
}
|
|
|
|
if i.foutBundler != nil {
|
|
rip := realIP.String()
|
|
_ = i.foutBundler.Add(rip, len(rip)+1) // for the newline
|
|
}
|
|
|
|
network, ok := internal.ClampIP(realIP)
|
|
if !ok {
|
|
lg.ErrorContext(r.Context(), "clampIP failed", "output", network, "ok", ok)
|
|
http.Error(w, "The cake is a lie", http.StatusTeapot)
|
|
return
|
|
}
|
|
|
|
networkCount := i.incrementNetwork(r.Context(), network.String())
|
|
|
|
stage := r.PathValue("stage")
|
|
|
|
if stage == "init" {
|
|
lg.DebugContext(r.Context(), "found new entrance point", "id", id, "stage", stage, "userAgent", r.UserAgent(), "clampedIP", network)
|
|
} else {
|
|
switch networkCount % 256 {
|
|
case 0:
|
|
lg.WarnContext(r.Context(), "found possible crawler", "id", id, "network", network, "userAgent", r.UserAgent())
|
|
}
|
|
}
|
|
|
|
millisecondAmount := min(math.Pow(float64(networkCount), 2), 1000)
|
|
time.Sleep(time.Duration(millisecondAmount) * time.Millisecond)
|
|
|
|
spins := i.makeSpins()
|
|
affirmations := i.makeAffirmations()
|
|
title := i.makeTitle()
|
|
|
|
var links []link
|
|
for _, affirmation := range affirmations {
|
|
links = append(links, link{
|
|
href: uuid.NewString(),
|
|
body: affirmation,
|
|
})
|
|
}
|
|
|
|
templ.Handler(
|
|
base(title, i.maze(spins, links)),
|
|
templ.WithStreaming(),
|
|
templ.WithStatus(http.StatusOK),
|
|
).ServeHTTP(w, r)
|
|
|
|
t1 := time.Since(t0)
|
|
honeypot.Timings.WithLabelValues("naive").Observe(float64(t1.Milliseconds()))
|
|
}
|
|
|
|
type link struct {
|
|
href string
|
|
body string
|
|
}
|