Part IV: PowerShell Multithreading – Asyncronous Network and Host Discovery Scanner

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

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

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

Convert-SecIPAddress

The Convert-SecIPAddress function is actually pretty cool. I was able to use mathematics learned from my cryptography courses during my undergrad to convert an IP address to an integer and vice-verse. It is performance optimized and has support for long integers which was where most of the errors I had during development were. Since many of the integers returned are 10char in length, I had to force cast them into LONG type so Powershell could actually work. This conversion was to allow the randomization of IP addresses in the Get-SecNetMap function. In order to randomize, I had to convert each IP to an integer so that I could “shuffle” the integers and use the Get-Random accordingly. Then the conversion from Int back to an IP address would take place. Lets take a look at the sample below.

ConvertIPAddress

Convert-SecIPAddress

[sourcecode language=”powershell” wraplines=”false” collapse=”false”]
Function Convert-SecIPAddress {
<#
.SYNOPSIS
Converts an IP Address to an Integer and vice versa

.DESCRIPTION
Performs mathmatical operations on each octect to determine the integer representation of an IP address.
Also will calculate an IP address from an Integer.

.PARAMETER toINT
Switch to allow script to process an IP address into an integer

.PARAMETER fromINT
Switch to allow script to process an integer into an IP address

.Example
Convert-SecIPAddress -toINT 192.168.1.101

3232235877

.Example
Convert-SecIPAddress -fromINT 3232235877

192.168.1.101

.NOTES
Name: Convert-SecIPAddress.ps1
Author: SecureKomodo
Version: 1.0
#>
[Cmdletbinding()]
Param (

[Parameter(ParameterSetName=”toINT”)]
[Switch]$toINT,

[Parameter(ParameterSetName=”fromINT”)]
[Switch]$fromINT,

[Parameter(Position=0,Mandatory = $True,ParameterSetName=”toINT”)]
[System.Net.IPAddress]$IPAddr,

[Parameter(Position=0,Mandatory = $True,ParameterSetName=”fromINT”)]
[System.Int64]$Int64
)

Begin{

# Declare Output Array
$OutputArray=@()

# Convert the IP to a byte array
if ($toINT) {[byte[]]$ByteArray = $IPAddr.GetAddressBytes()}

}

Process{

# Convert from IP Address to Integer
If ($toINT){
[System.Int64]$Int64=$Null

for ($ci=0; $ci -lt $ByteArray.Length; $ci++) {
$Power=(3-$ci)
$Int64+=[System.Int64]::Parse(($ByteArray)[$ci]) % 256 * [Math]::Pow(256, $Power)

} $OutputArray += $Int64

} # End If toINT

# Convert from Integer to IP Address
ElseIf ($fromINT) {

$IP=(
[math]::truncate($Int64/16777216),
[math]::truncate(($Int64%16777216)/65536),
[math]::truncate(($Int64%65536)/256),
[math]::truncate($Int64%256)) -join “.”

$OutputArray += $IP.ToString()

} # End ElseIf fromINT

} # End Process

End{

Return $OutputArray

}

} #End Convert-SecIPAddress
[/sourcecode]

Tags// ,