forked from tenable/Posh-Nessus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Posh-Nessus.psm1
159 lines (130 loc) · 4.23 KB
/
Posh-Nessus.psm1
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
if (!(Test-Path variable:Global:NessusConn ))
{
$Global:NessusConn = New-Object System.Collections.ArrayList
}
# Variables
$PermissionsId2Name = @{
16 = 'Read-Only'
32 = 'Regular'
64 = 'Administrator'
128 = 'Sysadmin'
}
$PermissionsName2Id = @{
'Read-Only' = 16
'Regular' = 32
'Administrator' = 64
'Sysadmin' = 128
}
$severity = @{
0 ='Info'
1 ='Low'
2 ='Medium'
3 ='High'
4 ='Critical'
}
# Load Functions
. "$PSScriptRoot\User.ps1"
. "$PSScriptRoot\Session.ps1"
. "$PSScriptRoot\Policy.ps1"
. "$PSScriptRoot\Scan.ps1"
. "$PSScriptRoot\Folders.ps1"
. "$PSScriptRoot\Server.ps1"
. "$PSScriptRoot\Plugin.ps1"
. "$PSScriptRoot\UserGroup.ps1"
. "$PSScriptRoot\Policy_Settings.ps1"
# Supporting Functions
##################################
function InvokeNessusRestRequest
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
$SessionObject,
[Parameter(Mandatory=$false)]
$Parameter,
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[String]$Method,
[Parameter(Mandatory=$false)]
[String]$OutFile,
[Parameter(Mandatory=$false)]
[String]$ContentType,
[Parameter(Mandatory=$false)]
[String]$InFile
)
$RestMethodParams = @{
'Method' = $Method
'URI' = "$($SessionObject.URI)$($Path)"
'Headers' = @{'X-Cookie' = "token=$($SessionObject.Token)"}
'ErrorVariable' = 'NessusUserError'
}
if ($Parameter)
{
$RestMethodParams.Add('Body', $Parameter)
}
if($OutFile)
{
$RestMethodParams.add('OutFile', $OutFile)
}
if($ContentType)
{
$RestMethodParams.add('ContentType', $ContentType)
}
if($InFile)
{
$RestMethodParams.add('InFile', $InFile)
}
try
{
#$RestMethodParams.Uri
$Results = Invoke-RestMethod @RestMethodParams
}
catch [Net.WebException]
{
[int]$res = $_.Exception.Response.StatusCode
if ($res -eq 401)
{
# Request failed. More than likely do to time-out.
# Re-Authenticating using information from session.
write-verbose -Message 'The session has expired, Re-authenticating'
$ReAuthParams = @{
'Method' = 'Post'
'URI' = "$($SessionObject.URI)/session"
'Body' = @{'username' = $SessionObject.Credentials.UserName; 'password' = $SessionObject.Credentials.GetNetworkCredential().password}
'ErrorVariable' = 'NessusLoginError'
'ErrorAction' = 'SilentlyContinue'
}
$TokenResponse = Invoke-RestMethod @ReAuthParams
if ($NessusLoginError)
{
Write-Error -Message 'Failed to Re-Authenticate the session. Session is being Removed.'
$FailedConnection = $SessionObject
[void]$Global:NessusConn.Remove($FailedConnection)
}
else
{
Write-Verbose -Message 'Updating session with new authentication token.'
# Creating new object with updated token so as to replace in the array the old one.
$SessionProps = New-Object -TypeName System.Collections.Specialized.OrderedDictionary
$SessionProps.add('URI', $SessionObject.URI)
$SessionProps.Add('Credentials',$SessionObject.Credentials)
$SessionProps.add('Token',$TokenResponse.token)
$SessionProps.Add('SessionId', $SessionObject.SessionId)
$Sessionobj = New-Object -TypeName psobject -Property $SessionProps
$Sessionobj.pstypenames[0] = 'Nessus.Session'
[void]$Global:NessusConn.Remove($SessionObject)
[void]$Global:NessusConn.Add($Sessionobj)
# Re-submit query with the new token and return results.
$RestMethodParams.Headers = @{'X-Cookie' = "token=$($Sessionobj.Token)"}
$Results = Invoke-RestMethod @RestMethodParams
}
}
else
{
$PSCmdlet.ThrowTerminatingError($_)
}
}
$Results
}