Code vulnerability detection method, system and medium based on data analysis
By lexical analysis and keyword filtering of the source code, an accurate control flow chart is constructed, which solves the problem of poor control flow chart results in keyword recognition errors in the existing technology, and improves the accuracy of code vulnerability detection.
Patent Information
- Application Number
- CN202411959359.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-03-07
- Estimated Expiration
- 2044-12-30
AI Technical Summary
The existing methods cannot accurately extract the keywords used to build the control flow graph, resulting in the poor effect of the constructed control flow graph and reduce the accuracy of code vulnerability detection.
By lexical analysis of the source code, keywords are extracted, and keywords are classified and positioned, the keywords are calculated, and the real keywords are selected to construct a control flow chart.
Improve the accuracy of source code vulnerability detection and enhance code security by building more accurate control flow diagrams.
Smart Images

Figure CN119377093B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of code vulnerability detection, and in particular to a code vulnerability detection method, system and medium based on data analysis. Background Art
[0002] Code is the core component of the software system, and its security is directly related to the stability and reliability of the entire system. Program vulnerabilities are inevitable in the code writing process. Vulnerabilities may be exploited by malicious attackers, leading to serious consequences such as data leakage and system crash. Regular code vulnerability detection is necessary to timely discover and fix these potential threats and improve software security.
[0003] In the related art, lexical or syntactic analysis is usually performed on the source code to extract keywords in the source code, and the source code is detected for vulnerabilities by constructing a control flow graph. However, for source code with complex structure, the types and numbers of keywords are large, and not all keywords can be used to construct a control flow graph, which makes it easy to make errors when identifying keywords used to construct a control flow graph. As a result, the existing methods are unable to accurately extract the keywords used to construct the control flow graph, resulting in poor effect of the constructed control flow graph, thereby reducing the accuracy of code vulnerability detection. Summary of the invention
[0004] In order to solve the technical problem that the existing methods cannot accurately extract keywords for constructing control flow graphs, resulting in poor effect of the constructed control flow graphs, thereby reducing the accuracy of code vulnerability detection, the purpose of the present invention is to provide a code vulnerability detection method, system and medium based on data analysis, and the technical solutions adopted are as follows:
[0005] The present invention proposes a code vulnerability detection method based on data analysis, the method comprising:
[0006] Obtaining a source code to be detected, performing lexical analysis on the source code, and extracting a plurality of keywords in the source code;
[0007] Classify all keywords to obtain different keyword categories and category label values of each keyword in the keyword category; locate the position of each keyword in the source code to obtain the position label value of each keyword; take any keyword as the target keyword, and obtain the position matching degree of the target keyword according to the difference between the category label value and the position label value of the target keyword and the number of keywords in the keyword category where the target keyword is located; and screen out the initial keyword from all keywords based on the position matching degree;
[0008] Taking any initial keyword as the target initial keyword, taking two initial keywords adjacent to the target initial keyword as neighborhood keywords of the target initial keyword, and obtaining the neighborhood cohesion degree of the target initial keyword according to the difference in the category label value between the target initial keyword and the neighborhood keyword; and screening out real keywords from all the initial keywords according to the position matching degree and the neighborhood cohesion degree of each initial keyword;
[0009] Based on all real keywords, a control flow graph of the source code is constructed, and vulnerability detection is performed on the source code.
[0010] Furthermore, obtaining the position matching degree of the target keyword includes:
[0011] The absolute value of the difference between the category label value and the position label value of the target keyword is used as the feature difference value of the target keyword;
[0012] Performing negative correlation mapping on the feature difference values to obtain matching parameters of the target keywords;
[0013] The matching parameters of the target keyword and the number of keywords in the keyword category where the target keyword is located are integrated and normalized to obtain the position matching degree of the target keyword.
[0014] Furthermore, the filtering out the initial keywords from all keywords based on the position matching degree includes:
[0015] The keyword whose position matching degree is greater than a preset matching degree threshold is used as the initial keyword.
[0016] Furthermore, the obtaining of the neighborhood connectivity of the target initial keyword includes:
[0017] The absolute value of the difference between the target initial keyword and each neighborhood keyword is used as the category label difference value between the target initial keyword and each neighborhood keyword;
[0018] The average value of the category label difference values between the target initial keyword and all neighboring keywords is used as the connectivity judgment value of the target initial keyword;
[0019] The absolute value of the difference between the connectivity judgment value of the target initial keyword and the value 1 is negatively correlated and normalized to obtain the neighborhood connectivity of the target initial keyword.
[0020] Furthermore, the step of selecting real keywords from all the initial keywords according to the position matching degree and the neighborhood connection degree of each initial keyword includes:
[0021] The position matching degree and the neighborhood connection degree of each initial keyword are integrated and normalized to obtain the selection possibility of each initial keyword;
[0022] The median of the selection probabilities of all initial keywords is obtained, and the initial keywords whose selection probabilities are greater than the median are taken as real keywords.
[0023] Furthermore, the construction of a control flow graph of the source code based on all real keywords and the detection of vulnerabilities in the source code include:
[0024] By using an integrated development environment tool, multiple basic blocks of the source code are extracted, and a control flow graph of the source code is constructed based on all the real keywords and the basic blocks, and the control flow graph is used to perform vulnerability detection on the source code.
[0025] Further, the obtaining of different keyword categories and the category label value of each keyword in the keyword category includes:
[0026] Through a manual labeling method, the keyword category is divided into a start keyword category, an end keyword category and a jump keyword category, and the category label value of each keyword in the start keyword category is set to a value of 0, the category label value of each keyword in the end keyword category is set to a value of 1, and the category label value of each keyword in the jump keyword category is set to a value of 2.
[0027] Furthermore, obtaining the position tag value of each keyword includes:
[0028] The source code is input into the syntax parser, and the generated abstract syntax tree is used to locate the position of each keyword in the source code. If each keyword is located at the beginning of the source code, the position label value of each keyword is set to 0. If each keyword is located at the end of the source code, the position label value of each keyword is set to 1. If each keyword is located in the text of the source code, the position label value of each keyword is set to 2.
[0029] The present invention also proposes a code vulnerability detection system based on data analysis, the system comprising:
[0030] A code acquisition module is used to acquire the source code to be detected, perform lexical analysis on the source code, and extract multiple keywords from the source code;
[0031] The first analysis module is used to classify all keywords, obtain different keyword categories and category label values of each keyword in the keyword category; locate the position of each keyword in the source code to obtain the position label value of each keyword; take any keyword as the target keyword, and obtain the position matching degree of the target keyword according to the difference between the category label value and the position label value of the target keyword and the number of keywords in the keyword category where the target keyword is located; and screen out the initial keyword from all keywords based on the position matching degree;
[0032] The second analysis module is used to take any initial keyword as a target initial keyword, take two initial keywords adjacent to the target initial keyword as neighborhood keywords of the target initial keyword, and obtain the neighborhood cohesion degree of the target initial keyword according to the difference in the category label value between the target initial keyword and the neighborhood keyword; and screen out real keywords from all initial keywords according to the position matching degree and the neighborhood cohesion degree of each initial keyword;
[0033] The vulnerability detection module is used to build a control flow graph of the source code based on all real keywords and perform vulnerability detection on the source code.
[0034] The present invention also proposes a computer medium, which includes a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, it implements any one of the steps of a code vulnerability detection method based on data analysis.
[0035] The present invention has the following beneficial effects:
[0036] The present invention takes into account that the existing methods cannot accurately extract keywords for constructing a control flow graph, resulting in poor effect of the constructed control flow graph and reducing the accuracy of code vulnerability detection. Therefore, the source code is firstly lexically analyzed to extract keywords in the source code, and then the keywords are classified and located, so as to facilitate the subsequent accurate screening of keywords for constructing the control flow graph based on the category label value and the position label value of the keywords. Considering that the category to which the keywords for constructing the control flow graph belong and their position in the code have a certain connection, and the number of keywords of the same category for constructing the control flow graph is large, the obtained position matching degree can be used to reflect the degree to which the keywords meet the position characteristics of the keywords for constructing the control flow graph, and the initial keywords are preliminarily screened out. Considering that the keywords for constructing the control flow graph also need to ensure the characteristics of context connection, the obtained neighborhood connection degree can be used to reflect the degree to which the target initial keywords have the characteristics of context connection, and then the position matching degree and the neighborhood connection degree are combined to accurately screen out the keywords for constructing the control flow graph, that is, the real keywords, and a better control flow graph is constructed through the real keywords, thereby improving the accuracy of source code vulnerability detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] In order to more clearly illustrate the technical solutions and advantages in the embodiments of the present invention or the prior art, the drawings required for use in the embodiments or the prior art descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0038] Figure 1 A flow chart of a code vulnerability detection method based on data analysis provided by an embodiment of the present invention;
[0039] Figure 2 A block diagram of a code vulnerability detection system based on data analysis provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0040] In order to further explain the technical means and effects adopted by the present invention to achieve the predetermined invention purpose, the following is a detailed description of a code vulnerability detection method, system and medium based on data analysis proposed by the present invention, its specific implementation method, structure, features and effects, in combination with the accompanying drawings and preferred embodiments. In the following description, different "one embodiment" or "another embodiment" does not necessarily refer to the same embodiment. In addition, specific features, structures or characteristics in one or more embodiments may be combined in any suitable form.
[0041] Unless defined otherwise, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention belongs.
[0042] The following is a detailed description of a code vulnerability detection method, system and medium based on data analysis provided by the present invention in conjunction with the accompanying drawings.
[0043] See also Figure 1 , which shows a flow chart of a code vulnerability detection method based on data analysis provided by an embodiment of the present invention, the method comprising:
[0044] Step S1: Obtain source code to be detected, perform lexical analysis on the source code, and extract multiple keywords from the source code.
[0045] Static code analysis technology is a basic technology for detecting code vulnerabilities. As a static code analysis technology, the control flow graph graphically displays the execution order and possible control flow between basic blocks in the program. In this way, the control flow graph can clearly display the execution process and logical structure of the code, thereby making it easier to discover potential errors and vulnerabilities in the code. The embodiments of the present invention construct a control flow graph of the code and combine it with data mining and analysis technology to detect potential vulnerabilities in the code. This type of method uses the grammatical structure, execution path and behavioral characteristics of the code to mine potential security vulnerabilities and defects in the code.
[0046] The embodiment of the present invention first extracts the source code to be detected from the code library, and then uses the regular expression method to preprocess the source code to clean up interference factors such as comments, blank lines, and unnecessary blank characters in the source code to avoid the influence of these interference factors on subsequent code vulnerability detection. Among them, regular expressions are technical means well known to those skilled in the art and will not be described in detail here.
[0047] In the subsequent embodiment of the present invention, a control flow graph of the source code is constructed to detect vulnerabilities in the source code. The basic building blocks in the control flow graph are nodes and directed edges. Each node represents a basic block, that is, a continuous code sequence without branch jumps, and the directed edges indicate the direction of the control flow, that is, the possible jump paths from one basic block to another basic block. The control flow graph indicates the direction of program execution through directed edges. These edges connect the basic blocks and reflect the logical relationship and execution order between different parts of the program. The construction of directed edges is the core content of the control flow graph and provides an important basis for program analysis and optimization. The keywords that control the execution path of the program in the source code, such as the if and else keywords that represent conditional branches, the for and while keywords that represent loop structures, and the end, return and exit keywords that represent the end of a code fragment, are the key factors that determine the program execution path and process. These keywords define the control structure in the code, thereby affecting the construction of the directed edges of the control flow graph.
[0048] Therefore, the embodiment of the present invention also needs to use a lexical analyzer to perform lexical analysis on the source code, so as to extract multiple keywords in the source code. The lexical analyzer is a technical means well known to those skilled in the art and will not be described in detail here.
[0049] So far, multiple keywords in the source code have been extracted.
[0050] Step S2: Classify all keywords to obtain different keyword categories and the category label value of each keyword in the keyword category; locate the position of each keyword in the source code to obtain the position label value of each keyword; take any keyword as the target keyword, and obtain the position matching degree of the target keyword according to the difference between the category label value and the position label value of the target keyword and the number of keywords in the keyword category where the target keyword is located; based on the position matching degree, filter out the initial keyword from all keywords.
[0051] After the lexical analysis of the source code is performed as described above, among all the extracted keywords, there are many keywords that cannot be used to construct a control flow graph. If all the extracted keywords are directly used to construct the control flow graph of the source code, the constructed control flow graph will have a poor detection effect on code vulnerabilities. The keywords used to construct the control flow graph are usually used to control the execution path of the code, including keywords such as if, else, for, while, switch, etc. These keywords have a certain connection with their positions in the source code, and these keywords have the characteristics of contextual connection in the source code. Therefore, the embodiment of the present invention first classifies all keywords, obtains different keyword categories and the category label value of each keyword in the keyword category, and locates the position of each keyword in the source code to obtain the position label value of each keyword. Subsequently, based on the label value and the position label value of the keyword, the keywords that can be used to construct the source code control flow graph can be accurately screened out, thereby improving the accuracy of code vulnerability detection.
[0052] Preferably, in an embodiment of the present invention, the method for obtaining the category label value of each keyword in different keyword categories and keyword categories specifically includes:
[0053] The embodiment of the present invention believes that the keywords in the source code are mainly divided into three categories, namely, keywords used to represent the start of the program, keywords used to represent the end of the program, and keywords used to represent the program jump. Therefore, the keyword categories can be divided into start keyword categories, end keyword categories and jump keyword categories through a manual annotation method, and the category label value of each keyword in the start keyword category is set to a value of 0, the category label value of each keyword in the end keyword category is set to a value of 1, and the category label value of each keyword in the jump keyword category is set to a value of 2, wherein the keywords included in the start keyword category are used to represent the start of the program, such as keywords such as def, class and try, the keywords included in the end keyword category are used to represent the end of the program, such as keywords such as break, return and exit, and the keywords included in the jump keyword category are used to represent the jump of the program, such as keywords such as if, else, switch, case and goto.
[0054] Preferably, in one embodiment of the present invention, the method for obtaining the location tag value of each keyword specifically includes:
[0055] The source code is input into a syntax parser, and the position of each keyword in the source code is located using the generated abstract syntax tree, wherein the position is represented by a row and column value or a character index. If each keyword is located at the beginning of the source code, the position tag value of each keyword is set to a value of 0. If each keyword is located at the end of the source code, the position tag value of each keyword is set to a value of 1. If each keyword is located in the text of the source code, the position tag value of each keyword is set to a value of 2. In one embodiment of the present invention, for keywords located in the first 1 / 3 of the source code, it is considered that the keywords are located at the beginning of the source code, for keywords located in the last 1 / 3 of the source code, it is considered that the keywords are located at the end of the source code, and for keywords located in the remaining parts of the source code, it is considered that the keywords are located in the text of the source code.
[0056] So far, the category label value and position label value of each keyword have been obtained. From the above analysis, it can be seen that not all keywords can be used to construct the control flow graph of the source code. Therefore, it is necessary to analyze any keyword and take any keyword as the target keyword. Considering that the category to which the keyword used to construct the control flow graph belongs and its position in the code have a certain connection, specifically, the smaller the difference between the category label value and the position label value of the keyword, the stronger the connection between the category and position of the keyword, and the keywords of the same category used to construct the control flow graph appear more frequently in the source code. Therefore, the difference between the category label value and the position label value of the target keyword, as well as the number of keywords in the keyword category where the target keyword is located can be analyzed. The obtained position matching degree reflects the degree to which the target keyword meets the position characteristics of the keyword used to construct the control flow graph. The larger the position matching degree, the greater the possibility that the target keyword can be used to construct the control flow graph. Subsequently, the keywords used to construct the control flow graph can be preliminarily screened out based on the position matching degree.
[0057] Preferably, in one embodiment of the present invention, the method for obtaining the position matching degree of the target keyword specifically includes:
[0058] First, the absolute value of the difference between the category label value and the position label value of the target keyword is used as the feature difference value of the target keyword. The smaller the feature difference value, the more likely the target keyword is to be used in control flow graph construction. For example, the category label value of a keyword belonging to the end keyword category is 1. If the keyword is located at the end of the source code, that is, the position label value of the keyword is 1, then the category of the keyword and its position in the source code match, and the more likely it is to be used in control flow graph construction. Therefore, the feature difference value can be negatively correlated to obtain the matching parameters of the target keyword, providing a data basis for the subsequent calculation of the position matching degree of the target keyword.
[0059] From the above analysis, we can see that keywords of the same category used to construct the control flow graph appear more frequently in the source code. Therefore, we can combine the matching parameters of the target keyword and the number of keywords in the keyword category where the target keyword is located and perform normalization, and limit the calculation results to range, thereby obtaining the position matching degree of the target keyword.
[0060] In the embodiment of the present invention, the combination of the matching parameter of the target keyword and the number of keywords in the keyword category where the target keyword belongs is calculated, which is not limited here.
[0061] In one embodiment of the present invention, the normalization processing can be specifically, for example, maximum and minimum value normalization processing, and the normalization in subsequent steps can all adopt maximum and minimum value normalization processing. In other embodiments of the present invention, other normalization methods can be selected according to the specific range of numerical values, which will not be described in detail.
[0062] As an example, in one embodiment of the present invention, the expression of the position matching degree of the target keyword may be specifically, for example, as follows:
[0063]
[0064] in, Indicates the position matching degree of the target keyword; Indicates the position tag value of the target keyword; Indicates the category label value of the target keyword; Indicates the feature difference value of the target keyword; Indicates the matching parameters of the target keyword; Indicates the number of keywords in the keyword category where the target keyword belongs; represents the normalization function; Indicates the preset first adjustment parameter, which is used to prevent the denominator from being 0. The value range is In one embodiment of the present invention, Set to 0.1, The specific value of can also be set by the implementer according to the specific implementation scenario and is not limited here.
[0065] It should be noted that in other embodiments of the present invention, negative correlation mapping may be achieved through other basic mathematical operations, which will not be elaborated herein.
[0066] The position matching degree of each keyword can be obtained by the same method as above. The greater the position matching degree, the greater the possibility that the keyword can be used to construct the control flow graph. Therefore, based on the position matching degree, the initial keywords that can be used to construct the control flow graph can be preliminarily screened out from all keywords. Further analysis can be made on the initial keywords to improve the effect of constructing the source code control flow graph, thereby improving the accuracy of vulnerability detection in the source code.
[0067] Preferably, in one embodiment of the present invention, a keyword whose position matching degree is greater than a preset matching degree threshold is used as the initial keyword, wherein the preset matching degree threshold is set to 0.8, and the specific value of the preset matching degree threshold can also be set by the implementer according to the specific implementation scenario, which is not limited here.
[0068] So far, the initial keywords that can be used to construct the source code control flow graph have been preliminarily screened out.
[0069] Step S3: Take any initial keyword as the target initial keyword, take the two initial keywords adjacent to the target initial keyword as the neighborhood keywords of the target initial keyword, and obtain the neighborhood connectivity of the target initial keyword based on the difference in category label values between the target initial keyword and the neighborhood keywords; and screen out real keywords from all initial keywords based on the position matching degree and neighborhood connectivity of each initial keyword.
[0070] Keywords with contextual connection characteristics can provide a more comprehensive understanding of program structure and behavior when constructing a flow graph, clarify the role and influence of each keyword in the program logic, and improve the fineness and depth of code analysis. Therefore, the initial keywords used to construct the source code control flow graph also need to ensure that they have contextual connection characteristics. This initial keyword is specifically manifested in the embodiment of the present invention as follows: the closer the difference in category label value between a certain initial keyword in the source code and its adjacent initial keyword is to the value 1, the greater the degree to which the initial keyword has contextual connection characteristics. Therefore, the embodiment of the present invention first analyzes any initial keyword, takes any initial keyword as the target initial keyword, and in the source code, takes two initial keywords adjacent to the target initial keyword as the neighborhood keywords of the target initial keyword, and analyzes the difference in category label values between the target initial keyword and the neighborhood keywords. The neighborhood connection degree obtained reflects the degree to which the target initial keyword has contextual connection characteristics. Subsequently, the initial keywords can be screened again in combination with the position matching degree and the neighborhood connection degree of the initial keywords, thereby obtaining real keywords that can be used to construct the control flow graph and improving the accuracy of source code vulnerability detection.
[0071] It should be noted that step S2 has achieved the positioning of the keywords in the source code. The two initial keywords adjacent to the target initial keyword mentioned above mean that the positions of the two initial keywords in the source code are adjacent to the target initial keyword, that is, the two initial keywords are respectively the previous and next initial keywords of the target initial keyword in the source code. At the same time, for the first and last initial keywords in the source code, there is only one initial keyword adjacent to them. At this time, for the first initial keyword, the second and third initial keywords in the source code can be used as the neighborhood keywords of the first initial keyword. Similarly, for the last initial keyword, the second to last and third to last initial keywords in the source code can be used as the neighborhood keywords of the last initial keyword, so that subsequent calculations can proceed smoothly.
[0072] Preferably, in one embodiment of the present invention, the method for obtaining the neighborhood cohesion degree of the target initial keyword specifically includes:
[0073] The absolute value of the difference between the category label values of the target initial keyword and each neighborhood keyword is used as the category label difference value between the target initial keyword and each neighborhood keyword. The closer the category label difference value is to the value 1, the stronger the contextual connection characteristic of the target initial keyword is. Then, the average value of the category label difference value between the target initial keyword and all neighborhood keywords can be used as the connectivity judgment value of the target initial keyword. The closer the connectivity judgment value is to the value 1, the greater the degree of contextual connection characteristic of the target initial keyword is. Then, the target initial keyword is more suitable for constructing a control flow graph. Therefore, the absolute value of the difference between the connectivity judgment value of the target initial keyword and the value 1 can be negatively correlated and normalized, and the calculation result can be limited to range, thereby obtaining the neighborhood connectivity of the target initial keyword.
[0074] As an example, in one embodiment of the present invention, the expression of the neighborhood cohesion degree of the target initial keyword can be specifically, for example, as follows:
[0075]
[0076]
[0077] in, Indicates the neighborhood cohesion of the target initial keyword; Indicates the cohesion judgment value of the target initial keyword; Represents the category label value of the target initial keyword; and Respectively represent the category label values of the two neighboring keywords of the target initial keyword; and Respectively represent the category label difference values between the target initial keyword and the two neighborhood keywords; represents the normalization function; Indicates the preset second adjustment parameter, which is used to prevent the denominator from being 0. The value range is In one embodiment of the present invention, Set to 0.1, The specific value of can also be set by the implementer according to the specific implementation scenario and is not limited here.
[0078] The same method as above can be used to obtain the neighborhood connectivity of each initial keyword. The greater the position matching degree and neighborhood connectivity of the initial keyword, the more the initial keyword conforms to the characteristics for constructing a control flow graph. Then, according to the position matching degree and neighborhood connectivity of each initial keyword, the real keywords can be screened out from all the initial keywords. Subsequently, a better control flow graph can be constructed based on the screened real keywords, thereby improving the accuracy of source code vulnerability detection.
[0079] Preferably, in one embodiment of the present invention, the method for acquiring real keywords specifically includes:
[0080] First, the position matching degree and neighborhood connection degree of each initial keyword are integrated and normalized, and the calculation results are limited to The larger the selection probability is, the greater the possibility is, and the more likely it is that the initial keyword will be selected to construct the control flow graph. The median of the selection probability of all initial keywords is obtained, and the initial keywords with a selection probability greater than the median are taken as real keywords. In other words, the initial keywords corresponding to the top 50% of the largest selection probabilities are taken as real keywords.
[0081] In the embodiment of the present invention, the position matching degree and the neighborhood connection degree of each initial keyword may be integrated by calculating the sum or product of the two, which is not limited here.
[0082] As an example, in one embodiment of the present invention, the expression of the selection possibility of each initial keyword may be specifically, for example, as follows:
[0083]
[0084] in, Indicates The possibility of selecting the initial keywords; Indicates The position matching degree of the initial keywords; Indicates The neighborhood cohesion of the initial keywords; Represents the normalization function.
[0085] At this point, the real keywords used to build the source code control flow graph are obtained.
[0086] Step S4: Based on all real keywords, a control flow graph of the source code is constructed, and vulnerability detection is performed on the source code.
[0087] Through the above process, we get the real keywords used to construct the control flow graph of the source code. Then, we can build the control flow graph of the source code based on all the real keywords, improve the vulnerability detection effect of the control flow graph, and perform more accurate vulnerability detection on the source code.
[0088] Preferably, in one embodiment of the present invention, the method for performing vulnerability detection on source code specifically includes:
[0089] Using integrated development environment tools such as Eclipse or PyCharm, multiple basic blocks of the source code are extracted, and based on all real keywords and basic blocks, a control flow graph of the source code is constructed, and the control flow graph is used to detect vulnerabilities in the source code. Among them, the extraction of basic blocks of the source code and the construction of the control flow graph are technical means well known to technical personnel in this field and will not be elaborated here.
[0090] An embodiment of the present invention provides a code vulnerability detection system based on data analysis, see Figure 2 , which shows a block diagram of a code vulnerability detection system based on data analysis provided by an embodiment of the present invention, the system comprising:
[0091] The code acquisition module is used to acquire the source code to be tested, perform lexical analysis on the source code, and extract multiple keywords from the source code;
[0092] The first analysis module is used to classify all keywords, obtain different keyword categories and the category label value of each keyword in the keyword category; locate the position of each keyword in the source code to obtain the position label value of each keyword; take any keyword as the target keyword, and obtain the position matching degree of the target keyword according to the difference between the category label value and the position label value of the target keyword and the number of keywords in the keyword category where the target keyword is located; and screen out the initial keyword from all keywords based on the position matching degree;
[0093] The second analysis module is used to take any initial keyword as the target initial keyword, take two initial keywords adjacent to the target initial keyword as the neighborhood keywords of the target initial keyword, and obtain the neighborhood cohesion of the target initial keyword according to the difference in category label values between the target initial keyword and the neighborhood keywords; and screen out the real keywords from all the initial keywords according to the position matching degree and neighborhood cohesion of each initial keyword;
[0094] The vulnerability detection module is used to build a control flow graph of the source code based on all real keywords and perform vulnerability detection on the source code.
[0095] The present invention further proposes a computer medium, the medium comprising a memory and a processor, wherein the memory stores a computer program, and the processor can implement the method described in steps S1 to S4 when executing the computer program.
[0096] In summary, the embodiment of the present invention first obtains the source code to be detected, performs lexical analysis on the source code, and extracts multiple keywords in the source code; classifies all the keywords to obtain different keyword categories and category label values of each keyword in the keyword category; locates the position of each keyword in the source code to obtain the position label value of each keyword; takes any keyword as the target keyword, and obtains the position matching degree of the target keyword according to the difference between the category label value and the position label value of the target keyword and the number of keywords in the keyword category where the target keyword is located; based on the position matching degree, selects the initial keyword from all the keywords; takes any initial keyword as the target initial keyword, and takes the two initial keywords adjacent to the target initial keyword as the neighborhood keywords of the target initial keyword, and obtains the neighborhood connection degree of the target initial keyword according to the difference in the category label value between the target initial keyword and the neighborhood keywords; selects the real keywords from all the initial keywords according to the position matching degree and the neighborhood connection degree of each initial keyword; constructs the control flow graph of the source code based on all the real keywords, and performs vulnerability detection on the source code.
[0097] It should be noted that the sequence of the above embodiments of the present invention is only for description and does not represent the advantages and disadvantages of the embodiments. The processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0098] The various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referenced to each other, and each embodiment focuses on the differences from other embodiments.
Claims
1. A code vulnerability detection method based on data analysis, characterized in that: The method comprises: Obtaining a source code to be detected, performing lexical analysis on the source code, and extracting a plurality of keywords in the source code; Classify all keywords to obtain different keyword categories and category label values of each keyword in the keyword category; locate the position of each keyword in the source code to obtain the position label value of each keyword; take any keyword as the target keyword, and obtain the position matching degree of the target keyword according to the difference between the category label value and the position label value of the target keyword and the number of keywords in the keyword category where the target keyword is located; and screen out the initial keyword from all keywords based on the position matching degree; Taking any initial keyword as the target initial keyword, taking two initial keywords adjacent to the target initial keyword as neighborhood keywords of the target initial keyword, and obtaining the neighborhood cohesion degree of the target initial keyword according to the difference in the category label value between the target initial keyword and the neighborhood keyword; and screening out real keywords from all the initial keywords according to the position matching degree and the neighborhood cohesion degree of each initial keyword; Based on all real keywords, a control flow graph of the source code is constructed, and vulnerability detection is performed on the source code.
2. A code vulnerability detection method based on data analysis according to claim 1, characterized in that: The obtaining of the position matching degree of the target keyword includes: The absolute value of the difference between the category label value and the position label value of the target keyword is used as the feature difference value of the target keyword; Performing negative correlation mapping on the feature difference values to obtain matching parameters of the target keywords; The matching parameters of the target keyword and the number of keywords in the keyword category where the target keyword belongs are integrated and normalized to obtain the position matching degree of the target keyword.
3. A code vulnerability detection method based on data analysis according to claim 1, characterized in that: The step of screening out initial keywords from all keywords based on the position matching degree includes: The keyword whose position matching degree is greater than a preset matching degree threshold is used as the initial keyword.
4. The method for detecting code vulnerabilities based on data analysis according to claim 1, characterized in that: The obtaining of the neighborhood cohesion degree of the target initial keyword includes: The absolute value of the difference between the target initial keyword and each neighborhood keyword is used as the category label difference value between the target initial keyword and each neighborhood keyword; The average value of the category label difference values between the target initial keyword and all neighboring keywords is used as the connectivity judgment value of the target initial keyword; The absolute value of the difference between the connectivity judgment value of the target initial keyword and the value 1 is negatively correlated and normalized to obtain the neighborhood connectivity of the target initial keyword.
5. The method for detecting code vulnerabilities based on data analysis according to claim 1, characterized in that: The step of selecting real keywords from all initial keywords according to the position matching degree and the neighborhood connection degree of each initial keyword includes: The position matching degree and the neighborhood connection degree of each initial keyword are integrated and normalized to obtain the selection possibility of each initial keyword; The median of the selection probabilities of all initial keywords is obtained, and the initial keywords whose selection probabilities are greater than the median are taken as real keywords.
6. A code vulnerability detection method based on data analysis according to claim 1, characterized in that: The control flow graph of the source code is constructed based on all real keywords, and vulnerability detection is performed on the source code, including: By using an integrated development environment tool, multiple basic blocks of the source code are extracted, and a control flow graph of the source code is constructed based on all the real keywords and the basic blocks, and the control flow graph is used to perform vulnerability detection on the source code.
7. The method for detecting code vulnerabilities based on data analysis according to claim 1, characterized in that: The step of obtaining different keyword categories and the category label value of each keyword in the keyword category includes: Through a manual labeling method, the keyword category is divided into a start keyword category, an end keyword category and a jump keyword category, and the category label value of each keyword in the start keyword category is set to a value of 0, the category label value of each keyword in the end keyword category is set to a value of 1, and the category label value of each keyword in the jump keyword category is set to a value of 2.
8. The method for detecting code vulnerabilities based on data analysis according to claim 1, characterized in that: The obtaining of the position tag value of each keyword includes: The source code is input into the syntax parser, and the generated abstract syntax tree is used to locate the position of each keyword in the source code. If each keyword is located at the beginning of the source code, the position label value of each keyword is set to 0. If each keyword is located at the end of the source code, the position label value of each keyword is set to 1. If each keyword is located in the text of the source code, the position label value of each keyword is set to 2.
9. A code vulnerability detection system based on data analysis, characterized in that: The system comprises: A code acquisition module is used to acquire the source code to be detected, perform lexical analysis on the source code, and extract multiple keywords from the source code; The first analysis module is used to classify all keywords, obtain different keyword categories and category label values of each keyword in the keyword category; locate the position of each keyword in the source code to obtain the position label value of each keyword; take any keyword as the target keyword, and obtain the position matching degree of the target keyword according to the difference between the category label value and the position label value of the target keyword and the number of keywords in the keyword category where the target keyword is located; and screen out the initial keyword from all keywords based on the position matching degree; The second analysis module is used to take any initial keyword as a target initial keyword, take two initial keywords adjacent to the target initial keyword as neighborhood keywords of the target initial keyword, and obtain the neighborhood cohesion degree of the target initial keyword according to the difference in the category label value between the target initial keyword and the neighborhood keyword; and screen out real keywords from all initial keywords according to the position matching degree and the neighborhood cohesion degree of each initial keyword; The vulnerability detection module is used to build a control flow graph of the source code based on all real keywords and perform vulnerability detection on the source code.
10. A computer medium, comprising a memory and a processor, wherein the memory stores a computer program, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 8 are implemented.
Citation Information
Patent Citations
Keyword analysis application method, system and device and computer readable medium
CN115391485A
Source code security detection method and device fusing code vulnerability characteristics and attribute graph
CN117592061A