| | 1 | | using System; |
| | 2 | | using System.Collections.Concurrent; |
| | 3 | | using System.Net.Sockets; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Microsoft.Extensions.Logging; |
| | 6 | |
|
| | 7 | | namespace SharpHoundCommonLib.Processors |
| | 8 | | { |
| | 9 | | public class PortScanner |
| | 10 | | { |
| 0 | 11 | | private static readonly ConcurrentDictionary<PingCacheKey, bool> PortScanCache = new(); |
| | 12 | | private readonly ILogger _log; |
| | 13 | |
|
| 130 | 14 | | public PortScanner() |
| 130 | 15 | | { |
| 130 | 16 | | _log = Logging.LogProvider.CreateLogger("PortScanner"); |
| 130 | 17 | | } |
| | 18 | |
|
| 0 | 19 | | public PortScanner(ILogger log = null) |
| 0 | 20 | | { |
| 0 | 21 | | _log = log ?? Logging.LogProvider.CreateLogger("PortScanner"); |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Checks if a specified port is open on a host. Defaults to 445 (SMB) |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="hostname"></param> |
| | 28 | | /// <param name="port"></param> |
| | 29 | | /// <param name="timeout">Timeout in milliseconds</param> |
| | 30 | | /// <returns>True if port is open, otherwise false</returns> |
| | 31 | | public virtual async Task<bool> CheckPort(string hostname, int port = 445, int timeout = 10000) |
| 0 | 32 | | { |
| 0 | 33 | | var key = new PingCacheKey |
| 0 | 34 | | { |
| 0 | 35 | | Port = port, |
| 0 | 36 | | HostName = hostname |
| 0 | 37 | | }; |
| | 38 | |
|
| 0 | 39 | | if (PortScanCache.TryGetValue(key, out var status)) |
| 0 | 40 | | { |
| 0 | 41 | | _log.LogTrace("Port scan cache hit for {HostName}:{Port}: {Status}", hostname, port, status); |
| 0 | 42 | | return status; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | try |
| 0 | 46 | | { |
| 0 | 47 | | using var client = new TcpClient(); |
| 0 | 48 | | var ca = client.ConnectAsync(hostname, port); |
| 0 | 49 | | if (await Task.WhenAny(ca, Task.Delay(timeout)) == ca) |
| 0 | 50 | | { |
| 0 | 51 | | if (ca.IsFaulted) |
| 0 | 52 | | { |
| 0 | 53 | | _log.LogDebug("PortScan faulted on {Hostname}:{Port} with error {Error}", hostname, port, ca.Exc |
| 0 | 54 | | PortScanCache.TryAdd(key, false); |
| 0 | 55 | | return false; |
| | 56 | | } |
| 0 | 57 | | PortScanCache.TryAdd(key, true); |
| 0 | 58 | | return true; |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | _log.LogDebug("{HostName} did not respond to scan on port {Port} within {Timeout}ms", hostname, port, ti |
| 0 | 62 | | PortScanCache.TryAdd(key, false); |
| 0 | 63 | | return false; |
| | 64 | | } |
| 0 | 65 | | catch (Exception e) |
| 0 | 66 | | { |
| 0 | 67 | | _log.LogDebug(e, "Exception checking {Hostname}:{Port}", hostname, port); |
| 0 | 68 | | PortScanCache.TryAdd(key, false); |
| 0 | 69 | | return false; |
| | 70 | | } |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public static void ClearCache() |
| 0 | 74 | | { |
| 0 | 75 | | PortScanCache.Clear(); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | private class PingCacheKey |
| | 79 | | { |
| 0 | 80 | | internal string HostName { get; set; } |
| 0 | 81 | | internal int Port { get; set; } |
| | 82 | |
|
| | 83 | | protected bool Equals(PingCacheKey other) |
| 0 | 84 | | { |
| 0 | 85 | | return HostName == other.HostName && Port == other.Port; |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | public override bool Equals(object obj) |
| 0 | 89 | | { |
| 0 | 90 | | if (ReferenceEquals(null, obj)) return false; |
| 0 | 91 | | if (ReferenceEquals(this, obj)) return true; |
| 0 | 92 | | if (obj.GetType() != GetType()) return false; |
| 0 | 93 | | return Equals((PingCacheKey) obj); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public override int GetHashCode() |
| 0 | 97 | | { |
| | 98 | | unchecked |
| 0 | 99 | | { |
| 0 | 100 | | return (HostName.GetHashCode() * 397) ^ Port; |
| | 101 | | } |
| 0 | 102 | | } |
| | 103 | | } |
| | 104 | | } |
| | 105 | | } |