Created
September 28, 2014 03:17
-
-
Save jstangroome/9adaa87a845e5be906c8 to your computer and use it in GitHub Desktop.
A simple PowerShell implementation of the most basic functionality of Netcat
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 Send-NetworkData { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string] | |
$Computer, | |
[Parameter(Mandatory)] | |
[ValidateRange(1, 65535)] | |
[Int16] | |
$Port, | |
[Parameter(ValueFromPipeline)] | |
[string[]] | |
$Data, | |
[System.Text.Encoding] | |
$Encoding = [System.Text.Encoding]::ASCII, | |
[TimeSpan] | |
$Timeout = [System.Threading.Timeout]::InfiniteTimeSpan | |
) | |
begin { | |
# establish the connection and a stream writer | |
$Client = New-Object -TypeName System.Net.Sockets.TcpClient | |
$Client.Connect($Computer, $Port) | |
$Stream = $Client.GetStream() | |
$Writer = New-Object -Type System.IO.StreamWriter -ArgumentList $Stream, $Encoding, $Client.SendBufferSize, $true | |
} | |
process { | |
# send all the input data | |
foreach ($Line in $Data) { | |
$Writer.WriteLine($Line) | |
} | |
} | |
end { | |
# flush and close the connection send | |
$Writer.Flush() | |
$Writer.Dispose() | |
$Client.Client.Shutdown('Send') | |
# read the response | |
$Stream.ReadTimeout = [System.Threading.Timeout]::Infinite | |
if ($Timeout -ne [System.Threading.Timeout]::InfiniteTimeSpan) { | |
$Stream.ReadTimeout = $Timeout.TotalMilliseconds | |
} | |
$Result = '' | |
$Buffer = New-Object -TypeName System.Byte[] -ArgumentList $Client.ReceiveBufferSize | |
do { | |
try { | |
$ByteCount = $Stream.Read($Buffer, 0, $Buffer.Length) | |
} catch [System.IO.IOException] { | |
$ByteCount = 0 | |
} | |
if ($ByteCount -gt 0) { | |
$Result += $Encoding.GetString($Buffer, 0, $ByteCount) | |
} | |
} while ($Stream.DataAvailable -or $Client.Client.Connected) | |
Write-Output $Result | |
# cleanup | |
$Stream.Dispose() | |
$Client.Dispose() | |
} | |
} | |
# pipe in a HTTP request: | |
'GET / HTTP/1.0', '' | Send-NetworkData -Computer www.powershellmagazine.com -Port 80 | |
# Use the Data parameter to do the same: | |
Send-NetworkData -Data 'GET / HTTP/1.0', '' -Computer www.powershellmagazine.com -Port 80 | |
# As before but only wait 2 seconds for a response: | |
Send-NetworkData -Data 'GET / HTTP/1.0', '' -Computer www.powershellmagazine.com -Port 80 -Timeout 0:00:02 | |
# Say hello to an SMTP server: | |
Send-NetworkData -Data "EHLO $Env:ComputerName", "QUIT" -Computer mail.example.com -Port 25 |
A complete Bash Netcat replacement,
echo -ne "ehlo [email protected]\r\nmail from:<[email protected]>\r\nrcpt to:<[email protected]>\r\ndata\r\nSubject: test\r\n\r\nHello, world\r\n.\r\nquit\r\n" | { mapfile -t lines; lines=("${lines[@]//$'\r'/}"); pslines=("${lines[@]/#/\"}"); pslines=("${pslines[@]/%/\"}"); pslines="${pslines[*]/%/,}"; pslines="${pslines:0: -1}"; powershell -noprofile -command '.\netcat.ps1' -Computer SERVER -Port 25 -Data "${pslines}" -Timeout "0:00:02"; }
Using =$True
in [Parameter(...=$True)]
makes a similar script compatible with PowerShell 2.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
} while ($Stream.DataAvailable -or $Client.Client.Connected)
} while ($Stream.DataAvailable -and $Client.Client.Connected)