Python language password API misuse automatic detection method, system and equipment and medium

By using static taint analysis technology and abstract syntax tree reconstruction in the Python language, the misuse of password APIs in Python applications is detected, which solves the problem of difficulty in misuse detection in the existing technology and achieves efficient and accurate detection results. .

CN119357958AActive Publication Date: 2025-01-24UNIV OF SCI & TECH OF CHINA +1

Patent Information

Application Number
CN202411390273.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-08
Publication Date
2025-01-24
Estimated Expiration
2044-10-08

AI Technical Summary

Technical Problem

It is difficult for the existing technology to effectively detect the misuse of password APIs in the Python language, especially in Python applications. Due to its unique syntax characteristics and advanced features, existing detection tools are prone to missed and false alarms.

Method used

By obtaining a set of Python language password API misuse rules based on cryptography security usage specifications, the program file is converted into an abstract syntax tree (AST) form, and the AST is reconstructed according to the Python language features. Then, mark the taint source, sanitization function and sink point, and use static taint analysis technology and taint detector to determine whether the program file triggers the password API misuse rule.

Benefits of technology

The function of the misuse of the password API in the Python language is implemented, the misuse of the misuse of automation detection is reduced, and the misunderstanding rate and missed reporting rate is filled.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119357958A_ABST
    Figure CN119357958A_ABST
Patent Text Reader

Abstract

The invention discloses an automatic detection method, system and device for Python language password API misuse and a medium. In the scheme provided by the invention, an abstract syntax tree is reconstructed in combination with Python language characteristics, the abstract syntax tree is reconstructed according to the Python language characteristics, node representation is simplified while function consistency is guaranteed, and the probability of misuse of the Python language password API misuse is lowered. Therefore, the false alarm rate and the missing report rate are reduced in the subsequent taint analysis process; meanwhile, a static taint analysis technology is utilized, taint propagation in the process and between functions is carried out based on reconstruction of an abstract syntax tree, and all password API misuse rules are covered through a taint detector, so that a detected password API misuse instance is determined. Generally speaking, the method fills up the blank that no password API misuse detection for the Python application program exists at present, and plays an important supporting and promoting role in the field of password application security evaluation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of cryptographic API misuse detection for Python language, and in particular to an automatic detection method, system, device and medium for cryptographic API misuse in Python language. Background Art

[0002] As the cornerstone of information system security, cryptography provides confidentiality, integrity, authenticity, non-repudiation and other related protections for information systems. Cryptography API (Application Programming Interface) is the provider of basic cryptographic services, allowing developers to easily use cryptographic service functions. However, cryptographic API can only play its security support role if it is used correctly and in compliance with regulations. In practical applications, the phenomenon of cryptographic API being misused is common. Existing literature shows that at least 52.26% of open source Python applications have at least one cryptographic API misuse. At the same time, the misuse of cryptographic API is usually difficult to detect. Most developers find it difficult to identify these problems due to their lack of professional cryptographic knowledge. These misuses do not cause program crashes, but are hidden in the system. Only in specific circumstances will security defects be exposed, and these vulnerabilities are often accompanied by serious security consequences. In recent years, cryptographic application security and cryptographic API misuse detection technology have received high and continuous attention from industry and academia.

[0003] At present, common cryptographic API misuse detection tools are mainly divided into two categories: dynamic detection and static detection. Among them, dynamic detection tools are based on the program execution process, and it is difficult to completely cover all cryptographic API statements. In addition, such tools are strongly dependent on the underlying and Hook strategies, which can easily lead to missed reports. Therefore, the current mainstream detection tools use static code analysis methods, which have a high code coverage rate and a low missed report rate. Such tools usually convert program files into IR (Intermediate Representation) to extract information flows, and then establish corresponding data flows between program execution points and their predecessors or subsequent execution points, and identify cryptographic API misuse through the data flows of program execution points and detection strategies.

[0004] Taint analysis is a practical technique for static code analysis. It tracks the flow of sensitive data to determine whether the information flow strategy is effectively implemented. Taint analysis is widely used in practical fields such as system privacy data leakage detection and system security vulnerability discovery. The taint analysis principle is based on a triple <source, sink, purification function> (<sources,sinks,sanitizers> ). Taint sources refer to the direct introduction of untrusted or confidential data into the system; taint sinks refer to the direct generation of security-sensitive operations (violating data integrity) or the leakage of private data to the outside world (violating data confidentiality); sanitizers refer to the use of data encryption or filtering of harmful operations to prevent data transmission from posing a threat to the information security of the software system. In cryptographic API misuse detection, taint analysis aims to evaluate whether the data introduced by taint sources in the program can be directly propagated to taint sinks without passing through sanitizers. For example, hard-coded key variables are directly passed into encryption functions along the information flow without being generated by a secure random number generator, which leads to security risks such as dangerous cryptographic API operations in the system.

