Public/Invoke-Nmap.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
Register-ArgumentCompleter -CommandName "Invoke-Nmap" -ParameterName "Preset" -ScriptBlock {(Get-NmapPresetArguments).keys}

function Invoke-Nmap {
    <#
    .SYNOPSIS
        Runs the NMAP command and then formats the output.
    .INPUTS
        [String[]]$ComputerName
    .OUTPUTS
        Depends on -OutFormat parameter
    .EXAMPLE
        Invoke-Nmap scanme.nmap.org
        Runs an NMAP scan with the Quick scan preset and provides the result as a formatted Powershell Object
    .EXAMPLE
        Invoke-Nmap scanme.nmap.org "-t4 -p 80,443"
        This is similar to running nmap "bare" but enjoy the format processing of invoke-nmap
 
    #>


    [CmdletBinding(DefaultParameterSetName="preset")]
    param (
        #A list of nmap host specifications. Defaults to localhost
        [Parameter(
            Position=0,
            ValueFromPipeline
        )]
        [String[]]
        $computerName = "localhost",

        #Specify raw argument parameters to nmap
        [Parameter(
            ParameterSetName="custom",
            Mandatory,
            Position=1
        )]
        [String[]]
        $ArgumentList,

        [String]
        [Parameter(
            ParameterSetName="preset",
            ValueFromPipeline
        )]
        [String]$Preset = "Quick",

        #Choose which format for the output (XML, JSON, HashTable, PSObject, or Raw). Default is PSObject
        [ValidateSet('PoshNmap','Summary','Raw','PSObject','XML','JSON','Hashtable')]
        [String]$OutFormat = 'PoshNmap',

        #Show all results, not just online hosts
        [Switch]$All,

        #Perform an SNMP community scan. This is also done automatically with the "snmp" preset
        [Switch]$Snmp,

        #A list of SNMP communities to scan. Defaults to public and private
        [String[]]
        [Parameter()]
        $snmpCommunityList = @("public","private")
    )

    if ($ArgumentList) {$ArgumentList = $ArgumentList.split(' ')}

    if ($Preset -and ($PSCmdlet.ParameterSetName -ne 'Custom')) {
        $nmapPresetArgumentNames = (Get-NmapPresetArguments).keys
        if ($Preset -notin $nmapPresetArgumentNames) {
            throwUser New-Object ArgumentException -ArgumentList "Invoke-Nmap: Value $Preset is not a valid choice. Please choose one of: $($nmapPresetArgumentNames -join ', ')","Preset"
        } else {
            $ArgumentList = Get-NmapPresetArguments $Preset
        }
    }

    if ($Preset -eq 'snmp') {$snmp = $true}
    if ($snmp) {
        $snmpCommunityFile = [io.path]::GetTempFileName()
        #Special file format required
        ($snmpCommunityList -join "`n") + "`n" | Set-Content -NoNewLine -Encoding ASCII -Path $snmpCommunityFile -Force
        $argumentList += '--script','snmp-brute','--script-args',"snmp-brute.communitiesdb=$snmpCommunityFile"
    }

    if (-not $All) {
        $argumentList += '--open'
    }

    try {
        switch -regex ($OutFormat) {
            'Raw|XML' {
                if ($Outformat -eq 'XML') {$argumentlist += '-oX','-'}
                StartNmap $argumentList $computerName -Raw
                break
            }
            default {
                StartNmap $argumentList $computerName | ConvertFrom-NmapXml -OutFormat $OutFormat
            }
        }
    } finally {
        if ($snmp -and (Test-Path $snmpCommunityFile)) {Remove-Item $snmpCommunityFile -Force -ErrorAction SilentlyContinue}
    }
}