Last active
March 12, 2018 22:21
-
-
Save ConnorGriffin/4682830a4e3ac0c8c939c2f6147317d5 to your computer and use it in GitHub Desktop.
Edit nessus scan details, including schedule, name, and policy data, extracted of Scan.ps1 from Posh-Nessus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Edit nessus scan details, including schedule, name, and policy data | |
.DESCRIPTION | |
Long description | |
.EXAMPLE | |
Example of how to use this cmdlet | |
.EXAMPLE | |
Another example of how to use this cmdlet | |
#> | |
function Edit-NessusScanDetail | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
# Nessus session Id | |
[Parameter(Mandatory=$true, | |
Position=0, | |
ValueFromPipelineByPropertyName=$true)] | |
[Alias('Index')] | |
[int32[]] | |
$SessionId = @(), | |
[Parameter(Mandatory=$true, | |
Position=1, | |
ValueFromPipelineByPropertyName=$true)] | |
[int32] | |
$ScanId, | |
[String]$Name, | |
[String]$Description, | |
[Int]$PolicyID, | |
[Int]$FolderID, | |
[Int]$ScannerID, | |
[Bool]$Enabled = $true, | |
[ValidateSet('Onetime','Daily','Weekly','Monthly','Yearly')]$Frequency, | |
[Int]$Interval, | |
[String[]]$ByDay, | |
[ValidateSet('Day of Month','Week of Month')][String]$RepeatBy, | |
[String]$Timezone=([System.TimeZoneInfo]::Local).Id, | |
[datetime]$StartTime, | |
[String[]]$TargetGroups, | |
[String[]]$AgentGroups, | |
[String[]]$TextTargets, | |
[String]$FileTargets, | |
[String[]]$Emails | |
) | |
Begin | |
{ | |
# Return the day and week of a date (1st Tuesday is 1TU, 3rd Wednesday is 3WE, etc.) | |
Function Get-WeekDay { | |
Param([datetime]$DateTime) | |
if ($DateTime.Day -le 7) { | |
$week = 1 | |
} | |
else { | |
$week = (1..$DateTime.Day).ForEach{ | |
Get-Date "$($DateTime.Year)-$($DateTime.Month)-$_" | |
}.Where{$_.DayOfWeek -eq $DateTime.DayOfWeek}.Count | |
} | |
"$week$($DateTime.DayOfWeek.ToString().Substring(0,2))".ToUpper() | |
} | |
$origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0 | |
} | |
Process | |
{ | |
$ToProcess = @() | |
foreach($i in $SessionId) | |
{ | |
$Connections = $Global:NessusConn | |
foreach($Connection in $Connections) | |
{ | |
if ($Connection.SessionId -eq $i) | |
{ | |
$ToProcess += $Connection | |
} | |
} | |
} | |
foreach($Connection in $ToProcess) | |
{ | |
$ScanDetails = InvokeNessusRestRequest -SessionObject $Connection -Path "/scans/$($ScanId)" -Method 'Get' -Parameter $Params | |
if ($ScanDetails -is [psobject]) | |
{ | |
# Set the rrules based on the frequency type | |
switch ($Frequency) { | |
'Onetime' { | |
$rRules = 'FREQ=ONETIME' | |
} | |
'Daily' { | |
$rRules = "FREQ=$Frequency;INTERVAL=$Interval" | |
} | |
'Weekly' { | |
if (!$ByDay) {$ByDay = $StartTime.DayOfWeek.ToString().Substring(0,2)} | |
$rRules = "FREQ=$Frequency;INTERVAL=$Interval;BYDAY=$($ByDay -join ',')" | |
} | |
'Monthly' { | |
switch ($RepeatBy) { | |
'Day of Month' { | |
$rRules = "FREQ=$Frequency;INTERVAL=$Interval;BYMONTHDAY=$($StartTime.Day)" | |
} | |
'Week of Month' { | |
$byDay = Get-WeekDay $StartTime | |
$rRules = "FREQ=$Frequency;INTERVAL=$Interval;BYDAY=$ByDay" | |
} | |
} | |
} | |
} | |
# Set the 'static' settings | |
$settings = @{ | |
enabled = $enabled | |
timezone = $Timezone | |
} | |
# If no targets are specified, use the targets from the previous scan | |
if (!($TargetGroups -or $AgentGroups -or $FileTargets -or $TextTargets)) { | |
$TextTargets = $ScanDetails.info.targets | |
} | |
# Set the 'dynamic' parameters. If these items are not sent to the API, they will remain unchanged | |
if ($Name) {$settings.name = $Name} | |
if ($Description) {$settings.description = $Description} | |
if ($PolicyID) {$settings.policy_id = $PolicyID} | |
if ($FolderID) {$settings.folder_id = $FolderID} | |
if ($ScannerID) {$settings.scanner_id = $ScannerID} | |
if ($rRules) {$settings.rrules = $rRules.ToUpper()} | |
if ($StartTime) {$settings.starttime = $StartTime.ToString('yyyyMMddTHHmmss')} | |
if ($rRules -and $Frequency -notlike 'Onetime') {$settings.launch = $Frequency.ToUpper()} | |
if ($LaunchNow) {$settings.launch_now = $true} | |
if ($TargetGroups) {$settings.target_groups = $TargetGroups} | |
if ($AgentGroups) {$settings.agent_groups = $AgentGroups} | |
if ($TextTargets) {$settings.text_targets = $TextTargets -join ','} | |
if ($FileTargets) {$settings.file_targets = $FileTargets} | |
if ($Emails) {$settings.emails = $Emails -join ','} | |
# Convert the settings to JSON format for use by the API | |
$scanParams = @{ | |
settings = $settings | |
} | |
$json = ConvertTo-Json -InputObject $scanParams -Compress | |
$ServerTypeParams = @{ | |
'SessionObject' = $Connection | |
'Path' = "/scans/$ScanId" | |
'Method' = 'PUT' | |
'ContentType' = 'application/json' | |
'Parameter' = $json | |
} | |
InvokeNessusRestRequest @ServerTypeParams | |
} | |
} | |
} | |
End{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment