COM Classes: Registry & Security
COM Classes: Registry & Security
explain this : COM classes are registered with the Operating System and
mapped to a GUID, which represents a Class Identifier
(CLSID).
CLSIDs are located in the registry, under the following
locations:
• HKLM\Software\Classes\CLSID
• HKCU\Software\Classes\CLSID
In summary, when a COM class is registered, its CLSID is placed in one of these registry locations. This
allows the operating system to find and instantiate the correct COM class when an application
requests it by its CLSID.
In the previously mentioned registry locations, the keys that we are interested in within each COM
object, are:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/23
• LocalServer32 – defines path to executable on disk
• InprocServer32 – defines path to DLL on disk
In the Windows Registry, the keys `LocalServer32` and `InprocServer32` within each COM object's
CLSID entry are crucial for defining how and where the COM object is implemented. Here’s a detailed
explanation:
1. LocalServer32:
The `LocalServer32` key specifies the path to the executable file (.exe) that implements the
COM object.
This is used for out-of-process COM servers, where the COM object runs in its own process
space, separate from the client application.
When an application requests an instance of this COM object, the operating system will
launch the executable specified in `LocalServer32`.
Example Path:
objectivec
HKLM\Software\Classes\CLSID\{Your-CLSID}\LocalServer32
2. InprocServer32:
The `InprocServer32` key specifies the path to the dynamic link library (.dll) that implements
the COM object.
This is used for in-process COM servers, where the COM object runs within the same
process space as the client application, offering potentially better performance because of
the reduced overhead of cross-process communication.
When an application requests an instance of this COM object, the operating system will
load the DLL specified in `InprocServer32` into the application's process space.
Example Path:
objectivec
HKLM\Software\Classes\CLSID\{Your-CLSID}\InprocServer32
swift
[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{12345678-1234-1234-1234-
1234567890AB}\LocalServer32]
@="C:\\Path\\To\\YourExecutable.exe"
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/23
swift
[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{12345678-1234-1234-1234-
1234567890AB}\InprocServer32]
@="C:\\Path\\To\\YourDLL.dll"
In summary, the `LocalServer32` and `InprocServer32` registry keys are used by the operating system
to locate and load the COM object’s implementation, whether it's an executable for out-of-process
servers or a DLL for in-process servers.
Example Scenario:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/23
A non-privileged user might add an entry in `HKCU\Software\Classes\CLSID` to register their own COM
object, perhaps to replace a legitimate COM object for certain operations within their user session.
Example Entry in HKCU:
swift
[HKEY_CURRENT_USER\Software\Classes\CLSID\{12345678-1234-1234-1234-
1234567890AB}\InprocServer32]
@="C:\\Path\\To\\UserSpecificDLL.dll"
In this example, if an application running under this user's context tries to instantiate the COM object
with CLSID `{12345678-1234-1234-1234-1234567890AB}`, it will load `UserSpecificDLL.dll` instead of the
system-wide COM object registered in `HKLM`.
Mitigations:
Organizations should enforce strict ACLs (Access Control Lists) and use security policies to limit
the potential for misuse.
Monitoring and auditing registry changes in `HKCU\Software\Classes\CLSID` can help detect and
prevent unauthorized COM object registrations.
explain this : The problem: CLSID values in HKCU take precedence over
CLSID values in HKLM (Hijacking!).
In other words, imagine there is a scheduled task which
executes a COM object with CLSID 1 in HKLM. If a user
registers a CLSID also with value 1 in HKCU, which
executes a malicious binary, the scheduled task will run the
one in HKCU.
The issue described involves a potential security vulnerability where CLSID values in the `HKCU`
(HKEY_CURRENT_USER) registry hive can take precedence over those in the `HKLM`
(HKEY_LOCAL_MACHINE) registry hive. This situation can be exploited for hijacking by non-privileged
users. Here’s a detailed explanation:
1. CLSID Registration:
CLSIDs (Class Identifiers) are registered in the Windows Registry under
`HKLM\Software\Classes\CLSID` for system-wide availability and under
`HKCU\Software\Classes\CLSID` for user-specific availability.
When a COM object is requested, the system first looks for its CLSID in `HKCU`. If it doesn’t
find it there, it then checks `HKLM`.
2. Precedence:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/23
CLSID values in `HKCU` take precedence over those in `HKLM`. This means that if the same
CLSID exists in both hives, the system will use the one from `HKCU`.
3. Hijacking Scenario:
Imagine there is a scheduled task configured to execute a COM object identified by CLSID
`1` that is registered in `HKLM`. This task expects to run a legitimate system-wide COM
object.
If a non-privileged user registers the same CLSID (`1`) in their `HKCU` hive, pointing to a
malicious binary, the system will execute the malicious binary instead of the legitimate one
when the scheduled task runs.
Example Steps of Exploitation:
Legitimate Registration:
swift
[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{1}\LocalServer32]
@="C:\\Path\\To\\LegitimateExecutable.exe"
swift
[HKEY_CURRENT_USER\Software\Classes\CLSID\{1}\LocalServer32]
@="C:\\Path\\To\\MaliciousExecutable.exe"
When the scheduled task runs and requests the COM object with CLSID `1`, the system
checks `HKCU` first, finds the malicious entry, and executes `MaliciousExecutable.exe`.
Implications
Security Risk: This behavior can be exploited by non-privileged users to escalate privileges,
execute arbitrary code, or disrupt system operations by hijacking legitimate COM object
requests.
Persistence: Malware can use this technique to achieve persistence, ensuring that even if the
system-wide (HKLM) COM object is intended for a legitimate purpose, the malicious version will
run for the specific user.
Mitigation Strategies
Conclusion
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/23
The precedence of `HKCU` over `HKLM` for CLSID values can be exploited by non-privileged users to
hijack the execution of scheduled tasks or other system operations that rely on COM objects. Proper
security measures and monitoring are necessary to mitigate this risk.
From the perspective of a privileged user (one with administrative rights), there are several potential
abuse scenarios involving the modification of COM object registrations in the registry. These scenarios
include altering the paths defined in `LocalServer32` or `InprocServer32` to execute malicious files, or
replacing the legitimate files at their original locations on disk. Here’s a detailed explanation of these
scenarios:
swift
[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{12345678-1234-1234-1234-
1234567890AB}\LocalServer32]
@="C:\\Path\\To\\LegitimateExecutable.exe"
swift
[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{12345678-1234-1234-1234-
1234567890AB}\LocalServer32]
@="C:\\Path\\To\\MaliciousExecutable.exe"
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/23
Instead of or in addition to modifying registry entries, a privileged user can replace the
legitimate executable or DLL file at its original location on disk with a malicious one. This
approach can be particularly stealthy because the registry entries remain unchanged.
Example:
Original File Path:
mathematica
C:\Path\To\LegitimateExecutable.exe
Implications
1. Elevated Privileges: By modifying the registry or replacing files, a privileged user can execute
code with elevated privileges, potentially gaining full control over the system.
2. Persistence: These changes can ensure that malicious code is executed persistently, even after
reboots or user logins.
3. Stealth: Replacing files on disk or making subtle registry modifications can be less noticeable
compared to other methods of executing malicious code.
Mitigation Strategies
1. Access Control: Implement strict access controls to limit who can modify critical registry keys
and executable files.
2. Monitoring and Auditing: Regularly monitor and audit changes to `LocalServer32`,
`InprocServer32`, and critical system files to detect unauthorized modifications.
3. Integrity Checks: Use file integrity monitoring (FIM) tools to ensure that important executables
and DLLs have not been tampered with.
4. Security Policies: Enforce security policies that restrict the ability to modify COM registrations
and critical system files, even for privileged users.
Conclusion
Privileged users have the potential to exploit COM object registrations by modifying `LocalServer32`
or `InprocServer32` registry keys to point to malicious files or by replacing legitimate files at their
original disk locations. These actions can lead to the execution of malicious code with elevated
privileges, persistence, and stealth, posing significant security risks. Implementing robust access
controls, monitoring, and security policies are essential to mitigate these risks.
explain this in deep: As hunters, our focus is therefore any registry additions or
modifications of CLSIDs on the keys LocalServer32 or
InprocServer32.
If baselines are not available to compare with, we could
also hunt by looking for presence of objects within
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/23
HKEY_CURRENT_USER\Software\Classes\CLSID\ as their
presence alone is anomalous behavior.
In the context of cybersecurity, "hunters" refers to security analysts or threat hunters who actively
search for indicators of compromise (IoCs) or malicious activity within an organization's systems and
networks. Their goal is to detect and mitigate threats before they can cause significant harm.
Attack Vector: Malicious actors may modify these keys to hijack legitimate processes and
execute malicious code.
Persistence: Changes to these keys can ensure that malicious code runs persistently, especially
during system startup or when specific COM objects are invoked.
Hunting Strategy
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/23
`HKLM\Software\Classes\CLSID\{CLSID}\InprocServer32`
`HKCU\Software\Classes\CLSID\{CLSID}\LocalServer32`
`HKCU\Software\Classes\CLSID\{CLSID}\InprocServer32`
2. Set Up Alerts for Modifications:
Use tools and scripts to set up real-time alerts for any additions or modifications to these
registry keys.
Windows Event Logs and Sysmon can be configured to log registry changes, which can then
be monitored by a Security Information and Event Management (SIEM) system.
3. Analyze Suspicious Entries:
For each detected modification or addition, analyze the following:
Timestamp: When was the entry created or modified?
User: Which user account made the change?
File Path: Does the file path point to a legitimate executable/DLL or a suspicious one?
4. Investigate Anomalies in HKCU:
Since `HKCU\Software\Classes\CLSID` should have few entries, any new or unexpected
CLSIDs should be scrutinized.
Check if the executables or DLLs pointed to by `LocalServer32` or `InprocServer32` are
legitimate and expected within the user’s context.
5. Correlate Findings:
Correlate registry changes with other logs and alerts (e.g., file integrity monitoring, process
execution logs) to build a comprehensive picture of potential malicious activity.
Look for patterns such as repeated modifications by the same user or targeting the same
COM objects.
Conclusion
To set up alerts for modifications to the `LocalServer32` and `InprocServer32` registry keys, you can
use various tools and techniques. Below are some examples using different platforms:
Sysmon (System Monitor) is a tool from Microsoft that logs system activity to the Windows Event
Log. You can configure Sysmon to log registry events, including changes to specific keys.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/23
2. Create a Sysmon configuration file to log registry events. Below is an example configuration
file that monitors modifications to `LocalServer32` and `InprocServer32`.
xml
<Sysmon schemaversion="4.12">
<!-- Capture registry key and value changes -->
<EventFiltering>
<RegistryEvent onmatch="include">
<TargetObject condition="contains">LocalServer32</TargetObject>
<TargetObject condition="contains">InprocServer32</TargetObject>
</RegistryEvent>
</EventFiltering>
</Sysmon>
sh
Sysmon logs can be viewed in the Windows Event Viewer under Applications and Services Logs
> Microsoft > Windows > Sysmon.
Set up alerts using Windows Event Viewer by creating a custom view:
If you are using a Security Information and Event Management (SIEM) system like Splunk, Elastic Stack
(ELK), or Microsoft Sentinel, you can create queries and alerts for registry modifications.
Splunk Example
spl
kql
Using PowerShell
For environments without advanced tools, you can use a PowerShell script to monitor and alert on
registry changes.
powershell
$path = "HKLM:\Software\Classes\CLSID"
$filter = "LocalServer32","InprocServer32"
Conclusion
Setting up alerts for modifications to `LocalServer32` and `InprocServer32` registry keys can be
achieved using tools like Sysmon with Windows Event Viewer, SIEM solutions like Splunk or ELK, or
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/23
even custom PowerShell scripts. Each method involves monitoring for specific registry changes and
configuring alerts to notify you of potential malicious activity.
Detecting the hijacking of a COM object involves monitoring for suspicious changes to registry keys
and paths associated with COM object registrations. Here are examples using different tools and
methodologies to detect such hijacking attempts:
Sysmon can be configured to monitor for registry changes, particularly for keys related to COM
objects (`LocalServer32` and `InprocServer32`).
Sysmon Configuration
First, set up Sysmon with a configuration file that monitors registry events:
xml
<Sysmon schemaversion="4.12">
<EventFiltering>
<!-- Capture registry key and value changes -->
<RegistryEvent onmatch="include">
<TargetObject condition="contains">LocalServer32</TargetObject>
<TargetObject condition="contains">InprocServer32</TargetObject>
</RegistryEvent>
</EventFiltering>
</Sysmon>
sh
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/23
2. Using Splunk
Splunk can be used to search and create alerts for suspicious registry changes.
Search Query
spl
Elastic Stack can also be configured to detect and alert on registry changes.
Kibana Query
kql
4. Using PowerShell
A PowerShell script can monitor for registry changes and detect hijacking attempts.
powershell
$path = "HKLM:\Software\Classes\CLSID"
$filter = "LocalServer32","InprocServer32"
5. Manual Detection
Suppose you have the following legitimate registry entry for a COM object:
sh
[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{12345678-1234-1234-1234-
1234567890AB}\LocalServer32]
@="C:\\Path\\To\\LegitimateExecutable.exe"
sh
[HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{12345678-1234-1234-1234-
1234567890AB}\LocalServer32]
@="C:\\Path\\To\\MaliciousExecutable.exe"
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 14/23
By setting up these monitoring and alerting mechanisms, you can detect and respond to COM object
hijacking attempts effectively.
explain this in deep: Some interesting research discovered that rundll32.exe can
also be used to start COM objects.
Using `rundll32.exe` to start COM objects is a technique that has been explored in various research
studies and threat actor methodologies. `rundll32.exe` is a legitimate Windows utility that can
execute functions exported from DLLs. It can also be leveraged to invoke COM objects, which can be
an interesting avenue for both legitimate and malicious purposes. Here’s a deeper look into this
concept:
Rundll32.exe Overview
Purpose: `rundll32.exe` is designed to load and run 32-bit dynamic-link libraries (DLLs) and call
their exported functions. It is commonly used by various Windows components and software
applications.
Location: Typically located in `C:\Windows\System32`.
COM objects can be instantiated and their methods invoked via `rundll32.exe`. This is achieved by
using a specific function call pattern that `rundll32.exe` can interpret. Here’s how it works:
cpp
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 15/23
IUnknown *pUnknown = NULL;
HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid,
(void**)&pUnknown);
if (SUCCEEDED(hr)) {
// Do something with the COM object
// ...
2. Using Rundll32.exe to Load the DLL and Execute the Exported Function:
Once the DLL is prepared and placed in a known location, you can use `rundll32.exe` to
execute the exported function:
sh
rundll32.exe path\to\your.dll,RunCOMObject
Example Scenario
Consider the following steps to demonstrate how `rundll32.exe` can start a COM object:
sh
rundll32.exe C:\Path\To\YourDLL.dll,RunCOMObject
In this example, `RunCOMObject` is the function within the DLL that instantiates the COM object and
invokes its methods.
While `rundll32.exe` is a legitimate utility, it can be abused by attackers for several reasons:
1. Evasion:
Using `rundll32.exe` to execute malicious code can help evade detection because it is a
trusted Windows binary.
Attackers can use it to load malicious DLLs that instantiate harmful COM objects.
2. Persistence:
Attackers can register COM objects that get instantiated by `rundll32.exe` on system
startup or during specific system events.
3. Execution of Malicious Code:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 16/23
Malicious DLLs can be loaded to perform a wide range of harmful activities, such as data
exfiltration, system modification, or further payload execution.
Conclusion
The use of `rundll32.exe` to start COM objects is a technique that leverages legitimate system
functionality for potentially malicious purposes. Understanding this technique is crucial for both
defending against and detecting its misuse. By monitoring, analyzing, and implementing robust
security measures, it is possible to mitigate the risks associated with this method.
To detect and mitigate the misuse of `rundll32.exe` for executing COM objects or malicious DLLs,
security analysts can leverage a combination of event logging, SIEM solutions, and endpoint detection
and response (EDR) tools. Here are real examples and queries using popular tools:
1. Sysmon Configuration
Sysmon can be configured to log detailed events about process creations, including `rundll32.exe`
executions.
xml
<Sysmon schemaversion="4.12">
<EventFiltering>
<!-- Log process creation events -->
<ProcessCreate onmatch="include">
<Image condition="contains">rundll32.exe</Image>
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 17/23
</ProcessCreate>
sh
Splunk Query
In Splunk, you can create a search query to detect `rundll32.exe` executions with suspicious
command lines:
spl
Configure the alert to trigger when the number of matching events exceeds a threshold.
Set up email notifications or other alert actions.
kql
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 18/23
3. Detecting with Windows Event Viewer
powershell
5. Mitigation Strategies
1. Application Control:
Implement application control policies to restrict the execution of `rundll32.exe` to known,
trusted DLLs only.
2. Behavioral Analysis:
Use EDR tools that can analyze the behavior of `rundll32.exe` and detect anomalies.
Configure rules in EDR tools to flag or block `rundll32.exe` executions with suspicious
command lines.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 19/23
text
Software Restriction Policies > Additional Rules > New Path Rule
Path: *\rundll32.exe
Security Level: Disallowed
2. Group Policy:
Use Group Policy to restrict `rundll32.exe` usage to prevent unauthorized DLL loading.
Conclusion
By configuring Sysmon, leveraging SIEM solutions like Splunk and ELK, using PowerShell scripts, and
implementing endpoint protection policies, you can effectively detect and mitigate the misuse of
`rundll32.exe` for executing COM objects or malicious DLLs. Regular monitoring, alerting, and
enforcing strict application control policies are crucial steps in securing your environment against
such threats.
Create YARA rules to detect suspicious patterns related to the misuse of rundll32.exe
Creating YARA rules to detect suspicious patterns related to the misuse of `rundll32.exe` involves
looking for specific command-line arguments or behaviors that indicate malicious activity. Below are
some example YARA rules that can help identify potentially malicious use of `rundll32.exe`.
This YARA rule detects the use of `rundll32.exe` with suspicious command-line arguments such as
those often used to load a DLL or execute a COM object.
yara
rule SuspiciousRundll32CommandLine
{
meta:
description = "Detects suspicious rundll32.exe command-line arguments"
author = "Your Name"
date = "2024-07-23"
reference = "https://example.com"
strings:
$suspicious1 = /rundll32\.exe.*
(,RunDLL|,DllRegisterServer|,DllInstall|,DllUnregisterServer|,EntryPoint)/ nocase
$suspicious2 = /rundll32\.exe.*(javascript:|vbscript:|file:|data:)/ nocase
$suspicious3 = /rundll32\.exe.*(\.dll,|\.ocx,)/ nocase
condition:
any of ($suspicious1, $suspicious2, $suspicious3)
}
yara
rule UncommonRundll32DllLoading
{
meta:
description = "Detects rundll32.exe loading uncommon DLLs"
author = "Your Name"
date = "2024-07-23"
reference = "https://example.com"
strings:
$dll_path1 = /rundll32\.exe.*C:\\Users\\.*\\AppData\\Local\\Temp\\.*\.dll/ nocase
$dll_path2 = /rundll32\.exe.*C:\\Windows\\Temp\\.*\.dll/ nocase
$dll_path3 = /rundll32\.exe.*C:\\ProgramData\\.*\.dll/ nocase
condition:
any of ($dll_path1, $dll_path2, $dll_path3)
}
This rule targets the loading of specific COM objects known to be commonly abused by malware
through `rundll32.exe`.
yara
rule SuspiciousCOMObjectLoading
To detect `rundll32.exe` loading suspicious COM objects, we need to identify patterns in the
command-line arguments that are associated with malicious activity. Below is an example YARA rule
that focuses on detecting the loading of suspicious COM objects by `rundll32.exe`.
This rule looks for the presence of specific patterns in the command-line arguments of `rundll32.exe`
that are indicative of malicious use of COM objects.
yara
rule SuspiciousRundll32COMObjectLoading
{
meta:
description = "Detects rundll32.exe loading suspicious COM objects"
author = "Your Name"
date = "2024-07-23"
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 21/23
reference = "https://example.com"
strings:
// Detecting common patterns used in rundll32.exe to load COM objects
$com_load1 = /rundll32\.exe.*CoCreateInstance/i
$com_load2 = /rundll32\.exe.*DllGetClassObject/i
$com_load3 = /rundll32\.exe.*Ole32\.dll/i
$com_load4 = /rundll32\.exe.*ProgIDFromCLSID/i
$com_load5 = /rundll32\.exe.*CLSIDFromProgID/i
$com_load6 = /rundll32\.exe.*CLSIDFromString/i
condition:
any of ($com_load1, $com_load2, $com_load3, $com_load4, $com_load5, $com_load6) or
any of ($guid1, $guid2, $guid3, $guid4, $guid5)
}
Explanation
Meta Section: Contains metadata about the rule, such as description, author, date, and
reference.
Strings Section: Defines the strings and regular expressions to match suspicious command-line
patterns and GUIDs associated with malicious COM objects.
`$com_load1` to `$com_load6`: These are regular expressions to match common methods
and libraries used to load COM objects with `rundll32.exe`.
`$guid1` to `$guid5`: These are GUIDs associated with COM objects that are known to be
abused by malware.
Condition Section: Specifies the conditions under which the rule will trigger. The rule triggers if
any of the defined strings or GUIDs are found in the command-line arguments.
Usage
1. Integration with EDR: This YARA rule can be integrated with endpoint detection and response
(EDR) solutions to monitor process creation events and analyze command-line arguments.
2. Log Analysis: Apply this YARA rule to logs collected from Sysmon, Windows Event Viewer, or
other logging mechanisms to detect suspicious `rundll32.exe` executions.
To use this rule with a YARA scanner, save the rule to a file, e.g., `rundll32_com_object_loading.yara`,
and run the scanner against the target logs or process memory.
sh
By implementing such YARA rules, security analysts can enhance their ability to detect and respond to
the misuse of `rundll32.exe` for loading malicious COM objects, thereby strengthening their overall
security posture.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 22/23
يمكن أن تصدر عنChatGPT لذلك يجب التحقق من المعلومات المهمة.بعض األخطاء.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 23/23