Trustwave and Cybereason Merge to Form Global MDR Powerhouse for Unparalleled Cybersecurity Value. Learn More

Trustwave and Cybereason Merge to Form Global MDR Powerhouse for Unparalleled Cybersecurity Value. Learn More

Services
Managed Detection & Response

Eliminate active threats with 24/7 threat detection, investigation, and response.

Co-Managed SOC (SIEM)

Maximize your SIEM investment, stop alert fatigue, and enhance your team with hybrid security operations support.

Advisory & Diagnostics

Advance your cybersecurity program and get expert guidance where you need it most.

Penetration Testing

Test your physical locations and IT infrastructure to shore up weaknesses before exploitation.

Database Security

Prevent unauthorized access and exceed compliance requirements.

Email Security

Stop email threats others miss and secure your organization against the #1 ransomware attack vector.

Digital Forensics & Incident Response

Prepare for the inevitable with 24/7 global breach response in-region and available on-site.

Firewall & Technology Management

Mitigate risk of a cyberattack with 24/7 incident and health monitoring and the latest threat intelligence.

Solutions
BY TOPIC
Microsoft Security
Unlock the full power of Microsoft Security
Offensive Security
Solutions to maximize your security ROI
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
About Us
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Security Operations Platform
Trustwave Security Colony
Partners
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats

HQL Injection Exploitation in MySQL

Are you familiar with an HQL injection exploitation? Chances are you’re not. While you may assume it’s intuitive since it’s related to SQL injection, you’re right, but it’s a little bit more complex.

HQL stands for Hibernate Query Language. Hibernate is an Object-Relational Mapping (ORM) that maps class definition (within a source code) with associated SQL tables. The HQL is a language similar to SQL but operating on persistent objects instead of directly operating with tables and columns. The HQL query is translated to SQL by the Hibernate framework, then the SQL query is passed into the database.

If we have an injection in HQL syntax, we cannot exploit it as a normal SQL injection as HQL language has its own syntax and it's more restricted (for example, there is no way to query unmapped tables). But a couple of years ago Russian security folks found a way to escape from the HQL context into the SQL context and communicate directly to the database. To be honest, I couldn’t find any English articles about how to exploit it, but there is some good Russian research. I’ll show how to exploit it using an example I recently found during a customer project. The escaping methods are different across databases and in my case, it was a MySQL database.

The initial request/response and parameter I paid attention for were (the POST body is in GWT format):

16225_img1

When I put the quote in the parameter, the server throws the error in the response:

16226_img2

Such special characters like vertical bar (‘|’) or backslash (‘\’) will break the GWT syntax, so I need to encode them properly. The additional encoding for the GWT format is:

  • | => \!
  • \ => \\

Before escape from the HQL context, I had to understand what database I have a deal with. So, I ran a couple of tests:

Payload Result
orderInGroup' error (EX)
'orderInGroup' ok (OK)
'orderInGroup'\!\!'x error (EX)
'orderInGroup'\!\!'x' ok (OK)
'orderInGroup'\!\!'x'\!\!'' ok (OK)
'orderInGroup'\!\!str(46)\!\!'' ok (OK)
'orderInGroup'\!\!substr('x',1,1)\!\!'' ok (OK)

After a little research by playing with the most well-known databases, such as MySQL, MSSQL, Oracle, the last payload could work only for MySQL. So, only now I could try to escape from the HQL context.

The set of characters \'' (backslash, single quote, single quote) escapes from the HQL context into the MySQL context. The single quote escapes the other single quote in the HQL query context, but the backslash is the normal symbol. It is opposite for MySQL, the backslash escapes the single quote, but not another single quote. Hence, when the Hibernate Framework parses the query it sees the one query, when it comes to the MySQL database, MySQL sees another query.

Hibernate sees:

'orderInGroup'||'\''|| (select 1)) -- ' // as a string

The payload goes to MySQL and MySQL sees:

'orderInGroup'||'\''|| (select 1)) -- ' // string '\'', logical 'or', select query

The screenshot below shows the positive result from using the payload (the response is “OK”):

16227_img3

After I got escaped, I wanted to retrieve some information from the database. The injection appears in the 'order by' query part. As the application does not reveal the queried data, I used 'boolean-based blind' exploitation technique. The updatexml MySQL function is the most suitable for this purpose because it has a specific behavior: it throws an error if the second parameter is not a valid XPath query string or returns no value if the XPath query is correct.

updatexml(xml_target,xpath_expr,new_xml)

The important part is that the error is being thrown only after the second parameter is evaluated (which contain the 'if' clause). So, depending on whether the ‘if’ clause is successful the application returns or does not return the error. This type of injection is called 'boolean-based blind' HQL injection. It allows an attacker to extract data character-by-character.

The screenshot shows the process of revealing characters one by one by using Burp Suite Intruder:

16228_img4

The whole obtained value will be:

16229_img5

And, finally, the decoded 'user()' value is '[...]PROD_SELF[...]'.

More examples for other databases such as Oracle, Postgresql, and MSSQL can be found here.

ABOUT TRUSTWAVE

Trustwave is a globally recognized cybersecurity leader that reduces cyber risk and fortifies organizations against disruptive and damaging cyber threats. Our comprehensive offensive and defensive cybersecurity portfolio detects what others cannot, responds with greater speed and effectiveness, optimizes client investment, and improves security resilience. Learn more about us.

Latest Intelligence

Discover how our specialists can tailor a security program to fit the needs of
your organization.

Request a Demo