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