參考phith0n的談一談如何在Python開發中拒絕SSRF漏洞,用PHP寫一個檢測是否是內網URL的函數。
需要注意的是,我寫的這個檢測URL的函數并沒有考慮到客戶端允許重定向跟蹤的情況。如果允許重定向跟蹤則需要檢查每個30x響應包的location字段。
<?php
/*
* 判斷是否合法的URL,合法則返回true;
* 格式不是http/https協議,或是內網IP,則視為不合法
*
* 注意:判斷是否是內網url的時候并沒有檢測30X跳轉的location響應頭部,
* 使用此方法時,要求curl或其他工具設置不允許重定向跟蹤
*
* @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協議
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函數:parse_url()
和gethostbyname()
。
-
PHP解析URL的host:
parse_url()
函數不是用來驗證給定 URL 的合法性的,只是將其分解為幾部分。不完整的 URL 也被接受,parse_url()
會嘗試盡量正確地將其解析。 -
PHP解析URL的IP:
gethostbyname($hostname)
函數會返回IPv4地址,解析失敗的話就會返回沒被修改過的$hostname
。
(以下內容于2021年1月25日添加)
之前的代碼早有繞過方式,且有個大的錯誤,就是在給$is_inner_ipaddress
變量賦值的時候用了or
,而不是||
,導致192.168.0.1
這些內網ip其實是沒被檢測到的,原因是or
的優先級比賦值符=
的小,||
的優先級才比=
的大(參考:https://stackoverflow.com/questions/5998309/logical-operators-or-or/5998330#5998330)。
<?php
/*
* 判斷是否合法的URL,合法則返回true
* 不合法的情況:
* 1. 包含非ASCII碼字符
* 2. 不是http或https協議
* 3. ipv6地址(不支持ipv6地址,將判斷為不合法)
* 4. URL的user、pass、host,fragment段包含@、\或其它字符
* 5. 請求地址為內網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跳轉的location響應頭部是否為合法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
下運行的結果:
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
這份驗證代碼有些繁瑣,不同場景下可能需要做點調整。如有什么問題,請指出~