| | 1 | | using System; |
| | 2 | | using System.Threading.Tasks; |
| | 3 | | using Microsoft.Extensions.Logging; |
| | 4 | | using SharpHoundCommonLib.OutputTypes; |
| | 5 | |
|
| | 6 | | namespace SharpHoundCommonLib.Processors |
| | 7 | | { |
| | 8 | | public class ComputerAvailability |
| | 9 | | { |
| | 10 | | public delegate Task ComputerStatusDelegate(CSVComputerStatus status); |
| | 11 | |
|
| | 12 | | private readonly int _computerExpiryDays; |
| | 13 | | private readonly ILogger _log; |
| | 14 | | private readonly PortScanner _scanner; |
| | 15 | | private readonly int _scanTimeout; |
| | 16 | | private readonly bool _skipPasswordCheck; |
| | 17 | | private readonly bool _skipPortScan; |
| | 18 | |
|
| 2 | 19 | | public ComputerAvailability(int timeout = 10000, int computerExpiryDays = 60, bool skipPortScan = false, |
| 2 | 20 | | bool skipPasswordCheck = false, ILogger log = null) |
| 2 | 21 | | { |
| 2 | 22 | | _scanner = new PortScanner(); |
| 2 | 23 | | _scanTimeout = timeout; |
| 2 | 24 | | _skipPortScan = skipPortScan; |
| 2 | 25 | | _log = log ?? Logging.LogProvider.CreateLogger("CompAvail"); |
| 2 | 26 | | _computerExpiryDays = computerExpiryDays; |
| 2 | 27 | | _skipPasswordCheck = skipPasswordCheck; |
| 2 | 28 | | } |
| | 29 | |
|
| 2 | 30 | | public ComputerAvailability(PortScanner scanner, int timeout = 500, int computerExpiryDays = 60, |
| 2 | 31 | | bool skipPortScan = false, bool skipPasswordCheck = false, |
| 2 | 32 | | ILogger log = null) |
| 2 | 33 | | { |
| 2 | 34 | | _scanner = scanner; |
| 2 | 35 | | _scanTimeout = timeout; |
| 2 | 36 | | _skipPortScan = skipPortScan; |
| 2 | 37 | | _log = log ?? Logging.LogProvider.CreateLogger("CompAvail"); |
| 2 | 38 | | _computerExpiryDays = computerExpiryDays; |
| 2 | 39 | | _skipPasswordCheck = skipPasswordCheck; |
| 2 | 40 | | } |
| | 41 | |
|
| | 42 | | public event ComputerStatusDelegate ComputerStatusEvent; |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Helper function to use commonlib types for IsComputerAvailable |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="result"></param> |
| | 48 | | /// <param name="entry"></param> |
| | 49 | | /// <returns></returns> |
| | 50 | | public Task<ComputerStatus> IsComputerAvailable(ResolvedSearchResult result, IDirectoryObject entry) |
| 0 | 51 | | { |
| 0 | 52 | | var name = result.DisplayName; |
| 0 | 53 | | var os = entry.GetProperty(LDAPProperties.OperatingSystem); |
| 0 | 54 | | var pwdlastset = entry.GetProperty(LDAPProperties.PasswordLastSet); |
| 0 | 55 | | var lastLogon = entry.GetProperty(LDAPProperties.LastLogonTimestamp); |
| | 56 | |
|
| 0 | 57 | | return IsComputerAvailable(name, os, pwdlastset, lastLogon); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Checks if a computer is available for SharpHound enumeration using the following criteria: |
| | 62 | | /// The "operatingsystem" LDAP attribute must contain the string "Windows" |
| | 63 | | /// The "pwdlastset" LDAP attribute must be within 60 days of the current date by default. |
| | 64 | | /// Port 445 must be open to allow API calls to succeed |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="computerName">The computer to check availability for</param> |
| | 67 | | /// <param name="operatingSystem">The LDAP operatingsystem attribute value</param> |
| | 68 | | /// <param name="pwdLastSet">The LDAP pwdlastset attribute value</param> |
| | 69 | | /// <param name="lastLogon">The LDAP lastlogontimestamp attribute value</param> |
| | 70 | | /// <returns>A <cref>ComputerStatus</cref> object that represents the availability of the computer</returns> |
| | 71 | | public async Task<ComputerStatus> IsComputerAvailable(string computerName, string operatingSystem, |
| | 72 | | string pwdLastSet, string lastLogon) |
| 4 | 73 | | { |
| 4 | 74 | | if (operatingSystem != null && !operatingSystem.StartsWith("Windows", StringComparison.OrdinalIgnoreCase)) |
| 1 | 75 | | { |
| 1 | 76 | | _log.LogTrace("{ComputerName} is not available because operating system {OperatingSystem} is not valid", |
| 1 | 77 | | computerName, operatingSystem); |
| 1 | 78 | | await SendComputerStatus(new CSVComputerStatus |
| 1 | 79 | | { |
| 1 | 80 | | Status = ComputerStatus.NonWindowsOS, |
| 1 | 81 | | Task = "ComputerAvailability", |
| 1 | 82 | | ComputerName = computerName |
| 1 | 83 | | }); |
| 1 | 84 | | return new ComputerStatus |
| 1 | 85 | | { |
| 1 | 86 | | Connectable = false, |
| 1 | 87 | | Error = ComputerStatus.NonWindowsOS |
| 1 | 88 | | }; |
| | 89 | | } |
| | 90 | |
|
| 3 | 91 | | if (!_skipPasswordCheck && !IsComputerActive(pwdLastSet, lastLogon)) |
| 1 | 92 | | { |
| 1 | 93 | | _log.LogTrace( |
| 1 | 94 | | "{ComputerName} is not available because password last set and lastlogontimestamp are out of range", |
| 1 | 95 | | computerName); |
| 1 | 96 | | await SendComputerStatus(new CSVComputerStatus |
| 1 | 97 | | { |
| 1 | 98 | | Status = ComputerStatus.NotActive, |
| 1 | 99 | | Task = "ComputerAvailability", |
| 1 | 100 | | ComputerName = computerName |
| 1 | 101 | | }); |
| 1 | 102 | | return new ComputerStatus |
| 1 | 103 | | { |
| 1 | 104 | | Connectable = false, |
| 1 | 105 | | Error = ComputerStatus.NotActive |
| 1 | 106 | | }; |
| | 107 | | } |
| | 108 | |
|
| 2 | 109 | | if (_skipPortScan) |
| 0 | 110 | | return new ComputerStatus |
| 0 | 111 | | { |
| 0 | 112 | | Connectable = true, |
| 0 | 113 | | Error = null |
| 0 | 114 | | }; |
| | 115 | |
|
| 2 | 116 | | if (!await _scanner.CheckPort(computerName, timeout: _scanTimeout)) |
| 1 | 117 | | { |
| 1 | 118 | | _log.LogTrace("{ComputerName} is not available because port 445 is unavailable", computerName); |
| 1 | 119 | | await SendComputerStatus(new CSVComputerStatus |
| 1 | 120 | | { |
| 1 | 121 | | Status = ComputerStatus.PortNotOpen, |
| 1 | 122 | | Task = "ComputerAvailability", |
| 1 | 123 | | ComputerName = computerName |
| 1 | 124 | | }); |
| 1 | 125 | | return new ComputerStatus |
| 1 | 126 | | { |
| 1 | 127 | | Connectable = false, |
| 1 | 128 | | Error = ComputerStatus.PortNotOpen |
| 1 | 129 | | }; |
| | 130 | | } |
| | 131 | |
|
| 1 | 132 | | _log.LogTrace("{ComputerName} is available for enumeration", computerName); |
| | 133 | |
|
| 1 | 134 | | await SendComputerStatus(new CSVComputerStatus |
| 1 | 135 | | { |
| 1 | 136 | | Status = CSVComputerStatus.StatusSuccess, |
| 1 | 137 | | Task = "ComputerAvailability", |
| 1 | 138 | | ComputerName = computerName |
| 1 | 139 | | }); |
| | 140 | |
|
| 1 | 141 | | return new ComputerStatus |
| 1 | 142 | | { |
| 1 | 143 | | Connectable = true, |
| 1 | 144 | | Error = null |
| 1 | 145 | | }; |
| 4 | 146 | | } |
| | 147 | |
|
| | 148 | | /// <summary> |
| | 149 | | /// Checks if a computer's passwordlastset/lastlogontimestamp attributes are within a certain range |
| | 150 | | /// </summary> |
| | 151 | | /// <param name="pwdLastSet"></param> |
| | 152 | | /// <param name="lastLogonTimestamp"></param> |
| | 153 | | /// <returns></returns> |
| 3 | 154 | | private bool IsComputerActive(string pwdLastSet, string lastLogonTimestamp) { |
| 3 | 155 | | var passwordLastSet = Helpers.ConvertLdapTimeToLong(pwdLastSet); |
| 3 | 156 | | var lastLogonTimeStamp = Helpers.ConvertLdapTimeToLong(lastLogonTimestamp); |
| 3 | 157 | | var threshold = DateTime.Now.AddDays(_computerExpiryDays * -1).ToFileTimeUtc(); |
| | 158 | |
|
| 3 | 159 | | return passwordLastSet >= threshold || lastLogonTimeStamp >= threshold; |
| 3 | 160 | | } |
| | 161 | |
|
| | 162 | | private async Task SendComputerStatus(CSVComputerStatus status) |
| 4 | 163 | | { |
| 4 | 164 | | if (ComputerStatusEvent is not null) await ComputerStatusEvent.Invoke(status); |
| 4 | 165 | | } |
| | 166 | | } |
| | 167 | | } |