Friday 5 December 2008

Send-UDPDatagram.ps1

  1. <#  
  2. .SYNOPSIS 
  3.     Sends a UDP datagram to a port 
  4. .DESCRIPTION 
  5.     This script used system.net.socckets to send a UDP 
  6.     datagram to a particular port. Being UDP, there's 
  7.     no way to determine if the UDP datagram actually 
  8.     was received.  
  9.     for this sample, a port was chosen (20000). 
  10. .NOTES 
  11.     File Name  : Send-UDPDatagram 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell V2 CTP3 
  14. .LINK 
  15.     http://www.pshscripts.blogspot.com 
  16. .EXAMPLE 
  17. #> 
  18.  
  19. ### 
  20. #  Start of Script 
  21. ## 
  22.  
  23. # Define port and target IP address 
  24. # Random here! 
  25. [int] $Port = 20000 
  26. $IP = "10.10.1.100" 
  27. $Address = [system.net.IPAddress]::Parse($IP
  28.  
  29. # Create IP Endpoint 
  30. $End = New-Object System.Net.IPEndPoint $address, $port 
  31.  
  32. # Create Socket 
  33. $Saddrf   = [System.Net.Sockets.AddressFamily]::InterNetwork 
  34. $Stype    = [System.Net.Sockets.SocketType]::Dgram 
  35. $Ptype    = [System.Net.Sockets.ProtocolType]::UDP 
  36. $Sock     = New-Object System.Net.Sockets.Socket $saddrf, $stype, $ptype 
  37. $Sock.TTL = 26 
  38.  
  39. # Connect to socket 
  40. $sock.Connect($end
  41.  
  42. # Create encoded buffer 
  43. $Enc     = [System.Text.Encoding]::ASCII 
  44. $Message = "Jerry Garcia Rocks`n"*10 
  45. $Buffer  = $Enc.GetBytes($Message
  46.  
  47. # Send the buffer 
  48. $Sent   = $Sock.Send($Buffer
  49. "{0} characters sent to: {1} " -f $Sent,$IP 
  50. "Message is:" 
  51. $Message 
  52. # End of Script 

 

If you run this script and are using WireShark to capture the network traffic, you might see something like this:

image

From this trace, you can see the UDP Datagram that was sent. And since the port does not exist on the target machine (10.10.1.100), that machine returns a Destination Port Unreachable as expected.

2 comments:

brettski said...

Lines 38 - 52 are covered up with the Wireshark image. Can you fix the layout so we can see the rest of the script?

Unknown said...

too bad the website owner doesn't fix this. i've parsed the html source and stripped the code:


<#  
.SYNOPSIS 
    Sends a UDP datagram to a port 
.DESCRIPTION 
    This script used system.net.socckets to send a UDP 
    datagram to a particular port. Being UDP, there's 
if the UDP datagram actually 
    was received.  
for this sample, a port was chosen (20000). 
.NOTES 
    File Name  : Send-UDPDatagram 
@psp.co.uk 
    Requires   : PowerShell V2 CTP3 
.LINK 
    http://www.pshscripts.blogspot.com 
.EXAMPLE 
#>  
 
###  
#  Start of Script  
##  
 
# Define port and target IP address  
# Random here!  
$Port = 20000 
$IP = "10.10.1.100"  
$Address = [system.net.IPAddress]::Parse( $IP ) 
 
# Create IP Endpoint  
$End = New-Object System.Net.IPEndPoint $address , $port  
 
# Create Socket  
$Saddrf    = [System.Net.Sockets.AddressFamily]::InterNetwork 
$Stype    = [System.Net.Sockets.SocketType]::Dgram 
$Ptype     = [System.Net.Sockets.ProtocolType]::UDP 
$Sock      = New-Object System.Net.Sockets.Socket $saddrf , $stype , $ptype  
$Sock.TTL = 26 
 
# Connect to socket  
$sock.Connect( $end ) 
 
# Create encoded buffer  
$Enc      = [System.Text.Encoding]::ASCII 
$Message = "Jerry Garcia Rocks`n" *10 
$Buffer   = $Enc.GetBytes( $Message ) 
 
# Send the buffer  
$Sent   = $Sock.Send( $Buffer ) 
"{0} characters sent to: {1} " -f $Sent , $IP  
"Message is:"  
$Message  
# End of Script