Created
January 15, 2018 17:03
-
-
Save VimalShekar/35265d71d7bbce5dff4212db886990d4 to your computer and use it in GitHub Desktop.
Send and Receive sample using PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function TcpSendRecv() | |
{ | |
param( | |
[int] $Port = 5005, | |
$IP = "127.0.0.1" , | |
$Message = "TRUN ." + "A"*6000 +". " | |
) | |
$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]::Stream | |
$Ptype = [System.Net.Sockets.ProtocolType]::Tcp #this could also be 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 | |
$Buffer = $Enc.GetBytes($Message) | |
# Send the buffer | |
$Sent = $Sock.Send($Buffer) | |
"{0} characters sent to: {1} " -f $Sent,$IP | |
"Message is: `n $Message" | |
#Now to receive -- we're assuming receive buffer is 400 | |
$buffer = new-object System.Byte[] 400 | |
$Received = $Sock.Receive($buffer) #-- oh-oh, buffer overflow exploit possible here... | |
"Received $Received bytes" | |
if($Received -ne 0) | |
{ | |
$Encode = new-object "System.Text.ASCIIEncoding" | |
$test = $Encode.GetString($buffer) | |
"TCP Message received:" $test | |
} | |
# End of Script | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment