Home Operation and Maintenance Safety What are the methods of SQL injection defense?

What are the methods of SQL injection defense?

Jun 29, 2020 am 09:34 AM
sql injection defense

SQL注入防御的方法有:1、PreparedStatement;2、使用正则表达式过滤传入的参数;3、字符串过滤。其中,采用预编译语句集是简单又有效的方法,因为它内置了处理SQL注入的能力。

What are the methods of SQL injection defense?

SQL注入防御的方法

下面针对JSP,说一下应对方法:

(推荐学习:mysql教程

1、(简单又有效的方法)PreparedStatement

采用预编译语句集,它内置了处理SQL注入的能力,只要使用它的setXXX方法传值即可。

使用好处:

(1)代码的可读性和可维护性;

(2)PreparedStatement尽最大可能提高性能;

(3)最重要的一点是极大地提高了安全性。

原理:

sql注入只对sql语句的准备(编译)过程有破坏作用,而PreparedStatement已经准备好了,执行阶段只是把输入串作为数据处理,而不再对sql语句进行解析、准备,因此也就避免了sql注入问题。

2、使用正则表达式过滤传入的参数

要引入的包:

import java.util.regex.*;
Copy after login

正则表达式:

private String CHECKSQL = “^(.+)\\sand\\s(.+)|(.+)\\sor(.+)\\s$”;
Copy after login

判断是否匹配:

Pattern.matches(CHECKSQL,targerStr);
Copy after login

下面是具体的正则表达式:

检测SQL meta-characters的正则表达式 :

/(\%27)|(\’)|(\-\-)|(\%23)|(#)/ix
Copy after login

修正检测SQL meta-characters的正则表达式 :

/((\%3D)|(=))[^\n]*((\%27)|(\’)|(\-\-)|(\%3B)|(:))/i
Copy after login

典型的SQL 注入攻击的正则表达式 :

/\w*((\%27)|(\’))((\%6F)|o|(\%4F))((\%72)|r|(\%52))/ix
Copy after login

检测SQL注入,UNION查询关键字的正则表达式 :

/((\%27)|(\’))union/ix(\%27)|(\’)
Copy after login

检测MS SQL Server SQL注入攻击的正则表达式:

/exec(\s|\+)+(s|x)p\w+/ix
Copy after login

等等…..

3、字符串过滤

比较通用的一个方法:(||之间的参数可以根据自己程序的需要添加)

public static boolean sql_inj(String str){
String inj_str = "'|and|exec|insert|select|delete|update|
count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";
String inj_stra[] = split(inj_str,"|");
for (int i=0 ; i < inj_stra.length ; i++ ){
if (str.indexOf(inj_stra[i])>=0){
return true;
}
}
return false;
}
Copy after login

凡涉及到执行的SQL中有变量时,用JDBC(或者其他数据持久层)提供的如:PreparedStatement就可以 ,切记不要用拼接字符串的方法就可以了。

The above is the detailed content of What are the methods of SQL injection defense?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nginx basic security knowledge: preventing SQL injection attacks Nginx basic security knowledge: preventing SQL injection attacks Jun 10, 2023 pm 12:31 PM

Nginx is a fast, high-performance, scalable web server, and its security is an issue that cannot be ignored in web application development. Especially SQL injection attacks, which can cause huge damage to web applications. In this article, we will discuss how to use Nginx to prevent SQL injection attacks to protect the security of web applications. What is a SQL injection attack? SQL injection attack is an attack method that exploits vulnerabilities in web applications. Attackers can inject malicious code into web applications

How to use exp for SQL error injection How to use exp for SQL error injection May 12, 2023 am 10:16 AM

0x01 Preface Overview The editor discovered another Double data overflow in MySQL. When we get the functions in MySQL, the editor is more interested in the mathematical functions. They should also contain some data types to save values. So the editor ran to test to see which functions would cause overflow errors. Then the editor discovered that when a value greater than 709 is passed, the function exp() will cause an overflow error. mysql>selectexp(709);+-----------------------+|exp(709)|+---------- ------------+|8.218407461554972

How does PHP prevent SSTI attacks? How does PHP prevent SSTI attacks? Jun 30, 2023 am 09:36 AM

How to use PHP to defend against Server-SideTemplateInjection (SSTI) attacks Introduction: Server-SideTemplateInjection (SSTI) is a common web application security vulnerability. An attacker can cause the server to execute arbitrary code by injecting malicious code into the template engine, thereby causing Serious safety hazard. In PHP applications, SST can be exposed when user input is not handled correctly

PHP Programming Tips: How to Prevent SQL Injection Attacks PHP Programming Tips: How to Prevent SQL Injection Attacks Aug 17, 2023 pm 01:49 PM

PHP Programming Tips: How to Prevent SQL Injection Attacks Security is crucial when performing database operations. SQL injection attacks are a common network attack that exploit an application's improper handling of user input, resulting in malicious SQL code being inserted and executed. To protect our application from SQL injection attacks, we need to take some precautions. Use parameterized queries Parameterized queries are the most basic and most effective way to prevent SQL injection attacks. It works by comparing user-entered values ​​with a SQL query

How to protect against cross-site scripting (XSS) attacks using PHP How to protect against cross-site scripting (XSS) attacks using PHP Jun 29, 2023 am 10:46 AM

How to Use PHP to Defend Cross-Site Scripting (XSS) Attacks With the rapid development of the Internet, Cross-SiteScripting (XSS) attacks are one of the most common network security threats. XSS attacks mainly achieve the purpose of obtaining user sensitive information and stealing user accounts by injecting malicious scripts into web pages. To protect the security of user data, developers should take appropriate measures to defend against XSS attacks. This article will introduce some commonly used PHP technologies to defend against XSS attacks.

Detection and repair of PHP SQL injection vulnerabilities Detection and repair of PHP SQL injection vulnerabilities Aug 08, 2023 pm 02:04 PM

Overview of detection and repair of PHP SQL injection vulnerabilities: SQL injection refers to an attack method in which attackers use web applications to maliciously inject SQL code into the input. PHP, as a scripting language widely used in web development, is widely used to develop dynamic websites and applications. However, due to the flexibility and ease of use of PHP, developers often ignore security, resulting in the existence of SQL injection vulnerabilities. This article will introduce how to detect and fix SQL injection vulnerabilities in PHP and provide relevant code examples. check

How to use PHP and Vue.js to develop applications that protect against sensitive data leaks How to use PHP and Vue.js to develop applications that protect against sensitive data leaks Jul 06, 2023 am 11:01 AM

How to use PHP and Vue.js to develop applications that protect against sensitive data leakage In today's information age, the privacy and sensitive data of individuals and institutions face many security threats, one of the most common threats is data leakage. To prevent this risk, we need to pay attention to data security when developing applications. This article will introduce how to use PHP and Vue.js to develop an application that prevents sensitive data leakage, and provide corresponding code examples. Use a secure connection When transmitting data, make sure to use a secure connection.

How to use PHP to defend against HTTP response splitting and HTTP parameter pollution attacks How to use PHP to defend against HTTP response splitting and HTTP parameter pollution attacks Jun 29, 2023 am 10:01 AM

How to use PHP to defend against HTTP response splitting and HTTP parameter pollution attacks. With the continuous development of the Internet, network security issues are becoming more and more important. HTTP response splitting and HTTP parameter pollution attacks are common network security vulnerabilities, which can lead to risks of server attacks and data leakage. This article will introduce how to use PHP to defend against both forms of attacks. 1. HTTP response splitting attack HTTP response splitting attack means that the attacker sends specially crafted requests to cause the server to return multiple independent HTTP responses.

See all articles