• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

What is SQL Injection? How to prevent SQL Injection vulnerabilities

AnonyViet by AnonyViet
March 2, 2026
in Tips
0

SQL injection vulnerabilities arise when you make database queries insecurely. Simply put, users can view your website’s database by entering a query into the URL or information form. Don’t underestimate SQL injection because it’s inside Top 10 web security vulnerabilities according to OWASP 2020 announcement there.

📢 Join the channel Telegram belong to AnonyViet


👉 Go to Telegram AnonyViet

Update new articles, cool tools and IT tips fastest

What is SQL Injection? How to prevent SQL Injection vulnerabilities

How to exploit SQL Injection vulnerability

The simplest way is to use the SQLi auto exploit tool. I often use it SQLmapbecause according to experience, this is the tool with the best exploitation ability. But the downside is that you have to use commands instead of the interface.

Or you can use tools SQL Dumper has an interface with many features that are easier to set up. If you are new to learning, you can try this tool to exploit the website’s database.

How to prevent SQL injection vulnerabilities?

The best way to prevent SQL injection vulnerabilities is to use a framework that allows you to securely filter input data before sending it to the database. ORM (Object Relational Mapper) is a good option that you should try. For additional layers of security, validate all input and use WAF (Web Application Firewall).

Simple example

Let’s say we have a Java application that allows users to retrieve their documents by ID. I can do it like this:

String query = "SELECT * FROM documents WHERE ownerId=" + authContext.getUserId() + " AND documentName="" + request.getParameter("docName") + """;
executeQuery(query);

If the user ID is 25 and the URL is https://www.example.com/documents/?docName=ABC123, the query will be:

SELECT * FROM documents WHERE ownerId=25 AND documentName="ABC123";

Still fine, right? But what if the URL is https://www.example.com/documents/?docName=ABC123’OR’1’=’1?

Now I will get the following query that returns all documents of all users (because 1 = 1 is always true):

SELECT * FROM documents WHERE ownerId=25 AND documentName="ABC123" OR '1'='1';

So how to avoid this error?

Use Object Relational Mapping

Taking Java as an example, using an ORM such as hibernate to implement JPA (Java Persistence API) might look like this.

First, determine the model.

@Entity
public class Document {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;
  private String documentName;
  private Integer ownerId;
}

Then, define the class repository.

@Repository
public interface DocumentRepository extends JpaRepository {
  List findByDocumentNameAndOwnerId(String documentName, Integer ownerId);
}

Finally, you can use the repository and fetch the documents as follows:

List docs = documentRepository.findByDocumentNameAndOwnerId(request.getParameter("docName"), authContext.getUserId());

The ORM will handle all parameters safely. Now, let’s say you want more control over queries. In that case, many ORMs provide query builders you can use, such as the Hibernate Criteria API.

If you use Python, Django has an equally great ORM; If you don’t use Django, sqlalchemy is a great choice.

PHP has Doctrine. You just need to google to search for ORMs that match the technology you choose.

Warning

ORM frameworks are not 100% perfect.

First, they still have raw SQL query support/query parts. You just need to avoid using those features.

The second is that ORM frameworks often have security vulnerabilities, just like any other software package. So, let’s learn other good practices: validate all input data, use WAF, update packages…

Prepared statements

Prepared statements are a more manual choice and should be avoided because compared to ORM, it has a significantly higher risk of human error. However, this still beats the simple string concatenation method (like the example above). This approach looks like this:

String query = "SELECT * FROM documents WHERE ownerId=? AND documentName = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, authContext.getUserId());
ps.setString(2, request.getParameter("docName"));
ResultSet rs = ps.executeQuery();

In theory, this method is quite safe. However, in my experience, as the codebase grows larger, mistakes will start to appear. You only need one mistake to be completely attacked. Cases like arrays (documentId IN (“foo”, “bar”)) are where developers often make mistakes.

So if you decide to use this method, be careful with it as you expand the codebase.

Web Application Firewall

