Structured query statement injection protection method and device

By detecting the structured query statement of the database under the real-time application self-protection environment, generating a token list and performing lexical semantic analysis, the problems of poor SQL injection protection performance and high false alarm rate in the existing technology are solved, real-time detection is realized and protection, improving database security and management efficiency.

CN118761057BActive Publication Date: 2024-12-03BEIJING SHENGXIN NETWORK TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202411224022.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-03
Publication Date
2024-12-03
Estimated Expiration
2044-09-03

AI Technical Summary

Technical Problem

The prior art has poor performance, high false alarm rate when protecting SQL injection, and limited database adaptation, so real-time detection and protection cannot be achieved.

Method used

By controlling the operation of the application in a real-time application self-protection environment, obtaining and detecting the structured query statements of the database, generating a token list, and performing lexical semantic analysis, real-time processing and automated detection of structured query statements are realized.

Benefits of technology

It improves database security and management efficiency, reduces operation and maintenance costs, realizes real-time detection and protection of SQL injection, and reduces false alarm rates.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118761057B_ABST
    Figure CN118761057B_ABST
Patent Text Reader

Abstract

The present disclosure provides a method and device for protecting against injection of structured query statements in an application, and relates to the field of database security technology. The specific implementation scheme is: controlling the application to run in a real-time application self-protection environment; obtaining the structured query statement for the application to access the database in the relevant interface class for the application to connect to the database; obtaining a token list based on the structured query statement; detecting the token list and the structured query statement to obtain the detection result of the structured query statement; and processing the structured query statement based on the detection result. This implementation method realizes the automatic detection of structured query statement injection, improves database security and database management efficiency, and reduces operation and maintenance costs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of software security technology, and in particular to a structured query statement injection protection method and device, an electronic device, and a computer-readable storage medium. Background Art

[0002] SQL (Structured Query Language) injection is to add SQL statements to the string input by the user. If the check is omitted in a poorly designed program, the injected SQL statements will be mistaken by the database server as normal SQL statements and run, allowing the attacker to execute unplanned commands or access unauthorized data.

[0003] The current means of SQL injection protection is to detect HTTP (Hypertext Transfer Protocol) requests and various parameters of HTTP requests. The false alarm rate is high and real-time detection and protection cannot be achieved.

[0004] The information disclosed in this background technology section is only intended to enhance the understanding of the overall background of the disclosure and should not be regarded as an acknowledgement or any form of suggestion that the information constitutes the prior art already known to a person skilled in the art. Summary of the invention

[0005] The present disclosure provides a structured query statement injection protection method and device, an electronic device, and a computer-readable storage medium.

[0006] According to the first aspect, a structured query statement injection protection method is provided, the method comprising: controlling an application to run in a real-time application self-protection environment; obtaining a structured query statement for an application to access a database in a related interface class for the application to connect to the database; obtaining a token list based on the structured query statement; detecting the token list and the structured query statement to obtain a detection result of the structured query statement; and processing the structured query statement based on the detection result.

[0007] According to the second aspect, a structured query statement injection protection device is provided, which includes: a control unit, configured to control an application to run in a real-time application self-protection environment; an acquisition unit, configured to acquire, in a relevant interface class of the application connecting to the database, a structured query statement for the application to access a database; a obtaining unit, configured to obtain a token list based on the structured query statement; a detection unit, configured to detect the token list and the structured query statement to obtain a detection result of the structured query statement; and a processing unit, configured to process the structured query statement based on the detection result.

[0008] According to a third aspect, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor, wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method described in any implementation manner of the first aspect.

[0009] According to a fourth aspect, a non-transitory computer-readable storage medium storing computer instructions is provided, where the computer instructions are used to cause a computer to execute the method described in any implementation of the first aspect.

[0010] The structured query statement injection protection method and device provided by the embodiment of the present disclosure firstly controls the application to run in a real-time application self-protection environment; secondly, obtains the structured query statement for the application to access the database in the relevant interface class for the application to connect to the database; thirdly, obtains the token list based on the structured query statement; then, detects the token list and the structured query statement to obtain the detection result of the structured query statement; finally, processes the structured query statement based on the detection result. Thus, by obtaining the structured query statement through the relevant interface class, the real-time processing effect of the structured query statement can be achieved; based on the token list and the structured query statement, the automatic detection of the structured query statement injection is realized, which improves the database security and database management efficiency and reduces the operation and maintenance cost.

[0011] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present disclosure.

[0013] Figure 1 It is a flowchart of an embodiment of a structured query statement injection protection method according to the present disclosure;

[0014] Figure 2 It is a structural schematic diagram of an embodiment of a structured query statement injection protection device according to the present disclosure;

[0015] Figure 3 It is a block diagram of an electronic device used to implement the structured query statement injection protection method of the embodiment of the present disclosure. DETAILED DESCRIPTION

[0016] Unless explicitly stated otherwise, throughout the specification and claims, the term “comprise” or variations such as “include” or “comprising”, etc., will be understood to include the stated elements or components but not to exclude other elements or components.

[0017] The technical solution of the present disclosure is described below by means of specific embodiments. It should be understood that one or more steps mentioned in the present disclosure do not exclude the existence of other methods and steps before and after the combination step, or other methods and steps may be inserted between these explicitly mentioned steps. It should also be understood that these examples are only used to illustrate the present disclosure and are not used to limit the scope of the present disclosure. Unless otherwise specified, the numbering of each method step is only for the purpose of identifying each method step, and does not limit the order of arrangement of each method or limit the scope of implementation of the present disclosure. Changes or adjustments in their relative relationships can also be regarded as the scope of implementation of the present disclosure without substantial changes in the technical content.

[0018] The sources of the raw materials and instruments used in the examples are not particularly limited and can be purchased from the market or prepared according to conventional methods known to those skilled in the art.

[0019] SQL (Structured Query Language) injection is a security vulnerability that occurs at the database layer of a program. It is the most common and simplest vulnerability in applications. The main reason for this vulnerability is that the program does not judge and process the legitimacy of user input data, which allows attackers to add additional SQL statements to the SQL statements pre-defined 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 data information.

[0020] Traditional structured query injection protection methods have the following problems:

[0021] Poor performance: openrasp (Open Runtime Application Self-Protection) performs detection through the jni (Java Native Interface) and js (JavaScript, a scripting language) engines, and its performance is not as good as that of the native Java language.

[0022] High false positive and false negative rates: rules need to be updated frequently, making it difficult to detect unknown threats. libinjection usually performs very simple lexical analysis on the values ​​of each field in the request, with only a dozen tokens and matching rules. It does not determine whether the request will execute SQL and what kind of SQL will be executed. rasp detects the location where specific SQL statements are executed, and can specifically locate what kind of SQL is executed and the specific location of the SQL injection vulnerability.

[0023] Limited database adaptability: Openrasp supports limited databases.

[0024] In view of the above defects, the present invention proposes a structured query statement injection protection method, which detects the security risks of structured query statements by performing comprehensive lexical and semantic detection on structured query statements, thereby improving data security and server stability. Figure 1 A process 100 of an embodiment of a structured query statement injection protection method according to the present disclosure is shown. The structured query statement injection protection method comprises the following steps:

[0025] Step 101, controlling the application to run in a real-time application self-protection environment.

[0026] In this embodiment, the application is a computer program running on a computer or smart device, such as enterprise management, data analysis, medical diagnosis, education and training programs. The real-time application self-protection environment refers to the operating environment where the RASP (Runtime application self-protection) program is installed. RASP, as an attack behavior detection method, runs on the execution subject and the service process of the application program at the same time, and detects or blocks the structured query statements in the application.

[0027] In this embodiment, when the application is implemented in JAVA language, the above step 101 includes:

[0028] Based on the JavaAgent in JDK (Java Development Kit, a software development kit for the Java language), also known as the Java probe, RASP is installed into the Java application through dynamic or static installation.

[0029] For example: the above-mentioned Java application is an educational training program. Installing the RASP program into the Java application will enable the Java application to run in a real-time application self-protection environment. The Java application will be protected by the RASP program. When the structured query statement in the Java application is abnormal, the RASP program will give an alarm message.

[0030] Step 102: Obtain a structured query statement for the application to access the database from a relevant interface class for the application to connect to the database.

[0031] In this embodiment, when the application accesses the database, there will be a structured query statement for accessing the database in the parameters of the relevant interface class of the application connecting to the database; in order to obtain the structured query statement, the parameters of the relevant interface class of the application connecting to the database can be directly obtained through HOOK technology, and the structured query statement in the parameter can be extracted.

[0032] In this embodiment, when the application is implemented in Java language, the relevant interface class for the application to connect to the database can be a JDBC interface, and the structured query statement for the application to access the database can be obtained from the JDBC interface. Among them, JDBC is a Java API for executing SQL statements, which can provide unified access to multiple relational databases. It consists of a group of classes and interfaces written in Java language. Java provides the interface specification of JDBC. The corresponding database manufacturer develops the driver class library according to the specification and provides it to developers. The corresponding driver class library can be obtained from the official website of the corresponding database manufacturer. The application and the data interact in the way of JDBC bridging. In the application, the proxy class can be added to the implementation class of the JDBC interface specification through the HOOK technology, and the structured query statement for the application to access the database can be obtained in the proxy class. An interceptor can also be added to the application, and the structured query statement for the application to access the database can be obtained through the interceptor in the relevant interface method for executing SQL statements.

[0033] In this embodiment, the class that implements the JDBC interface is hooked through the HOOK technology, and all database drivers that comply with the JDBC specification can be supported, with strong compatibility.

[0034] Step 103: Get a token list based on the structured query statement.

[0035] In this embodiment, the token list includes multiple tokens, each token represents a grammatical analysis result of an element of a structured query statement, and the element is a comment / keyword (SELECT, UPDATE) / operator / space / literal (text or number enclosed in quotation marks, such as "word", 1234) / identifier in the structured query statement, etc.

