Logo

dev-resources.site

for different kinds of informations.

Handling Injection Attacks in Java

Published at
3/14/2021
Categories
java
injection
sqlinjection
xxeinjection
Author
eidher
Author
6 person written this
eidher
open
Handling Injection Attacks in Java

An injection attack is the insertion of malicious data from the client to the application using SQL or XXE (XML External Entity).
It is important to prevent injection attacks because it allows attackers to spoof identity, tamper with existing data, disclosure all the data, destroy the data, become the administrator, etc.

SQL Injection

If the attacker introduces something like ' or 1=1 -- the application could display data from the database:

Alt Text

A patch to the SQL injection

The flaw is because the field (accountName) is concatenated to the SQL statement:

String query = "SELECT * FROM user_data WHERE last_name = '" + accountName + "'";

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
ResultSet results = statement.executeQuery(query);
Enter fullscreen mode Exit fullscreen mode

To patch this flaw you must use Immutable Queries, Static Queries, Parameterized Queries, or Stored Procedures. For the previous example the best solution is a parameterized query:

final String query = "SELECT * FROM user_data WHERE last_name = ?";

try {
  PreparedStatement statement = connection.prepareStatement(query,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
  statement.setString(1, accountName);
  ResultSet results = statement.executeQuery(query);
  ...
} catch (SQLException sqle) {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Now, if you try to inject SQL you get an exception.

XXE Injection

If you have a service that receives an XML, somebody could change that XML (using Burp Suite) in order to access local resources, execute code remotely, disclose files, or execute a DoS attack (using a Billion laughs attack).

Alt Text

You may say "no worries, I use JSON in my REST services". However, the attacker could change the content type of the request body and send the same XML. For instance, next we have the request of a service caught by Burp Suite (ellipsis is used to omit irrelevant information):

...
Content-Type: application/json
...

{"text":"test"}
Enter fullscreen mode Exit fullscreen mode

It is as easy as to change the Content-Type to XML and the payload to perform the attack:

...
Content-Type: application/xml
...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
  <!ELEMENT foo ANY>
  <!ENTITY xxe SYSTEM "../../">
]>
<comment>
  <text>&xxe;</text>
</comment>
Enter fullscreen mode Exit fullscreen mode

This service is used to post a comment, but now we have posted a directory of the server:
Alt Text

After some tries, you could print the content of an important file (like passwords or configurations).

A patch to the XXE injection

You can validate the input, the content type, or instruct your parser to ignore DTD (document type definition). See the setProperty invocation:

protected Comment parseXml(String xml) throws Exception {
  JAXBContext jc = JAXBContext.newInstance(Comment.class);

  XMLInputFactory xif = XMLInputFactory.newFactory();
  xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);

  XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(XML));

  Unmarshaller unmarshaller = jc.createUnmarshaller();
  return (Comment) unmarshaller.unmarshal(xsr);
}
Enter fullscreen mode Exit fullscreen mode

For Spring REST Services, you can specify the consumes = MediaType.APPLICATION_JSON_VALUE:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public AttackResult createNewUser(@RequestBody String commentStr, @RequestHeader("Content-Type") String contentType) throws Exception {
        ...
}
Enter fullscreen mode Exit fullscreen mode

Now, the attacker cannot send XML:

Alt Text

For more information see: https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html

injection Article's
26 articles in total
Favicon
Ore: Advanced Dependency Injection Package for Go
Favicon
Beauty Injection: Achieve Radiance Without Surgery
Favicon
Exploring Fat Burning Injections in Mt. Juliet, TN
Favicon
Dependency Injection Explained in C#
Favicon
Bug SQL Injection - Sub bawaslu.go.id - Badan Pengawas Pemilihan Umum
Favicon
Custom Plastic Molding Near Me, Mold Makers, Mold Manufacturers, and Rapid Prototyping: A Comprehensive Guide
Favicon
Loose Coupling and Dependency Injection (DI) principle
Favicon
Dependency Injection for JavaScript Developers
Favicon
AWS Security Stories #04.4: OWASP - Injection
Favicon
Angular Dependency Injection
Favicon
How to create your own dependency injection framework in Java
Favicon
NodeJs - Dependency injection, make it easy
Favicon
A Step by Step Guide to ASP.NET Core Dependency Injection
Favicon
Ծրագրային անվտանգություն՝ SQL Injection (մաս 1)
Favicon
How to implement Dependency Injection in Node.js with Example
Favicon
Why you should use dependency injection
Favicon
Handling Injection Attacks in Java
Favicon
A straightforward introduction to Dependency Injection
Favicon
How to create your own dependency injection framework in Java
Favicon
Let's stop being stupid about security
Favicon
Dagger with a Hilt
Favicon
Security: Released npm package 📦 for Protecting CSV Injection 🚀
Favicon
Yet another dependency injection library
Favicon
O11ycon Talks: Dr. Peter Alvaro, "Twilight Of The Experts"
Favicon
Jersey Injection Dependency example with HK2
Favicon
How to Convert date format to PHP with MYSQLI, using secure queries to avoid SQL injection

Featured ones: