Skip to content

什麼是 SQL 注入以及如何防止這種攻擊?

SQL Injection is one of the most dangerous vulnerabilities for websites and online applications. It occurs when a user adds untrusted data to a database query, for example, when filling out a web form. 

If data injection is enabled, attackers can create user input to steal valuable data, bypass authentication, or corrupt records in your database.

 There are different types of SQL injection attacks, but in general, they all have a similar cause. Untrusted data that the user enters is concatenated with the query string. 

Therefore, user input can change the original intent of the query and lead to numerous security issues

In this article, we cover and recommend some best practices for technicians to use in preventing SQL Injection attacks. Keep reading and understand more about these practices! 

Do Not Rely on Client-side Input Validation

Client-side input validation is an excellent practice to prevent SQL Injection attacks. With client-side input validation, you can now prevent invalid information from being sent to your system logic. However, this only works for users who have no bad intentions and want to use the system as designed. 

Providing the user with direct feedback that a certain value is not valid is very useful and simple. Therefore, you should use client-side validation to help your user experience. 

When looking at SQL injection, this is not a method you should trust. You can remove client-side validation by changing some Javascript code loaded in your browser. 

Also, it is very easy to make a basic HTTP call to the backend in a client-server architecture with a parameter that causes an SQL injection. Maybe using tools the old-school curl commands.

You should validate the server-side, preferably as close to the source as possible. In this case, you create the SQL query. Anything a client sends you should be considered potentially harmful. So, in this case, relying on client-side validation for SQL injection is a terrible idea.

Use Database Engines With Restricted Privileges

When creating a database user for your application, you should think about this user’s privileges.

Does the application need to be able to read, write and update all databases? How about truncating or dropping tables? If you limit your application’s privileges on the database, you can minimize the impact of SQL injection. 

It is advisable not to have a single database user for your application, but to create multiple database users and connect them to specific application roles with different privileges. Security issues are likely a ripple effect, so you should be aware of all relationships to avoid heavy damage.

Use Ready-made Instructions and Query Parameterization

Many languages have built-in features available that help prevent SQL injection. When writing SQL queries, you can use something like a ready-made statement to compile the query. 

With a ready-made statement, we can perform query parameterization, which is a technique to dynamically create SQL statements. You create the base query with some placeholders and securely attach user-supplied parameters to those placeholders.

When using a real ready-made statement and parameterized queries, the database itself actually takes care of the escape. First, it builds the query execution plan based on the query string with placeholders. 

In the second step, the (untrusted) parameters are sent to the database. The query plan is already created, so the parameters no longer influence this. This avoids the injection completely.

About Version 2 Limited
Version 2 Limited is one of the most dynamic IT companies in Asia. The company develops and distributes IT products for Internet and IP-based networks, including communication systems, Internet software, security, network, and media products. Through an extensive network of channels, point of sales, resellers, and partnership companies, Version 2 Limited offers quality products and services which are highly acclaimed in the market. Its customers cover a wide spectrum which include Global 1000 enterprises, regional listed companies, public utilities, Government, a vast number of successful SMEs, and consumers in various Asian cities.

About Senhasegura
Senhasegura strive to ensure the sovereignty of companies over actions and privileged information. To this end, we work against data theft through traceability of administrator actions on networks, servers, databases and a multitude of devices. In addition, we pursue compliance with auditing requirements and the most demanding standards, including PCI DSS, Sarbanes-Oxley, ISO 27001 and HIPAA.