I am trying to write a php script to add users to an LDAP. To this end I have a shell script, aptly titled "addldapuser.sh", that will take in user/screenname/password combinations and add them to the LDAP. This script works properly. We would like users to be able to enter their information on a web form, which will then invoke said script and add the user to the LDAP properly. The addldapuser script currently needs to be run as root, and while I am aware of the security concerns, they can be dealt with later. I have tried everything I can think of or find, gave everything every permission I could think of, gave everything I could root, mucked around with Apache for awhile, and now I am out of ideas.
$scrName = $_POST['screenname'];
$usrName = $_POST['username'];
$pass = $_POST['password'];
if (isset($_POST['submit']))
{
$out = nl2br(shell_exec("sudo /EasyLDAP/old_scripts/addldapuser.sh " . $usrName . " " . $scrName . " " . $pass));
}
Once again, I know this is just a terrible, terrible idea that is guaranteed to end in hackers destroying us all forever, but my boss wants me to at least make it function this way.
I do know that we should at least sanitize the input, and that will be taken care of in due time.
I recently published a project that allows PHP to obtain and interact with a real Bash shell, you can easily get a shell with root. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$strCmd = "/EasyLDAP/old_scripts/addldapuser.sh " . $usrName . " " . $scrName . " " . $pass;
$return1 = $shell->exeCmd($strCmd);
echo $return1;// return from your script
I would recommend creation of a daemon, which will be running as a root and will communicate with a script running at a user level.
You can do very simple python rpc server (e.g. XML-RPC which is built-in) and a client as a glue, run one of them as a server and root and the other one make into a client script.
The php code would then execute the python script with the required parameters, which then can communicate with the python server script.
As a benefit, you get potential security if you do the server part well. I chose python as a language which has most of the functionality built-in and is very simple to use.
Examples:
server - http://docs.python.org/library/simplexmlrpcserver.html#simplexmlrpcserver-example
client - http://docs.python.org/library/xmlrpclib.html#example-of-client-usage
Alternatively, if you insist on using php, you can run the server process as a php daemon and connect to it via some similar RPC means.
Related
I was given a lot of legacy code using Symfony2, which was running on Linux, but I need to start it on Windows. Almost everything is working (XAMPP), but I have problem with one thing.
During the user registration, sending mails was taking too long to load confirmation page (depending on the options there could be a couple of emails), so for every email there's a record added in DB, which contains just a command, like "php /path/to/app/console product:send_some_mail address#host" etc.
Then there's a Command which takes all those not sent emails and sends it one by one. That command was previously called like this:
$proc = new Process('php /var/www/product/app/console product:send_all_mails ' . $mailAddress .' --env='. $env . ' ');
That's quite obvious the path was not exacly right on Windows, so I tried to make it more universal. I'll paste the whole test code:
$proc = new Process('php ' . realpath($this->get('kernel')->getRootDir() . '/console') . ' product:send_all_mails ' . $mailAddress .' --env='. $env . ' ');
$proc->setEnhanceWindowsCompatibility(false); // this doesn't change anything...
$proc->start();
while($proc->isRunning()); // stupid debug
file_put_contents("MailProcessResult.txt", $proc->getCommandLine() . ' :: ' . $proc->getExitCodeText());
Strangely, everything I see in the pseudo-log file is:
php C:\gitrepos\product\app\console product:send_all_mails mymail#host.com --env=my_env :: General error
I have no idea, what's going on. I double-checked everything I could think of:
php.exe is in %PATH%
the command works, if is called from cmd, even with Windows Compatibility (cmd /V:ON /E:ON /C "(my_commandline)")
sendmail is configured correctly, other mails are sent without problems
I removed ConEmu which was taking over cmd
yes, on Linux it works
Earlier I tried to run the product:send_all_mails as command, but it wasn't running in background. That's quite obvious php has some problems executing php :)
What else could I try? I just need to run a batch of different Commands with different arguments from one Command (or Process) in background.
Change this Command to Event .
This way you wont need Process() - i believe this is the problem - too much complication (creating process to call commands from system ) .
Connect Event to doctrine to postPersist - that will make event fire up every time the new record was created .
In event check if added fing to DB is User Entity, and if yes send email from event
Thanks for taking your time to read about my problem, so lets get straight into it.
My ultimate (end) goal is to be able to have a web base PHP app which lets users modify active directory for users inside a network.
I have found out commands that need to be ran via Powershell to change user passwords inside an active directory.
Now here is my current setup:
There is one main server where IIS is installed and the web application sits at.
There will be several computers connected to the network which navigate to the website and execute these commands
I have tried adding the code:
$query = shell_exec('Import-Module ActiveDirectory');
$query = shell_exec('$newpwd = ConvertTo-SecureString -String "'.$safe_password.'" -AsPlainText –Force');
$query = shell_exec('Set-ADAccountPassword "'.$safe_username.'" -NewPassword $newpwd –Reset');
Now here are my questions:
1) Once a computer runs that page and has those commands executed, are the commands going to be executed in the main server? where powershell is installed and permissions are granted. If not, my whole app wont work, are there any other solutions?
2) those commands are all powershell commands not cmd.exe commands. So does shell_exec() even run those commands in powershell, or just in cmd.exe. If it only runs in cmd then this wont work, how can i make it run via powershell?
I would appreciate if you could answer and help me out here, thanks alot.
Setup the Parameters in your PowerShell like this:
Param([string]$username, [string]$password)
Then call your script like this:
shell_exec("powershell.exe " . $psscriptpath . " -username \"" . $username . "\" -password \"" . $password . "\"");
I have been searching for a while in how to successfully exec a Python script through PHP, just for a test thats all.
I've never actually worked with Python and a while since I programmed in PHP but looked for a simple code to send data from PHP to Python and then in .py send back data to PHP.
I have tried exec in PHP, with and without json enc/dec without any success.
These are the commands that have I tried in PHP:
* $result = shell_exec('pythontest.py' . escapeshellarg(json_encode($data)));
* exec("python pythontest.py", $resultData);
var_dump($resultData);
with no success.
In pythontest.py: a print to send back data.
Tried with full paths of both Python and pythontest.py (PHP).
enc/dec with json works in PHP file so nothing seems to be wrong in the PHP code still I can't run the script!
As I said, I haven't worked with Python before so I wonder if there is more needed than just writing the code in the script in order to make it work?
I have seen many posts about this and tried their code that "would work for them" but not for me^^
Additional info: PHP through WAMP, .py in same map. It is supposed to be web based, if that makes it difference.
Check to make sure that the directory containing python.exe is in the PATH of your PHP script (via getenv('PATH')).
Try running python c:\path\to\pythontest.py directly via a Windows command prompt. Does that work? If not, debug PATHs and issues from there, then try again in PHP.
If your WAMP stack runs PHP with limited permissions, check to make sure that user has "Read and execute" type permissions on the python.exe file, as well as the entire directory which contains it, recursively. This might be overkill, but recursively setting perms for that user on the whole python distribution directory rules out permissions-related problems.
Try qualifying the python executable's name with .exe, e.g. c:\python27\python.exe pythontest.py (you might have tried that anyway, since you say you tried with "full paths", but I'm throwing it out there to make sure you included the .exe).
Try replacing the contents of pythontest.py with something extremely simple (e.g. print 'hello') and check whether or not behavior changes.
Also, please post the contents (or at least first lines) of pythontest.py.
I have now tried changing the path, as python.exe was not in the same directory.
My end result on this was
$output = getenv('PATH');
echo "Real Path: " . $output . "<br>";
$ret = apache_getenv("PATH");
echo "Apache Path: " . $ret . "<br>";
apache_setenv("PATH", "C:/Python33");
$ret = apache_getenv("PATH");
echo "Apache new path: " . $ret . "<br>";
$output = getenv('PATH');
echo "Real path, hopefully changed: " . $output . "<br>";
this was just to see if i could change the path, since i have never done that before.
i have now only
import sys, json
print ("hello")
in my python file and tried to run it through cmd, and it worked.
but when i try run it with php, it fails once again.
i have also looked over the permissions:
admin, user have permission to read and execute, i even added "Everyone" with full permission on folder and sub-folder/files just to see if that made any difference.
something i have missed or did i do something wrong with permission, python code or paths ?
My friend has some sort of admin panel where he displays information from several servers. He is using cPanel XML api to display several things for example the server load average, the code looks something like this:
include("xmlapi.php");
$conf = simplexml_load_file("servers.xml");
foreach ( $conf->server as $server ) {
$xmlapi = new xmlapi( $server->ip );
$xmlapi->set_debug(1);
$xmlapi->hash_auth( $server->user, $server->accesshash);
$loadavg = $xmlapi->loadavg();
print $server->ip . " - " . $loadavg->one . " - " . $loadavg->five . " - " . $loadavg->fifteen;
}
I've been reading the wiki but the closer thing I found is retrieving an account disk usage. But I need the info that comes from the "df" command ran in ssh as root. What would be the easier and safest way to do this? Since i need to get the df command output from several servers and not from the one running the code.
Why do you need to run df as "root"? I can certainly run it as myself on my Linux machine?
(Yes, that's a question, not an answer)
Assuming you have full access to the machine, you could probably set up a useraccount "df", that is listed in sudoers with !authenticate attribute (so it can sudo without password), and then ssh df#${machine} sudo df
You can also restrict sudo so that the df" user can only do df, and not, for example rm -rf / or adduser blah
And of course set up your ssh to use a public/private key challeng rather than using your php code to have a password in the source - if nothing else because it makes it more flexible, and you don't have to update the source if you decide to change the password.
I'm building a web control that will let our junior IT staff manage firmware on our LifeSize phones. Currently we do this by uploading the new firmware onto a central server, then running this command for each phone we want to upgrade
cat new_firmware.cramfs | ssh -T cli#1.1.1.1 "upgrade all"
This asks me for the password, then uploads the firmware. Works like a champ, but it takes someone comfortable with CLI tools, SSH access to this server, and patience to look up all the IPs of all the phones.
It looks like we're stuck with a password logon, testing with certificates has been disastrous. The device being acted on is not a full-fledged computer, it's a telephone running a tiny, proprietary embedded OS.
I'm working on a PHP script that can iterate over all the phones, but basically duplicate that function. This is what I have so far:
<?php
$firmware_filename = "new_firmware.cramfs";
$firmware_stream = fopen($firmware_filename,"rb");
$ssh_connection = ssh2_connect("1.1.1.1", 22);
ssh2_auth_password($ssh_connection, "cli", "password");
$ssh_stream = ssh2_exec($ssh_connection,'upgrade all');
$written = stream_copy_to_stream($firmware_stream,$ssh_stream,-1);
if($written != filesize($full_filename)){
echo "The file is " . filesize($firmware_filename) . " bytes, I only wrote $written" . PHP_EOL;
}else{
echo "All Good" . PHP_EOL;
}
?>
But this always returns
The file is 26988590 bytes, I only wrote 8192
And the upgrade does not proceed correctly.
Well you could simply call
system('cat new_firmware.cramfs | ssh -T cli#1.1.1.1 "upgrade all"');
and then replace using your vars:
system('cat ' . $firmware . ' | ssh -T ' . $username . '#' . $host . ' "upgrade all"');
is this a solution for you?
you can automate the ssh-access by placing the certificate-file into .ssh-directory. Read about SSH login without password.
regards
There are several things you could try:
Copy the file first, then run the
command on the now-local file.
Assuming that you're filling an 8k buffer, try writing in a loop until you've successfully written the whole file
Take the easy way out, and just set up ssh keys so you don't need to enter a password, and exec shell commands directly from your script