Python code vulnerability detection method and system based on deep learning

By building and training a code vulnerability detection neural network model based on deep learning, the problem of low automation in the existing technology is solved, efficient and automated vulnerability detection is achieved, and code security is enhanced.

CN119885195APending Publication Date: 2025-04-25武汉智博创享科技股份有限公司
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411921363.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-25
Publication Date
2025-04-25

AI Technical Summary

Technical Problem

The existing code vulnerability detection methods are not very automated and require human intervention, resulting in low detection efficiency.

Method used

Using a deep learning-based method, by collecting and annotating Python code samples, a PyTorch-based code vulnerability detection neural network model is built, and the model is trained, evaluated and optimized to achieve automated vulnerability detection.

Benefits of technology

Improve the accuracy and efficiency of code vulnerability detection, enable potential vulnerabilities to be detected early in code development, enhance code security, and achieve real-time detection and feedback.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119885195A_ABST
    Figure CN119885195A_ABST
Patent Text Reader

Abstract

The invention discloses a Python code vulnerability detection method based on deep learning, and the method comprises the steps: collecting a Python code sample, carrying out the labeling and preprocessing of a Python code, and generating a Python code sample library; constructing a code vulnerability detection neural network model based on PyTorch as a deep learning framework; based on the Python code sample library, training the code vulnerability detection neural network model; and evaluating and optimizing the code vulnerability detection model, and detecting the optimized code vulnerability detection model to obtain a final code vulnerability detection model. The deep learning model can learn grammar, semantics and context information of codes, so that vulnerabilities are recognized more accurately, and the detection precision and efficiency are improved; potential vulnerabilities are found in the early stage of the code development stage, so that loss caused by utilization of the vulnerabilities in the subsequent stage is avoided, and code security is enhanced; real-time detection and feedback are achieved, developers are helped to repair vulnerabilities in time, and development efficiency and quality are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of deep learning technology, and in particular to a method and system for Python code vulnerability detection based on deep learning. Background Art

[0002] Software vulnerabilities threaten complex systems and may lead to information leakage and system crashes, with huge impacts on businesses, governments and individuals. For example, the WannaCry and Heartbleed vulnerabilities caused hundreds of millions of dollars in losses. Vulnerabilities usually stem from minor code errors, such as Heartbleed, which could have been avoided with two lines of code. Secure software engineering needs to combine quality assurance technology to automatically detect source code vulnerabilities. As the scale of software expands, software vulnerabilities continue to increase, and automated vulnerability detection has become a key research area. Current methods include software meta-features, anomaly detection, and misuse detection. However, the existing code vulnerability detection is not highly automated and requires human intervention, resulting in low efficiency in code vulnerability detection. Summary of the invention

[0003] In view of the above problems, the present invention is proposed to provide a method and system for Python code vulnerability detection based on deep learning that overcomes the above problems or at least partially solves the above problems.

[0004] In order to solve the above technical problems, the embodiments of the present application disclose the following technical solutions:

[0005] In a first aspect, an embodiment of the present invention discloses a method for detecting Python code vulnerabilities based on deep learning, comprising:

[0006] S100. Collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library;

[0007] S200. Build a code vulnerability detection neural network model based on PyTorch as a deep learning framework;

[0008] S300. Based on the Python code sample library, the code vulnerability detection neural network model is trained;

[0009] S400. Evaluate and optimize the code vulnerability detection model, and detect the optimized code vulnerability detection model to obtain a final code vulnerability detection model.

[0010] Furthermore, in S100, Python code samples are collected through an open source code platform and an internal code repository, and the Python code is annotated, specifically including: determining the granularity of the annotation, determining the accuracy of the annotation, and determining the format of the annotation; wherein, determining the granularity of the annotation includes at least determining the bounding box size of the object and the number of key points; determining the accuracy of the annotation includes at least determining the allowable error range; determining the format of the annotation includes determining the format and storage method of the annotation file.

[0011] Furthermore, in S100, the Python code is preprocessed, and the specific method includes: using regular expressions or Python's built-in ast module to remove comments and document strings in the code, using a code formatting tool to unify the code format, repairing and marking erroneous codes, and calculating code metrics, wherein the code metrics include at least the number of code lines, the number of functions, the number of classes, and the complexity; using Python's built-in ast module to convert the code into an abstract syntax tree, analyzing the module and package dependencies in the code, and understanding the structure and context of the code.

[0012] Furthermore, in S200, a code vulnerability detection neural network model based on PyTorch as a deep learning framework is constructed, and the specific method includes:

[0013] Determine the structure of the neural network model, specifically including determining the number of neurons in the input layer, hidden layer and output layer of the neural network model; wherein the number of neurons in the input layer matches the number of features of the input data, the number of neurons in the output layer matches the number of targets to be predicted, and the number of hidden layers and the number of neurons in each layer matches the complexity of the problem and the size of the data;

[0014] An activation function of the neural network model is determined, and the activation function is used by neurons to process input signals, and the activation function includes at least ReLU, sigmoid and tanh. A loss function of the neural network model is determined, and the loss function is used to measure the gap between the model prediction result and the actual result, and the loss function includes at least a mean square error function and a cross entropy loss function. An Adam optimizer is selected, and the optimizer is used to adjust the parameters of the model to minimize the loss function.

[0015] Furthermore, in S300, based on the Python code sample library, the code vulnerability detection neural network model is trained, and the specific method includes:

[0016] Dividing the Python code sample library into a training set, a validation set and a test set;

[0017] The code vulnerability detection neural network model is trained, and the model parameters are iteratively adjusted using the training set data to minimize the loss function. At the end of each epoch, the validation set is used to evaluate the model performance, and strategies such as early stopping or adjusting the learning rate are adopted to prevent overfitting;

[0018] Evaluate and tune the model, and use accuracy, recall, and F1 value evaluation indicators to evaluate the performance of the model; use cross-validation method to evaluate the model to ensure the stability and reliability of the evaluation results; optimize the model according to the evaluation results, including adjusting hyperparameters and improving model architecture; integrate multiple independently trained models to improve the generalization ability and robustness of the model.

[0019] Furthermore, in S300, the optimized code vulnerability detection model is detected, and the specific method includes: using a static code analysis tool to detect vulnerabilities in the code, wherein the vulnerabilities include at least syntax errors, unused variables, and undefined reference problems; using a security analysis tool to identify potential security vulnerabilities by converting the source code into a tree structure composed of syntax nodes; using the Safety or pip-check-requirements tool to check whether there are known security vulnerabilities in the project dependencies.

[0020] In a second aspect, an embodiment of the present invention discloses a system for Python code vulnerability detection based on deep learning, including: a Python code sample library generation unit, a code vulnerability detection neural network model construction unit, a code vulnerability detection neural network model training unit, and a code vulnerability detection model detection unit; wherein:

[0021] A Python code sample library generation unit, used to collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library;

[0022] A code vulnerability detection neural network model building unit, which is used to build a code vulnerability detection neural network model based on PyTorch as a deep learning framework;

[0023] A code vulnerability detection neural network model training unit, used to train the code vulnerability detection neural network model based on the Python code sample library;

[0024] The code vulnerability detection model detection unit is used to evaluate and optimize the code vulnerability detection model, detect the optimized code vulnerability detection model, and obtain the final code vulnerability detection model.

[0025] Furthermore, a Python code sample library generation unit is used to collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library; the specific method includes: determining the granularity of the annotation, determining the accuracy of the annotation, and determining the format of the annotation; wherein, determining the granularity of the annotation includes at least determining the size of the bounding box of the object and the number of key points; determining the accuracy of the annotation includes at least determining the allowable error range; determining the format of the annotation includes determining the format and storage method of the annotation file; using regular expressions or Python's built-in ast module to remove comments and document strings in the code, using a code formatting tool to unify the code format, repairing and marking erroneous codes, and calculating code metrics, wherein the code metrics include at least the number of code lines, the number of functions, the number of classes, and the complexity; using Python's built-in ast module to convert the code into an abstract syntax tree, analyzing the module and package dependencies in the code, and understanding the structure and context of the code.

[0026] Furthermore, a code vulnerability detection neural network model construction unit is used to construct a code vulnerability detection neural network model based on PyTorch as a deep learning framework; the specific method includes: determining the structure of the neural network model, specifically including determining the number of neurons in the input layer, hidden layer and output layer of the neural network model; wherein the number of neurons in the input layer matches the number of features of the input data, the number of neurons in the output layer matches the number of targets to be predicted, and the number of hidden layers and the number of neurons in each layer match the complexity of the problem and the scale of the data;