[0036] In this embodiment, the token list can be obtained in a variety of ways. For example, one way is: splitting the structured query statement into elements to obtain element groups, and for each element in the element group, obtaining the token of the element by querying a pre-calibrated table, wherein the table is used to represent the correspondence between each element and the corresponding token; another way is: inputting the structured query statement into a grammatical analysis model to obtain the token list output by the grammatical analysis model, wherein the grammatical analysis model is a pre-trained model for performing grammatical analysis on structured query statements.

[0037] Step 104: Detect the token list and the structured query statement to obtain a detection result of the structured query statement.

[0038] In this embodiment, the detection result of the structured query statement is a result that characterizes whether an abnormal statement is injected into the structured query statement. When the structured query statement has no abnormal statement injection, the detection result obtained is that the structured query statement is normal; when the structured query statement has an abnormal statement injection, the detection result obtained is that the structured query statement is abnormal.

[0039] In this embodiment, the token list can be detected in a variety of ways to obtain the detection result of the structured query statement; for example, the token list and the structured query statement are detected to obtain the detection result of the structured query statement, including: inputting the token list into the token detection model to obtain the detection result output by the token analysis model, wherein the token detection model is a pre-trained model for detecting tokens. Optionally, the token list and the structured query statement are detected to obtain the detection result of the structured query statement, including: determining a token detection template based on the structure of the structured query statement; matching the token list with the token detection template, and in response to a successful match, determining that the detection result of the structured query statement is: the structured query statement is normal. Among them, the token detection template is token information pre-set for structured query statements of various different structures.

[0040] Step 105: Process the structured query statement based on the detection result.

[0041] In this embodiment, the detection result may indicate that the structured query statement is an abnormal statement. For the detection result indicating that the structured query statement is an abnormal statement, an alarm message may be generated and reported to the server, and the structured query statement will not be executed subsequently.

[0042] Optionally, the detection result may also indicate that the structured query statement is a normal statement. If the detection result indicates that the structured query statement is a normal statement, the structured query statement is directly executed.

[0043] In response to the high false alarm rate of OpenRASP in traditional solutions, the structured query statement injection protection method provided by the present invention can not only detect and protect in real time, but also solve the blocking protection problem of SQL injection that cannot be enabled due to high false alarm rate through the SQL fingerprint whitelist solution.

[0044] The structured query statement injection protection method provided by the embodiment of the present disclosure firstly controls the application to run in a real-time application self-protection environment; secondly, obtains the structured query statement for the application to access the database in the relevant interface class for the application to connect to the database; thirdly, obtains the token list based on the structured query statement; then, detects the token list and the structured query statement to obtain the detection result of the structured query statement; finally, processes the structured query statement based on the detection result. Thus, by obtaining the structured query statement through the relevant interface class, the real-time processing effect of the structured query statement can be achieved; based on the token list and the structured query statement, the automatic detection of the structured query statement injection is realized, which improves the database security and database management efficiency and reduces the operation and maintenance cost.

[0045] In some optional implementations of the present disclosure, the above-mentioned detection of the token list to obtain the detection result of the structured query statement includes: detecting whether the token list conforms to preset lexical and semantic rules; in response to detecting that the token list does not conform to the preset lexical and semantic rules, detecting whether the structured query statement includes a statement or parameter input by the user; in response to detecting that the structured query statement does not include a statement or parameter input by the user, determining that the detection result of the structured query statement is that the structured query statement is a normal statement.

[0046] In this optional implementation, the lexical semantic rules are detection rules set for the token list. The grammatical semantic rules can be used to detect whether the tokens in the token list include defects such as sensitive parameters, malicious functions, stack injections, and short-circuit expressions. When the token list does not conform to the grammatical semantic rules, it is determined that the tokens in the token list do not have defects and the structured query statement is a normal statement; when the token list conforms to the grammatical semantic rules, it is determined that the tokens in the token list have defects and the structured query statement is an abnormal statement.

[0047] In this optional implementation, the lexical semantic rules include: syntax short-circuiting sub-rules, comment truncation sub-rules, stacked injection sub-rules, joint query injection sub-rules, hexadecimal string bypass injection sub-rules, oversized token sub-rules, export data to file sub-rules, and malicious function blacklist sub-rules; when the token list meets any sub-rule in the lexical semantic rules, it is determined that the token list meets the lexical semantic rules; when the token list does not meet all sub-rules in the lexical semantic rules, it is determined that the token list does not meet the lexical semantic rules.

[0048] In this optional implementation, the syntax short-circuit sub-rule refers to whether there is a short-circuit expression that is always true or always false. The main feature is that both the left and right sides of the operator are literals. For example, OR '123' = '123', this expression with both the left and right sides being literals is generally meaningless, either always true or always false, and can exclude the common scenario of where 1=1 added after where for the convenience of splicing SQL; SQL injection may use syntax short-circuit conditions to bypass some queries.

[0049] In this optional implementation, the comment truncation sub-rule means that the end of the SQL statement is a comment, and whether the SQL statement is a comment can be determined through the token list.

[0050] In this optional implementation, the stacked injection sub-rule means that the SQL statement includes multiple SQL statements (that is, including separators), excluding common batch insert tablex ...; insert tablex ... or batch update tablex ...; update tablex ....

[0051] In this optional implementation, the union query injection sub-rule refers to a sub-rule that includes the UNION keyword and has a continuous literal after the UNION keyword, that is, the token list includes the UNION keyword and has a sensitive token or sensitive function after the UNION keyword. The sensitive token and sensitive function after the UNION keyword can be modified by the server and sent to RASP. Specifically, common sensitive tokens are: "information_schema", "schemata", "schema_name", "table_schema",

[0052] ,

[0053] Common sensitive functions after UNION are:

[0054] "version","user","database","system_user","session_user","current_user","password","benchmark","sleep","from_base64","to_base64","sys_context","database","user","current_user","get_host_add ress","get_host_name","receive_message","sleep","benchmark","current_setting","current_database","pg_sleep","host_name","user_name","suser_name","is_member","db_name","object_id","col_name".

[0055] In this optional implementation, the hexadecimal string bypassing injection sub-rule refers to a hexadecimal string starting with 0x.

[0056] In this optional implementation, the oversized token sub-rule refers to detecting whether there is a token greater than a preset length. The tokens of normal SQL statements are relatively short, such as very short keywords, and the concatenation of general SQL statements rarely includes large strings. In some SQL injection bypass scenarios, an oversized string may be entered. By detecting whether the token in the SQL statement is greater than the preset length, that is, the token exceeds the oversized string, SQL statement injection can be effectively excluded. The preset length can be configured based on demand. For example, the preset length defaults to 1024 and is configured by the server.

[0057] In this optional implementation, the sub-rule of exporting data to a file refers to detecting whether the SQL statement includes a command for exporting a file, and the command may be: into outfile or into dumpfile.

[0058] In this optional implementation, the malicious function blacklist sub-rule refers to detecting whether a specific function in an SQL statement is in the blacklist function. Common blacklist functions include:

[0059] "sleep","pg_sleep","benchmark","load_file","updatexml","extractvalue","is_srvrolemember","system", "pg_read_file","get_host_address","get_host_name","sys_exec","receive_message".

[0060] Specifically, the malicious function blacklist sub-rule is to detect whether the string of the previous token is in the malicious blacklist function list when the current token is a left bracket "(". For example, an SQL statement is: SELECT FROM users WHEREusername = 'exampleUser' AND SLEEP(5) -- It contains SLEEP(5). When the left bracket is detected, the string of the previous token is SLEEP, which is converted to lowercase to sleep. Sleep is in the blacklist function configuration list, so the rule is matched.

[0061] In this optional implementation, whether the structured query statement includes the statement or parameter input by the user can be directly obtained through a conventional identification method, and the acquisition of the statement or parameter input by the user will not be described in detail here.

[0062] In this optional implementation, the token list is a list obtained from the structured query statement. When the structured query statement contains a statement or parameter input by the user, there is a corresponding token corresponding to the statement or parameter input by the user.

[0063] The optional implementation of the present disclosure provides a method for obtaining the detection result of a structured query statement, which detects whether a token list conforms to a preset lexical semantic rule; after detecting that the token list does not conform to the preset lexical semantic rule, detects whether the structured query statement includes a statement or parameter input by a user; after detecting that the structured query statement does not include a statement or parameter input by a user, determines that the detection result of the structured query statement is that the structured query statement is a normal statement. Thus, after performing lexical semantic analysis on the token list and analyzing the statement or parameter input by the user, the risk of abnormal statement injection in the SQL statement is eliminated, the security of the SQL statement is improved, the security of the application is improved, and the security of the application accessing the database data is improved.

[0064] In some optional implementations of the present disclosure, the above-mentioned detection of the token list and the structured query statement to obtain the detection result of the structured query statement also includes: in response to detecting that the structured query statement includes a statement or parameter input by the user, based on the token list, detecting whether the statement or parameter input by the user spans multiple tokens; in response to detecting that the statement or parameter input by the user does not span multiple tokens, determining that the detection result of the structured query statement is that the structured query statement is a normal statement.

[0065] In an optional implementation, the above-mentioned detection of whether the statement or parameter input by the user spans multiple tokens based on the token list includes: in the structured query statement, detecting the starting position and the ending position of the statement or parameter input by the user in the structured query statement; according to the starting position and the ending position, searching the token list for a token segment corresponding to the starting position and the ending position, and detecting the number of tokens in the token segment, in response to detecting that the number of tokens in the token segment is one, determining that the statement or parameter input by the user does not span multiple tokens, and determining that the structured query statement is a normal statement. In response to detecting that the number of tokens in the token segment is multiple, determining that the statement or parameter input by the user spans multiple tokens, and determining that the structured query statement is an abnormal statement.

[0066] For example, a normal user enters the username exampleUser and the SQL statement to query user information is as follows: SELECT FROM users WHERE username = 'exampleUser'; In the case of SQL injection, the user input is: exampleUser' AND SLEEP(5) --, the final SQL statement is: SELECT FROM usersWHERE username = 'exampleUser' AND SLEEP(5) --'Check that the input is contained in the SQL statement and spans multiple tokens, then the rule is hit.

