A malicious npm package detection method, device, equipment and medium

By extracting feature data from npm packages and utilizing classification models and secondary detection logic, the problem of low efficiency and low accuracy in the detection of malicious npm packages in existing technologies is solved, achieving efficient and accurate identification of malicious npm packages.

CN115718918BActive Publication Date: 2026-11-27HANGZHOU DBAPPSECURITY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202211482921.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-24
Publication Date
2026-11-27
Estimated Expiration
2042-11-24

AI Technical Summary

Technical Problem

Existing technologies are insufficient for efficiently and accurately detecting and identifying malware in npm packages, leading to the mislabeling of legitimate packages or the omission of malicious packages.

Method used

By extracting feature data from npm packages, a classification model is used for preliminary classification. If the package is classified as normal, a secondary detection is performed, including source code reconstructing and executable file comparison, to determine the maliciousness of the package.

Benefits of technology

It has achieved automated monitoring of malicious npm packages, improving detection efficiency and accuracy, and avoiding misclassifying malicious packages as normal packages.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115718918B_ABST
    Figure CN115718918B_ABST
Patent Text Reader

Abstract

The application discloses a malicious npm package detection method and device, equipment and medium, including: extracting feature data of a to-be-detected npm package; inputting the feature data into a classification model to obtain a classification result of the to-be-detected npm package; when the classification result is a normal package, detecting the to-be-detected npm package based on a preset detection logic to obtain a detection result. In this way, the automatic monitoring of the npm package is realized, and the malicious package is avoided from being marked as a normal package, so that the efficiency and accuracy of the malicious npm package detection can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of package detection technology, and in particular to a method, apparatus, device and medium for detecting malicious npm packages. Background Technology

[0002] JavaScript has been popular for a long time, and the V8 engine has further propelled its evolution into a server-side development language. Its corresponding package manager, npm (Node.js Package Manager), contains a vast number of reusable packages. The community's enthusiasm has fueled the development of npm and, consequently, Node.js. Publishing a new package on npm only requires registering an npm account, and updating a package to a new version simply involves uploading and synchronizing to the registry. Malicious packages have become a major concern in recent years because even if they are quickly detected and removed, their short-lived presence in the registry can still impact many end-users.

[0003] Currently, due to the large volume of npm package releases, traditional manual or semi-automated review methods are insufficient, and there are issues with low accuracy caused by incorrectly marking normal packages or omitting malicious packages. Summary of the Invention

[0004] In view of this, the purpose of this application is to provide a method, apparatus, device, and medium for detecting malicious npm packages, which can improve the efficiency and accuracy of malicious npm package detection. The specific solution is as follows:

[0005] Firstly, this application discloses a method for detecting malicious npm packages, including:

[0006] Extract feature data from the npm package to be detected;

[0007] The feature data is input into the classification model to obtain the classification result of the npm package to be detected;

[0008] If the classification result is a normal package, the npm package to be detected is detected based on the preset detection logic to obtain the detection result.

[0009] Optionally, the detection of the npm package to be detected based on preset detection logic to obtain the detection result includes:

[0010] The source code of the npm package to be detected is reconstructed to obtain a reconstructed file. Based on the reconstructed file and the information of the npm package to be detected in the registry, it is determined whether the npm package to be detected is a malicious package, and the detection result is obtained.

[0011] Alternatively, the executable file within the npm package to be detected can be compared with known malicious packages to obtain the detection result.

[0012] Optionally, the extraction of feature data of the npm package to be detected includes:

[0013] Extract feature data from the source code or executable file within the npm package to be detected;

[0014] And / or, extract feature data of the installation-time running script within the npm package to be detected;

[0015] And / or, extract version feature data of the npm package to be detected based on the documentation within the npm package to be detected.

[0016] Optionally, extracting feature data from the source code or executable file within the npm package to be detected includes:

[0017] Extract feature data of target operations from the source code or executable file of the npm package to be detected; the target operations include operations to obtain user personal information, operations to access specific system resources, and operations to call specific APIs;