[0027] An activation function of the neural network model is determined, and the activation function is used by neurons to process input signals, and the activation function includes at least ReLU, sigmoid and tanh. A loss function of the neural network model is determined, and the loss function is used to measure the gap between the model prediction result and the actual result, and the loss function includes at least a mean square error function and a cross entropy loss function. An Adam optimizer is selected, and the optimizer is used to adjust the parameters of the model to minimize the loss function.

[0028] In a third aspect, an embodiment of the present invention discloses an electronic device, including:

[0029] one or more processors;

[0030] A memory for storing one or more programs;

[0031] When the one or more programs are executed by the one or more processors, the one or more processors implement the Python code vulnerability detection method.

[0032] The beneficial effects of the above technical solution provided by the embodiment of the present invention include at least:

[0033] The present invention discloses a method for Python code vulnerability detection based on deep learning, including: collecting Python code samples, annotating and preprocessing the Python code, and generating a Python code sample library; constructing a code vulnerability detection neural network model based on PyTorch as a deep learning framework; training the code vulnerability detection neural network model based on the Python code sample library; evaluating and optimizing the code vulnerability detection model, and detecting the optimized code vulnerability detection model to obtain a final code vulnerability detection model. The deep learning model of the present invention can learn the syntax, semantics and context information of the code, so as to more accurately identify vulnerabilities and improve detection accuracy and efficiency; the present invention discovers potential vulnerabilities in the early stage of code development, so as to avoid the vulnerability being exploited in the subsequent stage to cause losses and enhance code security; the present invention realizes real-time detection and feedback, helps developers to fix vulnerabilities in time, and improves development efficiency and quality.

[0034] The technical solution of the present invention is further described in detail below through the accompanying drawings and embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] The accompanying drawings are used to provide a further understanding of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and do not constitute a limitation of the present invention. In the accompanying drawings:

[0036] Figure 1 This is a flowchart of a method for detecting Python code vulnerabilities based on deep learning in Example 1 of the present invention;

[0037] Figure 2 This is a schematic diagram of the structure of an electronic device in Example 3 of the present invention. DETAILED DESCRIPTION

[0038] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.

[0039] In order to solve the problems existing in the prior art, an embodiment of the present invention provides a method and system for Python code vulnerability detection based on deep learning.

[0040] Example 1

[0041] The present invention discloses a method for detecting Python code vulnerabilities based on deep learning. Figure 1 In, including:

[0042] S100. Collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library; specifically, collect Python code samples through open source code platforms and internal code repositories, and the open source code platforms can be open source projects (such as GitHub), online programming platforms (such as LeetCode, HackerRank), tutorial websites (such as GeeksforGeeks, Stack Overflow), etc., as well as the company's internal git code repository, etc.

[0043] In S100 of the present embodiment, Python code samples are collected through an open source code platform and an internal code repository, and the Python code is annotated, specifically including: determining the granularity of the annotation, determining the accuracy of the annotation, and determining the format of the annotation; wherein, determining the granularity of the annotation includes at least determining the bounding box size of the object and the number of key points; determining the accuracy of the annotation includes at least determining the allowable error range; determining the format of the annotation includes determining the format and storage method of the annotation file.

[0044] S200. Build a code vulnerability detection neural network model based on PyTorch as a deep learning framework; specifically, PyTorch is an open source deep learning framework developed by Facebook's Artificial Intelligence Research Group (FAIR). It has been widely used in academia and industry, especially in natural language processing (NLP), computer vision (CV), and reinforcement learning. PyTorch is known for its flexibility, ease of use, and powerful GPU support, allowing developers to easily build and train deep learning models.

[0045] In S200 of this embodiment, a code vulnerability detection neural network model based on PyTorch as a deep learning framework is constructed, and the specific method includes:

[0046] Determine the structure of the neural network model, specifically including determining the number of neurons in the input layer, hidden layer and output layer of the neural network model; wherein the number of neurons in the input layer matches the number of features of the input data, the number of neurons in the output layer matches the number of targets to be predicted, and the number of hidden layers and the number of neurons in each layer matches the complexity of the problem and the size of the data;

[0047] An activation function of the neural network model is determined, and the activation function is used by neurons to process input signals, and the activation function includes at least ReLU, sigmoid and tanh. A loss function of the neural network model is determined, and the loss function is used to measure the gap between the model prediction result and the actual result, and the loss function includes at least a mean square error function and a cross entropy loss function. An Adam optimizer is selected, and the optimizer is used to adjust the parameters of the model to minimize the loss function.

[0048] S300. Based on the Python code sample library, the code vulnerability detection neural network model is trained;

[0049] Specifically, in S300 of this embodiment, the code vulnerability detection neural network model is trained based on the Python code sample library, and the specific method includes:

[0050] Dividing the Python code sample library into a training set, a validation set and a test set;

[0051] The code vulnerability detection neural network model is trained, and the model parameters are iteratively adjusted using the training set data to minimize the loss function. At the end of each epoch, the validation set is used to evaluate the model performance, and strategies such as early stopping or adjusting the learning rate are adopted to prevent overfitting;

[0052] Evaluate and tune the model, and use accuracy, recall, and F1 value evaluation indicators to evaluate the performance of the model; use cross-validation method to evaluate the model to ensure the stability and reliability of the evaluation results; optimize the model according to the evaluation results, including adjusting hyperparameters and improving model architecture; integrate multiple independently trained models to improve the generalization ability and robustness of the model.

[0053] S400. Evaluate and optimize the code vulnerability detection model, and detect the optimized code vulnerability detection model to obtain a final code vulnerability detection model.

[0054] In S400 of this embodiment, the optimized code vulnerability detection model is detected, and the specific method includes: using static code analysis tools such as PyLint, PyFlakes, Pylama, etc. to detect vulnerabilities in the code, and the vulnerabilities include at least syntax errors, unused variables, and undefined reference problems; using security analysis tools such as Bandit and SonarQube to identify potential security vulnerabilities by converting source code into a tree structure composed of syntax nodes; using Safety or pip-check-requirements tools to check whether there are known security vulnerabilities in project dependencies.

[0055] This embodiment discloses a method for Python code vulnerability detection based on deep learning, including: collecting Python code samples, annotating and preprocessing the Python code, and generating a Python code sample library; constructing a code vulnerability detection neural network model based on PyTorch as a deep learning framework; training the code vulnerability detection neural network model based on the Python code sample library; evaluating and optimizing the code vulnerability detection model, and detecting the optimized code vulnerability detection model to obtain a final code vulnerability detection model. The deep learning model of the present invention can learn the syntax, semantics, and context information of the code, so as to more accurately identify vulnerabilities and improve detection accuracy and efficiency; the present invention discovers potential vulnerabilities early in the code development stage, thereby avoiding the vulnerability from being exploited in subsequent stages to cause losses and enhance code security; the present invention realizes real-time detection and feedback, helping developers to fix vulnerabilities in a timely manner and improve development efficiency and quality.

[0056] Example 2

[0057] Based on the same inventive concept, the embodiment of the present disclosure also provides a system for Python code vulnerability detection based on deep learning, including: a Python code sample library generation unit, a code vulnerability detection neural network model construction unit, a code vulnerability detection neural network model training unit, and a code vulnerability detection model detection unit; wherein:

[0058] A Python code sample library generation unit, used to collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library;

[0059] A code vulnerability detection neural network model building unit, which is used to build a code vulnerability detection neural network model based on PyTorch as a deep learning framework;

[0060] A code vulnerability detection neural network model training unit, used to train the code vulnerability detection neural network model based on the Python code sample library;

[0061] The code vulnerability detection model detection unit is used to evaluate and optimize the code vulnerability detection model, detect the optimized code vulnerability detection model, and obtain the final code vulnerability detection model.

[0062] In some preferred embodiments, a Python code sample library generation unit is used to collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library; the specific method includes: determining the granularity of the annotation, determining the accuracy of the annotation, and determining the format of the annotation; wherein, determining the granularity of the annotation includes at least determining the size of the bounding box of the object and the number of key points; determining the accuracy of the annotation includes at least determining the allowable error range; determining the format of the annotation includes determining the format and storage method of the annotation file; using regular expressions or Python's built-in ast module to remove comments and document strings in the code, using a code formatting tool to unify the code format, repairing and marking erroneous code, and calculating code metrics, wherein the code metrics include at least the number of code lines, the number of functions, the number of classes, and the complexity; using Python's built-in ast module to convert the code into an abstract syntax tree, analyzing the module and package dependencies in the code, and understanding the structure and context of the code.

