• 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
January 29, 2023
in Tips
0

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

Join the channel Telegram of the AnonyViet 👉 Link 👈

sql injection

How to exploit SQL Injection vulnerability

The simplest is to use the SQLi auto exploit tool. I usually use SQLmap, because according to experience this is the tool with the best possible exploitation. But the downside is that you have to use commands instead of interfaces.

Or you can use the tool SQL Dumper has an interface with more 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 it enters 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 I 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 would 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?

Using 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, define 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<Document, Long> {
  List<Document> findByDocumentNameAndOwnerId(String documentName, Integer ownerId);
}

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

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

The ORM will handle all parameters safely. Now suppose you want more control over the queries. In that case, many ORMs provide query builder 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 option.

PHP has Doctrine. You just need to google to search for ORMs that match the technology of your choice.

Warning

ORM frameworks are not 100% perfect.

The first is that they still have the functionality to support raw SQL queries/query parts. You just need to avoid using those features.

The second is that ORM frameworks often have security holes, just like any other software package. So 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 ORMs, 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 is pretty 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 devs often make mistakes.

So if you decide to use this approach, be careful with it when you extend the codebase.

Web Application Firewall

WAF products should not be considered a good SQL injection control. But they are a great extra 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 might consider trying out database firewalls. I have never tried this, but you can also find out the link below, maybe it will help you.

Conclusion

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

ORM is safer than prepared statements. And if you don’t need too much control over the queries, use a lower level ORM commonly known as a query builder. WAF can add an extra layer of security, but you should never rely on it for security.

The article achieved: 5/5 – (101 votes)

Tags: InjectionpreventSQLVulnerabilities
Previous Post

Basic hacking knowledge you should know – Part 3

Next Post

8 best spam call blocking apps for Android

AnonyViet

AnonyViet

Related Posts

Top 5 game programming languages ​​to learn now
Tips

Top 5 game programming languages ​​to learn now

June 8, 2025
[Godot Shooter] #2: Creating characters & shooting bullets
Tips

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025
What do you need to learn game programming? Is it difficult? How long does it take?
Tips

What do you need to learn game programming? Is it difficult? How long does it take?

June 6, 2025
Instructions for registering chatgpt team at $ 1
Tips

Instructions for registering chatgpt team at $ 1

June 5, 2025
How to engrave the right mouse menu error on Windows
Tips

How to engrave the right mouse menu error on Windows

June 5, 2025
How to create online meme photos is very easy with a few steps
Tips

How to create online meme photos is very easy with a few steps

June 5, 2025
Next Post
8 best spam call blocking apps for Android

8 best spam call blocking apps for Android

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 2025
Discover the glowing effect next to the iPhone ios 18 screen

Discover the glowing effect next to the iPhone ios 18 screen

June 8, 2025
[Godot Shooter] #2: Creating characters & shooting bullets

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025
Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 2025
Discover the glowing effect next to the iPhone ios 18 screen

Discover the glowing effect next to the iPhone ios 18 screen

June 8, 2025
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

Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí SHBET https://kubet88.yoga/ bj88

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí SHBET https://kubet88.yoga/ bj88

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