[0005] Existing work generally has shortcomings such as unclear analysis of password misuse rules, incomplete analysis of password libraries, and incomplete data flow analysis. Furthermore, existing work mainly designs detection tools for programming languages ​​such as C / C++ and Java, while research on other high-level programming languages ​​(such as Python) is relatively rare. Since various programming languages ​​have different grammatical characteristics, the high-level language features they support will greatly hinder the detection of password API misuse. For example, the object reference passing feature of Python supports assigning functions to variables and calling the original function by calling variables with parameters. When it comes to unsafe calls to password APIs, existing detection tools will produce missed reports; furthermore, when encountering computable conditional statements, such as the judgment logic in the conditional statement is always true or always false, the existing detection tools will not perform path-sensitive processing, resulting in false positives. Therefore, it is necessary to design an automated password API misuse detection solution for the Python programming language. Summary of the invention

[0006] The purpose of the present invention is to provide a method, system, device and medium for automatic detection of cryptographic API misuse in Python language, aiming at the API encapsulation characteristics of mainstream Python cryptographic library, to realize the automatic detection function of cryptographic API misuse, to make up for the current situation that there is no cryptographic API misuse detection tool for Python language, and to shift the risk of cryptographic API misuse to the left in the security development process.

[0007] The objective of the present invention is achieved through the following technical solutions:

[0008] An automated detection method for misuse of a cryptographic API in Python, comprising:

[0009] Obtain a set of Python language cryptography API misuse rules based on cryptography security usage specifications;

[0010] Convert the analyzed program file into an abstract syntax tree IR form, and reconstruct the abstract syntax tree according to the characteristics of the Python language;

[0011] According to the Python language cryptographic API misuse rule set, mark the taint sources, purification functions and convergence points in the reconstructed abstract syntax tree;

[0012] Combining the marked taint sources, purification functions and convergence points, based on the static taint analysis technology, using a taint detector to determine whether the analyzed program file triggers any cryptographic API misuse rule in the cryptographic API misuse rule set, so as to obtain a cryptographic API misuse detection result of the analyzed program file under the corresponding cryptographic API misuse rule;

[0013] The cryptographic API misuse detection result of the analyzed program file is obtained by integrating the cryptographic API misuse detection result of the analyzed program file under the cryptographic API misuse rule set of the Python language, so as to obtain the final cryptographic API misuse detection result of the analyzed program file.

[0014] An automated detection system for misuse of cryptographic APIs in Python, including:

[0015] The misuse rule acquisition module obtains the Python language cryptography API misuse rule set based on the cryptography security usage specification;

[0016] The program file conversion module is used to convert the analyzed program file into an abstract syntax tree IR form and reconstruct the abstract syntax tree according to the characteristics of the Python language;

[0017] The node initialization module is used to mark the taint sources, purification functions and convergence points in the reconstructed abstract syntax tree according to the Python language cryptographic API misuse rule set;

[0018] a misuse detection module, for combining the marked taint sources, purification functions and convergence points, and using a taint detector to determine whether the analyzed program file triggers any cryptographic API misuse rule in the cryptographic API misuse rule set based on static taint analysis technology, so as to obtain a cryptographic API misuse detection result of the analyzed program file under the corresponding cryptographic API misuse rule;

[0019] The misuse result generation module is used to synthesize the cryptographic API misuse detection result of the analyzed program file under the cryptographic API misuse rule set of the Python language to obtain the final cryptographic API misuse detection result of the analyzed program file.

[0020] A processing device, comprising: one or more processors; a memory for storing one or more programs;

[0021] When the one or more programs are executed by the one or more processors, the one or more processors implement the aforementioned method.

[0022] A readable storage medium stores a computer program, which implements the above method when the computer program is executed by a processor.

[0023] It can be seen from the technical solutions provided by the present invention that 1) the present invention combines the characteristics of the Python language to reconstruct the abstract syntax tree, reconstructs the abstract syntax tree according to the characteristics of the Python language, simplifies the node representation while ensuring functional consistency, and thus reduces the false alarm rate and missed alarm rate in the subsequent taint analysis process. 2) The present invention utilizes static taint analysis technology, based on the reconstruction of the abstract syntax tree, to propagate taints within the process and between functions, and uses a taint detector to cover all cryptographic API misuse rules, thereby determining the detected cryptographic API misuse instances. In general, the present invention fills the gap in the current lack of cryptographic API misuse detection for Python applications, and plays an important supporting and promoting role in the field of cryptographic application security assessment. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.

[0025] Figure 1 A flowchart of a Python language cryptographic API misuse automatic detection method provided by an embodiment of the present invention;

[0026] Figure 2 A schematic diagram of an automated detection system for misuse of a cryptographic API in Python provided by an embodiment of the present invention;

[0027] Figure 3 A schematic diagram of a processing device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0028] The following is a clear and complete description of the technical solutions in the embodiments of the present invention in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the protection scope of the present invention.

[0029] First, the terms that may be used in this article are explained as follows:

[0030] The terms "include", "comprises", "contains", "has" or other descriptions with similar semantics should be interpreted as non-exclusive inclusion. For example, including certain technical feature elements (such as raw materials, components, ingredients, carriers, dosage forms, materials, dimensions, parts, components, mechanisms, devices, steps, procedures, methods, reaction conditions, processing conditions, parameters, algorithms, signals, data, products or products, etc.) should be interpreted as including not only certain technical feature elements explicitly listed, but also other technical feature elements known in the art that are not explicitly listed.

