下面是一个简单的Powershell实现的端口监听程序,可以稍微改造一下变成一个后门:
$global:keepRunning = $true
$listener = $null
trap {
Write-Host "`nCtrl+C pressed, stopping server..."
if ($listener -ne $null -and $listener.Server.Connected) {
$listener.Stop()
Write-Host "Listener stopped, port 8080 freed."
}
$global:keepRunning = $false
continue
}
try {
$listener = [System.Net.Sockets.TcpListener]::new(8080)
$listener.Start()
Write-Host "Server is listening on port 8080..."
while ($global:keepRunning) {
if ($listener.Pending()) {
$client = $listener.AcceptTcpClient()
Write-Host "Client connected from $($client.Client.RemoteEndPoint)"
$runspace = [powershell]::Create().AddScript({
param ($client, $consoleUI)
function Handle-Client {
param ($client, $consoleUI)
$stream = $client.GetStream()
$reader = New-Object System.IO.StreamReader($stream)
$writer = New-Object System.IO.StreamWriter($stream)
$writer.AutoFlush = $true
$consoleUI.WriteLine("Client handler started for $($client.Client.RemoteEndPoint)")
try {
while ($true) {
if ($stream.DataAvailable) {
$data = $reader.ReadLine()
if ([string]::IsNullOrEmpty($data)) {
$consoleUI.WriteLine("Client disconnected or sent empty data.")
break
}
$consoleUI.WriteLine("Received from client: $data")
$writer.WriteLine("Echo: $data")
} else {
Start-Sleep -Milliseconds 100
}
}
} catch {
$consoleUI.WriteLine("Error while handling client: $_")
} finally {
if ($client.Connected) {
$client.Close()
}
$consoleUI.WriteLine("Client disconnected.")
}
}
Handle-Client $client $consoleUI
}).AddArgument($client).AddArgument($Host.UI)
$runspace.BeginInvoke() | Out-Null
} else {
Start-Sleep -Milliseconds 100
}
}
} catch {
Write-Host "Server encountered an error: $_"
} finally {
if ($listener -ne $null) {
$listener.Stop()
Write-Host "Listener stopped, port 8080 freed."
}
}
Write-Host "Server stopped."