How to check which custom php works like?

I need to determine if php works like no one else. How should I do it?

Are there any other names for "nobody"? "Apache"? Any other?

+89
php apache
Oct. 14 '11 at 17:56
source share
16 answers

If available, you can examine the current user account using posix_geteuid and then get the username using posix_getpwuid .

 $username = posix_getpwuid(posix_geteuid())['name']; 

However, if you work in safe mode (which often happens when exec is disabled), it is unlikely that your PHP process runs under anything except the default www-data or apache account.

+63
Oct 14 '11 at 18:03
source share

<?php echo exec('whoami'); ?>

+172
Oct 14 '11 at 17:58
source share

Kind of return path, but without exec / system:

 file_put_contents("testFile", "test"); $user = fileowner("testFile"); unlink("testFile"); 

If you create the file, the owner will be a PHP user.

It can also be run with any of the temporary file functions, such as tempnam() , which creates a random file in the temporary directory and returns the name of this file. If there is a problem due to something like permissions, open_basedir or safe mode that prevents the file from being written, usually the temp directory will still be allowed.

+62
Oct 14 2018-11-18T00:
source share

More details would be useful, but assuming it is a Linux system and assuming php runs under apache, it will work just like any custom apache executed as.

An easy way to check (again, assuming some kind of environment similar to unix) is to create a php file using

 <?php print shell_exec( 'whoami' ); ?> 

which will provide you with the user.

For my AWS instance, I get apache as output when I run this script.

+20
Oct. 14 '11 at 18:06
source share

You can try using back ticks:

 echo `whoami`; 
+14
Oct. 14 2018-11-18T00:
source share

I would use:

 lsof -i lsof -i | less lsof -i | grep :http 

any of them. You can enter em in your ssh command line and you will see which user is listening to which service.

you can also go and check this file:

 more /etc/apache2/envvars 

and find the following lines:

 export APACHE_RUN_USER=user-name export APACHE_RUN_GROUP=group-name 

to filter envvars file data you can use grep:

  more /etc/apache2/envvars |grep APACHE_RUN_ 
+8
Feb 08 '14 at 20:52
source share

exec('whoami') will do it

 <?php exec('whoami'); ?> 
+6
Oct 14 '11 at 17:58
source share

add the info.php file to the following directory - your default http / apache directory - usually / var / www / html

with the following content

 <?php phpinfo(); ?> 

Then restarting httpd / apache go to your default html directory http://enter.server.here/info.php

will provide the entire php pedigree!

+2
Jun 10 '17 at 2:06
source share

In my setup, I want to check if the current process is allowed to create folders, subfolders and files before starting the process, and offer a solution if it looks like I can't. I wanted to run stat(<file>) for different purposes to ensure that the permissions for the running process are consistent (I use php-fpm, so it depends on the pool).
The posix-based solution described by Mario above seems perfect, but it seems that the posix extension is --disabled, so I couldn’t do the above, and since I want to compare the results with the response from stat () running whoami in a separate shell also not useful (I need uid and gid, not username).

However, I found a useful hint, I could stat(/proc/self) and stat(/proc/self/attr) and see the uid and gid of the file.

Hope someone else helps

+1
Apr 22 '14 at 17:04
source share

You can run directly from the shell:

 php -r "echo exec('whoami');" 
+1
Feb 09 '19 at 3:35
source share

I usually use

 <?php echo get_current_user(); ?> 

I will be glad if this helps you.

0
Jun 03 '16 at 11:53 on
source share
 $_SERVER["USER"] $_SERVER["USERNAME"] 
0
11 Oct '16 at 14:03
source share

Sentence

A bit late, but although the following is workaround, it solves the requirement as it works very well:

 <? function get_sys_usr() { $unique_name = uniqid(); // not-so-unique id $native_path = "./temp/$unique_name.php"; $public_path = "http://example.com/temp/$unique_name.php"; $php_content = "<? echo get_current_user(); ?>"; $process_usr = "apache"; // fall-back if (is_readable("./temp") && is_writable("./temp")) { file_put_contents($native_path,$php_content); $process_usr = trim(file_get_contents($public_path)); unlink($native_path); } return $process_usr; } echo get_sys_usr(); // www-data ?> 


Description

The selection of the code above is inaccurate, copy and paste it into your favorite editor and view it as PHP code, or save and test it yourself.

As you probably know, get_current_user() returns the owner of the “current script run”, so if you are not “chown” a script on the server for the web server user, it will most likely be “nobody”, or if the developer the user exists in the same OS, he will rather display this username.

To get around this, we create a file with the current start of the process. If you just require() in the current script run, it will return the same as parent-script, as indicated; therefore, we need to run it as a separate request for entry into force.

Flow process

To make this effective, consider launching a design pattern that includes a “run mode”, so when the server is in “development or test mode”, then only it can run this function and save its output somewhere in include, or just text or a database, or depending on which one.

Of course, you can change some of the features of the code above, since you want to make it more dynamic, but the logic looks like this:

  • define a unique link to limit interference to other users
  • determine the local file path for writing a temporary file
  • define a common url / path to run this file in your own process
  • write a temporary php file that displays the name of the owner of the script
  • get the result of this script by making a request to it
  • delete the file because it is no longer needed - or leave it if you want
  • returns the result of the query as the return value of a function
0
May 26 '17 at 10:37
source share
 $user = $_SERVER['REMOTE_USER']; 

http://php.net/manual/en/reserved.variables.server.php

Authenticated user

0
04 Oct '18 at 0:28
source share
 <?php phpinfo(); ?> 

save as info.php and

open info.php in your browser

Ctrl + F then enter any of them:

 APACHE_RUN_USER APACHE_RUN_GROUP user/group 

You can see the user and the Apache group works as.

0
Nov 13 '18 at 0:22
source share

You can use these commands:

 <? system('whoami');?> 

or

 <? passthru('whoami');?> 

or

 <? print exec('whoami');?> 

or

 <? print shell_exec('whoami');?> 

or

 <? print get_current_user();?> 
0
May 23 '19 at
source share



All Articles