Skip to content

Instantly share code, notes, and snippets.

@sasqwatch
Forked from staaldraad/receivefile.ps1
Created May 31, 2017 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasqwatch/37f8b930b9e0ea8671c3de1211e5576e to your computer and use it in GitHub Desktop.
Save sasqwatch/37f8b930b9e0ea8671c3de1211e5576e to your computer and use it in GitHub Desktop.
Small powershell script to bind to port, accept connection and stream to file. useful for ```cat blah.exe | nc 192.168.1.7 8080```
$socket = new-object System.Net.Sockets.TcpListener('0.0.0.0', 1080);
if($socket -eq $null){
exit 1;
}
$socket.start();
$client = $socket.AcceptTcpClient();
$stream = $client.GetStream();
$buffer = new-object System.Byte[] 2048;
$file = 'c:/afile.exe';
$fileStream = New-Object System.IO.FileStream($file, [System.IO.FileMode]'Create', [System.IO.FileAccess]'Write');
do
{
$read = $null;
while($stream.DataAvailable -or $read -eq $null) {
$read = $stream.Read($buffer, 0, 2048);
if ($read -gt 0) {
$fileStream.Write($buffer, 0, $read);
}
}
} While ($read -gt 0);
$fileStream.Close();
$socket.Stop();
$client.close();
$stream.Dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment