Add Flypaper solution skeleton with HTTP tarpit, SQLite event log and tests
This commit is contained in:
1 parent
20314268c6
commit
f9161f48e7
55 files changed
+1331
No files matched your search
@@ -0,0 +1,72 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public sealed class ConnectionGovernor(IOptions<FlypaperOptions> options) : IConnectionGovernor
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<IPAddress, int> _perIp = [];
|
||||
private int _total;
|
||||
|
||||
public int ActiveConnections
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _total;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryAcquire(TrapSession session, [NotNullWhen(true)] out IDisposable? lease)
|
||||
{
|
||||
var ip = Normalize(session.RemoteIp);
|
||||
var limits = options.Value;
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
_perIp.TryGetValue(ip, out var count);
|
||||
if (_total >= limits.MaxConnections || count >= limits.MaxConnectionsPerIp)
|
||||
{
|
||||
lease = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
_total++;
|
||||
_perIp[ip] = count + 1;
|
||||
}
|
||||
|
||||
lease = new Lease(this, ip);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Release(IPAddress ip)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_total--;
|
||||
if (--_perIp[ip] == 0)
|
||||
{
|
||||
_perIp.Remove(ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IPAddress Normalize(IPAddress ip) => ip.IsIPv4MappedToIPv6 ? ip.MapToIPv4() : ip;
|
||||
|
||||
private sealed class Lease(ConnectionGovernor owner, IPAddress ip) : IDisposable
|
||||
{
|
||||
private int _disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) == 0)
|
||||
{
|
||||
owner.Release(ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Flypaper.Core;
|
||||
|
||||
internal sealed class CountingTrapChannel(ITrapChannel inner) : ITrapChannel
|
||||
{
|
||||
private long _bytesSent;
|
||||
|
||||
public TrapSession Session => inner.Session;
|
||||
|
||||
public long BytesSent => Interlocked.Read(ref _bytesSent);
|
||||
|
||||
public async ValueTask WriteAsync(ReadOnlyMemory<byte> data, CancellationToken ct)
|
||||
{
|
||||
await inner.WriteAsync(data, ct);
|
||||
Interlocked.Add(ref _bytesSent, data.Length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="Flypaper.Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public sealed class FlypaperOptions
|
||||
{
|
||||
public const string SectionName = "Flypaper";
|
||||
|
||||
public int MaxConnections { get; set; } = 1000;
|
||||
|
||||
public int MaxConnectionsPerIp { get; set; } = 10;
|
||||
|
||||
public TimeSpan MaxHoldDuration { get; set; } = TimeSpan.FromHours(1);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public interface IConnectionGovernor
|
||||
{
|
||||
bool TryAcquire(TrapSession session, [NotNullWhen(true)] out IDisposable? lease);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public interface ITarpitStrategy
|
||||
{
|
||||
string Name { get; }
|
||||
|
||||
bool Supports(TrapProtocol protocol);
|
||||
|
||||
Task RunAsync(ITrapChannel channel, CancellationToken ct);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public interface ITrapChannel
|
||||
{
|
||||
TrapSession Session { get; }
|
||||
|
||||
ValueTask WriteAsync(ReadOnlyMemory<byte> data, CancellationToken ct);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public interface ITrapEventSink
|
||||
{
|
||||
void Publish(TrapEvent trapEvent);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using Flypaper.Core.Strategies;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddFlypaperCore(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.Configure<FlypaperOptions>(configuration.GetSection(FlypaperOptions.SectionName));
|
||||
services.Configure<DripOptions>(configuration.GetSection(DripOptions.SectionName));
|
||||
|
||||
services.TryAddSingleton(TimeProvider.System);
|
||||
services.TryAddSingleton<IConnectionGovernor, ConnectionGovernor>();
|
||||
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITarpitStrategy, DripStrategy>());
|
||||
services.TryAddSingleton<TrapRunner>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public sealed class DripOptions
|
||||
{
|
||||
public const string SectionName = "Flypaper:Drip";
|
||||
|
||||
public TimeSpan Interval { get; set; } = TimeSpan.FromSeconds(10);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public sealed class DripStrategy(IOptions<DripOptions> options, TimeProvider timeProvider) : ITarpitStrategy
|
||||
{
|
||||
private static readonly byte[] Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"u8.ToArray();
|
||||
|
||||
public string Name => "drip";
|
||||
|
||||
public bool Supports(TrapProtocol protocol) => protocol == TrapProtocol.Http;
|
||||
|
||||
public async Task RunAsync(ITrapChannel channel, CancellationToken ct)
|
||||
{
|
||||
var buffer = new byte[1];
|
||||
while (true)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
buffer[0] = Alphabet[Random.Shared.Next(Alphabet.Length)];
|
||||
await channel.WriteAsync(buffer, ct);
|
||||
await Task.Delay(options.Value.Interval, timeProvider, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public sealed record TrapContext(TrapSession Session, string? Target = null, string? ClientInfo = null);
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.Net;
|
||||
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public sealed record TrapEvent(
|
||||
Guid SessionId,
|
||||
TrapProtocol Protocol,
|
||||
IPAddress RemoteIp,
|
||||
string Strategy,
|
||||
DateTimeOffset StartedAt,
|
||||
DateTimeOffset EndedAt,
|
||||
long BytesSent,
|
||||
TrapOutcome Outcome,
|
||||
string? Target = null,
|
||||
string? ClientInfo = null)
|
||||
{
|
||||
public TimeSpan Duration => EndedAt - StartedAt;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public enum TrapOutcome
|
||||
{
|
||||
ClientAborted,
|
||||
Timeout,
|
||||
Rejected,
|
||||
Completed
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public enum TrapProtocol
|
||||
{
|
||||
Http,
|
||||
Ssh
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public sealed class TrapRunner(
|
||||
IConnectionGovernor governor,
|
||||
IEnumerable<ITarpitStrategy> strategies,
|
||||
ITrapEventSink sink,
|
||||
IOptions<FlypaperOptions> options,
|
||||
TimeProvider timeProvider)
|
||||
{
|
||||
public async Task<TrapOutcome> RunAsync(
|
||||
TrapContext context,
|
||||
Func<CancellationToken, ValueTask<ITrapChannel>> openChannel,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var session = context.Session;
|
||||
var strategy = strategies.FirstOrDefault(s => s.Supports(session.Protocol));
|
||||
|
||||
if (strategy is null || !governor.TryAcquire(session, out var lease))
|
||||
{
|
||||
Publish(context, strategy?.Name ?? "none", 0, TrapOutcome.Rejected);
|
||||
return TrapOutcome.Rejected;
|
||||
}
|
||||
|
||||
using var _ = lease;
|
||||
using var hold = new CancellationTokenSource(options.Value.MaxHoldDuration, timeProvider);
|
||||
using var linked = CancellationTokenSource.CreateLinkedTokenSource(ct, hold.Token);
|
||||
|
||||
CountingTrapChannel? channel = null;
|
||||
var outcome = TrapOutcome.Completed;
|
||||
try
|
||||
{
|
||||
channel = new CountingTrapChannel(await openChannel(linked.Token));
|
||||
await strategy.RunAsync(channel, linked.Token);
|
||||
}
|
||||
catch (OperationCanceledException) when (linked.IsCancellationRequested)
|
||||
{
|
||||
outcome = hold.IsCancellationRequested && !ct.IsCancellationRequested
|
||||
? TrapOutcome.Timeout
|
||||
: TrapOutcome.ClientAborted;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
outcome = TrapOutcome.ClientAborted;
|
||||
}
|
||||
|
||||
Publish(context, strategy.Name, channel?.BytesSent ?? 0, outcome);
|
||||
return outcome;
|
||||
}
|
||||
|
||||
private void Publish(TrapContext context, string strategy, long bytesSent, TrapOutcome outcome)
|
||||
{
|
||||
var session = context.Session;
|
||||
sink.Publish(new TrapEvent(
|
||||
session.Id,
|
||||
session.Protocol,
|
||||
session.RemoteIp,
|
||||
strategy,
|
||||
session.StartedAt,
|
||||
timeProvider.GetUtcNow(),
|
||||
bytesSent,
|
||||
outcome,
|
||||
context.Target,
|
||||
context.ClientInfo));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using System.Net;
|
||||
|
||||
namespace Flypaper.Core;
|
||||
|
||||
public sealed record TrapSession(Guid Id, TrapProtocol Protocol, IPAddress RemoteIp, DateTimeOffset StartedAt);
|
||||
Reference in new issue
Block a user