Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Don't check in the Output dir
Output/
.DS_Store
launch.json
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## [0.2.4] 2019-11-18

- Add pagination support, add support for more than 5000 records, and code cleanup for Get-PSTenableSeverity. Thanks [@AaronG1234](https://github.com/AaronG1234)! [#12](https://github.com/jwmoss/PSTenable/issues/12)

## [0.2.3] 2019-09-01

- Code cleanup [#9](https://github.com/jwmoss/PSTenable/issues/9)

## [0.2.2] 2019-07-13

- Fixed automatically retrieving token.
Expand Down
6 changes: 4 additions & 2 deletions PSTenable/PSTenable.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PSTenable.psm1'

# Version number of this module.
ModuleVersion = '0.2.2'
ModuleVersion = '0.2.5'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -77,7 +77,9 @@
'Get-PSTenablePluginFamilyWindows',
'Get-PSTenableSeverity',
'Get-PSTenableWindowsServerJava'
'Invoke-PSTenableRest'
'Invoke-PSTenableRest',
'Get-PSTenableQuery',
'Get-PSTenableQueryAnalysis'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
6 changes: 6 additions & 0 deletions PSTenable/PSTenable.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Dot source public/private functions
$public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'public/*.ps1') -Recurse -ErrorAction Stop)
$private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'private/*.ps1') -Recurse -ErrorAction Stop)

Set-PSFconfig -Module PSTenable -Name Credential -Value $null -Initialize -Validation credential -Description "The credentials used for authenticating to the Tenable server"
Set-PSFconfig -Module PSTenalbe -Name WebSession -Value $null -Initialize -Description "WebSession for Invoke Rest Method"
Set-PSFconfig -Module PSTenalbe -Name Token -Value $null -Initialize -Validation string -Description "Token used with Tenable"
Set-PSFconfig -Module PSTenalbe -Name Server -Value $null -Initialize -Validation string -Description "REST Tenable endpoint for Tenable Server"

