>  기사  >  백엔드 개발  >  PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)

PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)

不言
不言원래의
2018-08-17 10:58:461200검색

이 기사의 내용은 PHP 확장 프로그램인 Taint가 웹사이트에서 잠재적인 보안 취약점을 찾는 방법에 대한 것입니다. 이는 특정 참조 가치가 있으므로 도움이 될 수 있기를 바랍니다.

1. 배경

저자는 컴퓨터를 접하면서부터 네트워크 보안에 관심을 가지게 되었고, 2016년 우연히 웹 보안에 관심을 가지게 되었습니다. Taint 확장 프로그램을 사용해 본 결과 정말 사용하기 쉽다는 것을 알았지만, 관련 정보를 검색해 보니 최근에 이 확장 프로그램에 관심을 두는 사람이 많지 않아서 이 확장 프로그램을 설치해야 했습니다. 아직은 이 확장 프로그램을 사용하는 사람이 상대적으로 적다는 것을 알았기 때문에 이후 사용을 용이하게 하고 더 많은 개발자가 taint를 이해할 수 있도록 프로세스와 테스트 결과를 기록하겠습니다.

2. 작업 개요

  1. 소스코드 다운로드 및 컴파일

  2. 확장된 구성 및 설치

  3. 기능 검사 및 테스트

3. 소스코드 다운로드 및 컴파일

Taint 확장 PHP 자체는 Linux 또는 Linux에서 지원되지 않습니다. Mac 시스템에서는 작성자가 직접 소스 코드를 다운로드하여 컴파일하고 설치해야 합니다

3.1 소스 코드 다운로드

저자의 개발 환경은 Mac 시스템이므로 PHP의 pecl 확장 웹사이트에 접속해야 합니다. taint의 주소는

https://pecl.php.net/package/taint

확장 웹사이트 끝에 아래와 같이 다운로드 주소가 나열되어 있습니다.

PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)

작성자는 적합한 버전을 선택해야 합니다. 제 개발 환경은 PHP7.1을 사용하기 때문에 최신 버전을 선택했습니다.

https://pecl.php.net/get/taint-2.0.4.tgz

wget을 사용하여 소스 코드를 다운로드합니다.

wget https://pecl.php.net/get/taint-2.0.4.tgz

Download 다운로드 후 필요합니다. 압축 해제 명령 참조는 다음과 같습니다.

tar -zxvf taint-2.0.4.tgz

압축 해제 후 해당 디렉터리에 들어갑니다. 참조 명령은 다음과 같습니다.

cd taint-2.0.4

3.2 소스 코드 컴파일

이제 소스 코드를 컴파일해야 합니다. PHP 환경에서 phpze를 사용하여 이를 감지하면 참조 명령은 다음과 같습니다.

phpize

반환 결과는 다음과 같습니다

Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303

Makefile을 생성하여 다음 컴파일을 준비합니다

./configure

결과 반환

checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h

컴파일 시작 , and install

make && make install
(cd .libs && rm -f taint.la && ln -s ../taint.la taint.la)
/bin/sh /Users/song/taint-2.0.4/libtool --mode=install cp ./taint.la /Users/song/taint-2.0.4/modules
cp ./.libs/taint.so /Users/song/taint-2.0.4/modules/taint.so
cp ./.libs/taint.lai /Users/song/taint-2.0.4/modules/taint.la
----------------------------------------------------------------------
Libraries have been installed in:
   /Users/song/taint-2.0.4/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/Cellar/php71/7.1.14_25/lib/php/extensions/no-debug-non-zts-20160303/

4. 구성 및 설치

확장을 컴파일한 후 작성자도 지정된 위치에 Taint를 넣고 구성 파일을 수정하여 이를 적용해야 합니다.

4.1 taint 구성

The 작성자는 먼저 PHP 구성 파일이 무엇인지 알아야 하며 구성 파일을 확인해야 합니다. 경로를 확장해야만 해당 파일에 so 파일을 배치할 수 있습니다. 구성 파일 위치를 보는 명령은 다음과 같습니다.

php --ini

The 반환 결과는 다음과 같습니다

