From 48304bb030ed7aad0791c33690bbb5c369fc9d74 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Sun, 9 Jan 2022 14:56:54 +0100 Subject: [PATCH] - Fix messed up output colors --- FraudCapturer/DomainHelper.cs | 6 ++-- FraudCapturer/FraudCapturer.csproj | 1 + FraudCapturer/IpHelper.cs | 30 ++++++++---------- FraudCapturer/Program.cs | 49 ++++++++++++++---------------- 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/FraudCapturer/DomainHelper.cs b/FraudCapturer/DomainHelper.cs index 53ed20d..18feb7e 100644 --- a/FraudCapturer/DomainHelper.cs +++ b/FraudCapturer/DomainHelper.cs @@ -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; } } diff --git a/FraudCapturer/FraudCapturer.csproj b/FraudCapturer/FraudCapturer.csproj index 6f4a3ce..ecd6d27 100644 --- a/FraudCapturer/FraudCapturer.csproj +++ b/FraudCapturer/FraudCapturer.csproj @@ -9,6 +9,7 @@ + diff --git a/FraudCapturer/IpHelper.cs b/FraudCapturer/IpHelper.cs index 2fb7915..f507b68 100644 --- a/FraudCapturer/IpHelper.cs +++ b/FraudCapturer/IpHelper.cs @@ -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) diff --git a/FraudCapturer/Program.cs b/FraudCapturer/Program.cs index a266179..9f7fa90 100644 --- a/FraudCapturer/Program.cs +++ b/FraudCapturer/Program.cs @@ -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();