2019-08-24 21:07:57 +00:00
|
|
|
|
using DSInternals.Common.Data;
|
|
|
|
|
using DSInternals.Replication.Model;
|
|
|
|
|
using DSInternals.Replication.Interop;
|
|
|
|
|
using NDceRpc;
|
|
|
|
|
using NDceRpc.Microsoft.Interop;
|
|
|
|
|
using NDceRpc.Native;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
using DSInternals.Common.Interop;
|
|
|
|
|
using DSInternals.Common.Cryptography;
|
|
|
|
|
using DSInternals.Common;
|
|
|
|
|
|
|
|
|
|
namespace DSInternals.Replication
|
2015-12-26 22:44:43 +00:00
|
|
|
|
{
|
|
|
|
|
public class DirectoryReplicationClient : IDisposable
|
|
|
|
|
{
|
2017-03-19 06:55:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Service principal name (SPN) of the destination server.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const string ServicePrincipalNameFormat = "ldap/{0}";
|
|
|
|
|
private const string DrsNamedPipeName = @"\pipe\lsass";
|
2019-08-24 21:07:57 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Identifier of Windows Server 2000 dcpromo.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly Guid DcPromoGuid2k = new Guid("6abec3d1-3054-41c8-a362-5a0c5b7d5d71");
|
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
/// <summary>
|
2019-08-24 21:07:57 +00:00
|
|
|
|
/// Identifier of Windows Server 2003+ dcpromo.
|
2015-12-26 22:44:43 +00:00
|
|
|
|
/// </summary>
|
2019-08-24 21:07:57 +00:00
|
|
|
|
private static readonly Guid DcPromoGuid2k3 = new Guid("6afab99c-6e26-464a-975f-f58f105218bc");
|
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
/// <summary>
|
2019-08-24 21:07:57 +00:00
|
|
|
|
/// Non-DC client identifier.
|
2015-12-26 22:44:43 +00:00
|
|
|
|
/// </summary>
|
2019-08-24 21:07:57 +00:00
|
|
|
|
private static readonly Guid NtdsApiClientGuid = new Guid("e24d201a-4fd6-11d1-a3da-0000f875ae0d");
|
2015-12-26 22:44:43 +00:00
|
|
|
|
|
|
|
|
|
private NativeClient rpcConnection;
|
|
|
|
|
private DrsConnection drsConnection;
|
|
|
|
|
private NamedPipeConnection npConnection;
|
2020-02-25 20:14:03 +00:00
|
|
|
|
private string domainNamingContext;
|
|
|
|
|
private string netBIOSDomainName;
|
|
|
|
|
|
|
|
|
|
public string DomainNamingContext
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if(this.domainNamingContext == null)
|
|
|
|
|
{
|
|
|
|
|
// Lazy loading
|
|
|
|
|
this.LoadDomainInfo();
|
|
|
|
|
}
|
|
|
|
|
return this.domainNamingContext;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string NetBIOSDomainName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (this.netBIOSDomainName == null)
|
|
|
|
|
{
|
|
|
|
|
// Lazy loading
|
|
|
|
|
this.LoadDomainInfo();
|
|
|
|
|
}
|
|
|
|
|
return this.netBIOSDomainName;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-26 22:44:43 +00:00
|
|
|
|
|
|
|
|
|
public DirectoryReplicationClient(string server, RpcProtocol protocol, NetworkCredential credential = null)
|
|
|
|
|
{
|
2019-08-24 21:07:57 +00:00
|
|
|
|
Validator.AssertNotNullOrWhiteSpace(server, nameof(server));
|
2015-12-26 22:44:43 +00:00
|
|
|
|
this.CreateRpcConnection(server, protocol, credential);
|
2019-08-24 21:07:57 +00:00
|
|
|
|
this.drsConnection = new DrsConnection(this.rpcConnection.Binding, NtdsApiClientGuid);
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 15:22:16 +00:00
|
|
|
|
public ReplicationCursor[] GetReplicationCursors(string namingContext)
|
|
|
|
|
{
|
2019-08-24 21:07:57 +00:00
|
|
|
|
Validator.AssertNotNullOrWhiteSpace(namingContext, nameof(namingContext));
|
2016-09-16 15:22:16 +00:00
|
|
|
|
return this.drsConnection.GetReplicationCursors(namingContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DSAccount> GetAccounts(string domainNamingContext, ReplicationProgressHandler progressReporter = null)
|
2015-12-26 22:44:43 +00:00
|
|
|
|
{
|
2019-08-24 21:07:57 +00:00
|
|
|
|
Validator.AssertNotNullOrWhiteSpace(domainNamingContext, nameof(domainNamingContext));
|
2015-12-26 22:44:43 +00:00
|
|
|
|
ReplicationCookie cookie = new ReplicationCookie(domainNamingContext);
|
2016-09-16 15:22:16 +00:00
|
|
|
|
return GetAccounts(cookie, progressReporter);
|
2016-02-21 09:05:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 15:22:16 +00:00
|
|
|
|
public IEnumerable<DSAccount> GetAccounts(ReplicationCookie initialCookie, ReplicationProgressHandler progressReporter = null)
|
2016-02-21 09:05:09 +00:00
|
|
|
|
{
|
2019-08-24 21:07:57 +00:00
|
|
|
|
Validator.AssertNotNull(initialCookie, nameof(initialCookie));
|
2016-09-16 15:22:16 +00:00
|
|
|
|
// Create AD schema
|
2015-12-26 22:44:43 +00:00
|
|
|
|
var schema = BasicSchemaFactory.CreateSchema();
|
2016-09-16 15:22:16 +00:00
|
|
|
|
var currentCookie = initialCookie;
|
2016-02-21 09:05:09 +00:00
|
|
|
|
ReplicationResult result;
|
2016-09-16 15:22:16 +00:00
|
|
|
|
int processedObjectCount = 0;
|
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
do
|
|
|
|
|
{
|
2016-09-16 15:22:16 +00:00
|
|
|
|
// Perform one replication cycle
|
|
|
|
|
result = this.drsConnection.ReplicateAllObjects(currentCookie);
|
|
|
|
|
|
|
|
|
|
// Report replication progress
|
|
|
|
|
if(progressReporter != null)
|
|
|
|
|
{
|
|
|
|
|
processedObjectCount += result.Objects.Count;
|
|
|
|
|
progressReporter(result.Cookie, processedObjectCount, result.TotalObjectCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process the returned objects
|
2015-12-26 22:44:43 +00:00
|
|
|
|
foreach (var obj in result.Objects)
|
|
|
|
|
{
|
|
|
|
|
obj.Schema = schema;
|
2016-02-21 09:05:09 +00:00
|
|
|
|
if (!obj.IsAccount)
|
2015-12-26 22:44:43 +00:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-03-09 19:56:48 +00:00
|
|
|
|
var account = new DSAccount(obj, this.NetBIOSDomainName, this.SecretDecryptor);
|
2015-12-26 22:44:43 +00:00
|
|
|
|
yield return account;
|
|
|
|
|
}
|
2016-09-16 15:22:16 +00:00
|
|
|
|
|
|
|
|
|
// Update the position of the replication cursor
|
|
|
|
|
currentCookie = result.Cookie;
|
2015-12-26 22:44:43 +00:00
|
|
|
|
} while (result.HasMoreData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DSAccount GetAccount(Guid objectGuid)
|
|
|
|
|
{
|
|
|
|
|
var obj = this.drsConnection.ReplicateSingleObject(objectGuid);
|
|
|
|
|
var schema = BasicSchemaFactory.CreateSchema();
|
|
|
|
|
obj.Schema = schema;
|
2020-03-09 19:56:48 +00:00
|
|
|
|
return new DSAccount(obj, this.NetBIOSDomainName, this.SecretDecryptor);
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DPAPIBackupKey> GetDPAPIBackupKeys(string domainNamingContext)
|
|
|
|
|
{
|
2016-09-16 15:22:16 +00:00
|
|
|
|
// TODO: Move schema from constructor to property?
|
2015-12-26 22:44:43 +00:00
|
|
|
|
// TODO: Split this function into RSA and Legacy Part so that exception in one of them does not crash the whole process
|
|
|
|
|
var schema = BasicSchemaFactory.CreateSchema();
|
|
|
|
|
|
2016-02-02 23:40:40 +00:00
|
|
|
|
// Fetch the legacy pointer first, because there is a higher chance that it is present than the RSA one.
|
2015-12-26 22:44:43 +00:00
|
|
|
|
string legacyPointerDN = DPAPIBackupKey.GetPreferredLegacyKeyPointerDN(domainNamingContext);
|
|
|
|
|
var legacyPointer = this.GetLSASecret(legacyPointerDN, schema);
|
|
|
|
|
yield return legacyPointer;
|
|
|
|
|
|
|
|
|
|
string legacyKeyDN = DPAPIBackupKey.GetKeyDN(legacyPointer.KeyId, domainNamingContext);
|
|
|
|
|
var legacyKey = this.GetLSASecret(legacyKeyDN, schema);
|
|
|
|
|
yield return legacyKey;
|
2016-02-02 23:40:40 +00:00
|
|
|
|
|
|
|
|
|
string rsaPointerDN = DPAPIBackupKey.GetPreferredRSAKeyPointerDN(domainNamingContext);
|
|
|
|
|
var rsaPointer = this.GetLSASecret(rsaPointerDN, schema);
|
|
|
|
|
yield return rsaPointer;
|
|
|
|
|
|
|
|
|
|
string rsaKeyDN = DPAPIBackupKey.GetKeyDN(rsaPointer.KeyId, domainNamingContext);
|
|
|
|
|
var rsaKey = this.GetLSASecret(rsaKeyDN, schema);
|
|
|
|
|
yield return rsaKey;
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DPAPIBackupKey GetLSASecret(string distinguishedName, BasicSchema schema)
|
|
|
|
|
{
|
|
|
|
|
var secretObj = this.drsConnection.ReplicateSingleObject(distinguishedName);
|
|
|
|
|
secretObj.Schema = schema;
|
|
|
|
|
return new DPAPIBackupKey(secretObj, this.SecretDecryptor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DSAccount GetAccount(string distinguishedName)
|
|
|
|
|
{
|
|
|
|
|
var obj = this.drsConnection.ReplicateSingleObject(distinguishedName);
|
|
|
|
|
// TODO: Extract?
|
|
|
|
|
var schema = BasicSchemaFactory.CreateSchema();
|
|
|
|
|
obj.Schema = schema;
|
2020-03-09 19:56:48 +00:00
|
|
|
|
return new DSAccount(obj, this.NetBIOSDomainName, this.SecretDecryptor);
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DSAccount GetAccount(NTAccount accountName)
|
|
|
|
|
{
|
|
|
|
|
Guid objectGuid = this.drsConnection.ResolveGuid(accountName);
|
|
|
|
|
return this.GetAccount(objectGuid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DSAccount GetAccount(SecurityIdentifier sid)
|
|
|
|
|
{
|
|
|
|
|
Guid objectGuid = this.drsConnection.ResolveGuid(sid);
|
|
|
|
|
return this.GetAccount(objectGuid);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 21:07:57 +00:00
|
|
|
|
public void WriteNgcKey(Guid objectGuid, byte[] publicKey)
|
2016-06-21 07:27:40 +00:00
|
|
|
|
{
|
2019-08-24 21:07:57 +00:00
|
|
|
|
string distinguishedName = this.drsConnection.ResolveDistinguishedName(objectGuid);
|
|
|
|
|
this.WriteNgcKey(distinguishedName, publicKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteNgcKey(NTAccount accountName, byte[] publicKey)
|
|
|
|
|
{
|
|
|
|
|
string distinguishedName = this.drsConnection.ResolveDistinguishedName(accountName);
|
|
|
|
|
this.WriteNgcKey(distinguishedName, publicKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteNgcKey(SecurityIdentifier sid, byte[] publicKey)
|
|
|
|
|
{
|
|
|
|
|
string distinguishedName = this.drsConnection.ResolveDistinguishedName(sid);
|
|
|
|
|
this.WriteNgcKey(distinguishedName, publicKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteNgcKey(string accountDN, byte[] publicKey)
|
|
|
|
|
{
|
|
|
|
|
this.drsConnection.WriteNgcKey(accountDN, publicKey);
|
2016-06-21 07:27:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
private DirectorySecretDecryptor SecretDecryptor
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new ReplicationSecretDecryptor(this.drsConnection.SessionKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateRpcConnection(string server, RpcProtocol protocol, NetworkCredential credential = null)
|
|
|
|
|
{
|
|
|
|
|
EndpointBindingInfo binding;
|
|
|
|
|
switch(protocol)
|
|
|
|
|
{
|
|
|
|
|
case RpcProtocol.TCP:
|
|
|
|
|
binding = new EndpointBindingInfo(RpcProtseq.ncacn_ip_tcp, server, null);
|
|
|
|
|
break;
|
|
|
|
|
case RpcProtocol.SMB:
|
|
|
|
|
binding = new EndpointBindingInfo(RpcProtseq.ncacn_np, server, DrsNamedPipeName);
|
|
|
|
|
if(credential != null)
|
|
|
|
|
{
|
|
|
|
|
// Connect named pipe
|
|
|
|
|
this.npConnection = new NamedPipeConnection(server, credential);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// TODO: Extract as string
|
2019-08-24 21:07:57 +00:00
|
|
|
|
throw new NotImplementedException("The requested RPC protocol is not supported.");
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
this.rpcConnection = new NativeClient(binding);
|
2017-03-19 06:55:01 +00:00
|
|
|
|
|
|
|
|
|
NetworkCredential rpcCredential = credential ?? Client.Self;
|
|
|
|
|
string spn = String.Format(ServicePrincipalNameFormat, server);
|
|
|
|
|
this.rpcConnection.AuthenticateAs(spn, rpcCredential, RPC_C_AUTHN_LEVEL.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN.RPC_C_AUTHN_GSS_NEGOTIATE);
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!disposing)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-24 21:07:57 +00:00
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
if (this.drsConnection != null)
|
|
|
|
|
{
|
|
|
|
|
this.drsConnection.Dispose();
|
|
|
|
|
this.drsConnection = null;
|
|
|
|
|
}
|
2019-08-24 21:07:57 +00:00
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
if (this.rpcConnection != null)
|
|
|
|
|
{
|
|
|
|
|
this.rpcConnection.Dispose();
|
|
|
|
|
this.rpcConnection = null;
|
|
|
|
|
}
|
2019-08-24 21:07:57 +00:00
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
if(this.npConnection != null)
|
|
|
|
|
{
|
|
|
|
|
this.npConnection.Dispose();
|
|
|
|
|
this.npConnection = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-25 20:14:03 +00:00
|
|
|
|
|
|
|
|
|
private void LoadDomainInfo()
|
|
|
|
|
{
|
|
|
|
|
// These is no direct way of retrieving current DC's domain info, so we are using a combination of 3 calls.
|
|
|
|
|
|
|
|
|
|
// We first retrieve FSMO roles. The PDC emulator lies in the same domain as the current server.
|
|
|
|
|
var fsmoRoles = this.drsConnection.ListRoles();
|
|
|
|
|
|
|
|
|
|
// We need the DC object of the PDC Emulator. It is the parent of the NTDS Settings object.
|
|
|
|
|
string pdcEmulator = new DistinguishedName(fsmoRoles.PdcEmulator).Parent.ToString();
|
|
|
|
|
|
|
|
|
|
// Get the PDC account object from the domain partition.
|
|
|
|
|
var pdcInfo = this.drsConnection.ListInfoForServer(pdcEmulator);
|
|
|
|
|
string pdcAccountDN = pdcInfo.ServerReference;
|
|
|
|
|
|
|
|
|
|
// Get the PDC Emulator's domain naming context.
|
|
|
|
|
this.domainNamingContext = new DistinguishedName(pdcAccountDN).RootNamingContext.ToString();
|
|
|
|
|
|
|
|
|
|
// Get the PDC Emulator's NetBIOS account name and extract the domain part.
|
|
|
|
|
NTAccount pdcAccount = this.drsConnection.ResolveAccountName(pdcAccountDN);
|
|
|
|
|
this.netBIOSDomainName = pdcAccount.NetBIOSDomainName();
|
|
|
|
|
}
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|