Pick tarpit content by request path

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
TomandClaude Sonnet 5.5 committed 2026-11-16 23:00:26 +01:00
1 parent e48b6bb832
commit 13468bbdf9
18 files changed
+307 -9

No files matched your search

@@ -5,6 +5,7 @@ using Flypaper.Core.Strategies;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Flypaper.Core;
@@ -20,6 +21,15 @@ public static class FlypaperCoreExtensions
services.TryAddSingleton<IRandomSource, SystemRandomSource>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITarpitStrategy, HtmlDripStrategy>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITarpitStrategy, TextDripStrategy>());
foreach (var profile in FileContent.Profiles)
{
services.AddSingleton<ITarpitStrategy>(provider => new FileDripStrategy(
profile,
provider.GetRequiredService<IOptionsMonitor<DripOptions>>(),
provider.GetRequiredService<IRandomSource>(),
provider.GetRequiredService<TimeProvider>()));
}
services.TryAddSingleton<ITrapStrategySelector, ConfiguredStrategySelector>();
services.TryAddSingleton<TrapRunner>();
services.TryAddSingleton<IIpEnricher, NullIpEnricher>();
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Flypaper.Core.Models;
public sealed record TrapContext(TrapSession Session, string? Target = null, string? ClientInfo = null);
public sealed record TrapContext(TrapSession Session, string? Target = null, string? ClientInfo = null, string? Path = null);
@@ -13,6 +13,12 @@ public sealed class ConfiguredStrategySelector(
public ITarpitStrategy? Select(TrapContext context)
{
var candidates = strategies.Where(strategy => strategy.Supports(context.Session.Protocol)).ToList();
if (options.CurrentValue.MatchPath && FileContent.Match(context.Path) is { } profile
&& candidates.FirstOrDefault(strategy => strategy.Name == profile.Name) is { } fileStrategy)
{
return fileStrategy;
}
string[] wanted = options.CurrentValue.Content switch
{
DripContent.Text => [TextDripStrategy.StrategyName],
@@ -10,6 +10,7 @@ public sealed record HoldMethodSettings(
int MinChunkSize,
int MaxChunkSize,
int HeadStartSize,
bool MatchPath,
TimeSpan MaxHoldDuration)
{
public double BytesPerSecond => AverageBytesPerSecond(MinDelay, MaxDelay, MinChunkSize, MaxChunkSize);
@@ -22,6 +23,7 @@ public sealed record HoldMethodSettings(
[SettingKeys.DripMinChunkSize] = MinChunkSize.ToString(CultureInfo.InvariantCulture),
[SettingKeys.DripMaxChunkSize] = MaxChunkSize.ToString(CultureInfo.InvariantCulture),
[SettingKeys.DripHeadStartSize] = HeadStartSize.ToString(CultureInfo.InvariantCulture),
[SettingKeys.DripMatchPath] = MatchPath ? "true" : "false",
[SettingKeys.MaxHoldDuration] = MaxHoldDuration.ToString("c", CultureInfo.InvariantCulture)
};
@@ -39,6 +41,7 @@ public sealed record HoldMethodInput(
string? MinChunkSize,
string? MaxChunkSize,
string? HeadStartSize,
bool MatchPath,
string? MaxHoldDuration);
public sealed record HoldMethodParseResult(HoldMethodSettings? Settings, IReadOnlyDictionary<string, string> Errors);
@@ -84,6 +84,7 @@ public static class RuntimeSettingsParser
minChunk!.Value,
maxChunk!.Value,
headStart!.Value,
input.MatchPath,
hold!.Value),
errors);
}
+2 -1
View File
@@ -13,12 +13,13 @@ public static class SettingKeys
public const string DripMinChunkSize = "Flypaper:Drip:MinChunkSize";
public const string DripMaxChunkSize = "Flypaper:Drip:MaxChunkSize";
public const string DripHeadStartSize = "Flypaper:Drip:HeadStartSize";
public const string DripMatchPath = "Flypaper:Drip:MatchPath";
public static readonly IReadOnlyList<string> General =
[MaxConnections, MaxConnectionsPerIp, IgnorePrivateNetworks, RetentionDays];
public static readonly IReadOnlyList<string> HoldMethod =
[DripContent, DripMinDelay, DripMaxDelay, DripMinChunkSize, DripMaxChunkSize, DripHeadStartSize, MaxHoldDuration];
[DripContent, DripMinDelay, DripMaxDelay, DripMinChunkSize, DripMaxChunkSize, DripHeadStartSize, DripMatchPath, MaxHoldDuration];
public static readonly IReadOnlySet<string> All =
new HashSet<string>(General.Concat(HoldMethod), StringComparer.OrdinalIgnoreCase);
@@ -15,4 +15,6 @@ public sealed class DripOptions
public int MaxChunkSize { get; set; } = 64;
public int HeadStartSize { get; set; } = 1024;
public bool MatchPath { get; set; } = true;
}
+142
View File
@@ -0,0 +1,142 @@
using System.Globalization;
using Flypaper.Core.Abstractions;
namespace Flypaper.Core.Strategies;
public sealed record FileProfile(
string Name,
string MediaType,
Func<string, bool> Matches,
Func<IRandomSource, IEnumerable<string>> Generate);
public static class FileContent
{
private static readonly string[] Sections = ["docs", "blog", "wiki", "support", "products", "archive", "news", "help", "files"];
public static readonly IReadOnlyList<FileProfile> Profiles =
[
new("git-drip", "text/plain; charset=utf-8", path => path.Contains("/.git/", StringComparison.Ordinal), GitConfig),
new("robots-drip", "text/plain; charset=utf-8", path => LastSegment(path) == "robots.txt", Robots),
new("env-drip", "text/plain; charset=utf-8", path => IsEnv(LastSegment(path)), Env),
new("json-drip", "application/json", path => path.EndsWith(".json", StringComparison.Ordinal), Json),
new("xml-drip", "application/xml", path => path.EndsWith(".xml", StringComparison.Ordinal), Xml),
new("yaml-drip", "text/plain; charset=utf-8", path => path.EndsWith(".yml", StringComparison.Ordinal) || path.EndsWith(".yaml", StringComparison.Ordinal), Yaml),
new("sql-drip", "text/plain; charset=utf-8", path => path.EndsWith(".sql", StringComparison.Ordinal), Sql)
];
public static FileProfile? Match(string? path)
{
if (string.IsNullOrEmpty(path))
{
return null;
}
var normalized = path.ToLowerInvariant();
return Profiles.FirstOrDefault(profile => profile.Matches(normalized));
}
private static string LastSegment(string path) => path[(path.LastIndexOf('/') + 1)..];
private static bool IsEnv(string name) =>
name.StartsWith(".env", StringComparison.Ordinal) || name.EndsWith(".env", StringComparison.Ordinal) || name == "env";
private static IEnumerable<string> Env(IRandomSource random)
{
yield return $"APP_NAME=Portal\nAPP_ENV=production\nAPP_DEBUG=false\nAPP_URL=https://example.com\nAPP_KEY=base64:{Hex(random, 32)}\n\n"
+ $"DB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=app\nDB_USERNAME=app\nDB_PASSWORD={Hex(random, 16)}\n\n";
while (true)
{
var key = $"{Upper(random)}_{Upper(random)}";
yield return random.Next(0, 4) switch
{
0 => $"{key}=true\n",
1 => $"{key}={random.Next(1, 65536)}\n",
2 => $"{key}=https://{LoremText.Word(random)}.internal/\n",
_ => $"{key}={Hex(random, 24)}\n"
};
}
}
private static IEnumerable<string> Json(IRandomSource random)
{
yield return "{\n \"name\": \"portal\",\n \"version\": \"1.4.2\",\n \"private\": true,\n \"entries\": [\n";
var id = 1;
while (true)
{
yield return $" {{\"id\": {id++}, \"name\": \"{LoremText.Word(random)}\", \"token\": \"{Hex(random, 16)}\", \"enabled\": {(random.Next(0, 2) == 0 ? "true" : "false")}}},\n";
}
}
private static IEnumerable<string> Xml(IRandomSource random)
{
yield return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
while (true)
{
var date = string.Create(CultureInfo.InvariantCulture, $"2026-{random.Next(1, 13):00}-{random.Next(1, 29):00}");
yield return $" <url>\n <loc>https://example.com/{Pick(random, Sections)}/{LoremText.Phrase(random, 2).Replace(' ', '-')}</loc>\n <lastmod>{date}</lastmod>\n </url>\n";
}
}
private static IEnumerable<string> Yaml(IRandomSource random)
{
yield return "version: \"3.8\"\nservices:\n";
while (true)
{
yield return $" {LoremText.Word(random)}:\n image: {LoremText.Word(random)}:{random.Next(1, 10)}.{random.Next(0, 20)}\n restart: unless-stopped\n environment:\n - SECRET={Hex(random, 12)}\n";
}
}
private static IEnumerable<string> Sql(IRandomSource random)
{
yield return "-- MySQL dump 10.13 Distrib 8.0.36, for Linux (x86_64)\n--\n-- Host: localhost Database: app\n-- ------------------------------------------------------\n\n"
+ "DROP TABLE IF EXISTS `users`;\nCREATE TABLE `users` (\n `id` int NOT NULL AUTO_INCREMENT,\n `name` varchar(64) NOT NULL,\n `token` varchar(64) NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n"
+ "LOCK TABLES `users` WRITE;\n";
var id = 1;
while (true)
{
yield return $"INSERT INTO `users` VALUES ({id++},'{LoremText.Word(random)}','{Hex(random, 32)}');\n";
}
}
private static IEnumerable<string> GitConfig(IRandomSource random)
{
yield return "[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = false\n\tlogallrefupdates = true\n"
+ "[remote \"origin\"]\n\turl = https://example.com/portal.git\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n";
while (true)
{
var branch = $"{LoremText.Word(random)}-{LoremText.Word(random)}";
yield return $"[branch \"{branch}\"]\n\tremote = origin\n\tmerge = refs/heads/{branch}\n";
}
}
private static IEnumerable<string> Robots(IRandomSource random)
{
yield return "User-agent: *\nCrawl-delay: 2\nSitemap: https://example.com/sitemap.xml\n";
while (true)
{
yield return $"Disallow: /{Pick(random, Sections)}/{LoremText.Word(random)}/\n";
}
}
private static string Hex(IRandomSource random, int length)
{
var chars = new char[length];
for (var i = 0; i < length; i++)
{
chars[i] = "0123456789abcdef"[random.Next(0, 16)];
}
return new string(chars);
}
private static string Upper(IRandomSource random) => LoremText.Word(random).ToUpperInvariant();
private static string Pick(IRandomSource random, string[] values) => values[random.Next(0, values.Length)];
}
@@ -0,0 +1,17 @@
using Flypaper.Core.Abstractions;
using Microsoft.Extensions.Options;
namespace Flypaper.Core.Strategies;
public sealed class FileDripStrategy(
FileProfile profile,
IOptionsMonitor<DripOptions> options,
IRandomSource random,
TimeProvider timeProvider) : DripStrategy(options, random, timeProvider)
{
public override string Name => profile.Name;
public override string MediaType => profile.MediaType;
protected override IEnumerable<string> Content(IRandomSource random) => profile.Generate(random);
}
@@ -35,7 +35,8 @@ internal sealed class FlypaperMiddleware(
var trapContext = new TrapContext(
session,
$"{request.Method} {StoredHost(request.Host)}{request.Path}{request.QueryString}",
request.Headers.UserAgent.ToString());
request.Headers.UserAgent.ToString(),
request.Path.Value);
var outcome = await runner.RunAsync(
trapContext,
@@ -14,5 +14,7 @@ public sealed class HoldMethodModel
public string? HeadStartSize { get; set; }
public bool MatchPath { get; set; }
public string? MaxHoldDuration { get; set; }
}
@@ -64,6 +64,14 @@
</SettingsField>
</div>
<label class="switch-row">
<InputCheckbox class="switch" @bind-Value="Model!.MatchPath" />
<span>
<strong>Inhalt zum Pfad passend wählen</strong>
<span class="muted">Anfragen wie /.env, /config.json, /sitemap.xml oder /backup.sql bekommen passend aussehenden Inhalt statt einer Webseite.</span>
</span>
</label>
<div class="estimate">
<span class="estimate-value">Ø <strong id="drip-bandwidth">@Bandwidth</strong> Bytes/s</span>
<span class="muted">pro gefangener Verbindung, berechnet aus Pausen und Paketgrößen</span>
@@ -129,6 +137,7 @@
Model.MinChunkSize,
Model.MaxChunkSize,
Model.HeadStartSize,
Model.MatchPath,
Model.MaxHoldDuration));
if (result.Settings is null)
@@ -158,6 +167,7 @@
MinChunkSize = drip.MinChunkSize.ToString(CultureInfo.InvariantCulture),
MaxChunkSize = drip.MaxChunkSize.ToString(CultureInfo.InvariantCulture),
HeadStartSize = drip.HeadStartSize.ToString(CultureInfo.InvariantCulture),
MatchPath = drip.MatchPath,
MaxHoldDuration = Core.CurrentValue.MaxHoldDuration.ToString("c", CultureInfo.InvariantCulture)
};
}