To learn some PowerShell I made a tool, I've made before in bash. It pings the subnet for arp values and checks if there is no weird behaviour.
function IPv4toBin ($i){
$b = $i -split '\.' | ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,'0')}
return $b -join ""
}
function Get-IPs($subnet)
{
#Split IP and subnet
$IP = ($Subnet -split "\/")[0]
$SubnetBits = ($Subnet -split "\/")[1]
$IPInBinary = IPv4toBin($IP)
$HostBits = 32-$SubnetBits
$NetworkIDInBinary = $IPInBinary.Substring(0, $SubnetBits)
$HostIDInBinary = $IPInBinary.Substring($SubnetBits, $HostBits)
$HostIDInBinary = $HostIDInBinary -replace "1","0"
$imax = [convert]::ToInt32(("1" * $HostBits),2) -1
$IPs = @()
For ($i = 1 ; $i -le $imax ; $i++)
{
$NextHostIDInDecimal = ([convert]::ToInt32($HostIDInBinary,2) + $i)
$NextHostIDInBinary = [convert]::ToString($NextHostIDInDecimal,2)
$NoOfZerosToAdd = $HostIDInBinary.Length - $NextHostIDInBinary.Length
$NextHostIDInBinary = ("0" * $NoOfZerosToAdd) + $NextHostIDInBinary
$NextIPInBinary = $NetworkIDInBinary + $NextHostIDInBinary
$IP = @()
For ($x = 1 ; $x -le 4 ; $x++)
{
$StartCharNumber = ($x-1)*8
$IPOctetInBinary = $NextIPInBinary.Substring($StartCharNumber,8)
$IPOctetInDecimal = [convert]::ToInt32($IPOctetInBinary,2)
$IP += $IPOctetInDecimal
}
$IP = $IP -join "."
$IPs += $IP
}
return $IPs
}
function Check-Spoof($iplist)
{
$gate_ip = ipconfig | Select-String "Default Gateway" | % { $_.ToString().Split(':')[1].Trim() }
$gate_ip = $gate_ip[0]
$gate_mac = arp -a $gate_ip | Select-String $gate_ip | % { $_.ToString() -split "\s+" }
$gate_mac = $gate_mac[2].ToUpper()
echo ""
echo "Gateway with ip: $gate_ip and mac: $gate_mac"
echo ""
for ($i = 0; $i -lt $iplist.Count; $i++)
{
if ($iplist[$i] -ne $gate_ip)
{
$ip = $iplist[$i]
$arp_call = arp -a $ip | Select-String "$ip"
if ($arp_call -ne $null)
{
$call = $arp_call -split "\s+"
$mac = $call[2].ToUpper()
echo "Active IP: $ip with MAC: $mac"
if ($mac -eq $gate_mac)
{
echo ""
echo "You are being Spoofed a duplicate ARP value found"
return
}
}
}
}
echo ""
echo "You are Safe, no duplicate ARP values found."
}
$subnet = "192.168.1.1/24"
$IPLIST = Get-IPs($subnet)
Check-Spoof($IPLIST)
I am fairly new to PowerShell, so I'm mostly concerned about the proper syntax.