IP4 Network Scanning using powershell.

sometimes you want to scan a subnet to see what devices are on it. If you need to scan from inside the more secure environment you can’t install any extra software on it.

Machines with the minimum attack surface.


This powershell script “ipscan.ps1” will work through ipv4 netowrk addresses and output a .csv file containing the address and dns name for all hosts that responded to pings.

#
# create_and_export_certificate.ps1 
# 
function My_Create_Certificate { 
    param ( 
        [string]$hostname, 
        [string]$domain, 
        [string]$subject, 
        [string]$domain2,
        [string]$domain3

        )


    # create a new certificate - 5 year expiration ACME-CA template
    #$hostname="*"
    $name="$hostname.$domain"
    $dnsname=@()
    $dnsname+=$name
    if ( $domain2 -ne "" )  { $dnsname+="$hostname.$domain2" } 
    if ( $domain3 -ne "" )  { $dnsname+="$hostname.$domain3" }
    $dnsname+="$hostname"

    $newcert=Get-Certificate -template "myinternalwebservertemplate5years" -subjectname $subject -dnsname $dnsname  -url ldap: -CertStoreLocation cert:localmachine\my

    $newcert

    
} 

# generate a random name for testing
$datetime=get-date -Format "yyyy-MM-dd-hh-mm-ss"
$hostname="my-new-device-$datetime"

$domain="acme.com.au" 
$domain2="acme.local" 
$domain3="" 
$subject='C=AU;O="Acme Australia Pty Ltd";OU=HQ;CN='+"$hostname.$domain"

$newcert = My_Create_Certificate -hostname $hostname -domain $domain -subject $subject -domain2  $domain2 -domain3 $domain3 
$password="e43$H()cvgs4344c434x612" 

# export the certificate and its private key to a file
cd $env:TEMP
cd pfx

$mycerts=Get-ChildItem -Path "Cert:\localmachine\My\$($newcert.certificate.thumbprint)"
#$mycerts |  Where-Object { $_.hasPrivateKey } | Foreach-Object {&certutil.exe @('-exportpfx', '-f', '-p','$password',$_.Thumbprint, "expo_$($_.Subject).pfx") }
foreach ( $cert in $mycerts ) {
    if ( $cert.hasprivatekey ) {
        $filename= $cert.subject -replace " ","-" 
        $filename=  $filename -replace "\*","X"
        $filename=  $filename -replace ",",""
        $filename=  $filename -replace "CN=",""
        $filename += ".pfx"
        $retval= &certutil.exe @('-exportpfx', '-f', '-p',"$password",$cert.Thumbprint, "$filename") 
        dir $filename

        #
        # now use openssl to split into a key file and a cert file - as wanted by Cisco ISE 
        #
        $fname = $filename -replace ".pfx", ""
        start-process -filepath 'C:\Program Files\OpenSSL-Win64\bin\openssl.exe' -argumentlist "pkcs12 -in $filename -out $fname-certs.txt -nodes -nokeys -password pass:$password" 
        start-process -filepath 'C:\Program Files\OpenSSL-Win64\bin\openssl.exe' -argumentlist "pkcs12 -in $filename -out $fname-key.txt -nodes -nocerts -password pass:$password" 

       
        dir $fname*.txt

        }
    }




This entry was posted in Powershell, VBS, VBA and other scripting.. Bookmark the permalink.