mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-04 04:31:51 +08:00
- Implement full-traffic transparent gateway (cmd/artex-gateway, traffic/netstack.go, traffic/transparent.go) using Linux TUN + gVisor netstack deferred TCP handshakes (eliminating nmap/nc false-positive open ports), real ICMPv4 Echo proxying, UDP/DNS interception, and per-container upstream proxy overrides. - Add egress blacklist + asset-intercept sync, non-HTTP flow/DNS SQLite persistence and APIs, and /system/proxy & /function/traffic TCP/UDP/DNS UI panels. - Fix sandbox Playwright MCP browser loopback --proxy-server rewrite and NODE_EXTRA_CA_CERTS override in server/mcpstdio.go. - Strip all SUID/SGID binaries in Dockerfile.sandbox, grant minimal cap_net_raw+ep to nmap/nping/ping with ARTEX_SANDBOX_ALLOW_NET_RAW toggle, and enforce IPv4/IPv6 anti-spoofing in iptables and netstack. - Remove hardcoded Chinese "(全局)" from GET /api/proxy/routes, localize ContainerRoutesTab / NonHTTPTrafficPanel labels, and expose reactive locale/isEn from useLiveTranslation for React Compiler compatibility.
287 lines
7.6 KiB
Go
287 lines
7.6 KiB
Go
package traffic
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// SSRFGuardConfig controls which destinations the recording proxy refuses to dial.
|
|
// Loopback, link-local / cloud metadata, and ARTEX's own internal services are
|
|
// always blocked when the guard is enabled. Private RFC1918 networks are allowed
|
|
// by default so authorized internal pentests keep working, and can be blocked by
|
|
// setting BlockPrivate=true.
|
|
type SSRFGuardConfig struct {
|
|
Enabled bool
|
|
BlockPrivate bool
|
|
BlockedHosts []string
|
|
BlockedCIDRs []*net.IPNet
|
|
// InfraIPs are machines that also host ordinary scan targets (for example the
|
|
// ARTEX host LAN address, where :18080 may be in scope). Every port on those
|
|
// addresses stays reachable except AdminPorts, which would reach this process.
|
|
InfraIPs []net.IP
|
|
AdminPorts []int
|
|
// HostNetworkMode should be set when the guard runs in the Docker host's
|
|
// network namespace (artex-gateway): local host interface IPs are protected
|
|
// on AdminPorts (via InfraIPs) rather than blocked on every port, and the
|
|
// host's LAN router is not treated as a Docker bridge hairpin address.
|
|
HostNetworkMode bool
|
|
}
|
|
type ssrfGuard struct {
|
|
mu sync.RWMutex
|
|
enabled bool
|
|
blockPrivate bool
|
|
blockedHosts map[string]struct{}
|
|
blockedIPs map[string]struct{}
|
|
blockedCIDRs []*net.IPNet
|
|
infraIPs []net.IP
|
|
adminPorts map[string]struct{}
|
|
}
|
|
|
|
var (
|
|
alwaysBlockedCIDRs = mustParseCIDRs(
|
|
"0.0.0.0/8",
|
|
"127.0.0.0/8",
|
|
"169.254.0.0/16",
|
|
"::1/128",
|
|
"::/128",
|
|
"fe80::/10",
|
|
"ff00::/8",
|
|
)
|
|
privateCIDRs = mustParseCIDRs(
|
|
"10.0.0.0/8",
|
|
"172.16.0.0/12",
|
|
"192.168.0.0/16",
|
|
"fc00::/7",
|
|
)
|
|
alwaysBlockedHostnames = map[string]struct{}{
|
|
"localhost": {},
|
|
"metadata.google.internal": {},
|
|
"metadata.goog": {},
|
|
"host.docker.internal": {},
|
|
"gateway.docker.internal": {},
|
|
}
|
|
)
|
|
|
|
func mustParseCIDRs(raw ...string) []*net.IPNet {
|
|
out := make([]*net.IPNet, 0, len(raw))
|
|
for _, s := range raw {
|
|
_, ipnet, err := net.ParseCIDR(s)
|
|
if err == nil {
|
|
out = append(out, ipnet)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func newSSRFGuard(cfg SSRFGuardConfig) *ssrfGuard {
|
|
g := &ssrfGuard{
|
|
enabled: cfg.Enabled,
|
|
blockPrivate: cfg.BlockPrivate,
|
|
blockedHosts: make(map[string]struct{}),
|
|
blockedIPs: make(map[string]struct{}),
|
|
blockedCIDRs: append([]*net.IPNet(nil), cfg.BlockedCIDRs...),
|
|
adminPorts: map[string]struct{}{"8787": {}, "8788": {}},
|
|
}
|
|
for _, p := range cfg.AdminPorts {
|
|
if p > 0 && p < 65536 {
|
|
g.adminPorts[strconv.Itoa(p)] = struct{}{}
|
|
}
|
|
}
|
|
for _, ip := range cfg.InfraIPs {
|
|
if ip = normalizeIP(ip); ip != nil {
|
|
g.infraIPs = append(g.infraIPs, ip)
|
|
}
|
|
}
|
|
for _, h := range cfg.BlockedHosts {
|
|
h = strings.ToLower(strings.TrimSpace(h))
|
|
if h == "" {
|
|
continue
|
|
}
|
|
if ip := net.ParseIP(h); ip != nil {
|
|
g.blockedIPs[ip.String()] = struct{}{}
|
|
continue
|
|
}
|
|
g.blockedHosts[h] = struct{}{}
|
|
if ips, err := net.LookupIP(h); err == nil {
|
|
for _, ip := range ips {
|
|
g.blockedIPs[ip.String()] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
infraSet := make(map[string]struct{}, len(g.infraIPs))
|
|
for _, ip := range g.infraIPs {
|
|
infraSet[ip.String()] = struct{}{}
|
|
}
|
|
// Protect local interface IPs. Inside the ARTEX container, block all ports on
|
|
// container IPs (unless explicitly listed in InfraIPs). In host network mode
|
|
// (artex-gateway), protect all host interface IPs on AdminPorts via infraIPs
|
|
// so ARTEX :8787/:8788 stay unreachable while other host LAN ports remain scannable.
|
|
if addrs, err := net.InterfaceAddrs(); err == nil {
|
|
for _, a := range addrs {
|
|
ipnet, ok := a.(*net.IPNet)
|
|
if !ok || ipnet.IP == nil {
|
|
continue
|
|
}
|
|
ip := normalizeIP(ipnet.IP)
|
|
if ip == nil {
|
|
continue
|
|
}
|
|
if cfg.HostNetworkMode {
|
|
if _, seen := infraSet[ip.String()]; !seen {
|
|
g.infraIPs = append(g.infraIPs, ip)
|
|
infraSet[ip.String()] = struct{}{}
|
|
}
|
|
continue
|
|
}
|
|
if _, isInfra := infraSet[ip.String()]; isInfra {
|
|
continue
|
|
}
|
|
g.blockedIPs[ip.String()] = struct{}{}
|
|
}
|
|
}
|
|
// In container mode, the default gateway is the Docker host bridge; blocking it
|
|
// stops hairpinning onto published host ports. In host network mode, the default
|
|
// gateway is the LAN router and must not be blocked on all ports.
|
|
if !cfg.HostNetworkMode {
|
|
if gw := linuxDefaultGatewayIPv4(); gw != nil {
|
|
if _, isInfra := infraSet[gw.String()]; !isInfra {
|
|
g.blockedIPs[gw.String()] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
return g
|
|
}
|
|
|
|
func normalizeIP(ip net.IP) net.IP {
|
|
if ip == nil {
|
|
return nil
|
|
}
|
|
if v4 := ip.To4(); v4 != nil {
|
|
return v4
|
|
}
|
|
return ip
|
|
}
|
|
|
|
// linuxDefaultGatewayIPv4 reads the IPv4 default route. Missing on non-Linux
|
|
// and in unit tests that do not mount /proc; callers treat nil as "unknown".
|
|
func linuxDefaultGatewayIPv4() net.IP {
|
|
data, err := os.ReadFile("/proc/net/route")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
lines := strings.Split(string(data), "\n")
|
|
for _, line := range lines[1:] {
|
|
fields := strings.Fields(line)
|
|
if len(fields) < 3 || fields[1] != "00000000" {
|
|
continue
|
|
}
|
|
gw, err := strconv.ParseUint(fields[2], 16, 32)
|
|
if err != nil || gw == 0 {
|
|
continue
|
|
}
|
|
return net.IPv4(byte(gw), byte(gw>>8), byte(gw>>16), byte(gw>>24)).To4()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Check returns a non-empty denial reason if rawHost (host or host:port) is blocked.
|
|
func (g *ssrfGuard) Check(rawHost string) string {
|
|
if g == nil {
|
|
return ""
|
|
}
|
|
g.mu.RLock()
|
|
defer g.mu.RUnlock()
|
|
if !g.enabled {
|
|
return ""
|
|
}
|
|
host, port := splitHostAndPort(rawHost)
|
|
host = strings.ToLower(strings.TrimSpace(host))
|
|
host = strings.TrimPrefix(host, "[")
|
|
host = strings.TrimSuffix(host, "]")
|
|
if host == "" {
|
|
return "empty target host"
|
|
}
|
|
if _, ok := alwaysBlockedHostnames[host]; ok || strings.HasSuffix(host, ".localhost") {
|
|
return fmt.Sprintf("internal host %q is blocked", host)
|
|
}
|
|
if _, ok := g.blockedHosts[host]; ok {
|
|
return fmt.Sprintf("protected service host %q is blocked", host)
|
|
}
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
if reason := g.checkIP(ip); reason != "" {
|
|
return reason
|
|
}
|
|
return g.checkInfraPort(ip, port)
|
|
}
|
|
// Resolve hostname to guard against DNS rebinding / internal Docker DNS names.
|
|
ips, err := net.LookupIP(host)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
for _, ip := range ips {
|
|
if reason := g.checkIP(ip); reason != "" {
|
|
return fmt.Sprintf("%s (resolved from %s)", reason, host)
|
|
}
|
|
if reason := g.checkInfraPort(ip, port); reason != "" {
|
|
return reason
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func splitHostAndPort(raw string) (string, string) {
|
|
raw = strings.TrimSpace(raw)
|
|
if h, p, err := net.SplitHostPort(raw); err == nil {
|
|
return h, p
|
|
}
|
|
return raw, ""
|
|
}
|
|
|
|
func (g *ssrfGuard) checkInfraPort(ip net.IP, port string) string {
|
|
if port == "" || len(g.infraIPs) == 0 {
|
|
return ""
|
|
}
|
|
if _, ok := g.adminPorts[port]; !ok {
|
|
return ""
|
|
}
|
|
ip = normalizeIP(ip)
|
|
for _, infra := range g.infraIPs {
|
|
if infra.Equal(ip) {
|
|
return fmt.Sprintf("ARTEX admin port %s on %s is blocked", port, ip.String())
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (g *ssrfGuard) checkIP(ip net.IP) string {
|
|
if ip == nil {
|
|
return ""
|
|
}
|
|
ip = normalizeIP(ip)
|
|
if _, ok := g.blockedIPs[ip.String()]; ok {
|
|
return fmt.Sprintf("protected host IP %s is blocked", ip.String())
|
|
}
|
|
for _, n := range alwaysBlockedCIDRs {
|
|
if n.Contains(ip) {
|
|
return fmt.Sprintf("restricted address %s (%s) is blocked", ip.String(), n.String())
|
|
}
|
|
}
|
|
for _, n := range g.blockedCIDRs {
|
|
if n.Contains(ip) {
|
|
return fmt.Sprintf("sandbox/internal network address %s (%s) is blocked", ip.String(), n.String())
|
|
}
|
|
}
|
|
if g.blockPrivate {
|
|
for _, n := range privateCIDRs {
|
|
if n.Contains(ip) {
|
|
return fmt.Sprintf("private network address %s (%s) is blocked", ip.String(), n.String())
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|