[0031] The term "consisting of..." means excluding any technical feature elements not explicitly listed. If this term is used in a claim, it will make the claim closed, so that it does not contain technical feature elements other than the technical feature elements explicitly listed, except for the conventional impurities related to them. If this term only appears in a clause of a claim, it only limits the elements explicitly listed in the clause, and the elements recorded in other clauses are not excluded from the overall claim.

[0032] The following is a detailed description of a Python language password API misuse automatic detection method, system, equipment and medium provided by the present invention. The contents not described in detail in the embodiments of the present invention belong to the prior art known to professionals in the field. If the specific conditions are not specified in the embodiments of the present invention, they are carried out according to the conventional conditions in the field or the conditions recommended by the manufacturer. If the manufacturer is not specified in the reagents or instruments used in the embodiments of the present invention, they are all conventional products that can be purchased commercially.

[0033] Embodiment 1

[0034] The present invention provides a Python language cryptographic API misuse automatic detection method, such as Figure 1 As shown, it mainly includes the following steps:

[0035] Step 1: Obtain a set of Python language cryptographic API misuse rules based on cryptographic security usage specifications.

[0036] Step 2: Convert the analyzed program file into an abstract syntax tree IR form, and reconstruct the abstract syntax tree according to the characteristics of the Python language.

[0037] Step 3: According to the Python language cryptographic API misuse rule set, mark the taint sources, purification functions and convergence points in the reconstructed abstract syntax tree.

[0038] Step 4: In combination with the marked taint sources, purification functions and convergence points, based on static taint analysis technology, use a taint detector to determine whether the analyzed program file triggers any cryptographic API misuse rule in the cryptographic API misuse rule set, so as to obtain the cryptographic API misuse detection result of the analyzed program file under the corresponding cryptographic API misuse rule.

[0039] Step 5: Under the cryptographic API misuse rule set of the Python language, the cryptographic API misuse detection result of the analyzed program file is integrated to obtain the final cryptographic API misuse detection result of the analyzed program file.

[0040] Compared with the prior art, the solution provided by the embodiment of the present invention has the following advantages:

[0041] 1. The present invention combines the characteristics of Python language to reconstruct the abstract syntax tree, rewrites the abstract syntax tree according to dynamic characteristics, object reference transfer characteristics, formatted strings, With statements, etc., simplifies node representation while ensuring functional consistency, thereby reducing false positive rate and missed negative rate in the subsequent taint analysis process.

[0042] 2. The present invention utilizes static taint analysis technology, based on the reconstruction of the abstract syntax tree, to perform taint propagation within the process and between functions, and uses a taint detector to cover all cryptographic API misuse rules, thereby determining the detected cryptographic API misuse instances.

[0043] In general, the present invention fills the gap of the current lack of cryptographic API misuse detection for Python applications, and plays an important supporting and promoting role in the field of cryptographic application security assessment.

[0044] In order to more clearly demonstrate the technical solution and technical effects provided by the present invention, the method provided by the embodiment of the present invention is described in detail with reference to specific embodiments below.

[0045] In the following embodiment, cryptographic misuse rules are sorted out for the APIs provided by 21 mainstream cryptographic libraries of the Python programming language, the triple abstraction of static taint analysis is mapped to the cryptographic API misuse detection scheme for the Python language, the abstract syntax tree is reconstructed to handle the advanced features of the Python language and the static calling relationship between functions in the program file, the static taint analysis technology is used to track the tainted data flow along the reconstructed abstract syntax tree, and five taint detectors are proposed and constructed to cover all cryptographic API misuse rules.

[0046] 1. Determine the set of rules for misuse of the Python language cryptographic API.

[0047] Password misuse rules can provide a basis for identifying password API misuse. By systematically combing through the APIs and functions of Python's mainstream password libraries, we extracted five main types of password API misuse. Then, by analyzing the best practices of the corresponding password APIs, we combed through the password misuse rules that are strongly coupled with the Python password API.

[0048] (1) Sort out the APIs provided by Python's cryptographic library.

[0049] This section mainly includes the classification of cryptographic library types and cryptographic library functions. In terms of cryptographic library types, they are classified according to the source of the cryptographic library, specifically divided into three basic categories: official standard cryptographic service library, cryptographic suite library, and single-function third-party cryptographic library. In terms of cryptographic library functions, they are classified according to the cryptographic service functions they support, specifically divided into five basic categories: symmetric cryptographic algorithms, asymmetric cryptographic algorithms, hash algorithms, SSL / TLS functions, and pseudo-random number generator functions. As shown in Table 1, the embodiments of the present invention sort out 21 mainstream cryptographic library APIs and cryptographic library functions in the Python language. Generally speaking, the mainstream cryptographic library APIs sorted out by the present invention cover most of the cryptographic usage scenarios. In addition, the present invention takes into account the scalability of the new cryptographic library API and uses YAML files for easy configuration.

[0050] Table 1: Python language mainstream cryptographic library API and function summary

[0051]

[0052]

[0053] (2) Summarize the misuse of passwords in Python projects.

[0054] In the embodiment of the present invention, five types of password misuse are summarized, which are described in detail as follows:

[0055] 1) Use weak security strength cryptographic algorithms:

[0056] 2) Using cryptographic algorithms with weak parameter configurations;

[0057] 3) Use cryptographic algorithms with weak randomness;

[0058] 4) Use weak SSL / TLS protocol algorithms;

[0059] 5) Other misuse situations.

[0060] (3) Combining the classification results with the password misuse in Python projects and based on the cryptographic security usage specifications, a set of Python language password API misuse rules is obtained.

[0061] In the embodiment of the present invention, 17 rules are sorted out for Python language, and the rules cover the above five password misuse situations. It should be noted that, in order to maintain the functional consistency of the password library API across programming languages, the password rule descriptions of the present invention are partially the same as those sorted out by other related programming languages, but some are different.

[0062] Table 2: Password misuse detection rules for Python language

[0063]

[0064]

[0065] As shown in Table 2, the embodiment of the present invention summarizes 17 password misuse detection rules for Python language, and divides password misuse situations into the following 5 categories and 17 rules:

[0066] a) Rules related to weak security strength: It is prohibited to use insecure hash cryptographic algorithms, insecure symmetric cryptographic algorithms, and insecure asymmetric cryptographic algorithms.

[0067] Specifically: According to NIST SP 800-57, cryptographic algorithms with an estimated maximum security strength of less than 112 bits (e.g., 80 bits) are no longer considered adequate. R-01, R-02, and R-03 focus on CWE-327 (i.e., Use of Broken or Compromised Cryptographic Algorithms) and prohibit the use of insecure hash cryptographic algorithms (such as MD2, MD4, MD5, SHA-1, and RIPEMD-160), insecure symmetric cryptographic algorithms (such as RC2, RC4, DES, Blowfish, and IDEA), and insecure asymmetric cryptographic algorithms (such as RSA-1024, ECDSA-P192, and DSA-1024).

[0068] b) Rules related to weak parameter configuration: It is forbidden to use the PKCS#1v1.5 padding algorithm in RSA, it is forbidden to use the ECB working mode in the symmetric encryption algorithm, and it is forbidden to use less than 1000 iterations in the PBE algorithm.

[0069] Specific: Cryptographic libraries often provide developers with various parameter options in specific APIs. However, it is important to be aware that some parameters may not be secure. R-04 prohibits the use of PKCS#1-v1.5 padding in the RSA algorithm (e.g., the PyCryptodome library) because it is vulnerable to chosen ciphertext attacks. In addition, it is recommended to use RSA with optimized asymmetric encryption padding (OAEP) for encryption and probabilistic signature schemes (PSS) for signing. R-05 prohibits the use of ECB mode in symmetric ciphers (e.g., the M2Crypto library) because of its predictability and lack of diffusion. R-06 requires password-based key derivation functions to have at least 1000 iterations (e.g., the cryptography library).

[0070] c) Rules related to weak randomness: It is forbidden to use insecure pseudo-random number generators, it is forbidden to use predictable / hard-coded salt values ​​and passwords in the PBE algorithm, it is forbidden to use predictable / hard-coded keys, it is forbidden to use non-random IVs in CBC and CFB working modes, and it is forbidden to use non-unique IVs / Nonces / Counters in CTR, OFB, GCM, and XTS working modes.

[0071] Specifically: Various cryptographically critical parameters such as salts, keys, and initialization vectors (IVs) should be randomly generated. R-07 Cryptographically insecure pseudo-random number generators (PRNGs) are not allowed to be used in security contexts such as key derivation and key agreement. Python provides both insecure PRNGs (such as the random library) and secure PRNGs (such as the secrets library). Since the random library is designed for modeling and simulation rather than security, the secrets library should be used in preference. R-08 Constant passwords / salts are prohibited in PBE, which defeats the purpose of adding randomness to derived keys. R-09 Predictable / constant cryptographic keys are prohibited, and keys should be derived from secure PRNGs rather than hard-coded in the program. R-10 and R-11 focus on the use of IVs in block ciphers, which are random numbers used to randomize encryption. Predictable IVs can lead to security vulnerabilities such as the BEAST attack. In particular, IVs / Nonces / Counters in CTR, OFB, GCM, and XTS modes should be unique, while IVs in CBC and CFB modes should be randomly generated.

[0072] d) Rules related to weak SSL / TLS protocols: It is forbidden to skip certificate / hostname verification in SSL / TLS protocols, it is forbidden to use insecure SSL / TLS protocol versions or cipher suites, and it is forbidden to turn off SSL / TLS protocol certificate verification warnings.

[0073] Specific: Due to the widespread use of SSL / TLS applications, there are many risks of cryptographic misuse associated with it. R-12 requires that certificates and hostnames be properly verified during network connections. Bypassing certificate verification or accepting all hostnames is considered unsafe. For example, in the requests library, a series of methods (such as post, get) can bypass verification by setting the verify parameter to false. R-13 prohibits the use of insecure SSL / TLS protocol versions (such as SSLv2.0, SSLv3.0, and TLSv1.0) and insecure cipher suites that may be affected by BEAST and CRIME attacks. In addition, because CBC mode is vulnerable to padding oracle attacks in client / server scenarios, cipher suites containing CBC mode (such as AES-CBC) and weak ciphers (such as DES, RC4) are unsafe. In addition, several libraries (such as urllib3 and requests) provide mechanisms to suppress all unsafe warnings. In particular, if the disable_warnings function is called, certificate verification failures will be ignored. R-14 describes this unsafe scenario.

[0074] e) Other related rules: It is prohibited to use insecure plain text protocols, it is prohibited to bind services to all network interfaces, and it is prohibited to use unverified JWT.

[0075] Specific: The remaining cryptographic API misuse rules are grouped into this group. R-15 restricts insecure protocols (e.g., HTTP, FTP, TELNET, LDAP) because they are plaintext in nature. It is recommended to choose corresponding encrypted protocols such as HTTPS, SFTP, SSH, and LDAPS. R-16 is concerned that binding to all network interfaces may accidentally expose services to traffic on unintended interfaces. R-17 emphasizes the significant risk to user identity privacy when JWT is not securely verified in authentication and authorization scenarios.

[0076] 2. Generate and reconstruct the abstract syntax tree of the program file.

[0077] In the embodiment of the present invention, the Python program file is analyzed, and after obtaining the Python program file, the analyzed program file is converted into an abstract syntax tree IR form, and the abstract syntax tree is reconstructed according to the characteristics of the Python language to simplify nodes and establish connections between nodes. The details are as follows:

[0078] (1) For dynamic features, the reconstruction of the abstract syntax tree aims to identify the parameter values ​​in dynamic methods and the fully qualified method names of cryptographic APIs. For example, the dynamic package import method of importlib is converted into an import call node according to its parameters.

[0079] Specifically, when reconstructing the abstract syntax tree according to dynamic characteristics, the main considerations include: two dynamic characteristics: the getattr method and the importlib method.

[0080] When refactoring the getattr method, the built-in dynamic attribute acquisition method getattr loads the attributes of the object at runtime. The getattr node in the abstract syntax tree is refactored into a call node, and the password API call process is matched in combination with the fully qualified API name.

[0081] When refactoring the importlib method, the built-in dynamic library import method importlib imports the specified library at runtime. By refactoring the importlib node in the abstract syntax tree into an import node, the fully qualified API name is obtained in combination with the library's import relationship to match the password API call process.

[0082] (2) In view of the object reference transfer feature, the abstract syntax tree is reconstructed to establish relationships between nodes to track the calling process.

[0083] Specifically, when reconstructing the abstract syntax tree according to the object reference transfer feature, a variable definition-use mapping table is constructed. After the variable is propagated through multiple assignments, the initial value is found through the mapping table to realize alias propagation; the variables include: regular variables and function variables; among which:

[0084] When refactoring a regular variable, the left value (variable name) of the assignment statement in the regular variable assignment operation is used as the key in the mapping table, and the right value (variable value) of the assignment statement is used as the value in the mapping table to construct a regular variable definition-use mapping table;

[0085] When refactoring a function variable, use the left value (variable name) of the assignment statement in the function variable assignment operation as the key in the mapping table, the right value (function name) of the assignment statement as the value in the mapping table, and the constructor variable definition - use the mapping table.

[0086] (3) In view of the formatted string feature, the abstract syntax tree is reconstructed by parsing the formatted string into a String node.

[0087] Specifically, when reconstructing the abstract syntax tree according to the characteristics of the formatted string, three types of formatted strings are mainly considered: C-printf style formatted strings, format style formatted strings, and interpolation formatted strings (f-string style) formatted strings; among them:

[0088] For C-style formatted strings, the C-style percent placeholders are matched with the following variables in order to form an integrated string.

[0089] For the formatting string of the formatting style, the bracket placeholders are matched with the variables in the format method in order to form an integrated string;

[0090] For a formatted string in the interpolation format string style, the interpolation format string style string is converted into a formatted string in the formatting style by matching brackets, and then converted into an integrated string.

[0091] (4) For the With statement feature, the abstract syntax tree is reconstructed by integrating additional var nodes in the With statement body to accurately locate the temporary assignment of the as clause. This method helps to identify aliasing behavior and enhances the recognition of fully qualified API names.

[0092] Specifically, the abstract syntax tree is reconstructed according to the characteristics of the With statement, the as clause in the With statement is reconstructed into a special assignment statement, and the variable node is used to record in the context established by the With statement.

[0093] 3. Mark the tainted sources, purification functions, and sinks according to the cryptographic API misuse rules.

[0094] In an embodiment of the present invention, the underlying cryptographic library API and the associated parameters in the upper-layer cryptographic misuse rules for taint analysis of the cryptographic application are determined. The cryptographic application based on the Python language uses pattern matching technology to match the fully qualified name of the relevant calling method by traversing the leaf nodes of the abstract syntax tree to mark the taint analysis role.

