| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using SharpHoundRPC.Handles; |
| | 5 | | using SharpHoundRPC.SAMRPCNative; |
| | 6 | | using SharpHoundRPC.Shared; |
| | 7 | |
|
| | 8 | | namespace SharpHoundRPC.Wrappers |
| | 9 | | { |
| | 10 | | public class SAMDomain : SAMBase, ISAMDomain |
| | 11 | | { |
| 0 | 12 | | public SAMDomain(SAMHandle handle) : base(handle) |
| 0 | 13 | | { |
| 0 | 14 | | } |
| | 15 | |
|
| | 16 | | public Result<(string Name, SharedEnums.SidNameUse Type)> LookupPrincipalByRid(int rid) |
| 0 | 17 | | { |
| 0 | 18 | | var (status, namePointer, usePointer) = SAMMethods.SamLookupIdsInDomain(Handle, rid); |
| | 19 | |
|
| 0 | 20 | | if (status.IsError()) return status; |
| | 21 | |
|
| 0 | 22 | | return (namePointer.GetData<SharedStructs.UnicodeString>().ToString(), |
| 0 | 23 | | (SharedEnums.SidNameUse) usePointer.GetData<int>()); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public Result<IEnumerable<(string Name, int Rid)>> GetAliases() |
| 0 | 27 | | { |
| 0 | 28 | | var (status, ridPointer, count) = SAMMethods.SamEnumerateAliasesInDomain(Handle); |
| | 29 | |
|
| 0 | 30 | | if (status.IsError()) |
| 0 | 31 | | { |
| 0 | 32 | | return status; |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | var ret = Result<IEnumerable<(string Name, int Rid)>>.Ok(ridPointer |
| 0 | 36 | | .GetEnumerable<SAMStructs.SamRidEnumeration>(count) |
| 0 | 37 | | .Select(x => (x.Name.ToString(), x.Rid))); |
| | 38 | |
|
| 0 | 39 | | return ret; |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public Result<ISAMAlias> OpenAlias(int rid, |
| | 43 | | SAMEnums.AliasOpenFlags desiredAccess = SAMEnums.AliasOpenFlags.ListMembers) |
| 0 | 44 | | { |
| 0 | 45 | | var (status, aliasHandle) = SAMMethods.SamOpenAlias(Handle, desiredAccess, rid); |
| 0 | 46 | | if (status.IsError()) return status; |
| | 47 | |
|
| 0 | 48 | | return new SAMAlias(aliasHandle); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public Result<ISAMAlias> OpenAlias(string name) |
| 0 | 52 | | { |
| 0 | 53 | | var getAliasesResult = GetAliases(); |
| 0 | 54 | | if (getAliasesResult.IsFailed) return getAliasesResult.Status; |
| | 55 | |
|
| 0 | 56 | | foreach (var alias in getAliasesResult.Value) |
| 0 | 57 | | if (alias.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) |
| 0 | 58 | | return OpenAlias(alias.Rid); |
| | 59 | |
|
| 0 | 60 | | return $"Alias {name} was not found"; |
| 0 | 61 | | } |
| | 62 | | } |
| | 63 | | } |