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,116 @@
using System.Text.RegularExpressions;
using Flypaper.Core.Models;
namespace Flypaper.Core.Analysis;
public sealed record DetectedCommand(string Command, string? Server);
public static class CommandDetector
{
public const int Version = 1;
public const int MaxCommandLength = 500;
public const int MaxServerLength = 256;
private const int LeadingContext = 80;
private static readonly TimeSpan Timeout = TimeSpan.FromMilliseconds(200);
private static readonly Regex Fetch = Create(
@"\b(?:wget|curl|tftp|ftpget|nc|ncat|netcat)(?:\s|\$\{IFS\}|\$IFS)[^\n;|&`]{0,80}?(?:(?:https?|ftp|tftp)://(?<url>[^\s/;|&`'""<>()?#]+)[^\s;|&`'""<>()]*|\b(?<ip>(?:\d{1,3}\.){3}\d{1,3})(?:[ :](?<port>\d{1,5})\b)?)");
private static readonly Regex Execute = Create(
@"\|\s*(?:/bin/)?(?:ba)?sh\b|/bin/(?:ba)?sh\b|\bchmod\s+(?:\+x|[0-7]{3,4})\s|<\?php|\bpowershell(?:\.exe)?\s+-|\bbase64\s+(?:-d|--decode)\b|\brm\s+-rf\s");
public static DetectedCommand? Detect(string? target, RequestDetails? request) => Detect(target, request?.Headers, request?.Body);
public static DetectedCommand? Detect(string? target, string? headers, string? body)
{
DetectedCommand? first = null;
foreach (var part in Parts(target, headers, body))
{
var fetch = Match(Fetch, part);
if (fetch is not null)
{
return new DetectedCommand(Snippet(part, fetch.Index), Server(fetch));
}
if (first is null && Match(Execute, part) is { } execute)
{
first = new DetectedCommand(Snippet(part, execute.Index), null);
}
}
return first;
}
private static IEnumerable<string> Parts(string? target, string? headers, string? body)
{
var slash = target?.IndexOf('/') ?? -1;
if (slash >= 0)
{
var path = target![slash..];
var question = path.IndexOf('?');
yield return Uri.UnescapeDataString(question < 0 ? path : path[..question]);
if (question >= 0)
{
foreach (var parameter in path[(question + 1)..].Split('&'))
{
yield return Decode(parameter);
}
}
}
if (body is not null)
{
var isForm = !body.Contains('\n') && !body.StartsWith('{') && !body.StartsWith('<') && body.Contains('=');
if (isForm)
{
foreach (var parameter in body.Split('&'))
{
yield return Decode(parameter);
}
}
else
{
yield return body;
}
}
foreach (var line in (headers ?? "").Split('\n', StringSplitOptions.RemoveEmptyEntries))
{
yield return line;
}
}
private static string Decode(string value) => Uri.UnescapeDataString(value.Replace('+', ' '));
private static Match? Match(Regex regex, string text)
{
try
{
var match = regex.Match(text);
return match.Success ? match : null;
}
catch (RegexMatchTimeoutException)
{
return null;
}
}
private static string? Server(Match fetch)
{
var server = fetch.Groups["url"].Success
? fetch.Groups["url"].Value
: fetch.Groups["port"].Success ? $"{fetch.Groups["ip"].Value}:{fetch.Groups["port"].Value}" : fetch.Groups["ip"].Value;
return server.Length > MaxServerLength ? server[..MaxServerLength] : server;
}
private static string Snippet(string part, int index)
{
var start = part.Length <= MaxCommandLength ? 0 : Math.Max(0, index - LeadingContext);
var snippet = part.Substring(start, Math.Min(MaxCommandLength, part.Length - start));
return string.Concat(snippet.Select(c => char.IsControl(c) ? ' ' : c)).Trim();
}
private static Regex Create(string pattern) =>
new(pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.NonBacktracking, Timeout);
}
+16
View File
@@ -0,0 +1,16 @@
using System.Text.RegularExpressions;
namespace Flypaper.Core.Analysis;
public static partial class Defang
{
public static string Text(string value) => Host().Replace(Scheme().Replace(value, "hxx$1://"), match => match.Value.Replace(".", "[.]"));
public static string Server(string value) => value.Replace(".", "[.]");
[GeneratedRegex(@"\bhtt(ps?)://", RegexOptions.IgnoreCase)]
private static partial Regex Scheme();
[GeneratedRegex(@"(?<=://)[^\s/;|&'""]+|\b(?:\d{1,3}\.){3}\d{1,3}\b")]
private static partial Regex Host();
}