Few Powershell Network tools
This is when you are in an environment that won't let you install software. I've collected these from various sources. I use them often enough to keep it handy here.
Check for TCP Listener:
1
2
3
4
| $socket = new-object net.sockets.tcpclient
$socket.connect("www.google.com",443)
$socket.connected
$socket = $null
|
Check for TCP Connections on your machine:
This is more than you really need, but it'll print a table of established connections every 5 seconds. If you just want the raw data and all the available connection state, just run lines 2 and 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| do{
$tcpproperties=[System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
$connections=$tcpproperties.GetActiveTcpConnections()
$output=@()
foreach($conn in $connections){
if($conn.State -eq 'Established'){
$outputObj = New-Object -typename PSObject
$outputObj | Add-Member -MemberType NoteProperty -Name "LocalAddress" -Value $conn.LocalEndPoint.Address
$outputObj | Add-Member -MemberType NoteProperty -Name "LocalPort" -Value $conn.LocalEndPoint.Port
$outputObj | Add-Member -MemberType NoteProperty -Name "RemoteAddress" -Value $conn.RemoteEndPoint.Address
$outputObj | Add-Member -MemberType NoteProperty -Name "RemotePort" -Value $conn.RemoteEndPoint.Port
$outputObj | Add-Member -MemberType NoteProperty -Name "State" -Value $conn.State
$output=$output + $outputObj
}
}
$output | format-table
sleep 5
}while($true)
|
Check for UDP Listener
For this one, I'm also illustrating a listener too. Good for troubleshooting firewall.
Receiver
Run this portion on your receive end and wait for traffic.
1
2
3
4
5
6
7
8
9
10
11
12
| $udpObject = new-object system.net.sockets.udpclient(1433)
$udpObject.client.ReceiveTimeout = 10000
do{
$remoteEndPoint = new-object System.Net.IPEndPoint([System.Net.IPAddress]::Any,0)
$receiveBytes = $udpObject.Receive([ref]$remoteEndPoint)
$a = New-Object System.Text.AsciiEncoding
[String]$returnData = $a.GetString($receiveBytes)
Write-host "Received: $($returnData.ToString())"
Write-host "Sent from: $($remoteEndPoint.Address.ToString()) on their port: $($remoteEndPoint.port.ToString())"
$ans = read-host "More (y/n)?"
}while($ans -ne 'n')
$udpObject.close()
|
Sender
Then run this from your remote machine to see the information show up on the remote end.
1
2
3
4
5
6
| $udpObject = new-object system.net.sockets.udpclient(1433)
$udpObject.connect("10.10.10.1",1433)
$a = new-object System.Text.AsciiEncoding
$byte = $a.GetBytes("$(get-date)")
[void]$udpObject.send($byte,$byte.length)
$udpObject.close()
|
Check for UDP Connection
1
2
| $udpProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
$udpProperties.GetActiveUDPListners()
|
Remote PowerShell Session
Again, more than you need, but incase you don't want to have to type in your password every time.
1
2
3
4
5
6
| $servername = "mydesktop1"
$username = "domain\username"
$password = "password"
$secPassword = convertTo-SecureString $password -AsPlainText -Force
$mycred = New-Object System.Management.Automation.PSCredential($username,$secPassword)
enter-pssession -Credential $mycred -Computername $servername
|
Send Email
Good way to test your SMTP Server.
1
2
3
4
5
6
| $SMTP = "mail.mycompany.net"
$Subject = "Test from $env:Computername"
$toaddress = "myemail@mycompany.net"
$fromaddress = $env:Computername + "@mycompany.net"
$body = "this message was sent via $SMTP at $(get-date)."
send-mailmessage -smtpserver $SMTP -to $toaddress -from $fromaddress -subject $subject -body $body
|
No comments:
Post a Comment