fix winpeas?

This commit is contained in:
carlospolop 2025-05-25 01:37:03 +02:00
parent b91334e5b3
commit c9282b4bdb

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
@ -7,79 +6,72 @@ using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
// ------------------------------------------------------------------
// Connectivity tester fixed timeouts + real HTTP/HTTPS endpoints
// ------------------------------------------------------------------
namespace winPEAS.Info.NetworkInfo namespace winPEAS.Info.NetworkInfo
{ {
// ───────────────────────────────────────────────────────────────
// POCO returned to the UI
// ───────────────────────────────────────────────────────────────
public class InternetConnectivityInfo public class InternetConnectivityInfo
{ {
public bool HttpAccess { get; set; } public bool HttpAccess { get; set; }
public bool HttpsAccess { get; set; } public bool HttpsAccess { get; set; }
public bool LambdaAccess { get; set; } public bool LambdaAccess { get; set; }
public bool DnsAccess { get; set; } public bool DnsAccess { get; set; }
public bool IcmpAccess { get; set; } public bool IcmpAccess { get; set; }
public string HttpError { get; set; } public string HttpError { get; set; }
public string HttpsError { get; set; } public string HttpsError { get; set; }
public string LambdaError { get; set; } public string LambdaError { get; set; }
public string DnsError { get; set; } public string DnsError { get; set; }
public string IcmpError { get; set; } public string IcmpError { get; set; }
public string SuccessfulHttpIp { get; set; } public string SuccessfulHttpIp { get; set; }
public string SuccessfulHttpsIp { get; set; } public string SuccessfulHttpsIp { get; set; }
public string SuccessfulDnsIp { get; set; } public string SuccessfulDnsIp { get; set; }
public string SuccessfulIcmpIp { get; set; } public string SuccessfulIcmpIp { get; set; }
} }
// ───────────────────────────────────────────────────────────────
// Connectivity tester
// ───────────────────────────────────────────────────────────────
public static class InternetConnectivity public static class InternetConnectivity
{ {
// 5seconds expressed in *milliseconds* to avoid unit mistakes private const int HTTP_TIMEOUT_MS = 5000; // 5s
private const int HTTP_TIMEOUT_MS = 5000; private const int ICMP_TIMEOUT_MS = 2000; // 2s
private const int ICMP_TIMEOUT_MS = 2000;
// IPs that really listen on 80/443 (example.com + Fastly CDN) // IPs that answer on 80 & 443
private static readonly string[] WEB_TEST_IPS = private static readonly string[] WEB_TEST_IPS =
{ "93.184.216.34", "151.101.1.69" }; { "93.184.216.34", "151.101.1.69" }; // example.com / Fastly
// DNS & ICMP targets stay the same public resolvers // Public DNS resolvers for DNS + ICMP checks
private static readonly string[] DNS_ICMP_IPS = private static readonly string[] DNS_ICMP_IPS =
{ "1.1.1.1", "8.8.8.8" }; { "1.1.1.1", "8.8.8.8" };
private const string LAMBDA_URL = private const string LAMBDA_URL =
"https://2e6ppt7izvuv66qmx2r3et2ufi0mxwqs.lambda-url.us-east-1.on.aws/"; "https://2e6ppt7izvuv66qmx2r3et2ufi0mxwqs.lambda-url.us-east-1.on.aws/";
// Reuse a single HttpClient to avoid socket exhaustion // Shared HttpClient (recommended pattern)
private static readonly HttpClient http = new HttpClient private static readonly HttpClient http = new HttpClient
{ {
Timeout = TimeSpan.FromMilliseconds(HTTP_TIMEOUT_MS) Timeout = TimeSpan.FromMilliseconds(HTTP_TIMEOUT_MS)
}; };
// ---------------------------------------------------------- // ─── Helpers ───────────────────────────────────────────────
// HTTP (port 80) private static bool TryHttpAccess(string ip, out string error) =>
// ---------------------------------------------------------- TryWebRequest($"http://{ip}", out error);
private static bool TryHttpAccess(string ip, out string error) =>
TryWebRequest($"http://{ip}", out error);
// ----------------------------------------------------------
// HTTPS (port 443)
// ----------------------------------------------------------
private static bool TryHttpsAccess(string ip, out string error) => private static bool TryHttpsAccess(string ip, out string error) =>
TryWebRequest($"https://{ip}", out error); TryWebRequest($"https://{ip}", out error);
// Common HTTP/HTTPS helper
private static bool TryWebRequest(string url, out string error) private static bool TryWebRequest(string url, out string error)
{ {
try try
{ {
using var cts = using var cts = new CancellationTokenSource(
new CancellationTokenSource(TimeSpan.FromMilliseconds(HTTP_TIMEOUT_MS)); TimeSpan.FromMilliseconds(HTTP_TIMEOUT_MS));
http.GetAsync(url, cts.Token).GetAwaiter().GetResult();
var resp = http.GetAsync(url, cts.Token).GetAwaiter().GetResult();
// Any response indicates that we reached the server
error = null; error = null;
return true; return true; // any response = connectivity
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -88,27 +80,22 @@ namespace winPEAS.Info.NetworkInfo
} }
} }
// ----------------------------------------------------------
// Lambda URL check (GET)
// ----------------------------------------------------------
private static bool TryLambdaAccess(out string error) private static bool TryLambdaAccess(out string error)
{ {
try try
{ {
using var cts = using var cts = new CancellationTokenSource(
new CancellationTokenSource(TimeSpan.FromMilliseconds(HTTP_TIMEOUT_MS)); TimeSpan.FromMilliseconds(HTTP_TIMEOUT_MS));
var req = new HttpRequestMessage(HttpMethod.Get, LAMBDA_URL); var req = new HttpRequestMessage(HttpMethod.Get, LAMBDA_URL);
req.Headers.UserAgent.ParseAdd("winpeas"); req.Headers.UserAgent.ParseAdd("winpeas");
req.Headers.Accept.Add( req.Headers.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json")); new MediaTypeWithQualityHeaderValue("application/json"));
var resp = http.SendAsync(req, cts.Token) var resp = http.SendAsync(req, cts.Token).GetAwaiter().GetResult();
.GetAwaiter().GetResult();
error = resp.IsSuccessStatusCode error = resp.IsSuccessStatusCode ? null :
? null $"HTTP {(int)resp.StatusCode}";
: $"HTTP {(int)resp.StatusCode}";
return error == null; return error == null;
} }
catch (Exception ex) catch (Exception ex)
@ -118,42 +105,35 @@ namespace winPEAS.Info.NetworkInfo
} }
} }
// ----------------------------------------------------------
// DNS test simple UDP query
// ----------------------------------------------------------
private static bool TryDnsAccess(string ip, out string error) private static bool TryDnsAccess(string ip, out string error)
{ {
try try
{ {
using var udp = new UdpClient(); using var udp = new UdpClient();
udp.Client.ReceiveTimeout = HTTP_TIMEOUT_MS; udp.Client.ReceiveTimeout = HTTP_TIMEOUT_MS;
udp.Client.SendTimeout = HTTP_TIMEOUT_MS; udp.Client.SendTimeout = HTTP_TIMEOUT_MS;
var server = new IPEndPoint(IPAddress.Parse(ip), 53); var server = new IPEndPoint(IPAddress.Parse(ip), 53);
// Minimal “A record for google.com” query // minimal query for google.com Arecord
byte[] query = new byte[] byte[] q = {
{
0x00,0x01, 0x01,0x00, 0x00,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x01, 0x01,0x00, 0x00,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00,
0x06,0x67,0x6f,0x6f,0x67,0x6c,0x65, 0x03,0x63,0x6f,0x6d, 0x00, 0x06,0x67,0x6f,0x6f,0x67,0x6c,0x65, 0x03,0x63,0x6f,0x6d, 0x00,
0x00,0x01, 0x00,0x01 0x00,0x01, 0x00,0x01
}; };
udp.Send(query, query.Length, server); udp.Send(q, q.Length, server);
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0); IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
byte[] response = udp.Receive(ref remote); byte[] resp = udp.Receive(ref remote);
error = response?.Length > 0 ? null : "No DNS response"; error = resp?.Length > 0 ? null : "No DNS response";
return error == null; return error == null;
} }
catch (SocketException ex) { error = ex.Message; return false; } catch (SocketException ex) { error = ex.Message; return false; }
catch (Exception ex) { error = ex.Message; return false; } catch (Exception ex) { error = ex.Message; return false; }
} }
// ----------------------------------------------------------
// ICMP (Ping)
// ----------------------------------------------------------
private static bool TryIcmpAccess(string ip, out string error) private static bool TryIcmpAccess(string ip, out string error)
{ {
try try
@ -169,68 +149,82 @@ namespace winPEAS.Info.NetworkInfo
catch (Exception ex) { error = ex.Message; return false; } catch (Exception ex) { error = ex.Message; return false; }
} }
// ---------------------------------------------------------- // ─── Main entry ───────────────────────────────────────────
// MAIN ENTRY
// ----------------------------------------------------------
public static InternetConnectivityInfo CheckConnectivity() public static InternetConnectivityInfo CheckConnectivity()
{ {
var info = new InternetConnectivityInfo(); var info = new InternetConnectivityInfo();
// --- HTTP / HTTPS ------------------------------------- // -------- HTTP / HTTPS --------------------------------
foreach (var ip in WEB_TEST_IPS) foreach (var ip in WEB_TEST_IPS)
{ {
if (!info.HttpAccess && // HTTP
TryHttpAccess(ip, out string eHttp)) if (!info.HttpAccess)
{ {
info.HttpAccess = true; string httpErr;
info.SuccessfulHttpIp = ip; if (TryHttpAccess(ip, out httpErr))
} {
else if (!info.HttpAccess) info.HttpAccess = true;
{ info.SuccessfulHttpIp = ip;
info.HttpError = eHttp; }
else
{
info.HttpError = httpErr;
}
} }
if (!info.HttpsAccess && // HTTPS
TryHttpsAccess(ip, out string eHttps)) if (!info.HttpsAccess)
{ {
info.HttpsAccess = true; string httpsErr;
info.SuccessfulHttpsIp = ip; if (TryHttpsAccess(ip, out httpsErr))
} {
else if (!info.HttpsAccess) info.HttpsAccess = true;
{ info.SuccessfulHttpsIp = ip;
info.HttpsError = eHttps; }
else
{
info.HttpsError = httpsErr;
}
} }
if (info.HttpAccess && info.HttpsAccess) break; if (info.HttpAccess && info.HttpsAccess) break;
} }
// --- Lambda ------------------------------------------ // -------- Lambda --------------------------------------
info.LambdaAccess = TryLambdaAccess(out string eLambda); info.LambdaAccess = TryLambdaAccess(out string lambdaErr);
if (!info.LambdaAccess) info.LambdaError = eLambda; if (!info.LambdaAccess) info.LambdaError = lambdaErr;
// --- DNS / ICMP -------------------------------------- // -------- DNS / ICMP ----------------------------------
foreach (var ip in DNS_ICMP_IPS) foreach (var ip in DNS_ICMP_IPS)
{ {
if (!info.DnsAccess && // DNS
TryDnsAccess(ip, out string eDns)) if (!info.DnsAccess)
{ {
info.DnsAccess = true; string dnsErr;
info.SuccessfulDnsIp = ip; if (TryDnsAccess(ip, out dnsErr))
} {
else if (!info.DnsAccess) info.DnsAccess = true;
{ info.SuccessfulDnsIp = ip;
info.DnsError = eDns; }
else
{
info.DnsError = dnsErr;
}
} }
if (!info.IcmpAccess && // ICMP
TryIcmpAccess(ip, out string ePing)) if (!info.IcmpAccess)
{ {
info.IcmpAccess = true; string pingErr;
info.SuccessfulIcmpIp = ip; if (TryIcmpAccess(ip, out pingErr))
} {
else if (!info.IcmpAccess) info.IcmpAccess = true;
{ info.SuccessfulIcmpIp = ip;
info.IcmpError = ePing; }
else
{
info.IcmpError = pingErr;
}
} }
if (info.DnsAccess && info.IcmpAccess) break; if (info.DnsAccess && info.IcmpAccess) break;