mirror of
https://github.com/GhostPack/SharpDPAPI
synced 2026-06-08 11:11:23 +00:00
195 lines
7.1 KiB
C#
Executable File
195 lines
7.1 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Principal;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace SharpDPAPI
|
|
{
|
|
public class Interop
|
|
{
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct LSA_UNICODE_STRING : IDisposable
|
|
{
|
|
public ushort Length;
|
|
public ushort MaximumLength;
|
|
public IntPtr buffer;
|
|
|
|
public LSA_UNICODE_STRING(string s)
|
|
{
|
|
Length = (ushort)(s.Length * 2);
|
|
MaximumLength = (ushort)(Length + 2);
|
|
buffer = Marshal.StringToHGlobalUni(s);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Marshal.FreeHGlobal(buffer);
|
|
buffer = IntPtr.Zero;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return Marshal.PtrToStringUni(buffer);
|
|
}
|
|
}
|
|
|
|
public enum POLICY_INFORMATION_CLASS
|
|
{
|
|
PolicyAuditLogInformation = 1,
|
|
PolicyAuditEventsInformation,
|
|
PolicyPrimaryDomainInformation,
|
|
PolicyPdAccountInformation,
|
|
PolicyAccountDomainInformation,
|
|
PolicyLsaServerRoleInformation,
|
|
PolicyReplicaSourceInformation,
|
|
PolicyDefaultQuotaInformation,
|
|
PolicyModificationInformation,
|
|
PolicyAuditFullSetInformation,
|
|
PolicyAuditFullQueryInformation,
|
|
PolicyDnsDomainInformation
|
|
}
|
|
|
|
public enum LSA_AccessPolicy : long
|
|
{
|
|
POLICY_VIEW_LOCAL_INFORMATION = 0x00000001L,
|
|
POLICY_VIEW_AUDIT_INFORMATION = 0x00000002L,
|
|
POLICY_GET_PRIVATE_INFORMATION = 0x00000004L,
|
|
POLICY_TRUST_ADMIN = 0x00000008L,
|
|
POLICY_CREATE_ACCOUNT = 0x00000010L,
|
|
POLICY_CREATE_SECRET = 0x00000020L,
|
|
POLICY_CREATE_PRIVILEGE = 0x00000040L,
|
|
POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x00000080L,
|
|
POLICY_SET_AUDIT_REQUIREMENTS = 0x00000100L,
|
|
POLICY_AUDIT_LOG_ADMIN = 0x00000200L,
|
|
POLICY_SERVER_ADMIN = 0x00000400L,
|
|
POLICY_LOOKUP_NAMES = 0x00000800L,
|
|
POLICY_NOTIFICATION = 0x00001000L
|
|
}
|
|
|
|
public struct LSA_OBJECT_ATTRIBUTES
|
|
{
|
|
public UInt32 Length;
|
|
public IntPtr RootDirectory;
|
|
public LSA_UNICODE_STRING ObjectName;
|
|
public UInt32 Attributes;
|
|
public IntPtr SecurityDescriptor;
|
|
public IntPtr SecurityQualityOfService;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
public struct DOMAIN_CONTROLLER_INFO
|
|
{
|
|
[MarshalAs(UnmanagedType.LPTStr)]
|
|
public string DomainControllerName;
|
|
[MarshalAs(UnmanagedType.LPTStr)]
|
|
public string DomainControllerAddress;
|
|
public uint DomainControllerAddressType;
|
|
public Guid DomainGuid;
|
|
[MarshalAs(UnmanagedType.LPTStr)]
|
|
public string DomainName;
|
|
[MarshalAs(UnmanagedType.LPTStr)]
|
|
public string DnsForestName;
|
|
public uint Flags;
|
|
[MarshalAs(UnmanagedType.LPTStr)]
|
|
public string DcSiteName;
|
|
[MarshalAs(UnmanagedType.LPTStr)]
|
|
public string ClientSiteName;
|
|
}
|
|
|
|
[Flags]
|
|
public enum DSGETDCNAME_FLAGS : uint
|
|
{
|
|
DS_FORCE_REDISCOVERY = 0x00000001,
|
|
DS_DIRECTORY_SERVICE_REQUIRED = 0x00000010,
|
|
DS_DIRECTORY_SERVICE_PREFERRED = 0x00000020,
|
|
DS_GC_SERVER_REQUIRED = 0x00000040,
|
|
DS_PDC_REQUIRED = 0x00000080,
|
|
DS_BACKGROUND_ONLY = 0x00000100,
|
|
DS_IP_REQUIRED = 0x00000200,
|
|
DS_KDC_REQUIRED = 0x00000400,
|
|
DS_TIMESERV_REQUIRED = 0x00000800,
|
|
DS_WRITABLE_REQUIRED = 0x00001000,
|
|
DS_GOOD_TIMESERV_PREFERRED = 0x00002000,
|
|
DS_AVOID_SELF = 0x00004000,
|
|
DS_ONLY_LDAP_NEEDED = 0x00008000,
|
|
DS_IS_FLAT_NAME = 0x00010000,
|
|
DS_IS_DNS_NAME = 0x00020000,
|
|
DS_RETURN_DNS_NAME = 0x40000000,
|
|
DS_RETURN_FLAT_NAME = 0x80000000
|
|
}
|
|
|
|
[DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
|
|
public static extern uint LsaOpenPolicy(
|
|
ref LSA_UNICODE_STRING SystemName,
|
|
ref LSA_OBJECT_ATTRIBUTES ObjectAttributes,
|
|
uint DesiredAccess,
|
|
out IntPtr PolicyHandle
|
|
);
|
|
|
|
[DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
|
|
public static extern uint LsaRetrievePrivateData(
|
|
IntPtr PolicyHandle,
|
|
ref LSA_UNICODE_STRING KeyName,
|
|
out IntPtr PrivateData
|
|
);
|
|
|
|
[DllImport("advapi32.dll", SetLastError = true)]
|
|
public static extern uint LsaNtStatusToWinError(uint status);
|
|
|
|
[DllImport("advapi32.dll", SetLastError = true)]
|
|
public static extern uint LsaClose(IntPtr ObjectHandle);
|
|
|
|
[DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
|
|
public static extern uint LsaFreeMemory(
|
|
IntPtr buffer
|
|
);
|
|
|
|
[DllImport("Netapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
public static extern int DsGetDcName
|
|
(
|
|
[MarshalAs(UnmanagedType.LPTStr)] string ComputerName,
|
|
[MarshalAs(UnmanagedType.LPTStr)] string DomainName,
|
|
[In] int DomainGuid,
|
|
[MarshalAs(UnmanagedType.LPTStr)] string SiteName,
|
|
[MarshalAs(UnmanagedType.U4)] DSGETDCNAME_FLAGS flags,
|
|
out IntPtr pDOMAIN_CONTROLLER_INFO
|
|
);
|
|
|
|
[DllImport("Netapi32.dll", SetLastError = true)]
|
|
public static extern int NetApiBufferFree(IntPtr Buffer);
|
|
|
|
|
|
public static string GetDCName()
|
|
{
|
|
// retrieves the current domain controller name
|
|
// adapted from https://www.pinvoke.net/default.aspx/netapi32.dsgetdcname
|
|
DOMAIN_CONTROLLER_INFO domainInfo;
|
|
const int ERROR_SUCCESS = 0;
|
|
IntPtr pDCI = IntPtr.Zero;
|
|
|
|
int val = DsGetDcName("", "", 0, "",
|
|
DSGETDCNAME_FLAGS.DS_DIRECTORY_SERVICE_REQUIRED |
|
|
DSGETDCNAME_FLAGS.DS_RETURN_DNS_NAME |
|
|
DSGETDCNAME_FLAGS.DS_IP_REQUIRED, out pDCI);
|
|
|
|
if (ERROR_SUCCESS == val)
|
|
{
|
|
domainInfo = (DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(pDCI, typeof(DOMAIN_CONTROLLER_INFO));
|
|
string dcName = domainInfo.DomainControllerName;
|
|
NetApiBufferFree(pDCI);
|
|
return dcName.Trim('\\');
|
|
}
|
|
else
|
|
{
|
|
string errorMessage = new Win32Exception((int)val).Message;
|
|
Console.WriteLine("\r\n [X] Error {0} retrieving domain controller : {1}", val, errorMessage);
|
|
NetApiBufferFree(pDCI);
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
} |