foreach ($import in @($public + $private)) {
try {
. $import.FullName
Expand Down
38 changes: 31 additions & 7 deletions PSTenable/Private/Invoke-PSTenableRest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,35 @@ function Invoke-PSTenableRest {

Begin {

$RestMethodParams = @{
URI = $(Get-PSFConfigValue -FullName 'PSTenable.Server') + $Endpoint
Method = $Method
Headers = @{"X-SecurityCenter" = $(Get-PSFConfigValue -FullName 'PSTenable.Token') }
ContentType = "application/json"
ErrorAction = "Stop"
WebSession = $(Get-PSFConfigValue -FullName "PSTenable.WebSession")
if((Get-PSFConfigValue -FullName "PSTenable.ApiKey" )){
#Check for API Key
$accessKey = Get-PSFConfigValue -FullName 'PSTenable.accesskey'
$secretkey= Get-PSFConfigValue -FullName 'PSTenable.secretkey'

$authString ="accesskey = $accessKey;secretkey=$secretkey;"

$headers = @{
"x-apikey" = $authString
}
$RestMethodParams = @{
URI = $(Get-PSFConfigValue -FullName 'PSTenable.Server') + '/rest'+ $Endpoint
Method = $Method
Headers = $headers
ContentType = "application/json"
ErrorAction = "Stop"
}

}else{
$headers = @{"X-SecurityCenter" = $(Get-PSFConfigValue -FullName 'PSTenable.Token') }

$RestMethodParams = @{
URI = $(Get-PSFConfigValue -FullName 'PSTenable.Server') + $Endpoint
Method = $Method
Headers = $headers
ContentType = "application/json"
ErrorAction = "Stop"
WebSession = $(Get-PSFConfigValue -FullName "PSTenable.WebSession")
}
}

if ($PSBoundParameters.ContainsKey('Body')) {
Expand All @@ -62,6 +84,8 @@ function Invoke-PSTenableRest {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_
}

Write-Debug $RestMethodParams.uri

Invoke-RestMethod @RestMethodParams
}

Expand Down
46 changes: 27 additions & 19 deletions PSTenable/Private/Invoke-PSTenableTokenStatus.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,36 @@ Function Invoke-PSTenableTokenStatus {

)

# Credentials
$APICredential = @{
username = (Get-PSFConfigValue -FullName 'PSTenable.Credential').UserName
password = (Get-PSFConfigValue -FullName 'PSTenable.Credential').GetNetworkCredential().Password
releaseSession = "FALSE"
}
$apiKeyStatus = Get-PSFConfigValue -FullName "PSTenable.ApiKey"

$SessionSplat = @{
URI = "$(Get-PSFConfigValue -FullName 'PSTenable.Server')/token"
SessionVariable = "SCSession"
Method = "Post"
ContentType = "application/json"
Body = (ConvertTo-Json $APICredential)
ErrorAction = "Stop"
}
Write-Debug "$apikeystatus"

if($apiKeyStatus -eq $false){

$Session = Invoke-RestMethod @SessionSplat
# Credentials
$APICredential = @{
username = (Get-PSFConfigValue -FullName 'PSTenable.Credential').UserName
password = (Get-PSFConfigValue -FullName 'PSTenable.Credential').GetNetworkCredential().Password
releaseSession = "FALSE"
}

if ($Session.response.releaseSession -eq $true) {
Write-Output $true
} else {
$SessionSplat = @{
URI = "$(Get-PSFConfigValue -FullName 'PSTenable.Server')/token"
SessionVariable = "SCSession"
Method = "Post"
ContentType = "application/json"
Body = (ConvertTo-Json $APICredential)
ErrorAction = "Stop"
}

$Session = Invoke-RestMethod @SessionSplat

if ($Session.response.releaseSession -eq $true) {
Write-Output $true
} else {
Write-Output $false
}
}else{
Write-Output $false
}
}

102 changes: 67 additions & 35 deletions PSTenable/Public/Connect-PSTenable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ function Connect-PSTenable {
Tenable Server Name, tenable.domain.com/rest
.PARAMETER Register
If specified, this will cache the Credential, TenableServer, Token, and Web Session.
.PARAMETER ApiKey
If specfied PSCredential Object will be treated as a API Key.
.INPUTS
None
.OUTPUTS
Expand All @@ -35,6 +37,10 @@ function Connect-PSTenable {

[Parameter(Position = 2, mandatory = $false)]
[switch]
$ApiKey,

[Parameter(Position = 3, mandatory = $false)]
[switch]
$Register
)
begin {
Expand All @@ -43,50 +49,76 @@ function Connect-PSTenable {

process {

# Credentials
$APICredential = @{
username = $Credential.UserName
password = $Credential.GetNetworkCredential().Password
releaseSession = "FALSE"
}
if($ApiKey){

$SessionSplat = @{
URI = "$TenableServer/token"
SessionVariable = "SCSession"
Method = "Post"
ContentType = "application/json"
Body = (ConvertTo-Json $APICredential)
ErrorAction = "Stop"
ErrorVariable = "TenableTokenError"
}
$accesskey = $Credential.UserName
$secretkey = $Credential.GetNetworkCredential().Password

$currentProgressPref = $ProgressPreference
$ProgressPreference = "SilentlyContinue"
$currentVersionTls = [Net.ServicePointManager]::SecurityProtocol
$currentSupportableTls = [Math]::Max($currentVersionTls.value__, [Net.SecurityProtocolType]::Tls.value__)
$availableTls = [enum]::GetValues('Net.SecurityProtocolType') | Where-Object { $_ -gt $currentSupportableTls }
$availableTls | ForEach-Object {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_
}
}Else{

# Credentials
$APICredential = @{
username = $Credential.UserName
password = $Credential.GetNetworkCredential().Password
releaseSession = "FALSE"
}

$Session = Invoke-RestMethod @SessionSplat
$SessionSplat = @{
URI = "$TenableServer/token"
SessionVariable = "SCSession"
Method = "Post"
ContentType = "application/json"
Body = (ConvertTo-Json $APICredential)
ErrorAction = "Stop"
ErrorVariable = "TenableTokenError"
}

[Net.ServicePointManager]::SecurityProtocol = $currentVersionTls
$ProgressPreference = $currentProgressPref
$currentProgressPref = $ProgressPreference
$ProgressPreference = "SilentlyContinue"
$currentVersionTls = [Net.ServicePointManager]::SecurityProtocol
$currentSupportableTls = [Math]::Max($currentVersionTls.value__, [Net.SecurityProtocolType]::Tls.value__)
$availableTls = [enum]::GetValues('Net.SecurityProtocolType') | Where-Object { $_ -gt $currentSupportableTls }
$availableTls | ForEach-Object {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_
}

$Session = Invoke-RestMethod @SessionSplat

[Net.ServicePointManager]::SecurityProtocol = $currentVersionTls
$ProgressPreference = $currentProgressPref
}
}

end {

Set-PSFconfig -FullName "PSTenable.WebSession" -Value $SCSession
Set-PSFconfig -FullName "PSTenable.Token" -Value $Session.response.token
Set-PSFConfig -FullName "PSTenable.Server" -Value $TenableServer
Set-PSFconfig -FullName "PSTenable.Credential" -Value $Credential
if($ApiKey){

Set-PSFconfig -FullName "PSTenable.accesskey" -Value $accesskey
Set-PSFconfig -FullName "PSTenable.secretkey" -Value $secretkey
Set-PSFConfig -FullName "PSTenable.Server" -Value $TenableServer
Set-PSFConfig -FullName "PSTenable.ApiKey" -Value $true

if ($Register -eq $true) {
Register-PSFConfig -FullName "PSTenable.accesskey"
Register-PSFConfig -FullName "PSTenable.secretkey"
Register-PSFConfig -FullName "PSTenable.Server"
Register-PSFConfig -FullName "PSTenable.Apikey"
}

}Else{
Set-PSFconfig -FullName "PSTenable.WebSession" -Value $SCSession
Set-PSFconfig -FullName "PSTenable.Token" -Value $Session.response.token
Set-PSFConfig -FullName "PSTenable.Server" -Value $TenableServer
Set-PSFconfig -FullName "PSTenable.Credential" -Value $Credential
Set-PSFConfig -FullName "PSTenable.ApiKey" -Value $false

if ($PSBoundParameters.ContainsKey('Register')) {
Register-PSFConfig -FullName "PSTenable.WebSession"
Register-PSFConfig -FullName "PSTenable.Token"
Register-PSFConfig -FullName "PSTenable.Server"
Register-PSFConfig -FullName "PSTenable.Token"
if ($Register -eq $true) {
Register-PSFConfig -FullName "PSTenable.WebSession"
Register-PSFConfig -FullName "PSTenable.Token"
Register-PSFConfig -FullName "PSTenable.Server"
Register-PSFConfig -FullName "PSTenable.Token"
Set-PSFConfig -FullName "PSTenable.ApiKey"
}
}
}
}
3 changes: 1 addition & 2 deletions PSTenable/Public/Get-PSTenableAssetAnalysis.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ function Get-PSTenableAssetAnalysis {
#>
[CmdletBinding()]
param (
[String]
[Parameter(Position = 0, Mandatory = $true)]
[string]
[PSFComputer]
$ComputerName
)

Expand Down
6 changes: 3 additions & 3 deletions PSTenable/Public/Get-PSTenableCVE.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Get-PSTenableCVE {
[parameter(Position = 0,
mandatory = $true,
ValueFromPipeline = $true)]
[string]
[string[]]
$CVE
)

Expand Down Expand Up @@ -74,13 +74,13 @@ function Get-PSTenableCVE {
}

## Get the pluginID and then call Get-TenablePlugin, and then output those results
$Results = Foreach ($Plugin in $output.response.results.pluginid) {
Foreach ($Plugin in $output.response.results.pluginid) {
Get-PSTenablePlugin -ID $Plugin
}

}

end {
$Results

}
}
4 changes: 2 additions & 2 deletions PSTenable/Public/Get-PSTenablePluginFamilyWindows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Get-PSTenablePluginFamilyWindows {
'29'
)

$Output = Foreach ($plugin in $WindowsPlugins) {
Foreach ($plugin in $WindowsPlugins) {

$query = @{
"tool" = "vulnipdetail"
Expand Down Expand Up @@ -82,6 +82,6 @@ function Get-PSTenablePluginFamilyWindows {
}

end {
$output

}
}
Loading