hacktricks/windows-hardening/ntlm/wmicexec.md

157 lines
6.6 KiB
Markdown
Raw Normal View History

2022-08-13 23:06:40 +00:00
# WmicExec
2022-04-28 16:01:33 +00:00
<details>
2024-01-02 18:28:04 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-02 18:28:04 +00:00
Other ways to support HackTricks:
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
2024-01-02 18:28:04 +00:00
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 00:36:13 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2024-01-02 18:28:04 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2024-02-04 16:10:29 +00:00
## How It Works Explained
2022-04-28 16:01:33 +00:00
2024-02-04 16:10:29 +00:00
Processes can be opened on hosts where the username and either password or hash are known through the use of WMI. Commands are executed using WMI by Wmiexec, providing a semi-interactive shell experience.
2024-02-04 16:10:29 +00:00
**dcomexec.py:** Utilizing different DCOM endpoints, this script offers a semi-interactive shell akin to wmiexec.py, specifically leveraging the ShellBrowserWindow DCOM object. It currently supports MMC20. Application, Shell Windows, and Shell Browser Window objects. (source: [Hacking Articles](https://www.hackingarticles.in/beginners-guide-to-impacket-tool-kit-part-1/))
2024-02-04 16:10:29 +00:00
## WMI Fundamentals
2021-01-20 17:41:14 +00:00
2022-08-13 23:06:40 +00:00
### Namespace
2021-01-20 23:53:30 +00:00
2024-02-04 16:10:29 +00:00
Structured in a directory-style hierarchy, WMI's top-level container is \root, under which additional directories, referred to as namespaces, are organized.
Commands to list namespaces:
2021-01-20 17:41:14 +00:00
```bash
2024-02-04 16:10:29 +00:00
# Retrieval of Root namespaces
2021-01-20 17:41:14 +00:00
gwmi -namespace "root" -Class "__Namespace" | Select Name
2024-02-04 16:10:29 +00:00
# Enumeration of all namespaces (administrator privileges may be required)
2021-01-20 17:41:14 +00:00
Get-WmiObject -Class "__Namespace" -Namespace "Root" -List -Recurse 2> $null | select __Namespace | sort __Namespace
2024-02-04 16:10:29 +00:00
# Listing of namespaces within "root\cimv2"
2021-01-20 17:41:14 +00:00
Get-WmiObject -Class "__Namespace" -Namespace "root\cimv2" -List -Recurse 2> $null | select __Namespace | sort __Namespace
```
2024-02-04 16:10:29 +00:00
Classes within a namespace can be listed using:
2021-01-20 17:41:14 +00:00
```bash
2024-02-04 16:10:29 +00:00
gwmwi -List -Recurse # Defaults to "root\cimv2" if no namespace specified
2021-01-20 17:41:14 +00:00
gwmi -Namespace "root/microsoft" -List -Recurse
```
2022-08-13 23:06:40 +00:00
### **Classes**
2021-01-20 23:53:30 +00:00
2024-02-04 16:10:29 +00:00
Knowing a WMI class name, such as win32\_process, and the namespace it resides in is crucial for any WMI operation.
Commands to list classes beginning with `win32`:
2021-01-20 17:41:14 +00:00
```bash
2024-02-04 16:10:29 +00:00
Get-WmiObject -Recurse -List -class win32* | more # Defaults to "root\cimv2"
2021-01-20 17:41:14 +00:00
gwmi -Namespace "root/microsoft" -List -Recurse -Class "MSFT_MpComput*"
```
2024-02-04 16:10:29 +00:00
Invocation of a class:
2021-01-20 17:41:14 +00:00
```bash
2024-02-04 16:10:29 +00:00
# Defaults to "root/cimv2" when namespace isn't specified
2021-01-20 17:41:14 +00:00
Get-WmiObject -Class win32_share
Get-WmiObject -Namespace "root/microsoft/windows/defender" -Class MSFT_MpComputerStatus
```
2022-08-13 23:06:40 +00:00
### Methods
2021-01-20 23:53:30 +00:00
2024-02-04 16:10:29 +00:00
Methods, which are one or more executable functions of WMI classes, can be executed.
2021-01-20 23:53:30 +00:00
```bash
2024-02-04 16:10:29 +00:00
# Class loading, method listing, and execution
2021-01-20 23:53:30 +00:00
$c = [wmiclass]"win32_share"
$c.methods
2024-02-04 16:10:29 +00:00
# To create a share: $c.Create("c:\share\path","name",0,$null,"My Description")
2021-01-20 23:53:30 +00:00
```
```bash
2024-02-04 16:10:29 +00:00
# Method listing and invocation
2021-01-20 23:53:30 +00:00
Invoke-WmiMethod -Class win32_share -Name Create -ArgumentList @($null, "Description", $null, "Name", $null, "c:\share\path",0)
```
2021-01-20 17:41:14 +00:00
2022-08-13 23:06:40 +00:00
## WMI Enumeration
2021-01-20 17:41:14 +00:00
2024-02-04 16:10:29 +00:00
### WMI Service Status
2021-01-20 17:41:14 +00:00
2024-02-04 16:10:29 +00:00
Commands to verify if the WMI service is operational:
2021-01-20 17:41:14 +00:00
```bash
2024-02-04 16:10:29 +00:00
# WMI service status check
2021-01-20 17:41:14 +00:00
Get-Service Winmgmt
2024-02-04 16:10:29 +00:00
# Via CMD
2021-01-20 17:41:14 +00:00
net start | findstr "Instrumentation"
```
2024-02-04 16:10:29 +00:00
### System and Process Information
2021-01-20 17:41:14 +00:00
2024-02-04 16:10:29 +00:00
Gathering system and process information through WMI:
2021-01-20 17:41:14 +00:00
```bash
2024-02-04 16:10:29 +00:00
Get-WmiObject -ClassName win32_operatingsystem | select * | more
2021-01-20 17:41:14 +00:00
Get-WmiObject win32_process | Select Name, Processid
```
2024-02-04 16:10:29 +00:00
For attackers, WMI is a potent tool for enumerating sensitive data about systems or domains.
2024-02-04 16:10:29 +00:00
```bash
wmic computerystem list full /format:list
wmic process list /format:list
wmic ntdomain list /format:list
wmic useraccount list /format:list
wmic group list /format:list
wmic sysaccount list /format:list
```
2024-02-04 16:10:29 +00:00
Remote querying of WMI for specific information, such as local admins or logged-on users, is feasible with careful command construction.
2021-01-09 15:04:24 +00:00
2024-02-04 16:10:29 +00:00
### **Manual Remote WMI Querying**
2024-02-04 16:10:29 +00:00
Stealthy identification of local admins on a remote machine and logged-on users can be achieved through specific WMI queries. `wmic` also supports reading from a text file to execute commands on multiple nodes simultaneously.
2024-02-04 16:10:29 +00:00
To remotely execute a process over WMI, such as deploying an Empire agent, the following command structure is employed, with successful execution indicated by a return value of "0":
```bash
2024-02-04 16:10:29 +00:00
wmic /node:hostname /user:user path win32_process call create "empire launcher string here"
```
2024-02-04 16:10:29 +00:00
This process illustrates WMI's capability for remote execution and system enumeration, highlighting its utility for both system administration and penetration testing.
2024-02-08 03:06:37 +00:00
## References
2024-02-04 16:10:29 +00:00
* [https://blog.ropnop.com/using-credentials-to-own-windows-boxes-part-3-wmi-and-winrm/](https://blog.ropnop.com/using-credentials-to-own-windows-boxes-part-2-psexec-and-services/)
## Automatic Tools
2022-04-28 16:01:33 +00:00
* [**SharpLateral**](https://github.com/mertdas/SharpLateral):
2022-04-28 16:01:33 +00:00
{% code overflow="wrap" %}
```bash
SharpLateral redwmi HOSTNAME C:\\Users\\Administrator\\Desktop\\malware.exe
```
{% endcode %}
2022-04-28 16:01:33 +00:00
<details>
2022-04-28 16:01:33 +00:00
2024-01-02 18:28:04 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-02 18:28:04 +00:00
Other ways to support HackTricks:
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
2024-01-02 18:28:04 +00:00
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-09 00:36:13 +00:00
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
2024-01-02 18:28:04 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>