[0067] The method for obtaining the detection result of the structured query statement provided by this optional implementation manner detects whether the statement or parameter input by the user spans multiple tokens based on the token list; in response to detecting that the statement or parameter input by the user does not span multiple tokens, determines that the detection result of the structured query statement is that the structured query statement is a normal statement; in response to detecting that the number of tokens in the token segment is multiple, determines that the statement or parameter input by the user spans multiple tokens, and determines that the structured query statement is an abnormal statement; thereby, after analyzing the token list, the structured query statement, and the user input information, determining that the SQL statement is a normal statement eliminates the risk of injection, improves the security of the SQL statement, improves the security of the application, and improves the security of the application accessing the database data.

[0068] In some optional implementations of the present disclosure, the above-mentioned detection of the token list and the structured query statement to obtain the detection result of the structured query statement also includes: in response to detecting that the token list conforms to preset lexical and semantic rules, determining that the detection result of the structured query statement is that the structured query statement is an abnormal statement.

[0069] In this embodiment, the lexical semantic rules are detection rules set for the token list. The lexical semantic rules can be used to detect whether the tokens in the token list include defects such as sensitive parameters, malicious functions, stack injections, and short-circuit expressions. When it is detected that the token list meets the preset lexical semantic rules, the structured query statement is determined to be an abnormal statement.

[0070] The optional implementation of the present invention provides a method for obtaining the detection result of a structured query statement, which detects that a token list conforms to preset lexical and semantic rules, and determines that the detection result of the structured query statement is that the structured query statement is an abnormal statement, thereby providing a reliable detection method for the detection of structured query statements.

[0071] In some optional implementations of the present disclosure, the above-mentioned obtaining a token list based on the structured query statement includes: giving the lexical file to a rule parsing generator to obtain a token parsing generator; and using the token parsing generator to parse the structured query statement to obtain a token list.

[0072] In this embodiment, a relatively complete SQL92 / Mysql lexical file is sent to a rule parsing generator to obtain a token parsing generator, and the token parsing generator is used to parse the structured query statement to obtain a token list.

[0073] For example, a structured query statement is: SELECT oid, attnum from example_table where oid in ('123')

[0074] The token parsing generator will recognize the following token tags. After the actual code lexically parses these tokens, each keyword / operator / identifier type has an integer id to represent it. Specifically:

[0075] SELECT: a keyword used to indicate the type of query operation, integer id: 42;

[0076] Spaces: (ignored, including subsequent spaces).

[0077] oid: An identifier, indicating the column name, integer id: 342.

[0078] ,: a keyword, comma, integer id: 35.

[0079] attnum: Another identifier, indicating the column name, integer id: 342.

[0080] from: A keyword used to specify the source of the data to be queried, integer id: 79.

[0081] example_table: An identifier indicating the table name. Integer id: 342.

[0082] where: A keyword that specifies the query condition. Integer id: 89.

[0083] oid: Another identifier, indicating the column name. Integer id: 342.

[0084] in: a keyword, integer id: 106.

[0085] (: a keyword, left bracket, integer id: 29.

[0086] '123': a literal, integer id: 343.

[0087] ): A keyword, a right bracket, an integer id: 30.

[0088] <eof>: End identifier, integer id: -1.

[0089] Finally, through the token parsing generator, the parsed token list is as follows:

[0090] [@0,0:5='SELECT', <42> ,1:0]

[0091] [@1,7:9='oid', <342> ,1:7]

[0092] [@2,10:10=',', <35> ,1:10]

[0093] [@3,12:17='attnum', <342> ,1:12]

[0094] [@4,19:22='from', <79> ,1:19]

[0095] [@5,24:36='example_table', <342> ,1:24]

[0096] [@6,38:42='where', <89> ,1:38]

[0097] [@7,44:46='oid', <342> ,1:44]

[0098] [@8,48:49='in', <106> ,1:48]

[0099] [@9,51:51='(', <29> ,1:51]

[0100] [@10,52:56=''123'', <343> ,1:52]

[0101] [@11,57:57=')', <30> ,1:57]

[0102] [@12,58:57=' <eof>',<-1>,1:58]]

[0103] Among them, the tokens in the token list [@0,0:5='SELECT', <42> ,1:0], the information of the token is explained as follows:

[0104] @0 indicates the token index, i.e. the current token number, starting from 0;

[0105] 0:5='SELECT' means the token character is in the position from 0 to 5 in the entire SQL statement, and the string is SELECT

[0106] <42> The integer id representing the token is 42, which represents the keyword SELECT in the code;

[0107] 1:0 indicates the row and column position of the token, 1 means in the first row and 0 means in the 0th column.

[0108] In this optional implementation, the rule parsing generator can be antlr4 (ANother Tool for Language Recognition 4.0, rule parser). antlr4 parses SQL statements, completes SQL92 / Mysql lexical analysis, semantic analysis, and performs detection in asynchronous threads. It has extremely high performance and a timeout is set. If the timeout expires, the execution continues without affecting normal business.

[0109] This optional implementation provides a method for obtaining a token list, which gives the lexical file to a rule parsing generator so that the obtained token parsing generator can have the lexical rules of the lexical file; the token parsing generator is used to parse the structured query statement to obtain the token list, providing a reliable implementation method for obtaining the token list.

[0110] In some optional implementations of the present disclosure, the above-mentioned processing of the structured query statement based on the detection result includes: obtaining fingerprint information based on the token list; in response to the detection result indicating that the structured query statement is a normal statement, adding the fingerprint information and the detection result to the whitelist.

[0111] In this optional implementation, the fingerprint information is a characteristic identifier of the structured query statement, and is used to identify the uniqueness of the structured query statement.

[0112] In this optional implementation, the fingerprint information can be an integer array type of the token in the token list and a string array of key tokens such as an identifier (usually a table name) / function name (the token string before the left bracket) after the FROM keyword. The above-mentioned fingerprint information is obtained based on the token list, including: determining the integer array of the token in the token list; determining the identifier or function name after the FROM keyword; and using the integer array and the identifier or function name as the fingerprint information.

[0113] For example, a structured query statement is: SELECT oid, attnum from example_table where oid in ('123'); the obtained fingerprint information is as follows:

[0114] {"types":[42,342,35,342,79,342,89,342,106,29,343,30,-1],"tokens": ["example_table"]}

[0115] The method for processing structured query statements provided by this optional implementation obtains fingerprint information based on a token list; in response to the detection result indicating that the structured query statement is a normal statement, the fingerprint information and the detection result are added to a whitelist, thereby recording the SQL fingerprint and adding the trusted whitelist through the SQL fingerprint, effectively eliminating false positives.

[0116] In some embodiments of the present disclosure, before detecting the token list and the structured query statement to obtain the detection result of the structured query statement, the method also includes: determining whether the fingerprint information of the structured query statement is in the whitelist; in response to the fingerprint information of the structured query statement not being in the whitelist, detecting the token list and the structured query statement to obtain the detection result.

[0117] The structured query statement injection protection method provided by the present invention controls the application to run in a real-time application self-protection environment; obtains the structured query statement for the application to access the database in the relevant interface class of the application connecting to the database; obtains a token list based on the structured query statement; obtains fingerprint information based on the token list; determines whether the fingerprint information of the structured query statement is in the white list; in response to the fingerprint information of the structured query statement not being in the white list, detects the token list and the structured query statement to obtain the detection result of the structured query statement; in response to the detection result indicating that the structured query statement is a normal statement, adds the fingerprint information and the detection result to the white list.

[0118] The structured query statement injection protection method provided in this embodiment first detects whether the fingerprint information of the structured query statement is in the whitelist before detecting the structured query statement and the token list. If it is not in the whitelist, the token list and the structured query statement are detected, which can save detection steps and simplify the detection process.

[0119] Optionally, the method further includes: in response to the fingerprint information of the structured query statement being in the whitelist, no longer detecting the token list and the structured query statement, and directly obtaining a detection result that the structured query statement is a normal statement.

[0120] In some optional implementations of the present disclosure, the processing of the structured query statement based on the detection result includes: in response to the detection result indicating that the structured query statement is not a normal statement, generating an alarm exception information, and prohibiting the application from executing the structured query statement.

[0121] The method for processing structured query statements provided by this optional implementation manner generates an alarm exception message when the detection result indicates that the structured query statement is not a normal statement, prohibits the application from executing the structured query statement, and can effectively block abnormal structured query statements.

[0122] Optionally, the above-mentioned processing of structured query statements based on the detection results also includes: receiving configuration information of lexical semantic rules; determining sub-rules to be blocked in the lexical semantic rules based on the configuration information; and blocking the structured query statement in response to the structured query statement in the detection result conforming to the sub-rule to be blocked. The sub-rules to be blocked may be one or more of grammatical short-circuit sub-rules, comment truncation sub-rules, stacked injection sub-rules, joint query injection sub-rules, hexadecimal string bypass injection sub-rules, oversized token sub-rules, export data to file sub-rules, and malicious function blacklist sub-rules.

[0123] Optionally, the above-mentioned processing of the structured query statement based on the detection result further includes: in response to the detection result indicating that the structured query statement is a normal statement, the structured query statement and the fingerprint information of the structured query statement are added as trust, without the need for the user to write a complex regular expression, and the fingerprint information of the structured query statement with the added trust behavior is sent to the real-time application self-protection environment. The real-time application self-protection environment will no longer report this false alarm in the future, which can effectively eliminate the false alarm.

[0124] Further references Figure 2 As an implementation of the methods shown in the above figures, the present disclosure provides an embodiment of a structured query statement injection protection device. Figure 1 Corresponding to the method embodiment shown, the device can be specifically applied to various electronic devices.

