Advertisement
joshtrier

blah

Jun 29th, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.00 KB | None | 0 0
  1. <?PHP
  2. set_time_limit(0);
  3. define("LOG_FILE","log.log");
  4. define("DEBUG","On");
  5. define("PORT",$argv[1]);
  6. define("BIND","127.0.0.1");
  7. define("KEY","passwd tits");
  8. define("BASH_DIR", "/home/josh/money/bash");
  9. class Daemon
  10. {
  11.    
  12.     var $commands;
  13.     var $input;
  14.     var $socket;
  15.     var $spawn;
  16.     var $includes = array('cmds.txt');
  17.     var $is_auth = 0;
  18.     var $auth_failures = 0;
  19.     var $type = "command"; 
  20.  
  21.     function daemon()
  22.     {
  23.         $this->init();
  24.         $this->socket_handler();
  25.  
  26.     }
  27.  
  28.     function init()
  29.     {
  30.         $this->init_check_root();
  31.         $this->init_check_port();
  32.         $this->init_check_includes();
  33.        
  34.         $this->commands_load();
  35.     }
  36.  
  37.     function authenticate()
  38.     {
  39.         $this->input = base64_decode($this->input);
  40.         $this->input = unserialize($this->input);
  41.         if($this->auth_failures < 2) {
  42.             if($this->input == KEY)
  43.                 $this->is_auth=1;  
  44.             else {
  45.                 $this->auth_failures++;
  46.             }  
  47.         }
  48.         else
  49.             $this->socket_write_output("You failed auth too many times. Goodbye sir.");
  50.  
  51.     }
  52.  
  53.     function init_check_root()
  54.     {
  55.         if(posix_getuid() != 0 && posix_getuid() != 1000)
  56.             $this->error("Daemon must be run as the root user");       
  57.  
  58.     }
  59.  
  60.     function init_check_includes()
  61.     {
  62.         foreach($this->includes as $include)
  63.         {
  64.             if(!file_exists($include))
  65.                 $this->error("File $include is missing!");
  66.         }      
  67.  
  68.     }
  69.  
  70.     function init_check_port()
  71.     {
  72.         $fp = @fsockopen(BIND,PORT,$errno,$errstr,10);
  73.         if($fp) {
  74.             fclose($fp);           
  75.             $this->error("Port (".PORT.") already in use... Exiting");
  76.         }
  77.     }
  78.  
  79.     function socket_handler()
  80.     {
  81.         $this->socket_open();
  82.         while(true)
  83.         {
  84.             $this->socket_read();
  85.         }
  86.  
  87.     }
  88.  
  89.     function command_exists()
  90.     {
  91.         $this->input = base64_decode($this->input);
  92.         $this->input = unserialize($this->input);
  93.         if(is_array($this->input)) {
  94.             if(key($this->input) == "Send") {
  95.                 $this->type = "Send";
  96.             }
  97.         }
  98.         else {
  99.             if(!array_key_exists($this->input,$this->commands))
  100.                 $this->socket_write_output("Invalid command\n");
  101.         }
  102.     }
  103.  
  104.     function write_to_file($data,$file)
  105.     {
  106.  
  107.             if (!$handle = fopen($file, 'w')) {
  108.                  $this->socket_write_output("Cannot open file ($file)");
  109.                  return false;
  110.             }
  111.  
  112.             if (fwrite($handle, $data) === FALSE) {
  113.                 $this->socket_write_output("Cannot write to file ($file)");
  114.                  return false;
  115.             }
  116.  
  117.             fclose($handle);
  118.  
  119.          return true;
  120.     }
  121.  
  122.     function send_do()
  123.     {
  124.         echo "output ->";
  125.         print_r($this->input);
  126.         $data = $this->input['Send']['Payload'];
  127.         $file = $this->input['Send']['File'];
  128.         $excecute = $this->input['Send']['File'];
  129.    
  130.         if($file != "" && $file != "Null")     
  131.             $res = $this->write_to_file($data,$file);
  132.    
  133.         if($res === true && $execute = "Yes") {
  134.             $out = exec("sh $file");
  135.             $this->socket_write_output($out);
  136.         }
  137.     }
  138.    
  139.  
  140.     function command_do()
  141.     {
  142.         foreach($this->commands[$this->input] as $type=>$cmd)
  143.         {
  144.             if($type == "system_call")
  145.                 $out = exec($cmd);
  146.             if($type == "bash_script")
  147.                 $out = exec("sh ".BASH_DIR."/$cmd");
  148.        
  149.                 $this->socket_write_output($out);
  150.  
  151.         }
  152.     }
  153.  
  154.     function commands_load()
  155.     {
  156.         $fh = fopen("cmds.txt","r");
  157.         $cmds = stream_get_contents($fh);
  158.         $cmds = explode("\n",$cmds);
  159.         $this->commands = array();
  160.         foreach($cmds as $cmd) {
  161.             if($cmd) {
  162.                 $parts = explode("^:",$cmd);
  163.                 $cmd_parts = explode("|||",$parts[1]);
  164.                 $this->commands[$parts[0]][$cmd_parts[0]] = $cmd_parts[1];
  165.             }
  166.         }
  167.  
  168.     }
  169.    
  170.     function socket_write_output($output)
  171.     {
  172.         $output .= "\n";
  173.         socket_write($this->spawn, $output, strlen ($output)) or $this->reopen_socket();
  174.  
  175.     }
  176.  
  177.     function socket_read()
  178.     {
  179.         sleep(.25);
  180.         $this->input = trim(socket_read($this->spawn, 1024)) or print("Could not read input\n");
  181.         if($this->is_auth == 0) {
  182.             $this->authenticate();
  183.             $this->socket_write_output("You need to auth first!");
  184.  
  185.         }
  186.         else {
  187.             $this->command_exists();
  188.             if($this->type == "Send")
  189.                 $this->send_do();
  190.             else
  191.                 $this->command_do();
  192.         }
  193.     }
  194.  
  195.     function reopen_socket()
  196.     {
  197.         $this->is_auth=0;
  198.         $this->auth_failures=0;
  199.         $this->socket_close();
  200.         $this->socket_open();
  201.     }
  202.  
  203.     function socket_close()
  204.     {
  205.         socket_close($this->spawn);
  206.         socket_close($this->socket);
  207.        
  208.     }
  209.  
  210.     function socket_open()
  211.     {
  212.         $this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or $this->error("Could not createsocket\n");    
  213.         socket_bind($this->socket, BIND, PORT) or $this->error("Could not bind to socket\n");      
  214.         socket_listen($this->socket, 3) or $this->error("Could not set up socket listener\n");
  215.         $this->spawn = socket_accept($this->socket) or $this->error("Could not accept incoming connection\n");
  216.         $this->log("Connection from ".socket_getpeername()."");
  217.         print_r($this->commands);
  218.     }
  219.  
  220.  
  221.     function error($data)
  222.     {
  223.         $this->log("ERROR: $data");
  224.         die();
  225.     }
  226.  
  227.  
  228.     function log($data)
  229.     {
  230.         $now = date("m/d/y H:i:s");
  231.         $fh = fopen(LOG_FILE,"a+");
  232.         fwrite($fh,$now.":  ".$data."\n");
  233.         fclose($fh);
  234.         if(DEBUG=="On")
  235.           echo $data."\n";
  236.     }
  237.  
  238. }
  239.  
  240. $test = new Daemon();
  241.  
  242. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement