Add link labyrinth, static robots.txt and robots bait paths
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b7027e2a72
commit
a25072e6fb
29 files changed
+774
-34
No files matched your search
@@ -0,0 +1,52 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public static partial class BaitPaths
|
||||
{
|
||||
public const int MaxCount = 8;
|
||||
|
||||
public const string Default = "/backup-2024/,/admin-old/,/internal/db-export/,/old-site/";
|
||||
|
||||
[GeneratedRegex("^/[A-Za-z0-9._~-]+(/[A-Za-z0-9._~-]+)*/?$")]
|
||||
private static partial Regex Valid();
|
||||
|
||||
public static IReadOnlyList<string> Split(string? raw) =>
|
||||
(raw ?? "").Split([',', ';', ' ', '\n', '\r', '\t'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
public static string? Normalize(string entry)
|
||||
{
|
||||
if (!Valid().IsMatch(entry) || entry.Length > 100)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var normalized = entry.ToLowerInvariant();
|
||||
if (!normalized.EndsWith('/'))
|
||||
{
|
||||
normalized += "/";
|
||||
}
|
||||
|
||||
var first = normalized.Split('/', StringSplitOptions.RemoveEmptyEntries)[0];
|
||||
return normalized == RobotsStaticStrategy.Path + "/" || Array.IndexOf(MazeContent.Roots, first) >= 0 ? null : normalized;
|
||||
}
|
||||
|
||||
public static IReadOnlyList<string> Parse(string? raw) =>
|
||||
Split(raw).Select(Normalize).OfType<string>().Distinct().Take(MaxCount).ToList();
|
||||
|
||||
public static bool Matches(IReadOnlyList<string> baits, string? path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || baits.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var normalized = path.ToLowerInvariant();
|
||||
if (!normalized.EndsWith('/'))
|
||||
{
|
||||
normalized += "/";
|
||||
}
|
||||
|
||||
return baits.Any(bait => normalized.StartsWith(bait, StringComparison.Ordinal));
|
||||
}
|
||||
}
|
||||
@@ -17,4 +17,8 @@ public sealed class DripOptions
|
||||
public int HeadStartSize { get; set; } = 1024;
|
||||
|
||||
public bool MatchPath { get; set; } = true;
|
||||
|
||||
public int LinkSpacing { get; set; } = 160;
|
||||
|
||||
public string BaitPaths { get; set; } = Strategies.BaitPaths.Default;
|
||||
}
|
||||
@@ -22,13 +22,13 @@ public abstract class DripStrategy(
|
||||
var head = current.HeadStartSize > 0 ? $" head {current.HeadStartSize}B" : "";
|
||||
return string.Create(
|
||||
CultureInfo.InvariantCulture,
|
||||
$"{Name} {current.MinDelay.TotalMilliseconds:0}-{current.MaxDelay.TotalMilliseconds:0}ms {current.MinChunkSize}-{current.MaxChunkSize}B{head}");
|
||||
$"{Name} {current.MinDelay.TotalMilliseconds:0}-{current.MaxDelay.TotalMilliseconds:0}ms {current.MinChunkSize}-{current.MaxChunkSize}B{head}{Extra(current)}");
|
||||
}
|
||||
|
||||
public async Task RunAsync(ITrapChannel channel, CancellationToken ct)
|
||||
{
|
||||
var current = options.CurrentValue;
|
||||
using var fragments = Content(random).GetEnumerator();
|
||||
using var fragments = Content(random, channel, current).GetEnumerator();
|
||||
|
||||
var head = DripSchedule.TakeHead(fragments, current.HeadStartSize);
|
||||
if (head.Length > 0)
|
||||
@@ -44,5 +44,7 @@ public abstract class DripStrategy(
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract IEnumerable<string> Content(IRandomSource random);
|
||||
protected virtual string Extra(DripOptions options) => "";
|
||||
|
||||
protected abstract IEnumerable<string> Content(IRandomSource random, ITrapChannel channel, DripOptions options);
|
||||
}
|
||||
@@ -11,12 +11,9 @@ public sealed record FileProfile(
|
||||
|
||||
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("wordpress-drip", "text/html; charset=UTF-8", path => LastSegment(path) == "wp-login.php", WordPressLogin),
|
||||
new("hikvision-drip", "application/xml", path => path.StartsWith("/sdk/", StringComparison.Ordinal) || path.StartsWith("/isapi/", StringComparison.Ordinal), Hikvision),
|
||||
new("env-drip", "text/plain; charset=utf-8", path => IsEnv(LastSegment(path)), Env),
|
||||
@@ -131,7 +128,7 @@ public static class FileContent
|
||||
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";
|
||||
yield return $" <url>\n <loc>https://example.com/{Pick(random, MazeContent.Roots)}/{LoremText.Phrase(random, 2).Replace(' ', '-')}</loc>\n <lastmod>{date}</lastmod>\n </url>\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,16 +167,6 @@ public static class FileContent
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
@@ -13,5 +13,5 @@ public sealed class FileDripStrategy(
|
||||
|
||||
public override string MediaType => profile.MediaType;
|
||||
|
||||
protected override IEnumerable<string> Content(IRandomSource random) => profile.Generate(random);
|
||||
protected override IEnumerable<string> Content(IRandomSource random, ITrapChannel channel, DripOptions options) => profile.Generate(random);
|
||||
}
|
||||
@@ -14,5 +14,5 @@ public sealed class HtmlDripStrategy(
|
||||
|
||||
public override string MediaType => "text/html; charset=utf-8";
|
||||
|
||||
protected override IEnumerable<string> Content(IRandomSource random) => HtmlContent.Generate(random);
|
||||
protected override IEnumerable<string> Content(IRandomSource random, ITrapChannel channel, DripOptions options) => HtmlContent.Generate(random);
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Flypaper.Core.Abstractions;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public static partial class MazeContent
|
||||
{
|
||||
public static readonly string[] Roots =
|
||||
["wiki", "docs", "blog", "backup", "archive", "files", "kb", "guides", "releases", "news", "help", "support", "products"];
|
||||
|
||||
private const int MaxDepth = 6;
|
||||
|
||||
public static bool IsDirectory(string? path) => path is { Length: > 1 } && path[^1] == '/';
|
||||
|
||||
public static IEnumerable<string> Page(IRandomSource random, string? path, string? host, int spacing)
|
||||
{
|
||||
var site = new Site(path, host);
|
||||
var siteName = site.Host ?? "Portal";
|
||||
var title = $"{LoremText.Capitalize(LoremText.Phrase(random, random.Next(2, 4)))} · {siteName}";
|
||||
|
||||
yield return $$"""
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{title}}</title>
|
||||
<style>body{margin:0 auto;max-width:52rem;padding:1.5rem;font:16px/1.6 system-ui,sans-serif}nav a{margin-right:1rem}</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>{{title}}</h1>
|
||||
|
||||
""";
|
||||
yield return Navigation(random, site);
|
||||
yield return "</header>\n<main>\n<h2>Related pages</h2>\n" + List(random, site, random.Next(7, 11));
|
||||
yield return "<p>";
|
||||
|
||||
var minFiller = Math.Max(1, spacing / 2);
|
||||
var maxFiller = Math.Max(minFiller, spacing * 3 / 2);
|
||||
while (true)
|
||||
{
|
||||
var words = Math.Max(2, random.Next(minFiller, maxFiller + 1) / 7);
|
||||
yield return $"{LoremText.Capitalize(LoremText.Phrase(random, words))}. {Anchor(random, site)} ";
|
||||
|
||||
switch (random.Next(0, 8))
|
||||
{
|
||||
case 0:
|
||||
yield return $"</p>\n{List(random, site, random.Next(3, 6))}<p>";
|
||||
break;
|
||||
case 1:
|
||||
yield return $"</p>\n<h3>{LoremText.Capitalize(LoremText.Phrase(random, random.Next(2, 6)))}</h3>\n<p>";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> Listing(IRandomSource random, string? path, string? host)
|
||||
{
|
||||
var site = new Site(path, host);
|
||||
var shown = WebUtility.HtmlEncode(site.DisplayPath);
|
||||
var wide = site.Section is "backup" or "files" or "archive";
|
||||
|
||||
yield return $$"""
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Index of {{shown}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Index of {{shown}}</h1>
|
||||
<pre>Name{{new string(' ', 46)}}Last modified Size
|
||||
<hr><a href="../">Parent Directory</a> -
|
||||
|
||||
""";
|
||||
|
||||
while (true)
|
||||
{
|
||||
yield return Entry(random, site, wide);
|
||||
}
|
||||
}
|
||||
|
||||
private static string Entry(IRandomSource random, Site site, bool wide)
|
||||
{
|
||||
var slug = Slug(random, site);
|
||||
var directory = random.Next(0, 3) == 0;
|
||||
var name = directory
|
||||
? slug + "/"
|
||||
: wide
|
||||
? $"{slug}-{Date(random)[..10]}{Pick(random, [".tar.gz", ".zip", ".bak", ".gz"])}"
|
||||
: $"{slug}{Pick(random, [".html", ".html", ".htm", ".txt"])}";
|
||||
var size = directory
|
||||
? "-"
|
||||
: random.Next(0, 4) == 0
|
||||
? string.Create(CultureInfo.InvariantCulture, $"{random.Next(1, 900)}.{random.Next(0, 10)}M")
|
||||
: string.Create(CultureInfo.InvariantCulture, $"{random.Next(1, 900)}K");
|
||||
|
||||
var href = random.Next(0, 5) == 0 ? Absolute(random, site) : name;
|
||||
var padding = new string(' ', Math.Max(1, 50 - name.Length));
|
||||
return $"<a href=\"{href}\">{name}</a>{padding}{Date(random)} {size}\n";
|
||||
}
|
||||
|
||||
private static string Date(IRandomSource random) =>
|
||||
string.Create(CultureInfo.InvariantCulture, $"{random.Next(2022, 2027)}-{random.Next(1, 13):00}-{random.Next(1, 29):00} {random.Next(0, 24):00}:{random.Next(0, 60):00}");
|
||||
|
||||
private static string Navigation(IRandomSource random, Site site)
|
||||
{
|
||||
var builder = new StringBuilder("<nav>\n");
|
||||
var count = random.Next(8, 12);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
builder.Append($"<a href=\"{Href(random, site, rootOnly: i % 2 == 0)}\">{LoremText.Capitalize(LoremText.Word(random))}</a>\n");
|
||||
}
|
||||
|
||||
return builder.Append("</nav>\n").ToString();
|
||||
}
|
||||
|
||||
private static string List(IRandomSource random, Site site, int items)
|
||||
{
|
||||
var builder = new StringBuilder("<ul>\n");
|
||||
for (var i = 0; i < items; i++)
|
||||
{
|
||||
builder.Append($"<li>{Anchor(random, site)}</li>\n");
|
||||
}
|
||||
|
||||
return builder.Append("</ul>\n").ToString();
|
||||
}
|
||||
|
||||
private static string Anchor(IRandomSource random, Site site) =>
|
||||
$"<a href=\"{Href(random, site)}\">{LoremText.Capitalize(LoremText.Phrase(random, random.Next(2, 5)))}</a>";
|
||||
|
||||
private static string Href(IRandomSource random, Site site, bool rootOnly = false)
|
||||
{
|
||||
if (rootOnly)
|
||||
{
|
||||
return $"/{Pick(random, Roots)}/";
|
||||
}
|
||||
|
||||
var kind = random.Next(0, 10);
|
||||
if (site.Section is null)
|
||||
{
|
||||
kind = 5 + kind % 5;
|
||||
}
|
||||
else if (site.Depth >= MaxDepth && kind is 0 or 3)
|
||||
{
|
||||
kind = 4;
|
||||
}
|
||||
|
||||
return kind switch
|
||||
{
|
||||
0 => $"{Slug(random, site)}/",
|
||||
1 or 2 => Slug(random, site),
|
||||
3 => $"{LoremText.Word(random)}/{Slug(random, site)}",
|
||||
4 => random.Next(0, 2) == 0 ? "../" : $"../{Slug(random, site)}",
|
||||
< 8 => Absolute(random, site),
|
||||
_ => site.Host is { } host ? $"http://{host}{Absolute(random, site)}" : Absolute(random, site)
|
||||
};
|
||||
}
|
||||
|
||||
private static string Absolute(IRandomSource random, Site site)
|
||||
{
|
||||
var root = site.Section is { } section && random.Next(0, 10) < 7 ? section : Pick(random, Roots);
|
||||
var slug = Slug(random, site);
|
||||
return root switch
|
||||
{
|
||||
"docs" => $"/docs/v{random.Next(1, 5)}/{slug}",
|
||||
"blog" => string.Create(CultureInfo.InvariantCulture, $"/blog/{random.Next(2022, 2027)}/{random.Next(1, 13):00}/{slug}"),
|
||||
"backup" => string.Create(CultureInfo.InvariantCulture, $"/backup/{random.Next(2022, 2027)}/{random.Next(1, 13):00}/"),
|
||||
_ => random.Next(0, 3) == 0 ? $"/{root}/{slug}/" : $"/{root}/{slug}"
|
||||
};
|
||||
}
|
||||
|
||||
private static string Slug(IRandomSource random, Site site)
|
||||
{
|
||||
var words = new List<string>();
|
||||
var count = random.Next(1, 4);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
words.Add(LoremText.Word(random));
|
||||
}
|
||||
|
||||
if (site.Topic is { } topic && random.Next(0, 4) == 0)
|
||||
{
|
||||
words[0] = topic;
|
||||
}
|
||||
|
||||
return string.Join('-', words);
|
||||
}
|
||||
|
||||
private static string Pick(IRandomSource random, string[] values) => values[random.Next(0, values.Length)];
|
||||
|
||||
[GeneratedRegex("^[A-Za-z0-9.-]{1,253}$")]
|
||||
private static partial Regex HostPattern();
|
||||
|
||||
[GeneratedRegex("^[a-z]{3,}$")]
|
||||
private static partial Regex TopicPattern();
|
||||
|
||||
private sealed class Site
|
||||
{
|
||||
public Site(string? path, string? host)
|
||||
{
|
||||
var normalized = (path ?? "/").ToLowerInvariant();
|
||||
var segments = normalized.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||||
var endsWithSlash = normalized.EndsWith('/');
|
||||
|
||||
Section = segments.Length > 0 && Array.IndexOf(Roots, segments[0]) >= 0 ? segments[0] : null;
|
||||
Depth = endsWithSlash ? segments.Length : Math.Max(0, segments.Length - 1);
|
||||
Host = host is not null && HostPattern().IsMatch(host) ? host : null;
|
||||
DisplayPath = normalized.Length > 200 ? normalized[..200] : normalized;
|
||||
|
||||
if (!endsWithSlash && segments.Length > 0)
|
||||
{
|
||||
var word = segments[^1].Split('-', '.', '_')[0];
|
||||
Topic = TopicPattern().IsMatch(word) ? word : null;
|
||||
}
|
||||
}
|
||||
|
||||
public string? Section { get; }
|
||||
|
||||
public int Depth { get; }
|
||||
|
||||
public string? Host { get; }
|
||||
|
||||
public string? Topic { get; }
|
||||
|
||||
public string DisplayPath { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Globalization;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public enum MazeKind
|
||||
{
|
||||
Page,
|
||||
Listing,
|
||||
Bait
|
||||
}
|
||||
|
||||
public sealed class MazeDripStrategy(
|
||||
MazeKind kind,
|
||||
IOptionsMonitor<DripOptions> options,
|
||||
IRandomSource random,
|
||||
TimeProvider timeProvider) : DripStrategy(options, random, timeProvider), IHoldExtension
|
||||
{
|
||||
public const string PageName = "maze-drip";
|
||||
|
||||
public const string ListingName = "index-drip";
|
||||
|
||||
public const string BaitName = "robots-bait";
|
||||
|
||||
public override string Name => kind switch
|
||||
{
|
||||
MazeKind.Listing => ListingName,
|
||||
MazeKind.Bait => BaitName,
|
||||
_ => PageName
|
||||
};
|
||||
|
||||
public override string MediaType => "text/html; charset=utf-8";
|
||||
|
||||
public TimeSpan MinimumHold => kind == MazeKind.Bait ? TimeSpan.FromHours(24) : TimeSpan.Zero;
|
||||
|
||||
protected override string Extra(DripOptions options) =>
|
||||
string.Create(CultureInfo.InvariantCulture, $" link {options.LinkSpacing}B");
|
||||
|
||||
protected override IEnumerable<string> Content(IRandomSource random, ITrapChannel channel, DripOptions options) =>
|
||||
kind == MazeKind.Listing || kind == MazeKind.Bait && MazeContent.IsDirectory(channel.Path)
|
||||
? MazeContent.Listing(random, channel.Path, channel.Host)
|
||||
: MazeContent.Page(random, channel.Path, channel.Host, options.LinkSpacing);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Text;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Core.Strategies;
|
||||
|
||||
public sealed class RobotsStaticStrategy(IOptionsMonitor<DripOptions> options) : ITarpitStrategy, IStrategyDetails
|
||||
{
|
||||
public const string StrategyName = "robots-static";
|
||||
|
||||
public const string Path = "/robots.txt";
|
||||
|
||||
public string Name => StrategyName;
|
||||
|
||||
public string MediaType => "text/plain; charset=utf-8";
|
||||
|
||||
public bool Supports(TrapProtocol protocol) => protocol == TrapProtocol.Http;
|
||||
|
||||
public string Describe() => Name;
|
||||
|
||||
public async Task RunAsync(ITrapChannel channel, CancellationToken ct) =>
|
||||
await channel.WriteAsync(Encoding.UTF8.GetBytes(Body()), ct);
|
||||
|
||||
public string Body() =>
|
||||
"User-agent: *\nDisallow: /\n" + string.Concat(BaitPaths.Parse(options.CurrentValue.BaitPaths).Select(bait => $"Disallow: {bait}\n"));
|
||||
}
|
||||
@@ -14,5 +14,5 @@ public sealed class TextDripStrategy(
|
||||
|
||||
public override string MediaType => "text/plain; charset=utf-8";
|
||||
|
||||
protected override IEnumerable<string> Content(IRandomSource random) => TextContent.Generate(random);
|
||||
protected override IEnumerable<string> Content(IRandomSource random, ITrapChannel channel, DripOptions options) => TextContent.Generate(random);
|
||||
}
|
||||
Reference in new issue
Block a user