A scanner farm inside one /24 is twenty engine bans and twenty operator decisions: the streak rule works per source, and nothing in the edge connected the rows. OPEN-66 made a range enforceable; this is the half that notices the range is there. The rulings: - The evidence is the edge's own live engine bans. An operator's row is a decision already taken, and an expired ban is a row the edge stopped honouring; neither is evidence about a netblock. Nothing new is observed and nothing new is stored. - An offer is not a ban. A candidate changes nothing on the wire: the operator's POST /admin-api/blocks with the CIDR is what enforces it, and the console button is that same call. Twenty honest visitors of one provider and one farm inside one rented block produce the same number, and only a human tells them apart. - Grouping is one allocation up from the key: v4 to /24, v6 to /48, because the v6 streak key is already a /64 and grouping there would only repeat the key. The count and both prefixes are policy keys with bounds, because a host that hands out /22s should be shown the block it can act on. - A ban keyed on a block wider than the netblock prefix is skipped: that row already claims more than a /24, and counting it as one ban inside a /24 would invent a candidate out of it. - Reading the list is not news — the scan that answers the surface raises no alert, so refreshing a page cannot manufacture alert rows. The timer's scan raises netblock_candidate through the same durable alert table and push queue as every other kind, at most once per block per six hours, and never for a block the operator has already banned. - A failed scan retires the offers instead of repeating the last ones, and /admin-api/state says which of off / failed / found-nothing this is. What the run caught: - The console's "ban this range" button posted a body with no content type, and the edge refused it: the button did nothing and the page looked idle rather than wrong. It uses the page's own postJson now, and the console gate asserts the row it writes, the wire answer for a source inside it, and the offer turning into "already banned". - The grouping parsed ban keys with SourceKey.TryParse, which fails on the "2001:db8:1:2::/64" spelling the engine stores v6 bans under — so no v6 ban was ever grouped. Keys are parsed as keys, prefix included. - An assertion claimed 203.0.113.137 sits under 203.0.113.128/24. That is a /25: the arithmetic was wrong in the test, not in the mask. build 0 warnings / 0 errors, unit 353/353, integration 326/326 in 31 s, console 114 assertions 0 failed, smoke passed 149 failed 0, compose 293 passed 0 failed against the built image at 262 MB. Refs OPEN-67.
148 lines
6.6 KiB
C#
148 lines
6.6 KiB
C#
using System.Net;
|
|
using System.Net.Sockets;
|
|
using OpenWaf.Domain.Engine;
|
|
using OpenWaf.Domain.Policy;
|
|
|
|
namespace OpenWaf.Domain.Security;
|
|
|
|
/// <summary>
|
|
/// A netblock the edge's own bans point at (OPEN-67). This is not a decision
|
|
/// and nothing here enforces anything: it says "these N live engine bans all
|
|
/// sit inside this one block", which is a fact about the edge's list, not a
|
|
/// claim that the block is one farm. Twenty honest visitors of one provider and
|
|
/// one scanner rotating through one rented /24 produce the same number, and
|
|
/// only the operator can tell them apart — which is why the candidate ends as a
|
|
/// suggestion on a surface and never as a row in the block list.
|
|
/// </summary>
|
|
/// <param name="Match">The block in the one CIDR spelling OPEN-66 stores and
|
|
/// enforces, so the string on the surface is the string the block form takes.</param>
|
|
/// <param name="Bans">How many live engine bans sit inside it.</param>
|
|
/// <param name="Sample">A few of those keys, so the operator can check the
|
|
/// claim against the rows rather than trust the count.</param>
|
|
/// <param name="AlreadyBanned">Whether a live operator range already covers this
|
|
/// block — the offer has been taken, and the row in the block list is the answer.</param>
|
|
public sealed record NetblockCandidate(
|
|
string Match,
|
|
int Prefix,
|
|
int Bans,
|
|
DateTimeOffset FirstBanAt,
|
|
DateTimeOffset LastBanAt,
|
|
IReadOnlyList<string> Sample,
|
|
bool AlreadyBanned);
|
|
|
|
/// <summary>The grouping rules, as pure functions: the arithmetic a reader has
|
|
/// to check sits in one place and is testable without a database.</summary>
|
|
public static class Netblocks
|
|
{
|
|
/// <summary>How many of a candidate's addresses the surface shows.</summary>
|
|
public const int SampleSize = 3;
|
|
|
|
/// <summary>The prefix a ban key itself names: 32 for a bare v4 address, 64
|
|
/// for a v6 key written "a:b:c:d::/64", -1 when the key names no address at
|
|
/// all (the "*" a /0 source key becomes).</summary>
|
|
public static int KeyPrefix(string? banKey)
|
|
{
|
|
if (!TryKeyAddress(banKey, out var ip)) return -1;
|
|
var full = ip.AddressFamily == AddressFamily.InterNetwork ? 32 : 128;
|
|
var slash = banKey!.Trim().IndexOf('/');
|
|
if (slash < 0) return full;
|
|
return int.TryParse(banKey.Trim()[(slash + 1)..], out var prefix) ? prefix : -1;
|
|
}
|
|
|
|
/// <summary>The address a ban key names, whether the key was written as one
|
|
/// address or as a block. The v6 key the engine stores is a block —
|
|
/// "2001:db8:1:2::/64" — so a parser that only accepts bare addresses cannot
|
|
/// see the bans it is supposed to group.</summary>
|
|
private static bool TryKeyAddress(string? banKey, out IPAddress address)
|
|
{
|
|
address = IPAddress.None;
|
|
if (string.IsNullOrWhiteSpace(banKey)) return false;
|
|
var text = banKey.Trim();
|
|
var slash = text.IndexOf('/');
|
|
if (slash >= 0) text = text[..slash];
|
|
return SourceKey.TryParse(text, out _, out address);
|
|
}
|
|
|
|
/// <summary>The block this ban key sits under, or null when the key is not
|
|
/// evidence about one block.</summary>
|
|
public static string? Covering(string? banKey, EdgePolicy policy)
|
|
{
|
|
if (!TryKeyAddress(banKey, out var ip)) return null;
|
|
var prefix = NetblockPrefix(ip.AddressFamily, policy);
|
|
return $"{SourceKey.Mask(ip, prefix)}/{prefix}";
|
|
}
|
|
|
|
public static int NetblockPrefix(AddressFamily family, EdgePolicy policy)
|
|
=> family == AddressFamily.InterNetwork ? policy.NetblockPrefixV4 : policy.NetblockPrefixV6;
|
|
|
|
/// <summary>Group live engine bans into the blocks they point at and keep
|
|
/// the blocks with enough of them to be worth showing.</summary>
|
|
public static IReadOnlyList<NetblockCandidate> Candidates(
|
|
IEnumerable<(string Key, DateTimeOffset CreatedAt)> liveEngineBans,
|
|
EdgePolicy policy,
|
|
NetworkBanIndex operatorRanges,
|
|
DateTimeOffset now)
|
|
{
|
|
if (policy.NetblockCandidateBans <= 0) return [];
|
|
|
|
var groups = new Dictionary<string, Group>(StringComparer.Ordinal);
|
|
foreach (var (key, createdAt) in liveEngineBans)
|
|
{
|
|
if (!TryKeyAddress(key, out var ip)) continue;
|
|
var ownPrefix = KeyPrefix(key);
|
|
|
|
// A key that names no address at all (the "*" a /0 source key
|
|
// becomes) is inside no block, and a ban whose own key is a block
|
|
// wider than the netblock prefix is not evidence about one netblock:
|
|
// counting the /8 the operator keyed on as "one ban inside
|
|
// 198.51.100.0/24" would invent a candidate out of a row that
|
|
// already claims more than that.
|
|
if (ownPrefix < 0 || ownPrefix < NetblockPrefix(ip.AddressFamily, policy)) continue;
|
|
var prefix = NetblockPrefix(ip.AddressFamily, policy);
|
|
|
|
var match = $"{SourceKey.Mask(ip, prefix)}/{prefix}";
|
|
if (!groups.TryGetValue(match, out var group)) groups[match] = group = new Group();
|
|
group.Add(key, createdAt);
|
|
}
|
|
|
|
var candidates = new List<NetblockCandidate>();
|
|
foreach (var (match, group) in groups)
|
|
{
|
|
if (group.Count < policy.NetblockCandidateBans) continue;
|
|
var slash = match.IndexOf('/');
|
|
// The block's own address answers the question "is this block
|
|
// already inside a range the operator wrote?" — a range equal to it
|
|
// or broader covers that address, and a narrower one does not.
|
|
var covered = operatorRanges.Covers(match[..slash], now);
|
|
candidates.Add(new NetblockCandidate(
|
|
match, NetworkBans.PrefixOf(match), group.Count, group.First, group.Last,
|
|
group.Sample(), covered));
|
|
}
|
|
|
|
candidates.Sort((a, b) =>
|
|
{
|
|
var byBans = b.Bans.CompareTo(a.Bans);
|
|
return byBans != 0 ? byBans : string.CompareOrdinal(a.Match, b.Match);
|
|
});
|
|
return candidates;
|
|
}
|
|
|
|
private sealed class Group
|
|
{
|
|
private readonly List<string> _sample = [];
|
|
public int Count { get; private set; }
|
|
public DateTimeOffset First { get; private set; }
|
|
public DateTimeOffset Last { get; private set; }
|
|
|
|
public void Add(string key, DateTimeOffset createdAt)
|
|
{
|
|
Count++;
|
|
if (Count == 1 || createdAt < First) First = createdAt;
|
|
if (Count == 1 || createdAt > Last) Last = createdAt;
|
|
if (_sample.Count < SampleSize && !_sample.Contains(key, StringComparer.Ordinal)) _sample.Add(key);
|
|
}
|
|
|
|
public IReadOnlyList<string> Sample() => _sample;
|
|
}
|
|
}
|