Passing a value to a file called using php exec function - php

I am using exec function to run my php file in background from another like below
<?php
$username = 'Test';
exec(PHP_BINDIR."/php /opt/lampp/htdocs/myscript/test.php >/dev/null &" );
?>
i want to send a value to the file which runs in background.
i tried below code
<?php
$username = 'Test';
exec(PHP_BINDIR."/php /opt/lampp/htdocs/myscript/test.php?user=".$username." >/dev/null &" );
?>
Test.php
<?php
var_dump($_REQUEST);
?>
but i got null as the value. can any one help me. how i can pass a value to a file which is running in background.

Pass it as command line argument:
$command = sprintf('%s/php /opt/lampp/htdocs/myscript/test.php %s >/dev/null &',
PHP_BINDIR,
escapeshellarg($username));
exec($command);
In the file:
$username = $argv[1];
You can't use URL-style query parameters because you're not using a URL, you're calling an executable.

Related

sudo useradd with PHP

I've just started checking out PHP and to sort of work it out I thought that I would make a form that adds a user to the system with PHP. I tried to run the command with exec(), but that yielded no results(I made sure that www-data is a sudoer), so I tried it with system() to get some output that might tell me what the problem was, but both of my outputs just returned a vague 1 and 6 in that order. Here is my code:
<?php $username = $_POST["username"];
$password = $_POST["password"];
?>
<html>
<body>
Welcome <?php echo $username; ?><br>
Your password will be set to: <?php echo $password; ?>
<?php system("sudo useradd -m $username", $output1);
system("sudo usermod -g user $username", $output2);
?>
<br>
<?php echo $output1;?>
<br>
<?php echo $output2;?>
</body>
</html>
It correctly prints out the username and password variables, but seems to be unable to run the sudo useradd and sudo usermod commands, although I'm not entirely sure.Is there anything here that might give an Idea as to what I am doing wrong?
Have you try with proc_open?
Something like this...
$cmd = "echo 1";
$descriptorspec = array(
0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
1 => array('file', '/var/www/html/proc-output.txt', 'a'), // stdout is a pipe that the child will write to
2 => array('file', '/var/www/html/error-output.txt', 'a') // stderr is a file to write to
);
$pipes = [];
$process = proc_open($cmd, $descriptorspec, $pipes);
$return_value = is_resource($process) ? proc_close($process) : die('this does not work');
How you input the www-data password?

How to answer to command response? (shell_exec, exec)

I should enter confirmation code after my function execute. My command wait an answer but I can't reply.
$cmd = 'cd/ && cd .... && ....';
echo shell_exec($cmd);
I tried:
shell_exec($confirmation_code);
I must enter confirmation code after get permission for some access.
Try this
$cmd = 'echo "Yes"| cd/ && cd .... && ....';
echo shell_exec($cmd);
Replace "Yes" by what you want
Symphony Process command can do this for you:
https://symfony.com/doc/current/components/process.html#streaming-to-the-standard-input-of-a-process

Can't execute powershell script function using PHPs shell_exec

I'm trying to pass arguments from a PHP page's POST request into a powershell script.
This is the relevant PHP snippet:
$selectedPartner = $_POST['partner'];
$selectedGroup = $_POST['group'];
$script = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe AddRemPartners";
if (isset($_POST['partner']) && isset($_POST['group'])){
if (isset($_POST['AddButton']) && $selectedPartner !== "Select Partner" && $selectedGroup !== "Select Group") {
echo "<br>";
echo "Adding " . $selectedPartner . " to " . $selectedGroup . "...<br>";
$cmd = $script . " -Add $selectedPartner $selectedGroup";
echo "command is:<br>" . $cmd;
shell_exec($cmd);
//shell_exec('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe C:\\xampp\\htdocs\\admin\\AddRemPartners.ps1 -Add $selectedPartner //$selectedGroup');
//$command = shell_exec('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe telnet 10.11.14.32 4444');
//echo "User added successfully!";
}
if (isset($_POST['RemoveButton']) && $selectedPartner !== "Select Partner" && $selectedGroup !== "Select Group") {
echo "<br>";
//echo "selection was REMOVE";
}
}
And this is my powershell script:
Param([switch]$Add, [switch]$Remove, [string]$User, [string]$Group)
$secpasswd = ConvertTo-SecureString "P#sSw0rd" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("userPortal", $secpasswd)
$server = "host.fqdn"
function AddRemPartners
{
if ($Add){
Write-Host "Add var = $Add"
Write-Host "Add was selected"
Add-ADGroupMember -Server $server -Credential $creds -Identity $Group -Member $User
}
if ($Remove) {
Write-Host "Remove var = $Remove"
Write-Host "Remove was selected"
Remove-ADGroupMember -Server $server -Credential $creds -Identity "$Group" -Member "$User" -Confirm:$false
}
}
AddRemPartners -Add $Add -Remove $Remove -User $User -Group $Group
Things I know:
The php post parameters are good. I captured the request in burpsuit and know that all the correct args are getting sent
The resultant ps query that is built is also good. I output it to the screen and it looks like this:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe AddRemPartners -Add Dude1 Dude1Group
No network issues. If I run the above command directly from within PowerShell is executes correctly.
I've sourced my script using . .\AddRemPartners.ps1 so I can call my function directly as such:
PS > AddRemPartners -Add User Group
PS > AddRemPartners -Remove User Group
I've narrowed it down to this: I can't execute my ps script from cmd like this:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe AddRemPartners -Add User Group
As that errors out with this:
AddRemPartners : The term 'AddRemPartners' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
I believe this to be suspect/culpable since I need run that in PHP. So PHP is probably running into the same issue. So why won't cmd recognize my script if I call powershell at the same time as execution?
Any tips and guidance greatly appreciated. I'm at wits end unfortunately. :/
First parameter for PS should be full path of script. Then you should respect parameter definition of PS, on command line as in the script.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\whereis\AddRemPartners.ps1 -Add -user User -group Group
First line of the script should be parameter definition and defaults.
param([string]$user, [string]$group, [switch]$Add, ...);

PHP use pipe and stdin from user input

When running
ls | php script.php
fgets(STDIN) is not waiting for user input:
<?php
$pipe = stream_get_contents(STDIN);
echo "Enter something";
$line = fgets(STDIN);
But if I run
php script2.php
With script2.php :
<?php
echo "Enter something";
$line = fgets(STDIN);
The script pauses waiting for my input.
How can I get pipe & wait for user input?
Instead use readline function of php, it will work fine,
$string=readline("Enter value: ");
echo $string;

Powershell command in PHP

I'm trying to be clever and create a form to create a VM in ESX.
I have found the PowerCLI add-in for Poweshell and i have manually managed to create one.
This is my code but I'm struggling to make it work.
{
// Get the variables submitted by POST in order to pass them to the PowerShell script:
$name = $_POST["name"];
$diskmb = $_POST["diskmb"];
$MemoryMB = $_POST["MemoryMB"];
$NumCPU = $_POST["NumCPU"];
$connectesx = "Connect-VIServer -Server IPADDRESS -Protocol https -User 'USERNAME' -password 'PASSWORD'";
$createvm = "New-VM -Name $name -DiskMB $diskmb -MemoryMB $MemoryMB -NumCPU $NumCPU";
$psScriptPath = "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\esxdhb.psc1";
// Execute the PowerShell script, passing the parameters:
shell_exec("powershell -psc $psScriptPath -command $connectesx && $createvm");
echo ("$name Created Successfully.");

Categories