[0018] And / or, extract the Shannon entropy from the source code or executable file.

[0019] Optionally, the operation of calling specific APIs includes the operation of calling dynamically generated code APIs, and the method includes:

[0020] Regular expression matching is performed on the source code or executable file in the npm package to be detected. If a template engine is matched, keyword matching is performed on the code generated by the template engine to obtain feature data of operations that call dynamically generated code APIs.

[0021] Optionally, extracting the feature data of the installation-time running script within the npm package to be detected includes:

[0022] Match the functions used to generate code in the installation runtime script, extract the function content of the functions, and obtain the feature data of the installation runtime script.

[0023] Optionally, the step of extracting version feature data of the npm package to be detected based on the documentation within the npm package to be detected includes:

[0024] The semantic features of the documentation are extracted using a preset semantic analysis logic, and the release interval between the npm package to be detected and the previous version is obtained to obtain the version feature data of the npm package to be detected.

[0025] Secondly, this application discloses a malicious npm package detection device, comprising:

[0026] The feature data extraction module is used to extract feature data of the npm package to be detected.

[0027] The classification result acquisition module is used to input the feature data into the classification model to obtain the classification result of the npm package to be detected;

[0028] The detection result acquisition module is used to detect the npm package to be detected based on a preset detection logic when the classification result is a normal package, and obtain the detection result.

[0029] Thirdly, this application discloses an electronic device, including a memory and a processor, wherein:

[0030] The memory is used to store computer programs;

[0031] The processor is used to execute the computer program to implement the aforementioned malicious npm package detection method.

[0032] Fourthly, this application discloses a computer-readable storage medium for storing a computer program, wherein the computer program, when executed by a processor, implements the aforementioned malicious npm package detection method.

[0033] As can be seen, this application first extracts the feature data of the npm package to be detected, then inputs the feature data into a classification model to obtain the classification result of the npm package to be detected. When the classification result is a normal package, the npm package to be detected is then detected based on a preset detection logic to obtain a detection result. That is, this application first uses a classifier to output the classification result of the npm package to be detected based on its feature data. When the classification result is a normal package, the preset detection logic is called to perform a secondary detection on the npm package to be detected. This achieves automated monitoring of npm packages and avoids marking malicious packages as normal packages, thus improving the efficiency and accuracy of malicious npm package detection. Attached Figure Description

[0034] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of this application. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0035] Figure 1 This is a flowchart of a malicious npm package detection method disclosed in this application;

[0036] Figure 2 This application discloses a flowchart of a specific method for detecting malicious npm packages.

[0037] Figure 3 This is a schematic diagram of the structure of a malicious npm package detection device disclosed in this application;

[0038] Figure 4 This is a structural diagram of an electronic device disclosed in this application. Detailed Implementation

[0039] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0040] First, let's explain the technical terms used in this application: npm is the default package manager for Node.js, the JavaScript runtime. JavaScript is the most popular scripting language on the internet. It can be used for HTML and the web, and is widely used on servers, PCs, laptops, tablets, and smartphones. TypeScript is a free and open-source programming language; it's a superset of JavaScript that extends JavaScript's syntax. Tree-sitter is a parser generator tool and incremental parsing library that builds concrete syntax trees for source code files and efficiently updates the syntax tree while editing the source file. It supports parsing multiple programming languages, including Python, Java, and C.

[0041] npm is a cornerstone of the JavaScript and TypeScript ecosystem, hosting a vast number of packages, ranging from simple utility libraries to complex frameworks and even entire applications. Every day, developers worldwide submit tens of thousands of updates and release hundreds of new packages. Due to npm's popularity, it has become a prime target for malicious attackers. These attackers introduce malware when releasing new packages or updating existing ones, potentially tampering with or leaking sensitive data of users who install these or any packages. Defending against such attacks is crucial for maintaining the integrity of the software supply chain, but the sheer volume of package updates means that complete manual review is impossible.

