2020-06-02 18:54:22 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2014-10-21 22:38:06 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2014-05-25 06:17:59 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Common.Instrumentation
|
|
|
|
{
|
|
|
|
public class CleanseLogMessage
|
|
|
|
{
|
2023-06-09 17:25:27 +00:00
|
|
|
private static readonly Regex[] CleansingRules =
|
|
|
|
{
|
|
|
|
// Url
|
|
|
|
new (@"(?<=\?|&|: )(apikey|(?:access[-_]?)?token|passkey|auth|authkey|user|uid|api|[a-z_]*apikey|account|passwd)=(?<secret>[^&=]+?)(?=[ ""&=]|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"(?<=\?|&)[^=]*?(username|password)=(?<secret>[^&=]+?)(?= |&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"rss(24h)?\.torrentleech\.org/(?!rss)(?<secret>[0-9a-z]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"torrentleech\.org/rss/download/[0-9]+/(?<secret>[0-9a-z]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"iptorrents\.com/[/a-z0-9?&;]*?(?:[?&;](u|tp)=(?<secret>[^&=;]+?))+(?= |;|&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"/fetch/[a-z0-9]{32}/(?<secret>[a-z0-9]{32})", RegexOptions.Compiled),
|
|
|
|
new (@"getnzb.*?(?<=\?|&)(r)=(?<secret>[^&=]+?)(?= |&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"\b(\w*)?(_?(?<!use|get_)token|username|passwo?rd)=(?<secret>[^&=]+?)(?= |&|$|;)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2024-04-06 06:06:35 +00:00
|
|
|
new (@"-hd.me/torrent/[a-z0-9-]\.[0-9]+\.(?<secret>[0-9a-z]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// Trackers Announce Keys; Designed for Qbit Json; should work for all in theory
|
2023-10-12 01:59:47 +00:00
|
|
|
new (@"announce(\.php)?(/|%2f|%3fpasskey%3d)(?<secret>[a-z0-9]{16,})|(?<secret>[a-z0-9]{16,})(/|%2f)announce", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2022-01-23 21:36:08 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// Path
|
|
|
|
new (@"C:\\Users\\(?<secret>[^\""]+?)(\\|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"/(home|Users)/(?<secret>[^/""]+?)(/|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2021-03-01 02:19:03 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// NzbGet
|
|
|
|
new (@"""Name""\s*:\s*""[^""]*(username|password)""\s*,\s*""Value""\s*:\s*""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2014-07-20 12:14:47 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// Sabnzbd
|
|
|
|
new (@"""[^""]*(username|password|api_?key|nzb_key)""\s*:\s*""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"""email_(account|to|from|pwd)""\s*:\s*""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2014-05-13 17:57:46 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// uTorrent
|
|
|
|
new (@"\[""[a-z._]*(username|password)"",\d,""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"\[""(boss_key|boss_key_salt|proxy\.proxy)"",\d,""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2014-05-13 17:57:46 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// Deluge
|
|
|
|
new (@"auth.login\(""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2016-04-14 18:44:49 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// BroadcastheNet
|
|
|
|
new (@"""?method""?\s*:\s*""(getTorrents)"",\s*""?params""?\s*:\s*\[\s*""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"getTorrents\(""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
new (@"(?<=\?|&)(authkey|torrent_pass)=(?<secret>[^&=]+?)(?=""|&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2020-06-03 21:52:07 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// Plex
|
|
|
|
new (@"(?<=\?|&)(X-Plex-Client-Identifier|X-Plex-Token)=(?<secret>[^&=]+?)(?= |&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2022-01-23 21:36:08 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// Notifiarr
|
|
|
|
new (@"api/v[0-9]/notification/radarr/(?<secret>[\w-]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2022-10-16 11:36:50 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
// Discord
|
|
|
|
new (@"discord.com/api/webhooks/((?<secret>[\w-]+)/)?(?<secret>[\w-]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase)
|
|
|
|
};
|
2014-07-20 12:14:47 +00:00
|
|
|
|
2023-06-09 17:25:27 +00:00
|
|
|
private static readonly Regex CleanseRemoteIPRegex = new (@"(?:Auth-\w+(?<!Failure|Unauthorized) ip|from) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", RegexOptions.Compiled);
|
2020-06-02 18:54:22 +00:00
|
|
|
|
2014-05-25 06:17:59 +00:00
|
|
|
public static string Cleanse(string message)
|
|
|
|
{
|
|
|
|
if (message.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2014-07-20 12:14:47 +00:00
|
|
|
foreach (var regex in CleansingRules)
|
|
|
|
{
|
2014-10-21 22:38:06 +00:00
|
|
|
message = regex.Replace(message, m =>
|
2023-06-09 17:25:27 +00:00
|
|
|
{
|
|
|
|
var value = m.Value;
|
|
|
|
foreach (var capture in m.Groups["secret"].Captures.OfType<Capture>().Reverse())
|
2014-10-21 22:38:06 +00:00
|
|
|
{
|
2023-06-09 17:25:27 +00:00
|
|
|
value = value.Replace(capture.Index - m.Index, capture.Length, "(removed)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
});
|
2014-07-20 12:14:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 18:54:22 +00:00
|
|
|
message = CleanseRemoteIPRegex.Replace(message, CleanseRemoteIP);
|
|
|
|
|
2014-07-20 12:14:47 +00:00
|
|
|
return message;
|
2014-05-25 06:17:59 +00:00
|
|
|
}
|
2020-06-02 18:54:22 +00:00
|
|
|
|
|
|
|
private static string CleanseRemoteIP(Match match)
|
|
|
|
{
|
|
|
|
var group = match.Groups[1];
|
|
|
|
var valueIP = group.Value;
|
|
|
|
|
|
|
|
if (IPAddress.TryParse(valueIP, out var address) && !address.IsLocalAddress())
|
|
|
|
{
|
|
|
|
var prefix = match.Value.Substring(0, group.Index - match.Index);
|
|
|
|
var postfix = match.Value.Substring(group.Index + group.Length - match.Index);
|
|
|
|
var items = valueIP.Split('.');
|
|
|
|
|
|
|
|
return $"{prefix}{items[0]}.*.*.{items[3]}{postfix}";
|
|
|
|
}
|
|
|
|
|
|
|
|
return match.Value;
|
|
|
|
}
|
2014-05-25 06:17:59 +00:00
|
|
|
}
|
|
|
|
}
|