Answer first visits to / and favicon with an instant WordPress lure
Unknown IPs get a small WordPress-looking page or icon and are not held; later requests are trapped as before. Switch: Flypaper__Drip__Lure. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2437880013
commit
4b946453fb
18 files changed
+360
-2
No files matched your search
@@ -0,0 +1,67 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Core.Runtime;
|
||||
using Flypaper.Core.Strategies;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Core;
|
||||
|
||||
public sealed class LureTests
|
||||
{
|
||||
private static readonly TrapSession Session = new(Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.9"), DateTimeOffset.UtcNow);
|
||||
|
||||
[Theory]
|
||||
[InlineData("/", true, "lure-page")]
|
||||
[InlineData("/favicon.ico", true, "lure-icon")]
|
||||
[InlineData("/", false, "html-drip")]
|
||||
[InlineData("/.env", true, "env-drip")]
|
||||
[InlineData("/wp-login.php", true, "wordpress-drip")]
|
||||
public void Lures_only_new_ips_on_start_page_and_favicon(string path, bool isNewIp, string expected) =>
|
||||
Selector(new DripOptions { LinkSpacing = 0 }).Select(new TrapContext(Session, "GET " + path, null, path, isNewIp)).ShouldNotBeNull().Name.ShouldBe(expected);
|
||||
|
||||
[Fact]
|
||||
public void Switch_turns_the_lure_off() =>
|
||||
Selector(new DripOptions { LinkSpacing = 0, Lure = false })
|
||||
.Select(new TrapContext(Session, "GET /", null, "/", true)).ShouldNotBeNull().Name.ShouldBe("html-drip");
|
||||
|
||||
[Fact]
|
||||
public async Task Lure_page_is_one_consistent_wordpress_response()
|
||||
{
|
||||
var channel = new FakeTrapChannel();
|
||||
var strategy = LureStrategy.Page();
|
||||
|
||||
await strategy.RunAsync(channel, default);
|
||||
|
||||
System.Text.Encoding.UTF8.GetString(channel.Writes.SelectMany(w => w).ToArray()).ShouldContain("WordPress 6.4.3");
|
||||
strategy.MediaType.ShouldStartWith("text/html");
|
||||
strategy.Headers["X-Powered-By"].ShouldStartWith("PHP/");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Lure_icon_is_a_valid_ico_file()
|
||||
{
|
||||
var icon = LureContent.Icon();
|
||||
|
||||
icon[..4].ShouldBe([0, 0, 1, 0]);
|
||||
BitConverter.ToInt32(icon, 14).ShouldBe(icon.Length - 22);
|
||||
LureStrategy.Icon().MediaType.ShouldBe("image/x-icon");
|
||||
}
|
||||
|
||||
private static ConfiguredStrategySelector Selector(DripOptions dripOptions)
|
||||
{
|
||||
var options = TestOptions.Monitor(dripOptions);
|
||||
var random = new SeededRandomSource(5);
|
||||
return new ConfiguredStrategySelector(
|
||||
[
|
||||
new HtmlDripStrategy(options, random, TimeProvider.System),
|
||||
new FileDripStrategy(FileContent.Profiles.First(p => p.Name == "env-drip"), options, random, TimeProvider.System),
|
||||
new FileDripStrategy(FileContent.Profiles.First(p => p.Name == "wordpress-drip"), options, random, TimeProvider.System),
|
||||
LureStrategy.Page(),
|
||||
LureStrategy.Icon()
|
||||
],
|
||||
options,
|
||||
random);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NSubstitute;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Http;
|
||||
|
||||
public sealed class LureTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Unknown_ip_gets_the_lure_page_with_matching_headers_and_is_not_held()
|
||||
{
|
||||
var (body, headers, sink) = await SendAsync(known: false, "/");
|
||||
|
||||
body.ShouldContain("WordPress");
|
||||
headers["X-Powered-By"].ToString().ShouldStartWith("PHP/");
|
||||
headers.Server.ToString().ShouldBe("nginx");
|
||||
sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Strategy == "lure-page" && e.Outcome == TrapOutcome.Completed));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Known_ip_gets_no_lure()
|
||||
{
|
||||
var (_, headers, sink) = await SendAsync(known: true, "/", cancelAfter: TimeSpan.FromMilliseconds(100));
|
||||
|
||||
headers.ContainsKey("X-Powered-By").ShouldBeFalse();
|
||||
sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Strategy != "lure-page"));
|
||||
}
|
||||
|
||||
private static async Task<(string Body, IHeaderDictionary Headers, ITrapEventSink Sink)> SendAsync(bool known, string path, TimeSpan? cancelAfter = null)
|
||||
{
|
||||
var lookup = Substitute.For<IKnownIpLookup>();
|
||||
lookup.HasSeenAsync(Arg.Any<IPAddress>(), Arg.Any<CancellationToken>()).Returns(known);
|
||||
var sink = Substitute.For<ITrapEventSink>();
|
||||
var configuration = new ConfigurationBuilder().Build();
|
||||
var services = new ServiceCollection()
|
||||
.AddLogging()
|
||||
.AddSingleton(sink)
|
||||
.AddSingleton(lookup)
|
||||
.AddFlypaperCore(configuration)
|
||||
.AddFlypaperHttp(configuration)
|
||||
.BuildServiceProvider();
|
||||
|
||||
var app = new ApplicationBuilder(services);
|
||||
app.UseFlypaper();
|
||||
var body = new MemoryStream();
|
||||
using var cts = new CancellationTokenSource();
|
||||
var httpContext = new DefaultHttpContext { RequestServices = services, RequestAborted = cts.Token };
|
||||
httpContext.Connection.RemoteIpAddress = IPAddress.Parse("203.0.113.9");
|
||||
httpContext.Connection.LocalPort = 8080;
|
||||
httpContext.Request.Method = "GET";
|
||||
httpContext.Request.Host = new HostString("example.com");
|
||||
httpContext.Request.Path = path;
|
||||
httpContext.Response.Body = body;
|
||||
if (cancelAfter is { } delay)
|
||||
{
|
||||
cts.CancelAfter(delay);
|
||||
}
|
||||
|
||||
await app.Build()(httpContext);
|
||||
return (Encoding.UTF8.GetString(body.ToArray()), httpContext.Response.Headers, sink);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Flypaper.Storage.Queries;
|
||||
using Flypaper.Tests.Mcp;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Storage;
|
||||
|
||||
public sealed class KnownIpLookupTests : IDisposable
|
||||
{
|
||||
private readonly McpTestData _data = new();
|
||||
|
||||
[Fact]
|
||||
public async Task Knows_only_ips_with_a_stored_catch()
|
||||
{
|
||||
using (var db = _data.Factory.CreateDbContext())
|
||||
{
|
||||
var started = McpTestData.Now.AddHours(-2);
|
||||
db.TrapEvents.Add(TrapEventEntity.From(new TrapEvent(
|
||||
Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("2001:db8::7"), "lure-page", started, started.AddSeconds(1), 100, TrapOutcome.Completed, "GET /", null)));
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
var lookup = new KnownIpLookup(_data.Factory);
|
||||
|
||||
(await lookup.HasSeenAsync(IPAddress.Parse("2001:db8::7"), default)).ShouldBeTrue();
|
||||
(await lookup.HasSeenAsync(IPAddress.Parse("203.0.113.99"), default)).ShouldBeFalse();
|
||||
}
|
||||
|
||||
public void Dispose() => _data.Dispose();
|
||||
}
|
||||
@@ -47,6 +47,7 @@ public sealed class StrategyNamesTests
|
||||
[
|
||||
MazeDripStrategy.PageName, MazeDripStrategy.ListingName, MazeDripStrategy.BaitName,
|
||||
HtmlDripStrategy.StrategyName, TextDripStrategy.StrategyName, RobotsStaticStrategy.StrategyName,
|
||||
LureStrategy.PageName, LureStrategy.IconName,
|
||||
.. FileContent.Profiles.Select(profile => profile.Name)
|
||||
];
|
||||
|
||||
|
||||
Reference in new issue
Block a user