Configuration File (php.ini) Path: /usr/local/etc/php/7.1
Loaded Configuration File:         /usr/local/etc/php/7.1/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.1/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.1/conf.d/ext-opcache.ini

작성자는 php.ini가 /usr/local/etc/php/7.1/php.ini에 있는 것을 볼 수 있습니다. 구성 파일을 알고 나면 찾아야 합니다. 확장 폴더 위치는 다음과 같습니다/usr/local/etc/php/7.1/php.ini当中

知道配置文件之后,笔者需要找到扩展文件夹位置,参考命令如下

cat /usr/local/etc/php/7.1/php.ini | grep extension_dir

命令执行结果如下,笔者可以看出扩展文件夹位置是 /usr/local/lib/php/pecl/20160303

extension_dir = "/usr/local/lib/php/pecl/20160303"
; extension_dir = "ext"
; Be sure to appropriately set the extension_dir directive.
;sqlite3.extension_dir =
명령 실행 결과는 /usr /local/lib/php/pecl/20160303 임을 알 수 있습니다.

cp /usr/local/Cellar/php71/7.1.14_25/lib/php/extensions/no-debug-non-zts-20160303/taint.so /usr/local/lib/php/pecl/20160303/
4.2 확장 프로그램 설치

이제 확장 파일을 PHP 확장 파일 위치에 복사해야 합니다. 참조 명령은 다음과 같습니다

vim /usr/local/etc/php/7.1/php.ini
복사 완료 후 작성자가 해야 합니다. 구성 파일을 편집하고 taint의 구성 항목을 복사합니다

[taint]
extension=taint.so
taint.enable=1
taint.error_level=E_WARNING
Tain의 구성 항목을 php.ini 파일에 추가합니다. 참조 구성은 다음과 같습니다.

brew services restart php@7.1
4.3 설치 결과 확인

이후 구성 파일을 저장하고 종료하면 작성자의 설치가 완료되었음을 의미합니다. 이제 PHP를 다시 시작해야 적용됩니다. 참조 명령은 다음과 같습니다

php -i | grep taint
다시 시작이 완료된 후 명령을 사용할 수 있습니다. 현재 PHP 확장에 Taint가 있는지 확인합니다. 참조 명령은 다음과 같습니다.

taint
taint support => enabled
taint.enable => On => On
taint.error_level => 2 => 2
표시되면 결과를 반환합니다. 정보를 확인하면 기본적으로 설치가 성공한 것입니다.

/Users/song/mycode/safe/permeate
5. 기능 검사 및 테스트

위의 두 단계 작업을 완료한 후 작성자는 이제 taint를 사용하여 효과를 테스트해야 합니다. , taint 작성자의 데모 코드를 사용하여 Test하고, 침투 테스트 시스템인 permeate를 사용하여 테스트하고, 마지막으로 제가 평소 개발하는 코드로 테스트합니다.

5.1 데모 파일 테스트

데모 파일로 테스트하는 목적은 작성자가 설치한 Taint가 실제로 적용되었는지 확인하고, 해당 Taint가 의미가 있는지 확인하는 것입니다.

5.1.1 데모 코드 복사

저자의 GitHub에 다음 데모 코드가 있습니다. 작성자가 웹 디렉터리에 복사한 위치는 다음과 같습니다.

