Marli-Sniffer.ps1

function Marli-Sniffer {
  Param
  (
    [parameter(Mandatory=$false)][String]$StartIP,
    [parameter(Mandatory=$false)][String]$DestinationIP,
    [parameter(Mandatory=$false)][Int]$TargetPort,
    [parameter(Mandatory=$false)][Switch]$IgnoreDNS
  )
  
  If (!($host.Name -like '*ise*'))
  {
    [System.Console]::CursorVisible = $false
  }

  #######################################################################################
  #######################################################################################
  function New_Entry ([int]$xposi,[int]$yposi,[string]$Text,[System.ConsoleColor]$Color) {
    [Console]::SetCursorPosition($xposi,$yposi)
    Write-Host $Text -ForegroundColor $Color
  }
  function lineposition {
    return $host.UI.RawUI.CursorPosition.Y
  }
  function Create-IPList{
    Param
    (
      [parameter(Mandatory=$true)][String[]]$SourceIP,
      [parameter(Mandatory=$true)][String[]]$TargetIP
    )
 
    $StartRelay = $true
    $List = @()
   
    If ($StartRelay -eq $true) {
      [int]$IPscan1 = $SourceIP.Split('.')[0]
    } else{[int]$IPscan1 = 0}
   
    Do {
      If ($StartRelay -eq $true) {
        [int]$IPscan2 = $SourceIP.Split('.')[1]
      } else{[int]$IPscan2 = 0}
   
      Do { 
        If ($StartRelay -eq $true) {
          [int]$IPscan3 = $SourceIP.Split('.')[2]
        } else{[int]$IPscan3 = 0}
   
        Do {
          If ($StartRelay -eq $true) 
          {
            $StartRelay = $false
            [int]$IPscan4 = $SourceIP.Split('.')[3]
          } else{[int]$IPscan4 = 0}
   
          Do {
            $IPAddress = -join($IPscan1, '.',$IPscan2, '.',$IPscan3, '.', $IPscan4)
            $List += $IPAddress

            If ($IPAddress -eq $TargetIP) {break}
            $IPscan4 += 1
          }While($IPscan4 -le 255)
   
          If ($IPAddress -eq $TargetIP) {break}
          $IPscan3 += 1
        }While($IPscan3 -le 255)
   
        If ($IPAddress -eq $TargetIP) {break}
        $IPscan2 += 1
      }While($IPscan2 -le 255)
   
      If ($IPAddress -eq $TargetIP) {break}
      $IPscan1 += 1
    }While($IPscan1 -le 255)
 
    return $List
  }
  function SniffIP($List, $MaxThreads,$TargetPort){
    function Jobtask($argum)
    { 
      Function Portping { Param($address, $port, $timeout=100)
      try {
        $socket=New-Object System.Net.Sockets.TcpClient
        $result=$socket.BeginConnect($address, $port, $NULL, $NULL)
        if (!$result.AsyncWaitHandle.WaitOne($timeout, $False)) {
          throw [System.Exception]::new('Connection Timeout')
        }
        $socket.EndConnect($result)
        $socket.Connected
        }
        catch{$socket = $false}
      
      finally
      {
        $socket.Close()
      }
    }


      If ($argum[1]) {
       $result = [bool](Portping $argum[0] -Port $argum[1])
      }
      else
      {
       $result = [bool](Test-NetConnection $argum[0] -InformationLevel Quiet)
      }

      If ($result -eq 'True')
      {
        $line = '' | SELECT Address
        $line.Address = $argum[0]
       
        $Solu = @()
        $Solu += $line
        return $Solu
      }

    }
 
    #=====================================================================
    $RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads)
    $RunspacePool.Open()
    $Jobs = New-Object System.Collections.ArrayList
    $Runspaces = foreach ($entry in $List)
    {

      $PowerShell = [powershell]::Create()
      $PowerShell.RunspacePool = $RunspacePool
      $argu = @()
      $argu += $entry
      $argu += $TargetPort
      $PowerShell.AddScript($Function:Jobtask).AddArgument($argu) | Out-Null
    
      $JobObj = New-Object -TypeName PSObject -Property @{
        Runspace = $PowerShell.BeginInvoke()
        PowerShell = $PowerShell  
      }

      $Jobs.Add($JobObj) | Out-Null
    }

    $LinePosition = $(lineposition) + 1
    while ($Jobs.Runspace.IsCompleted -contains $false) {
      New_Entry 0 $LinePosition '' White
      Write-Host "Still running... " -NoNewline
      Write-Host ($Jobs.Runspace | Where-Object {$_.IsCompleted -eq $True}).Count -ForegroundColor Yellow -NoNewline
      Write-Host "/" -NoNewline
      Write-Host $Jobs.Count -ForegroundColor Yellow
    }
 
    $Result = @()
    $Jobs | foreach {
      $Result += $_.Powershell.EndInvoke($_.Runspace)
    }

    $PowerShell.Dispose()
 
    return $Result
 
  }
  function SniffDNS($DNSAvail, $MaxThreads) {
    function Jobtask($List) {   
  
      $DNSText = [System.Net.Dns]::gethostentry($List.Address).Hostname
  
      $line = '' | SELECT Address, DNS
      $line.Address = $List.Address
      $line.DNS = $DNSText
     
      $Solu = @()
      $Solu += $line
      return $Solu
    }
  
    $RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads)
    $RunspacePool.Open()
    $Jobs = New-Object System.Collections.ArrayList
    $Runspaces = foreach ($entry in $DNSAvail) {
  
      $PowerShell = [powershell]::Create()
      $PowerShell.RunspacePool = $RunspacePool
      $PowerShell.AddScript($Function:Jobtask).AddArgument($entry) | Out-Null
      
      $JobObj = New-Object -TypeName PSObject -Property @{
        Runspace = $PowerShell.BeginInvoke()
        PowerShell = $PowerShell  
      }
  
      $Jobs.Add($JobObj) | Out-Null
    }
  
    while ($Jobs.Runspace.IsCompleted -contains $false) {
      Start-Sleep -Seconds 1
    }
  
    $Result = @()
    $Jobs | foreach {
      $Result += $_.Powershell.EndInvoke($_.Runspace)
    }
    $PowerShell.Dispose()
    return $Result
  }
  #######################################################################################
  #######################################################################################
  cls
  $From = $StartIP
  $To = $DestinationIP
  $LinePosition, $mem = lineposition

  Write-Host '---[ Sniffer Tool ]---' -ForegroundColor Yellow
  $UpdateLine = lineposition + 1
  Write-Host 'Scanning: ' -ForegroundColor White 
  #==================================================================================

  If ($From.Length -le 6 -and !($From -like '.'))
  {
    Do {
      $From = Read-Host 'Please enter >FROM< IP-Address'
    }While($From.Length -le 6 -and !($From -like '.'))
  }

  If ($To.Length -le 6 -and !($To -like '.'))
   {
    Do {
      $To = Read-Host 'Please enter >TO< IP-Address'
    }While($To.Length -le 6 -and !($To -like '.'))
  }

  #==================================================================================
  If ($mem -eq $(lineposition))
  {
   $UpdateLine = $UpdateLine - 4
  }
  New_Entry 10 $UpdateLine 'Creating IP-List ' Yellow
  $IPList = @()
  $IPList = Create-IPList -SourceIP $From -TargetIP $To
  #==================================================================================
  New_Entry 10 $UpdateLine 'IP-Addresses ' Yellow
  $IPAvail = SniffIP -List $IPList -MaxThreads 50 -TargetPort $TargetPort
  #==================================================================================
  If ($IgnoreDNS)
  {
   return $IPAvail
  }
  else
  {
   If ($IPAvail.Address.Count -ge 1)
   {
    $DNSAvail = @()
    New_Entry 10 $UpdateLine 'DNS-Names ' Yellow
    $DNSAvail = SniffDNS -DNSAvail $IPAvail -MaxThreads 50
    New_Entry 10 $UpdateLine 'Finished ' Green
    New_Entry 0 $($UpdateLine + 3) '' White
    return $DNSAvail
   }
   else
   {
    New_Entry 10 $UpdateLine 'No IP-Addresses available' Red
   }
  }
  #==================================================================================
  break
}