加固SQL参数与存储过程_MySQL
bitsCN.com
开发人员对于注入攻击可能有了一些了解,但是实际运用中却很难通过把握一些重要环节和技术对注入攻击进行防范。
本节为读者讲解如何利用ADO.NET本身的参数对象和存储过程技术防止注入攻击,以达到用户界面输入与原始SQL的分离,使黑客无法拼接SQL语句的目的。
SQL参数与存储过程
SQL参数是开发人员很容易忽视的一个环节,通常直接完成SQL语句,然后传递给数据库执行。这样的写法固然简单,但是也为注入攻击埋下了伏笔。如果要避免注入攻击,就要对SQL语句进行专门的过滤处理,但如果直接使用SQL参数对象就可以省去以上 环节。
SQL中的Parameters集合提供了类型检查和长度验证。如果研发人员使用Parameters集合,输入将被视为文本值进行处理,SQL不会将它视为可执行代码。使用Parameters集合的另一个好处是可以实施类型和长度检查,如果值超出范围将触发异常。这是纵深防范的一个好例子。尽可能地使用存储过程,而且应该通过Parameters集合调用它们。
下面通过几行代码说明如何使用参数集合对象,读者注意@au_id参数将被当作文本值而不是可执行代码。同样,对参数将进行类型和长度检查。在下面的示例中,输入值不能长于11个字符。如果数据不遵守参数所定义的类型或者长度,将出现异常。
Parameters集合演示代码如下:
SqlDataAdapter myCommand = new SqlDataAdapter("AuthorLogin",conn); myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; SqlParameter parm = myCommand.SelectCommand.Parameters.Add( "@au_id",SqlDbType.VarChar,11); parm.Value = Login.Text;
请读者注意,使用存储过程并不一定能防止SQL注入。重要的是在存储过程中使用参数对象。如果不使用参数,存储过程使用未经筛选的输入时,就很容易遭到SQL注入攻击。例如,以下代码片段就存在问题:
SqlDataAdapter myCommand = new SqlDataAdapter("LoginStoredProcedure '" + Login.Text + "'", conn);
正确的代码片段如下所示:
SqlDataAdapter myCommand = new SqlDataAdapter( "SELECT au_lname, au_fname FROM Authors WHERE au_id = @au_id",conn); SqlParameter parm = myCommand.SelectCommand.Parameters.Add("@au_id", SqlDbType.VarChar,11); parm.Value = Login.Text;
以上就是加固SQL参数与存储过程_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

Stored procedures in Oracle database are a specific type of stored procedures used to execute a series of SQL statements and data operations in the database. In actual database development work, sometimes we need to determine whether a certain table exists in the database, so that we can do some judgment and logical processing in the storage process. Below we will introduce how to implement the method of determining whether a table exists in Oracle database, and provide specific code examples. First, we can use the system table user_tables or all_t

MySQL methods for deleting stored procedures include using the DROP PROCEDURE statement, using MySQL Workbench, and using command line tools. Detailed introduction: 1. Use the DROP PROCEDURE statement. The steps are to first open the MySQL client or use any tool that supports MySQL, then connect to your MySQL database, and finally execute the following SQL statement to delete the stored procedure; 2. Use MySQL Workbench to delete Stored procedures and so on.

Implementation Principles and Applications of Golang Stored Procedures Stored procedures are precompiled programs that are stored in relational databases and can be called by applications. They can effectively reduce the cost of network transmission of data and improve the execution efficiency of the database. Although Golang does not directly support stored procedures, you can simulate the functions of stored procedures by using SQL statements. This article will introduce the principles and applications of implementing stored procedures in Golang, and provide specific code examples. 1. The implementation principle of Golang stored procedure is in Gol

Performance Optimization Strategies for Batch Updates of Oracle Stored Procedures In Oracle database, a stored procedure is a database object used to process data logic or perform specific tasks. It can provide certain performance optimization strategies, especially when updating data in batches. Updating data in batches usually involves a large number of row-level operations. In order to improve performance and efficiency, we can adopt some strategies and techniques to optimize the performance of stored procedures. The following will introduce some performance optimization strategies for batch updates of Oracle stored procedures and provide specific code examples.

Title: Detailed comparison and advantage analysis of Oracle stored procedures and functions. In Oracle database, stored procedures and functions are two important database objects. They can both be used to encapsulate a series of SQL statements and logic to improve the efficiency and complexity of data operations. Usability. This article will compare the characteristics of Oracle stored procedures and functions in detail, as well as their respective advantages, and provide specific code examples. Stored procedure A stored procedure is a set of SQL statements and PL/SQL code logic that are pre-written and stored in the database.

Golang is a powerful programming language that can easily implement stored procedures. In this article, we will introduce how to write efficient stored procedures using Golang and the benefits of using them in your projects.

How to write custom stored procedures and functions in MySQL using C# Introduction: MySQL is a widely used open source database management system, and C# is a commonly used object-oriented programming language. During the development process, we often need to use database stored procedures and functions to improve code reusability and performance. This article will introduce how to use C# to write custom stored procedures and functions in a MySQL database, and provide specific code examples. 1. Stored procedures A stored procedure is a set of SQL statements that perform specific tasks.