[0125] like Figure 2 As shown, the structured query statement injection protection device 200 provided in this embodiment includes: a control unit 201, an acquisition unit 202, a obtaining unit 203, a detection unit 204, and a processing unit 205. Among them, the above-mentioned control unit 201 can be configured to control the application to run in a real-time application self-protection environment. The above-mentioned acquisition unit 202 can be configured to obtain the structured query statement of the application accessing the database in the relevant interface class of the application connecting to the database. The above-mentioned obtaining unit 203 can be configured to obtain a token list based on the structured query statement. The above-mentioned detection unit 204 can be configured to detect the token list and the structured query statement to obtain the detection result of the structured query statement. The above-mentioned processing unit 205 is configured to process the structured query statement based on the detection result.

[0126] In this embodiment, in the structured query injection protection device 200, the specific processing of the control unit 201, the acquisition unit 202, the obtaining unit 203, the detection unit 204, and the processing unit 205 and the technical effects thereof can be referred to respectively. Figure 1 The relevant descriptions of step 101, step 102, step 103, step 104, and step 105 in the corresponding embodiments are not repeated here.

[0127] In some embodiments of the present disclosure, the above-mentioned detection unit 204 is configured to: detect whether the token list complies with preset lexical and semantic rules; in response to detecting that the token list does not comply with the preset lexical and semantic rules, detect whether the structured query statement includes a statement or parameter input by the user; in response to detecting that the structured query statement does not include a statement or parameter input by the user, determine that the detection result of the structured query statement is that the structured query statement is a normal statement.

[0128] In some embodiments of the present disclosure, the above-mentioned detection unit 204 is further configured to: in response to detecting that the structured query statement includes a statement or parameter input by the user, based on the token list, detect whether the statement or parameter input by the user spans multiple tokens; in response to detecting that the statement or parameter input by the user does not span multiple tokens, determine that the detection result of the structured query statement is that the structured query statement is a normal statement.

[0129] In some embodiments of the present disclosure, the detection unit 204 is further configured to: in response to detecting that the token list complies with a preset lexical semantic rule, determine that the detection result of the structured query statement is that the structured query statement is an abnormal statement.

[0130] In some embodiments of the present disclosure, the obtaining unit 203 is configured to: provide the lexical file to the rule parsing generator to obtain a token parsing generator; and use the token parsing generator to parse the structured query statement to obtain a token list.

[0131] In some embodiments of the present disclosure, the processing unit 205 is configured to: obtain fingerprint information based on the token list; and add the fingerprint information and the detection result to a whitelist in response to the detection result indicating that the structured query statement is a normal statement.

[0132] In some embodiments of the present disclosure, the above-mentioned device 200 also includes: a judgment unit (not shown in the figure), which is configured to: judge whether the fingerprint information of the structured query statement is in the whitelist; in response to the fingerprint information of the structured query statement not being in the whitelist, control the detection unit 204 to work.

[0133] In some embodiments of the present disclosure, the processing unit 205 is configured to: in response to the detection result indicating that the structured query statement is not a normal statement, generate an alarm exception information and prohibit the application from executing the structured query statement.

[0134] In the structured query statement injection protection device provided in the embodiment of the present disclosure, first, the control unit 201 controls the application to run in the real-time application self-protection environment; secondly, the acquisition unit 202 acquires the structured query statement of the application accessing the database in the relevant interface class of the application connecting to the database; thirdly, the acquisition unit 203 acquires the token list based on the structured query statement; then, the detection unit 204 detects the structured query statement and obtains the detection result of the structured query statement; finally, the processing unit 205 processes the structured query statement based on the detection result. Thus, by acquiring the structured query statement through the relevant interface class, the real-time processing effect of the structured query statement can be achieved; based on the token list and the structured query statement, the automatic detection of the structured query statement injection is realized, the database security and database management efficiency are improved, and the operation and maintenance cost is reduced.

[0135] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium and a computer program product.

[0136] Figure 3 A schematic block diagram of an example electronic device 300 that can be used to implement an embodiment of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their modes are merely examples and are not intended to limit the implementation of the present disclosure described and / or required herein.

[0137] like Figure 3 As shown, the device 300 includes a computing unit 301, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 302 or a computer program loaded from a storage unit 308 into a random access memory (RAM) 303. In the RAM 303, various programs and data required for the operation of the device 300 can also be stored. The computing unit 301, the ROM 302, and the RAM 303 are connected to each other via a bus 304. An input / output (I / O) interface 305 is also connected to the bus 304.

[0138] A number of components in the device 300 are connected to the I / O interface 305, including: an input unit 306, such as a keyboard, a mouse, etc.; an output unit 307, such as various types of displays, speakers, etc.; a storage unit 308, such as a disk, an optical disk, etc.; and a communication unit 309, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 309 allows the device 300 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.

[0139] The computing unit 301 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 301 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 301 performs the various methods and processes described above, such as the structured query statement injection protection method. For example, in some embodiments, the structured query statement injection protection method may be implemented as a computer software program, which is tangibly included in a machine-readable medium, such as a storage unit 308. In some embodiments, part or all of the computer program may be loaded and / or installed on the device 300 via the ROM 302 and / or the communication unit 309. When the computer program is loaded into the RAM 303 and executed by the computing unit 301, one or more steps of the structured query statement injection protection method described above may be performed. Alternatively, in other embodiments, the computing unit 301 may be configured to execute the structured query statement injection protection method in any other appropriate manner (for example, by means of firmware).

