DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Workarounds for Oracle Restrictions on the Size of Expression Lists
  • What Is SQL Injection and How Can It Be Avoided?
  • How To Work Effectively With JDBC in Java Scripts
  • Vulnerable Code [Comic]

Trending

  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  1. DZone
  2. Coding
  3. Languages
  4. How to Check Text Inputs for SQL Injection Attacks in Java

How to Check Text Inputs for SQL Injection Attacks in Java

Learn how to detect SQL injection attacks from text input(s) using an API in Java.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Apr. 25, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
8.3K Views

Join the DZone community and get the full member experience.

Join For Free

SQL (Structured Query Language) injection is a code injection technique used to attack data-driven applications; the SQL statements are inserted into an entry field for execution and wreak havoc from there. This type of attack tends to seek and target existing security vulnerabilities within websites or other databases to acquire access to sensitive information. For example, if the field of an online form is coded incorrectly, this provides an opening for the malicious user to sneak in SQL commands that the system will consider valid and return a response containing information that can be leveraged to access the data and manipulate, modify, or destroy it from there.

Despite the growing number of organizations that have reported successful SQL injection attacks, this type of threat is often underestimated in comparison to other cyber-crimes. Due to their reliance on check-out forms for their websites, retail companies have shown to be particularly susceptible to these threats. While standard firewalls may aim at protecting your website or application from SQL injection, the potential for failure can cause serious damage and data loss for your company. The following APIs can assist in providing supplementary protection by detecting SQL injection attacks from single or multiple text inputs, and will even define the threat detection level you want to utilize.

To run the APIs in Java, we will first install the Maven SDK by adding a reference to the repository:

Java
 




x


 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>



Then we will add a reference to the dependency:

Java
 




xxxxxxxxxx
1


 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.90</version>
6
</dependency>
7
</dependencies>



Now for our first API, we will be checking a singular text input for SQL injection. The only parameters that need to be included are:

  • User-facing text input: the target text input string.
  • API key: your personal API key; this can be retrieved by registering for a free account on the Cloudmersive website.
  • Detection level (optional): threat detection level for the process; set to Normal to target a high-security SQL Injection detection level with a very low false-positive rate; select High to target a very-high security SQL Injection detection level with higher false positives. Default is Normal (recommended).

We can input the above information into the following code to call our validation function:

Java
 




xxxxxxxxxx
1
26


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.TextInputApi;
7

          
8
ApiClient defaultClient = Config    uration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
TextInputApi apiInstance = new TextInputApi();
17
String value = "value_example"; // String | User-facing text input.
18
String detectionLevel = "detectionLevel_example"; // String | Set to Normal to target a high-security SQL Injection detection level with a very low false positive rate; select High to target a very-high security SQL Injection detection level with higher false positives.  Default is Normal (recommended).
19
try {
20
    SqlInjectionDetectionResult result = apiInstance.textInputCheckSqlInjection(value, detectionLevel);
21
    System.out.println(result);
22
} catch (ApiException e) {
23
    System.err.println("Exception when calling TextInputApi#textInputCheckSqlInjection");
24
    e.printStackTrace();
25
}



The returned response will simply indicate if an SQL injection attack was found. Now for our next API, we will detect SQL injection attacks from multiple text inputs in batch by using the following code:

Java
 




xxxxxxxxxx
1
35


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.TextInputApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
TextInputApi apiInstance = new TextInputApi();
17
SqlInjectionCheckBatchRequest value = new SqlInjectionCheckBatchRequest(); // SqlInjectionCheckBatchRequest | User-facing text input.
18
try {
19
    SqlInjectionCheckBatchResponse result = apiInstance.textInputCheckSqlInjectionBatch(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling TextInputApi#textInputCheckSqlInjectionBatch");
23
    e.printStackTrace();
24
}
25

          
26
The only parameters required for the operation to run smoothly are the API key and the user-facing text inputs as shown in the following example string:
27
{
28
  "RequestItems": [
29
    {
30
      "InputText": "string"
31
    }
32
  ],
33
  "DetectionLevel": "string"
34
}



To keep things as simple as possible, the output from the operation will preserve the order of the text inputs.

And that’s it! Integrating this protection into your site or database will keep you safe from those custom-made and easy-to-miss SQL injection threats. If you have questions on either of these APIs, feel free to contact our sales team here; they’re always happy to help.

sql Injection Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Workarounds for Oracle Restrictions on the Size of Expression Lists
  • What Is SQL Injection and How Can It Be Avoided?
  • How To Work Effectively With JDBC in Java Scripts
  • Vulnerable Code [Comic]

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!