fix: hardening and fixes

This commit is contained in:
fufsob committed 2026-09-15 08:56:33 +00:00
1 parent aa19042ee5
commit a82417a2bd
32 files changed
+619 -256

No files matched your search

+65 -84
View File
@@ -284,17 +284,38 @@ def _set_interface_value(config: str, key: str, value: str) -> str:
return "\n".join(lines) + "\n"
def _interface_keys(config: str) -> set[str]:
keys: set[str] = set()
in_interface = False
def conf_sections(config: str) -> list[tuple[str, list[tuple[str, str]]]]:
"""Each [Section] of a WireGuard .conf, lowercased, with its key = value lines in order.
Keys are lowercased; comments (# or ;) and lines outside a section are dropped.
"""
sections: list[tuple[str, list[tuple[str, str]]]] = []
for line in config.splitlines():
stripped = line.split("#", 1)[0].split(";", 1)[0].strip()
if stripped.startswith("[") and stripped.endswith("]"):
in_interface = stripped.lower() == "[interface]"
continue
if in_interface and "=" in stripped:
key = stripped.split("=", 1)[0].strip()
keys.add(re.sub(r"[^a-z0-9]", "", key.lower()))
sections.append((stripped[1:-1].strip().lower(), []))
elif "=" in stripped and sections:
key, value = (part.strip() for part in stripped.split("=", 1))
sections[-1][1].append((key.lower(), value))
return sections
def section_values(config: str, section: str, key: str) -> list[str]:
"""Every value of `key` in every `section`, in file order."""
return [
value
for name, pairs in conf_sections(config)
if name == section
for found, value in pairs
if found == key
]
def _interface_keys(config: str) -> set[str]:
keys: set[str] = set()
for name, pairs in conf_sections(config):
if name == "interface":
keys.update(re.sub(r"[^a-z0-9]", "", key) for key, _ in pairs)
return keys
@@ -443,39 +464,15 @@ def render_settings(settings: dict[str, Any]) -> str:
lines.append(f"{target} = {settings[source]}")
obfuscation = _merge_obfuscation(settings)
obfuscation_map = {
"jc": "Jc",
"jmin": "Jmin",
"jmax": "Jmax",
"s1": "S1",
"s2": "S2",
"s3": "S3",
"s4": "S4",
"h1": "H1",
"h2": "H2",
"h3": "H3",
"h4": "H4",
"i1": "I1",
"i2": "I2",
"i3": "I3",
"i4": "I4",
"i5": "I5",
"contentPaddingAddition": "ContentPaddingAddition",
"rekeyAfterTime": "RekeyAfterTime",
"rekeyTimeout": "RekeyTimeout",
"rejectAfterTime": "RejectAfterTime",
"keepaliveTimeout": "KeepaliveTimeout",
"maxHandshakeAttempts": "MaxHandshakeAttempts",
}
for source, target in obfuscation_map.items():
if obfuscation.get(source) is not None:
lines.append(f"{target} = {obfuscation[source]}")
for source, target in {
"randomTrailers": "RandomTrailers",
"disableCookies": "DisableCookies",
}.items():
if obfuscation.get(source) is not None:
lines.append(f"{target} = {'on' if obfuscation[source] else 'off'}")
# Option names are the config keys in camelCase; the key comes last, from its secret.
values = {key.lower(): value for key, value in obfuscation.items()}
for normalized, target in VPN_INTERFACE_FIELDS.items():
value = values.get(normalized)
if normalized == "headerprotectionkey" or value is None:
continue
if isinstance(value, bool):
value = "on" if value else "off"
lines.append(f"{target} = {value}")
if obfuscation.get("headerProtectionKey") is not None or obfuscation.get(
"headerProtectionKeyFile"
) is not None:
@@ -531,17 +528,7 @@ def validate_config(config: str, allow_hooks: bool = False) -> None:
def probe_address(config: str) -> str:
"""Return an address covered by a peer route without exposing credentials."""
networks: list[ipaddress.IPv4Network | ipaddress.IPv6Network] = []
in_peer = False
for line in config.splitlines():
stripped = line.split("#", 1)[0].split(";", 1)[0].strip()
if stripped.startswith("[") and stripped.endswith("]"):
in_peer = stripped.lower() == "[peer]"
continue
if not in_peer or "=" not in stripped:
continue
key, value = (part.strip() for part in stripped.split("=", 1))
if key.lower() != "allowedips":
continue
for value in section_values(config, "peer", "allowedips"):
for item in value.split(","):
try:
networks.append(ipaddress.ip_network(item.strip(), strict=False))
@@ -561,25 +548,26 @@ def probe_address(config: str) -> str:
def transport_implementation(config: str) -> str:
"""Select userspace for the upstream RandomTrailers/ranged-header defect."""
random_trailers = False
ranged_handshake_header = False
in_interface = False
for line in config.splitlines():
stripped = line.split("#", 1)[0].split(";", 1)[0].strip()
if stripped.startswith("[") and stripped.endswith("]"):
in_interface = stripped.lower() == "[interface]"
continue
if not in_interface or "=" not in stripped:
continue
key, value = (part.strip() for part in stripped.split("=", 1))
normalized_key = key.lower()
if normalized_key == "randomtrailers":
random_trailers = value.lower() in {"on", "true", "yes", "1"}
elif normalized_key in {"h1", "h2", "h3"}:
ranged_handshake_header |= re.fullmatch(r"[0-9]+\s*-\s*[0-9]+", value) is not None
trailers = section_values(config, "interface", "randomtrailers")
random_trailers = bool(trailers) and trailers[-1].lower() in {"on", "true", "yes", "1"}
ranged_handshake_header = any(
re.fullmatch(r"[0-9]+\s*-\s*[0-9]+", value) is not None
for key in ("h1", "h2", "h3")
for value in section_values(config, "interface", key)
)
return "userspace" if random_trailers and ranged_handshake_header else "auto"
def rekey_after_time(config: str) -> int:
"""Longest wait between rekeys, in seconds; 0 when rekeying is off."""
values = section_values(config, "interface", "rekeyaftertime")
if not values:
return 120
if values[-1] == "(off)":
return 0
return int(values[-1].split("-")[-1])
def prepare(manifest: dict[str, Any]) -> str:
kind = manifest.get("kind")
if kind == "configFile":
@@ -619,25 +607,18 @@ def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--manifest")
parser.add_argument("--output")
parser.add_argument("--probe-address")
parser.add_argument("--transport-implementation")
parser.add_argument(
"--inspect",
metavar="CONFIG",
help="print the transport implementation, a probe address and the rekey interval",
)
args = parser.parse_args()
try:
if args.probe_address is not None:
if (
args.manifest is not None
or args.output is not None
or args.transport_implementation is not None
):
raise ConfigError("--probe-address cannot be combined with rendering options")
print(probe_address(_read_limited(args.probe_address)))
return 0
if args.transport_implementation is not None:
if args.inspect is not None:
if args.manifest is not None or args.output is not None:
raise ConfigError(
"--transport-implementation cannot be combined with rendering options"
)
print(transport_implementation(_read_limited(args.transport_implementation)))
raise ConfigError("--inspect cannot be combined with rendering options")
config = _read_limited(args.inspect)
print(transport_implementation(config), probe_address(config), rekey_after_time(config))
return 0
if args.manifest is None or args.output is None:
raise ConfigError("--manifest and --output are required when rendering a configuration")
+21
View File
@@ -10,12 +10,14 @@ import zlib
from pathlib import Path
from unittest.mock import patch
import amneziawg_config
from amneziawg_config import (
ConfigError,
decode_vpn_link,
extract_vpn_config,
prepare,
probe_address,
rekey_after_time,
render_settings,
transport_implementation,
validate_config,
@@ -415,6 +417,25 @@ class AmneziaWgConfigTests(unittest.TestCase):
)
self.assertEqual(probe_address(config), "10.23.42.1")
def test_rekey_after_time_takes_the_upper_bound(self):
config = BASE_CONFIG.replace("$PRIMARY_DNS,$SECONDARY_DNS", "1.1.1.1")
self.assertEqual(rekey_after_time(config.replace("[Peer]", "RekeyAfterTime = 100-300\n[Peer]")), 300)
self.assertEqual(rekey_after_time(config.replace("[Peer]", "RekeyAfterTime = (off)\n[Peer]")), 0)
# Only [Interface] counts, and WireGuard's own 120 is the default.
self.assertEqual(rekey_after_time(config + "RekeyAfterTime = 999\n"), 120)
def test_inspect_prints_what_the_start_script_reads(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "awg.conf"
path.write_text(BASE_CONFIG.replace("$PRIMARY_DNS,$SECONDARY_DNS", "1.1.1.1"))
result = subprocess.run(
[sys.executable, amneziawg_config.__file__, "--inspect", str(path)],
capture_output=True,
text=True,
check=True,
)
self.assertEqual(result.stdout, "auto 1.1.1.1 120\n")
def test_random_trailers_with_ranged_handshake_headers_forces_userspace(self):
config = BASE_CONFIG.replace(
"ContentPaddingAddition = 8-16",
+5 -14
View File
@@ -8,23 +8,14 @@ import json
import sys
from typing import Any
class ConfigError(ValueError):
pass
from amneziawg_config import ConfigError, conf_sections
def parse_conf(text: str) -> tuple[dict[str, str], dict[str, str]]:
"""Return the [Interface] section and the first [Peer], keys lowercased."""
sections: list[tuple[str, dict[str, str]]] = []
for line in text.splitlines():
stripped = line.split("#", 1)[0].strip()
if stripped.startswith("[") and stripped.endswith("]"):
sections.append((stripped[1:-1].strip().lower(), {}))
elif "=" in stripped and sections:
key, value = (part.strip() for part in stripped.split("=", 1))
sections[-1][1][key.lower()] = value
interface = next((s for name, s in sections if name == "interface"), None)
peer = next((s for name, s in sections if name == "peer"), None)
sections = conf_sections(text)
interface = next((dict(pairs) for name, pairs in sections if name == "interface"), None)
peer = next((dict(pairs) for name, pairs in sections if name == "peer"), None)
if interface is None or peer is None:
raise ConfigError("configuration requires [Interface] and [Peer] sections")
for section, key in ((interface, "privatekey"), (interface, "address"), (peer, "publickey"), (peer, "endpoint")):
@@ -78,7 +69,7 @@ def main() -> int:
args = parser.parse_args()
try:
print(json.dumps(build(sys.stdin.read(), args.tag, args.routing_mark)))
except (ConfigError, ValueError) as error:
except ValueError as error:
print(f"proxy-suite: warp: {error}", file=sys.stderr)
return 1
return 0