PHP開發(fā)中防御SSRF

參考phith0n的談一談如何在Python開發(fā)中拒絕SSRF漏洞,用PHP寫一個檢測是否是內(nèi)網(wǎng)URL的函數(shù)。

需要注意的是,我寫的這個檢測URL的函數(shù)并沒有考慮到客戶端允許重定向跟蹤的情況。如果允許重定向跟蹤則需要檢查每個30x響應(yīng)包的location字段。

<?php
/*
*  判斷是否合法的URL,合法則返回true;
*  格式不是http/https協(xié)議,或是內(nèi)網(wǎng)IP,則視為不合法
*
*  注意:判斷是否是內(nèi)網(wǎng)url的時候并沒有檢測30X跳轉(zhuǎn)的location響應(yīng)頭部,
*  使用此方法時,要求curl或其他工具設(shè)置不允許重定向跟蹤
*
*  @author Ovie
*  @param string $url
*  @return bool
*/
function is_allowed_URL($url){
    //可用來防止HTTP頭注入
    if (!$url || !filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED & FILTER_FLAG_HOST_REQUIRED & FILTER_FLAG_QUERY_REQUIRED)){
        return false;
    }
  
    //僅允許http或https協(xié)議
    if(!preg_match('/^https?:\/\/.*$/', $url)){
        return false;
    }
    
    $host = parse_url($url, PHP_URL_HOST);
    if(!$host){
        return false;
    }
    
    $ip = gethostbyname($host);
    $ip = ip2long($ip);
    if($ip === false){
        return false;
    }
    
    $is_inner_ipaddress = ip2long('127.0.0.0') >> 24 == $ip >> 24 or 
        ip2long('10.0.0.0') >> 24 == $ip >> 24 or 
        ip2long('172.16.0.0') >> 20 == $ip >> 20 or 
        ip2long('192.168.0.0') >> 16 == $ip >> 16 ;
    if($is_inner_ipaddress){
        return false;
    }
    
    return true;
}

if(isset($_GET['url'])){
    $url = $_GET['url'];
    var_dump(is_allowed_URL($url));
}
?>

說下上面的用到的兩個PHP函數(shù):parse_url()gethostbyname()。

  1. PHP解析URL的host:

    parse_url()函數(shù)不是用來驗證給定 URL 的合法性的,只是將其分解為幾部分。不完整的 URL 也被接受,parse_url()會嘗試盡量正確地將其解析。

    參考:http://php.net/manual/zh/function.parse-url.php

  2. PHP解析URL的IP:

    gethostbyname($hostname)函數(shù)會返回IPv4地址,解析失敗的話就會返回沒被修改過的$hostname。

    參考:http://php.net/manual/zh/function.gethostbyname.php


(以下內(nèi)容于2021年1月25日添加)

之前的代碼早有繞過方式,且有個大的錯誤,就是在給$is_inner_ipaddress變量賦值的時候用了or,而不是||,導(dǎo)致192.168.0.1這些內(nèi)網(wǎng)ip其實是沒被檢測到的,原因是or的優(yōu)先級比賦值符=的小,||的優(yōu)先級才比=的大(參考:https://stackoverflow.com/questions/5998309/logical-operators-or-or/5998330#5998330)。

<?php
/*
*  判斷是否合法的URL,合法則返回true
*  不合法的情況:
*  1. 包含非ASCII碼字符
*  2. 不是http或https協(xié)議
*  3. ipv6地址(不支持ipv6地址,將判斷為不合法)
*  4. URL的user、pass、host,fragment段包含@、\或其它字符
*  5. 請求地址為內(nèi)網(wǎng)IP:192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,127.0.0.1/8
*  6. 請求地址為169.254.169.254,100.100.100.200,192.0.0.192這幾個常見云主機metadata地址ip
*
*
*  注意,該方法未考慮到:
*  1. 30X跳轉(zhuǎn)的location響應(yīng)頭部是否為合法URL
*  2. DNS Rebinding攻擊
*
*  @author Ovie
*  @param string $url
*  @return bool
*/
function is_allowed_URL($url)
{
    // Only allow ASCII URL (so will forbid CRLF and other Unicode char)
    if (!$url || !filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED & FILTER_FLAG_HOST_REQUIRED & FILTER_FLAG_QUERY_REQUIRED)) {
        return false;
    }

    // Only allow http and https scheme
    if (!preg_match('/^https?:\/\/.*$/', $url)) {
        return false;
    }

    $parts = parse_url($url);
    foreach (array("user", "pass", "host", "fragment") as $key) {
        if (isset($parts[$key]) && preg_match("~[:/?#\\\\@]~", $parts[$key])) {
            return false;
        }
    }

    // gethostbyname do not support ipv6
    $ip = gethostbyname($parts['host']);
    $ip = ip2long($ip);
    if ($ip === false) {
        return false;
    }


    $is_inner_ipaddress = ip2long('127.0.0.0') >> 24 == $ip >> 24 ||
    ip2long('10.0.0.0') >> 24 == $ip >> 24 ||
    ip2long('172.16.0.0') >> 20 == $ip >> 20 ||
    ip2long('192.168.0.0') >> 16 == $ip >> 16 ||
    $ip == 0 ||
    ip2long('169.254.169.254') == $ip ||
    ip2long('192.0.0.192') == $ip ||
    ip2long('100.100.100.200') == $ip;
    if ($is_inner_ipaddress) {
        return false;
    }

    return true;
}

/*
if(isset($_GET['url'])){
    $url = $_GET['url'];
    var_dump(is_allowed_URL($url));
}
*/

function test($url)
{
    $res = is_allowed_URL($url);
    echo $url . " : " . ($res ? $res : "false") . "\n";
}



test("https://127.0.0.1:8080/ppppp");
test("https://169.254.169.254:8080/ppppp");
test("https://192.0.0.192:8080/ppppp");
test("https://100.100.100.200:8080/ppppp");
test("https://127.0.0.1:8080\@baidu.com/ppppp");
test("https://baidu.com#@127.0.0.1:8080/ppppp");
test("https://127.1:8080/ppppp");
test("https://127.0.0.1.:8080/ppppp");
test("https://2130706433:8080/ppppp");
test("https://0:8080/ppppp");
test("https://[::]:8080/ppppp");
test("https://localhost:8080/ppppp");
test("https://0:0:0:0:0:ffff:127.0.0.1:8080/ppppp");
test("https://127.0000.000000.000001:8080/ppppp");
test("https://127.0.0.1:8080#@baidu.com/ppppp");
test("http://[::1]/");
test("https://0.0.0.0:8080/ppppp");
test("https://127.2.0.2:8080/ppppp");
test("https://0x7f.0x0.0x0.0x1:8080/ppppp");
test("https://baidu.com.127.0.0.1.nip.io:8080/ppppp");
test("https://baidu.com@127.0.0.1:8080@baidu.com/ppppp");
test("https://127.0.0.1:8080\.baidu.com/ppppp");
test("https://127.0.0.1:8080:443/ppppp");


?>

php 7.2下運行的結(jié)果:

https://127.0.0.1:8080/ppppp : false
https://169.254.169.254:8080/ppppp : false
https://192.0.0.192:8080/ppppp : false
https://100.100.100.200:8080/ppppp : false
https://127.0.0.1:8080\@baidu.com/ppppp : false
https://baidu.com#@127.0.0.1:8080/ppppp : false
https://127.1:8080/ppppp : false
https://127.0.0.1.:8080/ppppp : false
https://2130706433:8080/ppppp : false
https://0:8080/ppppp : false
https://[::]:8080/ppppp : false
https://localhost:8080/ppppp : false
https://0:0:0:0:0:ffff:127.0.0.1:8080/ppppp : false
https://127.0000.000000.000001:8080/ppppp : false
https://127.0.0.1:8080#@baidu.com/ppppp : false
http://[::1]/ : false
https://0.0.0.0:8080/ppppp : false
https://127.2.0.2:8080/ppppp : false
https://0x7f.0x0.0x0.0x1:8080/ppppp : false
https://baidu.com.127.0.0.1.nip.io:8080/ppppp : false
https://baidu.com@127.0.0.1:8080@baidu.com/ppppp : false
https://127.0.0.1:8080\.baidu.com/ppppp : false
https://127.0.0.1:8080:443/ppppp : false

這份驗證代碼有些繁瑣,不同場景下可能需要做點調(diào)整。如有什么問題,請指出~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容