I'm trying to create a temporary tunnel via php so I can query a remote database.
The following code works through php-cli and as a shell command, but it doesn't seem to do anything when I run it trough apache:
$connect = "ssh -i remotekey -f -L 3315:localhost:3306 user#<remote IP> sleep 20 >> /tmp/logfile";
$out = shell_exec($connect);
A few notes:
remotekey is owned by wwwrun (the apache user under openSuse), perms are at 600
The logfile in /tmp gets created (and is blank)
safe_mode is off
Using PHP 5.3.17
After opening the site, I check the running processes for the background ssh and get nothing.
If I run it through php-cli, I see the tunnel running.
This has been driving me crazy. Any help would be greatly appreciated.
UPDATE
The issue was with the command silently failing as the apache user due to the remote server not being in the known_hosts file for the apache user.
Running the command with:
-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
circumvented this and the tunnel now works.
Thanks to the helpful folks who pointed me in the right direction in the comments.
Related
I'm running
nginx 1.17.4
php 7.4
arch linux 5.4.2
Trying to execute the following command to get my user MAC address from IP (this script will run on my lan server) and $_SERVER['REMOTE_ADDR'] does return a valid ip
shell_exec("sudo /usr/bin/nmap -n -sn ".$_SERVER['REMOTE_ADDR'])
But it returns null so I tried the following to get a more info
shell_exec("sudo /usr/bin/nmap -n -sn ".$_SERVER['REMOTE_ADDR'] ." 2>&1")
And got the following: sudo: effective uid is not 0, is sudo installed setuid root?
I don't understand why I get this error because I have added the following in my sudder file
http ALL=NOPASSWD: /usr/bin/nmap
I've modified my passwd to allow login from HTTP to try it in shell and it works but not when I run it in the browser.
Help please!
Thanks
since my server is arch linux I decide to go ask in that arch forum and found was able to fix my issue. so for anybody interested please view
https://bbs.archlinux.org/viewtopic.php?pid=1877560#p1877560
Basically for me it was changing the php-fpm service file to the following
/etc/systemd/system/multi-user.target.wants/php-fpm.service
Set NoNewPrivileges=false
comment CapabilityBoundingSet
I've been unable to run php scripts that I need to use to start and stop webcam services that run on the local machine with the scripts. I can find nothing in the logs to indicate why the script doesn't' work.
I confess to being severely handicapped regarding PHP, especially server-side scripting.
The environment is Debian Jesse running Nginx with all required SSH and PHP modules installed
I have added www-data to the sudoers file with:
www-data ALL=(ALL) NOPASSWD: /var/www/html/start_webcam.sh
Enabled the $PATH environment for www-data at:
/etc/php5/fpm/pool.d/www.conf
The shell script resides in the .../html directory and runs from the terminal with no issues.
This is the code for both the php and shell scripts:
start_webcam.php:
<?php
echo exec('sudo bash /var/www/html/aspirebox/start_webcam.sh 2>&1, $output');
print_r($output);
?>
The $output and print_r stuff is there because it was the last thing I tried based on a post I found out here somewhere.
start_webcam.sh
#!/bin/bash
service motion start
Thanks in advance to anyone out here that has a clue. After 2 days of wrestling with this, I am sure that I do not.
according to Passing Variables to shell_exec()? you should change your code like this:
<?php
$output = exec('/var/www/html/aspirebox/start_webcam.sh 2>&1 ');
print_r($output);
?>
and let your bash script execute as all (no need to sudo bash):
chmod a+x /var/www/html/aspirebox/start_webcam.sh
Thank you very much - that worked.
I worked through getting the path straight for the directory the shell script runs in, and the correct path to run "service".
All I have now is to figure out why I'm getting "Failed to start motion.service: Access denied"
I've given www-data permission to run the script without a password on sudoers, have to keep digging.
Thanks again!
I'm trying to execute with PHP a command (rsync) to copy folders and files from a remote server to a local folder.
This is the code I wrote in php. Command WORKS in SSH (local Terminal and remote with putty.exe), copying correctly the folders and the files.
But it doesn't work in PHP. What can I do? Do you know a better(secure/optimal) way to do this?
exec("echo superuserpassword | sudo -S sshpass -p 'sshremoteserverpassword' rsync -rvogp --chmod=ugo=rwX --chown=ftpuser:ftpuser -e ssh remoteserveruser#remoteserver.com:/path/files/folder /opt/lampp/htdocs/dowloadedfiles/", $output, $exit_code);
EDIT:
I had read this guide to create a link between my server and my local machine.
Now I can login with ssh in my remote machine without password.
I changed my command:
rsync -crahvP --chmod=ugo=rwX --chown=ftpuser:ftpuser remote.com:/path/to/remote/files /path/to/local/files/
This command works too in terminal, but when I send it with exec php command, it fails again, but I got another different error: 127.
As MarcoS told in his answer, I checked the error_log.
The messages are this:
ssh: relocation error: ssh: symbol EVP_des_cbc, version OPENSSL_1.0.0 not defined in file libcrypto.so.1.0.0 with link time reference
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: remote command not found (code 127) at io.c(226) [Receiver=3.1.1]
Well, after lot of try/error, I finished to cut the problem in the root:
I readed this guide (like the last one, but better explained) and I changed the php file that execute the rsync command to the remote server (where files are located) and run the rsync.php file there, and it worked perfectly.
To execute in the machine with the files (the files to copy and the rsync.php)
1.- ssh-keygen generates keys
ssh-keygen
Enter an empty passphrase and repeat empty passphrase again.
2.- ssh-copy-id copies public key to remote host
ssh-copy-id -i ~/.ssh/id_rsa.pub remoteserveraddressip(xxx.xxx.xxx.xxx)
The rsync.php file:
exec("rsync -crahvP /path/in/local/files/foldertocopy remoteuser#remoteserveraddress:/path/in/remote/destinationfolder/", $output, $exit_code);
After all of that, navigate to the rsync.php file and all must work. At least worked for me...
I suppose you are experiencing identity problems... :-)
On a cli, you are running the command as the logged-in user.
On PHP, you are running the command as the user your web server runs as (for example, apache often runs as www-data, or apache user...).
One possible solution I see (if the above is the problem real cause), is to add your user to web-server group...
I'd also suggest you to check the web-server error logs, to be sure about the real cause of the problem... :-)
Trying to trigger a shell script with POST request (handled by nginx). No success so far.
What I have:
exec('whoami'); // nginx
nginx:x:220:498:Nginx web server:/var/lib/nginx:/sbin/nologin
As we can see, nginx user has no shell. Could that be a problem? Next,
nginx ALL=(user) NOPASSWD: /path/to/script.sh
If I change nginx to my real user, I can run sudo -u user /path/to/script.sh without password prompt, but for some reason I'm still asked for password if I run su -s /bin/bash -c /path/to/script.sh user. Perhaps, latter invokation is somewhat more suitable for the nginx user, since he has nologin shell.
So whenever I run exec('sudo -u user /path/to/script.sh'); or run some wrapper script with that line in it through PHP - nothing happens. I can't even identify what doesn't work, since no errors are thrown.
Any help would be much appreciated.
Thanks to the #fejese and #triplee , I finally figured it out. I had to requiretty parameter in sudoers file, when that was changed to...
#Defaults requiretty
#Defaults !visiblepw
...it all went going!
We are calling a bash script from PHP that will do a simple git pull.
When we run this script from terminal using root or the apache user it executes fine.
However, when php excecutes the script using exec it outputs this error:
error: Failed to connect to XX.XX.XX.XX: Permission denied while accessing https://someuser#bitbucket.org/somecompany/testproject.git/info/refs
XX.XX.XX.XX is the IP address our http proxy resolves to
It also prints out the user and proxy config (as you will see in the bash script below)
PHP:
chdir('/var/www/scripts');
$cmd = './gitBranch.sh 2>&1';
exec($cmd,$currentOutput,$err);
print_r($currentOutput);
BASH:
#!/bin/bash
cd /var/www/gitManagedPackages/testproject
whoami #to verify it's the apache user
git config --get http.proxy #to verify it has the proper proxy setting
git pull
When running the script as the apache user [su -c ./gitBranch.sh -s /bin/sh apache]
apache
http://someproxy.somecompany.net:8181
Already up-to-date.
Why does it fail when running from PHP? It's executing as the apache user and has the correct proxy set.
As it turns out, httpd is not allowed to make outgoing connections by default. The outputted error is actually from git's use of curl.
running this fixed it:
setsebool -P httpd_can_network_connect 1