-
Notifications
You must be signed in to change notification settings - Fork 4
/
Update-Unitrends.ps1
256 lines (196 loc) · 10.2 KB
/
Update-Unitrends.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
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
<#
.SYNOPSIS
This cmdlet is used to update the Unitrends appliance with the use of it's API
.DESCRIPTION
This cmdlet updates the Unitrends appliance by creating an authenticated session to the Unitrends appliance. The auth token is then used to contact the API and update the appliance.
.PARAMETER Server
This parameter is used to define the FQDN or IP address of the Unitrends appliance
.PARAMETER Port
This parameter defines the HTTP or HTTPS port that is open on your Unitrends appliance. The default value is 443
.PARAMETER Protocol
This parameter defines whether you are using HTTP or HTTPS. The default value is HTTPS
.PARAMETER Username
This parameter is used to define the user you wish to authenticate as
.PARAMETER Passwd
This parameter accepts the password used to authenticate the -Username user you define
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: rosborne@osbornepro.com
.LINK
https://osbornepro.com/f/dns-protections-and-applications
https://osbornepro.com
https://btpssecpack.osbornepro.com
https://writeups.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.INPUTS
None
.OUTPUTS
None
#>
Function Update-Unitrends {
[CmdletBinding()]
param(
[Parameter(
Position=0,
Mandatory=$True,
ValueFromPipeline=$False,
HelpMessage="`n[H] Define the FQDN or IP address of your Unitrends Appliance. `n[E] EXAMPLE: unitrends.domain.com")] # End Parameter
[String]$Server,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False)] # End Parameter
[Int32]$Port = 443,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False)] # End Parameter
[ValidateSet('http','https')]
[String]$Protocol = 'https',
[Parameter(
Position=1,
Mandatory=$True,
ValueFromPipeline=$False,
HelpMessage="`n[H] Enter the username you wish to authenticate to your Unitrends appliance with. `n[E] EXAMPLE: root")] # End Parameter
[String]$Username,
[Parameter(
Position=2,
Mandatory=$True,
ValueFromPipeline=$False,
HelpMessage="`n[H] Enter the password for the username you are authenticating as, EXAMPLE: Password123!")] # End Parameter
[String]$Passwd
) # End param
Write-Verbose "Sending authentication request to appliance"
$LoginURL = "$Protocol" + "://" + "$Server" + ":" + "$Port" + "/api/login"
$Headers = New-Object -TypeName "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36')
$Headers.Add("Host","$Server")
$Body = @{username=$Username;password=$Passwd} | ConvertTo-Json
$LoginRequest = Invoke-WebRequest -Method POST -UseBasicParsing -Uri $LoginURL -SessionVariable $WebSession -Headers $Headers -Body $Body -ContentType "application/json"
If ($LoginRequest.StatusCode -eq 201) {
Write-Output "[*] Successfully authenticated to appliance"
} # End If
Else {
Throw "[x] Invalid credentials entered"
} # End Else
Write-Verbose "Checking for available updates"
$AuthToken = ($LoginRequest.Content | ConvertFrom-Json).auth_token
$UpdateURL = "$Protocol" + "://" + "$Server" + ":" + "$Port" + "/api/updates"
$Headers = New-Object -TypeName "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36')
$Headers.Add("Host","$Server")
$Headers.Add("AuthToken","$AuthToken")
$CheckUpdateRequest = Invoke-WebRequest -Method GET -Uri $UpdateURL -WebSession $WebSession -Headers $Headers -ContentType "application/json"
$UpdatesAvailable = ($CheckUpdateRequest.Content | ConvertFrom-Json).data.updates
If ($Null -eq $UpdatesAvailable) {
Write-Output "[*] Unitrends appliance fully up to date"
} # End If
Else {
$InstallUpdates = Invoke-WebRequest -UseBasicParsing -Method POST -Uri $UpdateURL -WebSession $WebSession -Headers $Headers -ContentType "application/json"
If ($InstallUpdates.StatusCode -eq 201) {
Write-Output "[*] Update request successfully initiated"
If (0 -ne (($InstallUpdates.Content | ConvertFrom-Json).result.code)) {
$Results = ($InstallUpdates.Content | ConvertFrom-Json).result
$Css = @"
<style>
table {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
"@ # End CSS
$PreContent = "<Title>Unitrends Appliance Updated</Title>"
$NoteLine = "This Message was Sent on $(Get-Date -Format 'MM/dd/yyyy HH:mm:ss')"
$PostContent = "<br><p><font size='2'><i>$NoteLine</i></font>"
$MailBody = $Results | ConvertTo-Html -Head $Css -PostContent $PostContent -PreContent $PreContent -Body "<br>This email has been sent to inform you that the Unitrends appliance update request has been initated successfully.<br><br><hr><br><br>" | Out-String
# Below output can be generated using https://github.com/tobor88/PowerShell/blob/master/Hide-PowerShellScriptPassword.ps1
$Var1 = 'BBBBBBBBBBBBBBBBBBBB'
$User1 = "from-email@domain.com"
$PasswordFile1 = "C:\Users\Public\Documents\PwdHide\$Var1.AESpassword.txt"
$KeyFile1 = "C:\Users\Public\Documents\PwdHide\$Var1"
$Key1 = Get-Content -Path $KeyFile1
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User1, (Get-Content -Path $PasswordFile1 | ConvertTo-SecureString -Key $Key1)
Send-MailMessage -From $User1 -To $To -Subject "Unitrends Appliance Updated" -BodyAsHtml -Body "$MailBody" -Credential $Cred -UseSSL -Port 587 -SmtpServer smtp.office365.com
} # End If
} # End If
ElseIf ($InstallUpdates.StatusCode -eq 500) {
Write-Output "[x] Reference returned error code at https://github.com/unitrends/unitrends-api-doc/wiki/API-Document#result-format"
$Code = ($InstallUpdates.Content | ConvertFrom-Json).result.code | Out-String
Write-Warning "[!] ERROR CODE: $Code `nUpdate request failed"
$Results = ($InstallUpdates.Content | ConvertFrom-Json).result
$Css = @"
<style>
table {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
"@ # End CSS
$PreContent = "<Title>Failed Unitrends Appliance Update</Title>"
$NoteLine = "This Message was Sent on $(Get-Date -Format 'MM/dd/yyyy HH:mm:ss')"
$PostContent = "<br><p><font size='2'><i>$NoteLine</i></font>"
$MailBody = $Results | ConvertTo-Html -Head $Css -PostContent $PostContent -PreContent $PreContent -Body "<br>This email has been sent to inform you that the Unitrends appliance update request has failed. Please update manually and verify the update script is working. A password may need to be updated.<br> ERROR CODE: $Code <br>Error code reference at https://github.com/unitrends/unitrends-api-doc/wiki/API-Document#result-format<br><br><hr><br><br>" | Out-String
# Below output can be generated for your user using https://github.com/tobor88/PowerShell/blob/master/Hide-PowerShellScriptPassword.ps1
$Var1 = 'BBBBBBBBBBBBBBBBBBBB'
$User1 = "from-user@domain.com"
$PasswordFile1 = "C:\Users\Public\Documents\PwdHide\$Var1.AESpassword.txt"
$KeyFile1 = "C:\Users\Public\Documents\PwdHide\$Var1"
$Key1 = Get-Content -Path $KeyFile1
$Cred1 = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User1, (Get-Content -Path $PasswordFile1 | ConvertTo-SecureString -Key $Key1)
Send-MailMessage -From $From -To $To -Subject "FAILED Unitrends Appliance Update Request" -BodyAsHtml -Body "$MailBody" -Credential $Cred1 -UseSSL -Port 587 -SmtpServer smtp.office365.com
} # End Else
} # End Else
} # End Function Update-Unitrends
# Update these values. $Username is the user that authenticates to Unitrends and $To is the email address to send notifications too
$Username = 'root'
$To = "to-user@domain.com"
# Below Output can be generated for your user using https://github.com/tobor88/PowerShell/blob/master/Hide-PowerShellScriptPassword.ps1
$Var = 'AAAAAAAAAAAAAAAAAAAA'
$PasswordFile = "C:\Users\Public\Documents\PwdHide\$Var.AESpassword.txt"
$KeyFile = "C:\Users\Public\Documents\PwdHide\$Var"
$Key = Get-Content -Path $KeyFile
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, (Get-Content -Path $PasswordFile | ConvertTo-SecureString -Key $Key)
$SecurePass = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Cred.Password)
$PassString = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($SecurePass)
Update-Unitrends -Server $UnitrendServer -Port 443 -Protocol https -Username $Username -Passwd $PassString
Remove-Variable -Name PassString,PasswordFile