-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathazfw-afd.azcli
552 lines (500 loc) · 31 KB
/
azfw-afd.azcli
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
############################################################################
# Created by Jose Moreno
# April 2020
#
# The script creates AzFW and AFD, plus some VMs for testing:
#
############################################################################
# Variables
rg=azfwafd
location=eastus
vnet_name=azfw
vnet_prefix=192.168.0.0/16
vm_name=testvmlinux
vm_pip_name=testvmlinux-pip
vm_sku=Standard_B2ms # A bit of a larger VM is required for the AFD probes
vm_subnet_name=vm
vm_subnet_prefix=192.168.1.0/24
vm_cloudinit_filename=/tmp/cloudinit-whoami.txt
vm_cloudinit_url=https://raw.githubusercontent.com/erjosito/azcli/master/cloudinit-whoami.txt
vm_rt_name=vm
vm_nsg_name=vm-nsg
# AzFW
azfw_name=myazfw
azfw_pip_name=myazfw-pip
azfw_subnet_name=AzureFirewallSubnet
azfw_subnet_prefix=192.168.100.0/24
azfw_policy_name=myazfw_policy
# AFD
afd_name=azfwafd
# RG and vnet
az group create -n $rg -l $location -o none
az network vnet create -n $vnet_name -g $rg --address-prefixes $vnet_prefix --subnet-name $azfw_subnet_name --subnet-prefixes $azfw_subnet_prefix -o none
az network vnet subnet create -n $vm_subnet_name --vnet-name $vnet_name -g $rg --address-prefixes $vm_subnet_prefix -o none
# Create Log Analytics workspace
logws_name=$(az monitor log-analytics workspace list -g $rg --query '[].name' -o tsv 2>/dev/null) # Retrieve the WS name if it already existed
if [[ -z "$logws_name" ]]
then
logws_name=log$RANDOM
az monitor log-analytics workspace create -n $logws_name -g $rg -o none
fi
logws_id=$(az resource list -g $rg -n $logws_name --query '[].id' -o tsv)
logws_customerid=$(az monitor log-analytics workspace show -n $logws_name -g $rg --query customerId -o tsv)
# Deploy a Linux VM with a troubleshooting web page
az network public-ip create -g $rg -n $vm_pip_name --sku standard --allocation-method static -l $location -o none
az network nsg create -n $vm_nsg_name -g $rg -o none
az network nsg rule create -n ssh --nsg-name $vm_nsg_name -g $rg --priority 500 --destination-port-ranges 22 --access Allow --protocol Tcp -o none
az network nsg rule create -n web --nsg-name $vm_nsg_name -g $rg --priority 510 --destination-port-ranges 8080 --access Allow --protocol Tcp -o none
az network nsg rule create -n https --nsg-name $vm_nsg_name -g $rg --priority 520 --destination-port-ranges 443 --access Allow --protocol Tcp -o none
az network nsg rule create -n web80 --nsg-name $vm_nsg_name -g $rg --priority 530 --destination-port-ranges 80 --access Allow --protocol Tcp -o none
cat <<EOF > $vm_cloudinit_filename
#cloud-config
runcmd:
- apt update && apt install -y python3-pip nginx
- pip3 install flask
- wget https://raw.githubusercontent.com/erjosito/azcli/master/myip.py -O /root/myip.py
- python3 /root/myip.py &
EOF
az vm create -n $vm_name -g $rg --image UbuntuLTS --generate-ssh-keys --size $vm_sku \
--vnet-name $vnet_name --subnet $vm_subnet_name --nsg $vm_nsg_name --public-ip-address $vm_pip_name \
--custom-data $vm_cloudinit_filename -o none
vm_private_ip=$(az vm show -g $rg -n $vm_name -d --query privateIps -o tsv) && echo $vm_private_ip
# Generate certificates for the AzFW
# Create openssl.cnf
cnf_filename=/tmp/openssl.cnf
rootCA_cert=/tmp/rootCA.crt
rootCA_key=/tmp/rootCA.key
interCA_key=/tmp/interCA.key
interCA_csr=/tmp/interCA.csr
interCA_cert=/tmp/interCA.crt
interCA_pfx=/tmp/interCA.pfx
interCA_base64=/tmp/interCA.pfx.base64
cat <<EOF > $cnf_filename
[ req ]
default_bits = 4096
distinguished_name = req_distinguished_name
string_mask = utf8only
default_md = sha512
[ req_distinguished_name ]
countryName = DE
stateOrProvinceName = BY
localityName = Munich
0.organizationName = Contoso
organizationalUnitName = Contoso
commonName = contoso.com
emailAddress = [email protected]
[ rootCA_ext ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
[ interCA_ext ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:1
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
EOF
# Create root CA
openssl req -x509 -new -nodes -newkey rsa:4096 -keyout $rootCA_key -sha256 -days 1024 -out $rootCA_cert -subj "/C=US/ST=US/O=Self Signed/CN=Self Signed Root CA" -config $cnf_filename -extensions rootCA_ext
# Create intermediate CA request
openssl req -new -nodes -newkey rsa:4096 -keyout $interCA_key -sha256 -out $interCA_csr -subj "/C=US/ST=US/O=Self Signed/CN=Self Signed Intermediate CA"
# Sign on the intermediate CA
openssl x509 -req -in $interCA_csr -CA $rootCA_cert -CAkey $rootCA_key -CAcreateserial -out $interCA_cert -days 1024 -extfile $cnf_filename -extensions interCA_ext
# Export the intermediate CA into PFX
openssl pkcs12 -export -out $interCA_pfx -inkey $interCA_key -in $interCA_cert -password "pass:"
# Convert the PFX into base64
cat $interCA_pfx | base64 > $interCA_base64
# echo ""
# echo "================"
# echo "Successfully generated root and intermediate CA certificates"
# echo " - rootCA.crt/rootCA.key - Root CA public certificate and private key"
# echo " - interCA.crt/interCA.key - Intermediate CA public certificate and private key"
# echo " - interCA.pfx.base64 - Intermediate CA pkcs12 package to be consumed by CACertificate template"
# echo "================"
# Upload to AKV
akv_name=erjositoKeyvault
akv_secret_name=azfwca
akv_secret_value=$(cat $interCA_base64)
az keyvault secret set -n $akv_secret_name --value $akv_secret_value --vault-name $akv_name
akv_secret_id=$(az keyvault secret show --vault-name $akv_name -n $akv_secret_name --query id -o tsv)
# Create identity so that the AzFW can read from AKV
id_name=azfwid
az identity create -n $id_name -g $rg -o none
id_principal_id=$(az identity show -n $id_name -g $rg --query principalId -o tsv)
az keyvault set-policy -n "$akv_name" --object-id "$id_principal_id" --secret-permissions get list -o none
id_id=$(az identity show -n $id_name -g $rg --query id -o tsv) && echo $id_id
# Create AzFw policy with TLS
az network firewall policy create -n "$azfw_policy_name" -g $rg --sku Premium --idps-mode Deny --cert-name "$akv_secret_name" --key-vault-secret-id "$akv_secret_id" --identity "$id_id" -o none
# Deploy AzFW Premium
az network public-ip create -g $rg -n $azfw_pip_name --sku standard --allocation-method static -l $location -o none
azfw_pip_address=$(az network public-ip show -g $rg -n $azfw_pip_name --query ipAddress -o tsv)
azfw_policy_id=$(az network firewall policy show -n $azfw_policy_name -g $rg --query id -o tsv)
# Go to the portal!
az network firewall create -n $azfw_name -g $rg -l $location --tier Premium --policy $azfw_policy_id -o none # Not working yet...
az network firewall ip-config create -f $azfw_name -n azfw-ipconfig -g $rg --public-ip-address $azfw_pip_name --vnet-name $vnet_name -o none
az network firewall update -n $azfw_name -g $rg -o none
# Continue here...
azfw_id=$(az network firewall show -n $azfw_name -g $rg -o tsv --query id)
az monitor diagnostic-settings create -n mydiag --resource $azfw_id --workspace $logws_id \
--metrics '[{"category": "AllMetrics", "enabled": true, "retentionPolicy": {"days": 0, "enabled": false }, "timeGrain": null}]' \
--logs '[{"category": "AzureFirewallApplicationRule", "enabled": true, "retentionPolicy": {"days": 0, "enabled": false}},
{"category": "AzureFirewallNetworkRule", "enabled": true, "retentionPolicy": {"days": 0, "enabled": false}}]' -o none
azfw_private_ip=$(az network firewall show -n $azfw_name -g $rg -o tsv --query 'ipConfigurations[0].privateIpAddress') && echo $azfw_private_ip
# Outgoing app rule for all FQDNs
az network firewall policy rule-collection-group create -n ruleset01 --policy-name $azfw_policy_name -g $rg --priority 100
# App rule for SSL/HTTP
az network firewall policy rule-collection-group collection add-filter-collection --policy-name $azfw_policy_name --rule-collection-group-name ruleset01 -g $rg \
--name apprule --collection-priority 200 --action Allow --rule-name permitVnet --rule-type ApplicationRule --description "Permit HTTP traffic" \
--target-fqdns '*' --source-addresses '*' --protocols Http=8080 Https=443 --enable-tls-inspection -o none
az network firewall policy rule-collection-group collection rule add --policy-name $azfw_policy_name --rule-collection-group-name ruleset01 -g $rg \
--collection-name apprule --name permit80 --description "Permit HTTP traffic on TCP80" --rule-type ApplicationRule \
--target-fqdns '*' --source-addresses '*' --protocols Http=80 -o none
# Net rule for ICMP
az network firewall policy rule-collection-group collection add-filter-collection --policy-name $azfw_policy_name --rule-collection-group-name ruleset01 -g $rg \
--name NetworkTraffic --collection-priority 150 --action Allow --rule-name permitIcmp --rule-type NetworkRule --description "Permit intra-vnet ICMP traffic" \
--destination-addresses '*' --destination-ports '*' --source-addresses '*' --ip-protocols 'Icmp' -o none
az network firewall policy rule-collection-group collection rule add --policy-name $azfw_policy_name --rule-collection-group-name ruleset01 -g $rg \
--collection-name NetworkTraffic --name RFC1918 --description "Permit internal traffic" --rule-type NetworkRule \
--destination-addresses '10.0.0.0/8' '172.16.0.0/12' '192.168.0.0/16' --destination-ports '*' --source-addresses '10.0.0.0/8' '172.16.0.0/12' '192.168.0.0/16' --ip-protocols 'Any' -o none
# Sample for delete rule
# az network firewall policy rule-collection-group collection rule remove --policy-name $azfw_policy_name --rule-collection-group-name ruleset01 -g $rg \
# --collection-name NetworkTraffic --name permit8080
# DNAT rules for the test VM, to access the test VM over SSH/443 through the firewall
# These commands might give some errors, but they still seem to work for me
az network firewall policy rule-collection-group collection add-nat-collection --policy-name $azfw_policy_name --rule-collection-group-name ruleset01 -g $rg \
--name testDNAT --collection-priority 110 --action DNAT --rule-name DnatSSL --description "DNAT 443 traffic to test VM" \
--source-addresses '*' --destination-addresses "$azfw_pip_address" --destination-ports 443 --ip-protocols TCP \
--translated-address "$vm_private_ip" --translated-port 443 -o none
az network firewall policy rule-collection-group collection rule add --policy-name $azfw_policy_name --rule-collection-group-name ruleset01 -g $rg \
--collection-name testDNAT --name DnatSSH --description "DNAT TCP22 traffic to test VM" --rule-type NatRule \
--source-addresses '*' --destination-addresses "$azfw_pip_address" --destination-ports 22 --ip-protocols TCP \
--translated-address "$vm_private_ip" --translated-port 22 -o none
az network firewall policy rule-collection-group collection rule add --policy-name $azfw_policy_name --rule-collection-group-name ruleset01 -g $rg \
--collection-name testDNAT --name Dnat8080 --description "DNAT TCP8080 traffic to test VM" --rule-type NatRule \
--source-addresses '*' --destination-addresses "$azfw_pip_address" --destination-ports 8080 --ip-protocols TCP \
--translated-address "$vm_private_ip" --translated-port 8080 -o none
# Create public DNS record
public_domain=cloudtrooper.net
fw_dns_name='test-fw'
azfw_fqdn="${fw_dns_name}.${public_domain}"
public_dns_rg=$(az network dns zone list --query "[?name=='$public_domain'].resourceGroup" -o tsv)
if [[ -z "$public_dns_rg" ]]
then
echo "ERROR: I could not find the public DNS zone $public_domain in subscription $sub_name"
else
# First, remove any existing A-record if already exists
a_record_set=$(az network dns record-set a show -n "$fw_dns_name" -z "$public_domain" -g "$public_dns_rg" -o tsv --query id 2>/dev/null)
if [[ -n "$a_record_set" ]]
then
echo "Deleting existing A record for ${fw_dns_name}.${public_domain}..."
az network dns record-set a delete -n "$fw_dns_name" -z "$public_domain" -g "$public_dns_rg" -y
else
echo "No conflicting A records found in ${public_domain}"
fi
# Now create new A record
az network dns record-set a create -g $public_dns_rg -z cloudtrooper.net -n $fw_dns_name
az network dns record-set a add-record -g $public_dns_rg -z cloudtrooper.net -n $fw_dns_name -a $azfw_pip_address
fi
# Verify Linux VM deployed correctly and SSH is working fine
vm_pip_address=$(az network public-ip show -n $vm_pip_name -g $rg --query ipAddress -o tsv) && echo $vm_pip_address
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $vm_pip_address "ip a"
curl "http://${vm_pip_address}:8080/api/healthcheck"
curl "http://${vm_pip_address}:8080/api/ip"
# Install nginx for SSL termination
# ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $vm_pip_address "sudo apt install -y nginx"
nginx_config_file=/tmp/nginx.conf
cat <<EOF > $nginx_config_file
worker_processes auto;
events {
worker_connections 1024;
}
pid /var/run/nginx.pid;
http {
server {
listen 80; # If port 80 is required additionally to 8080
listen [::]:443 ssl;
listen 443 ssl;
server_name localhost;
ssl_protocols TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:AES128:AES256:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m; # a 1mb cache can hold about 4000 sessions, so we can hold 40000 sessions
ssl_session_timeout 24h;
keepalive_timeout 75; # up from 75 secs default
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains';
ssl_certificate /etc/nginx/ssl.crt;
ssl_certificate_key /etc/nginx/ssl.key;
location /api/ {
proxy_pass http://127.0.0.1:8080 ;
proxy_set_header Connection "";
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-For \$remote_addr;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
}
}
EOF
scp "$nginx_config_file" "${vm_pip_address}:~/"
# Put public certs in NGINX. Note: you need to have some public certs in advance
cert_file="${HOME}/onedrive/Admin/Certs/cloudtrooper.net/2021/cloudtrooper_chain.pem"
key_file="${HOME}/onedrive/Admin/Certs/cloudtrooper.net/2021/cloudtrooper.key"
scp "$cert_file" "${vm_pip_address}:~/ssl.crt"
scp "$key_file" "${vm_pip_address}:~/ssl.key"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $vm_pip_address "sudo mv ./* /etc/nginx/"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $vm_pip_address "systemctl status nginx"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $vm_pip_address "sudo systemctl restart nginx"
# Private DNS Zone for AzFW resolution
az network private-dns zone create -n $public_domain -g $rg -o none
az network private-dns record-set a add-record --record-set-name $vm_dns_name -z $public_domain -g $rg -a $vm_private_ip -o none
az network private-dns record-set a add-record --record-set-name "${vm_dns_name}-privatezone" -z $public_domain -g $rg -a $vm_private_ip -o none
az network private-dns link vnet create -g $rg -z $public_domain -n myDnsLink --virtual-network $vnet_name --registration-enabled false -o none
# Create A record in public domain
public_domain=cloudtrooper.net
vm_dns_name='test-vm'
public_dns_rg=$(az network dns zone list --query "[?name=='$public_domain'].resourceGroup" -o tsv)
if [[ -z "$public_dns_rg" ]]
then
echo "ERROR: I could not find the public DNS zone $public_domain in subscription $sub_name"
else
# First, remove any existing A-record if already exists
a_record_set=$(az network dns record-set a show -n "$vm_dns_name" -z "$public_domain" -g "$public_dns_rg" -o tsv --query id 2>/dev/null)
if [[ -n "$a_record_set" ]]
then
echo "Deleting existing A record for ${vm_dns_name}.${public_domain}..."
az network dns record-set a delete -n "$vm_dns_name" -z "$public_domain" -g "$public_dns_rg" -y
else
echo "No conflicting A records found in ${public_domain}"
fi
# Now create new A record
az network dns record-set a create -g $public_dns_rg -z cloudtrooper.net -n $vm_dns_name -o none
az network dns record-set a add-record -g $public_dns_rg -z cloudtrooper.net -n $vm_dns_name -a $vm_pip_address -o none
fi
vm_fqdn="${vm_dns_name}.${public_domain}" && echo $vm_fqdn
curl -k "https://${vm_fqdn}/api/healthcheck"
# Test file uploads
test_file="/tmp/testfile"
echo $(tr -dc a-zA-Z0-9 </dev/urandom 2>/dev/null| head -c 25) >$test_file
curl -k -F "file=@${test_file}" "https://${vm_fqdn}/api/filesize"
bad_file="/tmp/badfile"
wget "https://secure.eicar.org/eicar.com" -O $bad_file
curl -k -F "file=@${bad_file}" "https://${vm_fqdn}/api/filesize"
# Disable lower case header IDPS signatures in AzFW
az network firewall policy intrusion-detection add --policy-name $azfw_policy_name -g $rg --mode Off --signature-id 2845390
az network firewall policy intrusion-detection add --policy-name $azfw_policy_name -g $rg --mode Off --signature-id 2845391
# Disable signatures for ifconfig.co
az network firewall policy intrusion-detection add --policy-name $azfw_policy_name -g $rg --mode Off --signature-id 2834195
az network firewall policy intrusion-detection add --policy-name $azfw_policy_name -g $rg --mode Off --signature-id 2013028
###################
# Test Client VM #
###################
# You can optionally use this VM to connect to the linux VM over HTTPS and test certificates, from the AppGW's VNet,
# or to test the AppGW effective routes
test_subnet_name=testvm
test_subnet_prefix=192.168.2.0/24
az network vnet subnet create -g $rg -n $test_subnet_name --vnet-name $vnet_name --address-prefix $test_subnet_prefix
# Linux
test_vm_name=clientlinux
test_vm_pip_name="${test_vm_name}-pip"
test_vm_nsg_name="${test_vm_name}-nsg"
test_vm_sku=Standard_B1s
az vm create -n $test_vm_name -g $rg --image UbuntuLTS --generate-ssh-keys --size $test_vm_sku \
--vnet-name $vnet_name --subnet $test_subnet_name --nsg $test_vm_nsg_name --public-ip-address $test_vm_pip_name
test_vm_pip_ip=$(az network public-ip show -g $rg -n $test_vm_pip_name --query ipAddress -o tsv) && echo $test_vm_pip_ip
# Windows
win_user=$(whoami)
win_password=Microsoft123!
win_vm_name=testvmwin
win_vm_sku=Standard_B2ms
win_pip_name=testvmwin-pip
az vm create -n $win_vm_name -g $rg --image win2019datacenter --admin-username $win_user --admin-password $win_password --size $win_vm_sku \
--vnet-name $appgw_vnet_name --subnet $win_subnet_name --public-ip-address $win_pip_name
#######################
# Deploy Front Door #
#######################
# Create AFD (profile, endpoint and origin group)
az afd profile create --profile-name "${afd_name}-profile" -g $rg --sku Premium_AzureFrontDoor -o none
az afd endpoint create --profile-name "${afd_name}-profile" -g $rg --endpoint-name "${afd_name}-ep" --enabled-state Enabled -o none
az afd origin-group create --profile-name "${afd_name}-profile" -g $rg --origin-group-name "${afd_name}-og" \
--probe-request-type GET --probe-protocol Http --probe-interval-in-seconds 60 --probe-path '/healthcheck.html' \
--sample-size 4 --successful-samples-required 3 --additional-latency-in-milliseconds 50 -o none
# Add origin
az afd origin create --profile-name "${afd_name}-profile" -g $rg --origin-group-name "${afd_name}-og" --origin-name "${location}-azfw" --host-name $azfw_pip_address \
--origin-host-header $vm_fqdn --priority 1 --weight 1000 --enabled-state Enabled --http-port 8080 --https-port 443 --enforce-certificate-name-check false -o none
# Add route
az afd route create -g $rg --profile-name "${afd_name}-profile" --endpoint-name "${afd_name}-ep" --origin-group "${afd_name}-og" \
--forwarding-protocol HttpOnly --route-name "${afd_name}-route" --https-redirect Enabled \
--supported-protocols Https --link-to-default-domain Enabled -o none
# Enable diagnostic settings in the Front Door profile
afd_profile_id=$(az afd profile show --profile-name "${afd_name}-profile" -g $rg -o tsv --query id)
az monitor diagnostic-settings create -n mydiag --resource $afd_profile_id --workspace $logws_id \
--metrics '[{"category": "AllMetrics", "enabled": true, "retentionPolicy": {"days": 0, "enabled": false }, "timeGrain": null}]' \
--logs '[{"category": "FrontDoorAccessLog", "enabled": true, "retentionPolicy": {"days": 0, "enabled": false}},
{"category": "FrontDoorHealthProbeLog", "enabled": true, "retentionPolicy": {"days": 0, "enabled": false}},
{"category": "FrontDoorWebApplicationFirewallLog", "enabled": true, "retentionPolicy": {"days": 0, "enabled": false}}]' -o none
# Custom domain (Assumes you have a public DNS zone in Azure)
dns_zone_name=cloudtrooper.net
dns_name=test-afd
dns_zone_rg=$(az network dns zone list -o tsv --query "[?name=='$dns_zone_name'].resourceGroup")
dns_fqdn=${dns_name}.${dns_zone_name}
afd_fqdn=$(az afd endpoint show --profile-name "${afd_name}-profile" -g $rg --endpoint-name "${afd_name}-ep" --query hostName -o tsv)
az network dns record-set cname set-record -z $dns_zone_name -g $dns_zone_rg -n ${dns_name} -c ${afd_fqdn} -o none
# Add custom domain
akv_name=erjositoKeyvault
akv_id=$(az keyvault show -n $akv_name --query id -o tsv)
akv_cert_name=cloudtroopernet
akv_cert_sid=$(az keyvault certificate show -n cloudtroopernet --vault-name $akv_name --query sid -o tsv)
az afd secret create -g $rg --profile-name "${afd_name}-profile" --secret-name "${afd_name}-secret" --use-latest-version \
--secret-source "${akv_id}/secrets/${akv_cert_name}" -o none
az afd custom-domain create -g $rg --custom-domain-name "${afd_name}-custom-name" --profile-name "${afd_name}-profile" --host-name $dns_fqdn \
--minimum-tls-version TLS12 --certificate-type CustomerCertificate --secret "${afd_name}-secret" -o none
# End
echo "Point your browser to http://${afd_fqdn} or https://${afd_fqdn}"
########################
# Query AzMonitor logs #
########################
# AzFW Resources
query='AzureDiagnostics
| distinct Resource'
az monitor log-analytics query -w $logws_customerid --analytics-query $query -o tsv
# AzFW Log Categories
query='AzureDiagnostics
| where ResourceType == "AZUREFIREWALLS"
| distinct Category '
az monitor log-analytics query -w $logws_customerid --analytics-query $query -o tsv
# AzFW Net rules
query_net='AzureDiagnostics
| where ResourceType == "AZUREFIREWALLS"
| where Category == "AzureFirewallNetworkRule"
| where TimeGenerated >= ago(5m)
| project TimeGenerated, msg_s
| take 100 '
az monitor log-analytics query -w $logws_customerid --analytics-query $query_net -o tsv
# AzFW App rules
query_app='AzureDiagnostics
| where ResourceType == "AZUREFIREWALLS"
| where Category == "AzureFirewallApplicationRule"
| where TimeGenerated >= ago(5m)
| project TimeGenerated, msg_s
| take 100'
az monitor log-analytics query -w $logws_customerid --analytics-query $query_app -o tsv
# AFD Logs
query_afd='AzureDiagnostics
| where TimeGenerated >= ago(10m)
| where Category == "FrontdoorAccessLog"
| project TimeGenerated, clientIp_s, httpMethod_s, requestUri_s'
query_afd='AzureDiagnostics
| where TimeGenerated >= ago(10m)
| distinct Category '
az monitor log-analytics query -w $logws_customerid --analytics-query $query_afd -o tsv
###################
# Test Windows VM #
###################
# You can use this VM to connect to the linux VM over HTTPS and test certificates
win_user=$(whoami)
win_password=Microsoft123!
win_vm_name=testvmwin
win_vm_sku=Standard_B2ms
win_pip_name=testvmwin-pip
az vm create -n $win_vm_name -g $rg --image win2019datacenter --admin-username $win_user --admin-password $win_password --size $win_vm_sku \
--vnet-name $vnet_name --subnet $vm_subnet_name --public-ip-address $win_pip_name
#################
# Test Analyzer #
#################
# Create peered vnet with Ubuntu VM
nva_subnet_name=nva
nva_subnet_prefix=192.168.10.0/24
nva_name=ubuntufw
nva_pip=${nva_name}-pip
nva_size=Standard_B1s
az network vnet subnet create --vnet-name $vnet_name --name $nva_subnet_name -g $rg --address-prefixes $nva_subnet_prefix
az vm create -n $nva_name -g $rg -l $location --image ubuntuLTS --generate-ssh-keys \
--public-ip-address $nva_pip --vnet-name $vnet_name --size $nva_size --subnet $nva_subnet_name
nva_nic_id=$(az vm show -n $nva_name -g "$rg" --query 'networkProfile.networkInterfaces[0].id' -o tsv)
az network nic update --ids $nva_nic_id --ip-forwarding
nva_pip_ip=$(az network public-ip show -n $nva_pip -g $rg --query ipAddress -o tsv) && echo $nva_pip_ip
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $nva_pip_ip "sudo sysctl -w net.ipv4.ip_forward=1"
nva_private_ip=$(az network nic show --ids $nva_nic_id --query 'ipConfigurations[0].privateIpAddress' -o tsv) && echo $nva_private_ip
# Send traffic between AppGW and AzFW to the NVA
# RT in the AzFW Subnet
azfw_rt_name=azfwrt
az network route-table create --name "$azfw_rt_name" --resource-group "$rg" --location "$location"
az network route-table route create --route-table-name $azfw_rt_name -g $rg --address-prefix $appgw_subnet_prefix --name "ToAppGWSubnet" --next-hop-type VirtualAppliance --next-hop-ip-address "$nva_private_ip"
az network route-table route create --route-table-name $azfw_rt_name -g $rg --address-prefix '0.0.0.0/0' --name "Defaultroute" --next-hop-type Internet
az network vnet subnet update --name $azfw_subnet_name --route-table $azfw_rt_name --vnet-name $vnet_name --resource-group $rg
# Change AppGW RT
appgw_rt_name=appgwrt
az network route-table route update --route-table-name $appgw_rt_name -g $rg --address-prefix $vm_subnet_prefix --name "ToVMSubnet" --next-hop-type VirtualAppliance --next-hop-ip-address "$nva_private_ip"
# RT in the NVA Subnet
nva_rt_name=nvart
az network route-table create --name "$nva_rt_name" --resource-group "$rg" --location "$location"
az network route-table route create --route-table-name $nva_rt_name -g $rg --address-prefix $vm_subnet_prefix --name "ToVMSubnet" --next-hop-type VirtualAppliance --next-hop-ip-address "$azfw_private_ip"
az network vnet subnet update --name $nva_subnet_name --route-table $nva_rt_name --vnet-name $vnet_name --resource-group $rg
# Capture traffic on port 8080
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $nva_pip_ip "sudo tcpdump -n port 8080"
# Restablish normal traffic flow
az network vnet subnet update --name $azfw_subnet_name --route-table '' --vnet-name $vnet_name --resource-group $rg
az network route-table route update --route-table-name $appgw_rt_name -g $rg --address-prefix $vm_subnet_prefix --name "ToVMSubnet" --next-hop-type VirtualAppliance --next-hop-ip-address "$azfw_private_ip"
###############
# Diagnostics #
###############
# Public IPs
az network public-ip list -g $rg -o table
# Virtual Machines
az vm list -g $rg -o table
# Azure Firewall
az network firewall nat-rule collection list -f $azfw_name -g $rg -o table
az network firewall nat-rule list -c VMTraffic -f $azfw_name -g $rg --query rules -o table
az network firewall network-rule collection list -f $azfw_name -g $rg -o table
az network firewall application-rule collection list -f $azfw_name -g $rg -o table
az network firewall application-rule list -c AllowAll -f $azfw_name -g $rg --query rules -o table
az network firewall application-rule list -c AllowAll -f $azfw_name -g $rg --query rules --query '[].{Name:name,Protocols:protocols[],SourceAddresses:sourceAddresses[],TargetFQDNs:targetFqdns[]}'
# Azure Firewall: SNAT configuration
# No yet supported with CLI (https://docs.microsoft.com/en-us/azure/firewall/snat-private-range)
# AFD
az network front-door backend-pool backend list --pool-name $backend_pool_name -f $afd_name -g $rg -o table
az network front-door probe list -f $afd_name -g $rg -o table
# App Gateway
az network application-gateway list -g $appgw_rg -o table
az network application-gateway http-listener list -g $appgw_rg --gateway-name $appgw_name -o table
az network application-gateway frontend-ip list -g $appgw_rg --gateway-name $appgw_name -o table
az network application-gateway probe list -g $appgw_rg --gateway-name $appgw_name -o table
az network application-gateway address-pool list -g $appgw_rg --gateway-name $appgw_name -o table
az network application-gateway rule list -g $appgw_rg --gateway-name $appgw_name -o table
az network application-gateway rule show -g $appgw_rg --gateway-name $appgw_name -n rule1
az network application-gateway rule list -g $appgw_rg --gateway-name $appgw_name -o table
rule=$(az network application-gateway rule list -g $appgw_rg --gateway-name $appgw_name --query '[0].name' -o tsv)
az network application-gateway rule show -g $appgw_rg --gateway-name $appgw_name -n $rule
az network application-gateway url-path-map list -g $appgw_rg --gateway-name $appgw_name -o table
az network application-gateway http-settings list -g $appgw_rg --gateway-name $appgw_name -o table
# Delete VM
vm_nic_id=$(az vm show -n $vm_name -g $rg --query 'networkProfile.networkInterfaces[0].id' -o tsv)
vm_disk_id=$(az vm show -n $vm_name -g $rg --query 'storageProfile.osDisk.managedDisk.id' -o tsv)
az vm delete -n $vm_name -g $rg -y
az disk delete --ids $vm_disk_id -y
az network nic delete --ids $vm_nic_id
# Cleanup all
az group delete -n $rg -y --no-wait
################################ WORK IN PROGRES ############################################
# APIM Gateway
apim_subnet_name=APIMSubnet
apim_subnet_prefix=192.168.210.0/24
apim_sku=Developer # The Premium SKU offers multi-region on top
apim_vnet_type=Internal
apim_name=apim$RANDOM
apim_publisher_name=Contoso
az network vnet subnet create -g $rg -n $apim_subnet_name --vnet-name $vnet_name --address-prefix $apim_subnet_prefix
apim_subnet_id=$(az network vnet subnet show -n $apim_subnet_name --vnet-name $vnet_name -g $rg --query id -o tsv)
# This takes 34m!
az apim create -n $apim_name -g $rg \
--publisher-email $apim_publisher_email --publisher-name $apim_publisher_name \
--sku-name $apim_sku --virtual-network $apim_vnet_type
# And this another 23m
az apim update -n $apim_name -g $rg \
--set virtualNetworkType=$apim_vnet_type \
--set virtualNetworkConfiguration.subnetResourceId=$apim_subnet_id
##############################################################################################