-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.tf
320 lines (273 loc) · 11.2 KB
/
main.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
###############################################################
# Author : Jerzy 'Yuri' Kramarz (op7ic) #
# Version : 1.0 #
# Type : Terraform/Ansible #
# Description : Cloud-Investigate. See README.md for details #
# License : See LICENSE for details #
###############################################################
############################################################
# Provider And Resource Group Definition
############################################################
# Azure Provider source and version being used
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.46.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
# Create resource group
# Note that all deployment relies on this resource group so we set manual "depends_on" everywhere
resource "azurerm_resource_group" "resourcegroup" {
location = var.region
name = var.resource_group
}
############################################################
# Public IP (we use this to configure firewall)
############################################################
# Get Public IP of my current system
data "http" "public_IP" {
url = "https://ipinfo.io/json"
request_headers = {
Accept = "application/json"
}
}
############################################################
# Local variables used in this template
############################################################
# Define local variables which we will use across number of systems
# Reference variables from main variables.tf file
# If you prefer to add different IP as source, change 'public_ip' variable to match
locals {
public_ip = jsondecode(data.http.public_IP.response_body).ip
config_file = yamldecode(file(var.ci_config_file))
}
############################################################
# Networking Setup - Internal
############################################################
# Define primary network range (10.0.0.0/16)
resource "azurerm_virtual_network" "main" {
depends_on = [azurerm_resource_group.resourcegroup]
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = var.region
resource_group_name = var.resource_group
}
# Define LAN for Cloud-Investigate server
resource "azurerm_subnet" "servers" {
depends_on = [azurerm_resource_group.resourcegroup]
name = "servers"
resource_group_name = var.resource_group
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = [var.server_subnet_cidr]
}
############################################################
# Networking Setup - External
############################################################
resource "azurerm_public_ip" "server" {
depends_on = [azurerm_resource_group.resourcegroup]
name = "${var.prefix}-ingress"
location = var.region
resource_group_name = var.resource_group
allocation_method = "Static"
idle_timeout_in_minutes = 30
}
############################################################
# Firewall Rule Setup
############################################################
resource "azurerm_network_security_group" "windows" {
depends_on = [azurerm_resource_group.resourcegroup]
name = "windows-nsg"
location = var.region
resource_group_name = var.resource_group
security_rule {
name = "Allow-RDP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "3389"
source_address_prefix = "${local.public_ip}/32"
destination_address_prefix = "*"
}
security_rule {
name = "Allow-WinRM"
priority = 101
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "5985"
source_address_prefix = "${local.public_ip}/32"
destination_address_prefix = "*"
}
security_rule {
name = "Allow-WinRM-secure"
priority = 102
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "5986"
source_address_prefix = "${local.public_ip}/32"
destination_address_prefix = "*"
}
security_rule {
name = "Allow-SMB"
priority = 103
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "445"
source_address_prefix = "${local.public_ip}/32"
destination_address_prefix = "*"
}
security_rule {
name = "Allow-SFTP"
priority = 104
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "${local.public_ip}/32"
destination_address_prefix = "*"
}
}
############################################################
# Server Resource
############################################################
# Create IP address space and interface for our forensic server
resource "azurerm_network_interface" "server" {
depends_on = [azurerm_resource_group.resourcegroup]
name = "${var.prefix}-nic"
location = var.region
resource_group_name = var.resource_group
ip_configuration {
name = "server-static"
subnet_id = azurerm_subnet.servers.id
private_ip_address_allocation = "Static"
private_ip_address = cidrhost(var.server_subnet_cidr, 10)
public_ip_address_id = azurerm_public_ip.server.id
}
}
# Associate IP and Security Group with our DC
resource "azurerm_network_interface_security_group_association" "forensicserver" {
depends_on = [azurerm_resource_group.resourcegroup]
network_interface_id = azurerm_network_interface.server.id
network_security_group_id = azurerm_network_security_group.windows.id
}
# Create a server
resource "azurerm_virtual_machine" "forensicserver" {
depends_on = [azurerm_resource_group.resourcegroup]
name = "CloudInvestigate-Server"
location = var.region
resource_group_name = var.resource_group
network_interface_ids = [azurerm_network_interface.server.id]
vm_size = var.server_size
# Apply tag to server
tags = {
kind = "CloudInvestigate-Server"
}
# Delete the OS disk automatically when deleting the VM
delete_os_disk_on_termination = true
# Delete data disks automatically when deleting the VM
delete_data_disks_on_termination = true
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = var.server_os
sku = var.server_SKU
version = "latest"
}
storage_os_disk {
name = "os-disk"
caching = "None"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
disk_size_gb = var.disk_size
}
os_profile {
computer_name = local.config_file.server_name
admin_username = local.config_file.local_admin_credentials.username
admin_password = local.config_file.local_admin_credentials.password
}
os_profile_windows_config {
provision_vm_agent = true
enable_automatic_upgrades = false
timezone = "Central European Standard Time"
winrm {
protocol = "HTTP"
}
}
# Format partition and install chocoladey with WSL so we can install remaining tools
# This will block execution of other installers until chocoladey install is complete
# The order of operation is important here so PATH variables are aligned
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
working_dir = "${path.root}/../ansible/"
command = "sleep 120; /bin/bash -c 'ANSIBLE_CONFIG=${path.root}/ansible.cfg ansible-playbook server.yml -t baseline,diskpart,chocoladey'"
}
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
working_dir = "${path.root}/../ansible/"
command = "sleep 120; /bin/bash -c 'ANSIBLE_CONFIG=${path.root}/ansible.cfg ansible-playbook server.yml -t wsl2'"
}
}
resource "null_resource" "independent-tools-install" {
depends_on = [azurerm_resource_group.resourcegroup, azurerm_virtual_machine.forensicserver]
# Provision tools in the background
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
working_dir = "${path.root}/../ansible/"
command = "sleep 700; /bin/bash -c 'ANSIBLE_CONFIG=${path.root}/ansible.cfg ansible-playbook server.yml -t tools'"
}
}
resource "null_resource" "independent-kape-install" {
depends_on = [azurerm_resource_group.resourcegroup, azurerm_virtual_machine.forensicserver]
# Provision kape in the background
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
working_dir = "${path.root}/../ansible/"
command = "sleep 120; /bin/bash -c 'ANSIBLE_CONFIG=${path.root}/ansible.cfg ansible-playbook server.yml -t kape'"
}
}
resource "null_resource" "independent-sysmon-install" {
depends_on = [azurerm_resource_group.resourcegroup, azurerm_virtual_machine.forensicserver]
# Provision sysmon and monitoring in the background
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
working_dir = "${path.root}/../ansible/"
command = "sleep 180; /bin/bash -c 'ANSIBLE_CONFIG=${path.root}/ansible.cfg ansible-playbook server.yml -t sysmon,monitoring'"
}
}
resource "null_resource" "independent-autopsy-install" {
depends_on = [azurerm_resource_group.resourcegroup, azurerm_virtual_machine.forensicserver]
# Provision autopsy in the background
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
working_dir = "${path.root}/../ansible/"
command = "sleep 90; /bin/bash -c 'ANSIBLE_CONFIG=${path.root}/ansible.cfg ansible-playbook server.yml -t autopsy'"
}
}
############################################################
# Outputs
############################################################
output "printout" {
value = <<EOF
Network Setup:
Cloud Investigate Server IP = ${azurerm_public_ip.server.ip_address}
Credentials:
Local Administrator:
Username: ${local.config_file.local_admin_credentials.username}
Password: ${local.config_file.local_admin_credentials.password}
RDP to Cloud Investigate server:
xfreerdp /v:${azurerm_public_ip.server.ip_address} /u:${local.config_file.local_admin_credentials.username} '/p:${local.config_file.local_admin_credentials.password}' +clipboard /cert-ignore
EOF
}