- 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
+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();