< Summary

Class:SharpHoundRPC.LSANative.LSAMethods
Assembly:SharpHoundRPC
File(s):D:\a\SharpHoundCommon\SharpHoundCommon\src\SharpHoundRPC\LSANative\LSAMethods.cs
Covered lines:0
Uncovered lines:43
Coverable lines:43
Total lines:135
Line coverage:0% (0 of 43)
Covered branches:0
Total branches:8
Branch coverage:0% (0 of 8)

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
LsaOpenPolicy(...)100%100%
LsaQueryInformationPolicy(...)100%100%
LsaEnumerateAccountsWithUserRight(...)100%100%
LsaLookupSids(...)100%100%
LsaLookupSids(...)0%800%

File(s)

D:\a\SharpHoundCommon\SharpHoundCommon\src\SharpHoundRPC\LSANative\LSAMethods.cs

#LineLine coverage
 1using System;
 2using System.Runtime.InteropServices;
 3using System.Security;
 4using System.Security.Principal;
 5using SharpHoundRPC.Handles;
 6using SharpHoundRPC.Shared;
 7
 8namespace SharpHoundRPC.LSANative
 9{
 10    [SuppressUnmanagedCodeSecurity]
 11    public class LSAMethods
 12    {
 13        internal static (NtStatus status, LSAHandle policyHandle) LsaOpenPolicy(string computerName,
 14            LSAEnums.LsaOpenMask desiredAccess)
 015        {
 016            var us = new SharedStructs.UnicodeString(computerName);
 017            var objectAttributes = default(LSAStructs.ObjectAttributes);
 018            var status = LsaOpenPolicy(ref us, ref objectAttributes, desiredAccess, out var policyHandle);
 19
 020            return (status, policyHandle);
 021        }
 22
 23        [DllImport("advapi32.dll")]
 24        private static extern NtStatus LsaOpenPolicy(
 25            ref SharedStructs.UnicodeString server,
 26            ref LSAStructs.ObjectAttributes objectAttributes,
 27            LSAEnums.LsaOpenMask desiredAccess,
 28            out LSAHandle policyHandle
 29        );
 30
 31        [DllImport("advapi32.dll")]
 32        internal static extern NtStatus LsaClose(
 33            IntPtr handle
 34        );
 35
 36        [DllImport("advapi32.dll")]
 37        internal static extern NtStatus LsaFreeMemory(
 38            IntPtr buffer
 39        );
 40
 41        internal static (NtStatus status, LSAPointer pointer) LsaQueryInformationPolicy(LSAHandle policyHandle,
 42            LSAEnums.LSAPolicyInformation policyInformation)
 043        {
 044            var status = LsaQueryInformationPolicy(policyHandle, policyInformation, out var pointer);
 45
 046            return (status, pointer);
 047        }
 48
 49        [DllImport("advapi32.dll")]
 50        private static extern NtStatus LsaQueryInformationPolicy(
 51            LSAHandle policyHandle,
 52            LSAEnums.LSAPolicyInformation policyInformation,
 53            out LSAPointer buffer
 54        );
 55
 56        internal static (NtStatus status, LSAPointer sids, int count) LsaEnumerateAccountsWithUserRight(
 57            LSAHandle policyHandle,
 58            string userRight)
 059        {
 060            var arr = new SharedStructs.UnicodeString[1];
 061            arr[0] = new SharedStructs.UnicodeString(userRight);
 62
 063            var status = LsaEnumerateAccountsWithUserRight(policyHandle, arr, out var sids, out var count);
 64
 065            return (status, sids, count);
 066        }
 67
 68        [DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true)]
 69        private static extern NtStatus LsaEnumerateAccountsWithUserRight(
 70            LSAHandle policyHandle,
 71            SharedStructs.UnicodeString[] userRight,
 72            out LSAPointer sids,
 73            out int count
 74        );
 75
 76        internal static (NtStatus status, LSAPointer referencedDomains, LSAPointer names, int count)
 77            LsaLookupSids(LSAHandle policyHandle,
 78                LSAPointer sids, int count)
 079        {
 080            var status = LsaLookupSids(policyHandle, count, sids, out var referencedDomains, out var names);
 081            return (status, referencedDomains, names, count);
 082        }
 83
 84        internal static (NtStatus status, LSAPointer referencedDomains, LSAPointer names, int count)
 85            LsaLookupSids(LSAHandle policyHandle,
 86                SecurityIdentifier[] sids)
 087        {
 088            var count = sids.Length;
 089            if (count == 0)
 090                return (NtStatus.StatusInvalidParameter, null, null, 0);
 91
 092            var gcHandles = new GCHandle[count];
 093            var pSids = new IntPtr[count];
 94
 095            for (var i = 0; i < count; i++)
 096            {
 097                var sid = sids[i];
 098                var b = new byte[sid.BinaryLength];
 099                sid.GetBinaryForm(b, 0);
 0100                gcHandles[i] = GCHandle.Alloc(b, GCHandleType.Pinned);
 0101                pSids[i] = gcHandles[i].AddrOfPinnedObject();
 0102            }
 103
 104            try
 0105            {
 0106                var status = LsaLookupSids(policyHandle, count, pSids, out var referencedDomains, out var names);
 0107                return (status, referencedDomains, names, count);
 108            }
 109            finally
 0110            {
 0111                foreach (var handle in gcHandles)
 0112                    if (handle.IsAllocated)
 0113                        handle.Free();
 0114            }
 0115        }
 116
 117        [DllImport("advapi32.dll")]
 118        private static extern NtStatus LsaLookupSids(
 119            LSAHandle policyHandle,
 120            int count,
 121            LSAPointer sidArray,
 122            out LSAPointer referencedDomains,
 123            out LSAPointer names
 124        );
 125
 126        [DllImport("advapi32.dll")]
 127        private static extern NtStatus LsaLookupSids(
 128            LSAHandle policyHandle,
 129            int count,
 130            [MarshalAs(UnmanagedType.LPArray)] IntPtr[] sidArray,
 131            out LSAPointer referencedDomains,
 132            out LSAPointer names
 133        );
 134    }
 135}