cursor rewrite + network checks

This commit is contained in:
carlospolop
2025-05-24 08:29:47 +02:00
parent 604580adbd
commit 1e7a90d29f
73 changed files with 4059 additions and 1018 deletions

View File

@@ -19,6 +19,7 @@ namespace winPEAS.Checks
{
public static bool IsDomainEnumeration = false;
public static bool IsNoColor = false;
public static bool DontCheckHostname = false;
public static bool Banner = true;
public static bool IsDebug = false;
public static bool IsLinpeas = false;
@@ -162,6 +163,11 @@ namespace winPEAS.Checks
IsNoColor = true;
}
if (string.Equals(arg, "dont-check-hostname", StringComparison.CurrentCultureIgnoreCase))
{
DontCheckHostname = true;
}
if (string.Equals(arg, "quiet", StringComparison.CurrentCultureIgnoreCase))
{
Banner = false;

View File

@@ -9,6 +9,7 @@ using winPEAS.Helpers.Extensions;
using winPEAS.Info.NetworkInfo;
using winPEAS.Info.NetworkInfo.Enums;
using winPEAS.Info.NetworkInfo.InternetSettings;
using winPEAS.Info.NetworkInfo.NetworkScanner;
namespace winPEAS.Checks
{
@@ -26,9 +27,9 @@ namespace winPEAS.Checks
public void PrintInfo(bool isDebug)
{
Beaprint.GreatPrint("Network Information");
new List<Action>
Beaprint.GreatPrint("Network Information");
var baseChecks = new List<Action>
{
PrintNetShares,
PrintMappedDrivesWMI,
@@ -38,7 +39,15 @@ namespace winPEAS.Checks
PrintFirewallRules,
PrintDNSCache,
PrintInternetSettings,
}.ForEach(action => CheckRunner.Run(action, isDebug));
PrintInternetConnectivity,
};
// Only create hostnameCheck list if we want to run it
var allChecks = !Checks.DontCheckHostname
? baseChecks.Concat(new List<Action> { () => PrintHostnameResolution().GetAwaiter().GetResult() })
: baseChecks;
allChecks.ForEach(action => CheckRunner.Run(action, isDebug));
}
private void PrintNetShares()
@@ -224,9 +233,9 @@ namespace winPEAS.Checks
foreach (var udpConnectionInfo in NetworkInfoHelper.GetUdpConnections(IPVersion.IPv4, processesByPid))
{
if (udpConnectionInfo.ProcessName == "dns") // Hundreds of them sometimes
{
continue;
if (udpConnectionInfo.ProcessName == "dns") // Hundreds of them sometimes
{
continue;
}
Beaprint.AnsiPrint(
@@ -260,9 +269,9 @@ namespace winPEAS.Checks
foreach (var udpConnectionInfo in NetworkInfoHelper.GetUdpConnections(IPVersion.IPv6, processesByPid))
{
if (udpConnectionInfo.ProcessName == "dns") // Hundreds of them sometimes
{
continue;
if (udpConnectionInfo.ProcessName == "dns") // Hundreds of them sometimes
{
continue;
}
Beaprint.AnsiPrint(
@@ -389,8 +398,8 @@ namespace winPEAS.Checks
var info = InternetSettings.GetInternetSettingsInfo();
Beaprint.ColorPrint(" General Settings", Beaprint.LBLUE);
Beaprint.NoColorPrint($" {"Hive",-10} {"Key",-40} {"Value"}");
Beaprint.NoColorPrint($" {"Hive",-10} {"Key",-40} {"Value"}");
foreach (var i in info.GeneralSettings)
{
Beaprint.NoColorPrint($" {i.Hive,-10} {i.ValueName,-40} {i.Value}");
@@ -410,9 +419,9 @@ namespace winPEAS.Checks
{
Beaprint.NoColorPrint($" {i.Hive,-10} {i.ValueName,-40} {i.Interpretation}");
}
}
Beaprint.ColorPrint("\n Zone Auth Settings", Beaprint.LBLUE);
}
Beaprint.ColorPrint("\n Zone Auth Settings", Beaprint.LBLUE);
if (info.ZoneAuthSettings.Count == 0)
{
Beaprint.NoColorPrint(" No Zone Auth Settings");
@@ -423,11 +432,96 @@ namespace winPEAS.Checks
{
Beaprint.NoColorPrint($" {i.Interpretation}");
}
}
}
}
catch (Exception ex)
{
}
}
private void PrintInternetConnectivity()
{
try
{
Beaprint.MainPrint("Internet Connectivity");
Beaprint.LinkPrint("", "Checking if internet access is possible via different methods");
var connectivityInfo = InternetConnectivity.CheckConnectivity();
// HTTP Access
Beaprint.AnsiPrint($" HTTP (80) Access: {(connectivityInfo.HttpAccess ? Beaprint.ansi_color_good + "Yes" + Beaprint.NOCOLOR : Beaprint.ansi_color_bad + "No" + Beaprint.NOCOLOR)}");
if (connectivityInfo.HttpAccess)
{
Beaprint.AnsiPrint($" Successful IP: {connectivityInfo.SuccessfulHttpIp}");
}
else if (!string.IsNullOrEmpty(connectivityInfo.HttpError))
{
Beaprint.AnsiPrint($" Error: {connectivityInfo.HttpError}");
}
// HTTPS Access
Beaprint.AnsiPrint($" HTTPS (443) Access: {(connectivityInfo.HttpsAccess ? Beaprint.ansi_color_good + "Yes" + Beaprint.NOCOLOR : Beaprint.ansi_color_bad + "No" + Beaprint.NOCOLOR)}");
if (connectivityInfo.HttpsAccess)
{
Beaprint.AnsiPrint($" Successful IP: {connectivityInfo.SuccessfulHttpsIp}");
}
else if (!string.IsNullOrEmpty(connectivityInfo.HttpsError))
{
Beaprint.AnsiPrint($" Error: {connectivityInfo.HttpsError}");
}
// DNS Access
Beaprint.AnsiPrint($" DNS (53) Access: {(connectivityInfo.DnsAccess ? Beaprint.ansi_color_good + "Yes" + Beaprint.NOCOLOR : Beaprint.ansi_color_bad + "No" + Beaprint.NOCOLOR)}");
if (connectivityInfo.DnsAccess)
{
Beaprint.AnsiPrint($" Successful IP: {connectivityInfo.SuccessfulDnsIp}");
}
else if (!string.IsNullOrEmpty(connectivityInfo.DnsError))
{
Beaprint.AnsiPrint($" Error: {connectivityInfo.DnsError}");
}
// ICMP Access
Beaprint.AnsiPrint($" ICMP (ping) Access: {(connectivityInfo.IcmpAccess ? Beaprint.ansi_color_good + "Yes" + Beaprint.NOCOLOR : Beaprint.ansi_color_bad + "No" + Beaprint.NOCOLOR)}");
if (connectivityInfo.IcmpAccess)
{
Beaprint.AnsiPrint($" Successful IP: {connectivityInfo.SuccessfulIcmpIp}");
}
else if (!string.IsNullOrEmpty(connectivityInfo.IcmpError))
{
Beaprint.AnsiPrint($" Error: {connectivityInfo.IcmpError}");
}
}
catch (Exception ex)
{
Beaprint.PrintException(ex.Message);
}
}
private async Task PrintHostnameResolution()
{
try
{
Beaprint.MainPrint("Hostname Resolution");
Beaprint.LinkPrint("", "Checking if the hostname can be resolved externally");
var resolutionInfo = await HostnameResolution.CheckResolution();
Beaprint.AnsiPrint($" Hostname: {resolutionInfo.Hostname}");
if (!string.IsNullOrEmpty(resolutionInfo.ExternalCheckResult))
{
Beaprint.AnsiPrint($" External Check Result: {resolutionInfo.ExternalCheckResult}");
}
else if (!string.IsNullOrEmpty(resolutionInfo.Error))
{
Beaprint.AnsiPrint($" {Beaprint.ansi_color_bad}{resolutionInfo.Error}{Beaprint.NOCOLOR}");
}
}
catch (Exception ex)
{
Beaprint.PrintException(ex.Message);
}
}
}
}

View File

@@ -142,6 +142,7 @@ namespace winPEAS.Helpers
Console.WriteLine(LCYAN + " searchpf" + GRAY + " Search credentials via regex also in Program Files folders" + NOCOLOR);
Console.WriteLine(LCYAN + " wait" + GRAY + " Wait for user input between checks" + NOCOLOR);
Console.WriteLine(LCYAN + " debug" + GRAY + " Display debugging information - memory usage, method execution time" + NOCOLOR);
Console.WriteLine(LCYAN + " dont-check-hostname" + GRAY + " Don't check the hostname externally" + NOCOLOR);
Console.WriteLine(LCYAN + " log[=logfile]" + GRAY + $" Log all output to file defined as logfile, or to \"{Checks.Checks.DefaultLogFile}\" if not specified" + NOCOLOR);
Console.WriteLine(LCYAN + " max-regex-file-size=1000000" + GRAY + $" Max file size (in Bytes) to search regex in. Default: {Checks.Checks.MaxRegexFileSize}B" + NOCOLOR);

View File

@@ -0,0 +1,69 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text;
namespace winPEAS.Info.NetworkInfo.NetworkScanner
{
public class HostnameResolutionInfo
{
public string Hostname { get; set; }
public string ExternalCheckResult { get; set; }
public string Error { get; set; }
}
public static class HostnameResolution
{
private const int INTERNET_SEARCH_TIMEOUT = 15;
private static readonly HttpClient httpClient = new HttpClient();
public static async Task<HostnameResolutionInfo> CheckResolution()
{
var result = new HostnameResolutionInfo();
try
{
// Get the current hostname
result.Hostname = Dns.GetHostName();
// Environment.MachineName if hostname empty
if (string.IsNullOrEmpty(result.Hostname))
{
result.Hostname = Environment.MachineName;
}
// Prepare the request
var content = new StringContent(
JsonSerializer.Serialize(new { hostname = result.Hostname }),
Encoding.UTF8,
"application/json"
);
httpClient.DefaultRequestHeaders.Add("User-Agent", "winpeas");
httpClient.Timeout = TimeSpan.FromSeconds(INTERNET_SEARCH_TIMEOUT);
// Make the request to the same endpoint as Linux version
var response = await httpClient.PostAsync(
"https://2e6ppt7izvuv66qmx2r3et2ufi0mxwqs.lambda-url.us-east-1.on.aws/",
content
);
if (response.IsSuccessStatusCode)
{
result.ExternalCheckResult = await response.Content.ReadAsStringAsync();
}
else
{
result.ExternalCheckResult = $"External check failed with status code: {response.StatusCode}";
}
}
catch (Exception ex)
{
result.Error = $"Error during hostname check: {ex.Message}";
}
return result;
}
}
}

View File

@@ -0,0 +1,257 @@
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
namespace winPEAS.Info.NetworkInfo.NetworkScanner
{
public class InternetConnectivityInfo
{
public bool HttpAccess { get; set; }
public bool HttpsAccess { get; set; }
public bool LambdaAccess { get; set; }
public bool DnsAccess { get; set; }
public bool IcmpAccess { get; set; }
public string HttpError { get; set; }
public string HttpsError { get; set; }
public string LambdaError { get; set; }
public string DnsError { get; set; }
public string IcmpError { get; set; }
public string SuccessfulHttpIp { get; set; }
public string SuccessfulHttpsIp { get; set; }
public string SuccessfulDnsIp { get; set; }
public string SuccessfulIcmpIp { get; set; }
}
public static class InternetConnectivity
{
private const int HTTP_TIMEOUT = 5000; // 5 seconds
private const int ICMP_TIMEOUT = 2000; // 2 seconds
private static readonly string[] TEST_IPS = new[] { "1.1.1.1", "8.8.8.8" }; // Cloudflare DNS, Google DNS
private const string LAMBDA_URL = "https://2e6ppt7izvuv66qmx2r3et2ufi0mxwqs.lambda-url.us-east-1.on.aws/";
private static bool TryHttpAccess(string ip, out string error)
{
try
{
using (var client = new WebClient())
{
client.Timeout = HTTP_TIMEOUT;
client.DownloadString($"http://{ip}");
error = null;
return true;
}
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
private static bool TryHttpsAccess(string ip, out string error)
{
try
{
using (var client = new WebClient())
{
client.Timeout = HTTP_TIMEOUT;
client.DownloadString($"https://{ip}");
error = null;
return true;
}
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
private static bool TryLambdaAccess(out string error)
{
try
{
using (var client = new WebClient())
{
client.Timeout = HTTP_TIMEOUT;
client.Headers.Add("User-Agent", "winpeas");
client.Headers.Add("Content-Type", "application/json");
client.DownloadString(LAMBDA_URL);
error = null;
return true;
}
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
private static bool TryDnsAccess(string ip, out string error)
{
try
{
using (var udpClient = new UdpClient())
{
// Set a timeout for the connection attempt
udpClient.Client.ReceiveTimeout = HTTP_TIMEOUT;
udpClient.Client.SendTimeout = HTTP_TIMEOUT;
// Create DNS server endpoint
var dnsServer = new IPEndPoint(IPAddress.Parse(ip), 53);
// Create a simple DNS query for google.com (type A record)
byte[] dnsQuery = new byte[] {
0x00, 0x01, // Transaction ID
0x01, 0x00, // Flags (Standard query)
0x00, 0x01, // Questions: 1
0x00, 0x00, // Answer RRs: 0
0x00, 0x00, // Authority RRs: 0
0x00, 0x00, // Additional RRs: 0
// google.com
0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00,
0x00, 0x01, // Type: A
0x00, 0x01 // Class: IN
};
// Send the DNS query
udpClient.Send(dnsQuery, dnsQuery.Length, dnsServer);
// Try to receive a response
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
byte[] response = udpClient.Receive(ref remoteEP);
// If we got a response, the DNS server is reachable
if (response != null && response.Length > 0)
{
error = null;
return true;
}
error = "No response received from DNS server";
return false;
}
}
catch (SocketException ex)
{
error = $"Socket error: {ex.Message}";
return false;
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
private static bool TryIcmpAccess(string ip, out string error)
{
try
{
using (var ping = new Ping())
{
var reply = ping.Send(ip, ICMP_TIMEOUT);
if (reply?.Status == IPStatus.Success)
{
error = null;
return true;
}
error = $"Ping failed with status: {reply?.Status}";
return false;
}
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
public static InternetConnectivityInfo CheckConnectivity()
{
var result = new InternetConnectivityInfo();
// Test HTTP (port 80) on each IP until success
foreach (var ip in TEST_IPS)
{
if (TryHttpAccess(ip, out string error))
{
result.HttpAccess = true;
result.SuccessfulHttpIp = ip;
break;
}
else if (ip == TEST_IPS[TEST_IPS.Length - 1]) // Last IP
{
result.HttpAccess = false;
result.HttpError = error;
}
}
// Test HTTPS (port 443) on each IP until success
foreach (var ip in TEST_IPS)
{
if (TryHttpsAccess(ip, out string error))
{
result.HttpsAccess = true;
result.SuccessfulHttpsIp = ip;
break;
}
else if (ip == TEST_IPS[TEST_IPS.Length - 1]) // Last IP
{
result.HttpsAccess = false;
result.HttpsError = error;
}
}
// Test Lambda URL
result.LambdaAccess = TryLambdaAccess(out string lambdaError);
if (!result.LambdaAccess)
{
result.LambdaError = lambdaError;
}
else
{
result.HttpsAccess = true;
}
// Test DNS on each IP until success
foreach (var ip in TEST_IPS)
{
if (TryDnsAccess(ip, out string error))
{
result.DnsAccess = true;
result.SuccessfulDnsIp = ip;
break;
}
else if (ip == TEST_IPS[TEST_IPS.Length - 1]) // Last IP
{
result.DnsAccess = false;
result.DnsError = error;
}
}
// Test ICMP (ping) on each IP until success
foreach (var ip in TEST_IPS)
{
if (TryIcmpAccess(ip, out string error))
{
result.IcmpAccess = true;
result.SuccessfulIcmpIp = ip;
break;
}
else if (ip == TEST_IPS[TEST_IPS.Length - 1]) // Last IP
{
result.IcmpAccess = false;
result.IcmpError = error;
}
}
return result;
}
}
}