[0042] To fundamentally solve the problem of attacks against npm, two approaches can be taken: First, block these malicious packages from being synchronized to the registry, i.e., strictly enforce review mechanisms to check and clean up these malicious packages from the time they are uploaded until they are actually synchronized to the registry; second, implement access control to restrict the package installation process and reduce the attack surface. In the process of implementing this application, the inventors discovered that: 1. Since malicious attackers may release a malicious npm package by stealing the credentials of npm package maintainers, simply trusting the npm package provider or publisher is not applicable. 2. Malicious npm packages often do not publicly disclose their source code to avoid detection, so pure source code detection methods may not be suitable. 3. Attackers often release multiple identical copies of the same malicious package under different names. However, since the package metadata may differ, traditional classifiers sometimes fail to detect these copies. 4. In a very short period of time, developers release a large number of public package versions, including new packages and updated versions of existing packages, so traditional manual or semi-automated review and classification methods cannot handle such a large volume. 5. Due to the extremely rapid update speed of npm packages, a quick method is needed to classify whether an npm package is malicious. To address this issue, this application provides a malicious npm package detection scheme that improves the efficiency and accuracy of malicious npm package detection.

[0043] See Figure 1 As shown in the figure, this application discloses a method for detecting malicious npm packages, including:

[0044] Step S11: Extract feature data of the npm package to be detected.

[0045] In specific implementations, feature data of the source code or executable file within the npm package to be tested can be extracted; and / or, feature data of the installation-time script within the npm package to be tested can be extracted; and / or, version feature data of the npm package to be tested can be extracted based on its documentation. It is understood that an npm package may include source code / executable files, installation-time scripts, and documentation. When the publisher does not wish to release the source code, it can release it as an executable file. The installation-time script is a script that performs environment configuration and other operations during the installation of the npm package. The documentation is typically a description of the npm package and may include descriptions of software functions and updated features.

[0046] Furthermore, the extraction of feature data from the source code or executable file within the npm package to be detected can specifically include: extracting feature data from target operations within the source code or executable file of the npm package to be detected; the target operations include operations to obtain user personal information, operations to access specific system resources, operations to call specific APIs; and / or, extracting the Shannon entropy of the source code or executable file. Specifically, operations to obtain user personal information can include operations to obtain user accounts and passwords; operations to access specific system resources can include file operations (e.g., read / write), process creation operations (creating new processes), network requests (sending and receiving data), etc. Operations to call specific APIs can include operations to call APIs with encryption functions, operations to call APIs with data encoding, and operations to call APIs with dynamically generated code. Furthermore, the source code may be compressed short code.

[0047] In specific implementations, string matching can be used to extract feature data from operations that retrieve user personal information. Furthermore, API keywords can be used to match feature data from operations that access specific system resources or call specific APIs, including matching using operating system APIs and other API keywords. If the operation of calling specific APIs includes calling dynamically generated code APIs, regular expression matching can be performed on the source code or executable file within the npm package to be detected. If a template engine is matched, keyword matching is performed on the code generated by the template engine to obtain feature data from the operation of calling dynamically generated code APIs. Here, keyword matching can be performed using API keywords. A template engine is an engine used to generate code. The feature data of the matched target operation can include functions and function content.

[0048] Furthermore, the functions used to generate code in the installation runtime script can be matched, and the function content of the functions can be extracted to obtain the feature data of the installation runtime script.

[0049] Furthermore, semantic features of the documentation can be extracted using preset semantic analysis logic, and the release interval between the npm package to be detected and its previous version can be obtained to acquire version feature data of the npm package to be detected. The release interval is the current version release time minus the previous version release time; if the npm package to be detected is an initial version, the release interval is set to 0. In other words, the version feature data includes semantic features and the release interval. Thus, by using version features, further detection of other versions of the same package can be performed, enabling the detection not only of malicious updates (i.e., previously benign packages becoming malicious) but also of packages that were malicious from the outset.

[0050] Step S12: Input the feature data into the classification model to obtain the classification result of the npm package to be detected.

