2020-06-02 18:54:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
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
|
|
|
|
|
{
|
2017-08-11 13:51:28 +00:00
|
|
|
|
private static readonly Regex[] CleansingRules = new[]
|
2014-07-20 12:14:47 +00:00
|
|
|
|
{
|
|
|
|
|
// Url
|
2021-08-12 01:57:51 +00:00
|
|
|
|
new Regex(@"(?<=\?|&)(apikey|token|passkey|auth|authkey|user|uid|api|[a-z_]*apikey|account|passwd)=(?<secret>[^&=""]+?)(?=[ ""&=]|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2014-05-13 17:57:46 +00:00
|
|
|
|
new Regex(@"(?<=\?|&)[^=]*?(username|password)=(?<secret>[^&=]+?)(?= |&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2016-01-17 16:19:23 +00:00
|
|
|
|
new Regex(@"torrentleech\.org/(?!rss)(?<secret>[0-9a-z]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
|
new Regex(@"torrentleech\.org/rss/download/[0-9]+/(?<secret>[0-9a-z]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2014-10-21 22:38:06 +00:00
|
|
|
|
new Regex(@"iptorrents\.com/[/a-z0-9?&;]*?(?:[?&;](u|tp)=(?<secret>[^&=;]+?))+(?= |;|&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2015-12-27 19:32:35 +00:00
|
|
|
|
new Regex(@"/fetch/[a-z0-9]{32}/(?<secret>[a-z0-9]{32})", RegexOptions.Compiled),
|
2019-12-06 18:47:46 +00:00
|
|
|
|
new Regex(@"getnzb.*?(?<=\?|&)(r)=(?<secret>[^&=]+?)(?= |&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2021-08-03 04:43:28 +00:00
|
|
|
|
|
2021-02-24 23:22:58 +00:00
|
|
|
|
// Trackers Announce Keys; Designed for Qbit Json; should work for all in theory
|
|
|
|
|
new Regex(@"announce(\.php)?(/|%2f|%3fpasskey%3d)(?<secret>[a-z0-9]{16,})|(?<secret>[a-z0-9]{16,})(/|%2f)announce"),
|
2021-08-03 04:43:28 +00:00
|
|
|
|
|
2014-05-13 17:57:46 +00:00
|
|
|
|
// Path
|
2021-02-19 22:53:29 +00:00
|
|
|
|
new Regex(@"C:\\Users\\(?<secret>[^\""]+?)(\\|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
|
new Regex(@"/home/(?<secret>[^/""]+?)(/|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2014-05-13 17:57:46 +00:00
|
|
|
|
|
2014-07-20 12:14:47 +00:00
|
|
|
|
// NzbGet
|
|
|
|
|
new Regex(@"""Name""\s*:\s*""[^""]*(username|password)""\s*,\s*""Value""\s*:\s*""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
|
|
|
|
|
|
// Sabnzbd
|
|
|
|
|
new Regex(@"""[^""]*(username|password|api_?key|nzb_key)""\s*:\s*""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2014-05-13 17:57:46 +00:00
|
|
|
|
new Regex(@"""email_(account|to|from|pwd)""\s*:\s*""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
|
|
|
|
|
|
// uTorrent
|
2015-12-27 19:32:35 +00:00
|
|
|
|
new Regex(@"\[""[a-z._]*(username|password)"",\d,""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2014-05-13 17:57:46 +00:00
|
|
|
|
new Regex(@"\[""(boss_key|boss_key_salt|proxy\.proxy)"",\d,""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
|
|
2016-04-14 18:44:49 +00:00
|
|
|
|
// Deluge
|
|
|
|
|
new Regex(@"auth.login\(""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
|
|
2014-05-13 17:57:46 +00:00
|
|
|
|
// BroadcastheNet
|
|
|
|
|
new Regex(@"""?method""?\s*:\s*""(getTorrents)"",\s*""?params""?\s*:\s*\[\s*""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2016-04-14 18:44:49 +00:00
|
|
|
|
new Regex(@"getTorrents\(""(?<secret>[^""]+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
2020-06-03 21:52:07 +00:00
|
|
|
|
new Regex(@"(?<=\?|&)(authkey|torrent_pass)=(?<secret>[^&=]+?)(?=""|&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
|
|
|
|
|
|
// Plex
|
2021-12-24 21:06:31 +00:00
|
|
|
|
new Regex(@"(?<=\?|&)(X-Plex-Client-Identifier|X-Plex-Token)=(?<secret>[^&=]+?)(?= |&|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
|
|
|
|
|
|
|
|
|
|
// Webhooks
|
|
|
|
|
// Notifiarr
|
|
|
|
|
new Regex(@"api/v[0-9]/notification/sonarr/(?<secret>[\w-]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase)
|
2014-07-20 12:14:47 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-06-02 18:54:22 +00:00
|
|
|
|
private static readonly Regex CleanseRemoteIPRegex = new Regex(@"(?:Auth-\w+(?<!Failure|Unauthorized) ip|from) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", RegexOptions.Compiled);
|
|
|
|
|
|
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 =>
|
|
|
|
|
{
|
|
|
|
|
var value = m.Value;
|
|
|
|
|
foreach (var capture in m.Groups["secret"].Captures.OfType<Capture>().Reverse())
|
|
|
|
|
{
|
2016-02-18 18:11:39 +00:00
|
|
|
|
value = value.Replace(capture.Index - m.Index, capture.Length, "(removed)");
|
2014-10-21 22:38:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 valueAll = match.Value;
|
|
|
|
|
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('.');
|
|
|
|
|
|
2020-06-03 16:33:00 +00:00
|
|
|
|
return $"{prefix}{items[0]}.*.*.{items[3]}{postfix}";
|
2020-06-02 18:54:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return match.Value;
|
|
|
|
|
}
|
2014-05-25 06:17:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|