-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFW_API_Update_URLS_Group.ps1
More file actions
342 lines (331 loc) · 14.8 KB
/
Copy pathFW_API_Update_URLS_Group.ps1
File metadata and controls
342 lines (331 loc) · 14.8 KB
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
# This script use 2 json files
# 1. contains firewall list
# 2. contains a list of url that have to be pushed to firewall WebFilter URL Group
#
# ---- CLI Parameters ----
param (
# Param01 is firewalls list.
[Parameter(Mandatory = $true, HelpMessage = "Please provide Firewalls list file in JSON format :")]
[string]$Param01 = "",
# Param02 is the input JSON file taht contains all URL that have to be pushed to firewalls
[Parameter(Mandatory = $true, HelpMessage = "Please provide Output file name with full path :")]
[string]$Param02 = ""
# Param03 is the Log filename
# [Parameter(Mandatory = $true, HelpMessage = "Please provide Output Log file :")]
# [string]$Param03 = ""
)
Clear-Host
Write-Output "==============================================================================="
Write-Output "Sophos Firewall API - Update Web Filtering URL lists"
Write-Output "==============================================================================="
Write-Output ""
Write-Output "It requires 3 parameters : "
Write-Output ""
Write-Host $MyInvocation.MyCommand.Name" param01=<firewall_list.json> param02<=url_list.json> param03<=Ouput Log file.txt>" -ForegroundColor Green
Write-Output ""
# ---- Functions ----
function Split-StringAfterEqualSign
{
param (
[string]$inputString
)
try {
if (-not $inputString.Contains("=")) {
throw "Input string does not contain an '=' sign."
}
$splitString = $inputString -split "="
write-host $splitString[1]
if (($null -eq $splitString[1]) -or ($splitString[1] -eq "")) {
throw "No value after '=' for $splitString"
}
return @{
Key = $splitString[0]
Value = $splitString[1]
}
}
catch {
Write-Error "An error occurred : $_"
exit 5
}
}
function GenearateRandomString
{
param
(
[Int16]$NbCharacters
)
$UsedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
$RandomString = -join((1..$NbCharacters)| ForEach-Object {$UsedCharacters[(Get-Random -Maximum $UsedCharacters.Length)] })
return $RandomString
}
function BuildURLFunction
{
param (
[string]$FuncFwIP,
[string]$FuncFwPort
)
$FuncUrlLogin = "https://" + $FuncFwIP + ":" + $FuncFwPort + "/webconsole/APIController"
[string]$WholeCompletedURL = $FuncUrlLogin
return $WholeCompletedURL
}
function BuildURLPayload
{
param (
[string]$PayloadFwLogin,
[string]$PayloadFwPwd,
[string]$PayloadStrLength,
[string]$PayloadParameters
)
$TransactionId= GenearateRandomString -NbCharacters $PayloadStrLength
# $PayLoadString = ' -Form "reqxml=<'
$PayLoadString = "?reqxml="
$PayLoadLogin = "<Request><Login><Username>"+ $PayloadFwLogin+"</Username><Password>"+$PayloadFwPwd+"</Password></Login>"
$PayloadCommand = "<Set><WebFilterURLGroup>"
$PayloadlEnding = "</WebFilterURLGroup></Set></Request>"
[string]$WholePayload = $PayLoadString + $PayLoadLogin + $PayloadCommand + $PayloadParameters + $PayloadlEnding
return $WholePayload
}
try
{
if (($null -eq $Param01) -or ($Param01 -eq ""))
{
write-host""
Write-Host " No input firewalls list has been provided " -ForegroundColor Red -BackgroundColor Yellow -NoNewline
write-host""
exit 10
}
if (($null -eq $Param02) -or ($Param02 -eq ""))
{
write-host""
Write-Host " No Input WebURL file list has been provided " -ForegroundColor Red -BackgroundColor Yellow -NoNewline
write-host""
exit 20
}
# if (($null -eq $Param03) -or ($Param03 -eq "")) {
# write-host""
# Write-Host " No Output log file name has been provided " -ForegroundColor Red -BackgroundColor Yellow -NoNewline
# write-host""
# exit 4
# }
else
{
# Check values are not empties
$result01 = Split-StringAfterEqualSign -inputString $Param01
# Write-Host "Param01 Name : "$result01.Key
# Write-Host "Param01 content : "$result01.Value
$Input01Name = $result01.key
$Input01Value = $result01.Value
$result02 = Split-StringAfterEqualSign -inputString $Param02
# Write-Host "Param02 Name : "$result02.Key
# Write-Host "Param02 content : "$result02.Value
$Input02Name = $result02.key
$Input02Value = $result02.Value
# $result03 = Split-StringAfterEqualSign -inputString $Param03
# Write-Host "Param03 Name : "$result03.Key
# Write-Host "Param03 content : "$result03.Value
# $Input03Name = $result03.key
# $Input03Value = $result03.Value
}
# Ready to go
if (-Not (Test-Path -Path $Input01Value))
{
# Input file does not exist, we should stop
Write-Host "File "$Input01Value" does not exist"
exit 11
}
else
{
try
{
$file01 = Get-Item $Input01Value
$file01.OpenRead().Close()
}
catch
{
Write-Host "File "$Input01Value" exists but can not be accessed in Read mode"
exit 12
}
}
if (-Not (Test-Path -Path $Input02Value))
{
# Input file does not exist, we should stop
Write-Host "File "$Input02Value" does not exist"
exit 21
}
else
{
# All input files exist We can continue
try
{
$file02 = Get-Item $Input02Value
$file02.OpenRead().Close()
}
catch
{
Write-Host "File "$Input02Value" exists but can not be accessed in Read mode"
exit 22
}
try
{
$ImportJsonFwFile = Get-content -Path $Input01Value -Raw | ConvertFrom-Json
$ImportJsonURLFile = Get-content -Path $Input02Value -Raw | ConvertFrom-Json
$ArrayFwList = @($ImportJsonFwFile)
$SortedArrayFwList = $ArrayFwList | Sort-Object -Property IPAddress
$ArrayUrlListForFw = @($ImportJsonURLFile)
# $ArrayUrlListForFw
# $SortedArrayFwList
# Convert table to xml Document
$FwCounter = 0
$WholeStepCounter=0
foreach ($FirewallEntry in $ArrayUrlListForFw)
{
$ComputingFw = $FirewallEntry[$FwCounter].Firewall
write-host ""
write-host "---------------------------------"
write-host "Firewall traité :"$ComputingFw
write-host "---------------------------------"
$UrlListCounter = 0
foreach ($FirewallURLList in $FirewallEntry[$FwCounter].FirewallURLS)
{
$ComputingURLList = $FirewallURLList[$UrlListCounter]
$UrlListNumber = 0
$EntriesInListNumber = $ComputingURLList[$UrlListNumber].XmlUrlList | Measure-Object | Select-Object -ExpandProperty Count
$UrlListName=$ComputingURLList.Name
# write-host "Number of entries into list :"$UrlListName" :"$EntriesInListNumber
# write-host "Destination firewall :"$ComputingFw
# XML content stored in a string
# Beginning of the string that will compose the XLM entries
$xmlContentStart = "<Name>$UrlListName</Name>"
# Iterates to build the list of objects
[string]$xmlContentObjects=""
for ($i = 0; $i -lt $EntriesInListNumber; $i++)
{
$xmlContentObjects+="<URL>$($ComputingURLList[$($UrlListNumber)].XmlUrlList[$($i)])</URL>"
}
# End of the string
$xmlContentEnding="<Description>$($ComputingURLList.Description)</Description><URLlist>"
#Build the whole string
$xmlContent=$xmlContentStart+$xmlContentEnding+$xmlContentObjects+"</URLlist>"
# write-host "URL List : "$xmlContent
$WholeStepCounter++
# write-host "IP Address firewall to update :"$ComputingFw
write-host ""
foreach ($SearchedFirewall in $SortedArrayFwList.IPAddress)
{
if ($SearchedFirewall -eq $ComputingFw)
{
try {
$FwAdminIpAddress = $SortedArrayFwList.IPAddress
# Write-Host "IP Address :"$FwAdminIpAddress
$FwAdminListeningPort = $SortedArrayFwList.AccesPortNb
# Write-Host "Port Number :"$FwAdminListeningPort
$EncryptedPassword = $SortedArrayFwList.Password
$Password = ConvertTo-SecureString -String $EncryptedPassword
$Credentials = New-Object System.Management.Automation.PSCredential ($SortedArrayFwList.LoginName, $Password)
# Write-Host "Credentials Login name : $($Credentials.UserName)"
# Write-Host "Credentials Login Password : $($Credentials.GetNetworkCredential().Password)"
$AccessTimeOut = $SortedArrayFwList.TimeOut
# Write-Host "Access TimeOut :"$AccessTimeOut
Write-Output "Identifiants pour $SearchedFirewall trouvés !"
# Faites quelque chose ici
$FuncURL = BuildURLFunction -FuncFwIP $FwAdminIpAddress -FuncFwPort $FwAdminListeningPort
$FormPayload = BuildURLPayload -PayloadFwLogin $($Credentials.UserName) -PayloadFwPwd $($Credentials.GetNetworkCredential().Password) -PayloadParameters $xmlContent -PayloadStrLength 8
# $PayloadDict = @{$FormPayload}
try
{
write-host "Form Payload :" $FormPayload
write-host "FuncURL Reply :" $FuncURL
$FullURI = $FuncURL+$FormPayload
# $HttpResult = Invoke-RestMethod -Uri $FuncURL -Method 'Post' -ContentType "application/xml" -SkipCertificateCheck -Body $FormPayload -TimeoutSec $AccessTimeOut
#$HttpResult = Invoke-RestMethod -SkipCertificateCheck -Uri $FuncURL -Method "Post" -Body $FormPayload -TimeoutSec $AccessTimeOut
$HttpResult = Invoke-RestMethod -Uri $FullURI -Method 'Post' -ContentType "application/xml" -SkipCertificateCheck -Body $FormPayload -TimeoutSec $AccessTimeOut
write-host "URL Passée :" $FullURI
$HttpResult.OuterXml
Write-host " Resultat :"$HttpResult
}
catch
{
Write-host "Error calling URL"
Write-Host "Error : $($_.Exception.Message)"
}
break
}
catch {
Write-Host "Error encountered while parsing the file "$Input01Value
Write-Host "Error $($_.Exception.Message)"
exit 1
}
}
else
{
write-host "LogFile update"
}
}
$UrlListNumber++
}
$UrlListCounter++
$WholeStepCounter++
}
$FwCounter++
$MainTable = [System.Collections.ArrayList]::new()
foreach ($Item in $ImportJsonFwFile)
{
try
{
# Write-Host "---------------------------------------------------------"
# Write-Host "Iteration Number :"$Counter
$FwAdminIpAddress = $Item.IPAddress
# Write-Host "IP Address :"$FwAdminIpAddress
$FwAdminListeningPort = $Item.AccesPortNb
# Write-Host "Port Number :"$FwAdminListeningPort
$EncryptedPassword = $Item.Password
$Password = ConvertTo-SecureString -String $EncryptedPassword
$Credentials = New-Object System.Management.Automation.PSCredential ($Item.LoginName, $Password)
# Write-Host "Credentials Login name : $($Credentials.UserName)"
# Write-Host "Credentials Login Password : $($Credentials.GetNetworkCredential().Password)"
$AccessTimeOut = $Item.TimeOut
# Write-Host "Access TimeOut :"$AccessTimeOut
$FuncURL = BuildURLFunction -FuncFwIP $FwAdminIpAddress -FuncFwPort $FwAdminListeningPort -FuncFwLogin $($Credentials.UserName) -FuncFwPwd $($Credentials.GetNetworkCredential().Password)
# Write-Host $FuncURL
# try {
# $HttpResult = (Invoke-RestMethod -Uri $FuncURL -Method Post -ContentType "application/xml" -SkipCertificateCheck -TimeoutSec $AccessTimeOut)
# $EntriesListArray = TranformInterfacesXmlListToArray -XmlDocument $HttpResult
# $Firewalls_Object = [PSCustomObject]@{
# Firewall = $Item.IPAddress
# FirewallURLS = $EntriesListArray
# }
# $Firewalls_Object
# $MainTable.add($Firewalls_Object) | Out-Null
# }
# catch {
# Write-host "Error calling URL"
# Write-Host "Error : $($_.Exception.Message)"
# }
}
catch
{
Write-Host "Error encountered while parsing "$InputFile
Write-Host "Error $($_.Exception.Message)"
exit 1
}
}
Write-Host ""
# Write-Host "Firewall Number in "$Input01Value ":" $($FwCounter+1)
$FwCounter++
}
catch
{
write-host ""
Write-Error "An error occurred: $_"
write-host ""
exit 1
}
}
}
catch
{
write-host ""
Write-Error "An error occurred: $_"
write-host ""
exit 1
}