PortScan.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

<#PSScriptInfo
 
.VERSION 1.0.0.0
 
.GUID 1fc69e57-b727-43ce-b317-8ad9c7d020b0
 
.AUTHOR Faris Malaeb
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Port,External Scanning, Security, TCP/IP
 
.LICENSEURI
 
.PROJECTURI https://github.com/farismalaeb/Powershell/tree/master/PortScan
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Perform External Port Scanning
 
#>
 
[cmdletbinding()]
param(
[parameter(Mandatory=$true)]$HostToScan,
[parameter(Mandatory=$true,ParameterSetName="Range")][int]$StartingPort,
[parameter(Mandatory=$true,ParameterSetName="Range")][int]$EndingPort,
[parameter(Mandatory=$true,ParameterSetName="SelectivePort")][array]$SelectivePort
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$OpenPorts=@()

if ($PSCmdlet.ParameterSetName -like "Range"){
    $Body=@{
    remoteHost =$HostToScan
    start_port = $PSBoundParameters['StartingPort']
    end_port = $PSBoundParameters['EndingPort']
    normalScan = 'Yes'
    scan_type = 'connect'
    ping_type = 'none'
    }



try{

    $WebResult=(Invoke-RestMethod -Method Post -Body $Body -Uri 'https://www.ipfingerprints.com/scripts/getPortsInfo.php' -ErrorAction Stop).PortScanInfo.Split("`n") |where {$_ -match "(^\d*)\/\D{3}"}
    if (!($WebResult)){
        Write-Host "No result return, Please check the site name or IP if its valid or not" -ForegroundColor Red 
        return
        }

    foreach ($Singleline in $WebResult){

    $FilteredResult=[pscustomobject]@{
        Host=$PSBoundParameters['HostToScan']
        Port=""
        Result=""
    }
      $FilteredResult.Port=([regex]::Matches($Singleline,"^\d*")).value
      if ($Singleline -like "*open*"){$FilteredResult.Result="Open"}
      Else{$FilteredResult.Result="Filtered"}
        $OpenPorts+=$FilteredResult
    }


}


Catch{
Write-Host $_.exception.Message
}
}

if ($PSCmdlet.ParameterSetName -like "SelectivePort"){



foreach ($SinglePort in $SelectivePort){
if ($SinglePort.gettype().name -notlike 'int32'){
    Write-Host "$($SinglePort) is an invalid port number"
    return
    }
    $Body=@{
        remoteHost =$HostToScan
        start_port = $SinglePort
        end_port = $SinglePort
        normalScan = 'Yes'
        scan_type = 'connect'
        ping_type = 'none'
        }
    
       $SingleFilteredResult=[pscustomobject]@{
        Host=$PSBoundParameters['HostToScan']
        Port=""
        Result=""
        }


        Try{
    $SingleWebRequest=(Invoke-RestMethod -Method Post -Body $Body -Uri 'https://www.ipfingerprints.com/scripts/getPortsInfo.php' -ErrorAction Stop).PortScanInfo.Split("`n") |where {$_ -match "(^\d*)\/\D{3}"}
        if (!($SingleWebRequest)){
        Write-Host "No result return, Please check the site name or IP if its valid or not" -ForegroundColor Red 
        return
        }

      $SingleFilteredResult.Port=([regex]::Matches($SingleWebRequest,"^\d*")).value
      if ($SingleWebRequest -like "*open*"){ $SingleFilteredResult.Result="Open"}
      Else{ $SingleFilteredResult.Result="Filtered"}
        $OpenPorts+=$SingleFilteredResult
    }
    catch{
    Write-Host $_.exception.Message

    }
    
  }

}    
    
return  $OpenPorts