Skip to content

Back to the category index / Back to the command index

Runs commands or a semi-interactive shell through DCOM/WMI Win32_Process.Create.

ItemDetails
Implementationexamples/wmiexec.py
CategoryRemote execution / WMI / services
Size469 lines
DistributionInstalled as a script through scripts=examples/*.py in setup.py

Run python examples/wmiexec.py -h for the current option definitions.

ArgumentDescription
target[[domain/]username[:password]@]
-shareShare from which to retrieve output (default: ADMIN$)
-nooutputDo not print output (no SMB connection is created)
-tsAdd a timestamp to each log message
-silentcommandRun the given command without cmd.exe (no output)
-debugEnable debug output
-codecHelp text generated dynamically from the runtime value
-shell-typeCommand processor for the semi-interactive shell (choices: cmd, powershell; default: cmd)
-com-versionDCOM version in MAJOR_VERSION:MINOR_VERSION format, for example 5.7
commandCommand to run on the target; if empty, launch a semi-interactive shell
-hashesNTLM hashes in LMHASH:NTHASH format
-no-passDo not prompt for a password (useful with -k)
-kUse Kerberos authentication. Look for matching credentials in the ccache file named by KRB5CCNAME; if none are valid, use those specified on the command line
-aesKeyAES key for Kerberos authentication (128 or 256 bits)
-dc-ipDomain controller IP address; if omitted, use the domain name (FQDN) from the target parameter
-target-ipTarget machine IP address; if omitted, use the target value. Useful when a NetBIOS target name cannot be resolved
-Asmbclient/mount.cifs-style authentication file; see smbclient manual page option -A
-keytabRead SPN keys from a keytab file

Related technology: DCOM/WMI in DCE/RPC and output retrieval over SMB.

The tool connects over DCOM, logs into the WMI root\cimv2 namespace, obtains Win32_Process, and creates a process. With output enabled, it redirects the command to a temporary file on a share and reads it back over SMB. A path equivalent to -nooutput runs without an SMB connection in a fire-and-forget mode.

WMIEXEC connects to the WMI service over DCOM and calls the Create method of the Win32_Process class in the root\cimv2 namespace. Unlike approaches that create an SCMR service, Windows Management Instrumentation provides the control plane for process creation. DCOM authentication and WMI namespace login are separate stages.

A WMI method returns process creation status and a PID, but not stdout. To capture output, a shell command redirects it to a temporary file on an SMB share. RemoteShell.get_output() waits for the file, then reads it. The -silentcommand and no-output paths avoid this SMB dependency, but also reduce the ability to observe completion and the exit code.

For the interactive shell, cd keeps the remote current directory in local state and prefixes it to the next command. The WMI process itself has no persistent current directory. Manage the lifecycles of DCOM interface releases, the WMI login object, the SMB session, and temporary files separately.

SymbolLocationWhere to trace its responsibility
WMIEXEC49__init__(), run()
RemoteShell121__init__(), do_shell(), do_help(), do_lcd(), do_lget(), do_lput(), do_exit(), do_EOF(), emptyline(), do_cd(), default(), get_output(), and others
AuthFileSyntaxError305__init__()
load_smbclient_auth_file()319Module function
ModuleMain names used
impacket.exampleslogger
impacket.examples.utilsparse_target
impacketversion
impacket.smbconnectionSMB2_DIALECT_002, SMB2_DIALECT_21, SMBConnection, SMB_DIALECT
impacket.dcerpc.v5.dcomrtCOMVERSION, DCOMConnection
impacket.dcerpc.v5.dcomwmi
impacket.dcerpc.v5.dtypesNULL
impacket.krb5.keytabKeytab

Output, side effects, and required privileges

Section titled “Output, side effects, and required privileges”
  • Some operations change the target’s state. Check the target name, SID, and path before making changes.
  • Some paths use temporary resources such as remote services, tasks, files, shares, and WMI/RPC resources. Cleaning them up after an abnormal exit is part of the behavior contract.
  • On failure, use protocol status codes to determine whether name resolution, connection, authentication, service state, or permissions caused the stop.
  • Even without an explicit cleanup function, check for finally-equivalent paths and closure or deletion of protocol handles.

The code depends on dcerpc.v5.dcomrt, dcom.wmi, SMBConnection, and keytabs. WMIEXEC owns the WMI session, while RemoteShell owns commands and file transfers. Preserve COM releases and temporary-file removal.