-
Notifications
You must be signed in to change notification settings - Fork 7
/
AllowCountryNSG.ps1
193 lines (170 loc) · 5.37 KB
/
AllowCountryNSG.ps1
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
#requires -Version 3.0 -Modules Az.Network
function Get-OdCountryIpAddressCidrList
{
[CmdletBinding(
DefaultParameterSetName = 'code'
)]
param (
# ISO 3166 country
[Parameter(
ParameterSetName = 'country'
)]
[string[]]
$CountryName,
# ISO 3166 2 character code
[Parameter(
ParameterSetName = 'code'
)]
[string[]]
$CountryAlpha2Code,
# IP protocol
[Parameter(
Mandatory
)]
[ValidateSet('Ipv4', 'Ipv6')]
[string]
$Protocol
)
begin {
$ErrorActionPreference = 'stop'
try
{
$CountryCodeUrl = 'https://restcountries.eu/rest/v2/all'
$Iso3166CountryList = Invoke-RestMethod -Uri $CountryCodeUrl -Verbose:$false -ErrorAction Stop
$date = Get-Date -Format 's'
$CountryList = [System.Collections.Generic.List[psobject]]::new()
}
# NOTE: When you use a SPECIFIC catch block, exceptions thrown by -ErrorAction Stop MAY LACK
# some InvocationInfo details such as ScriptLineNumber.
# REMEDY: If that affects you, remove the SPECIFIC exception type [System.ArgumentException] in the code below
# and use ONE generic catch block instead. Such a catch block then handles ALL error types, so you would need to
# add the logic to handle different error types differently by yourself.
catch [System.ArgumentException]
{
# get error record
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
$info
}
catch
{
$PSCmdlet.WriteError(
$PSItem
)
}
}
process {
$ErrorActionPreference = 'stop'
try
{
if ($PSCmdlet.ParameterSetName -eq 'country')
{
foreach ($name in $CountryName)
{
$country = $Iso3166CountryList | Where-Object -Property name -EQ -Value $name
if ($country)
{
$CountryList.Add($country)
}
else
{
$CountryNotFoundMessage = "Cound not find ISO 3166-2 country '$name'. ISO 3166-2 list retrieved from $CountryCodeUrl on $date."
Write-Error -Message $CountryNotFoundMessage
}
}
}
else
{
foreach ($code in $CountryAlpha2Code)
{
$country = $Iso3166CountryList | Where-Object -Property alpha2Code -EQ -Value $code
if ($country)
{
$CountryList.Add($country)
}
else
{
$CodeNotFoundMessage = "Cound not find ISO 3166-2 country code for '$code' in list. ISO 3166-2 list retrieved from $CountryCodeUrl on $date."
Write-Error -Message $CodeNotFoundMessage
}
}
}
}
catch
{
$PSCmdlet.WriteError(
$PSItem
)
}
}
end {
$ErrorActionPreference = 'stop'
try
{
$headers = @{
Accept = 'application/vnd.github.v3.raw'
}
$IpList = foreach ($entry in $CountryList)
{
$IsoCode = $entry.alpha2Code
$IsoName = $entry.Name
$path = '{0}/{1}.cidr' -f $Protocol, $IsoCode
[uri]$uri = 'https://api.github.com/repos/herrbischoff/country-ip-blocks/contents/{0}' -f $path.ToLower()
Write-Verbose -Message "Retrieving country IPs for $IsoName ($IsoCode)"
[PSCustomObject][ordered]@{
Country = $IsoName
Code = $IsoCode
TopLevelDomain = $entry.topLevelDomain
CidrList = ((Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -Verbose:$false).Split([System.Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries) -split "`n").Trim()
CidrListSource = 'https://github.com/herrbischoff/country-ip-blocks/blob/master/{0}' -f $path.ToLower()
}
}
$IpList | Sort-Object -Property Code
}
catch
{
$PSCmdlet.WriteError(
$PSItem
)
}
}
}
$CountryCodeUrl = 'https://restcountries.eu/rest/v2/all'
$Iso3166CountryList = Invoke-RestMethod -Uri $CountryCodeUrl -Verbose:$false
$nsg = Get-AzNetworkSecurityGroup -Name 'LOL'
$port = 8081
ForEach ($country in $Iso3166CountryList)
{
$CountryName = $country.name
If ($CountryName -eq "New Zealand") {
$CN = $country.alpha2Code
$IPRanges = Get-OdCountryIpAddressCidrList -Protocol Ipv4 -CountryAlpha2Code $CN
$IPRangeCidr = $IPRanges.CidrList
ForEach ($range in $IPRangeCidr)
{
IF ($count -eq $null)
{
$count = 1
}
$char = ([char](96 + $count))
$count++
IF ($priority -eq $null)
{
$priority = 100
}
$priority++
$nsg | Add-AzNetworkSecurityRuleConfig -Name "Allow_$($CN)_$($char)" -Description "Allows access from $Countryname" -Access Allow `
-Protocol * -Direction Inbound -Priority $priority -SourceAddressPrefix "$range" -SourcePortRange * `
-DestinationAddressPrefix * -DestinationPortRange *
$nsg | Set-AzNetworkSecurityGroup
}}
}