前言
最近在學習SQL注入,處於入門狀態,發現有些注入語句在后面會加上#或者--,可能是搜索姿勢不對,百度找不到,因此就根據個人的想法記錄一下#和--的作用
正題
看一下正常的SQL語句
select * from table where id='1'
假設id=‘1’處存在SQL注入漏洞,1是我們唯一可以進行操作的參數,既然都已經知道存在SQL漏洞(假設),我們就不再使用and 1=1啥的去驗證了,直接進入正題
如果我們需要查詢數據庫信息,我們可能會想到聯合查詢union select 1,database(),因此會這樣子輸入
1 union select 1,database()
確實,如果不好好分析,輸入的時候想,不就是查詢id=1時聯合一條語句查詢嗎?但是轉換成SQL語句就知道為什么了
select * from table where id='1 union select 1,database()'
可以看到,1 union select 1,database()會被當做條件字符串來查詢,因此我們需要給1用單引號’閉合
select * from table where id='1' union select 1,database()'
現在已經非常接近了,但是會發現,這條語句執行會報錯(可以數據庫控制台執行看看,或者navcat軟件等等),大概報錯是這樣
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 ''' at line 1
原因就是語句后面多出了單引號',因此我們需要想辦法去掉,這時候使用#或者--就可以解決
select * from table where id='1' union select 1,database()#'
或者
select * from table where id='1' union select 1,database()--'
#號和--號在數據庫里面起到單行注釋的作用(每個數據庫可能不一樣,需要按照數據庫的特性去決定使用那個符號),這里用#號或者--號,就是把后面的語句注釋掉,從而達到按照攻擊者意圖去執行SQL指令的目的
換成攻擊者輸入,就是下面這樣子
1' union select 1,database()#
或者
1' union select 1,database()--
結束...
