Webshell file detection method and system
By converting the code of the webshell file into an abstract syntax tree and replacing the identifier, obtaining features and metadata, and inputting the classification model for detection, the problem of low accuracy of webshell file detection in the existing technology is solved, and the detection of highly concealed and unknown objects is solved. Effective identification and detection of files.
Patent Information
- Application Number
- CN202410877494.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-02
- Publication Date
- 2024-10-08
- Estimated Expiration
- 2044-07-02
AI Technical Summary
When existing machine learning detection solutions detect encoded, encrypted, and obfuscated webshell files, the actual content of the files cannot be restored through traditional preprocessing and feature extraction processes, which affects detection accuracy and causes false positives and missed negatives. .
Convert the code of the script file to be detected into an abstract syntax tree, replace special identifiers with universal tags, obtain file features and metadata features, and input the trained classification model for detection. Eliminate and optimize feature data through recursive features to improve detection. accuracy.
It effectively identifies highly concealed and unknown webshell files, reduces system resource requirements, and improves detection accuracy and efficiency.
Smart Images

Figure CN118747367A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of network security technology, and in particular to a method and system for detecting webshell files. Background Art
[0002] Webshell is a code execution environment in the form of Web scripts such as ASP, ASPX, JSP and PHP. Network administrators can use it to manage servers and website backends, while attackers can use webshell to execute system commands, steal data, implant viruses, extort core data, and perform SEO Trojans on Web servers, which are extremely harmful. In order to evade security detection, attackers often use strategies such as code obfuscation and encryption to disguise webshells.
[0003] However, when detecting encoded, encrypted, and obfuscated websehll files, existing machine learning detection solutions are unable to restore the actual content of the files through traditional preprocessing and feature extraction processes, which in turn affects the accuracy of detection and leads to false positives and false negatives. Summary of the invention
[0004] The present application provides a webshell file detection method, which aims to solve the problem that the websehll file detection method in the prior art cannot detect websehll files that are highly concealed or have never appeared.
[0005] To achieve the above objectives, this application adopts the following technical solutions:
[0006] A webshell file detection method of the present application includes the following steps:
[0007] Convert the code in the script file to be detected into a first abstract syntax tree, and replace special identifiers in the first abstract syntax tree with general tags;
[0008] Restoring the replaced first abstract syntax tree to a first script file, and obtaining file features of the first script file and metadata features of the script file to be detected;
[0009] The file features and metadata features are input into a trained classification model, and it is determined whether the script file to be detected is a webshell file according to the detection result of the classification model.
[0010] Preferably, the replacing the special identifier in the first abstract syntax tree with a general tag comprises:
[0011] Special identifiers in the first abstract syntax tree are extracted, wherein the special identifiers include variable names, function names, class names, and method names, and the variable names, function names, class names, and method names are replaced with general tags.
[0012] Preferably, before converting the code in the script file to be detected into the first abstract syntax tree, the method further includes:
[0013] Non-script code characters in the script file to be detected are cleaned, wherein the non-script code characters include comments and blank lines.
[0014] Preferably, the file features include risk function, file content information entropy, file compression ratio, file content coincidence index, risk function, file longest word, file longest word information entropy, function nesting depth, array nesting depth, loop nesting depth and judgment nesting depth;
[0015] The metadata characteristics include file size, modification date, creation date, and last access date.
[0016] Preferably, the function nesting depth, array nesting depth, loop nesting depth and the method for obtaining the nesting depth include:
[0017] Recursively traverse the first abstract syntax tree after replacement, and when entering a new structure block, modify the corresponding nesting depth according to the type of the new structure block until the traversal is completed, and use each maximum depth as the final value of the corresponding nesting depth.
[0018] Preferably, the training process of the classification model includes:
[0019] Acquire an original sample file, and convert the code in the original sample file into a second abstract syntax tree;
[0020] Replace the special identifier in the second abstract syntax tree with a common tag, and restore the replaced second abstract syntax tree to the first sample file;
[0021] Acquire the file features of the first sample file and the metadata features of the original sample file, and optimize the file features of the first sample file and the metadata features of the original sample file by using a recursive feature elimination method;
[0022] The optimized feature data is input into the xgboost model for training to obtain a classification model for webshell files.
[0023] Preferably, the method further comprises:
[0024] Whether the detection result is correct is judged manually. If the result is a false positive, the type of the script file to be detected is re-marked, and the classification model is incrementally trained based on the script file to be detected.
[0025] A webshell file detection system, comprising:
[0026] A conversion module, configured to convert the code in the script file to be detected into a first abstract syntax tree, and replace special identifiers in the first abstract syntax tree with general tags;
[0027] An extraction module, used to restore the replaced first abstract syntax tree to a first script file, and obtain file features of the first script file and metadata features of the script file to be detected;
[0028] The classification module is used to input the file features and metadata features into a trained classification model, and determine whether the script file to be detected is a webshell file according to the detection result of the classification model.
[0029] An electronic device comprises a memory and a processor, wherein the memory is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor to implement a method for detecting a webshell file as described in any one of the above.
[0030] A computer-readable storage medium storing a computer program, wherein the computer program enables a computer to implement a method for detecting a webshell file as described in any one of the above when executed.
[0031] The present invention has the following beneficial effects:
[0032] This application relies on the constructed classification model to analyze and detect webshell files, reducing the demand for system resources and effectively identifying highly concealed and unknown webshell files. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0034] Figure 1 This is a flow chart of a method for detecting a webshell file provided in an embodiment of the present application;
[0035] Figure 2 It is a structural schematic diagram of a webshell file detection system provided in an embodiment of the present application;
[0036] Figure 3 It is a schematic diagram of an electronic device for implementing a method for detecting a webshell file provided in an embodiment of the present application. DETAILED DESCRIPTION
[0037] The following will be combined with the accompanying drawings to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.
[0038] The terms "first", "second", etc. in the claims and specification of the present application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that the terms used in this way are interchangeable under appropriate circumstances. This is merely a way of distinguishing objects with the same properties in the embodiments of the present application. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, so that a process, method, system, product or apparatus that includes a series of units is not necessarily limited to those units, but may include other units not expressly listed or inherent to these processes, methods, products or apparatus.
[0039] Example
[0040] like Figure 1 As shown, the present application provides a method for detecting a webshell file, comprising the following steps:
[0041] S110: Convert the code in the script file to be detected into a first abstract syntax tree, and replace special identifiers in the first abstract syntax tree with general tags.
[0042] After obtaining the script file to be detected, it is first preprocessed. In this embodiment, the preprocessing includes two parts: cleaning and code abstraction.
[0043] Among them, cleaning refers to cleaning out non-script code characters in the script file to be detected. Preferably, non-script code characters include comments and blank lines. This cleaning can reduce data noise and ensure that subsequent analysis targets the core content of the script file.
[0044] Next, code abstraction is performed. Specifically, the cleaned code in the script file to be detected is converted into a first abstract syntax tree through php-parser, wherein php-parser is a PHP library that aims to convert PHP code into an abstract syntax tree AST, and extract special identifiers in the first abstract syntax tree such as variable names, function names, class names, and method names, and replace these special characters with common characters, such as renaming variables to var1, var2, and var3. This can eliminate personalized information in the code, so that similar code structures can be recognized as the same or similar results even when using different special identifiers.
[0045] S120: Restore the replaced first abstract syntax tree to a first script file, and obtain file features of the first script file and metadata features of the script file to be detected.
[0046] The preprocessed files will be used for file feature extraction. Specifically, the file features include risk function, file content information entropy, file compression ratio, file content coincidence index, risk function, file longest word, file longest word information entropy, function nesting depth, array nesting depth, loop nesting depth and judgment nesting depth.
[0047] In this embodiment, the replaced first abstract syntax tree will be converted into a script file, namely the first script file, wherein file features such as risk function, file content information entropy, file compression ratio, file content overlap index, risk function, file longest word and file longest word information entropy are all determined based on the first script file. This is the prior art and will not be elaborated here.
[0048] Nested depth features such as function nesting depth, array nesting depth, loop nesting depth and judgment nesting depth are obtained based on the first abstract syntax tree after identifier replacement. Specifically, the syntax tree is traversed recursively, and each time a new structure block is entered, such as if judgment, while loop, for loop, function call and array, the nesting depth type of the depth plus one can be determined according to the type of the structure block. During the traversal process, the maximum depth encountered is recorded. This maximum value is the final value of the corresponding nesting depth. The nested depth feature can intuitively represent the complexity of the code. High-complexity code may often be malicious.
[0049] At the same time, metadata features of the script file to be detected are extracted, including file size, modification date, creation date and last access date, which can help identify abnormal behavior. For example, frequent file modifications in a short period of time may indicate that the file has been maliciously modified.
[0050] S130: input the file features and metadata features into a trained classification model, and determine whether the script file to be detected is a webshell file according to the detection result of the classification model.
[0051] The file features extracted from the first script file and the metadata features extracted from the script file to be detected are input into the trained classification model. The classification model analyzes these features and outputs the detection result. According to the detection result, it can be determined whether the script file to be detected is a webshell file. Then, manual determination is used to determine whether the detection result is correct. If it is a false alarm, the manual judgment result is fed back to the system for review by security experts to ensure that the false alarm is accurately marked. Based on the feedback result, the system re-marks the type of the script file to be detected as a normal file or a webshell file, and performs incremental training of the classification model accordingly.
[0052] The training process of the classification model specifically includes the following steps:
[0053] Acquire an original sample file, and convert the code in the original sample file into a second abstract syntax tree;
[0054] Replace the special identifier in the second abstract syntax tree with a common tag, and restore the replaced second abstract syntax tree to the first sample file;
[0055] Acquire the file features of the first sample file and the metadata features of the original sample file, and optimize the file features of the first sample file and the metadata features of the original sample file by using a recursive feature elimination method;
[0056] The optimized feature data is input into the xgboost model for training to obtain a classification model for webshell files.
[0057] The processing method of the original sample file is the same as that of the script file to be detected, that is, preprocessing is performed first, and after cleaning the non-script code characters in the original sample file, the code in the cleaned original sample file is converted into a second abstract syntax tree, so as to replace the variable name, function name, class name and method name with a common mark, and then the replaced second abstract syntax tree is converted into the first sample file, and the file features of the first sample file and the metadata features of the original sample file are extracted. Then, the recursive feature elimination method RFE is used to eliminate and optimize all the extracted features. Preferably, the RFE class in the scikit-learn library is used, the target number of features is set to 10, and the step length of each iteration is 1, that is, one feature with the lowest importance is removed at each step, and the sample file is divided into a training set and a validation set, and RFE is fitted on the training data. After RFE training is completed, the features selected by RFE are used to train the xgboost model and save the model. Then, an independent test set is used to verify the performance of the model after feature selection. The performance indicators of the model, such as accuracy and recall, are recorded. Then, the number of features of RFE is reduced by 1, and the above steps are repeated until the performance indicators of the model in the test set no longer improve. The model with the best performance indicators is used as the final model, that is, the classification model for webshell files is obtained. If the performance of the model is not good, it can be optimized in two aspects: (1) adjust the xgboost parameter learning rate, tree depth and number of trees, (2) increase the training data and retrain. More training data can help the model learn more data patterns and features, reduce excessive dependence on individual samples, and thus improve the generalization ability of the model.
[0058] The present application can effectively detect new webshell files that are highly concealed or have never appeared before, with high accuracy and low system resource usage.
[0059] like Figure 2 As shown, the present application also provides a webshell file detection system, including a conversion module, an extraction module and a classification module, wherein:
[0060] The conversion module is used to convert the code in the script file to be detected into a first abstract syntax tree, and replace the special identifier in the first abstract syntax tree with a general tag;
[0061] The extraction module is used to restore the replaced first abstract syntax tree to a first script file, and obtain file features of the first script file and metadata features of the script file to be detected;
[0062] The classification module is used to input the file features and metadata features into a trained classification model, and determine whether the script file to be detected is a webshell file according to the detection result of the classification model.
[0063] like Figure 3 As shown, the present application also provides an electronic device, including a memory 301 and a processor 302, wherein the memory 301 is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor 302 to implement the above-mentioned method for detecting a webshell file.
[0064] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the electronic device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0065] The present application also provides a computer-readable storage medium storing a computer program, wherein the computer program enables a computer to implement a method for detecting a webshell file as described above when executed.
[0066] Exemplarily, the computer program may be divided into one or more modules / units, one or more modules / units are stored in the memory 301 and executed by the processor 302, and the input interface 305 and the output interface 306 complete the I / O interface transmission of data to complete the present invention. The one or more modules / units may be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program in the computer device.
[0067] The computer device may be a computing device such as a desktop computer, a notebook, a PDA, a cloud server, etc. The computer device may include, but is not limited to, a memory 301 and a processor 302. Those skilled in the art may understand that this embodiment is only an example of a computer device and does not constitute a limitation on the computer device. The computer device may include more or fewer components, or a combination of certain components, or different components. For example, the computer device may also include an input device 307, a network access device, a bus, etc.
[0068] The processor 302 may be a central processing unit (CPU), or other general-purpose processors 302, digital signal processors 302 (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor 302 may be a microprocessor 302, or the processor 302 may be any conventional processor 302, etc.
[0069] The memory 301 may be an internal storage unit of a computer device, such as a hard disk or memory of the computer device. The memory 301 may also be an external storage device of the computer device, such as a plug-in hard disk equipped on the computer device, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. Further, the memory 301 may also include both an internal storage unit of the computer device and an external storage device. The memory 301 is used to store computer programs and other programs and data required by the computer device. The memory 301 may also be used to temporarily store in the output device 308, and the aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory ROM303, a random access memory RAM304, a disk or an optical disk.
[0070] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions within the technical scope disclosed by 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 method for detecting a webshell file, characterized in that: The following steps are involved: Convert the code in the script file to be detected into a first abstract syntax tree, and replace special identifiers in the first abstract syntax tree with general tags; Restoring the replaced first abstract syntax tree to a first script file, and obtaining file features of the first script file and metadata features of the script file to be detected; The file features and metadata features are input into a trained classification model, and it is determined whether the script file to be detected is a webshell file according to the detection result of the classification model.
2. A method for detecting a webshell file according to claim 1, characterized in that: The replacing the special identifier in the first abstract syntax tree with a general tag includes: Special identifiers in the first abstract syntax tree are extracted, wherein the special identifiers include variable names, function names, class names, and method names, and the variable names, function names, class names, and method names are replaced with general tags.
3. A method for detecting a webshell file according to claim 1, characterized in that: Before converting the code in the script file to be detected into the first abstract syntax tree, the method further includes: Non-script code characters in the script file to be detected are cleaned, wherein the non-script code characters include comments and blank lines.
4. A method for detecting a webshell file according to claim 1, characterized in that: The file features include risk function, file content information entropy, file compression ratio, file content coincidence index, risk function, file longest word, file longest word information entropy, function nesting depth, array nesting depth, loop nesting depth and judgment nesting depth; The metadata characteristics include file size, modification date, creation date, and last access date.
5. A method for detecting a webshell file according to claim 4, characterized in that: The function nesting depth, array nesting depth, loop nesting depth and a method for determining the nesting depth include: Recursively traverse the first abstract syntax tree after replacement, and when entering a new structure block, modify the corresponding nesting depth according to the type of the new structure block until the traversal is completed, and use each maximum depth as the final value of the corresponding nesting depth.
6. A method for detecting a webshell file according to claim 1, characterized in that: The training process of the classification model includes: Acquire an original sample file, and convert the code in the original sample file into a second abstract syntax tree; Replace the special identifier in the second abstract syntax tree with a common tag, and restore the replaced second abstract syntax tree to the first sample file; Acquire the file features of the first sample file and the metadata features of the original sample file, and optimize the file features of the first sample file and the metadata features of the original sample file by using a recursive feature elimination method; The optimized feature data is input into the xgboost model for training to obtain a classification model for webshell files.
7. A method for detecting a webshell file according to claim 1, characterized in that: The method further comprises: Whether the detection result is correct is judged manually. If the result is a false positive, the type of the script file to be detected is re-marked, and the classification model is incrementally trained based on the script file to be detected.
8. A webshell file detection system, characterized in that: include: A conversion module, configured to convert the code in the script file to be detected into a first abstract syntax tree, and replace special identifiers in the first abstract syntax tree with general tags; An extraction module, used to restore the replaced first abstract syntax tree to a first script file, and obtain file features of the first script file and metadata features of the script file to be detected; The classification module is used to input the file features and metadata features into a trained classification model, and determine whether the script file to be detected is a webshell file according to the detection result of the classification model.
9. An electronic device, characterized in that: It comprises a memory and a processor, wherein the memory is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor to implement a method for detecting a webshell file as described in any one of claims 1 to 7.
10. A computer-readable storage medium storing a computer program, characterized in that: The computer program implements a method for detecting a webshell file according to any one of claims 1 to 7 when executed by a computer.
Citation Information
Patent Citations
Method and apparatus for detecting WebShell file
CN106572117A
Webshell detection method and device
CN109462575A
Webshell detection method and webshell detection device
CN112052451A
Multi-engine WebShell script file detection method and system
CN113111346A
Method and device for generating malicious file detection rule and method and device for detecting malicious file
CN115168851A