[0140] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0141] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable structured query statement injection protection device, so that when the program code is executed by the processor or controller, the mode / operation specified in the flow chart and / or block diagram is implemented. The program code may be executed entirely on the machine, partially on the machine, partially on the machine as a stand-alone software package and partially on a remote machine, or entirely on a remote machine or server.

[0142] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or device, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0143] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

[0144] The systems and techniques described herein may be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), and the Internet.

[0145] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.

[0146] The foregoing description of specific exemplary embodiments of the present disclosure is for the purpose of illustration and demonstration. These descriptions are not intended to limit the present disclosure to the precise form disclosed, and it is clear that many changes and variations can be made based on the above teachings. The purpose of selecting and describing the exemplary embodiments is to explain the specific principles of the present disclosure and its practical application, so that those skilled in the art can realize and utilize various different exemplary embodiments of the present disclosure and various different selections and changes. The scope of the present disclosure is intended to be defined by the claims and their equivalents.< / eof> < / eof>

Claims

1. A structured query statement injection protection method, characterized in that: The method comprises: Control applications run in a real-time application self-protection environment; In the relevant interface class of the application connecting to the database, obtaining a structured query statement for the application to access the database; Based on the structured query statement, a token list is obtained; Detecting the token list and the structured query statement to obtain a detection result of the structured query statement; Based on the detection result, processing the structured query statement; The token list and the structured query statement are detected, and the detection result of the structured query statement obtained includes: Detecting whether the token list complies with preset lexical semantic rules; the lexical semantic rules are detection rules set for the token list, and the lexical semantic rules are used to detect whether the tokens in the token list include sensitive parameters, malicious functions, stack injections, and short-circuit expressions; In response to detecting that the token list does not conform to a preset lexical semantic rule, detecting whether the structured query statement includes a statement or parameter input by a user; In response to detecting that the structured query statement does not include a statement or parameter input by a user, it is determined that the detection result of the structured query statement is that the structured query statement is a normal statement.

2. The method according to claim 1, wherein: The detecting the token list and the structured query statement to obtain the detection result of the structured query statement further includes: In response to detecting that the structured query statement includes a statement or parameter input by a user, based on the token list, detecting whether the statement or parameter input by the user spans multiple tokens; In response to detecting that the statement or parameter input by the user does not span multiple tokens, determining that the detection result of the structured query statement is that the structured query statement is a normal statement.

3. The method according to claim 1, wherein: The detecting the token list and the structured query statement to obtain the detection result of the structured query statement further includes: In response to detecting that the token list complies with a preset lexical semantic rule, it is determined that the detection result of the structured query statement is that the structured query statement is an abnormal statement.

4. The method according to claim 1, wherein: The token list obtained based on the structured query statement includes: Give the lexical file to the rule parser generator to get the token parser generator; The token parsing generator is used to parse the structured query statement to obtain a token list.

5. The method according to claim 1, wherein: The processing of the structured query statement based on the detection result includes: Based on the token list, obtain fingerprint information; In response to the detection result indicating that the structured query statement is a normal statement, the fingerprint information and the detection result are added to a whitelist.

6. The method according to claim 5, before detecting the token list and the structured query statement to obtain the detection result of the structured query statement, the method further comprises: Determine whether the fingerprint information of the structured query statement is in the whitelist; In response to the fingerprint information of the structured query statement not being in the whitelist, the token list and the structured query statement are detected to obtain a detection result.

7. The method according to claim 1, wherein: The processing of the structured query statement based on the detection result includes: In response to the detection result indicating that the structured query statement is not a normal statement, an alarm exception message is generated to prohibit the application from executing the structured query statement.

8. A structured query statement injection protection device, the device comprising: a control unit configured to control the application to run in a real-time application self-protection environment; An acquisition unit, configured to acquire, from a relevant interface class of the application connecting to the database, a structured query statement for the application to access the database; An obtaining unit is configured to obtain a token list based on the structured query statement; A detection unit, configured to detect the token list and the structured query statement to obtain a detection result of the structured query statement; The detection unit is further configured to: detect whether the token list complies with a preset lexical semantic rule; the lexical semantic rule is a detection rule set for the token list, and the lexical semantic rule is used to detect whether the tokens in the token list include sensitive parameters, malicious functions, stack injections, and short-circuit expressions; In response to detecting that the token list does not conform to a preset lexical semantic rule, detecting whether the structured query statement includes a statement or parameter input by a user; In response to detecting that the structured query statement does not include a statement or parameter input by a user, determining that a detection result of the structured query statement is that the structured query statement is a normal statement; The processing unit is configured to process the structured query statement based on the detection result.

9. An electronic device, characterized in that: include: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method according to any one of claims 1 to 7.

10. A non-transitory computer-readable storage medium storing computer instructions, characterized in that: The computer instructions are used to enable the computer to execute the method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • SQL statement injection attack detection method and device

    CN112650769A