porthunter.psm1
#--------------------------- GLOBAL VARIABLES $scriptPath = $MyInvocation.MyCommand.Path $wd= [System.IO.Path]::GetDirectoryName($scriptPath) $date = Get-Date -Format dd-mm-yy $hour = Get-Date -Format HHmm $global:array = @() $global:tracker= @() ##----------------Open File Dialog function Add-Type -AssemblyName System.Windows.Forms $rootOut = "C:\temp\" $userProfile = $env:USERPROFILE $Path ="$userProfile\Downloads" $global:filePath = "" $global:array = @() $global:tracker= @() $global:outfile = $rootOut + "$fileName" + "-Scan-port-" + "$port" + "$date@$hour.csv" $global:delimeter ="" $global:port = "" $global:fileName = "" #----------------------------------------------------------------------- #### function intro { "@ PORT HUNTER~~ /i //, * * | ///i -X- ,/ ).'i SEEK . . | | )-i AND | )i . YOU . ' )i . / |- . _.-./-. /z_ SHALL . `-. >._\ _ );i. . , / `-'/`k-'`u)-'` . FIND... / )- . . * ,.----' ) ' * / )1` ~~by PB a.k.a CyberSamurai ///v`-v\v https://cybersamurai.co.uk /v @" Write-Host $art -ForegroundColor Magenta $checkOutPath = Test-Path "C:\temp" if($checkOutPath -eq $false){Write-Host "Creating Output folder" -ForegroundColor Yellow;mkdir "C:\temp"} else{} Write-Host "" } function help{clear Write-Host "Port Hunter v1.1 ~ by PB a.k.a CyberSamurai" -ForegroundColor Gray Write-Host "You can use porthunter to ping IPv4 and IPv6 across the network" -ForegroundColor Gray Write-Host "porthunter will automatically skip duplicates from your feed" -ForegroundColor Gray Write-Host "You can feed .txt and .csv files and it will automatically detected it" -ForegroundColor Gray Write-Host "If you are using .csv files you can test multiple IPs per host, considering each IP is separated by a delimiter" -ForegroundColor Gray Write-Host "" Write-Host "Flags" -ForegroundColor Gray Write-Host "" Write-Host "-h Will show this help message" -ForegroundColor Gray Write-Host "-p Port that will be used during scanning" -ForegroundColor Gray Write-Host "-d Delimeter used on .csv files to separate IP addresses" -ForegroundColor Gray Write-Host "-out Full file path of where file will be saved. if -out is not used output will be saved on C:\temp" -ForegroundColor Gray Write-Host "" Write-Host "Usage:" -ForegroundColor Gray Write-Host "Running the command by itself a pop up window will open up asking you to load a .txt|.csv file" -ForegroundColor Gray Write-Host "porthunter" -ForegroundColor Yellow Write-Host "" Write-Host "Running with the flag -f you can add the file path to be used to perform the port scan" -ForegroundColor Gray Write-Host 'porthunter "c:\Users\Joker\Downloads\Ip.csv' -ForegroundColor Yellow Write-Host "" Write-Host "Running the commands with multiple flags" -ForegroundColor Gray Write-Host 'porthunter ".\test csv.csv" -p "443" -d "|" -out ".\AnotherOne.csv"' -ForegroundColor Yellow Write-Host "" -ForegroundColor Gray Write-Host "Running the commands some of the flags only" -ForegroundColor Gray Write-Host 'porthunter ".\test csv.csv" -p "443"' -ForegroundColor Yellow Write-Host "" Write-Host "" } function subOpenFile{ $FileDialogObject = [System.Windows.Forms.OpenFileDialog] # Create Object and add properties $OpenFileDialog = New-Object $FileDialogObject $OpenFileDialog.InitialDirectory = $Path $OpenFileDialog.CheckPathExists = $true $OpenFileDialog.CheckFileExists = $true $OpenFileDialog.Title = "‼►._.◄‼▬ Select a the file which we are going to check if ports are open‼►._.◄‼" $OpenFileDialog.Filter = "csv files mate‼ (*.csv)|*.csv|txt files mate‼ (*.txt)|*.txt" $OpenFileDialog.ShowDialog() if ($openFileDialog.CheckFileExists -eq $true) { $filePath = $openFileDialog.FileName} $global:filePath = $filePath } #inner function for OpenFile function function openFile{ $dataArray = @() Try{subOpenFile $data = Import-Csv $global:filePath $check = $true Write-Host "File loaded successfully" -ForegroundColor Green;sleep 1} Catch{Write-Host "Please select a file to ingest" -ForegroundColor Yellow} #return $data } function autoPickFileType($fileType){ switch($fileType){ "csv" {portHunterCsv $global:filePath} "txt" {portHunterTxt $global:filePath} } } #read file type based on regex search, depending if txt or csv diff function is run. #-MAIN function porthunter{ param( [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] #this let file be the first parameter [string]$FilePath = "", [switch]$f = $false, [switch]$h = $false, [switch]$help =$false, [string]$p = "", [string]$d = "", [string]$out = "" ) if($h -eq $true -or $help -eq $true){help} elseif($FilePath -ne "" -and $h -eq $false){ intro $data = Get-Content $FilePath $global:filePath = $FilePath $fileType = $global:filePath.split("\.\(\w*)$") $fileType = $fileType[-1] autoPickFileType $fileType } else{ intro $data = openFile $fileType = $global:filePath.split("\.\(\w*)$") $fileType = $fileType[-1] autoPickFileType $fileType } } # main function to be exported #### ---- BRAIN FUNCTIONS START ----- #### # - TXT PortHunter Function function portHunterTxt($filePath) { Write-Host "running TXT function" -ForegroundColor Yellow $outArray = @() if($p -eq ""){ Write-Host "Type the port number you would like to check if its open|closed?" -ForegroundColor Green $port = Read-Host "Port Number" } else{$port=$p} if($out -eq ""){ $global:outfile = "C:\temp\" + "$global:fileName" + "-Scan-port-" + "$global:port" + "$date@$hour.csv" } else{ $global:outfile = $out Write-Host "out flag used" Write-Host $global:outfile} $array = @() $array sleep 8 $data = Get-Content $filePath Write-Host "File will be saved at $global:outfile" -ForegroundColor Green -BackgroundColor Black forEach ($record in $data){ if($record -notin $global:tracker){ $row = New-Object Object $connect = Test-NetConnection $record -Port $port if($connect.TcpTestSucceeded -like "True" ){Write-Host "[OPEN] $record PORT: $port" -ForegroundColor green; $row | Add-Member -MemberType NoteProperty -Name "IP" -Value $record $row | Add-Member -MemberType NoteProperty -Name "Port" -Value $port $row | Add-Member -MemberType NoteProperty -Name "Status" -Value "Open" } else{Write-Host "[CLOSED] $record PORT: $port" -ForegroundColor Red $row | Add-Member -MemberType NoteProperty -Name "IP" -Value $record $row | Add-Member -MemberType NoteProperty -Name "Port" -Value $port $row | Add-Member -MemberType NoteProperty -Name "Status" -Value "Closed" } $array += $row $global:tracker += $record }#close if not in globalTracker }#--close forEach $array | Export-Csv $global:outfile -NoTypeInformation Write-Host "File exported to $outFile" -ForegroundColor Yellow ###Import results and show it on screen Start-Sleep -Seconds 1 #read file that we are able to ping on console that was just exported $log = Import-Csv $global:outfile $log } # - CSV PortHunter Function function pingEach($line){ $ip = $line."IP" $hostname =$line.hostname if ($hostname -like "" -or $hostname -like $null){$hostname = "Unknown"} $ipParts = $ip.split("$global:delimeter") $ipCount = $ipparts.count for($i=0;$i -lt $ipCount;$i++){ $record = $ipParts[$i] $row = New-Object Object if($record -notin $global:tracker){ $connect = Test-NetConnection $record -Port $global:port if($connect.TcpTestSucceeded -like "True" ){Write-Host "[OPEN] $record PORT: $port" -ForegroundColor green; $row | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $hostname $row | Add-Member -MemberType NoteProperty -Name "IP" -Value $record $row | Add-Member -MemberType NoteProperty -Name "Port" -Value $port $row | Add-Member -MemberType NoteProperty -Name "Status" -Value "Open" } else{Write-Host "[CLOSED] $record PORT: $global:port" -ForegroundColor Red $row | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $hostname $row | Add-Member -MemberType NoteProperty -Name "IP" -Value $record $row | Add-Member -MemberType NoteProperty -Name "Port" -Value $port $row | Add-Member -MemberType NoteProperty -Name "Status" -Value "Closed" } $global:tracker += $record $global:array += $row }#--close if record not in tracker, skip this record }#--close forLoop } function portHunterCsv($filePath){ "@ PING HUNTER~~ /i //, * * | ///i -X- ,/ ).'i SEEK . . | | )-i AND | )i . YOU . ' )i . / |- . _.-./-. /z_ SHALL . `-. >._\ _ );i. . , / `-'/`k-'`u)-'` . FIND... / )- . . * ,.----' ) ' * / )1` ~~by PB a.k.a CyberSamurai ///v`-v\v https://cybersamurai.co.uk /v @" Write-Host $art -ForegroundColor Magenta $data = Import-Csv $filePath -Header "IP" | select -Skip 1 #query user if($out -eq ""){ Write-Host "File will be saved on C:/temp" -ForegroundColor Yellow -BackgroundColor Black;Start-Sleep -Milliseconds 100 Write-Host "Please type the file name to be saved" -ForegroundColor Yellow -BackgroundColor Black;Start-Sleep -Milliseconds 100 $global:fileName = Read-Host "File Name" } else{} if($d -eq ""){ Write-Host "What is the delimeter used to separte IPS iniside the IP row? Default value is ;" -ForegroundColor Yellow -BackgroundColor Black ;Start-Sleep -Milliseconds 100 $global:delimeter = Read-Host "Delimeter" } else{$global:delimeter = $d} if($p -eq ""){ Write-Host "Type the port number you would like to check if its open|closed?" -ForegroundColor Green $global:port = Read-Host "Port Number" } else{$global:port=$p} if($out -eq ""){ $global:outfile = "C:\temp\" + "$global:fileName" + "-Scan-port-" + "$global:port" + "$date@$hour.csv" } else{$global:outfile = $out} Write-Host "File will be saved at $global:outfile" -ForegroundColor Green -BackgroundColor Black forEach($x in $data){ pingEach $x } $global:array | Export-Csv $global:outfile -NoTypeInformation } #### ---- BRAIN FUNCTIONS END ----- #### Export-ModuleMember -Function porthunter |