-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSession.ps1
340 lines (296 loc) · 10.7 KB
/
Session.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
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
#region Session
<#
.Synopsis
Create a new Nessus Session.
.DESCRIPTION
Creates a new Nessus Session.
.EXAMPLE
New-NessusSession -ComputerName 192.168.1.205 -Credentials (Get-Credential carlos) -Verbose
VERBOSE: POST https://192.168.1.205:8834/session with -1-byte payload
VERBOSE: received 60-byte response of content type application/json
Id : 0
URI : https://192.168.1.205:8834
Token : 50168808199c1a2197d180fa62fb9cc3cb9108054911476a
#>
function New-NessusSession
{
[CmdletBinding()]
Param
(
# Nessus Server IP Address or FQDN to connect to.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$ComputerName,
# Port number of the Nessus web service. Default 8834
[int]
$Port = 8834,
# Credentials for connecting to the Nessus Server
[Parameter(Mandatory=$true,
Position=1)]
[Management.Automation.PSCredential]$Credentials
)
Begin
{
}
Process
{
if ([System.Net.ServicePointManager]::CertificatePolicy.ToString() -ne 'IgnoreCerts')
{
$Domain = [AppDomain]::CurrentDomain
$DynAssembly = New-Object System.Reflection.AssemblyName('IgnoreCerts')
$AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('IgnoreCerts', $false)
$TypeBuilder = $ModuleBuilder.DefineType('IgnoreCerts', 'AutoLayout, AnsiClass, Class, Public, BeforeFieldInit', [System.Object], [System.Net.ICertificatePolicy])
$TypeBuilder.DefineDefaultConstructor('PrivateScope, Public, HideBySig, SpecialName, RTSpecialName') | Out-Null
$MethodInfo = [System.Net.ICertificatePolicy].GetMethod('CheckValidationResult')
$MethodBuilder = $TypeBuilder.DefineMethod($MethodInfo.Name, 'PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask', $MethodInfo.CallingConvention, $MethodInfo.ReturnType, ([Type[]] ($MethodInfo.GetParameters() | % {$_.ParameterType})))
$ILGen = $MethodBuilder.GetILGenerator()
$ILGen.Emit([Reflection.Emit.Opcodes]::Ldc_I4_1)
$ILGen.Emit([Reflection.Emit.Opcodes]::Ret)
$TypeBuilder.CreateType() | Out-Null
# Disable SSL certificate validation
[System.Net.ServicePointManager]::CertificatePolicy = New-Object IgnoreCerts
}
# Force usage of TSL1.2 as Nessus web server only supports this and will hang otherwise
# Source: https://stackoverflow.com/questions/32355556/powershell-invoke-restmethod-over-https
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$SessionProps = New-Object -TypeName System.Collections.Specialized.OrderedDictionary
foreach($computer in $ComputerName)
{
$URI = "https://$($computer):$($Port)"
$RestMethodParams = @{
'Method' = 'Post'
'URI' = "$($URI)/session"
'Body' = @{'username' = $Credentials.UserName; 'password' = $Credentials.GetNetworkCredential().password}
'ErrorVariable' = 'NessusLoginError'
}
$TokenResponse = Invoke-RestMethod @RestMethodParams
if ($TokenResponse)
{
$SessionProps.add('URI', $URI)
$SessionProps.Add('Credentials',$Credentials)
$SessionProps.add('Token',$TokenResponse.token)
$SessionIndex = $Global:NessusConn.Count
$SessionProps.Add('SessionId', $SessionIndex)
$sessionobj = New-Object -TypeName psobject -Property $SessionProps
$sessionobj.pstypenames[0] = 'Nessus.Session'
[void]$Global:NessusConn.Add($sessionobj)
$sessionobj
}
}
}
End
{
}
}
<#
.Synopsis
Get one or more current Nessus Sessions.
.DESCRIPTION
Get one or more current Nessus Sessions.
.EXAMPLE
Get-NessusSession
Id : 0
URI : https://192.168.1.205:8834
Token : 50168808199c1a2197d180fa62fb9cc3cb9108054911476a
Id : 1
URI : https://192.168.1.205:8834
Token : 2683f239b257c7729a9b501a2b916c7022a730d20b536c12
.EXAMPLE
Get-NessusSession -SessionId 1
Id : 1
URI : https://192.168.1.205:8834
Token : 2683f239b257c7729a9b501a2b916c7022a730d20b536c12
#>
function Get-NessusSession
{
[CmdletBinding()]
param(
# Nessus session Id
[Parameter(Mandatory=$false,
ParameterSetName = 'Index',
Position=0)]
[Alias('Index')]
[int32[]]
$SessionId = @()
)
Begin{}
Process
{
if ($Index.Count -gt 0)
{
foreach($i in $SessionId)
{
foreach($Connection in $Global:NessusConn)
{
if ($Connection.SessionId -eq $i)
{
$Connection
}
}
}
}
else
{
# Return all sessions.
$return_sessions = @()
foreach($s in $Global:NessusConn){$s}
}
}
End{}
}
<#
.Synopsis
Closes one or more Nessus Sessions.
.DESCRIPTION
Closes one or more Nessus Sessions.
.EXAMPLE
Remove-NessusSession -Id 1 -Verbose
VERBOSE: Removing server session 1
VERBOSE: Disposing of connection
VERBOSE: DELETE https://192.168.1.205:8834/session with 0-byte payload
VERBOSE: received 4-byte response of content type application/json
VERBOSE: Removing session from $Global:NessusConn
VERBOSE: Session 1 removed.
#>
function Remove-NessusSession
{
[CmdletBinding()]
param(
# Nessus session Id
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipelineByPropertyName=$true)]
[Alias('Index')]
[int32[]]
$SessionId = @()
)
Begin{}
Process
{
# Finding and saving sessions in to a different Array so they can be
# removed from the main one so as to not generate an modification
# error for a collection in use.
$connections = $Global:NessusConn
$toremove = New-Object -TypeName System.Collections.ArrayList
if ($SessionId.Count -gt 0)
{
foreach($i in $SessionId)
{
Write-Verbose -Message "Removing server session $($i)"
foreach($Connection in $connections)
{
if ($Connection.SessionId -eq $i)
{
[void]$toremove.Add($Connection)
}
}
}
foreach($Connection in $toremove)
{
Write-Verbose -Message 'Disposing of connection'
$RestMethodParams = @{
'Method' = 'Delete'
'URI' = "$($connection.URI)/session"
'Headers' = @{'X-Cookie' = "token=$($Connection.Token)"}
'ErrorVariable' = 'DisconnectError'
'ErrorAction' = 'SilentlyContinue'
}
try
{
$RemoveResponse = Invoke-RestMethod @RestMethodParams
}
catch
{
Write-Verbose -Message "Session with Id $($connection.SessionId) seems to have expired."
}
Write-Verbose -message "Removing session from `$Global:NessusConn"
$Global:NessusConn.Remove($Connection)
Write-Verbose -Message "Session $($i) removed."
}
}
}
End{}
}
<#
.Synopsis
Get detailed information on one or more Nessus Sessions.
.DESCRIPTION
Get detailed information on one or more Nessus Sessions.
.EXAMPLE
Get-NessusSessionInfo -SessionId 0 -Verbose
VERBOSE: Removing server session 0
VERBOSE: GET https://192.168.1.205:8834/session with 0-byte payload
VERBOSE: received 196-byte response of content type application/json
Id : 2
Name : carlos
UserName : carlos
Email :
Type : local
Permission : Sysadmin
LastLogin : 2/23/2015 8:58:49 PM
Groups :
Connectors :
#>
function Get-NessusSessionInfo
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipelineByPropertyName=$true)]
[Alias('Index')]
[int32[]]$SessionId = @()
)
Begin
{
$origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
}
Process
{
$connections = $Global:NessusConn
$ToProcess = New-Object -TypeName System.Collections.ArrayList
foreach($i in $SessionId)
{
Write-Verbose "Removing server session $($i)"
foreach($Connection in $connections)
{
if ($Connection.SessionId -eq $i)
{
[void]$ToProcess.Add($Connection)
}
}
foreach ($Connection in $ToProcess)
{
$RestMethodParams = @{
'Method' = 'Get'
'URI' = "$($connection.URI)/session"
'Headers' = @{'X-Cookie' = "token=$($Connection.Token)"}
'ErrorVariable' = 'NessusSessionError'
}
$SessInfo = Invoke-RestMethod @RestMethodParams
if($SessInfo -is [psobject])
{
$SessionProps = [ordered]@{}
$SessionProps.Add('Id', $SessInfo.id)
$SessionProps.Add('Name', $SessInfo.name)
$SessionProps.Add('UserName', $SessInfo.UserName)
$SessionProps.Add('Email', $SessInfo.Email)
$SessionProps.Add('Type', $SessInfo.Type)
$SessionProps.Add('Permission', $PermissionsId2Name[$SessInfo.permissions])
$SessionProps.Add('LastLogin', $origin.AddSeconds($SessInfo.lastlogin).ToLocalTime())
$SessionProps.Add('Groups', $SessInfo.groups)
$SessionProps.Add('Connectors', $SessInfo.connectors)
$SessInfoObj = New-Object -TypeName psobject -Property $SessionProps
$SessInfoObj.pstypenames[0] = 'Nessus.SessionInfo'
$SessInfoObj
}
}
}
}
End{}
}
#endregion