CN114936369B - Active defense method, system and storage medium for SQL injection attacks based on markers - Google Patents
Active defense method, system and storage medium for SQL injection attacks based on markers Download PDFInfo
- Publication number
- CN114936369B CN114936369B CN202210438587.9A CN202210438587A CN114936369B CN 114936369 B CN114936369 B CN 114936369B CN 202210438587 A CN202210438587 A CN 202210438587A CN 114936369 B CN114936369 B CN 114936369B
- Authority
- CN
- China
- Prior art keywords
- untrusted
- variables
- sql
- variable
- database
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000002347 injection Methods 0.000 title claims abstract description 45
- 239000007924 injection Substances 0.000 title claims abstract description 45
- 238000000034 method Methods 0.000 title claims abstract description 40
- 230000007123 defense Effects 0.000 title claims abstract description 23
- 238000001914 filtration Methods 0.000 claims abstract description 39
- 230000006870 function Effects 0.000 claims description 74
- 238000004458 analytical method Methods 0.000 claims description 28
- 230000008569 process Effects 0.000 claims description 17
- 230000004048 modification Effects 0.000 claims description 11
- 238000012986 modification Methods 0.000 claims description 11
- 239000000203 mixture Substances 0.000 claims description 6
- 238000003066 decision tree Methods 0.000 claims description 4
- 238000010276 construction Methods 0.000 claims 1
- 230000009897 systematic effect Effects 0.000 claims 1
- 238000005516 engineering process Methods 0.000 abstract description 3
- 230000003068 static effect Effects 0.000 description 6
- 230000006399 behavior Effects 0.000 description 3
- 239000000243 solution Substances 0.000 description 3
- 101001121408 Homo sapiens L-amino-acid oxidase Proteins 0.000 description 2
- 102100026388 L-amino-acid oxidase Human genes 0.000 description 2
- 238000010586 diagram Methods 0.000 description 2
- 101100012902 Saccharomyces cerevisiae (strain ATCC 204508 / S288c) FIG2 gene Proteins 0.000 description 1
- 101100233916 Saccharomyces cerevisiae (strain ATCC 204508 / S288c) KAR5 gene Proteins 0.000 description 1
- 230000005540 biological transmission Effects 0.000 description 1
- 230000007547 defect Effects 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 238000012038 vulnerability analysis Methods 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F21/00—Security arrangements for protecting computers, components thereof, programs or data against unauthorised activity
- G06F21/50—Monitoring users, programs or devices to maintain the integrity of platforms, e.g. of processors, firmware or operating systems
- G06F21/55—Detecting local intrusion or implementing counter-measures
- G06F21/56—Computer malware detection or handling, e.g. anti-virus arrangements
- G06F21/562—Static detection
- G06F21/563—Static detection by source code analysis
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/22—Indexing; Data structures therefor; Storage structures
- G06F16/2228—Indexing structures
- G06F16/2246—Trees, e.g. B+trees
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/24—Querying
- G06F16/242—Query formulation
- G06F16/2433—Query languages
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F21/00—Security arrangements for protecting computers, components thereof, programs or data against unauthorised activity
- G06F21/50—Monitoring users, programs or devices to maintain the integrity of platforms, e.g. of processors, firmware or operating systems
- G06F21/55—Detecting local intrusion or implementing counter-measures
- G06F21/554—Detecting local intrusion or implementing counter-measures involving event detection and direct action
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- Computer Security & Cryptography (AREA)
- Software Systems (AREA)
- General Physics & Mathematics (AREA)
- Computer Hardware Design (AREA)
- Databases & Information Systems (AREA)
- Data Mining & Analysis (AREA)
- Health & Medical Sciences (AREA)
- Virology (AREA)
- General Health & Medical Sciences (AREA)
- Mathematical Physics (AREA)
- Computational Linguistics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
- Computer And Data Communications (AREA)
Abstract
本发明涉及网络安全技术领域,为基于标记的SQL注入攻击主动防御方法、系统及存储介质。其方法包括:将源代码转换为抽象语法树,定义不可信变量;对抽象语法树中的变量自上至下逐行进行分析,分析所有变量的赋值引用关系,构建变量依赖关系,定位源代码中的所有不可信变量;对抽象语法树进行分析,判断数据库参数是否可信,判断SQL语句是否含有不可信变量;若SQL语句含有不可信变量,则对源代码进行修改,标记不可信变量;在数据库函数中加入对不可信变量过滤处理的代码;运行修改后的源代码,对数据库函数进行动态库拦截,识别出不可信输入的字符串部分,对不可信输入的字符串部分的检查和过滤,实现SQL注入攻击语句的运行失效。
The present invention relates to the field of network security technology, and is a method, system and storage medium for active defense against SQL injection attacks based on marking. The method comprises: converting source code into an abstract syntax tree, defining untrusted variables; analyzing the variables in the abstract syntax tree line by line from top to bottom, analyzing the assignment reference relationship of all variables, building variable dependency relationships, and locating all untrusted variables in the source code; analyzing the abstract syntax tree, judging whether the database parameters are credible, and judging whether the SQL statement contains untrusted variables; if the SQL statement contains untrusted variables, modifying the source code and marking the untrusted variables; adding code for filtering and processing untrusted variables in the database function; running the modified source code, intercepting the database function with a dynamic library, identifying the string part of the untrusted input, checking and filtering the string part of the untrusted input, and realizing the operation failure of the SQL injection attack statement.
Description
技术领域Technical Field
本发明涉及网络安全技术领域,为基于标记的SQL注入攻击主动防御方法、系统及存储介质。The present invention relates to the technical field of network security, and is a method, system and storage medium for actively defending against SQL injection attacks based on markers.
背景技术Background technique
结构化查询语言(StructuredQueryLanguage)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统,被广泛应用于类似MicrosoftAccess、DB2、Informix、MicrosoftSQLServer、Oracle、Sybase以及其他的数据库系统中。Structured Query Language (SQL) is a special-purpose programming language. It is a database query and programming language used to access data, query, update, and manage relational database systems. It is widely used in database systems such as Microsoft Access, DB2, Informix, Microsoft SQL Server, Oracle, Sybase, and others.
由于一些应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。Because some applications do not judge the legitimacy of user input data or do not filter it strictly, attackers can add additional SQL statements to the end of pre-defined query statements in the application, perform illegal operations without the administrator's knowledge, and thereby deceive the database server into executing unauthorized arbitrary queries, thereby further obtaining corresponding data information.
目前,代码注入攻击的安全防护方法主要源于程序分析和输入规则匹配两种技术思路,比如识别SQL语句中的危险符号并进行过滤;对程序中SQL语句中的关键字进行随机化处理,以达到和用户输入相区别的方法等。这些方法通常需要对利用方式、攻击行为进行分析,并采取对应的防护措施,但在面对新型的、未知的攻击行为或是当攻击者有防护系统的先验知识时存在着固有缺陷。At present, the security protection methods for code injection attacks mainly come from two technical ideas: program analysis and input rule matching, such as identifying dangerous symbols in SQL statements and filtering them; randomizing keywords in SQL statements in the program to distinguish them from user input, etc. These methods usually require analysis of the use method and attack behavior, and take corresponding protection measures, but there are inherent defects when facing new and unknown attack behaviors or when the attacker has prior knowledge of the protection system.
发明内容Summary of the invention
为了能够更好地应对未知攻击行为,实现主动防御,本发明提出了基于标记的SQL注入攻击主动防御方法、系统及存储介质,适用于多语言环境,利用污点分析的原理对服务器源代码进行分析,对不可信变量进行标记处理。In order to better deal with unknown attack behaviors and achieve active defense, the present invention proposes a tag-based SQL injection attack active defense method, system and storage medium, which are suitable for multi-language environments, use the principle of taint analysis to analyze the server source code, and mark untrusted variables.
本发明防御方法所采取的技术方案为:基于标记的SQL注入攻击主动防御方法,包括以下步骤:The technical solution adopted by the defense method of the present invention is: a tag-based SQL injection attack active defense method, comprising the following steps:
将源代码转换为抽象语法树,并定义一系列不可信变量,构建不可信变量列表;Convert the source code into an abstract syntax tree, define a series of untrusted variables, and build an untrusted variable list;
对抽象语法树中的变量自上至下逐行进行分析,分析抽象语法树中所有变量的赋值引用关系,构建变量依赖关系,从而定位源代码中的所有不可信变量,并将不可信变量记录在不可信变量列表中;Analyze the variables in the abstract syntax tree line by line from top to bottom, analyze the assignment reference relationship of all variables in the abstract syntax tree, build variable dependency relationships, and thus locate all untrusted variables in the source code, and record the untrusted variables in the untrusted variable list;
对不可信变量分析完毕后,再次对抽象语法树进行分析,寻找其中的数据库函数,对数据库函数中的参数进行分析,判断参数是否可信,进而判断SQL语句是否含有不可信变量;After the analysis of untrusted variables is completed, the abstract syntax tree is analyzed again to find the database function in it, and the parameters in the database function are analyzed to determine whether the parameters are credible, and then determine whether the SQL statement contains untrusted variables;
若SQL语句含有不可信变量,则对源代码进行修改,标记数据库函数参数中的不可信变量;If the SQL statement contains untrusted variables, modify the source code and mark the untrusted variables in the database function parameters;
对源代码修改完毕后,重新编写数据库函数,在数据库函数中加入对不可信变量过滤处理的代码;After modifying the source code, rewrite the database function and add the code for filtering untrusted variables to the database function;
当运行修改后的源代码时,对数据库函数进行动态库拦截,根据标记识别出SQL语句中来自不可信输入的字符串部分,并根据设定的过滤策略对不可信输入的字符串部分的检查和过滤,实现SQL注入攻击语句的运行失效。When the modified source code is run, the database function is intercepted by the dynamic library, the string part from the untrusted input in the SQL statement is identified according to the mark, and the string part of the untrusted input is checked and filtered according to the set filtering strategy, so as to achieve the failure of the operation of the SQL injection attack statement.
本发明防御系统所采取的技术方案为:基于标记的SQL注入攻击主动防御系统,包括:The technical solution adopted by the defense system of the present invention is: a SQL injection attack active defense system based on tags, including:
列表构建模块,用于将源代码转换为抽象语法树,并定义一系列不可信变量,构建不可信变量列表;A list building module, which is used to convert source code into an abstract syntax tree and define a series of untrusted variables to build an untrusted variable list;
变量分析模块,用于对抽象语法树中的变量自上至下逐行进行分析,分析抽象语法树中所有变量的赋值引用关系,构建变量依赖关系,从而定位源代码中的所有不可信变量,并将不可信变量记录在不可信变量列表中;The variable analysis module is used to analyze the variables in the abstract syntax tree line by line from top to bottom, analyze the assignment reference relationship of all variables in the abstract syntax tree, build variable dependency relationships, thereby locating all untrusted variables in the source code, and record the untrusted variables in the untrusted variable list;
参数分析判断模块,用于对不可信变量分析完毕后,再次对抽象语法树进行分析,寻找其中的数据库函数,对数据库函数中的参数进行分析,判断参数是否可信,进而判断SQL语句是否含有不可信变量;The parameter analysis and judgment module is used to analyze the abstract syntax tree again after the analysis of the untrusted variables is completed, find the database function in it, analyze the parameters in the database function, determine whether the parameters are credible, and then determine whether the SQL statement contains untrusted variables;
变量标记模块,用于当SQL语句含有不可信变量时,对源代码进行修改,标记数据库函数参数中的不可信变量;The variable marking module is used to modify the source code and mark the untrusted variables in the database function parameters when the SQL statement contains untrusted variables;
代码修改模块,用于对源代码修改完毕后,重新编写数据库函数,在数据库函数中加入对不可信变量过滤处理的代码;The code modification module is used to rewrite the database function after the source code is modified, and add the code for filtering untrusted variables to the database function;
动态库拦截模块,用于当运行修改后的源代码时,对数据库函数进行动态库拦截,根据标记识别出SQL语句中来自不可信输入的字符串部分,并根据设定的过滤策略对不可信输入的字符串部分的检查和过滤,实现SQL注入攻击语句的运行失效。The dynamic library interception module is used to intercept the database function in the dynamic library when running the modified source code, identify the string part from the untrusted input in the SQL statement according to the mark, and check and filter the string part of the untrusted input according to the set filtering strategy, so as to achieve the operation failure of the SQL injection attack statement.
本发明的存储介质,其上存储有计算机指令,当计算机指令被处理器执行时,实现本发明SQL注入攻击主动防御方法的各步骤。The storage medium of the present invention stores computer instructions thereon, and when the computer instructions are executed by a processor, each step of the SQL injection attack active defense method of the present invention is implemented.
本发明与现有技术相比,其取得的技术效果包括:Compared with the prior art, the present invention has the following technical effects:
1、本发明采取了静态分析方法对源码进行分析,对SQL语句中的不可信变量进行标记,从而可以准确定位SQL语句中的不可信变量;再根据标记定位不可信变量并动态过滤有害输入,防止SQL注入;避免了对可信变量的错误过滤,并且能更好地针对不可信变量进行过滤处理,提高了准确性。1. The present invention adopts a static analysis method to analyze the source code and mark the untrusted variables in the SQL statement, so that the untrusted variables in the SQL statement can be accurately located; then the untrusted variables are located according to the marks and harmful inputs are dynamically filtered to prevent SQL injection; the erroneous filtering of trusted variables is avoided, and the untrusted variables can be better filtered and processed, thereby improving the accuracy.
2、相对于传统的静态分析方法对所有SQL语句进行处理,本发明只对包含不可信变量的SQL语句进行处理,一定程度上减小了过滤处理的运行时开销。2. Compared with the traditional static analysis method that processes all SQL statements, the present invention only processes SQL statements containing untrusted variables, which reduces the runtime overhead of filtering processing to a certain extent.
3、本发明利用抽象语法树进行静态分析,该分析方法可以适用于多种语言。3. The present invention uses an abstract syntax tree to perform static analysis, and the analysis method can be applied to multiple languages.
4、本发明采用了动态库拦截的方式对数据库函数进行拦截,可以更好地适用于多语言复杂使用场景,拥有更强的普适性。4. The present invention adopts a dynamic library interception method to intercept database functions, which can be better applied to complex multi-language usage scenarios and has stronger universality.
附图说明BRIEF DESCRIPTION OF THE DRAWINGS
图1为本发明实施例中防御方法的流程图;FIG1 is a flow chart of a defense method according to an embodiment of the present invention;
图2为本发明实施例中不可信变量过滤模式的示意图;FIG2 is a schematic diagram of an untrusted variable filtering mode in an embodiment of the present invention;
图3为本发明实施例中防御系统的结构示意图。FIG3 is a schematic diagram of the structure of a defense system in an embodiment of the present invention.
具体实施方式Detailed ways
总的来说,本发明利用污点分析的原理对服务器源代码进行分析,对不可信变量进行标记处理。污点分析是一种跟踪并分析污点信息在程序中流动的技术。在漏洞分析中,使用污点分析技术将所感兴趣的数据标记为污点数据,然后通过跟踪和污点数据相关的信息的流向,可以知道它们是否会影响某些关键的程序操作,进而挖掘程序漏洞。In general, the present invention uses the principle of taint analysis to analyze the server source code and mark the untrusted variables. Taint analysis is a technology that tracks and analyzes the flow of taint information in the program. In vulnerability analysis, the taint analysis technology is used to mark the data of interest as tainted data, and then by tracking the flow of information related to the tainted data, it can be known whether they will affect certain key program operations, thereby mining program vulnerabilities.
本发明的主要过程为:首先将数据库应用的源代码转化为抽象语法树(Abstractsyntaxtree,AST),通过对AST进行静态分析和源代码修改,标记出SQL语句中使用的不可信输入变量;之后对程序中的数据库访问函数进行运行时动态库拦截(Hook),根据通用规则对SQL语句中标记出来的不可信输入字符串进行有效性判定和过滤,实现SQL注入攻击语句的运行失效。The main process of the present invention is: first, the source code of the database application is converted into an abstract syntax tree (AST), and the untrusted input variables used in the SQL statement are marked by static analysis of the AST and source code modification; then, the database access function in the program is intercepted by a runtime dynamic library (Hook), and the validity of the untrusted input string marked in the SQL statement is determined and filtered according to general rules, so as to achieve the operation failure of the SQL injection attack statement.
下面结合实施例和附图对本发明的实施方式做进一步的说明,但本发明的实施方式不限于此。The embodiments of the present invention are further described below in conjunction with examples and drawings, but the embodiments of the present invention are not limited thereto.
实施例1Example 1
本实施例提供的是基于标记的SQL注入攻击主动防御方法,如图1所示,包括以下步骤:This embodiment provides a method for actively defending against SQL injection attacks based on tags, as shown in FIG1 , including the following steps:
S1、将数据库应用的源代码转换为抽象语法树,并定义一系列污点源,如$_GET、$_POST等用户输入变量,将其设置为不可信变量,构建不可信变量列表。S1. Convert the source code of the database application into an abstract syntax tree and define a series of taint sources, such as user input variables such as $_GET and $_POST, set them as untrusted variables, and build an untrusted variable list.
S2、对抽象语法树中的变量自上至下逐行进行分析,分析抽象语法树中所有变量的赋值引用关系,构建变量依赖关系,也即跟踪污点数据的流动传递过程,从而定位源代码中所有不可信变量,并将不可信变量记录在不可信变量列表中。S2. Analyze the variables in the abstract syntax tree line by line from top to bottom, analyze the assignment reference relationship of all variables in the abstract syntax tree, and build variable dependencies, that is, track the flow and transmission process of tainted data, so as to locate all untrusted variables in the source code and record the untrusted variables in the untrusted variable list.
在不可信变量的定位过程中,可以从数据库访问函数开始搜索,找出构成SQL语句的字符串中引用的不可信变量并对其添加标签,实现对SQL语句的字符串中所有不可信输入的位置标记。In the process of locating untrusted variables, the search can be started from the database access function to find out the untrusted variables referenced in the string constituting the SQL statement and add labels to them, thereby marking the positions of all untrusted inputs in the string of the SQL statement.
如果一个变量由另一个不可信变量赋值而来,则该变量同样可视为不可信变量。例如“$name=$_GET[‘name’]”,$name变量由$_GET变量赋值而来,因此将其加入不可信变量列表。If a variable is assigned by another untrusted variable, then the variable is also considered an untrusted variable. For example, “$name=$_GET[‘name’]”, the $name variable is assigned by the $_GET variable, so it is added to the list of untrusted variables.
S3、对不可信变量分析完毕后,再次对抽象语法树进行分析,寻找其中的数据库函数,如mysqli_query函数,对数据库函数中的参数进行分析,判断参数是否可信,进而判断SQL语句是否含有不可信变量。S3. After the analysis of the untrusted variables is completed, the abstract syntax tree is analyzed again to find the database functions therein, such as the mysqli_query function, and the parameters in the database function are analyzed to determine whether the parameters are credible, and then determine whether the SQL statement contains untrusted variables.
数据库函数是SQL注入攻击的攻击点,恶意注入代码往往通过数据库函数传向数据库并执行相应操作,而数据库函数中的SQL语句通常由用户输入构成,是引发SQL注入攻击的根源。Database functions are the attack points of SQL injection attacks. Malicious injection codes are often transmitted to the database through database functions and perform corresponding operations. The SQL statements in database functions are usually composed of user input and are the root cause of SQL injection attacks.
对数据库函数中的SQL语句进行分析,如果为不可信变量,则说明该SQL语句依赖用户输入,为潜在攻击点。例如$sql=“select*fromtablewherename=‘$name’”,$sql为SQL语句,其由两部分构成,字符串常量select*fromtablewherename=‘’和不可信变量$name构成,因此该SQL语句含有不可信变量;如果SQL语句为可信变量,则说明该SQL语句为程序内置变量或常量构成,不会成为攻击点。Analyze the SQL statements in the database function. If it is an untrusted variable, it means that the SQL statement depends on user input and is a potential attack point. For example, $sql = "select*fromtablewherename = '$name'", $sql is a SQL statement, which consists of two parts, the string constant select*fromtablewherename = '' and the untrusted variable $name, so the SQL statement contains an untrusted variable; if the SQL statement is a trusted variable, it means that the SQL statement is composed of program built-in variables or constants and will not become an attack point.
S4、若SQL语句含有不可信变量,则对源代码进行修改,标记数据库函数参数中的不可信变量。S4. If the SQL statement contains untrusted variables, modify the source code and mark the untrusted variables in the database function parameters.
如果SQL语句含有不可信变量,则需要对SQL语句进行处理,对程序(即源代码)进行修改,利用进程ID作标签,标记其中的用户输入部分,构成新的SQL语句,例如上述变量$sql,对其进行处理后可以得到$sql=“select*fromtablewherename=‘<$pid>$name<$pid>’”,其中<$pid><$pid>为标签,$pid为利用进程ID生成的变量。当运行代码时,会生成新进程,进程ID每次都是随机生成的,因此可以有效避免攻击者利用先验知识对防御系统进行绕过欺骗。If the SQL statement contains untrusted variables, the SQL statement needs to be processed, the program (i.e. source code) needs to be modified, and the process ID is used as a label to mark the user input part to form a new SQL statement. For example, the above variable $sql can be processed to obtain $sql = "select * from table where name = '<$pid>$name<$pid>'", where <$pid><$pid> are labels and $pid is a variable generated using the process ID. When the code is run, a new process is generated, and the process ID is randomly generated each time, so it can effectively prevent attackers from using prior knowledge to bypass and deceive the defense system.
S5、对源代码修改完毕后,表示静态分析部分已完成;然后重新编写数据库函数,在数据库函数中加入对不可信变量过滤处理的代码。S5. After the source code is modified, it means that the static analysis part is completed; then the database function is rewritten, and the code for filtering untrusted variables is added to the database function.
其中,过滤处理的过程包括:The filtering process includes:
利用当前进程ID生成标签;Generate a label using the current process ID;
利用生成的标签定位数据库函数中SQL语句的不可信变量;Use the generated tags to locate untrusted variables in SQL statements in database functions;
对不可信变量按照设定的过滤策略进行过滤,除去其中可能的恶意代码;Filter untrusted variables according to the set filtering strategy to remove possible malicious code;
其中,设定的过滤策略为:通过对互联网中存在的常见SQL注入攻击代码进行总结,得到一套通用的SQL注入攻击的语句构成模式,比如一个攻击语句往往都会包含注释符、SQL关键字(例如INSERT、DELETE等)等;基于所述SQL注入攻击的语句构成模式,利用决策树对SQL语句的输入长度、特殊符号等信息进行检查,实现对不可信变量的过滤,从而除去其中可能的恶意代码;一个简单的过滤模式如图2所示,用户可以根据程序生成的过滤日志对过滤情况进行检查,并适当的修改过滤策略,以减小误报率和漏报率;Among them, the set filtering strategy is: by summarizing the common SQL injection attack codes existing in the Internet, a set of common SQL injection attack statement composition patterns are obtained, for example, an attack statement often contains comment symbols, SQL keywords (such as INSERT, DELETE, etc.), etc.; based on the SQL injection attack statement composition pattern, the decision tree is used to check the input length, special symbols and other information of the SQL statement to filter untrusted variables, thereby removing possible malicious codes; a simple filtering mode is shown in Figure 2, and the user can check the filtering situation according to the filtering log generated by the program, and appropriately modify the filtering strategy to reduce the false alarm rate and the missed alarm rate;
利用过滤后的不可信变量构成新的SQL语句;Use the filtered untrusted variables to form new SQL statements;
重新调用系统的数据库函数,将SQL语句传给SQL引擎,以执行相应数据库操作。Recall the system's database function and pass the SQL statement to the SQL engine to perform the corresponding database operation.
S6、当运行修改后的源代码时,实现数据库访问函数的运行时拦截,即对数据库函数进行动态库拦截,根据标记识别出SQL语句中来自不可信输入的字符串部分,并根据设定的过滤策略对不可信输入字符串部分的检查和过滤,实现SQL注入攻击语句的运行失效。S6. When the modified source code is run, runtime interception of the database access function is implemented, that is, dynamic library interception of the database function is performed, the string part from the untrusted input in the SQL statement is identified according to the mark, and the untrusted input string part is checked and filtered according to the set filtering strategy, so as to achieve the operation failure of the SQL injection attack statement.
当对数据库函数进行动态库拦截时,可以使每次调用数据库函数时调用重新编写的数据库函数而非系统的数据库函数,使得SQL语句每次都可以得到过滤处理,从而可以较好地避免SQL注入攻击。When dynamic library interception is performed on database functions, the rewritten database function can be called instead of the system database function each time the database function is called, so that the SQL statement can be filtered and processed each time, thereby better avoiding SQL injection attacks.
本步骤通过动态库拦截的方式,一方面可以在不对系统数据库函数进行修改的条件下对输入进行过滤,避免用户修改系统数据库函数造成未知影响,使得系统稳定性、便捷性得到提升。另一方面,由于大部分脚本语言如Python、PHP等为了提高运行速度,会调用C语言动态库进行处理,因此动态库拦截可以在只修改拦截C语言动态库的情况下,实现同时对多种语言的处理,因此能够适用于更加复杂的使用场景,提升了本发明技术方案的普适性。This step uses the method of dynamic library interception. On the one hand, it can filter the input without modifying the system database function, avoiding unknown impact caused by user modification of the system database function, so that the stability and convenience of the system are improved. On the other hand, since most scripting languages such as Python and PHP will call the C language dynamic library for processing in order to increase the running speed, the dynamic library interception can realize the processing of multiple languages at the same time while only modifying and intercepting the C language dynamic library, so it can be applied to more complex usage scenarios, which improves the universality of the technical solution of the present invention.
例如用户输入一段恶意代码,使得用户输入变量$name=“’or1=1#”,如果不对该SQL语句进行修改过滤,传入数据库函数中的SQL语句为select*fromtablewherename=‘’or1=1#’,该SQL语句为重言式,执行该语句可以获取table表中的所有数据而不需要输入正确的参数,从而完成了一次SQL攻击。而进行代码修改之后传入数据库函数中的SQL语句为select*fromtablewherename=‘<$pid>’or1=1#<$pid>’,对数据库函数进行动态库拦截,并根据标签对内部内容进行过滤,可以得到新的SQL语句select*fromtablewherename=‘”,完成了对SQL语句中的恶意代码的过滤,从而避免了SQL攻击的发生。For example, if a user enters a malicious code, such that the user enters a variable $name = "'or1 = 1#", if the SQL statement is not modified and filtered, the SQL statement passed into the database function is select*fromtablewherename = ''or1 = 1#', which is a tautology. Executing this statement can obtain all the data in the table without entering the correct parameters, thus completing an SQL attack. After the code is modified, the SQL statement passed into the database function is select*fromtablewherename = '<$pid>'or1 = 1#<$pid>', and the database function is intercepted by the dynamic library, and the internal content is filtered according to the label, and a new SQL statement select*fromtablewherename = '" can be obtained, completing the filtering of malicious code in the SQL statement, thereby avoiding the occurrence of SQL attacks.
实施例2Example 2
本实施例与实施例1基于相同的发明构思,提供的是基于标记的SQL注入攻击主动防御系统,包括以下模块:This embodiment is based on the same inventive concept as the first embodiment, and provides a tag-based SQL injection attack active defense system, including the following modules:
列表构建模块,用于将源代码转换为抽象语法树,并定义一系列不可信变量,构建不可信变量列表;A list building module, which is used to convert source code into an abstract syntax tree and define a series of untrusted variables to build an untrusted variable list;
变量分析模块,用于对抽象语法树中的变量自上至下逐行进行分析,分析抽象语法树中所有变量的赋值引用关系,构建变量依赖关系,从而定位源代码中的所有不可信变量,并将不可信变量记录在不可信变量列表中;The variable analysis module is used to analyze the variables in the abstract syntax tree line by line from top to bottom, analyze the assignment reference relationship of all variables in the abstract syntax tree, build variable dependency relationships, thereby locating all untrusted variables in the source code, and record the untrusted variables in the untrusted variable list;
参数分析判断模块,用于对不可信变量分析完毕后,再次对抽象语法树进行分析,寻找其中的数据库函数,对数据库函数中的参数进行分析,判断参数是否可信,进而判断SQL语句是否含有不可信变量;The parameter analysis and judgment module is used to analyze the abstract syntax tree again after the analysis of the untrusted variables is completed, find the database function in it, analyze the parameters in the database function, determine whether the parameters are credible, and then determine whether the SQL statement contains untrusted variables;
变量标记模块,用于当SQL语句含有不可信变量时,对源代码进行修改,标记数据库函数参数中的不可信变量;The variable marking module is used to modify the source code and mark the untrusted variables in the database function parameters when the SQL statement contains untrusted variables;
代码修改模块,用于对源代码修改完毕后,重新编写数据库函数,在数据库函数中加入对不可信变量过滤处理的代码;The code modification module is used to rewrite the database function after the source code is modified, and add the code for filtering untrusted variables to the database function;
动态库拦截模块,用于当运行修改后的源代码时,对数据库函数进行动态库拦截,根据标记识别出SQL语句中来自不可信输入的字符串部分,并根据设定的过滤策略对不可信输入的字符串部分的检查和过滤,实现SQL注入攻击语句的运行失效。The dynamic library interception module is used to intercept the database function in the dynamic library when running the modified source code, identify the string part from the untrusted input in the SQL statement according to the mark, and check and filter the string part of the untrusted input according to the set filtering strategy, so as to achieve the operation failure of the SQL injection attack statement.
其中,变量分析模块在不可信变量的定位过程中,从数据库访问函数开始搜索,找出构成SQL语句的字符串中引用的不可信变量并对其添加标签,实现对SQL语句的字符串中所有不可信输入的位置标记。Among them, in the process of locating untrusted variables, the variable analysis module starts searching from the database access function, finds out the untrusted variables referenced in the string constituting the SQL statement and adds labels to them, thereby marking the positions of all untrusted inputs in the string of the SQL statement.
而代码修改模块在数据库函数中加入的代码对过滤处理的过程包括:The code modification module adds the code to the database function to filter the process including:
利用当前进程ID生成标签;Generate a label using the current process ID;
利用生成的标签定位数据库函数中SQL语句的不可信变量;Use the generated tags to locate untrusted variables in SQL statements in database functions;
对不可信变量按照设定的过滤策略进行过滤,除去其中可能的恶意代码;Filter untrusted variables according to the set filtering strategy to remove possible malicious code;
利用过滤后的不可信变量构成新的SQL语句;Use the filtered untrusted variables to form new SQL statements;
重新调用系统的数据库函数,将SQL语句传给SQL引擎,以执行相应数据库操作。Recall the system's database function and pass the SQL statement to the SQL engine to perform the corresponding database operation.
代码修改模块中,设定的过滤策略为:In the code modification module, the filtering strategy is set as follows:
通过对互联网中存在的常见SQL注入攻击代码进行总结,得到通用的SQL注入攻击的语句构成模式;基于所述SQL注入攻击的语句构成模式,利用决策树对SQL语句的输入长度、特殊符号信息进行检查,实现对不可信变量的过滤。By summarizing the common SQL injection attack codes existing on the Internet, a common SQL injection attack statement composition pattern is obtained; based on the SQL injection attack statement composition pattern, a decision tree is used to check the input length and special symbol information of the SQL statement to filter out untrusted variables.
如图3所示,本实施例的防御系统在实际应用中,变量分析模块、参数分析判断模块和变量标记模块可呈现为源代码静态分析与标记工具,实现数据库应用的原始源代码的分析与不可信变量的标记,得到数据库应用标记后源代码;代码修改模块对标记后源代码进行编译执行或解释执行;动态库拦截模块生成包含过滤规则的数据库函数动态库,并使其调用优先级高于系统动态库,使得应用程序在调用数据库函数时优先调用自己编写的动态库,从而利用编写的数据库函数实现对输入的过滤。As shown in Figure 3, in actual applications of the defense system of this embodiment, the variable analysis module, the parameter analysis and judgment module, and the variable marking module can be presented as source code static analysis and marking tools to analyze the original source code of the database application and mark untrusted variables, and obtain the marked source code of the database application; the code modification module compiles or interprets the marked source code; the dynamic library interception module generates a database function dynamic library containing filtering rules, and makes its calling priority higher than the system dynamic library, so that the application program gives priority to calling the dynamic library written by itself when calling the database function, thereby using the written database function to filter the input.
本实施例还提供存储介质,其上存储有计算机指令,当计算机指令被处理器执行时,实现实施例1中SQL注入攻击主动防御方法的各步骤。This embodiment also provides a storage medium on which computer instructions are stored. When the computer instructions are executed by a processor, each step of the SQL injection attack active defense method in Embodiment 1 is implemented.
以上所述是本发明的优选实施方式,应当指出,对于本技术领域的普通技术人来说,在不脱离本发明所述原理的前提下,还可以做出若干改进和润饰,这些改进和润饰应视为本发明的保护范围。The above is a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present invention. These improvements and modifications should be regarded as within the scope of protection of the present invention.
Claims (10)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210438587.9A CN114936369B (en) | 2022-04-25 | 2022-04-25 | Active defense method, system and storage medium for SQL injection attacks based on markers |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202210438587.9A CN114936369B (en) | 2022-04-25 | 2022-04-25 | Active defense method, system and storage medium for SQL injection attacks based on markers |
Publications (2)
Publication Number | Publication Date |
---|---|
CN114936369A CN114936369A (en) | 2022-08-23 |
CN114936369B true CN114936369B (en) | 2024-04-19 |
Family
ID=82861891
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202210438587.9A Active CN114936369B (en) | 2022-04-25 | 2022-04-25 | Active defense method, system and storage medium for SQL injection attacks based on markers |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN114936369B (en) |
Families Citing this family (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN117251477B (en) * | 2023-11-17 | 2024-02-20 | 吉贝克信息技术(北京)有限公司 | Standardized inspection method and system for data development script based on SQL (structured query language) analysis |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106355094A (en) * | 2016-07-08 | 2017-01-25 | 耿童童 | SQL (structured query language) injection attack defensive system and defensive method based on grammar transformation |
CN108712448A (en) * | 2018-07-09 | 2018-10-26 | 四川大学 | A kind of injection attack detection model based on the analysis of dynamic stain |
CN108875366A (en) * | 2018-05-23 | 2018-11-23 | 四川大学 | A kind of SQL injection behavioral value system towards PHP program |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US8484232B2 (en) * | 2005-11-22 | 2013-07-09 | International Business Machines Corporation | Method, computer arrangement, computer program and computer program product for checking for the presence of control statements in a data value |
-
2022
- 2022-04-25 CN CN202210438587.9A patent/CN114936369B/en active Active
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106355094A (en) * | 2016-07-08 | 2017-01-25 | 耿童童 | SQL (structured query language) injection attack defensive system and defensive method based on grammar transformation |
CN108875366A (en) * | 2018-05-23 | 2018-11-23 | 四川大学 | A kind of SQL injection behavioral value system towards PHP program |
CN108712448A (en) * | 2018-07-09 | 2018-10-26 | 四川大学 | A kind of injection attack detection model based on the analysis of dynamic stain |
Non-Patent Citations (1)
Title |
---|
基于敏感字符的SQL注入攻击防御方法;张慧琳;丁羽;张利华;段镭;张超;韦韬;李冠成;韩心慧;;计算机研究与发展;20161015(第10期);全文 * |
Also Published As
Publication number | Publication date |
---|---|
CN114936369A (en) | 2022-08-23 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US9715593B2 (en) | Software vulnerabilities detection system and methods | |
Xie et al. | Static Detection of Security Vulnerabilities in Scripting Languages. | |
Halfond et al. | Preventing SQL injection attacks using AMNESIA | |
Dahse et al. | Simulation of Built-in PHP Features for Precise Static Code Analysis. | |
CN101661543B (en) | Method and device for detecting security flaws of software source codes | |
Jang et al. | Detecting SQL injection attacks using query result size | |
Yu et al. | Automata-based symbolic string analysis for vulnerability detection | |
Sadeghian et al. | A taxonomy of SQL injection detection and prevention techniques | |
CN112651028B (en) | Vulnerable code clone detection method based on context semantics and patch verification | |
US20170017789A1 (en) | High Performance Software Vulnerabilities Detection System and Methods | |
Amirtahmasebi et al. | A survey of SQL injection defense mechanisms | |
CN115906086A (en) | Method, system and storage medium for detecting webpage backdoor based on code attribute graph | |
Yan et al. | Detection method of the second-order SQL injection in Web applications | |
US9600644B2 (en) | Method, a computer program and apparatus for analyzing symbols in a computer | |
Cetin et al. | SQL-Identifier injection attacks | |
CN114936369B (en) | Active defense method, system and storage medium for SQL injection attacks based on markers | |
Livshits et al. | SecuriFly: Runtime protection and recovery from Web application vulnerabilities | |
Chenyu et al. | Defending SQL injection attacks based-on intention-oriented detection | |
US20070185703A1 (en) | Method, Computer Program and Apparatus for Analysing Symbols in a Computer System | |
Ali et al. | Review of the defensive approaches for structured query language injection attacks and their countermeasures | |
CN110647749A (en) | Second-order SQL injection attack defense method | |
CN113139183B (en) | Detection method, detection device, detection equipment and storage medium | |
Wu et al. | Test SQL injection vulnerabilities in web applications based on structure matching | |
Perkins et al. | AutoRand: Automatic keyword randomization to prevent injection attacks | |
Sivaraman et al. | Detecting and Fixing SQL Injection and Cross-Site Scripting Vulnerabilities in Web Applications |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |