219 lines
6.4 KiB
C#
219 lines
6.4 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Backend.Helper;
|
|
|
|
public static class FilterHelper
|
|
{
|
|
// https://stackoverflow.com/a/56116499
|
|
private const string DomainPattern = @"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$";
|
|
|
|
public static void GetDomain(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "CN=";
|
|
|
|
if (!data.Contains("subject:") || !data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.Length;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
|
|
bool isValid = ValidateUrl(result);
|
|
|
|
if (!isValid)
|
|
{
|
|
result = "";
|
|
}
|
|
}
|
|
|
|
public static void GetServerType(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "Server:";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.Length;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetHttpVersion(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "> HEAD / ";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.Length;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetALPN(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "* ALPN: server accepted ";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.Length;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetCertificateIssuerCountry(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "issuer: C=";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.IndexOf(';', StringComparison.Ordinal);
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetCertificateOrganizationName(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "O=";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.IndexOf(';', StringComparison.Ordinal);
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetIpV6(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "IPv6: ";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.Length;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetTlsVersion(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "SSL connection using ";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.IndexOf('/') - 1;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetConnection(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "< Connection: ";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.Length;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetEncoding(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "< Vary: ";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.Length;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetPublicKeyType(string data, out string result, string searchTerm)
|
|
{
|
|
result = "";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
int start = data.LastIndexOf(searchTerm, StringComparison.Ordinal) + searchTerm.Length;
|
|
int end = data.Length;
|
|
|
|
result = GetSubstring(data, start, end);
|
|
}
|
|
|
|
public static void GetKeyExchangeAlgorithm(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "* SSL connection using ";
|
|
|
|
if (!data.Contains(searchTerm) || string.IsNullOrWhiteSpace(data)) return;
|
|
|
|
string[] temp = data.Split('/');
|
|
|
|
switch (temp.Length)
|
|
{
|
|
case 0:
|
|
return;
|
|
case 2:
|
|
result = $"{temp[1].Trim()}";
|
|
return;
|
|
default:
|
|
result = temp[2].Trim();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public static void GetCipherSuite(string data, out string result)
|
|
{
|
|
result = "";
|
|
const string searchTerm = "* SSL connection using ";
|
|
|
|
if (!data.Contains(searchTerm)) return;
|
|
|
|
string[] temp = data.Split('/');
|
|
|
|
result = temp[1].Trim();
|
|
}
|
|
|
|
private static string GetSubstring(string input, int start, int end)
|
|
{
|
|
try
|
|
{
|
|
if (start >= end)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
return input.AsSpan().Slice(start, end - start).ToString();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine($"start index {start} end index {end}");
|
|
Console.WriteLine();
|
|
Console.WriteLine(e);
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
private static bool ValidateUrl(string url)
|
|
{
|
|
Regex rgx = new(DomainPattern);
|
|
return rgx.IsMatch(url);
|
|
}
|
|
} |