[0051] In a specific implementation, the classification model is a model obtained by training an initial model based on training data. The initial model can be a decision tree, a Naive Bayes classifier, or an SVM (Support Vector Machine). The training data includes feature data and label information of samples from the npm package. Furthermore, the training data for decision trees and Naive Bayes classifiers includes feature data of both normal and malicious samples, while the training data for SVM includes feature data of normal samples.

[0052] Step S13: When the classification result is a normal package, the npm package to be detected is detected based on the preset detection logic to obtain the detection result.

[0053] In a specific implementation, the source code within the npm package to be detected can be reconstructed to obtain a reconstructed file. Based on the reconstructed file and the information of the npm package to be detected in the registry, it can be determined whether the npm package to be detected is a malicious package, and a detection result can be obtained; or, the executable file within the npm package to be detected can be compared with known malicious packages to obtain a detection result.

[0054] It is understood that the registry includes registration information for npm packages, and this registration information includes the hash value of the npm package. In this embodiment, the source code within the npm package to be detected can be reconstructed to obtain a reconstructed file. The hash value of the reconstructed file is calculated. If this hash value does not match the hash value of the npm package in the registry, the package is determined to be malicious; otherwise, it is considered a normal package. Furthermore, the executable file within the npm package to be detected is compared with known malicious packages. Specifically, the MD5 hash of the executable file is calculated and compared with the MD5 hash table of known malicious packages. If a matching MD5 hash exists in the MD5 hash table, the npm package is determined to be malicious; otherwise, it is considered a normal package. This avoids the classifier misclassifying malicious packages as normal packages, improving detection accuracy.

[0055] As can be seen, this embodiment first extracts the feature data of the npm package to be detected, then inputs the feature data into a classification model to obtain the classification result of the npm package to be detected. When the classification result is a normal package, the npm package to be detected is detected based on a preset detection logic to obtain a detection result. That is, this embodiment first uses a classifier to output the classification result of the npm package to be detected based on its feature data. When the classification result is a normal package, the preset detection logic is called to perform a second detection on the npm package to be detected. This achieves automated monitoring of npm packages and avoids marking malicious packages as normal packages, thus improving the efficiency and accuracy of malicious npm package detection.

[0056] See Figure 2 As shown, Figure 2 This is a flowchart illustrating a specific malicious npm package detection method disclosed in an embodiment of this application. The classifier, or classification model, is obtained by training a machine learning classifier using labeled training data. The training data includes API features and metadata obtained using lightweight semantic scanning. The reconstructor is used to reconstruct the package using the source code, rebuilding the package from the source code and comparing it with the version published in the registry. The clone detector is used to check npm against known malicious packages, specifically calculating the MD hash of the package tarball content and comparing it with a list of hashes of known malicious packages, ignoring the package name and version specified in the package.json file (configuration file) when calculating the hash.

[0057] During the training phase, 643 malicious packets were acquired, and 1147 normal packets released on the same date were added to form a labeled corpus of 1790 packets. Since this corpus was insufficient compared to the total data volume of 1.7 million packets per version at the time, the following algorithms were used to address the extreme imbalance of the samples: decision trees, Naive Bayes classifiers, and one-class SVMs. Decision trees effectively illustrate feature importance, while Naive Bayes classifiers and SVMs can solve the sample imbalance problem. The sklearn library in Python was used. For decision trees, information gain was used as the splitting criterion. For the Naive Bayes classifier, a Bernoulli variant that can only handle Boolean features was used, thus omitting discrete features (entropy mean, standard deviation, and update time). A feature is considered present if it collapses other features to 1, otherwise 0. For the SVM, a linear kernel was chosen, and training was only performed on benign examples, as the classifier's task was to detect anomalous versions that were clearly different from benign ones. The ν parameter of SVM was determined by leave-one-out experiments on a basic corpus. This parameter approximates the expected number of outliers.

[0058] Furthermore, in this embodiment, a feature extractor can be used to extract the feature dataset of any npm package to be detected from the npm package database. The feature dataset is then input into the trained classifier to obtain the prediction result. After that, reconstruction or matching detection with known malicious packages is performed to obtain the final prediction result.

[0059] The collected npm package feature dataset includes the following features: 1. Acquiring sensitive personal information of end users: accounts, passwords, etc.; 2. Accessing specific system resources, including: 2.1 File operations: reading / writing; 2.2 Process creation: creating new processes; 2.3 Network requests: sending and receiving data; 3. Using specific APIs: 3.1 Encryption functions; 3.2 Data encoding; 3.3 Dynamic code generation; 4. Running scripts during installation; 5. Short code (to avoid detection) or binary files (executable files). Avoiding detection refers to reducing code readability through code compression, making conventional keyword detection of malicious npm inapplicable, thus evading detection. 6. Multi-version features (i.e., version feature data in the aforementioned embodiments) include: 1. Version release interval; 2. Update type described in the version update description. Furthermore, the feature extractor uses Tree-sitte to parse the JavaScript and Typescript files in the package to obtain single-package features: for operations that retrieve sensitive end-user information such as accounts and passwords, string matching is used for feature matching; for accessing specific system resources or using specific APIs, matching is performed using operating system APIs and other API keywords. For dynamic code generation feature matching template engines, keyword matching is performed on the content generated by the engine. For scripts that run during installation, `eval` and `Function` (two functions) are called to generate code, and the function content is matched. For short code or binary files, Shannon entropy is used for extraction. For multi-version features, the npm view time is used to obtain the version release interval; semantic analysis is used to obtain the update type, i.e., semantic features. For packages uploaded to the npm central repository for the first time, a pseudo-update version is introduced, setting the time between version releases to 0, and the update type is the semantic feature described in the package's documentation.

[0060] This approach effectively prevents attacks by malicious attackers, solves the problem of insufficient identification of malicious npm packages due to excessive updates and additions, automates malicious npm package detection without manual review, offers high real-time performance to keep up with version updates, and boasts high accuracy, avoiding the misclassification of legitimate packages or the omission of malicious ones. This solution allows for feature extraction and classifier running for each npm package typically within seconds. Retraining the classifier is also inexpensive and rapid, enabling continuous improvement as more results are classified.

[0061] It's important to note that decision trees extract the main feature points of npm packages. This means that decision trees can group npm packages with similar or identical features into one category. Given the known characteristics of malicious npm packages, unknown npm packages can be quickly classified based on these features (determining whether they are malicious). Naive Bayes classifier: Naive Bayes is an algorithm that uses Bayes' conditional probability for classification. In solving classification problems, it can be used to address the "inverse probability" problem. The "forward probability" problem refers to, for example, given a bag containing 2 white balls and 3 black balls, what is the probability of drawing a black ball in a random draw? However, in reality, it's more common to not know the total number and color of the balls beforehand, only the color of the drawn ball, and to infer the color distribution of the balls in the bag. This is the "inverse probability" problem. Naive Bayes classification is a classification method based on Bayes' theorem and the assumption of conditional independence of features. Originating from classical mathematical theory, it possesses a stable mathematical foundation and classification efficiency. It is a very simple classification algorithm. By calculating the probability of each category for a given item to be classified, the class of the item is determined. Without additional conditions, Naive Bayes classification will choose the class with the highest probability given the known conditions. SVM: SVM classification is an advanced version of linear classification. It adds the concept of support vectors to linear classification, aiming to find a classification line that maximizes the margin between different classes, thus avoiding validation data. Support Vector Machine is a supervised learning method widely used in statistical classification and regression analysis. Support Vector Machine belongs to the generalized linear classifier category. It can simultaneously minimize empirical error and maximize the geometric margin region; therefore, it is also called the maximum margin region classifier. Support Vector Machine maps vectors to a higher-dimensional space, where a large-margin hyperplane is built. Two parallel hyperplanes are built on either side of the separating hyperplane, maximizing the distance between the two parallel hyperplanes. It is assumed that the larger the distance or gap between the parallel hyperplanes, the smaller the total error of the classifier.

[0062] See Figure 3 As shown, this application discloses a malicious npm package detection device, comprising:

[0063] Feature data extraction module 11 is used to extract feature data of the npm package to be detected;

[0064] The classification result acquisition module 12 is used to input the feature data into the classification model to obtain the classification result of the npm package to be detected;

[0065] The detection result acquisition module 13 is used to detect the npm package to be detected based on a preset detection logic when the classification result is a normal package, and obtain the detection result.

[0066] As can be seen, this embodiment first extracts the feature data of the npm package to be detected, then inputs the feature data into a classification model to obtain the classification result of the npm package to be detected. When the classification result is a normal package, the npm package to be detected is detected based on a preset detection logic to obtain a detection result. That is, this embodiment first uses a classifier to output the classification result of the npm package to be detected based on its feature data. When the classification result is a normal package, the preset detection logic is called to perform a second detection on the npm package to be detected. This achieves automated monitoring of npm packages and avoids marking malicious packages as normal packages, thus improving the efficiency and accuracy of malicious npm package detection.

[0067] Specifically, the detection result acquisition module 13 is used to reconstruct the source code in the npm package to be detected to obtain a reconstructed file, and determine whether the npm package to be detected is a malicious package based on the reconstructed file and the information of the npm package to be detected in the registry, and obtain the detection result; or, compare the executable file in the npm package to be detected with known malicious packages to obtain the detection result.

[0068] Furthermore, the feature data extraction module 11 may specifically include:

[0069] The first feature extraction unit is used to extract feature data from the source code or executable file in the npm package to be detected;

[0070] The second feature extraction unit is used to extract feature data of the installation-time running script in the npm package to be detected.

[0071] The third feature extraction unit is used to extract version feature data of the npm package to be detected based on the documentation in the npm package to be detected.

[0072] The first feature extraction unit is specifically used for:

[0073] Extract feature data of target operations from the source code or executable file of the npm package to be detected; the target operations include operations to obtain user personal information, operations to access specific system resources, operations to call specific APIs; and / or, extract the Shannon entropy of the source code or executable file.

[0074] Furthermore, the operation of calling specific APIs includes the operation of calling dynamically generated APIs. The first feature extraction unit is specifically used to: perform regular expression matching on the source code or executable file in the npm package to be detected; if a template engine is matched, perform keyword matching on the code generated by the template engine to obtain feature data of the operation of calling dynamically generated APIs.

[0075] The second feature extraction unit is used to match the function used to generate code in the installation runtime script and extract the function content to obtain the feature data of the installation runtime script.

[0076] The third feature extraction unit is used to extract the semantic features of the documentation using a preset semantic analysis logic, and to obtain the version release interval between the npm package to be detected and the previous version, thereby obtaining the version feature data of the npm package to be detected.

[0077] See Figure 4 As shown in the figure, this application discloses an electronic device 20, including a processor 21 and a memory 22; wherein, the memory 22 is used to store a computer program; the processor 21 is used to execute the computer program, the malicious npm package detection method disclosed in the foregoing embodiment.

[0078] For details regarding the specific process of the above-mentioned malicious npm package detection method, please refer to the relevant content disclosed in the foregoing embodiments, which will not be repeated here.

[0079] Furthermore, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk, or optical disk, and the storage method can be temporary storage or permanent storage.

[0080] In addition, the electronic device 20 also includes a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26; wherein, the power supply 23 is used to provide operating voltage for the various hardware devices on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.

[0081] Furthermore, embodiments of this application also disclose a computer-readable storage medium for storing a computer program, wherein the computer program, when executed by a processor, implements the malicious npm package detection method disclosed in the foregoing embodiments.

[0082] For details regarding the specific process of the above-mentioned malicious npm package detection method, please refer to the relevant content disclosed in the foregoing embodiments, which will not be repeated here.

[0083] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.

[0084] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.

[0085] The above provides a detailed description of a method, apparatus, device, and medium for detecting malicious npm packages provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the method and its core ideas. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.

Claims

1. A method for detecting malicious npm packages, characterized in that, include: Extract feature data from the npm package to be detected; The feature data is input into the classification model to obtain the classification result of the npm package to be detected; If the classification result is a normal package, the npm package to be detected is detected based on the preset detection logic to obtain the detection result; The step of detecting the npm package to be detected based on a preset detection logic and obtaining a detection result includes: reconstructing the source code in the npm package to be detected to obtain a reconstructed file, calculating the hash value of the reconstructed file, and if the hash value of the reconstructed file is inconsistent with the hash value of the npm package to be detected in the registry, then the npm package to be detected is determined to be a malicious package; otherwise, it is a normal package, and a detection result is obtained. The extraction of feature data from the npm package to be tested includes: extracting feature data from the source code or executable file within the npm package to be tested; the extraction of feature data from the source code or executable file within the npm package to be tested includes: extracting feature data from target operations within the source code or executable file within the npm package to be tested; the target operations include operations to obtain user personal information, operations to access specific system resources, and operations to call specific APIs; operations to access specific system resources include file operations, process creation operations, and network requests; when the operation to call specific APIs includes operations to call dynamically generated APIs, regular expression matching is performed on the source code or executable file within the npm package to be tested; if a template engine is matched, keyword matching is performed on the code generated by the template engine to obtain feature data of operations to call dynamically generated APIs.

2. The malicious npm package detection method according to claim 1, characterized in that, The extraction of feature data of the npm package to be detected also includes: Extract feature data of the installation-time running scripts within the npm package to be detected; And / or, extract version feature data of the npm package to be detected based on the documentation within the npm package to be detected.

3. The method for detecting malicious npm packages according to claim 1, characterized in that, The step of extracting feature data from the source code or executable file within the npm package to be detected also includes: Extract the Shannon entropy from the source code or executable file.

4. The method for detecting malicious npm packages according to claim 2, characterized in that, The extraction of feature data from the installation-time scripts within the npm package to be detected includes: Match the functions used to generate code in the installation runtime script, extract the function content of the functions, and obtain the feature data of the installation runtime script.

5. The method for detecting malicious npm packages according to claim 2, characterized in that, The step of extracting version feature data of the npm package to be detected based on the documentation within the npm package to be detected includes: The semantic features of the documentation are extracted using a preset semantic analysis logic, and the release interval between the npm package to be detected and the previous version is obtained to obtain the version feature data of the npm package to be detected.

6. A malicious npm package detection device, characterized in that, include: The feature data extraction module is used to extract feature data of the npm package to be detected. The classification result acquisition module is used to input the feature data into the classification model to obtain the classification result of the npm package to be detected; The detection result acquisition module is used to detect the npm package to be detected based on a preset detection logic when the classification result is a normal package, and obtain the detection result. The step of detecting the npm package to be detected based on a preset detection logic and obtaining a detection result includes: reconstructing the source code in the npm package to be detected to obtain a reconstructed file, calculating the hash value of the reconstructed file, and if the hash value of the reconstructed file is inconsistent with the hash value of the npm package to be detected in the registry, then the npm package to be detected is determined to be a malicious package; otherwise, it is a normal package, and a detection result is obtained. The extraction of feature data from the npm package to be tested includes: extracting feature data from the source code or executable file within the npm package to be tested; the extraction of feature data from the source code or executable file within the npm package to be tested includes: extracting feature data from target operations within the source code or executable file within the npm package to be tested; the target operations include operations to obtain user personal information, operations to access specific system resources, and operations to call specific APIs; operations to access specific system resources include file operations, process creation operations, and network requests; when the operation to call specific APIs includes operations to call dynamically generated APIs, regular expression matching is performed on the source code or executable file within the npm package to be tested; if a template engine is matched, keyword matching is performed on the code generated by the template engine to obtain feature data of operations to call dynamically generated APIs.

7. An electronic device, characterized in that, Includes memory and processor, wherein: The memory is used to store computer programs; The processor is configured to execute the computer program to implement the malicious npm package detection method as described in any one of claims 1 to 5.

8. A computer-readable storage medium, characterized in that, Used to store computer programs, wherein the computer programs, when executed by a processor, implement the malicious npm package detection method as described in any one of claims 1 to 5.