-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFW_API_Grab_XOPS.ps1
225 lines (211 loc) · 9.51 KB
/
FW_API_Grab_XOPS.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
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
<#
.SYNOPSIS
#This script reads a JSON file that contains The X-OPS Setup
.DESCRIPTION
It reads the following columns : IP ADDRESS, LOGIN NAME, PASSWORD, Access TimeOut in seconds
It must be used as follows
.PARAMETER Param01
Description Input file that lists all details about firewalls that have to be requested. Please provide filename with fullpath access
.PARAMETER Param02
Description Ouput file that is generated with all details gathered from firewalls. Please provide filename with fullpath access
.EXAMPLE
<script_name>.ps1 input=<firewalls list> output=<Generated_xops_setup.json>
#>
# ---- CLI Parameters ----
#
param (
[Parameter(Mandatory = $true, HelpMessage = "Please provide Firewalls list file in JSON format :")]
[string]$Param01 = "",
# Param01 is firewalls list.
[Parameter(Mandatory = $true, HelpMessage = "Please provide Output file name with full path :")]
[string]$Param02 = ""
# Param02 is the Input JSON file that contains the URL that aree retrieved from each firewall in the list
)
Clear-Host
Write-Output "============================================================="
Write-Output "Sophos Firewall API - Retrieve Sophs X-OPS Threat Feeds Setup"
Write-Output "============================================================="
Write-Output ""
Write-Output "It requires 2 parameters : "
Write-Output ""
Write-Host $MyInvocation.MyCommand.Name" param01=<firewall_list.json> param02=<X-OPS_Status.json>" -ForegroundColor Green
Write-Output ""
# ---- Functions ----
function BuildURLFunction {
param (
[string]$FuncFwIP,
[string]$FuncFwPort,
[string]$FuncFwLogin,
[string]$FuncFwPwd,
[string]$FuncFwTimeOut
)
$WholeCompletedURL = "https://" + $FuncFwIP + ":" + $FuncFwPort + "/webconsole/APIController?reqxml=<Request><Login><Username>" + $FuncFwLogin + "</Username><Password>" + $FuncFwPwd + "</Password></Login><GET>"
$WholeCompletedURL += "<ATP/>"
$WholeCompletedURL += "</GET></Request>"
return $WholeCompletedURL
}
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 4
}
}
function TranformInterfacesXmlListToArray {
param (
[xml]$XmlDocument
)
$XmlTag = $XmlDocument.SelectNodes("//ATP")
$OutTagArray = @()
foreach ($Node in $XmlTag) {
$OutTag = $Node.OuterXml
$OutTagArray = @{
ThreatProtectionStatus = $Node.ThreatProtectionStatus
Policy = $Node.Policy
HostException = $Node.HostException.Host
ThreatException = $Node.ThreatException.Threat
InspectContent = $Node.InspectContent
}
}
return $OutTagArray
}
# ---- Main program ----
# Checks if firewalls list can exists and can be read
try {
if (($null -eq $Param01) -or ($Param01 -eq "")) {
Write-Host " No input firewalls list has been provided " -ForegroundColor Red -BackgroundColor Yellow -NoNewline
write-host""
write-host""
exit 2
}
if (($null -eq $Param02) -or ($Param02 -eq "")) {
Write-Host " No Output X-OPS Threat file list has been provided " -ForegroundColor Red -BackgroundColor Yellow -NoNewline
write-host""
write-host""
exit 3
}
# if (($null -eq $Param03) -or ($Param03 -eq "")) {
# Write-Host " No Output log file list has been provided " -ForegroundColor Red -BackgroundColor Yellow -NoNewline
# write-host""
# write-host""
# exit 4
# }
else {
#
# ALL Is OK now we can compute.
#
$result01 = Split-StringAfterEqualSign -inputString $Param01
# Write-Host "Param01 Name : "$result01.Key
# Write-Host "Param01 content : "$result01.Value
$Input01Name = $result01.key
$Input01Value = $result01.Value
# Write-Host "First Parameter name : "$Input01Name
# Write-Host "Input Filename : "$Input01Value
$InputFile = $Input01Value
$result02 = Split-StringAfterEqualSign -inputString $Param02
# Write-Host "Param02 Name : "$result02.Key
# Write-Host "Param02 content : "$result02.Value
$Input02Name = $result02.key
$Input02Value = $result02.Value
# Write-Host "Second Parameter name : "$Input02Name
# Write-Host "Output Filename : "$Input02Value
# $result03 = Split-StringAfterEqualSign -inputString $Param03
# Write-Host "Param03 Name : "$result03.Key
# Write-Host "Param03 content : "$result03.Value
# $Input03Name = $result03.key
# $Input03Value = $result03.Value
$OutputFile = $Input02Value
if (Test-Path -Path $InputFile) {
# Input file exists, we can continue
try {
$file = Get-Item $InputFile
$file.OpenRead().Close()
$ImportJsonFile = Get-content -Path $InputFile | ConvertFrom-Json
$Counter = 0
$MainTable = [System.Collections.ArrayList]::new()
foreach ($Item in $ImportJsonFile) {
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)
# $XmlContent = $HttpResult.OuterXml
[xml]$XmlContent = $HttpResult
# write-host "Xml Content :" $XmlContent
# write-host "HTTP Result :"$HttpResult
# $EntriesListArray = TranformInterfacesXmlListToArray -XmlDocument $HttpResult
$ValuesAtpTable = TranformInterfacesXmlListToArray -XmlDocument $HttpResult
$MainTable = @()
foreach ($Name in $ValuesAtpTable) {
$MainTable += [PSCustomObject]@{
Firewall = $FwAdminIpAddress
ATPValues = $Name
}
}
}
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
}
}
$Counter++
}
catch {
Write-Host "File "$FullFileName" exists but can not be accessed in Read mode"
exit 2
}
}
else {
# Input file does not exist exit program
Write-Host "File "$InputFile" does not exist"
exit 3
}
}
}
catch {
write-host ""
Write-Error "An error occurred: $_"
write-host ""
exit 1
}
#End of loops
#$MainTable | Format-Table -AutoSize
$Table_In_JSON = $($MainTable) | ConvertTo-Json -Depth 2
# write-host $Table_In_JSON
$Table_In_JSON | Out-File -FilePath $OutputFile utf8