어떤 사용자 PHP가 실행 중인지 확인하는 방법은 무엇입니까?
PHP가 아무도 실행 중인지 감지해야합니다. 어떻게해야합니까?
"아무도"에 대한 다른 이름이 있습니까? "아파치"? 다른 사람?
사용 가능한 경우 현재 사용자 계정을 posix_geteuid
조사한 다음을 사용하여 사용자 이름을 가져올 수 posix_getpwuid
있습니다.
$username = posix_getpwuid(posix_geteuid())['name'];
그러나 안전 모드에서 실행하는 경우 (종종 exec가 비활성화 된 경우), PHP 프로세스가 기본 www-data
또는 apache
계정 이외의 다른 곳에서 실행되지 않을 가능성이 높습니다 .
<?php echo exec('whoami'); ?>
일종의 역방향이지만 exec / system없이 :
file_put_contents("testFile", "test");
$user = fileowner("testFile");
unlink("testFile");
파일을 생성하는 경우 소유자는 PHP 사용자가됩니다.
이는 tempnam()
임시 디렉토리에 임의의 파일을 생성하고 해당 파일의 이름을 반환하는와 같은 임시 파일 함수를 사용하여 실행할 수도 있습니다 . 권한과 같은 문제 open_basedir
또는 파일 쓰기를 방지하는 안전 모드 로 인해 문제가있는 경우 일반적으로 임시 디렉터리가 계속 허용됩니다.
자세한 내용은 유용하지만 Linux 시스템이고 php가 아파치에서 실행되고 있다고 가정하면 사용자 아파치가 실행되는 방식으로 실행됩니다.
확인하는 쉬운 방법 (다시 말하지만 환경과 같은 일부 유닉스를 가정)은 다음을 사용하여 php 파일을 만드는 것입니다.
<?php
print shell_exec( 'whoami' );
?>
그것은 당신에게 사용자를 줄 것입니다.
내 AWS 인스턴스의 apache
경우이 스크립트를 실행할 때 출력으로 표시됩니다.
다음과 같이 백틱을 사용해 볼 수 있습니다.
echo `whoami`;
나는 사용할 것이다 :
lsof -i
lsof -i | less
lsof -i | grep :http
이들 중 하나. ssh 명령 줄에 em을 입력하면 어떤 사용자가 어떤 서비스를 듣고 있는지 확인할 수 있습니다.
이 파일을 확인할 수도 있습니다.
more /etc/apache2/envvars
다음 줄을 찾으십시오.
export APACHE_RUN_USER=user-name
export APACHE_RUN_GROUP=group-name
envvars 파일 데이터를 필터링하려면 grep을 사용할 수 있습니다.
more /etc/apache2/envvars |grep APACHE_RUN_
exec('whoami')
이것을 할 것이다
<?php
exec('whoami');
?>
info.php 파일을 다음 디렉토리에 추가하십시오-기본 http / apache 디렉토리-일반적으로 / var / www / html
다음 내용으로
<?php
phpinfo();
?>
그런 다음 httpd / apache는 기본 html 디렉토리 http://enter.server.here/info.php 로 이동을 다시 시작합니다 .
전체 PHP 혈통을 전달할 것입니다!
쉘에서 바로 실행할 수 있습니다.
php -r "echo exec('whoami');"
내 설정에서 프로세스를 시작하기 전에 현재 프로세스에 폴더, 하위 폴더 및 파일을 생성 할 수있는 권한이 있는지 확인하고 할 수없는 경우 솔루션을 제안하고 싶습니다. stat(<file>)
권한이 실행중인 프로세스의 권한과 일치하는지 확인하기 위해 다양한 작업 을 실행 하고 싶었습니다 (나는 php-fpm을 사용하므로 풀에 따라 다릅니다).
Mario가 위에서 준 posix 기반 솔루션은 완벽 해 보이지만 posix 확장이 --disabled 인 것 같아서 위의 작업을 수행 할 수없고 결과를 whoami
별도의 셸 에서 실행중인 stat () 실행의 응답과 비교하고 싶습니다. 도움이되지 않습니다 (사용자 이름이 아닌 uid와 gid가 필요합니다).
그러나 나는 내가 할 수있는 유용한 힌트를 발견 stat(/proc/self)
하고 stat(/proc/self/attr)
및 UID를 확인하고 파일의 gid가.
다른 사람에게 도움이되는 희망
나는 보통 사용
<?php echo get_current_user(); ?>
I will be glad if it helped you
$_SERVER["USER"]
$_SERVER["USERNAME"]
Proposal
A tad late, but even though the following is a work-around, it solves the requirement as this works just fine:
<?
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 code-highlighting above is not accurate, please copy & paste in your favorite editor and view as PHP code, or save and test it yourself.
As you probably know, get_current_user()
returns the owner of the "current running script" - so if you did not "chown" a script on the server to the web-server-user it will most probably be "nobody", or if the developer-user exists on the same OS, it will rather display that username.
To work around this, we create a file with the current running process. If you just require()
this into the current running script, it will return the same as the parent-script as mentioned; so, we need to run it as a separate request to take effect.
Process-flow
In order to make this effective, consider running a design pattern that incorporates "runtime-mode", so when the server is in "development-mode or test-mode" then only it could run this function and save its output somewhere in an include, -or just plain text or database, or whichever.
Of course you can change some particulars of the code above as you wish to make it more dynamic, but the logic is as follows:
- define a unique reference to limit interference with other users
- define a local file-path for writing a temporary file
- define a public url/path to run this file in its own process
- write the temporary php file that outputs the script owner name
- get the output of this script by making a request to it
- delete the file as it is no longer needed - or leave it if you want
- return the output of the request as return-value of the function
$user = $_SERVER['REMOTE_USER'];
http://php.net/manual/en/reserved.variables.server.php
Authenticated user
<?php phpinfo(); ?>
save as info.php and
open info.php in your browser
ctrl+f then type any of these:
APACHE_RUN_USER
APACHE_RUN_GROUP
user/group
you can see the user and the group apache is running as.
You can use these commands :
<? system('whoami');?>
or
<? passthru('whoami');?>
or
<? print exec('whoami');?>
or
<? print shell_exec('whoami');?>
or
<? print get_current_user();?>
참고URL : https://stackoverflow.com/questions/7771586/how-to-check-what-user-php-is-running-as
'Programming' 카테고리의 다른 글
TableViewController, iOS에서 여분의 빈 셀을 제거하는 방법-Swift (0) | 2020.08.19 |
---|---|
Android 5.0의 Datepicker 대화 상자 색상 변경 (0) | 2020.08.19 |
문자열 변수를 변수 이름으로 사용 (0) | 2020.08.19 |
Android ImageView에서 이미지 제거 (0) | 2020.08.19 |
SQL 열에서 문자 인스턴스를 계산하는 방법 (1) | 2020.08.19 |