i try to execute a grep command inside a php shell_exec. And it works fine besides it fails when i have a underscore in the search word. I can not seem to figure out why this fails because of a underscore since, a grep command with a underscore in search word works in shell code below:
$output = shell_exec("grep -l -r '$search_word'");
The content in search_word variable is dynamic from database but the word that gives me trouble is base_64
Try like this:
$output = shell_exec("grep -l -r '$search_word' ./*");
Before PHP spawns a subprocess your command will be $search_word evaluated:
grep -l -r '....'
# So in $search_word is set to `john doe` it will become:
grep -l -r 'john doe'
How PHP behaves I'm not sure, it might be stalling waiting for the process to finish, it might have been closing stdin already.
Your above command will expect input from stdin because no file name is specified, breakdown:
grep [option]... [pattern] [file]...
-l will only print file name of the matched file
-r recursive search.
TLDR: You properly want to specify a file / directory to search in:
$output = shell_exec("grep -l -r '$search_word' .");
// Or maybe
$output = shell_exec("grep -l -r '${search}_word' ."); # will use $search variable as an input from PHP while _word is a string now.
Related
I am working in the Laravel framework where I am using shell_exec(). I am creating the string of command as in the following example.
$cmd = "php artisan serve:setup";
$resp = shell_exec($cmd);
How can I put a name to the above command? It shows in the system monitor as only "php"; I have multiple commands like this. I need to access the PID of that command and also the status of that command.
The command is running successfully, and in the system monitor, it shows the process name as "PHP".
I need process name with exec() or shell_exec() from PHP.
I have tried like this:
$cmd = "bash -c exec ServerCPP -a cd $path && php artisan serve:setup 2>&1";
$resp = shell_exec($cmd);
dd($resp);
It gives the error: "Could not open input file: artisan."
Your last attempt needs to be corrected:
cd first and quote the path in case the $path variable contains spaces
quotes around the command executed with bash -c
exec -a ServerCPP if you want to give the name ServerCPP to your process
$cmd = "cd '$path' && bash -c 'exec -a ServerCPP php artisan serve:setup' 2>&1";
$resp = shell_exec($cmd);
dd($resp);
The best idea would be to create a command.
php artisan make:command ServeSetup
Once the file is generated, set the name of your command in app/Console/Commands.
protected $signature = 'serve:setup';
Hope this helps.
I have a php script that creates a shell script file that finally executes as the www-data user, all of the commands are executed except for the last one which implies a binary file. If I run the command as root, it runs ok...
This is the last part of the script:
&& echo "Tokenizing the file........" >> Logs/table_of_contents.php \
&& perl ../common/Scripts/xmltokenize.pl --filename=xmlfiles/table_of_contents.xml >> Logs/table_of_contents.php \
&& perl ../common/Scripts/xmlrenumber.pl --filename=xmlfiles/table_of_contents.xml >> Logs/table_of_contents.php \
&& echo "Tagging the file........" >> Logs/table_of_contents.php \
# I have added this line to check if it helps but id doesn't
&& export HOME="/tmp/" \
# And this is the command that calls the binary file
&& perl tagfile.pl xmlfiles/table_of_contents.xml \
Here you have the content of the tagfile.pl
use File::Find;
$\ = "\n";
$fn = shift;
if ( $fn =~ /([^\/\.]+)\.xml/ ) { $fileid = $1; } else { exit;};
print $fileid;
$cmd = "perl tagfl2/makevrt.pl 'xmlfiles/$fileid.xml' > 'tagtmp/$fileid.vrt'";
print $cmd;
print `$cmd`;
#ALL OF THE PREVIOUS WORKS
#THIS IS THE ONE THAT GIVES PERMISSION ERRORS
# OF COURSE: "www-data:www-data tagtmp/" and "www-data:www-data $fileid.vrt = table_of_contents.vrt"
$cmd = "cut -f 1 tagtmp/'$fileid.vrt' | tagfl2/treetagger/bin/tree-tagger -no-unknown -token -lemma tagfl2/treetagger/lib/english.par > 'tagtmp/$fileid.tagged'";
print $cmd;
`$cmd`;
$cmd = "perl tagfl2/mrg.pl 'tagtmp/$fileid.vrt' 'tagtmp/$fileid.tagged' > 'tagtmp/$fileid.mrg'";
print $cmd;
`$cmd`;
$cmd = "perl tagfl2/tagxml.pl 'tagtmp/$fileid.mrg' 'xmlfiles/$fileid.xml'";
print $cmd;
`$cmd`;
Here is the error:
sh: 1: tagfl2/treetagger/bin/tree-tagger: Permission denied
Also, just in case:
chown -R www-data:www-data tagfl2/
chmod -R g+rwx tagfl2/
Try to define a full path to the script
$cmd = "perl /[full_path]/makevrt.pl 'xmlfiles/$fileid.xml' > 'tagtmp/$fileid.vrt'";
Why did you update user ownership?
Changing the group ownership should have been enough:
chgrp -R www-data tagfl2/
chmod -R g+rwX tagfl2/
And change the lowercase x by a greater one, to give access/execution permission, only if it is already the case for the user owner (no need to give otherwise).
You may then check the permission like this:
su -m -c 'ls -R tagfl2/' www-data
And see if you reproduce access issue; and then update permission accordingly.
Ok, all solved, one thing was giving the file system, actually the mounted unit, the exec attribution.
The second thing was moving treetagger directory to /usr/local/
Then, at /usr/local/bin/ I have created a soft link this way:
ln -s ../treetagger/bin/tree-tagger
Making the binary file globally executable. Actually, this last step was the ultimate solution.
Then at the tagfile.pl perl script, the line containing the tree-tagger command, I have changed it this way:
cut -f 1 'tagtmp/$fileid.vrt' | /usr/local/bin/tree-tagger -no-unknown -token -lemma tagfl2/treetagger/lib/english.par > 'tagtmp/$fileid.tagged'
I have a PHP script that starts a detached screen through SSH:
$ssh->exec("screen -m -d -S ".$user);
I now need to execute a command in that screen without being in that screen. I have the code that does that, which I have tested through a SSH client, but when I try to use it with the phpseclib exec command, it does not work. This is the code that works:
screen -S ".$user." -X stuff "cd minecraft/servers/".$user."/;sh start.sh $(printf '\r')"
And this is it in the PHP script:
$ssh->exec("screen -S ".$user." -X stuff \"cd minecraft/servers/".$user."/;sh start.sh $(printf '\r')\"");
I attempted to escape the extra double quotes in the code.
Is there anything I can do to make this work through PHP? Thanks
Hmmm...
create please two bash script, first: create screen with user parameter with name f.e. run_screen, second: tester for SSH client with user parameter with name f.e. run_test.
Run first script:
$ssh->exec('[full_path]/run_screen ' . $user);
and second:
$ssh->exec('[full_path]/run_test ' . $user);
bash syntax is here bash syntax
Sure that the user of server (f.e. Apache) has permissions to run scripts.
I want to have a grep that has one or more from each of the following groups.
"include,include_once,exec"
AND
"$_GET, $_POST"
So the file must have one or more from BOTH groups. (checking a whole directory of php files)
I'm trying to scan some PHP files for vulnerabilities but I really suck at grep :(
I suggest starting simple with commands like:
grep --color=auto -ER '\$_POST|\$_GET' .
or
grep --color=auto -ER 'include(_once)+|exec\(|require(_once)+' .
The above commands will color the found patterns in the output. Notice the regular expressions (enabled via -E) and the recursive search (via -R). Special characters of the regex are escaped via a backspace character (that is, the $ char and the opening bracket of exec)
To see the context of found occurrences use the -C num argument of grep to see num lines around the found line:
grep --color=auto -C 2 -ER '\$_POST|\$_GET' .
You can also use -A(fter) and -B(efore) if you want to output a different number of lines before or after the line with the found occurrence of your pattern. You may also use the -n argument to get the line number of a found occurrence. If you want to get more fancy you can combine the find command with xargs and grep or use multiple grep commands connected via pipes, but that's better explained in tutorials on the web.
grep -l will just list the files that match; you can then pipe the output of that into another grep:
grep -l -e "$_POST" -e "$_GET" * | grep -e "include" -e "include_once" -e "exec"
I am working with a form that allows me to upload files via a local folder and FTP.
So I want to move files over ftp (which already works)
Because of performance reasons I chose this process to run in the background so I use nfcftpput (linux)
In CLI the following command works perfectly:
ncftpput-b-u name -p password -P 1980 127.0.0.1 /upload/ /home/Downloads/upload.zip
(Knowing that the b-parameter triggers background process)
But if I run it via PHP it does not work (without the-b parameter it does)
PHP code:
$cmd = "ncftpput -b -u name -p password -P 1980 127.0.0.1 /upload/ /home/Downloads/upload.zip";
$return = exec($cmd);
Try one of the following:
1) Use the command $cmd = "ncftpput -b -u name -p password -P 1980 127.0.0.1 /upload/ /home/Downloads/upload.zip &";
(Notice the &)
2) Try php's proc_open function http://php.net/manual/en/function.proc-open.php
Try adding '&' at the end of command, this will fork it on linux level. Also try shell_exec(), if previous won't work.
Take a look at pcntl_fork. This user note has information how to correctly spawn a background process. Note that the extension that provides this function might not be activated in your PHP installation.
The best working solution for me is the following code:
function executeBackgroundProces($command) {
$command = $command . ' > /dev/null 2>&1 & echo $!';
exec ( $command, $op );
$pid = ( int ) $op [0];
if ($pid != "")
return $pid;
return false;
}
The command I run is: "ls bashfile"
The bash file contains commands like the upload and deletion of the original files separated by ;
This works fine by me