<?php $a = trim($_GET[&#39;a&#39;]);

$file_name = &#39;/tmp&#39; .  $a;
$output    = "Welcome, {$a} !!!";
$var       = "output";
$sql       = "Select *  from " . $a;
$sql      .= "ooxx";

echo $output;

print $$var;

include($file_name);

mysql_query($sql);
데모 코드의 내용은 다음과 같습니다. 독자는 실험할 때 이를 사용할 수 있습니다.

    server {
        listen       80;
        server_name  test.localhost;
        root  /Users/song/mycode/safe/permeate;
        location / {
            index index.html index.htm index.php; 
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
5.1.2 가상 호스트 구성

코드 파일이 저장된 후 작성자는 브라우저가 액세스할 수 있도록 nginx 구성 파일에 가상 호스트를 추가해야 합니다. 이 파일의 참조 구성은 다음과 같습니다.

http://test.localhost/taintdemo.php?a=1
5.1.3 브라우저 액세스

그러면 작성자는 브라우저를 통해 해당 코드 파일에 액세스합니다.

Warning: main() [echo]: Attempt to echo a string that might be tainted in /Users/song/mycode/work/test/taintdemo.php on line 10
Welcome, 1 !!!
Warning: main() [print]: Attempt to print a string that might be tainted in /Users/song/mycode/work/test/taintdemo.php on line 12
Welcome, 1 !!!
Warning: main() [include]: File path contains data that might be tainted in /Users/song/mycode/work/test/taintdemo.php on line 14

Warning: include(/tmp1): failed to open stream: No such file or directory in /Users/song/mycode/work/test/taintdemo.php on line 14

Warning: include(): Failed opening '/tmp1' for inclusion (include_path='.:/usr/local/Cellar/php@7.1/7.1.19/share/php@7.1/pear') in /Users/song/mycode/work/test/taintdemo.php on line 14

Fatal error: Uncaught Error: Call to undefined function mysql_query() in /Users/song/mycode/work/test/taintdemo.php:16 Stack trace: #0 {main} thrown in /Users/song/mycode/work/test/taintdemo.php on line 16
브라우저가 페이지에 액세스한 후의 URL 주소는 다음과 같습니다. , 작성자는 페이지에서 일부 경고 메시지를 볼 수 있으며 내용은 다음과 같습니다.🎜
Warning: main() [echo]: Attempt to echo a string that might be tainted in /Users/song/mycode/work/test/taintdemo.php on line 10
Welcome, 1 !!!
Warning: main() [print]: Attempt to print a string that might be tainted in /Users/song/mycode/work/test/taintdemo.php on line 12
Welcome, 1 !!!
Warning: main() [include]: File path contains data that might be tainted in /Users/song/mycode/work/test/taintdemo.php on line 14

Warning: include(/tmp1): failed to open stream: No such file or directory in /Users/song/mycode/work/test/taintdemo.php on line 14

Warning: include(): Failed opening '/tmp1' for inclusion (include_path='.:/usr/local/Cellar/php@7.1/7.1.19/share/php@7.1/pear') in /Users/song/mycode/work/test/taintdemo.php on line 14

Fatal error: Uncaught Error: Call to undefined function mysql_query() in /Users/song/mycode/work/test/taintdemo.php:16 Stack trace: #0 {main} thrown in /Users/song/mycode/work/test/taintdemo.php on line 16

从警告信息当中可以看出,笔者的taint已经生效,给出了很多警告提示,提示参数可能受到污染,因为参数并没有经过任何过滤;

5.1.4 参数过滤测试

如果不想让taint给出警告提示,可以将demo代码中的第二行代码更改或增加一下过滤规则,参考代码如下:

$a = htmlspecialchars($_GET['a']);

再次回到浏览器当中,刷新当前页面,可以看到返回的信息已经发生了变化,返回内容如下

Welcome, 1 !!!Welcome, 1 !!!
Warning: include(/tmp1): failed to open stream: No such file or directory in /Users/song/mycode/work/test/taintdemo.php on line 15

Warning: include(): Failed opening '/tmp1' for inclusion (include_path='.:/usr/local/Cellar/php@7.1/7.1.19/share/php@7.1/pear') in /Users/song/mycode/work/test/taintdemo.php on line 15

Fatal error: Uncaught Error: Call to undefined function mysql_query() in /Users/song/mycode/work/test/taintdemo.php:17 Stack trace: #0 {main} thrown in /Users/song/mycode/work/test/taintdemo.php on line 17

因为笔者在代码中增加了参数转义,此时再次刷新浏览器,会看到taint不再给发出警告提醒。

5.2 渗透测试系统验证

用demo系统验证taint扩展生效之后,现在笔者将用一个渗透测试系统来做一个实验,在这个系统中本身存在了很多安全问题,使用taint来找出这些问题,使用的渗透测试系统为 permeate渗透测试系统,地址如下

https://git.oschina.net/songboy/permeate

5.2.1 下载permeate

笔者通过git将其源码下载下来,参考命令如下

https://gitee.com/songboy/permeate.git

下载下来之后,同样创建一个虚拟主机,可以参考上面的nginx配置

5.2.2 导入数据库

因为这个系统会用到数据库,所以笔者下载之后需要新建数据库给permeate使用

PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)

新建完成数据库之后,笔者需要将一些数据表结构以及初始化数据导入到数据库当中,在使用git下载下来之后,在其跟目录有一个doc的文件夹,笔者打开它之后,能看到有一个sql文件,如下图所示

PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)

打开此文件并将其里面的内容复制,将复制的内容到管理数据库的Navicat Premium当中,然后执行这些SQL语句,如下图所示

PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)

5.2.3 修改配置文件

导入数据库完成之后,笔者修改数据库配置文件,让permeate能够连接次数据库,配置文件在根目录 conf/dbconfig.php,里面的配置代码如下,将其地址账户以及密码和数据库名称一一对应填写

<?php     !defined(&#39;DB_HOST&#39;) && define(&#39;DB_HOST&#39;,&#39;127.0.0.1&#39;);
    !defined(&#39;DB_USER&#39;) && define(&#39;DB_USER&#39;,&#39;root&#39;);
    !defined(&#39;DB_PASS&#39;) && define(&#39;DB_PASS&#39;,&#39;root&#39;);
    !defined(&#39;DB_NAME&#39;) && define(&#39;DB_NAME&#39;,&#39;permeate&#39;);
    !defined(&#39;DB_CHARSET&#39;) && define(&#39;DB_CHARSET&#39;,&#39;utf8&#39;);
    $sex=array(&#39;保密&#39;,&#39;男&#39;,&#39;女&#39;);
    $edu=array(&#39;保密&#39;,&#39;小学&#39;,&#39;初中&#39;,&#39;高中/中专&#39;,&#39;大专&#39;,&#39;本科&#39;,&#39;研究生&#39;,&#39;博士&#39;,&#39;博士后&#39;);
    $admins=array(&#39;普通用户&#39;,&#39;管理员&#39;)

5.2.4 验证安装结果

设置好数据库之后,笔者安装permeate便已经完成了,此时打开首页,看到的界面应该如下图所示:
PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)
如果在首页当中没有看到板块以及分区等信息,很有可能是数据库没有连接成功或者数据库没有正确导入数据所致。

5.2.5 挖掘漏洞

下面开始进行测试,笔者点击第一个板块SQL注入,并点击列表下发的下一页按钮,此时看到的页面如下图所示:
PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)
在这个板块列表页中没看到任何问题,但是实际上taint已经给笔者发出了警告提醒。

笔者可以通过查看源代码时候来看到这些问题,如下图所示,taint提示在代码文件 /Users/song/mycode/safe/permeate/core/common.php的50行,存在参数被污染的情况。

PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)

5.2.5 漏洞分析

笔者找到对应的代码位置,发现代码内容如下:

function includeAction($model, $action)
{
    //判断控制器是否存在
    $filePath = "./action/$model.php";
    if (is_readable($filePath)) {
        require_once $filePath;
        $class = new $model;
        if (is_callable(array($class, $action))) {
            $class->$action();
            return true;
        }
    }

在代码中笔者看到有一个require_once函数加载了文件,里面的参数使用了变量 $model$action ,通过最终变量来源,在代码文件/Users/song/mycode/safe/permeate/home/router.php发现这两个参数确实没有经过过滤,如下代码所示:

<?php require_once "/core/common.php";
$model = !empty($_GET[&#39;m&#39;]) ? $_GET[&#39;m&#39;] : &#39;index&#39;;
$action = !empty($_GET[&#39;a&#39;]) ? $_GET[&#39;a&#39;] : &#39;index&#39;;

includeAction("$model","$action");

最后需要提醒大家,Taint在开发环境安装即可,不要安装到生产环境当中,否则可能会把网站的安全问题直接暴露给攻击者

相关推荐:

PHP网站常见安全漏洞及相应防范措施总结,安全漏洞防范措施

새로운 PHP 취약점 마이닝의 디버그로 인해 발생하는 보안 취약점(Edusoho)

PHPShop에는 여러 보안 취약점이 있습니다_PHP 튜토리얼 #🎜🎜 #

위 내용은 PHP 확장 Taint가 웹사이트의 잠재적인 보안 취약점을 찾는 방법(필독)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.