Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- set_time_limit(0);
- define("LOG_FILE","log.log");
- define("DEBUG","On");
- define("PORT",$argv[1]);
- define("BIND","127.0.0.1");
- define("KEY","passwd tits");
- define("BASH_DIR", "/home/josh/money/bash");
- class Daemon
- {
- var $commands;
- var $input;
- var $socket;
- var $spawn;
- var $includes = array('cmds.txt');
- var $is_auth = 0;
- var $auth_failures = 0;
- var $type = "command";
- function daemon()
- {
- $this->init();
- $this->socket_handler();
- }
- function init()
- {
- $this->init_check_root();
- $this->init_check_port();
- $this->init_check_includes();
- $this->commands_load();
- }
- function authenticate()
- {
- $this->input = base64_decode($this->input);
- $this->input = unserialize($this->input);
- if($this->auth_failures < 2) {
- if($this->input == KEY)
- $this->is_auth=1;
- else {
- $this->auth_failures++;
- }
- }
- else
- $this->socket_write_output("You failed auth too many times. Goodbye sir.");
- }
- function init_check_root()
- {
- if(posix_getuid() != 0 && posix_getuid() != 1000)
- $this->error("Daemon must be run as the root user");
- }
- function init_check_includes()
- {
- foreach($this->includes as $include)
- {
- if(!file_exists($include))
- $this->error("File $include is missing!");
- }
- }
- function init_check_port()
- {
- $fp = @fsockopen(BIND,PORT,$errno,$errstr,10);
- if($fp) {
- fclose($fp);
- $this->error("Port (".PORT.") already in use... Exiting");
- }
- }
- function socket_handler()
- {
- $this->socket_open();
- while(true)
- {
- $this->socket_read();
- }
- }
- function command_exists()
- {
- $this->input = base64_decode($this->input);
- $this->input = unserialize($this->input);
- if(is_array($this->input)) {
- if(key($this->input) == "Send") {
- $this->type = "Send";
- }
- }
- else {
- if(!array_key_exists($this->input,$this->commands))
- $this->socket_write_output("Invalid command\n");
- }
- }
- function write_to_file($data,$file)
- {
- if (!$handle = fopen($file, 'w')) {
- $this->socket_write_output("Cannot open file ($file)");
- return false;
- }
- if (fwrite($handle, $data) === FALSE) {
- $this->socket_write_output("Cannot write to file ($file)");
- return false;
- }
- fclose($handle);
- return true;
- }
- function send_do()
- {
- echo "output ->";
- print_r($this->input);
- $data = $this->input['Send']['Payload'];
- $file = $this->input['Send']['File'];
- $excecute = $this->input['Send']['File'];
- if($file != "" && $file != "Null")
- $res = $this->write_to_file($data,$file);
- if($res === true && $execute = "Yes") {
- $out = exec("sh $file");
- $this->socket_write_output($out);
- }
- }
- function command_do()
- {
- foreach($this->commands[$this->input] as $type=>$cmd)
- {
- if($type == "system_call")
- $out = exec($cmd);
- if($type == "bash_script")
- $out = exec("sh ".BASH_DIR."/$cmd");
- $this->socket_write_output($out);
- }
- }
- function commands_load()
- {
- $fh = fopen("cmds.txt","r");
- $cmds = stream_get_contents($fh);
- $cmds = explode("\n",$cmds);
- $this->commands = array();
- foreach($cmds as $cmd) {
- if($cmd) {
- $parts = explode("^:",$cmd);
- $cmd_parts = explode("|||",$parts[1]);
- $this->commands[$parts[0]][$cmd_parts[0]] = $cmd_parts[1];
- }
- }
- }
- function socket_write_output($output)
- {
- $output .= "\n";
- socket_write($this->spawn, $output, strlen ($output)) or $this->reopen_socket();
- }
- function socket_read()
- {
- sleep(.25);
- $this->input = trim(socket_read($this->spawn, 1024)) or print("Could not read input\n");
- if($this->is_auth == 0) {
- $this->authenticate();
- $this->socket_write_output("You need to auth first!");
- }
- else {
- $this->command_exists();
- if($this->type == "Send")
- $this->send_do();
- else
- $this->command_do();
- }
- }
- function reopen_socket()
- {
- $this->is_auth=0;
- $this->auth_failures=0;
- $this->socket_close();
- $this->socket_open();
- }
- function socket_close()
- {
- socket_close($this->spawn);
- socket_close($this->socket);
- }
- function socket_open()
- {
- $this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or $this->error("Could not createsocket\n");
- socket_bind($this->socket, BIND, PORT) or $this->error("Could not bind to socket\n");
- socket_listen($this->socket, 3) or $this->error("Could not set up socket listener\n");
- $this->spawn = socket_accept($this->socket) or $this->error("Could not accept incoming connection\n");
- $this->log("Connection from ".socket_getpeername()."");
- print_r($this->commands);
- }
- function error($data)
- {
- $this->log("ERROR: $data");
- die();
- }
- function log($data)
- {
- $now = date("m/d/y H:i:s");
- $fh = fopen(LOG_FILE,"a+");
- fwrite($fh,$now.": ".$data."\n");
- fclose($fh);
- if(DEBUG=="On")
- echo $data."\n";
- }
- }
- $test = new Daemon();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement