نوع المستند : Original Article
الموضوعات الرئيسية
|
|
|
|
||
|
Al-Noor Journal for Information Technology and Cybersecurity |
||||
|
https://jncs.alnoor.edu.iq/ |
||||
|
|
||||
|
|
||||
|
Tailoring Static Code Analysis for Top 25 CWE in Python |
||||
|
|
||||
|
1 Dept. of Computer Science, College of Computer Science and Mathematics, University of Mosul, Iraq 2 ICT Research Unit, Computer Centre, University of Mosul, Iraq |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
Article information |
|
Abstract |
||
|
Article history: Received: 5 October, 2024 Revised: November, 20, 2024 Accepted December, 24, 2024 |
|
The topic of security for computers is of significant importance. Over the past decade, countless cybercrimes have been executed by exploiting software flaws. This issue has led to considerable social stress, substantial losses, and higher interest in security. Vulnerabilities in applications developed in various programming languages can be identified using various methodologies and techniques. We can employ static or dynamic methods for analysis to detect vulnerabilities. Bandit is a tool for static analysis designed to identify security vulnerabilities in Python code, examining a defined range of issues. This study introduces an additional collection of vulnerabilities, specifically the top 25 CWE, to enhance the tool's detection capabilities. The approach involves analyzing Python code and constructing an Abstract Syntax Tree (AST) using the AST library in Python. By traversing the nodes of the tree and gathering information regarding the code's characteristics, potential vulnerabilities are identified based on predefined checks for each scenario. The tool's capability for predicting all the incorporated scenarios was demonstrated after the completion of the tests added to it.
|
||
|
Keywords: python code software security static analysis vulnerability detection
|
||||
|
Correspondence: Ali Shihab
|
||||
|
|
|
|
||
|
DOI: DOI: https://doi.org/10.69513/jnfit.v1.i0.a7 ©Authors, 2025, Alnoor University. This is an open access article under the CC BY 4.0 license (http://creativecommons.org/licenses/by/4.0/). |
||||
Introduction
Software development requires a critical practice of writing secure coding in order to ensure that the software is designed to be resistant to potential threats. This process requires that there be built-in security measures to prevent common problems in the coding life cycle such as unauthorized access, injection attacks, etc.(1)). Software defects that are targeted and exploited in security attacks are called security vulnerabilities(2). Security vulnerabilities have significant impacts on millions of consumers and threaten computer systems to operate securely(3). Undetected vulnerabilities can be exploited by hackers and cause significant harm to users(4).
Software analysis includes dynamic analysis and static analysis. Possible programming errors in the code, as well as security weaknesses, are discovered through static analysis if the process is completed without the need to execute the code(5). (Nachtigall & Bodden, 2019) (6). Programming languages such as C and C++ are pivotal in the static analysis process because their type systems provide additional details to the analyst because they are static type systems, unlike dynamic type systems such as Python, where there is an increasing demand for them(7).
There are many analysis tools currently available that can statically analyze code written in Python(8). The current analysis tools that are widely used in static analysis of the Python programming language are pyflakes, mypy, pylint and others(5). Another important tool used to find vulnerabilities in Python code is the bandit tool(9). CWE, which is a classification and organization system for common vulnerabilities, helps developers identify software vulnerabilities by assigning a single CWE identifier to each vulnerability(10).
It has been observed that static analysis tools work to find vulnerabilities in software written in the Python programming language, as some of these tool’s focus on code styling issues and do not focus on security issues, and others focus on security issues, but they lack coverage of many common vulnerabilities. For example, the bandit tool does not detect many of the top 25 CWEs. The goal of the research is to add the top 25 common vulnerabilities (CWEs) to the bandit tool. After adding custom checks, the tool now covers a wider range of common vulnerabilities and is able to detect the top 25 CWEs.
The remainder of this paper is structured as follows: Section 2 covers related works. Section 3 demonstrates top 25 CWE and python. Section 4 describes the methodology. Section 5 includes results and discussion. Section 6 provides the conclusion.
Related Works
Static code analysis is a method used when we want to evaluate the source code without having to execute the programs. The purpose of the analysis is to discover errors, issues related to code quality, and security vulnerabilities. It is of great benefit in evaluating the efficiency of programs and indicating the aspects in which programmers need to strengthen their skills to improve their programs(11).
(12) improved the detection of security vulnerabilities in software by applying advanced deep learning models, particularly transformer-based models such as BERT. The research treats source code as text and models the detection process as a natural language processing (NLP) problem. By leveraging these NLP techniques, the researchers aim to detect vulnerabilities more effectively and efficiently than traditional methods like static and dynamic code analysis, which are often inaccurate and inefficient. The study also explores the use of transfer learning from written English to source code, highlighting its effectiveness in classifying security vulnerabilities in C/C++ code.
(13) developed an advanced system for detecting fine-grained software vulnerabilities in code with very slight differences between vulnerable and non-vulnerable versions. The study aims to improve the accuracy of vulnerability detection by introducing VulSniper, a model that uses an attention neural network to focus on critical features in the code. This approach allows VulSniper to effectively capture subtle distinctions in code that may lead to vulnerabilities, such as minor changes in conditions that could cause buffer overflows or resource management errors. The ultimate objective is to surpass the limitations of traditional static analysis methods, which often struggle with false positives and false negatives, by utilizing attention mechanisms and deep learning to enhance vulnerability detection accuracy, particularly for fine-grained issues.
(14) combined data-flow analysis techniques with machine learning to create a static analysis method for detecting software vulnerabilities, particularly SQL injection (SQLi) and Cross-Site Scripting (XSS) vulnerabilities in PHP applications. The study aims to improve the accuracy and efficiency of detecting these vulnerabilities by using control-flow graphs (CFGs) to extract features from code samples, which are then used to train various probabilistic machine learning classifiers. By leveraging both data-flow analysis and machine learning, the research seeks to address the limitations of existing static analysis tools and enhance the detection of vulnerabilities in real-world and open-source software applications.
(15) developed a deep learning-based method for detecting software vulnerabilities more efficiently and accurately. The researchers introduce a hybrid model that combines a convolutional neural network (CNN) with a bidirectional long short-term memory (Bi-LSTM) network, and apply a discrete Fourier transform (DFT) to convert source code into the frequency domain. This approach helps in capturing significant patterns in the code to better detect vulnerabilities. The key objective is to improve the detection of common software vulnerabilities, such as buffer errors and resource management errors, by focusing on both local and global features of the code, while also addressing challenges related to feature extraction and the diverse nature of vulnerabilities in modern software systems.
Id PY, & Wang X , 2024 (16) designed a deep learning-based method to detect software vulnerabilities in source code. Specifically, the research focuses on a hierarchical approach that first identifies whether a piece of source code is vulnerable and then pinpoints the exact lines of code responsible for the vulnerability. By using techniques inspired by natural language processing (NLP) and representing source code as binary vectors, the model leverages a bidirectional LSTM to capture dependencies between lines of code. The key objective is to improve accuracy and reduce false alarms in detecting vulnerable lines of code, which is crucial for mitigating potential cyberattacks.
(Piran, 2022) (17) investigated the existence of security vulnerabilities in software that shares similar code, such as cloned or near-duplicate code fragments. The study aims to empirically analyze vulnerabilities in C/C++ projects to determine whether the same security issues occur across applications that reuse or share similar code or business logic. By examining a dataset of 315 open-source projects, the researchers aim to identify common security flaws and assess the prevalence of vulnerabilities in similar code structures. Ultimately, the research seeks to provide insights for improving automated vulnerability detection tools by tailoring them to specific classes of vulnerabilities frequently found in cloned or similar code fragments.
(Alsamel, 2023) (18) proposed an automated tool that helps security engineers and developers classify and label software vulnerability reports with appropriate Common Weakness Enumeration (CWE) tags. The tool, called Vulnerability Report Tagger (VrT), leverages machine learning algorithms, specifically the FastText classifier, to automatically assign vulnerability types based on the descriptions found in the National Vulnerability Database (NVD). The purpose of VrT is to reduce the manual effort and potential errors involved in tagging vulnerabilities, streamline the vulnerability management process, and improve the prioritization of security fixes. This research aims to enhance the efficiency and accuracy of handling cybersecurity threats, making it easier for security professionals to manage and respond to new vulnerabilities.
(Sun & Wang, 2023) (19) improved the process of analyzing and understanding vulnerabilities by using a knowledge graph-based system that links various vulnerability databases and infers hidden relationships between vulnerabilities. The research focuses on building a vulnerability knowledge graph from CVE, CWE, CAPEC, and other data sources to enhance vulnerability correlation and prediction. This knowledge graph, combined with chain reasoning, allows the researchers to detect complex, compound vulnerabilities and uncover hidden links between software products and their associated vulnerabilities. The main objective is to improve the accuracy and efficiency of vulnerability scanning and provide better insights for cybersecurity management.
(Kang & Son, 2022) (20) developed an advanced static analysis framework, named Tracer, for detecting recurring software vulnerabilities by focusing on both syntactic and semantic similarities in the code. The research aims to address the limitations of existing vulnerability detection methods, which primarily focus on syntactic similarities and fail to detect vulnerabilities that recur with different syntactic structures but share the same underlying behavior. Tracer uses taint analysis and inter procedural data dependency to identify vulnerable code patterns and create vulnerability signatures, allowing it to detect both known and semantically similar, recurring vulnerabilities in new programs. The main objective is to improve the accuracy, robustness, and scalability of vulnerability detection in large-scale software projects.
(Id & Wang, 2024) (16) suggested a method that improves the detection and prediction of software vulnerabilities by utilizing an enhanced information gain (IG) algorithm within a deep neural network (DNN) framework. The study aims to address the challenge of incomplete vulnerability data and improve both the accuracy and speed of vulnerability detection. By using techniques such as Dropout to prevent overfitting, the model enhances its ability to extract and predict vulnerabilities effectively.
Below is a set of analysis tools for Python code, along with an explanation of some of the features and capabilities of these tools( 7 ):
TABLE 1. Comparison for Static Code Analysis Tools
|
Tool |
Focus |
False Positives |
Performance |
Style Checks |
Unique Features |
|
Bandit |
Security vulnerabilities |
Low |
Moderate |
No |
Security-focused, plugin-based |
|
Pylint |
Code style, complexity, and errors |
Moderate |
Slow (high complexity) |
Yes (PEP8 standards) |
Customizable limits (complexity and standards) |
|
Pyflakes |
Coding errors |
Low |
Fast |
No |
Fast error detection |
|
DeepSource |
Code review, performance improvement, autofix |
Very Low |
Moderate |
Yes (Autopep8, Black) |
Autofix feature, integration by CI/CD |
|
SonarQube |
Code smells, technical debt, security vulnerabilities |
Moderate (depending on rules enabled) |
Slow (depending on rules enabled) |
Yes (by all rules enabled) |
Comprehensive code quality metrics |
|
DeepCode |
Real-time feedback by AI-driven analysis |
Low |
Fast |
No |
AI-based analysis, real-time feedback |
Top 25 Cwe and Python
A list that is compiled annually, the Top 25 CWE vulnerabilities highlights the software security flaws that are the most prevalent and potentially most serious. As a result of these vulnerabilities, which are weaknesses in the design or implementation of software, attackers have the ability to take advantage of them in order to cause damage to systems or obtain improper access to data. For the purpose of assisting in the prioritization of security efforts and the development of methods for defense against attacks, the list is critically important for software developers and cybersecurity professionals(21).
As a result of the fact that they are the most widespread and severe security flaws in software that may be exploited by attackers, the Top 25 CWE vulnerabilities are extremely important. These vulnerabilities frequently result in major implications, such as the shutting down of the system, the unauthorized access of data, or the complete control over the system. Because they are simple to locate and exploit, the vulnerabilities are serious. Furthermore, they affect a wide variety of systems, which makes them frequent targets for attackers. The address of these vulnerabilities is critical for the maintenance of software security and the prevention of exploits and breaches that are significant(22).
The CWEs particularly pertinent to Python comprise the following:
For a number of different reasons, the dynamic typing, dependency on external libraries, and flexibility, the Python has substantial challenges when it comes to detecting vulnerabilities:
Methodology
Design Overview
In this work, a system was built, which is an expansion of the bandit tool, where checks were added to perform a test for common weaknesses (top 25 CWEs). The python code is received into the system and it analyzes the code, finds the weaknesses in it, and indicates the degree of severity and confidence for them.
Rule Set Tailoring for Top 25 CWE
For the purpose of ensuring that possible software vulnerabilities are recognized in an accurate and efficient manner, a set of processes was followed in order to build rules for evaluating code and finding software vulnerabilities. In order to develop a testing rule, the following fundamental procedures were carried out:
Understanding the Intended Security Vulnerability
To begin the process of developing a testing rule, the first thing you need to do is recognize and comprehend the vulnerability that you want to find. This was accomplished using the following studying:
Identifying Code Patterns
Upon identifying the vulnerability, the code patterns that might cause it were determined. This stage involved identifying the structures or behaviors analyzed in the code. for instance:
For instance, if the next code snippet is inputted into the scanning tool:
The result will indicate the potential presence of a SQL Injection vulnerability in the code.
For instance, if the next code snippet is inputted into the scanning tool:
This will be demonstrated by the result, which will show that credentials are inserted right into the code.
For example, if the following code snippet was entered into the tool for testing:
On the basis of the result, it would be clear that authorization was absent.
The dynamic typing of Python and its dependence on external libraries present numerous challenges that complicate vulnerability detection:
Integration with Bandit
The architecture needs to take into consideration a number of different things in order to successfully incorporate CWE rules with the Bandit tool for identifying security vulnerabilities in Python. The following is an in-depth explanation to the architecture, with particular focus on the alterations that are required:
Adding Hooks: In order for Bandit to be able to activate CWE-specific rules, its AST traversal scheme will need to be modified. Hooks are used to map CWE patterns with particular AST nodes. For example, hooks can be used to identify vulnerable functions such as eval or exec, which could be potential indicators of a vulnerability.
CWE Rule Configuration: The Bandit API requires an upgrade to facilitate the activation and configuration of CWE rules.
Support for Custom Plugins: It should be possible for developers to construct and register their own custom CWE rules using the API. This is something that can be accomplished by extending the design of Bandit's plugins, which will allow users to produce their own security tests.
A custom plugin is created in order to work on discovering a specific case of CWEs that was not originally present in bandit. This is done by understanding the vulnerability and the reasons that lead to it. The custom plugin is saved in a Python file with a specific name that indicates the scan it is performing, and it is called through the main file (main_code.py) when there is an analysis process to discover vulnerabilities in a specific code.
Dataset for Testing and Verification
For the purposes of testing and Verification, the datasets were selected using open-source Python projects and known vulnerable codebases (for example, vulnerable web applications and GitHub repositories).
Evaluation Criteria
After custom plugins were added to the analysis tool to detect cases of the top 25 CWEs, the detection range of the tool expanded and it is able to cover all cases of the top 25 CWEs.
The tailored tool has good capabilities and outperforms other analysis tools such as Pylint, Flake8 and others in discovering vulnerabilities, especially security issues.
Results and Discussion
The findings in table 2 demonstrate a tool's efficacy for determining the Top 25 CWE vulnerabilities:
where:
Here is how the metrics were computed(C. Lin et al., 2023):
Bandit's strengths for discovering vulnerabilities involve the following:
Considering all of these advantages, Bandit is a highly efficient tool for improving code security.
The enhanced capability of Bandit to discover CWE vulnerabilities which are generally challenging to detect in Python consist of multiple aspects:
Because of these features, Bandit is a powerful tool for detecting vulnerabilities that are difficult to identify, especially on projects that are large or complex.
Utilizing the Bandit tool to identify vulnerabilities associated with the CWE Top 25 may present challenges in mitigating false negatives or false positives. These instances include:
Future enhancements will involve the incorporation of extra CWEs and the development of future Python versions.
|
|
TABLE 2. The results. |
|||||||||||
|
|
F1 Score |
Recall |
Precision |
Accuracy |
FN |
TN |
FP |
TP |
CWE Name |
CWE ID |
||
|
|
0.90 |
1 |
0.83 |
0.90 |
0 |
4 |
1 |
5 |
Cross-Site Scripting (XSS) |
CWE-79 |
||
|
|
0.83 |
1 |
0.71 |
0.80 |
0 |
3 |
2 |
5 |
SQL Injection |
CWE-89 |
||
|
|
0.80 |
0.8 |
0.8 |
0.80 |
1 |
4 |
1 |
4 |
Path Traversal |
CWE-22 |
||
|
|
0.90 |
1 |
0.83 |
0.90 |
0 |
4 |
1 |
5 |
Cross-Site Request Forgery |
CWE-352 |
||
|
|
0.88 |
0.8 |
1 |
0.90 |
1 |
5 |
0 |
4 |
Improper Input Validation |
CWE-20 |
||
Conclusion
After work was done on adding a set of tests that detect cases of the top 25 CWEs to the bandit tool, which is one of the analysis tools that performs static analysis on codes written in the Python language and which focuses when analyzing security issues in the code, the tool’s ability to detect vulnerabilities has become larger and able to cover all common vulnerabilities top 25 CWEs. The tool will be developed in future work to also include dynamic analysis of the code in order to give us more accurate results when testing to detect vulnerabilities.
Acknowledgments
The authors are grateful to Computer Science Department at the University of Mosul/ Iraq for the collaborative efforts that make this work achieved and to the ICT Research unit at Computer Center of University of Mosul/ Iraq, for the support during the course of this work.
References
12.Ziems, N. (2021). Security Vulnerability Detection Using Deep Learning Natural Language Processing
14.Kronjee J, Hommersom, A, & Vranken H. Discovering software vulnerabilities using data-flow analysis and machine learning. ACM International Conference Proceeding Series. 2018;https://doi.org/10.1145/3230833.3230856
15.Cao D, Huang J, Zhang X, & Liu X. FTCLNet: Convolutional LSTM with Fourier Transform for Vulnerability Detection. Proceedings - 2020 IEEE 19th International Conference on Trust, Security and Privacy in Computing and Communications, TrustCom 2020; 539–546. https://doi.org/ 10.1109/TrustCom50675.2020.00078.
16 Id PY, & Wang X. Vulnerability extraction and prediction method based on improved information gain algorithm. 2024; 1–23. https://doi.org/10.1371/journal.pone.0309809.
17.Piran A. Vulnerability Analysis of Similar Code. December 2021. https://doi.org/10.1109/QRS54544.2021.00071
19.Sun X, & Wang Z. Intelligent Association of CVE Vulnerabilities Based on Chain Reasoning. 2023;28–34. https://doi.org/10.3233/FAIA230788.
23.Shahab A, Engineers E, Alenezi M, Technology TS, & Nadeem M. An automated approach to fix buffer overflows. 2020;February, 3778–3788. https://doi.org/10.11591/ ijece.v10i4.pp3778-3788.
26.Chen Z, Chen B, & Chen L. An Empirical Study on Dynamic Typing Related Practices in Python Systems. Conference: ICPC '20: 28th International Conference on Program Comprehensio 2020:83–93. Doi: 0.1145/3387904.3389253
28.Shuanghe P, Peiyao L I U, & Jing H A N. A Python Security Analysis Framework in Integrity Verification and Vulnerability Detection. 20192;4(2), 141–148. Doi 10.1007/s11859-019-1379-5
3
.
12.Ziems, N. (2021). Security Vulnerability Detection Using Deep Learning Natural Language Processing
14.Kronjee J, Hommersom, A, & Vranken H. Discovering software vulnerabilities using data-flow analysis and machine learning. ACM International Conference Proceeding Series. 2018;https://doi.org/10.1145/3230833.3230856
15.Cao D, Huang J, Zhang X, & Liu X. FTCLNet: Convolutional LSTM with Fourier Transform for Vulnerability Detection. Proceedings - 2020 IEEE 19th International Conference on Trust, Security and Privacy in Computing and Communications, TrustCom 2020; 539–546. https://doi.org/ 10.1109/TrustCom50675.2020.00078.
16 Id PY, & Wang X. Vulnerability extraction and prediction method based on improved information gain algorithm. 2024; 1–23. https://doi.org/10.1371/journal.pone.0309809.
17.Piran A. Vulnerability Analysis of Similar Code. December 2021. https://doi.org/10.1109/QRS54544.2021.00071
19.Sun X, & Wang Z. Intelligent Association of CVE Vulnerabilities Based on Chain Reasoning. 2023;28–34. https://doi.org/10.3233/FAIA230788.
23.Shahab A, Engineers E, Alenezi M, Technology TS, & Nadeem M. An automated approach to fix buffer overflows. 2020;February, 3778–3788. https://doi.org/10.11591/ ijece.v10i4.pp3778-3788.
26.Chen Z, Chen B, & Chen L. An Empirical Study on Dynamic Typing Related Practices in Python Systems. Conference: ICPC '20: 28th International Conference on Program Comprehensio 2020:83–93. Doi: 0.1145/3387904.3389253
28.Shuanghe P, Peiyao L I U, & Jing H A N. A Python Security Analysis Framework in Integrity Verification and Vulnerability Detection. 20192;4(2), 141–148. Doi 10.1007/s11859-019-1379-5
3
.