WAF products should not be considered a good SQL injection control. But they are a great additional layer of security and are often quite effective against SQL injection attacks.

A great open source solution is to deploy Apache with ModSecurity CRS in front of your webapp.

Database Firewall

Depending on your database and budget, you may consider trying database firewalls. I have never tried this, but you can also check out the link below, maybe it will help you.

Conclude

SQL injection is a simple injection vulnerability. And like all other security vulnerabilities, you can prevent it by using an appropriate library or framework to build the protocol, in this case SQL.

ORM is more secure than prepared statements. And if you don’t need too much control over the queries, use a lower level ORM often called a query builder. A WAF can add a layer of security, but you should never rely on it for security.

Frequently asked questions

What is SQL Injection and why is it dangerous?

SQL Injection is a security vulnerability that allows attackers to inject malicious SQL code into website database queries. This could allow them to access, edit, or delete sensitive data in your database.

What is the most effective way to prevent SQL Injection?

The best way is to use Object-Relational Mapper (ORM). ORM helps you create safe queries by safely handling input parameters. In addition, validating input data and using Web Application Firewall (WAF) is also important.

Are Prepared Statements safer than direct string concatenation?

Prepared Statements are safer than direct string concatenation when building SQL queries, but they still pose risks if not used properly. ORM is often recommended because of its ease of use and fewer errors.

Previous Post

Ứng dụng cầu nguyện của Iran bị hack, gửi thông điệp kêu gọi đầu hàng

Next Post

Synthesis of csc prompt to create beautiful studio-like food infographic

AnonyViet

AnonyViet

Related Posts

Create images of guardian mascots using AI according to year of birth
Tips

Create images of guardian mascots using AI according to year of birth

May 20, 2026
Try the Shopee Profit Calculator now, avoid the risk of selling at a loss
Tips

Try the Shopee Profit Calculator now, avoid the risk of selling at a loss

May 19, 2026
Prompt converts photos into extremely impressive children’s drawings
Tips

Prompt converts photos into extremely impressive children’s drawings

May 17, 2026
Instructions for receiving 1 month of ChatGPT Plus for free
Tips

Instructions for receiving 1 month of ChatGPT Plus for free

May 12, 2026
How to get ChatGPT Business 48 months discount from only 0 VND
Tips

How to get ChatGPT Business 48 months discount from only 0 VND

May 9, 2026
How to create a tiny version of yourself to follow the trend very easily
Tips

How to create a tiny version of yourself to follow the trend very easily

May 9, 2026
Next Post
Synthesis of csc prompt to create beautiful studio-like food infographic

Synthesis of csc prompt to create beautiful studio-like food infographic

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Don’t rush to buy a new MacBook if you don’t know these differences

Don’t rush to buy a new MacBook if you don’t know these differences

May 20, 2026
Create images of guardian mascots using AI according to year of birth

Create images of guardian mascots using AI according to year of birth

May 20, 2026
Try the Shopee Profit Calculator now, avoid the risk of selling at a loss

Try the Shopee Profit Calculator now, avoid the risk of selling at a loss

May 19, 2026
LibreOffice: Office suite to replace Microsoft Office

LibreOffice: Office suite to replace Microsoft Office

May 18, 2026
Don’t rush to buy a new MacBook if you don’t know these differences

Don’t rush to buy a new MacBook if you don’t know these differences

May 20, 2026
Create images of guardian mascots using AI according to year of birth

Create images of guardian mascots using AI according to year of birth

May 20, 2026
Try the Shopee Profit Calculator now, avoid the risk of selling at a loss

Try the Shopee Profit Calculator now, avoid the risk of selling at a loss

May 19, 2026
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

Don’t rush to buy a new MacBook if you don’t know these differences

Don’t rush to buy a new MacBook if you don’t know these differences

May 20, 2026
Create images of guardian mascots using AI according to year of birth

Create images of guardian mascots using AI according to year of birth

May 20, 2026
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply