Contents

Part III: Powershell Multithreading – Asynchronous Network and Host Discovery Scanner

Part III of my Get-SecNetMap “Mini-Module”:

  1. Get-SecNetMap
  2. Get-SecPortScan
  3. Get-SecIPRange (This Post)
  4. Convert-SecIPAddress
  5. Get-SecArpTable

You can download the module source files here: http://securekomodo.net/files/Get-SecNetMap.zip

Get-SecIPRange

Get-SecIPRange is designed to enumerate all IP addresses within a given range. I have intentionally coded this to support a max range of 65534 addresses or a (/16 CIDR) because seriously, who needs to scan such a large range like that. Anyway, this function will loop through only the third and fourth octets of an IP address range to determine each address in the range for scanning. Currently this is designed as a sort of “queue” of IPs to scan for the Get-SecNetMap to work against. Lets take a look at the output.

IPrange

It is relatively simple and to achieve this output involves a few if/then statements and some nested loops. I wrote it relatively quick and havent had any issues so far but it has had the least amount of bug checking out of the 5 functions in this module.

Get-SecIPRange

[sourcecode language=”powershell” wraplines=”false” collapse=”false”]
Function Get-SecIPRange {
<#
.SYNOPSIS
Enumerates all IP addresses in a given range

.DESCRIPTION
Uses nested loops to quickly list all IP addresses based on a minimum and maximum IP address

.PARAMETER minIP
Will only accept an IP address input for the minimum in the range

.PARAMETER maxIP
Will only accept an IP address input for the maximum in the range

.NOTES
Seriously. Dont go below a /16 CIDR unless you want to lock up your system…

Future enhancements will be to accept a CIDR notation like 192.168.0.0/24

Name: Get-SecIPRange.ps1
Author: SecureKomodo
Version: 1.0
#>
[Cmdletbinding()]
Param (

[Parameter(Mandatory = $True)]
[System.Net.IPAddress]$minIP,

[Parameter(Mandatory = $True)]
[System.Net.IPAddress]$maxIP
)

$minOctet=$minIP.GetAddressBytes()
$maxOctet=$maxIP.GetAddressBytes()

#Store in Object
$TargetRange=@()

# Enumerate IP Addresses in the given range
If ($minOctet[2] -lt $maxOctet[2]) {

foreach ($1 in (($minOctet[3])..255)) {
Write-Verbose (([string]$minOctet[0]) + “.” + ([string]$minOctet[1]) + “.” + ([string]$minOctet[2]) + “.” + “$1”)
$TargetRange+=(([string]$minOctet[0]) + “.” + ([string]$minOctet[1]) + “.” + ([string]$minOctet[2]) + “.” + “$1”)
}

Do {
foreach ($2 in (1..255)) {
Write-Verbose (([string]$minOctet[0]) + “.” + ([string]$minOctet[1]) + “.” + ([string]$minOctet[2]) + “.” + “$2”)
$TargetRange+=(([string]$minOctet[0]) + “.” + ([string]$minOctet[1]) + “.” + ([string]$minOctet[2]) + “.” + “$2”)
}
$minOctet[2]++
} Until ($minOctet[2]-eq $maxOctet[2])

foreach ($3 in (1..($maxOctet[3]))) {
Write-Verbose (([string]$minOctet[0]) + “.” + ([string]$minOctet[1]) + “.” + ([string]$maxOctet[2]) + “.” + “$3”)
$TargetRange+=(([string]$minOctet[0]) + “.” + ([string]$minOctet[1]) + “.” + ([string]$minOctet[2]) + “.” + “$3”)
}

} Else {

If ($minOctet[2] -eq $maxOctet[2]) {
foreach ($4 in ($minOctet[3]..$maxOctet[3])) {
Write-Verbose (([string]$minOctet[0]) + “.” + ($minOctet[1]) + “.” + ($minOctet[2]) + “.” + “$4”)
$TargetRange+=(([string]$minOctet[0]) + “.” + $minOctet[1] + “.” + $minOctet[2] + “.” + “$4”)
}

}

}

Return $TargetRange

}
[/sourcecode]

Tags// ,