[0063] In some preferred embodiments, the code vulnerability detection neural network model construction unit is used to construct a code vulnerability detection neural network model based on PyTorch as a deep learning framework; the specific method includes: determining the structure of the neural network model, specifically including determining the number of neurons in the input layer, hidden layer and output layer of the neural network model; wherein the number of neurons in the input layer matches the number of features of the input data, the number of neurons in the output layer matches the number of targets to be predicted, and the number of hidden layers and the number of neurons in each layer match the complexity of the problem and the scale of the data;

[0064] An activation function of the neural network model is determined, and the activation function is used by neurons to process input signals, and the activation function includes at least ReLU, sigmoid and tanh. A loss function of the neural network model is determined, and the loss function is used to measure the gap between the model prediction result and the actual result, and the loss function includes at least a mean square error function and a cross entropy loss function. An Adam optimizer is selected, and the optimizer is used to adjust the parameters of the model to minimize the loss function.

[0065] Among them, other specific working methods of the Python code sample library generation unit, the code vulnerability detection neural network model construction unit, the code vulnerability detection neural network model training unit and the code vulnerability detection model detection unit have been recorded in detail in Example 1, and will not be repeated in this embodiment.

[0066] Example 3

[0067] Based on the same inventive concept, an embodiment of the present disclosure also provides an electronic device. Figure 2 FIG. 1 is a schematic diagram of the structure of an electronic device according to an embodiment of the present disclosure. Figure 2 As shown, an embodiment of the present disclosure provides an electronic device including: one or more processors 101, a memory 102, and one or more I / O interfaces 103. The memory 102 stores one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement any optimization method in the above embodiments; the one or more I / O interfaces 103 are connected between the processor and the memory, and are configured to implement information interaction between the processor and the memory.

[0068] Among them, the processor 101 is a device with data processing capabilities, including but not limited to a central processing unit (CPU), etc.; the memory 102 is a device with data storage capabilities, including but not limited to random access memory (RAM, more specifically SDRAM, DDR, etc.), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory (FLASH); the I / O interface (read-write interface) 103 is connected between the processor 101 and the memory 102, and can realize information interaction between the processor 101 and the memory 102, including but not limited to a data bus (Bus), etc.

[0069] In some embodiments, the processor 101 , the memory 102 , and the I / O interface 103 are connected to each other via a bus 104 , and further connected to other components of the computing device.

[0070] In some embodiments, the one or more processors 101 include a field programmable gate array.

[0071] According to an embodiment of the present disclosure, a computer-readable medium is further provided, wherein a computer program is stored on the computer-readable medium, wherein when the program is executed by a processor, the steps in any optimization method in the above-mentioned embodiment are implemented.

[0072] It should be understood that the specific order or hierarchy of steps in the disclosed process is an example of an exemplary method. Based on design preferences, it should be understood that the specific order or hierarchy of steps in the process can be rearranged without departing from the scope of protection of the present disclosure. The attached method claims present the elements of the various steps in an exemplary order and are not intended to be limited to the specific order or hierarchy described.

[0073] In the above detailed description, various features are grouped together in a single embodiment to simplify the disclosure. This method of disclosure should not be interpreted as reflecting an intention that the embodiments of the claimed subject matter require more features than are clearly stated in each claim. On the contrary, as reflected in the appended claims, the invention is in a state of having less than all the features of the disclosed individual embodiments. Therefore, the appended claims are hereby expressly incorporated into the detailed description, with each claim standing on its own as a separate preferred embodiment of the invention.

[0074] Those skilled in the art will also appreciate that the various illustrative logic blocks, modules, circuits, and algorithmic steps described in conjunction with the embodiments herein can all be implemented as electronic hardware, computer software, or a combination thereof. In order to clearly illustrate the interchangeability between hardware and software, various illustrative components, blocks, modules, circuits, and steps are generally described above around their functions. Whether such functions are implemented as hardware or software depends on specific applications and the design constraints imposed on the entire system. A skilled person can implement the described functions in an alternative manner for each specific application, but such implementation decisions should not be interpreted as departing from the scope of protection of the present disclosure.

[0075] The steps of the method or algorithm described in conjunction with the embodiments herein may be directly embodied as hardware, a software module executed by a processor, or a combination thereof. The software module may be located in a RAM memory, a flash memory, a ROM memory, an EPROM memory, an EEPROM memory, a register, a hard disk, a mobile disk, a CD-ROM, or any other form of storage medium well known in the art. An exemplary storage medium is connected to the processor so that the processor can read information from the storage medium and can write information to the storage medium. Of course, the storage medium may also be an integral part of the processor. The processor and the storage medium may be located in an ASIC. The ASIC may be located in a user terminal. Of course, the processor and the storage medium may also be present in a user terminal as discrete components.

[0076] For software implementation, the techniques described in this application can be implemented with modules (e.g., procedures, functions, etc.) that perform the functions described in this application. These software codes can be stored in a memory unit and executed by a processor. The memory unit can be implemented within the processor or outside the processor. In the latter case, it is coupled to the processor in a communication manner via various means, which are well known in the art.

[0077] The above description includes examples of one or more embodiments. Of course, it is impossible to describe all possible combinations of components or methods for the purpose of describing the above embodiments, but it should be recognized by those skilled in the art that the various embodiments may be further combined and arranged. Therefore, the embodiments described herein are intended to cover all such changes, modifications and variations that fall within the scope of protection of the appended claims. In addition, with respect to the term "comprising" used in the specification or claims, the word is covered in a manner similar to the term "including", just as "including," is explained as a transitional word in the claims. In addition, any term "or" used in the specification of the claims is intended to mean "non-exclusive or".

Claims

1. A method for Python code vulnerability detection based on deep learning, characterized in that: include: S100. Collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library; S200. Build a code vulnerability detection neural network model based on PyTorch as a deep learning framework; S300. Based on the Python code sample library, the code vulnerability detection neural network model is trained; S400. Evaluate and optimize the code vulnerability detection model, and detect the optimized code vulnerability detection model to obtain a final code vulnerability detection model.

2. A method for detecting Python code vulnerabilities based on deep learning as claimed in claim 1, characterized in that: In S100, Python code samples are collected through an open source code platform and an internal code repository, and the Python code is annotated, specifically including: determining the granularity of the annotation, determining the accuracy of the annotation, and determining the format of the annotation; wherein, determining the granularity of the annotation includes at least determining the bounding box size of the object and the number of key points; determining the accuracy of the annotation includes at least determining the allowable error range; determining the format of the annotation includes determining the format and storage method of the annotation file.

3. The method for detecting Python code vulnerabilities based on deep learning as claimed in claim 1, characterized in that: In S100, the Python code is preprocessed, and the specific method includes: using regular expressions or Python's built-in ast module to remove comments and document strings in the code, using a code formatting tool to unify the code format, repairing and marking erroneous codes, and calculating code metrics, wherein the code metrics include at least the number of code lines, the number of functions, the number of classes, and the complexity; using Python's built-in ast module to convert the code into an abstract syntax tree, analyzing the module and package dependencies in the code, and understanding the structure and context of the code.

4. The method for detecting Python code vulnerabilities based on deep learning as claimed in claim 1, characterized in that: In S200, a code vulnerability detection neural network model based on PyTorch as a deep learning framework is built. The specific methods include: Determine the structure of the neural network model, specifically including determining the number of neurons in the input layer, hidden layer and output layer of the neural network model; wherein the number of neurons in the input layer matches the number of features of the input data, the number of neurons in the output layer matches the number of targets to be predicted, and the number of hidden layers and the number of neurons in each layer matches the complexity of the problem and the size of the data; An activation function of the neural network model is determined, and the activation function is used by neurons to process input signals, and the activation function includes at least ReLU, sigmoid and tanh. A loss function of the neural network model is determined, and the loss function is used to measure the gap between the model prediction result and the actual result, and the loss function includes at least a mean square error function and a cross entropy loss function. An Adam optimizer is selected, and the optimizer is used to adjust the parameters of the model to minimize the loss function.

