-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathaws_instance_private_windows.tf
474 lines (444 loc) · 16.3 KB
/
aws_instance_private_windows.tf
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Windows AD Pen-Testing lab
# Based on https://blog.focal-point.com/how-to-build-a-cheap-active-directory-pen-test-lab-in-aws-without-any-effort
# CloudFormation code: https://cf-template-pub.s3.us-east-2.amazonaws.com/pentest-lab.json
//Vulnerable Instance: Microsoft Windows Server 2012 R2
// Ami Id: ami-09a015f8b891cdc6c
// Ami Alias: /aws/service/marketplace/prod-miayn6czrf6yi/2021.09.15 Learn More New
// Product Code: pbucpm8t34wjexnsv8hgyaf6
variable "admin_password" {
description = "Windows Administrator password to login as."
default = "123456789" # vulnerable password listed by default in Infection Monkey
}
variable "DomainDNSName" {
description = "DomainDNSName"
default = "DomainDNSName"
}
variable "DomainNetBiosName" {
description = "DomainNetBiosName"
default = "DomainNetBiosName"
}
# data "http" "ftpu" {
# url = "hhttps://www.exploit-db.com/apps/6388a2ae7dd2965225b3c8fad62f2b3b-ftpu_10.zip"
# # Optional request headers
# request_headers = {
# Accept = "application/zip"
# }
# }
data "aws_ami" "windowsserver2012R2ami" {
most_recent = true
owners = ["801119661308"]
filter {
name = "name"
values = ["*Windows_Server-2012-R2_RTM-English-64Bit-Base*"]
}
}
resource "aws_instance" "windowsserver2012R2" {
count = var.deploment-control["windowsserver"] ? 1 :0 // control variable from variables.tf
# The connection block tells our provisioner how to
# communicate with the resource (instance)
connection {
type = "winrm"
user = "Administrator"
password = var.admin_password
# set from default of 5m to 10m to avoid winrm timeout
timeout = "10m"
}
ami = data.aws_ami.windowsserver2012R2ami.id
instance_type = "t2.micro"
#key_name = "key12345"
key_name = aws_key_pair.kali-key-pair.key_name
private_ip = "10.0.1.10"
subnet_id = aws_subnet.privateSubnet.id
vpc_security_group_ids = [aws_security_group.SecurityGroup-VulnerableMachines.id]
tags = {
Name = "Windows Server 2012 R2"
}
# Vulnerability - Konica Minolta FTP Utility 1.00 - CWD Command Overflow (SEH)
# https://www.exploit-db.com/exploits/39215
# https://security.stackexchange.com/questions/119107/install-a-vulnerable-service-for-windows-7
# provisioner "file" {
# source = data.http.ftpu
# destination = "C:/6388a2ae7dd2965225b3c8fad62f2b3b-ftpu_10.zip"
# # connection = {
# # type = "winrm"
# # user = "Administrator"
# # password = var.admin_password
# # agent = "false"
# # }
# }
# Note that terraform uses Go WinRM which doesn't support https at this time. If server is not on a private network,
# recommend bootstraping Chef via user_data. See asg_user_data.tpl for an example on how to do that.
user_data = <<EOF
<script>
winrm quickconfig -q & winrm set winrm/config @{MaxTimeoutms="1800000"} & winrm set winrm/config/service @{AllowUnencrypted="true"} & winrm set winrm/config/service/auth @{Basic="true"}
</script>
<powershell>
# Disable Win Firewall
Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled False
#netsh advfirewall firewall add rule name="WinRM in" protocol=TCP dir=in profile=any localport=5985 remoteip=any localip=any action=allow
# Set Administrator password
$admin = [adsi]("WinNT://./administrator, user")
$admin.psbase.invoke("SetPassword", "${var.admin_password}")
# Install AD
#Install-WindowsFeature AD-Domain-Services, rsat-adds -IncludeAllSubFeature
#Install-ADDSForest -DomainName "${var.DomainDNSName}" -SafeModeAdministratorPassword (ConvertTo-SecureString '"${var.admin_password}"' -AsPlainText -Force) -DomainMode Win2012R2 -DomainNetbiosName "${var.DomainNetBiosName}" -ForestMode Win2012R2 -Confirm:$false -Force
# Restart AD service
#Restart-Service NetLogon -EA 0
</powershell>
EOF
depends_on = [
aws_vpc.VPC,
aws_subnet.privateSubnet,
aws_route_table.PublicRouteTable,
aws_security_group.SecurityGroup-VulnerableMachines
]
}
/*
"DC1": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"configSets": {
"config": [
"setup",
"rename",
"installADDS",
"finalize"
]
},
"setup": {
"files": {
"c:\\cfn\\cfn-hup.conf": {
"content": {
"Fn::Join": [
"",
[
"[main]\n",
"stack=",
{
"Ref": "AWS::StackName"
},
"\n",
"region=",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
},
"c:\\cfn\\hooks.d\\cfn-auto-reloader.conf": {
"content": {
"Fn::Join": [
"",
[
"[cfn-auto-reloader-hook]\n",
"triggers=post.update\n",
"path=Resources.DC1.Metadata.AWS::CloudFormation::Init\n",
"action=cfn-init.exe -v -c config -s ",
{
"Ref": "AWS::StackId"
},
" -r DC1",
" --region ",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
},
"c:\\cfn\\scripts\\Set-StaticIP.ps1": {
"content": {
"Fn::Join": [
"",
[
"$netip = Get-NetIPConfiguration;",
"$ipconfig = Get-NetIPAddress | ?{$_.IpAddress -eq $netip.IPv4Address.IpAddress};",
"Get-NetAdapter | Set-NetIPInterface -DHCP Disabled;",
"Get-NetAdapter | New-NetIPAddress -AddressFamily IPv4 -IPAddress $netip.IPv4Address.IpAddress -PrefixLength $ipconfig.PrefixLength -DefaultGateway $netip.IPv4DefaultGateway.NextHop;",
"Get-NetAdapter | Set-DnsClientServerAddress -ServerAddresses $netip.DNSServer.ServerAddresses;",
"\n"
]
]
}
}
},
"services": {
"windows": {
"cfn-hup": {
"enabled": "true",
"ensureRunning": "true",
"files": [
"c:\\cfn\\cfn-hup.conf",
"c:\\cfn\\hooks.d\\cfn-auto-reloader.conf"
]
}
}
},
"commands": {
"a-disable-win-fw": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command \"Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled False"
]
]
},
"waitAfterCompletion": "0"
}
}
},
"rename": {
"commands": {
"a-set-static-ip": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -ExecutionPolicy RemoteSigned -Command c:\\cfn\\scripts\\Set-StaticIP.ps1"
]
]
},
"waitAfterCompletion": "15"
},
"b-run-powershell-RenameComputer-no-reboot": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe Rename-Computer -NewName DC1 -force -restart"
]
]
},
"waitAfterCompletion": "forever"
}
}
},
"installADDS": {
"commands": {
"1-install-prereqs": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command \"Install-WindowsFeature AD-Domain-Services, rsat-adds -IncludeAllSubFeature"
]
]
},
"waitAfterCompletion": "0"
},
"2-install-adds": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command Install-ADDSForest -DomainName ",
{
"Ref": "DomainDNSName"
},
" -SafeModeAdministratorPassword (ConvertTo-SecureString '",
{
"Ref": "AdminPassword"
},
"' -AsPlainText -Force) -DomainMode Win2012R2 -DomainNetbiosName ",
{
"Ref": "DomainNetBiosName"
},
" -ForestMode Win2012R2 -Confirm:$false -Force"
]
]
},
"waitAfterCompletion": "forever"
},
"3-restart-service": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command Restart-Service NetLogon -EA 0"
]
]
},
"waitAfterCompletion": "20"
},
"4-start-ADWS": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command $s = Get-Service -Name ADWS; while ($s.Status -ne 'Running'){ Start-Service ADWS; Start-Sleep 3 }"
]
]
},
"waitAfterCompletion": "30"
},
"5-create-adminuser": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command $u = New-ADUser ",
{
"Ref": "DomainAdminUser"
},
" -SamAccountName ",
{
"Ref": "DomainAdminUser"
},
" -UserPrincipalName ",
{
"Ref": "DomainAdminUser"
},
"@",
{
"Ref": "DomainDNSName"
},
" -AccountPassword (ConvertTo-SecureString '",
{
"Ref": "AdminPassword"
},
"' -AsPlainText -Force) -Enabled $true -PasswordNeverExpires $true -PassThru; Add-ADGroupMember -Identity 'domain admins' -Members $u"
]
]
},
"waitAfterCompletion": "0"
},
"6-create-spnuser": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command $u = New-ADUser ",
{
"Ref": "SPNUser"
},
" -SamAccountName ",
{
"Ref": "SPNUser"
},
" -UserPrincipalName ",
{
"Ref": "SPNUser"
},
"@",
{
"Ref": "DomainDNSName"
},
" -AccountPassword (ConvertTo-SecureString 'Summer2019!' -AsPlainText -Force) -Enabled $true -PasswordNeverExpires $true -PassThru; Add-ADGroupMember -Identity 'domain admins' -Members $u"
]
]
},
"waitAfterCompletion": "0"
},
"7-make-user-spn": {
"command": {
"Fn::Join": [
"",
[
"powershell.exe -Command setspn -A SERVICE_X/SERVICE_X ",
{
"Ref": "SPNUser"
}
]
]
},
"waitAfterCompletion": "0"
}
}
},
"finalize": {
"commands": {
"a-signal-success": {
"command": {
"Fn::Join": [
"",
[
"cfn-signal.exe -e 0 ",
{
"Fn::Base64": {
"Ref": "DomainControllerWaitHandle"
}
},
""
]
]
}
}
}
}
}
},
"Properties": {
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"VolumeSize": "40"
}
}
],
"ImageId": {
"Ref": "WindowsAmiId"
},
"InstanceType": "t2.micro",
"KeyName": {
"Ref": "KeyName"
},
"DisableApiTermination": "false",
"InstanceInitiatedShutdownBehavior": "stop",
"Monitoring": "false",
"Tags": [
{
"Key": "Name",
"Value": "Windows Domain Controller"
}
],
"NetworkInterfaces": [
{
"DeleteOnTermination": "true",
"Description": "Primary network interface",
"DeviceIndex": 0,
"SubnetId": {
"Ref": "PublicSubnet"
},
"PrivateIpAddresses": [
{
"PrivateIpAddress": "192.168.1.100",
"Primary": "true"
}
],
"GroupSet": [
{
"Ref": "LabSecurityGroup"
}
]
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<script>\n",
"cfn-init.exe -v -c config -s ",
{
"Ref": "AWS::StackId"
},
" -r DC1",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"</script>\n"
]
]
}
}
}
},
*/