- Fix messed up output colors

This commit is contained in:
Stone_Red
2022-01-09 14:56:54 +01:00
parent 0b09e749bc
commit 48304bb030
4 changed files with 40 additions and 46 deletions
+4 -2
View File
@@ -1,5 +1,7 @@
using PacketDotNet;
using Stone_Red_Utilities.ConsoleExtentions;
using System.Net;
using System.Net.Sockets;
using System.Text;
@@ -49,7 +51,7 @@ internal class DomainHelper
}
catch (HttpRequestException ex)
{
Console.WriteLine($"error: {ex.Message}");
ConsoleExt.WriteLine($"error: {ex.Message}", ConsoleColor.Gray);
return null;
}
@@ -72,7 +74,7 @@ internal class DomainHelper
}
catch (SocketException ex)
{
Console.WriteLine($"error: {ex.Message} ({domain})");
ConsoleExt.WriteLine($"error: {ex.Message} ({domain})", ConsoleColor.Gray);
return null;
}
}
+1
View File
@@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="SharpPcap" Version="6.1.0" />
<PackageReference Include="Stone_Red-C-Sharp-Utilities" Version="1.0.3.1" />
</ItemGroup>
</Project>
+12 -18
View File
@@ -1,4 +1,6 @@
using System.Net;
using Stone_Red_Utilities.ConsoleExtentions;
using System.Net;
using System.Text.Json;
namespace FraudCapturer;
@@ -18,7 +20,7 @@ internal class IpHelper
}
catch (HttpRequestException ex)
{
Console.WriteLine($"error: {ex.Message}");
ConsoleExt.WriteLine($"error: {ex.Message}", ConsoleColor.Gray);
return null;
}
@@ -31,10 +33,10 @@ internal class IpHelper
if (statusValue.GetString() != "ok")
{
Console.Write(statusValue.GetString());
ConsoleExt.Write(statusValue.GetString(), ConsoleColor.Gray);
if (responseData.RootElement.TryGetProperty("message", out JsonElement messageValue))
{
Console.WriteLine($": {messageValue.GetString()}");
ConsoleExt.WriteLine($": {messageValue.GetString()}", ConsoleColor.Gray);
}
else
{
@@ -96,21 +98,13 @@ internal class IpHelper
}
byte[] ip = IPAddress.Parse(ipAdress).GetAddressBytes();
switch (ip[0])
return ip[0] switch
{
case 10:
case 127:
return true;
case 172:
return ip[1] >= 16 && ip[1] < 32;
case 192:
return ip[1] == 168;
default:
return false;
}
10 or 127 => true,
172 => ip[1] >= 16 && ip[1] < 32,
192 => ip[1] == 168,
_ => false,
};
}
public static bool IsLocalIpAddress(string host)
+23 -26
View File
@@ -3,6 +3,8 @@ using PacketDotNet;
using SharpPcap;
using Stone_Red_Utilities.ConsoleExtentions;
using System.Collections.Concurrent;
using System.Net;
@@ -38,10 +40,8 @@ public class Program
if (string.IsNullOrWhiteSpace(args.FirstOrDefault()))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("No proxycheck api key provided! You are limited to 100 IP checks per day. Get one for free at proxycheck.io.");
ConsoleExt.WriteLine("No proxycheck api key provided! You are limited to 100 IP checks per day. Get one for free at proxycheck.io.", ConsoleColor.Red);
Console.WriteLine();
Console.ResetColor();
}
else
{
@@ -93,7 +93,7 @@ public class Program
device.Open();
Console.WriteLine();
Console.WriteLine("-- Listening on {0}, hit 'Ctrl-C' to exit...", device.Description);
Console.WriteLine($"-- Listening on {device.Description}, hit 'Ctrl-C' to exit...");
// Start capture of packets
device.Capture();
@@ -139,7 +139,7 @@ public class Program
capturedIpsCache.Clear();
capturedDomainsCache.Clear();
File.WriteAllText(IpStorePath, string.Empty);
Console.WriteLine("Cleared cache");
ConsoleExt.WriteLine("Cleared cache", ConsoleColor.Gray);
}
//Check if a DNS packet contains a "dangerous" domain.
@@ -156,7 +156,7 @@ public class Program
await CheckIpAddress(remoteIpAddress, direction);
TimeSpan timeRemainingUntilCacheReset = new TimeSpan(0, 10, 0) - (DateTime.Now - lastCacheClear);
Console.WriteLine($"Next cache reset in {timeRemainingUntilCacheReset.Minutes} minute(s) and {timeRemainingUntilCacheReset.Seconds} second(s)");
ConsoleExt.WriteLine($"Next cache reset in {timeRemainingUntilCacheReset.Minutes} minute(s) and {timeRemainingUntilCacheReset.Seconds} second(s)", ConsoleColor.Gray);
}
}
}
@@ -167,43 +167,42 @@ public class Program
if (IpHelper.IsInternalIpAddress(remoteIpAddress.ToString()))
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"[{direction}] [Internal] {remoteIpAddress}");
ConsoleExt.WriteLine($"[{direction}] [Internal] {remoteIpAddress}", ConsoleColor.Cyan);
}
else if (ipInfo is not null)
{
bool block = false;
ConsoleColor consoleColor;
if (ipInfo.Risk >= 67)
{
FirewallHelper.BlockIp(remoteIpAddress);
Console.ForegroundColor = ConsoleColor.Red;
consoleColor = ConsoleColor.Red;
block = true;
}
else if (ipInfo.Risk >= 34 && ipInfo.IsProxy)
{
FirewallHelper.BlockIp(remoteIpAddress);
Console.ForegroundColor = ConsoleColor.DarkYellow;
consoleColor = ConsoleColor.DarkYellow;
block = true;
}
else if (ipInfo.IsProxy && ipInfo.Type != "VPN")
{
FirewallHelper.BlockIp(remoteIpAddress);
Console.ForegroundColor = ConsoleColor.DarkYellow;
consoleColor = ConsoleColor.DarkYellow;
block = true;
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
consoleColor = ConsoleColor.Green;
}
Console.WriteLine($"[{direction}] [Provider: {ipInfo.Provider}] [Risk: {ipInfo.Risk}] [Proxy: {ipInfo.IsProxy}] [Type: {ipInfo.Type}] [Block: {block}] {remoteIpAddress}");
ConsoleExt.WriteLine($"[{direction}] [Provider: {ipInfo.Provider}] [Risk: {ipInfo.Risk}] [Proxy: {ipInfo.IsProxy}] [Type: {ipInfo.Type}] [Block: {block}] {remoteIpAddress}", consoleColor);
}
else
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine($"[{direction}] [Invalid] {remoteIpAddress}");
ConsoleExt.WriteLine($"[{direction}] [Invalid] {remoteIpAddress}", ConsoleColor.Magenta);
}
Console.ResetColor();
}
private static async Task CheckDns(Packet packet, IPAddress remoteIpAddress, string direction)
@@ -231,49 +230,47 @@ public class Program
if (domainInfo is null)
{
Console.ForegroundColor = ConsoleColor.Magenta;
if (lastDomain != domain)
{
lastDomain = domain;
Console.WriteLine($"[{direction}] [Dns] [Invalid] [Domain: {domain}] {remoteIpAddress}");
ConsoleExt.WriteLine($"[{direction}] [Dns] [Invalid] [Domain: {domain}] {remoteIpAddress}", ConsoleColor.Magenta);
}
Console.ResetColor();
continue;
}
if (domainInfo.IsMatch == false)
{
Console.ForegroundColor = ConsoleColor.Green;
if (lastDomain != domain)
{
lastDomain = domain;
Console.WriteLine($"[{direction}] [Dns] [Domain: {domain}] [Type: Undetected] [Block: {block}] {remoteIpAddress}");
ConsoleExt.WriteLine($"[{direction}] [Dns] [Domain: {domain}] [Type: Undetected] [Block: {block}] {remoteIpAddress}", ConsoleColor.Green);
}
Console.ResetColor();
continue;
}
ConsoleColor consoleColor;
if (domainInfo.TrustRating >= 0.9)
{
FirewallHelper.BlockIp(domainInfo.IpAddress);
Console.ForegroundColor = ConsoleColor.Red;
consoleColor = ConsoleColor.Red;
block = true;
}
else if (domainInfo.TrustRating >= 0.5)
{
FirewallHelper.BlockIp(domainInfo.IpAddress);
Console.ForegroundColor = ConsoleColor.DarkYellow;
consoleColor = ConsoleColor.DarkYellow;
block = true;
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
consoleColor = ConsoleColor.Green;
}
if (lastDomain != domain)
{
lastDomain = domain;
Console.WriteLine($"[{direction}] [Dns] [Domain: {domain}] [Type: {domainInfo.Type}] [Source: {domainInfo.Source}] [Source Trust: {domainInfo.TrustRating * 100d}] [Block: {block}] {remoteIpAddress}");
ConsoleExt.WriteLine($"[{direction}] [Dns] [Domain: {domain}] [Type: {domainInfo.Type}] [Source: {domainInfo.Source}] [Source Trust: {domainInfo.TrustRating * 100d}] [Block: {block}] {remoteIpAddress}", consoleColor);
}
Console.ResetColor();