-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathHostResolver.cs
More file actions
234 lines (203 loc) · 9.73 KB
/
HostResolver.cs
File metadata and controls
234 lines (203 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SharpHoundRPC.NetAPINative;
using SharpHoundRPC.PortScanner;
namespace SharpHoundCommonLib;
/// <summary>
/// Resolves hostnames and IP addresses to Active Directory SIDs.
/// Extracted from LdapUtils to give host resolution a single, focused home.
/// </summary>
internal class HostResolver {
private static readonly AdaptiveTimeout RequestNetBiosNameAdaptiveTimeout = new AdaptiveTimeout(
maxTimeout: TimeSpan.FromMinutes(1),
Logging.LogProvider.CreateLogger("HostResolver.RequestNETBIOSName"));
private static readonly AdaptiveTimeout CallNetWkstaGetInfoAdaptiveTimeout = new AdaptiveTimeout(
maxTimeout: TimeSpan.FromMinutes(2),
Logging.LogProvider.CreateLogger("HostResolver.NetWkstaGetInfo"));
private static readonly byte[] NameRequest = {
0x80, 0x94, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x43, 0x4b, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x00, 0x00, 0x21,
0x00, 0x01
};
private readonly ConcurrentDictionary<string, string> _hostResolutionMap =
new(StringComparer.OrdinalIgnoreCase);
private readonly PrincipalResolver _principalResolver;
private readonly IPortScanner _portScanner;
private readonly NativeMethods _nativeMethods;
private readonly ILogger _log;
internal HostResolver(PrincipalResolver principalResolver, IPortScanner portScanner,
NativeMethods nativeMethods, ILogger log) {
_principalResolver = principalResolver;
_portScanner = portScanner;
_nativeMethods = nativeMethods;
_log = log;
}
internal async Task<(bool Success, string SecurityIdentifier)> ResolveHostToSid(string host, string domain) {
var strippedHost = Helpers.StripServicePrincipalName(host).ToUpper().TrimEnd('$');
if (string.IsNullOrEmpty(strippedHost)) {
return (false, string.Empty);
}
if (_hostResolutionMap.TryGetValue(strippedHost, out var sid)) return (sid != null, sid);
// NetWkstaGetInfo is our most reliable indicator if successful
if (await GetWorkstationInfo(strippedHost) is (true, var workstationInfo)) {
var tempName = workstationInfo.ComputerName;
var tempDomain = workstationInfo.LanGroup;
_log.LogTrace("Get workstation info for {HostName} succeeded. Workstation {ComputerName} found.", host, tempName);
if (string.IsNullOrWhiteSpace(tempDomain)) {
tempDomain = domain;
}
if (!string.IsNullOrWhiteSpace(tempName)) {
tempName = $"{tempName}$".ToUpper();
if (await _principalResolver.ResolveAccountName(tempName, tempDomain) is (true, var principal)) {
_hostResolutionMap.TryAdd(strippedHost, principal.ObjectIdentifier);
return (true, principal.ObjectIdentifier);
}
}
}
// Try NETBIOS name via UDP socket
try {
var (requestNetBiosNameSuccess, netBiosName) =
await RequestNETBIOSNameFromComputerWithTimeout(strippedHost, domain);
if (requestNetBiosNameSuccess && !string.IsNullOrWhiteSpace(netBiosName)) {
var result = await _principalResolver.ResolveAccountName($"{netBiosName}$", domain);
if (result.Success) {
_hostResolutionMap.TryAdd(strippedHost, result.Principal.ObjectIdentifier);
return (true, result.Principal.ObjectIdentifier);
}
}
}
catch (TimeoutException) {
_log.LogDebug("RequestNETBIOSNameFromComputer timeout on host {Host}, domain {Domain}.", strippedHost, domain);
}
// Handle non-IP hostnames
if (!IPAddress.TryParse(strippedHost, out _)) {
if (strippedHost.Contains(".")) {
var split = strippedHost.Split('.');
var name = split[0];
var result = await _principalResolver.ResolveAccountName($"{name}$", domain);
if (result.Success) {
_hostResolutionMap.TryAdd(strippedHost, result.Principal.ObjectIdentifier);
return (true, result.Principal.ObjectIdentifier);
}
var tempDomain = string.Join(".", split.Skip(1).ToArray());
result = await _principalResolver.ResolveAccountName($"{name}$", tempDomain);
if (result.Success) {
_hostResolutionMap.TryAdd(strippedHost, result.Principal.ObjectIdentifier);
return (true, result.Principal.ObjectIdentifier);
}
}
else {
var result = await _principalResolver.ResolveAccountName($"{strippedHost}$", domain);
if (result.Success) {
_hostResolutionMap.TryAdd(strippedHost, result.Principal.ObjectIdentifier);
return (true, result.Principal.ObjectIdentifier);
}
}
}
try {
// Blocking External Call
var resolvedHostname = (await Dns.GetHostEntryAsync(strippedHost)).HostName;
var split = resolvedHostname.Split('.');
var name = split[0];
var result = await _principalResolver.ResolveAccountName($"{name}$", domain);
if (result.Success) {
_hostResolutionMap.TryAdd(strippedHost, result.Principal.ObjectIdentifier);
return (true, result.Principal.ObjectIdentifier);
}
var tempDomain = string.Join(".", split.Skip(1).ToArray());
result = await _principalResolver.ResolveAccountName($"{name}$", tempDomain);
if (result.Success) {
_hostResolutionMap.TryAdd(strippedHost, result.Principal.ObjectIdentifier);
return (true, result.Principal.ObjectIdentifier);
}
}
catch {
//pass
}
_hostResolutionMap.TryAdd(strippedHost, null);
return (false, "");
}
private async Task<(bool Success, NetAPIStructs.WorkstationInfo100 Info)> GetWorkstationInfo(string hostname) {
if (!await _portScanner.CheckPort(hostname)) {
_log.LogTrace("CheckPort returned false for {HostName}.", hostname);
return (false, default);
}
// Blocking External Call
var result = await CallNetWkstaGetInfoAdaptiveTimeout.ExecuteNetAPIWithTimeout(
(_) => _nativeMethods.CallNetWkstaGetInfo(hostname));
if (result.IsSuccess)
return (true, result.Value);
_log.LogError(result.Error);
return (false, default);
}
private static async Task<(bool Success, string NetBiosName)> RequestNETBIOSNameFromComputerWithTimeout(
string server, string domain) {
var result = await RequestNetBiosNameAdaptiveTimeout.ExecuteWithTimeout(
async (timeoutToken) => await RequestNETBIOSNameFromComputerAsync(server, domain, timeoutToken));
if (result.IsSuccess)
return (result.Value.Success, result.Value.NetBiosName);
throw new TimeoutException();
}
private static async Task<(bool Success, string NetBiosName)> RequestNETBIOSNameFromComputerAsync(
string server, string domain, CancellationToken cancellationToken = default) {
var receiveBuffer = new byte[1024];
var requestSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try {
requestSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
EndPoint remoteEndpoint;
if (IPAddress.TryParse(server, out var parsedAddress)) {
remoteEndpoint = new IPEndPoint(parsedAddress, 137);
}
else {
try {
IPAddress address;
if (server.Contains("."))
address = (await Dns.GetHostAddressesAsync(server))
.First(x => x.AddressFamily == AddressFamily.InterNetwork);
else
address = (await Dns.GetHostAddressesAsync($"{server}.{domain}"))[0];
if (address == null) {
return (false, null);
}
remoteEndpoint = new IPEndPoint(address, 137);
}
catch {
return (false, null);
}
}
var originEndpoint = new IPEndPoint(IPAddress.Any, 0);
cancellationToken.ThrowIfCancellationRequested();
// Blocking External Call
requestSocket.Bind(originEndpoint);
try {
// Blocking External Call
requestSocket.SendTo(NameRequest, remoteEndpoint);
cancellationToken.ThrowIfCancellationRequested();
// Blocking External Call
var receivedByteCount = requestSocket.ReceiveFrom(receiveBuffer, ref remoteEndpoint);
if (receivedByteCount >= 90) {
var netbios = new ASCIIEncoding().GetString(receiveBuffer, 57, 16).Trim('\0', ' ');
return (true, netbios);
}
return (false, null);
}
catch (SocketException) {
return (false, null);
}
}
finally {
requestSocket.Close();
}
}
}