< Summary

Class:SharpHoundRPC.NetAPINative.NetAPIMethods
Assembly:SharpHoundRPC
File(s):D:\a\SharpHoundCommon\SharpHoundCommon\src\SharpHoundRPC\NetAPINative\NetAPIMethods.cs
Covered lines:4
Uncovered lines:26
Coverable lines:30
Total lines:104
Line coverage:13.3% (4 of 30)
Covered branches:1
Total branches:12
Branch coverage:8.3% (1 of 12)

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
NetWkstaUserEnum(...)0%400%
NetSessionEnum(...)0%400%
NetWkstaGetInfo(...)0%200%
DsGetDcName(...)50%2080%

File(s)

D:\a\SharpHoundCommon\SharpHoundCommon\src\SharpHoundRPC\NetAPINative\NetAPIMethods.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Runtime.InteropServices;
 5using SharpHoundRPC.Handles;
 6
 7namespace SharpHoundRPC.NetAPINative
 8{
 9    public static class NetAPIMethods
 10    {
 11        private const int NetWkstaUserEnumQueryLevel = 1;
 12        private const int NetSessionEnumLevel = 10;
 13        private const int NetWkstaGetInfoQueryLevel = 100;
 14
 15        [DllImport("netapi32.dll")]
 16        internal static extern NetAPIEnums.NetAPIStatus NetApiBufferFree(
 17            IntPtr buffer);
 18
 19        public static NetAPIResult<IEnumerable<NetWkstaUserEnumResults>> NetWkstaUserEnum(string computerName)
 020        {
 021            var resumeHandle = 0;
 022            var result = NetWkstaUserEnum(computerName, NetWkstaUserEnumQueryLevel, out var buffer, -1,
 023                out var entriesRead, out _, ref resumeHandle);
 24
 025            if (result != NetAPIEnums.NetAPIStatus.Success && result != NetAPIEnums.NetAPIStatus.ErrorMoreData)
 026                return result;
 27
 028            return NetAPIResult<IEnumerable<NetWkstaUserEnumResults>>.Ok(buffer
 029                .GetEnumerable<NetAPIStructs.WkstaUserInfo1>(entriesRead)
 030                .Select(x => new NetWkstaUserEnumResults(x.Username, x.LogonDomain)));
 031        }
 32
 33        [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 34        private static extern NetAPIEnums.NetAPIStatus NetWkstaUserEnum(
 35            string servername,
 36            int level,
 37            out NetAPIPointer buffer,
 38            int preferredMaxLength,
 39            out int entriesRead,
 40            out int totalEntries,
 41            ref int resumeHandle);
 42
 43        public static NetAPIResult<IEnumerable<NetSessionEnumResults>> NetSessionEnum(string computerName)
 044        {
 045            var resumeHandle = 0;
 046            var result = NetSessionEnum(computerName, null, null, NetSessionEnumLevel, out var buffer, -1,
 047                out var entriesRead, out _, ref resumeHandle);
 48
 049            if (result != NetAPIEnums.NetAPIStatus.Success && result != NetAPIEnums.NetAPIStatus.ErrorMoreData)
 050                return result;
 51
 052            return NetAPIResult<IEnumerable<NetSessionEnumResults>>.Ok(buffer
 053                .GetEnumerable<NetAPIStructs.SessionInfo10>(entriesRead)
 054                .Select(x => new NetSessionEnumResults(x.Username, x.CName)));
 055        }
 56
 57        [DllImport("NetAPI32.dll", SetLastError = true)]
 58        private static extern NetAPIEnums.NetAPIStatus NetSessionEnum(
 59            [MarshalAs(UnmanagedType.LPWStr)] string serverName,
 60            [MarshalAs(UnmanagedType.LPWStr)] string uncClientName,
 61            [MarshalAs(UnmanagedType.LPWStr)] string userName,
 62            int level,
 63            out NetAPIPointer buffer,
 64            int preferredMaxLength,
 65            out int entriesRead,
 66            out int totalEntries,
 67            ref int resumeHandle);
 68
 69        public static NetAPIResult<NetAPIStructs.WorkstationInfo100> NetWkstaGetInfo(string computerName)
 070        {
 071            var result = NetWkstaGetInfo(computerName, NetWkstaGetInfoQueryLevel, out var buffer);
 72
 073            if (result != NetAPIEnums.NetAPIStatus.Success) return result;
 74
 075            return buffer.GetData<NetAPIStructs.WorkstationInfo100>();
 076        }
 77
 78        [DllImport("netapi32.dll", SetLastError = true)]
 79        private static extern NetAPIEnums.NetAPIStatus NetWkstaGetInfo(
 80            [MarshalAs(UnmanagedType.LPWStr)] string serverName,
 81            uint level,
 82            out NetAPIPointer bufPtr);
 83
 84        public static NetAPIResult<NetAPIStructs.DomainControllerInfo> DsGetDcName(string computerName,
 85            string domainName, uint flags)
 186        {
 187            var result = DsGetDcName(computerName, domainName, null, null, flags, out var buffer);
 288            if (result != NetAPIEnums.NetAPIStatus.Success) return result;
 89
 090            return buffer.GetData<NetAPIStructs.DomainControllerInfo>();
 191        }
 92
 93        [DllImport("Netapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 94        private static extern NetAPIEnums.NetAPIStatus DsGetDcName
 95        (
 96            [MarshalAs(UnmanagedType.LPTStr)] string computerName,
 97            [MarshalAs(UnmanagedType.LPTStr)] string domainName,
 98            [In] NetAPIStructs.GuidClass domainGuid,
 99            [MarshalAs(UnmanagedType.LPTStr)] string siteName,
 100            uint flags,
 101            out NetAPIPointer pDomainControllerInfo
 102        );
 103    }
 104}