首頁  >  文章  >  後端開發  >  PHP的擴充Taint如何尋找網站的潛在安全漏洞(必看)

PHP的擴充Taint如何尋找網站的潛在安全漏洞(必看)

不言
不言原創
2018-08-17 10:58:461201瀏覽

這篇文章帶給大家的內容是關於PHP的擴充Taint如何尋找網站的潛在安全漏洞,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

一、背景

筆者從接觸電腦後就對網路安全一直比較感興趣,在做PHP開發後對WEB安全一直比較關注,2016時無意中發現Taint這個擴展,體驗之後發現確實好用;不過當時在查詢相關資料時候發現關注此擴展的人數並不多;最近因為換了台電腦,需要再次安裝了此擴展,發現這個擴展用的人還是比較少,於是筆者將安裝的過程與測試結果記錄下來,方便後續使用同時也讓更多開發者來了解taint

二、操作概要

  1. 原始碼下載與編譯

  2. 「擴充配置與安裝

# #功能檢驗與測試

三、原始碼下載與編譯

PHP的擴充Taint如何尋找網站的潛在安全漏洞(必看)#Taint擴充PHP本身不攜帶,在linux或mac系統當中筆者需要下載原始碼自己去編譯安裝

3.1 原始碼下載

筆者的開發環境是mac系統,所以需要去PHP的pecl擴充網站去下載原始碼,其中taint的位址為:

https://pecl.php.net/package/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
下載下來之後,筆者需要解壓縮,解壓縮指令參考如下:

tar -zxvf taint-2.0.4.tgz
解壓縮之後,進入目錄,參考指令如下:

cd taint-2.0.4

3.2 原始碼編譯

現在筆者需要編譯一下原始碼,在編譯之前可以使用phpze來偵測PHP的環境,參考指令如下:

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
開始編譯,並安裝

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/

四、設定與安裝

#在編譯擴充功能之後,筆者還需要把Taint放到指定位置,以及修改設定檔讓其生效

#4.1 設定taint

##筆者首先需要知道PHP的設定檔是多少,然後透過查看設定檔的擴充路徑,才能把so檔放到對應裡面去,查看設定檔位置指令如下:<pre class="brush:php;toolbar:false">php --ini</pre>回傳結果如下

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當中

#知道設定檔之後,筆者需要找到擴充資料夾位置,參考指令如下

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 =

# 4.2 安裝擴充功能

現在筆者需要把擴充檔案複製到,PHP的擴充檔案位置,參考指令如下

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/

複製完成之後,筆者需要編輯設定文件,將taint的配置項目複製進去

vim /usr/local/etc/php/7.1/php.ini

增加Tain的配置項目到php.ini檔案當中,參考配置如下:

[taint]
extension=taint.so
taint.enable=1
taint.error_level=E_WARNING

4.3 安裝結果驗證

#儲存設定檔並退出之後,則代表筆者的安裝已經完成,現在需要重新啟動php讓其生效,參考指令如下

brew services restart php@7.1

重啟完成之後,可以透過指令查看PHP目前的擴充有沒有Taint,參考指令如下:

php -i | grep taint
傳回結果如果出現了一下訊息,基本上已經安裝成功。

taint
taint support => enabled
taint.enable => On => On
taint.error_level => 2 => 2

五、功能檢驗與測試

完成上面的兩步驟操作之後,筆者安裝階段已經大功告成了,現在筆者需要用taint來檢驗效果,檢驗分為三個部分,先用taint作者的demo程式碼檢驗,之後再用滲透測試系統permeate來檢驗,最後以筆者平時所發展的程式碼進行測試。

5.1 demo檔案測試

用demo檔案測試的目的是檢驗筆者安裝的taint是否真的已經生效,並確認taint有沒有意義。

5.1.1 複製demo程式碼

在作者的GitHub上面有下面的這樣一份demo程式碼,筆者將其複製到web目錄,位置如下:

/Users/song/mycode/safe/permeate
demo程式碼內容如下,讀者實驗時可以將其拷貝:

<?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);

5.1.2 配置虛擬主機

###當程式碼檔案儲存之後,筆者需要在nginx設定檔中增加一個虛擬主機,用於瀏覽器存取此文件,參考配置如下:###
    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.3 瀏覽器存取#########接著筆者透過瀏覽器存取對應程式碼文件,URL位址如下:###
http://test.localhost/taintdemo.php?a=1
###瀏覽器存取頁面之後,筆者能在頁面中看到一些警告訊息,內容如下:###
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漏洞挖掘之debug導致的安全漏洞(Edusoho)

#PHPShop存在多個安全漏洞_PHP教學

################################################################ #####

以上是PHP的擴充Taint如何尋找網站的潛在安全漏洞(必看)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn