| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Runtime.InteropServices; |
| | 4 | | using System.Security; |
| | 5 | | using System.Security.Principal; |
| | 6 | | using SharpHoundRPC.Handles; |
| | 7 | | using SharpHoundRPC.Shared; |
| | 8 | |
|
| | 9 | | namespace SharpHoundRPC.SAMRPCNative |
| | 10 | | { |
| | 11 | | [SuppressUnmanagedCodeSecurity] |
| | 12 | | public static class SAMMethods |
| | 13 | | { |
| | 14 | | internal static (NtStatus status, SAMHandle handle) SamConnect(string serverName, |
| | 15 | | SAMEnums.SamAccessMasks requestedConnectAccess) |
| 0 | 16 | | { |
| 0 | 17 | | var us = new SharedStructs.UnicodeString(serverName); |
| 0 | 18 | | var objectAttributes = default(SAMStructs.ObjectAttributes); |
| | 19 | |
|
| 0 | 20 | | var status = SamConnect(ref us, out var handle, requestedConnectAccess, ref objectAttributes); |
| 0 | 21 | | objectAttributes.Dispose(); |
| | 22 | |
|
| 0 | 23 | | return (status, handle); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | [DllImport("samlib.dll", CharSet = CharSet.Unicode)] |
| | 27 | | private static extern NtStatus SamConnect( |
| | 28 | | ref SharedStructs.UnicodeString serverName, |
| | 29 | | out SAMHandle serverHandle, |
| | 30 | | SAMEnums.SamAccessMasks desiredAccess, |
| | 31 | | ref SAMStructs.ObjectAttributes objectAttributes |
| | 32 | | ); |
| | 33 | |
|
| | 34 | | internal static (NtStatus status, SAMPointer domainRids, int count) |
| | 35 | | SamEnumerateDomainsInSamServer(SAMHandle serverHandle) |
| 0 | 36 | | { |
| 0 | 37 | | var enumerationContext = 0; |
| 0 | 38 | | var status = |
| 0 | 39 | | SamEnumerateDomainsInSamServer(serverHandle, ref enumerationContext, out var domains, -1, |
| 0 | 40 | | out var count); |
| | 41 | |
|
| 0 | 42 | | return (status, domains, count); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | [DllImport("samlib.dll", CharSet = CharSet.Unicode)] |
| | 46 | | private static extern NtStatus SamEnumerateDomainsInSamServer( |
| | 47 | | SAMHandle serverHandle, |
| | 48 | | ref int enumerationContext, |
| | 49 | | out SAMPointer buffer, |
| | 50 | | int prefMaxLen, |
| | 51 | | out int count |
| | 52 | | ); |
| | 53 | |
|
| | 54 | | internal static (NtStatus status, SAMPointer securityIdentifier) SamLookupDomainInSamServer( |
| | 55 | | SAMHandle serverHandle, string name) |
| 0 | 56 | | { |
| 0 | 57 | | var us = new SharedStructs.UnicodeString(name); |
| 0 | 58 | | var status = SamLookupDomainInSamServer(serverHandle, ref us, out var sid); |
| | 59 | |
|
| 0 | 60 | | return (status, sid); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | [DllImport("samlib.dll", CharSet = CharSet.Unicode)] |
| | 64 | | private static extern NtStatus SamLookupDomainInSamServer( |
| | 65 | | SAMHandle serverHandle, |
| | 66 | | ref SharedStructs.UnicodeString name, |
| | 67 | | out SAMPointer sid); |
| | 68 | |
|
| | 69 | | internal static (NtStatus status, SAMHandle domainHandle) SamOpenDomain(SAMHandle serverHandle, |
| | 70 | | SAMEnums.DomainAccessMask desiredAccess, byte[] domainSid) |
| 0 | 71 | | { |
| 0 | 72 | | var status = SamOpenDomain(serverHandle, desiredAccess, domainSid, out var domainHandle); |
| 0 | 73 | | return (status, domainHandle); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | [DllImport("samlib.dll", CharSet = CharSet.Unicode)] |
| | 77 | | private static extern NtStatus SamOpenDomain( |
| | 78 | | SAMHandle serverHandle, |
| | 79 | | SAMEnums.DomainAccessMask desiredAccess, |
| | 80 | | [MarshalAs(UnmanagedType.LPArray)] byte[] domainSid, |
| | 81 | | out SAMHandle domainHandle |
| | 82 | | ); |
| | 83 | |
|
| | 84 | | internal static (NtStatus status, SAMSidArray members, int count) SamGetMembersInAlias(SAMHandle aliasHandle) |
| 0 | 85 | | { |
| 0 | 86 | | var status = SamGetMembersInAlias(aliasHandle, out var members, out var count); |
| 0 | 87 | | return (status, members, count); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | [DllImport("samlib.dll", CharSet = CharSet.Unicode)] |
| | 91 | | private static extern NtStatus SamGetMembersInAlias( |
| | 92 | | SAMHandle aliasHandle, |
| | 93 | | out SAMSidArray members, |
| | 94 | | out int count |
| | 95 | | ); |
| | 96 | |
|
| | 97 | | internal static (NtStatus status, SAMHandle aliasHandle) SamOpenAlias(SAMHandle domainHandle, |
| | 98 | | SAMEnums.AliasOpenFlags desiredAccess, int aliasId) |
| 0 | 99 | | { |
| 0 | 100 | | var status = SamOpenAlias(domainHandle, desiredAccess, aliasId, out var aliasHandle); |
| 0 | 101 | | return (status, aliasHandle); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | [DllImport("samlib.dll", CharSet = CharSet.Unicode)] |
| | 105 | | private static extern NtStatus SamOpenAlias( |
| | 106 | | SAMHandle domainHandle, |
| | 107 | | SAMEnums.AliasOpenFlags desiredAccess, |
| | 108 | | int aliasId, |
| | 109 | | out SAMHandle aliasHandle |
| | 110 | | ); |
| | 111 | |
|
| | 112 | | internal static (NtStatus status, SAMPointer pointer, int count) SamEnumerateAliasesInDomain( |
| | 113 | | SAMHandle domainHandle) |
| 0 | 114 | | { |
| 0 | 115 | | var enumerationContext = 0; |
| 0 | 116 | | var status = SamEnumerateAliasesInDomain(domainHandle, ref enumerationContext, out var buffer, -1, |
| 0 | 117 | | out var count); |
| 0 | 118 | | return (status, buffer, count); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | [DllImport("samlib.dll", CharSet = CharSet.Unicode)] |
| | 122 | | private static extern NtStatus SamEnumerateAliasesInDomain( |
| | 123 | | SAMHandle domainHandle, |
| | 124 | | ref int enumerationContext, |
| | 125 | | out SAMPointer buffer, |
| | 126 | | int prefMaxLen, |
| | 127 | | out int count |
| | 128 | | ); |
| | 129 | |
|
| | 130 | | internal static (NtStatus status, SAMPointer names, SAMPointer use) SamLookupIdsInDomain(SAMHandle domainHandle, |
| | 131 | | int rid) |
| 0 | 132 | | { |
| 0 | 133 | | var rids = new[] {rid}; |
| 0 | 134 | | var status = SamLookupIdsInDomain(domainHandle, 1, rids, out var names, out var use); |
| 0 | 135 | | return (status, names, use); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | [DllImport("samlib.dll", CharSet = CharSet.Unicode)] |
| | 139 | | private static extern NtStatus SamLookupIdsInDomain(SAMHandle domainHandle, |
| | 140 | | int count, |
| | 141 | | int[] rids, |
| | 142 | | out SAMPointer names, |
| | 143 | | out SAMPointer use); |
| | 144 | |
|
| | 145 | | #region Cleanup |
| | 146 | |
|
| | 147 | | [DllImport("samlib.dll")] |
| | 148 | | internal static extern NtStatus SamFreeMemory( |
| | 149 | | IntPtr handle |
| | 150 | | ); |
| | 151 | |
|
| | 152 | | [DllImport("samlib.dll")] |
| | 153 | | internal static extern NtStatus SamCloseHandle( |
| | 154 | | IntPtr handle |
| | 155 | | ); |
| | 156 | |
|
| | 157 | | #endregion |
| | 158 | | } |
| | 159 | | } |