160 lines
4.8 KiB
Go
Executable File
160 lines
4.8 KiB
Go
Executable File
// Package netguard provides defense-in-depth network policy for the host
|
|
// agent: it never speaks UPnP/NAT-PMP and never asks a router to forward a
|
|
// port, and as a second line of defense it refuses TCP connections from
|
|
// source addresses outside private LAN / loopback ranges even if a user
|
|
// mistakenly forwards the port anyway.
|
|
package netguard
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// private/reserved LAN ranges plus loopback and link-local. This also
|
|
// covers the address space most VPN overlay networks use for peer
|
|
// addressing: 100.64.0.0/10 is the IANA-reserved "shared address space"
|
|
// (RFC 6598) that Tailscale and some WireGuard setups default to, and
|
|
// wg-quick's typical defaults (e.g. 10.x.x.x) already fall inside
|
|
// 10.0.0.0/8 below. Traffic in any of these ranges is never routable on
|
|
// the public internet on its own — it only reaches the host via a
|
|
// LAN or an already-authenticated, encrypted tunnel (like WireGuard).
|
|
var privateRanges = []netip.Prefix{
|
|
netip.MustParsePrefix("10.0.0.0/8"),
|
|
netip.MustParsePrefix("172.16.0.0/12"),
|
|
netip.MustParsePrefix("192.168.0.0/16"),
|
|
netip.MustParsePrefix("100.64.0.0/10"),
|
|
netip.MustParsePrefix("127.0.0.0/8"),
|
|
netip.MustParsePrefix("169.254.0.0/16"),
|
|
netip.MustParsePrefix("fc00::/7"),
|
|
netip.MustParsePrefix("fe80::/10"),
|
|
netip.MustParsePrefix("::1/128"),
|
|
}
|
|
|
|
var (
|
|
extraTrustedMu sync.RWMutex
|
|
extraTrusted []netip.Prefix
|
|
)
|
|
|
|
// AddTrustedCIDR extends the private-range allowlist with an additional
|
|
// CIDR, for VPN overlay subnets that don't fall within any standard
|
|
// private range above (a custom WireGuard AllowedIPs scheme, for
|
|
// example). Intended to be called a handful of times at startup, before
|
|
// the host starts accepting connections.
|
|
func AddTrustedCIDR(cidr string) error {
|
|
p, err := netip.ParsePrefix(cidr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
extraTrustedMu.Lock()
|
|
extraTrusted = append(extraTrusted, p)
|
|
extraTrustedMu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// IsPrivate reports whether addr is a loopback, private-LAN, or
|
|
// explicitly trusted (via AddTrustedCIDR) address.
|
|
func IsPrivate(addr netip.Addr) bool {
|
|
addr = addr.Unmap()
|
|
for _, p := range privateRanges {
|
|
if p.Contains(addr) {
|
|
return true
|
|
}
|
|
}
|
|
extraTrustedMu.RLock()
|
|
defer extraTrustedMu.RUnlock()
|
|
for _, p := range extraTrusted {
|
|
if p.Contains(addr) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
var allowAnySource atomic.Bool
|
|
|
|
// SetAllowAnySource disables the private-address source filter entirely
|
|
// when v is true, so IsAllowedRemote accepts a connection from anywhere.
|
|
// This exists only for short-lived connectivity testing over the public
|
|
// internet (e.g. proving a manual port-forward works before wiring up
|
|
// WireGuard) — it should never stay enabled on a host that keeps running,
|
|
// since it removes the one network-layer guard against an accidental
|
|
// internet exposure. Auth (passphrase + per-IP lockout) still applies.
|
|
func SetAllowAnySource(v bool) {
|
|
allowAnySource.Store(v)
|
|
}
|
|
|
|
// IsAllowedRemote parses a net.Addr (as returned by net.Conn.RemoteAddr)
|
|
// and reports whether it's a private/loopback address, or any address at
|
|
// all if SetAllowAnySource(true) was called.
|
|
func IsAllowedRemote(remote net.Addr) bool {
|
|
if allowAnySource.Load() {
|
|
return true
|
|
}
|
|
host, _, err := net.SplitHostPort(remote.String())
|
|
if err != nil {
|
|
host = remote.String()
|
|
}
|
|
addr, err := netip.ParseAddr(host)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return IsPrivate(addr)
|
|
}
|
|
|
|
// LoginThrottle tracks failed authentication attempts per source IP and
|
|
// enforces an increasing lockout, so a network-reachable attacker can't
|
|
// brute-force the passphrase at line rate.
|
|
type LoginThrottle struct {
|
|
mu sync.Mutex
|
|
failures map[string]*attemptState
|
|
}
|
|
|
|
type attemptState struct {
|
|
count int
|
|
lockedTill time.Time
|
|
}
|
|
|
|
func NewLoginThrottle() *LoginThrottle {
|
|
return &LoginThrottle{failures: map[string]*attemptState{}}
|
|
}
|
|
|
|
// Allowed reports whether ip is currently permitted to attempt a login.
|
|
func (lt *LoginThrottle) Allowed(ip string) bool {
|
|
lt.mu.Lock()
|
|
defer lt.mu.Unlock()
|
|
st, ok := lt.failures[ip]
|
|
if !ok {
|
|
return true
|
|
}
|
|
return time.Now().After(st.lockedTill)
|
|
}
|
|
|
|
// RecordFailure registers a failed attempt from ip and extends its lockout
|
|
// window exponentially (2s, 4s, 8s, ... capped at 5 minutes).
|
|
func (lt *LoginThrottle) RecordFailure(ip string) {
|
|
lt.mu.Lock()
|
|
defer lt.mu.Unlock()
|
|
st, ok := lt.failures[ip]
|
|
if !ok {
|
|
st = &attemptState{}
|
|
lt.failures[ip] = st
|
|
}
|
|
st.count++
|
|
backoff := time.Duration(1<<uint(min(st.count, 8))) * time.Second
|
|
const maxBackoff = 5 * time.Minute
|
|
if backoff > maxBackoff {
|
|
backoff = maxBackoff
|
|
}
|
|
st.lockedTill = time.Now().Add(backoff)
|
|
}
|
|
|
|
// RecordSuccess clears any failure history for ip.
|
|
func (lt *LoginThrottle) RecordSuccess(ip string) {
|
|
lt.mu.Lock()
|
|
defer lt.mu.Unlock()
|
|
delete(lt.failures, ip)
|
|
}
|