@@ -11,44 +11,44 @@ SQL注入攻击(SQL Injection),简称注入攻击,是Web开发中最常
11
11
考虑以下简单的登录表单:
12
12
``` html
13
13
14
- <form action =" /login" method =" POST" >
15
- <p >Username: <input type =" text" name =" username" /></p >
16
- <p >Password: <input type =" password" name =" password" /></p >
17
- <p ><input type =" submit" value =" 登陆" /></p >
18
- </form >
14
+ <form action =" /login" method =" POST" >
15
+ <p >Username: <input type =" text" name =" username" /></p >
16
+ <p >Password: <input type =" password" name =" password" /></p >
17
+ <p ><input type =" submit" value =" 登陆" /></p >
18
+ </form >
19
19
20
20
```
21
21
我们的处理里面的SQL可能是这样的:
22
22
``` Go
23
23
24
- username := r.Form .Get (" username" )
25
- password := r.Form .Get (" password" )
26
- sql := " SELECT * FROM user WHERE username='" +username+" ' AND password='" +password+" '"
24
+ username := r.Form .Get (" username" )
25
+ password := r.Form .Get (" password" )
26
+ sql := " SELECT * FROM user WHERE username='" +username+" ' AND password='" +password+" '"
27
27
28
28
```
29
29
如果用户的输入的用户名如下,密码任意
30
30
``` Go
31
31
32
- myuser' or ' foo' = ' foo' --
32
+ myuser' or ' foo' = ' foo' --
33
33
34
34
```
35
35
那么我们的SQL变成了如下所示:
36
36
```Go
37
37
38
- SELECT * FROM user WHERE username=' myuser' or ' foo' = ' foo' --' ' AND password=' xxx'
38
+ SELECT * FROM user WHERE username=' myuser' or ' foo' = ' foo' --' ' AND password=' xxx'
39
39
```
40
40
在SQL里面`--`是注释标记,所以查询语句会在此中断。这就让攻击者在不知道任何合法用户名和密码的情况下成功登录了。
41
41
42
42
对于MSSQL还有更加危险的一种SQL注入,就是控制系统,下面这个可怕的例子将演示如何在某些版本的MSSQL数据库上执行系统命令。
43
43
```Go
44
44
45
- sql:="SELECT * FROM products WHERE name LIKE ' %" +prod+" %' "
46
- Db.Exec(sql)
45
+ sql:="SELECT * FROM products WHERE name LIKE ' %" +prod+" %' "
46
+ Db.Exec(sql)
47
47
```
48
48
如果攻击提交`a%' exec master..xp_cmdshell ' net user test testpass /ADD' --` 作为变量 prod的值,那么sql将会变成
49
49
` ` ` Go
50
50
51
- sql := " SELECT * FROM products WHERE name LIKE '%a%' e xec master..xp_cmdshell 'net user test testpass /ADD'--% '"
51
+ sql := " SELECT * FROM products WHERE name LIKE '%a%' e xec master..xp_cmdshell 'net user test testpass /ADD'--% '"
52
52
```
53
53
MSSQL服务器会执行这条SQL语句,包括它后面那个用于向系统添加新用户的命令。如果这个程序是以sa运行而 MSSQLSERVER服务又有足够的权限的话,攻击者就可以获得一个系统帐号来访问主机了。
54
54
0 commit comments