Powershell: TCP Client Windows
A TCP client with two-way communication (similar to Ncat)
$Host.UI.RawUI.WindowTitle = "TCP CLIENT"
# resize the powershell window
$Window = $Host.UI.RawUI.WindowSize
$Window.Height = 25
$Window.Width = 88
$Host.UI.RawUI.Set_WindowSize($Window)
Write-Host "-------------------------------------- TCP CLIENT -------------------------------------" -ForegroundColor Yellow
$dst_ip = Read-Host -Prompt "Destination IP Address"
[int32]$dst_port = Read-Host -Prompt "Destination Port"
# Build the tcp_client object
$tcp_client = New-Object System.Net.Sockets.TcpClient
# attempt to connect
try {
Write-Host "Attempting to connect to ${dst_ip}:${dst_port}" -ForegroundColor Cyan
$tcp_client.Connect($dst_ip, $dst_port)
}
catch {
Write-Host "Failed to connect to ${dst_ip}:${dst_port}" -ForegroundColor Red
Write-Host $_ -ForegroundColor Red
Pause
Exit
}
Clear-Host
$Host.UI.RawUI.WindowTitle = "TCP CLIENT CONNECTION $($tcp_client.Client.LocalEndPoint) --> $($tcp_client.Client.RemoteEndPoint)"
Write-Host "---------------------------------------------------------------------------------------" -ForegroundColor Yellow
Write-Host "TCP CLIENT CONNECTION $($tcp_client.Client.LocalEndPoint) --> $($tcp_client.Client.RemoteEndPoint)" -ForegroundColor Yellow
Write-Host "---------------------------------------------------------------------------------------" -ForegroundColor Yellow
# grab stream reader
$tcp_stream = $tcp_client.GetStream()
# set the read variables
$read_string = ""
$read_buffer = New-Object byte[] 65536
$key_read = $true
# set the write variables
$write_string = ""
$encoding = [System.Text.Encoding]::ASCII
while ($tcp_client.Connected) {
# checks if data is available to be printed
if ($tcp_stream.DataAvailable) {
if ($key_read) {
0..$($write_string.Length) | ForEach-Object { Write-Host "`b `b" -NoNewline }
$key_read = $false
}
# read the input
$read_bytes = $tcp_stream.Read($read_buffer, 0, 1024)
$read_string = [Text.Encoding]::ASCII.GetString($read_buffer, 0, $read_bytes)
# write to the screen
Write-Host "$($tcp_client.Client.RemoteEndPoint)> " -ForegroundColor Green -NoNewline
Write-Host $read_string -NoNewline -ForegroundColor Green
if (-not $read_string.EndsWith("`n")) {
Write-Host ""
}
# clear the read buffer
0..${read_bytes} | ForEach-Object {
$read_buffer[$_] = [byte]0
}
}
if (-not $key_read) {
Write-Host $write_string -NoNewline
[console]::SetCursorPosition($write_string.Length, [console]::CursorTop)
$key_read = $true
}
# checks if a keyboard input has been read (NOTE: this can read multiple queued keypresses)
while ([Console]::KeyAvailable) {
$key_read = $true
$key = [console]::ReadKey()
if ($key.Key -eq "Enter") {
# send the write_string
$write_string = $write_string + "`n"
$out_buffer = $encoding.GetBytes($write_string)
# exception handling for closed connections
try {
$tcp_stream.Write($out_buffer, 0, $write_string.Length) 1>$null
}
catch {
Write-Host "Failed to write to remote endpoint: $($tcp_client.Client.RemoteEndPoint)" -ForegroundColor Red
if ($tcp_client.Connected -ne $true) {
Write-Host "The connection was closed by the remote host" -ForegroundColor Red
}
break
}
# write to the screen
Write-Host "$($tcp_client.Client.LocalEndPoint)> " -ForegroundColor Cyan -NoNewline
Write-Host "$($write_string.Remove($write_string.Length - 1))" -ForegroundColor Cyan
# clears the write_string
$write_string = ""
break
} elseif ($key.Key -eq "Backspace") {
Write-Host " `b" -NoNewline
if ($write_string.Length -ne 0) {
$write_string = $write_string.Remove($write_string.Length - 1)
}
} elseif ($key.Key -eq "Escape") {
# this is a placeholder that will be used to cancel the input
} else {
$write_string += $key.KeyChar
}
}
}
Write-Host "---------------------------------- CONNECTION CLOSED ----------------------------------" -ForegroundColor Yellow
Write-Host "`nPress ENTER to Exit"
Read-Host
Powershell: TCP Server Windows
A simple TCP server with two-way communication (similar to Ncat)
$Host.UI.RawUI.WindowTitle = "TCP SERVER"
# resize the powershell window
$Window = $Host.UI.RawUI.WindowSize
$Window.Height = 25
$Window.Width = 88
$Host.UI.RawUI.Set_WindowSize($Window)
Write-Host "-------------------------------------- TCP SERVER -------------------------------------" -ForegroundColor Yellow
# determine if listener should be open to the network or only on loopback
$scope = Read-Host -Prompt "Local (127.0.0.1) or Remote (0.0.0.0)? L/R"
if ($scope -contains "L" -or $scope -contains "Local") {
Write-Host "Setting up listener on loopback (127.0.0.1)" -ForegroundColor Cyan
$src_ip = [ipaddress]"127.0.0.1"
} elseif ($scope -contains "R" -or $scope -contains "Remote") {
Write-Host "Setting up network-facing listener (0.0.0.0)" -ForegroundColor Cyan
$src_ip = [ipaddress]"0.0.0.0"
} else {
Write-Host "Invalid input, defaulting to setting up listener on loopback (127.0.0.1)" -ForegroundColor Red
$src_ip = [ipaddress]"127.0.0.1"
}
[int32]$src_port = Read-Host -Prompt "Destination Port"
# attempt to connect
try {
Write-Host "Opening local endpoint on ${src_ip}:${src_port}" -ForegroundColor Cyan
$tcp_listener = New-Object System.Net.Sockets.TcpListener($src_ip, [int32]$src_port)
$tcp_listener.Start()
}
catch {
Write-Host "Failed to open local endpoint on ${src_ip}:${src_port}" -ForegroundColor Red
Write-Host $_ -ForegroundColor Red
Pause
Exit
}
Write-Host "Waiting on connection" -ForegroundColor Yellow
# blocks the script until a connection is received
$tcp_client = $tcp_listener.AcceptTcpClient()
# stop the listener to prevent additional connections
$tcp_listener.Stop()
Clear-Host
$Host.UI.RawUI.WindowTitle = "TCP SERVER CONNECTION $($tcp_client.Client.LocalEndPoint) --> $($tcp_client.Client.RemoteEndPoint)"
Write-Host "---------------------------------------------------------------------------------------" -ForegroundColor Yellow
Write-Host "TCP SERVER CONNECTION $($tcp_client.Client.LocalEndPoint) <-- $($tcp_client.Client.RemoteEndPoint)" -ForegroundColor Yellow
Write-Host "---------------------------------------------------------------------------------------" -ForegroundColor Yellow
# create tcp stream
$tcp_stream = $tcp_client.GetStream()
# set the read variables
$read_string = ""
$read_buffer = New-Object byte[] 65536
$key_read = $true
# set the write variables
$write_string = ""
$encoding = [System.Text.Encoding]::ASCII
try {
while ($tcp_client.Connected) {
# checks if data is available to be printed
if ($tcp_stream.DataAvailable) {
if ($key_read) {
0..$($write_string.Length) | ForEach-Object { Write-Host "`b `b" -NoNewline }
$key_read = $false
}
# read the input
$read_bytes = $tcp_stream.Read($read_buffer, 0, 1024)
$read_string = [Text.Encoding]::ASCII.GetString($read_buffer, 0, $read_bytes)
# write to the screen
Write-Host "$($tcp_client.Client.RemoteEndPoint)> " -ForegroundColor Green -NoNewline
Write-Host $read_string -NoNewline -ForegroundColor Green
if (-not $read_string.EndsWith("`n")) {
Write-Host ""
}
# clear the read buffer
0..${read_bytes} | ForEach-Object {
$read_buffer[$_] = [byte]0
}
}
if (-not $key_read) {
Write-Host $write_string -NoNewline
[console]::SetCursorPosition($write_string.Length, [console]::CursorTop)
$key_read = $true
}
# checks if a keyboard input has been read (NOTE: this can read multiple queued keypresses)
while ([Console]::KeyAvailable) {
$key_read = $true
$key = [console]::ReadKey()
if ($key.Key -eq "Enter") {
# send the write_string
$write_string = $write_string + "`n"
$out_buffer = $encoding.GetBytes($write_string)
$tcp_stream.Write($out_buffer, 0, $write_string.Length) 1>$null
# write to the screen
Write-Host "$($tcp_client.Client.LocalEndPoint)> " -ForegroundColor Cyan -NoNewline
Write-Host "$($write_string.Remove($write_string.Length - 1))" -ForegroundColor Cyan
# clears the write_string
$write_string = ""
break
} elseif ($key.Key -eq "Backspace") {
Write-Host " `b" -NoNewline
if ($write_string.Length -ne 0) {
$write_string = $write_string.Remove($write_string.Length - 1)
}
} elseif ($key.Key -eq "Escape") {
# this is a placeholder that will be used to cancel the input
} else {
$write_string += $key.KeyChar
}
}
}
} finally {
# shutdown the listener and client
$tcp_client.Close()
$tcp_listener.Dispose()
$tcp_client.Dispose()
# clear variables
$read_string = $null
$read_buffer = $null
}
Write-Host "---------------------------------- CONNECTION CLOSED ----------------------------------" -ForegroundColor Yellow
Write-Host "`nPress ENTER to Exit"
Read-Host
Powershell: UDP Client Windows
A UDP client with two-way communication (similar to Ncat)
$Host.UI.RawUI.WindowTitle = "UDP CLIENT"
# resize the powershell window
$Window = $Host.UI.RawUI.WindowSize
$Window.Height = 25
$Window.Width = 88
$Host.UI.RawUI.Set_WindowSize($Window)
Write-Host "-------------------------------------- UDP CLIENT -------------------------------------" -ForegroundColor Yellow
$dst_ip = Read-Host -Prompt "Destination IP Address"
[int32]$dst_port = Read-Host -Prompt "Destination Port"
# Build the udp_client object
$udp_client = New-Object System.Net.Sockets.UdpClient
# attempt to connect
try {
Write-Host "Attempting to connect to ${dst_ip}:${dst_port}" -ForegroundColor Cyan
$udp_client.Connect($dst_ip, $dst_port)
}
catch {
Write-Host "Failed to connect to ${dst_ip}:${dst_port}" -ForegroundColor Red
Write-Host $_ -ForegroundColor Red
Pause
Exit
}
Clear-Host
$Host.UI.RawUI.WindowTitle = "UDP CLIENT CONNECTION $($udp_client.Client.LocalEndPoint) --> $($udp_client.Client.RemoteEndPoint)"
Write-Host "---------------------------------------------------------------------------------------" -ForegroundColor Yellow
Write-Host "UDP CLIENT CONNECTION $($udp_client.Client.LocalEndPoint) --> $($udp_client.Client.RemoteEndPoint)" -ForegroundColor Yellow
Write-Host "---------------------------------------------------------------------------------------" -ForegroundColor Yellow
# set the read variables
$async_receive = $udp_client.ReceiveAsync()
$read_string = ""
$key_read = $true
# set the write variables
$write_string = ""
$encoding = [System.Text.Encoding]::ASCII
while ($udp_client.Client.Connected) {
# checks if data is available to be printed
if ($async_receive.IsCompleted) {
if ($key_read) {
0..$($write_string.Length) | ForEach-Object { Write-Host "`b `b" -NoNewline }
$key_read = $false
}
# read the input
$read_bytes = $async_receive.Result.Buffer.Length
$read_buffer = $async_receive.Result.Buffer
$read_string = [Text.Encoding]::ASCII.GetString($read_buffer, 0, $read_bytes)
# write to the screen
Write-Host "$($udp_client.Client.RemoteEndPoint)> " -ForegroundColor Green -NoNewline
Write-Host $read_string -NoNewline -ForegroundColor Green
if (-not $read_string.EndsWith("`n")) {
Write-Host ""
}
# create a new async receive object
$async_receive = $udp_client.ReceiveAsync()
}
if (-not $key_read) {
Write-Host $write_string -NoNewline
[console]::SetCursorPosition($write_string.Length, [console]::CursorTop)
$key_read = $true
}
# checks if a keyboard input has been read (NOTE: this can read multiple queued key presses)
while ([Console]::KeyAvailable) {
$key_read = $true
$key = [console]::ReadKey()
if ($key.Key -eq "Enter") {
# send the write_string
$write_string = $write_string + "`n"
$out_buffer = $encoding.GetBytes($write_string)
# exception handling for closed connections
try {
$udp_client.Send($out_buffer) 1>$null
}
catch {
Write-Host "Failed to write to remote endpoint: $($udp_client.Client.RemoteEndPoint) $_" -ForegroundColor Red
if ($udp_client.Active -ne $true) {
Write-Host "The connection was closed by the remote host" -ForegroundColor Red
}
break
}
# write to the screen
Write-Host "$($udp_client.Client.LocalEndPoint)> " -ForegroundColor Cyan -NoNewline
Write-Host "$($write_string.Remove($write_string.Length - 1))" -ForegroundColor Cyan
# clears the write_string
$write_string = ""
break
} elseif ($key.Key -eq "Backspace") {
Write-Host " `b" -NoNewline
if ($write_string.Length -ne 0) {
$write_string = $write_string.Remove($write_string.Length - 1)
}
} elseif ($key.Key -eq "Escape") {
# this is a placeholder that will be used to cancel the input
} else {
$write_string += $key.KeyChar
}
}
}
Write-Host "---------------------------------- CONNECTION CLOSED ----------------------------------" -ForegroundColor Yellow
Write-Host "`nPress ENTER to Exit"
Read-Host
Powershell: UDP Server Windows
A simple UDP server with two-way communication (similar to Ncat)
$Host.UI.RawUI.WindowTitle = "UDP SERVER"
# resize the powershell window
$Window = $Host.UI.RawUI.WindowSize
$Window.Height = 25
$Window.Width = 88
$Host.UI.RawUI.Set_WindowSize($Window)
Write-Host "-------------------------------------- UDP SERVER -------------------------------------" -ForegroundColor Yellow
# determine if listener should be open to the network or only on loopback
$scope = Read-Host -Prompt "Local (127.0.0.1) or Remote (0.0.0.0)? L/R"
if ($scope -contains "L" -or $scope -contains "Local") {
Write-Host "Setting up listener on loopback (127.0.0.1)" -ForegroundColor Cyan
$src_ip = [ipaddress]"127.0.0.1"
} elseif ($scope -contains "R" -or $scope -contains "Remote") {
Write-Host "Setting up network-facing listener (0.0.0.0)" -ForegroundColor Cyan
$src_ip = [ipaddress]"0.0.0.0"
} else {
Write-Host "Invalid input, defaulting to setting up listener on loopback (127.0.0.1)" -ForegroundColor Red
$src_ip = [ipaddress]"127.0.0.1"
}
[int32]$src_port = Read-Host -Prompt "Destination Port"
# attempt to connect
try {
Write-Host "Opening local endpoint on ${src_ip}:${src_port}" -ForegroundColor Cyan
$group_endpoint = New-Object System.Net.IPEndPoint($src_ip, $src_port)
$udp_client = New-Object System.Net.Sockets.UdpClient($group_endpoint)
}
catch {
Write-Host "Failed to open local endpoint on ${src_ip}:${src_port}" -ForegroundColor Red
Write-Host $_ -ForegroundColor Red
Pause
Exit
}
Clear-Host
$Host.UI.RawUI.WindowTitle = "UDP SERVER ON $($udp_client.Client.LocalEndPoint)"
Write-Host "---------------------------------------------------------------------------------------" -ForegroundColor Yellow
Write-Host "UDP SERVER ON $($udp_client.Client.LocalEndPoint)" -ForegroundColor Yellow
Write-Host "---------------------------------------------------------------------------------------" -ForegroundColor Yellow
# set the read variables
$async_receive = $udp_client.ReceiveAsync()
$read_string = ""
$key_read = $true
# set the write variables
$write_string = ""
$remote_endpoint = $null
$encoding = [System.Text.Encoding]::ASCII
try {
while ($true) {
# checks if data is available to be printed
if ($async_receive.IsCompleted) {
if ($key_read) {
0..$($write_string.Length) | ForEach-Object { Write-Host "`b `b" -NoNewline }
$key_read = $false
}
# read the input
$read_bytes = $async_receive.Result.Buffer.Length
$read_buffer = $async_receive.Result.Buffer
$remote_endpoint = $async_receive.Result.RemoteEndPoint
$read_string = [Text.Encoding]::ASCII.GetString($read_buffer, 0, $read_bytes)
# write to the screen
Write-Host "${remote_endpoint}> " -ForegroundColor Green -NoNewline
Write-Host $read_string -NoNewline -ForegroundColor Green
if (-not $read_string.EndsWith("`n")) {
Write-Host ""
}
# create a new async receive object
$async_receive = $udp_client.ReceiveAsync()
}
if (-not $key_read) {
Write-Host $write_string -NoNewline
[console]::SetCursorPosition($write_string.Length, [console]::CursorTop)
$key_read = $true
}
# checks if a keyboard input has been read (NOTE: this can read multiple queued key presses)
while ([Console]::KeyAvailable) {
$key_read = $true
$key = [console]::ReadKey()
if ($key.Key -eq "Enter") {
# send the write_string
$write_string = $write_string + "`n"
$out_buffer = $encoding.GetBytes($write_string)
# exception handling for closed connections
try {
$udp_client.Send($out_buffer, $remote_endpoint) 1>$null
}
catch {
Write-Host "Failed to write to remote endpoint: $remote_endpoint $_" -ForegroundColor Red
if ($udp_client.Active -ne $true) {
Write-Host "The connection was closed by the remote host" -ForegroundColor Red
}
break
}
# write to the screen
Write-Host "$($udp_client.Client.LocalEndPoint) to $remote_endpoint> " -ForegroundColor Cyan -NoNewline
Write-Host "$($write_string.Remove($write_string.Length - 1))" -ForegroundColor Cyan
# clears the write_string
$write_string = ""
break
} elseif ($key.Key -eq "Backspace") {
Write-Host " `b" -NoNewline
if ($write_string.Length -ne 0) {
$write_string = $write_string.Remove($write_string.Length - 1)
}
} elseif ($key.Key -eq "Escape") {
# this is a placeholder that will be used to cancel the input
} else {
$write_string += $key.KeyChar
}
}
}
} finally {
# shutdown the listener and client
$udp_client.Close()
$udp_client.Dispose()
# clear variables
$remote_endpoint = $null
$read_string = $null
$read_buffer = $null
}
Write-Host "---------------------------------- CONNECTION CLOSED ----------------------------------" -ForegroundColor Yellow
Write-Host "`nPress ENTER to Exit"
Read-Host
Bash: TCP Client Linux
A simple TCP client with two-way communication (similar to Ncat)
#!/usr/bin/env bash
read -p "Remote host: " host
read -p "Remote port: " port
# open bidirectional fd 3
exec 3<>/dev/tcp/$host/$port || { echo "connect to ${host}:${port} failed"; exit 1; }
# determine our ephemeral local port
local_port=$(ss -tanp 2>/dev/null | awk -v h="$host:$port" '$1=="ESTAB" && $5==h {split($4,a,":"); print a[length(a)]}' | head -n1)
[[ -z "$local_port" ]] && local_port="??"
# cleanup on exit
cleanup() {
exec 3>&- 3<&-
[[ -n "$reader_pid" ]] && kill "$reader_pid" 2>/dev/null
}
trap cleanup EXIT INT
# background reader
while IFS= read -r line <&3; do
printf '%s:%s> %s\n' "$host" "$port" "$line"
done &
reader_pid=$!
echo "connected to $host:$port"
echo "type messages to send. '/quit' to exit."
# small helper: move cursor up 1 line + clear that line
move_up_and_clear() {
if command -v tput >/dev/null 2>&1; then
tput cuu1 # up 1 line
tput cr # to column 0
tput el # clear to end of line
else
printf '\033[1A\r\033[2K' # ANSI fallback
fi
}
# main loop
while IFS= read -r userline; do
[[ "$userline" == "/quit" ]] && break
# clear the user entered line, and reprint it with the RHP
move_up_and_clear
printf '127.0.0.1:%s> %s\n' "$local_port" "$userline"
# transmit the data to the remote host
printf '%s\r\n' "$userline" >&3
done
cleanup