| | 1 | | using System.Collections.Concurrent; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Security.Principal; |
| | 5 | | using SharpHoundRPC.Handles; |
| | 6 | | using SharpHoundRPC.SAMRPCNative; |
| | 7 | | using SharpHoundRPC.Shared; |
| | 8 | |
|
| | 9 | | namespace SharpHoundRPC.Wrappers |
| | 10 | | { |
| | 11 | | public class SAMServer : SAMBase, ISAMServer |
| | 12 | | { |
| | 13 | | private readonly ConcurrentDictionary<string, SAMDomain> _domainHandleCache; |
| | 14 | | private SecurityIdentifier _cachedMachineSid; |
| | 15 | |
|
| 0 | 16 | | public SAMServer(SAMHandle handle, string computerName) : base(handle) |
| 0 | 17 | | { |
| 0 | 18 | | _domainHandleCache = new ConcurrentDictionary<string, SAMDomain>(); |
| 0 | 19 | | ComputerName = computerName; |
| 0 | 20 | | } |
| | 21 | |
|
| 0 | 22 | | public string ComputerName { get; } |
| | 23 | |
|
| | 24 | | public static Result<SAMServer> OpenServer(string computerName, SAMEnums.SamAccessMasks requestedConnectAccess = |
| | 25 | | SAMEnums.SamAccessMasks.SamServerConnect | |
| | 26 | | SAMEnums.SamAccessMasks |
| | 27 | | .SamServerEnumerateDomains | |
| | 28 | | SAMEnums.SamAccessMasks.SamServerLookupDomain) |
| 0 | 29 | | { |
| 0 | 30 | | var (status, handle) = SAMMethods.SamConnect(computerName, requestedConnectAccess); |
| | 31 | |
|
| 0 | 32 | | return status.IsError() |
| 0 | 33 | | ? status |
| 0 | 34 | | : new SAMServer(handle, computerName); |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public Result<IEnumerable<(string Name, int Rid)>> GetDomains() |
| 0 | 38 | | { |
| 0 | 39 | | var (status, rids, count) = SAMMethods.SamEnumerateDomainsInSamServer(Handle); |
| 0 | 40 | | if (status.IsError()) return status; |
| | 41 | |
|
| 0 | 42 | | var ret = Result<IEnumerable<(string Name, int Rid)>>.Ok(rids |
| 0 | 43 | | .GetEnumerable<SAMStructs.SamRidEnumeration>(count).Select(x => (x.Name.ToString(), x.Rid))); |
| 0 | 44 | | return ret; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public Result<SecurityIdentifier> LookupDomain(string name) |
| 0 | 48 | | { |
| 0 | 49 | | var (status, sid) = SAMMethods.SamLookupDomainInSamServer(Handle, name); |
| 0 | 50 | | using (sid) |
| 0 | 51 | | { |
| 0 | 52 | | return status.IsError() ? status : sid.GetData<SecurityIdentifier>(); |
| | 53 | | } |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public Result<SecurityIdentifier> GetMachineSid(string testName = null) |
| 0 | 57 | | { |
| 0 | 58 | | if (_cachedMachineSid != null) |
| 0 | 59 | | return _cachedMachineSid; |
| | 60 | |
|
| 0 | 61 | | SecurityIdentifier sid = null; |
| | 62 | |
|
| 0 | 63 | | if (testName != null) |
| 0 | 64 | | { |
| 0 | 65 | | var result = LookupDomain(testName); |
| 0 | 66 | | if (result.IsSuccess) sid = result.Value; |
| 0 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | if (sid == null) |
| 0 | 70 | | { |
| 0 | 71 | | var domainResult = GetDomains(); |
| 0 | 72 | | if (domainResult.IsSuccess) |
| 0 | 73 | | { |
| 0 | 74 | | var result = LookupDomain(domainResult.Value.FirstOrDefault().Name); |
| 0 | 75 | | if (result.IsSuccess) sid = result.Value; |
| 0 | 76 | | } |
| 0 | 77 | | } |
| | 78 | |
|
| 0 | 79 | | if (sid == null) return "Unable to get machine sid"; |
| 0 | 80 | | _cachedMachineSid = sid; |
| 0 | 81 | | return sid; |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | public Result<(string Name, SharedEnums.SidNameUse Type)> LookupPrincipalBySid( |
| | 85 | | SecurityIdentifier securityIdentifier) |
| 0 | 86 | | { |
| 0 | 87 | | var openDomainResult = OpenDomain(securityIdentifier); |
| 0 | 88 | | if (openDomainResult.IsFailed) return $"OpenDomain returned {openDomainResult.Status}"; |
| | 89 | |
|
| 0 | 90 | | var domain = openDomainResult.Value; |
| | 91 | |
|
| 0 | 92 | | return domain.LookupPrincipalByRid(securityIdentifier.Rid()); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public Result<ISAMDomain> OpenDomain(string domainName, SAMEnums.DomainAccessMask requestedDomainAccess = |
| | 96 | | SAMEnums.DomainAccessMask.Lookup | |
| | 97 | | SAMEnums.DomainAccessMask.ListAccounts) |
| 0 | 98 | | { |
| 0 | 99 | | var lookupResult = LookupDomain(domainName); |
| 0 | 100 | | if (lookupResult.IsFailed) return $"LookupDomain returned {lookupResult.Error}"; |
| | 101 | |
|
| 0 | 102 | | var sid = lookupResult.Value; |
| | 103 | |
|
| 0 | 104 | | if (_domainHandleCache.TryGetValue(sid.Value, out var domain)) return domain; |
| | 105 | |
|
| 0 | 106 | | var (status, domainHandle) = SAMMethods.SamOpenDomain(Handle, requestedDomainAccess, sid.GetBytes()); |
| 0 | 107 | | if (status.IsError()) return status; |
| | 108 | |
|
| 0 | 109 | | domain = new SAMDomain(domainHandle); |
| | 110 | |
|
| 0 | 111 | | _domainHandleCache.TryAdd(sid.Value, domain); |
| 0 | 112 | | return domain; |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public Result<ISAMDomain> OpenDomain(SecurityIdentifier securityIdentifier, |
| | 116 | | SAMEnums.DomainAccessMask requestedDomainAccess = |
| | 117 | | SAMEnums.DomainAccessMask.Lookup | |
| | 118 | | SAMEnums.DomainAccessMask.ListAccounts) |
| 0 | 119 | | { |
| 0 | 120 | | if (_domainHandleCache.TryGetValue(securityIdentifier.Value, out var domain)) return domain; |
| | 121 | |
|
| 0 | 122 | | var (status, domainHandle) = |
| 0 | 123 | | SAMMethods.SamOpenDomain(Handle, requestedDomainAccess, securityIdentifier.GetBytes()); |
| 0 | 124 | | if (status.IsError()) return status.ToString(); |
| | 125 | |
|
| 0 | 126 | | domain = new SAMDomain(domainHandle); |
| 0 | 127 | | _domainHandleCache.TryAdd(securityIdentifier.Value, domain); |
| 0 | 128 | | return domain; |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | protected override void Dispose(bool disposing) |
| 0 | 132 | | { |
| 0 | 133 | | foreach (var domainHandle in _domainHandleCache.Values) domainHandle.Dispose(); |
| 0 | 134 | | base.Dispose(disposing); |
| 0 | 135 | | } |
| | 136 | | } |
| | 137 | | } |