[0095] As shown in Table 3, the present invention defines the source, sink or sanitizer role played by the Python cryptographic library API in the taint analyzer. By deeply analyzing the APIs and parameters of 21 Python cryptographic libraries, considering the fully qualified name of the API involved in each rule, the pattern matching technology is used to mark specific roles when traversing the abstract syntax tree nodes.

[0096] Table 3: Python cryptographic library APIs and their role as source, sink, or sanitizer in taint analyzers

[0097]

[0098]

[0099] 4. Static stain analysis.

[0100] In an embodiment of the present invention, based on the static taint analysis technology, it is determined whether the program file triggers any cryptographic API misuse rule in the cryptographic API misuse rule set, so as to obtain the cryptographic API misuse detection result of the program file under the cryptographic API misuse rule. Specifically, based on the reconstructed abstract syntax, the cryptographic API misuse detection based on static taint analysis is performed for the cryptographic misuse rules in the cryptographic application. Based on the above-mentioned parsing of the program file and the reconstruction of the obtained abstract syntax tree, the source node corresponding to the rule is located in the reconstructed abstract syntax tree, and the taint propagation is performed with the source node as the starting point. Specifically, the layer-order traversal of the abstract syntax tree is performed, and the taint propagation is performed within the process and between methods of the intermediate nodes of the purification function or assignment propagation, and finally the taint detection is performed at the convergence point according to the taint detector.

[0101] In the embodiment of the present invention, the following five taint detectors are proposed to cover all cryptographic API misuse rules.

[0102] (1) An unsafe function call detector, used to identify unsafe cryptographic API calls in the program file.

[0103] Specifically, cryptographic libraries usually provide developers with a variety of API choices. However, in order to be compatible with old systems, these libraries may contain certain unsafe API functions. The detector checks the call nodes in the abstract syntax tree and matches the fully qualified name with the pattern defined by the API. For example, when the md5 function in the hashlib library is used, the detector triggers a warning alert. There are other unsafe function calls, such as unsafe symmetric encryption algorithms (R-02) and unsafe padding algorithms for RSA (R-04). For rule R-05, the pyaes library provides an unsafe AESModeOfOperationECB API for direct calling. Regarding rule R-14, the present invention considers that using APIs such as urllib3.disable_warning and requests.packages.urllib3.disable_warnings to suppress SSL / TLS certificate verification warnings is considered unsafe. For R-15, the present invention detects library calls (such as ftplib, http.client) that provide unsafe protocols (such as FTP and HTTP).

[0104] (2) A parameter value detector for identifying specific cryptographic API calls in the program file and determining unsafe parameter value calls.

[0105] Specifically, in Python cryptographic libraries, user-defined methods are often used to specify specific cryptographic primitives, such as key sizes in asymmetric algorithms and the number of iterations in PBE. The detector processes numerical parameters, directly using numerical types as taint sources and specific function calls as convergence points to analyze characteristic parameter configurations. For example, APIs for key generation in asymmetric cryptographic algorithms often use various parameters (such as key_size, bits) to generate key pairs of different sizes. Developers who lack cryptographic knowledge may generate weak keys by setting inadequate size values ​​in the API. The number of iterations for password-based key derivation methods should exceed 1,000 (R-06).

[0106] (3) a parameter option detector for identifying specific cryptographic API calls in the program file and determining unsafe parameter option calls.

[0107] Specifically, when parameters go beyond simple numerical ranges and contain specific complex properties for selection, the detector identifies insecure option properties that violate established rules as potential sources of concern. For example, for R-03, the cryptographic API uses attribute curves (such as ecdsa.NIST192p) to represent different key lengths for elliptic curve cryptographic algorithms. For R-05, R-10, and R-11, they are derived from the same type of API and provide different encryption mode options, such as Crypto.Cipher.AES.MODE_ECB. For R-12 and R-13, they involve SSL / TLS version and verification issues. Libraries often provide many insecure SSL / TLS versions (such as OpenSSL.SSL.TLSv1_METHOD) and allow disabling certificate verification during network connections (such as setting ssl.CERT_NONE to the cert_reqs parameter in urllib3.PoolManager).

[0108] (4) A randomness detector for identifying specific cryptographic API calls in the program file and determining hard-coded parameter calls and insecure random number generator API calls.

[0109] Specifically, the weak randomness addressed by the Crypto API Misuse Rules (R-07 to R-11) requires that keys, initialization vectors, and salt values ​​have sufficient randomness to ensure strong security. This detector involves two steps when detecting parameters passed to the sink. First, determine whether the parameter has been filtered through a clean point (e.g., secrets.token_bytes). Second, evaluate whether the parameter is marked as tainted, indicating the use of an insecure pseudo-random number generator (PRNG) method (e.g., the random library) or a constant object (e.g., a hard-coded string or bytes). In particular, in R-10 and R-11, it is essential to implement a two-layer detection check for randomness, which involves tracking parameter options (e.g., Cryptodome.Cipher.DES.MODE_CTR) to confirm the encryption mode and performing detailed randomness checks on the initialization vector.

[0110] (5) A specific structure detector for identifying specific cryptographic API calls in the program file and determining unsafe specific structure calls.

[0111] Specifically, the detector performs preliminary structural parsing to assess rule violations when the detected API parameters display a specific structure (e.g., tuple, dictionary, or formatted string). For example, in R-15, the request.post function requires parameters (e.g., url) to be HTTP links, which is validated by matching their string values ​​with regular expressions. In addition, in R-16, the bind method in the socket library requires an address parameter, which is a tuple containing the IP address and port number for the socket binding. Further, in R-17, the jwt.decode function includes an options dictionary parameter that provides extended decoding and verification options (e.g., handling unverified JWT signatures).

[0112] 5. Obtain the results of the password API misuse detection.

[0113] In the embodiment of the present invention, based on the password misuse rules set in the password misuse rule set and combined with the judgment method for each password misuse rule in the fourth part above, the password API misuse detection result of the program file is obtained.

[0114] The above-mentioned scheme provided by the embodiment of the present invention can determine the cryptographic misuse rules that are strongly coupled with the Python language by sorting out the cryptographic algorithm API and cryptographic functions of the mainstream Python cryptographic library, and specifically reconstruct the abstract syntax tree to adapt to the advanced characteristics of the Python dynamic language, thereby designing an automatic detection method for cryptographic API misuse based on taint analysis technology, and providing a comprehensive and intuitive cryptographic application security assessment mechanism for applications developed based on the Python language.

[0115] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above embodiments can be implemented by software, or by means of software plus necessary general hardware platforms. Based on such understanding, the technical solutions of the above embodiments can be embodied in the form of software products, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.), including several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in the various embodiments of the present invention.

[0116] Embodiment 2

[0117] The present invention also provides a Python language password API misuse automatic detection system, which is mainly used to implement the method provided in the above embodiment, such as Figure 2 As shown, the system mainly includes:

[0118] The misuse rule acquisition module obtains the Python language cryptography API misuse rule set based on the cryptography security usage specification;

[0119] The program file conversion module is used to convert the analyzed program file into an abstract syntax tree IR form and reconstruct the abstract syntax tree according to the characteristics of the Python language;

[0120] The node initialization module is used to mark the taint sources, purification functions and convergence points in the reconstructed abstract syntax tree according to the Python language cryptographic API misuse rule set;

[0121] a misuse detection module, for combining the marked taint sources, purification functions and convergence points, and using a taint detector to determine whether the analyzed program file triggers any cryptographic API misuse rule in the cryptographic API misuse rule set based on static taint analysis technology, so as to obtain a cryptographic API misuse detection result of the analyzed program file under the corresponding cryptographic API misuse rule;

[0122] The misuse result generation module is used to synthesize the cryptographic API misuse detection result of the analyzed program file under the cryptographic API misuse rule set of the Python language to obtain the final cryptographic API misuse detection result of the analyzed program file.

[0123] Considering that the main technical details and technical effects involved in the above system have been explained in the above embodiments, they will not be repeated here.

[0124] Technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the system can be divided into different functional modules to complete all or part of the functions described above.

[0125] Embodiment 3

[0126] The present invention also provides a processing device, such as Figure 3 As shown, it mainly includes: one or more processors; a memory for storing one or more programs; wherein, when the one or more programs are executed by the one or more processors, the one or more processors implement the methods provided in the aforementioned embodiments.

[0127] Furthermore, the processing device also includes at least one input device and at least one output device; in the processing device, the processor, memory, input device, and output device are connected via a bus.

[0128] In the embodiment of the present invention, the specific types of the memory, input device and output device are not limited; for example:

[0129] The input device may be a touch screen, an image acquisition device, a physical button or a mouse, etc.;

[0130] The output device may be a display terminal;

[0131] The memory may be a random access memory (RAM) or a non-volatile memory, such as a disk memory.

[0132] Embodiment 4

[0133] The present invention also provides a readable storage medium storing a computer program, which implements the method provided in the above embodiment when the computer program is executed by a processor.

[0134] In the embodiment of the present invention, the readable storage medium is a computer-readable storage medium and can be set in the aforementioned processing device, for example, as a memory in the processing device. In addition, the readable storage medium can also be a U disk, a mobile hard disk, a read-only memory (ROM), a disk or an optical disk, etc., which can store program codes.

[0135] The above is only a preferred specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed in the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.

Claims

1. A Python language cryptographic API misuse automatic detection method, characterized in that: include: Obtain a set of Python language cryptography API misuse rules based on cryptography security usage specifications; Convert the analyzed program file into an abstract syntax tree intermediate representation, and reconstruct the abstract syntax tree according to Python language characteristics; According to the Python language cryptographic API misuse rule set, mark the taint sources, purification functions and convergence points in the reconstructed abstract syntax tree; Combining the marked taint sources, purification functions and convergence points, based on the static taint analysis technology, using a taint detector to determine whether the analyzed program file triggers any cryptographic API misuse rule in the cryptographic API misuse rule set, so as to obtain a cryptographic API misuse detection result of the analyzed program file under the corresponding cryptographic API misuse rule; The cryptographic API misuse detection result of the analyzed program file is obtained by integrating the cryptographic API misuse detection result of the analyzed program file under the cryptographic API misuse rule set of the Python language, so as to obtain the final cryptographic API misuse detection result of the analyzed program file.

2. The Python language cryptographic API misuse automatic detection method according to claim 1 is characterized in that: The Python language cryptography API misuse rule set obtained based on the cryptography security usage specification is obtained based on the following steps: Sorting out the APIs provided by Python's cryptographic library, including: classifying the types of cryptographic libraries and their functions; Summarize the misuse of passwords in Python projects; Combining the classification results with the password misuse in Python projects and according to the cryptography security usage specifications, a set of Python language password API misuse rules is obtained.

3. The Python language cryptographic API misuse automatic detection method according to claim 1 is characterized in that: The reconstruction of the abstract syntax tree according to the Python language characteristics includes: Reconstruct the abstract syntax tree according to dynamic features, including getattr method and importlib method; among them: When refactoring the getattr method, the built-in dynamic attribute acquisition method getattr loads the attributes of the object at runtime. The getattr node in the abstract syntax tree is reconstructed as a call node, and the password API call process is matched in combination with the fully qualified API name. When refactoring the importlib method, the built-in dynamic library import method importlib imports the specified library at runtime. By reconstructing the importlib node in the abstract syntax tree into an import node, the fully qualified API name is obtained in combination with the import relationship of the library to match the password API call process.

4. The Python language cryptographic API misuse automatic detection method according to claim 1 is characterized in that: The reconstruction of the abstract syntax tree according to the Python language characteristics includes: The abstract syntax tree is reconstructed according to the object reference transfer feature, and a variable definition-use mapping table is constructed. After the variable is propagated through multiple assignments, the initial value is found through the mapping table; the variables include: regular variables and function variables; wherein: When refactoring a regular variable, the left value of the assignment statement in the regular variable assignment operation is used as the key in the mapping table, and the right value of the assignment statement is used as the value in the mapping table to construct a regular variable definition-use mapping table; When refactoring a function variable, use the left value of the assignment statement in the function variable assignment operation as the key in the mapping table, the right value of the assignment statement as the value in the mapping table, and the constructor variable definition - use the mapping table.

5. The Python language cryptographic API misuse automatic detection method according to claim 1 is characterized in that: The reconstruction of the abstract syntax tree according to the Python language characteristics includes: Reconstruct the abstract syntax tree according to the characteristics of the formatted string. The formatted string includes: C-style formatted string, formatted-style formatted string, and interpolation-style formatted string; among them: For C-style formatted strings, the C-style percent placeholders are matched with the following variables in order to form an integrated string. For the formatting string of the formatting style, the bracket placeholders are matched with the variables in the format method in order to form an integrated string; For a formatted string in the interpolation format string style, the interpolation format string style string is converted into a formatted string in the formatting style by matching brackets, and then converted into an integrated string.

6. The Python language cryptographic API misuse automatic detection method according to claim 1 is characterized in that: The reconstruction of the abstract syntax tree according to the Python language characteristics includes: Reconstruct the abstract syntax tree according to the characteristics of the With statement, reconstruct the as clause in the With statement into a special assignment statement, and use the variable node to record in the context established by the With statement.

7. The Python language cryptographic API misuse automatic detection method according to claim 1 is characterized in that: The stain detector comprises: an unsafe function call detector for identifying unsafe cryptographic API calls in the program file; A parameter value detector for identifying specific cryptographic API calls in the program file and determining unsafe parameter value calls; A parameter option detector for identifying specific cryptographic API calls in the program file and determining unsafe parameter option calls; A randomness detector for identifying specific cryptographic API calls in the program file and determining hard-coded parameter calls and insecure random number generator API calls; A specific structure detector is used to identify specific cryptographic API calls in the program file and determine unsafe specific structure calls.

8. A Python language cryptographic API misuse automatic detection system, characterized in that: include: The misuse rule acquisition module obtains the Python language cryptography API misuse rule set based on the cryptography security usage specification; A program file conversion module is used to convert the analyzed program file into an abstract syntax tree intermediate representation and reconstruct the abstract syntax tree according to Python language characteristics; The node initialization module is used to mark the taint sources, purification functions and convergence points in the reconstructed abstract syntax tree according to the Python language cryptographic API misuse rule set; a misuse detection module, for combining the marked taint sources, purification functions and convergence points, and using a taint detector to determine whether the analyzed program file triggers any cryptographic API misuse rule in the cryptographic API misuse rule set based on static taint analysis technology, so as to obtain a cryptographic API misuse detection result of the analyzed program file under the corresponding cryptographic API misuse rule; The misuse result generation module is used to synthesize the cryptographic API misuse detection result of the analyzed program file under the cryptographic API misuse rule set of the Python language to obtain the final cryptographic API misuse detection result of the analyzed program file.

9. A processing 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 the method according to any one of claims 1 to 7.

10. A readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Software safety code analyzer based on static analysis of source code and testing method therefor

    CN101017458A

  • Automatic detection method and device for misuse of Go language password

    CN115828224A

  • Python code static analysis method and device

    CN116303053A

  • Runtime detection of injection attacks on web applications via static and dynamic analysis

    US20210099483A1

Cited By

  • Test method and equipment of intelligent door lock and storage medium

    CN119621591A