forked from erjosito/azcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowlogs.azcli
185 lines (168 loc) · 7.44 KB
/
flowlogs.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
########################################################
# Script to deploy VMs and generate traffic between them.
# NSG flow logs and Traffic Analytics can be used to
# explore the traffic flows
#
# Jose Moreno
# May 2021
########################################################
# Variables
rg=flowlogs
location=westeurope
vnet_name=flowlogs
vnet_prefix=192.168.0.0/16
subnet_name=vm
subnet_prefix=192.168.1.0/24
nsg_name=flowlogs
cloudinit_file="/tmp/cloudinit.txt"
vm_size=Standard_B1s
no_of_vms=4
flows="1;2:3;4:1433:1000,1:2:80:100,3:4:443:100" # Syntax: src:dst:port:kb/min
# Some helper functions
# Converts a list to a shell array
function convert_string_to_array () {
# Default to comma as separator
if [[ -n $2 ]]
then
separator=$2
else
separator=','
fi
# Different syntax for bash and zsh
if [ -n "$BASH_VERSION" ]; then
arr_opt=a
elif [ -n "$ZSH_VERSION" ]; then
arr_opt=A
fi
# Do the split into array
IFS=$separator read -r"${arr_opt}" myarray <<< "$1"
echo "${myarray[@]}"
}
# Configure flow logs for all NSGs in a given location
function configure_flowlogs () {
if [[ -z $1 ]]; then
nsg_location=$(az group show -n $rg --query location -o tsv)
echo "No argument specified, taking the RG's location $nsg_location"
else
echo "Using $1 as location"
nsg_location=$1
fi
storage_account_name=$(az storage account list -g $rg -o tsv --query "[?location=='$nsg_location'].name" | head -1)
if [[ -z "$storage_account_name" ]]; then
storage_account_name=$(echo "logs$RANDOM${nsg_location}" | cut -c1-24) # max 24 characters
echo "No storage account found in $nsg_location, creating one..."
az storage account create -n $storage_account_name -g $rg --sku Standard_LRS --kind StorageV2 -l $nsg_location -o none
else
echo "Storage account $storage_account_name created in $nsg_location, using it for NSG flow flogs"
fi
echo "Looking for NSGs in resource group $rg in location $nsg_location..."
nsg_list=$(az network nsg list -g $rg -o tsv --query "[?location=='$nsg_location'].name")
echo "$(echo $nsg_list | wc -l) NSGs found"
while IFS= read -r nsg_name; do
echo "Configuring Flow Logs for NSG $nsg_name into storage account $storage_account_name..."
az network watcher flow-log create -l $nsg_location -n "${nsg_name}-${nsg_location}" -g $rg --nsg $nsg_name --storage-account $storage_account_name --log-version 2 --retention 1 -o none
done <<< "$nsg_list"
}
# Create RG, Vnet, NSG
az group create -n $rg -l $location
az network vnet create -g $rg -n $vnet_name --address-prefix $vnet_prefix --subnet-name $subnet_name --subnet-prefix $subnet_prefix -l $location
az network nsg create -n $nsg_name -g $rg -l $location
az network nsg rule create -n allowSSHin --nsg-name $nsg_name -g $rg --priority 1000 --destination-port-ranges 22 --access Allow --protocol Tcp
# 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
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)
# Create storage account
storage_account_name=$(az storage account list -g $rg --query '[].name' -o tsv 2>/dev/null) # Retrieve the storage account name if it already existed
if [[ -z "$storage_account_name" ]]
then
storage_account_name=log$RANDOM
az storage account create -n $storage_account_name -g $rg --sku Standard_LRS --kind StorageV2 -l $location
fi
# Enable flow logs
az network watcher flow-log create -l $location -n "flowlog-$location" -g $rg \
--nsg $nsg_name --storage-account $storage_account_name --log-version 2 --retention 7 \
--workspace $logws_id --interval 10 --traffic-analytics true
# Generate cloudinit file to create VMs
cat <<EOF > $cloudinit_file
#cloud-config
packages:
- jq
- pv
EOF
# Create VMs
for i in {1..$no_of_vms}
do
vm_name="vm$(printf "%02d" i)"
az vm create -n $vm_name -g $rg --image UbuntuLTS --generate-ssh-keys --size $vm_size \
--vnet-name $vnet_name --subnet $subnet_name --public-ip-address "${vm_name}-pip" --nsg $nsg_name \
--custom-data $cloudinit_file -l $location --no-wait
done
# Wait some seconds and create JSON with required IPs
sleep 60
ip_json=$(az vm list-ip-addresses -g $rg -o json)
# Get the private IP of a specific VM out of the output of the command "az vm list-ip-addresses"
function get_private_ip () {
echo $1 | jq -r '.[] | select(.virtualMachine.name == "'$2'") | .virtualMachine.network.privateIpAddresses[0]'
}
# Get the public IP of a specific VM out of the output of the command "az vm list-ip-addresses"
function get_public_ip () {
echo $1 | jq -r '.[] | select(.virtualMachine.name == "'$2'") | .virtualMachine.network.publicIpAddresses[0].ipAddress'
}
# Start traffic generation
flows_array=($(convert_string_to_array $flows ','))
for flow in "${flows_array[@]}"
do
# echo "Processing flow $flow..."
sources=$(echo $flow | cut -d':' -f 1)
destinations=$(echo $flow | cut -d':' -f 2)
port=$(echo $flow | cut -d':' -f 3)
kb_min=$(echo $flow | cut -d':' -f 4)
src_array=($(convert_string_to_array $sources ';'))
dst_array=($(convert_string_to_array $destinations ';'))
for dst in $dst_array; do
# Start nc listening on port for destination
dst_vm_name="vm$(printf "%02d" $dst)"
dst_pip=$(get_public_ip $ip_json "$dst_vm_name")
dst_ip=$(get_private_ip $ip_json "$dst_vm_name")
echo "Running \"nc -dlk -p ${port}\" on ${dst_vm_name}, ${dst_pip}"
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$dst_pip" "nc -dlk -p $port > /dev/null &"
for src in $src_array; do
# Configure crontab entry to send traffic every minute
src_vm_name="vm$(printf "%02d" $src)"
src_pip=$(get_public_ip $ip_json "$src_vm_name")
cmd='(crontab -l 2>/dev/null; echo "* * * * * dd if=/dev/urandom bs=1000 count='${kb_min}' | pv -L 10M | nc '${dst_ip}' '${port}'") | crontab -'
echo "Adding crontab entry for ${src_vm_name}, ${src_pip}"
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$src_pip" "$cmd"
done
done
done
# Diagnostics
az network watcher flow-log list -o table -l $location
src_vm_name=vm01
dst_vm_name=vm03
port=1433
src_pip=$(get_public_ip $ip_json "$src_vm_name")
dst_pip=$(get_public_ip $ip_json "$dst_vm_name")
dst_ip=$(get_private_ip $ip_json "$dst_vm_name")
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$src_pip" "crontab -l | grep \"$dst_ip $port\""
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$src_pip" "nc -vz $dst_ip $port"
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$dst_pip" "ps -ef | grep \"nc -dlk -p $port\" | grep -v grep"
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$dst_pip" "sudo netstat -tunlp | grep \":$port\""
# Simulate port scan
src_vm_name=vm01
port=22
src_pip=$(get_public_ip $ip_json "$src_vm_name") && echo $pip
for i in {1..$no_of_vms}
do
dst_vm_name="vm$(printf "%02d" i)"
dst_ip=$(get_private_ip $ip_json "$dst_vm_name")
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$src_pip" "nc -vz $dst_ip $port"
done
# Cleanup
az network watcher flow-log delete -l $location -n