fix: hardening

This commit is contained in:
FUFSoB committed 2026-10-05 22:44:09 +05:00
1 parent 017a947a18
commit a435942b4f
168 files changed
+9166 -1644

No files matched your search

+116 -40
View File
@@ -9,7 +9,8 @@
proxyInboundsSelfSources,
proxyInboundsRuntimeEnabled,
proxyInboundsRuntimeVias,
proxyInboundRuntimePorts,
proxyInboundsHostPorts,
proxyInboundsLoopback,
zapretDirectRules,
}:
@@ -34,17 +35,24 @@ let
# A blocked listener stays blocked: neither serverAddress nor the proxy exceptions open it.
exceptionInboundTags = map (ib: ib.tag) (builtins.filter (ib: ib.via != "block") proxyInbounds);
# Only Tor reaches .onion: to the local proxy, whose own rule sends it there. First, so no
# IP rule before it has XRay look the name up, which would leak it to a resolver.
onionRule = lib.optional proxyInboundsRouteOnion (forException {
type = "field";
ruleTag = "inbound-tor-onion";
domain = [ "domain:onion" ];
inboundTag = map (ib: ib.tag) (
builtins.filter (ib: ib.via != "block" && ib.listener.type != "amneziawg") proxyInbounds
);
outboundTag = "proxy";
});
# .onion goes to the local proxy, which hands it to Tor; first, so no IP rule looks it up.
# An address with an .onion SNI keeps its listener's via (mkSniffGuard).
onionListeners = builtins.filter (
ib: ib.via != "block" && ib.listener.type != "amneziawg"
) proxyInbounds;
onionRule =
lib.optionals (proxyInboundsRouteOnion && proxyInboundsResolveInSingBox) (
mkSniffGuard "inbound-tor-onion-sniffed" [ "domain:onion" ] onionListeners runtimeOpenVias { }
)
++ lib.optional proxyInboundsRouteOnion (forException {
type = "field";
ruleTag = "inbound-tor-onion";
domain = [ "domain:onion" ];
inboundTag = map (ib: ib.tag) (
builtins.filter (ib: ib.via != "block" && ib.listener.type != "amneziawg") proxyInbounds
);
outboundTag = "proxy";
});
blockPrivateRule = lib.optional proxyInboundsCfg.routing.blockPrivate {
type = "field";
@@ -70,30 +78,37 @@ let
# Only the ports clients dial there, and the ones listed as public: the rest of this host
# (wildcard services the firewall keeps from the internet) must not be reachable through it.
serverAddressPorts = lib.unique (
map (ib: if ib.listener.sharePort != null then ib.listener.sharePort else ib.listener.port) (
# AmneziaWG is UDP to the interface, never relayed through XRay.
builtins.filter (ib: ib.listener.type != "amneziawg") proxyInbounds
)
++ lib.optionals proxyInboundsCfg.subscriptions.enable [
80
443
]
++ proxyInboundsCfg.serverPorts
++ proxyInboundRuntimePorts
);
# An allow toward "direct" only: hostFenceRules below refuse the rest there.
serverAddressPorts = proxyInboundsHostPorts;
# XRay routes a UDP session by its first packet, so no exception leads to plain "direct"
# over UDP: IP rules go to outbounds whose finalRules allow only those addresses, name rules
# are held to TCP.
exceptionTarget =
field: ipOutbound:
if field == "ip" then
{ outboundTag = ipOutbound; }
else
{
outboundTag = "direct";
network = "tcp";
};
# Names and IPs in rules of their own: XRay ANDs the fields of one rule.
mkServerAddressRule =
ruleTag: field: items:
lib.optional (items != [ ] && any exceptionInboundTags) (forException {
type = "field";
inherit ruleTag;
${field} = items;
inboundTag = exceptionInboundTags;
port = serverPortList;
outboundTag = "direct";
});
lib.optional (items != [ ] && any exceptionInboundTags) (
forException (
{
type = "field";
inherit ruleTag;
${field} = items;
inboundTag = exceptionInboundTags;
port = serverPortList;
}
// exceptionTarget field "direct-server"
)
);
# Sniffing is routeOnly (proxy_inbound.py): a name rule also matches a connection to an IP
# whose TLS SNI or HTTP Host carries that name, and "direct" then dials the IP, from this
@@ -249,13 +264,17 @@ let
mkZapretRule =
ruleTag: field: items:
lib.optional (zapretDirectEnabled && items != [ ]) (forDefault {
type = "field";
inherit ruleTag;
${field} = items;
inboundTag = defaultInboundTags;
outboundTag = "direct";
});
lib.optional (zapretDirectEnabled && items != [ ]) (
forDefault (
{
type = "field";
inherit ruleTag;
${field} = items;
inboundTag = defaultInboundTags;
}
// exceptionTarget field "direct-zapret"
)
);
zapretDomains = map (domain: "domain:${domain}") zapretDirectRules.domains;
# Under IPOnDemand the guard would take every zapret name too: the domain rule stays off.
@@ -314,9 +333,66 @@ let
ip = [ "geoip:private" ];
}
];
# The "direct" outbound's fence around this host, whose own addresses it reaches past the
# firewall: only the ports above. The start script adds its interfaces' addresses.
hostAddresses =
proxyInboundsLoopback
++ lib.filter (range: range != null) [
proxyInboundsCfg.routing.serverSource.ipv4
proxyInboundsCfg.routing.serverSource.ipv6
]
++ serverIps;
hostFenceRules =
lib.optional (serverAddressPorts != [ ]) {
action = "allow";
ip = hostAddresses;
port = serverPortList;
_hostAddresses = true;
}
++ [
{
action = "block";
ip = hostAddresses;
_hostAddresses = true;
}
];
in
{
inherit xrayInboundRules selfFinalRules;
inherit xrayInboundRules selfFinalRules hostFenceRules;
# The names whose rules send a connection past its listener's via (this host, zapret's
# sites, .onion): matched only on the address the client asked for, never a sniffed name.
sniffDomainsExcluded = lib.unique (
serverNameDomains
++ lib.optionals zapretDirectEnabled zapretDomains
++ lib.optional (onionListeners != [ ]) "domain:onion"
);
# The outbounds the IP exceptions above lead to: each dials its own addresses alone. The
# start script puts "direct"'s fence around this host ahead of their rules.
exceptionOutbounds =
lib.optional (serverIps != [ ] && serverAddressPorts != [ ] && any exceptionInboundTags) {
protocol = "freedom";
tag = "direct-server";
settings.finalRules = [
{
action = "allow";
ip = serverIps;
port = serverPortList;
}
{ action = "block"; }
];
}
++ lib.optional (zapretDirectEnabled && zapretDirectRules.ips != [ ]) {
protocol = "freedom";
tag = "direct-zapret";
settings.finalRules = [
{
action = "allow";
ip = zapretDirectRules.ips;
}
{ action = "block"; }
];
};
# Names of this host sent direct with no guard (IPOnDemand, and no IP aliases): a warning.
unguardedServerNames = lib.optionals (!serverNameGuarded) serverNames;
}
+12 -1
View File
@@ -9,6 +9,8 @@
onionOutbound,
geodata,
ruleSets,
proxyInboundsLoopback,
hopPorts,
}:
let
@@ -85,6 +87,13 @@ let
++ lib.optional (onionOutbound != null) {
domain_suffix = [ "onion" ];
outbound = onionOutbound;
}
# A client asking for 127.0.0.1:<hop> (mixed-in's local users) would have the daemon dial
# the loopback hops the nft guard keeps from them.
++ lib.optional (hopPorts != [ ]) {
ip_cidr = proxyInboundsLoopback;
port = hopPorts;
action = "reject";
};
customRouteRules = map (rule: {
@@ -135,13 +144,15 @@ let
(mkRulesetRule "proxy" (map remoteTag r.proxy.ruleSets))
];
# Block ahead of the direct lists (category-ru's ad domains are in category-ads-all too);
# the user's own proxy lists still come first.
singBoxRoutingRules =
commonRules
++ lib.concatMap (item: item.entries) customRouteRules
++ proxyPrimaryRules
++ blockRules
++ directRules
++ safetyDirectRules
++ blockRules
++ proxyGeoRules;
# DNS follows the routing: a name that goes through the proxy is looked up through it, so
+31 -6
View File
@@ -8,6 +8,7 @@
customRuleCategory,
onionOutbound,
selectionMode,
proxyInboundsLoopback,
}:
let
@@ -117,19 +118,43 @@ let
(xrayGeoIPRules "proxy-geoip" "proxy" r.proxy.geoips)
];
# Kept by every route mode, like sing-box's common rules.
xrayCommonRules = lib.optional (onionOutbound != null) (
mkXrayRule "tor-onion" { domain = [ "domain:onion" ]; } onionOutbound
);
# Sniffing (and the TUN's fake DNS) puts a name in place of where a client connected: a
# TProxy LAN client's "Host: 127.0.0.1:2375", or a container's lvh.me through the TUN, must
# not reach this host's loopback. Names resolved only at the dial are the direct
# outbound's finalRules' (config-templates/xray.nix). No "::ffff:127.0.0.0/104": XRay
# refuses its whole config over it.
xrayTransparentInbounds = [
"tproxy-in"
"tproxy-in6"
"tun-in"
];
xrayLoopbackGuardRules = [
(mkXrayRule "guard-loopback-ip" {
inboundTag = xrayTransparentInbounds;
ip = proxyInboundsLoopback;
} "block")
(mkXrayRule "guard-loopback-domain" {
inboundTag = xrayTransparentInbounds;
domain = [ "domain:localhost" ];
} "block")
];
# Kept by every route mode, like sing-box's common rules.
xrayCommonRules =
xrayLoopbackGuardRules
++ lib.optional (onionOutbound != null) (
mkXrayRule "tor-onion" { domain = [ "domain:onion" ]; } onionOutbound
);
# Block ahead of the direct lists, as sing-box has it (rules/sing-box.nix).
xrayRoutingRules =
xrayCommonRules
++ lib.concatMap (item: item.entries) xrayCustomRouteRules
++ xrayProxyPrimaryRules
++ xrayBlockRules
++ xrayDirectRules
++ xraySafetyDirectRules
++ xrayProxyGeoRules
++ xrayBlockRules;
++ xrayProxyGeoRules;
xrayRouteModeRules = {
common = xrayCommonRules;