5. The method for detecting Python code vulnerabilities based on deep learning as claimed in claim 1, characterized in that: In S300, based on the Python code sample library, the code vulnerability detection neural network model is trained, and the specific method includes: Dividing the Python code sample library into a training set, a validation set and a test set; The code vulnerability detection neural network model is trained, and the model parameters are iteratively adjusted using the training set data to minimize the loss function. At the end of each epoch, the validation set is used to evaluate the model performance, and strategies such as early stopping or adjusting the learning rate are adopted to prevent overfitting; Evaluate and tune the model, and use accuracy, recall, and F1 value evaluation indicators to evaluate the performance of the model; use cross-validation method to evaluate the model to ensure the stability and reliability of the evaluation results; optimize the model according to the evaluation results, including adjusting hyperparameters and improving model architecture; integrate multiple independently trained models to improve the generalization ability and robustness of the model.

6. A method for detecting Python code vulnerabilities based on deep learning as claimed in claim 1, characterized in that: In S400, the optimized code vulnerability detection model is detected, and the specific method includes: using a static code analysis tool to detect vulnerabilities in the code, wherein the vulnerabilities include at least syntax errors, unused variables, and undefined reference problems; using a security analysis tool to identify potential security vulnerabilities by converting the source code into a tree structure composed of syntax nodes; using the Safety or pip-check-requirements tool to check whether there are known security vulnerabilities in the project dependencies.

7. A Python code vulnerability detection system based on deep learning, characterized in that: include: Python code sample library generation unit, code vulnerability detection neural network model construction unit, code vulnerability detection neural network model training unit, code vulnerability detection model detection unit; wherein: A Python code sample library generation unit, used to collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library; A code vulnerability detection neural network model building unit, which is used to build a code vulnerability detection neural network model based on PyTorch as a deep learning framework; A code vulnerability detection neural network model training unit, used to train the code vulnerability detection neural network model based on the Python code sample library; The code vulnerability detection model detection unit is used to evaluate and optimize the code vulnerability detection model, detect the optimized code vulnerability detection model, and obtain the final code vulnerability detection model.

8. A method for detecting Python code vulnerabilities based on deep learning as claimed in claim 7, characterized in that: A Python code sample library generation unit is used to collect Python code samples, annotate and preprocess the Python code, and generate a Python code sample library; the specific method includes: determining the granularity of the annotation, determining the accuracy of the annotation, and determining the format of the annotation; wherein determining the granularity of the annotation includes at least determining the size of the bounding box of the object and the number of key points; determining the accuracy of the annotation includes at least determining the allowable error range; determining the format of the annotation includes determining the format and storage method of the annotation file; using regular expressions or Python's built-in ast module to remove comments and document strings in the code, using a code formatting tool to unify the code format, repairing and marking erroneous codes, and calculating code metrics, wherein the code metrics include at least the number of code lines, the number of functions, the number of classes, and the complexity; using Python's built-in ast module to convert the code into an abstract syntax tree, analyzing the module and package dependencies in the code, and understanding the structure and context of the code.

9. A method for detecting Python code vulnerabilities based on deep learning as claimed in claim 7, characterized in that: A code vulnerability detection neural network model building unit is used to build a code vulnerability detection neural network model based on PyTorch as a deep learning framework; the specific method includes: determining the structure of the neural network model, specifically including determining the number of neurons in the input layer, hidden layer and output layer of the neural network model; wherein the number of neurons in the input layer matches the number of features of the input data, the number of neurons in the output layer matches the number of targets to be predicted, and the number of hidden layers and the number of neurons in each layer match the complexity of the problem and the scale of the data; An activation function of the neural network model is determined, and the activation function is used by neurons to process input signals, and the activation function includes at least ReLU, sigmoid and tanh. A loss function of the neural network model is determined, and the loss function is used to measure the gap between the model prediction result and the actual result, and the loss function includes at least a mean square error function and a cross entropy loss function. An Adam optimizer is selected, and the optimizer is used to adjust the parameters of the model to minimize the loss function.

10. An electronic device, characterized in that: include: one or more processors; A memory for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement any vulnerability detection method in claims 1-6.

Citation Information

Cited By

  • Code analysis detection method and system based on large language model

    CN120066935A