Skip to content

Back to the category index / Back to the command index

Uploads and starts the RemCom service binary on the target, then runs commands or an interactive shell through named pipes.

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

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

ArgumentDescription
target[[domain/]username[:password]@]
commandCommand to run on the target (without a path), or arguments when using -c (default: cmd.exe)
-cCopy a file for later execution; pass its arguments in the command option
-pathPath of the command to run
-fileAlternative RemCom binary (it must not require the CRT)
-tsAdd a timestamp to each log message
-debugEnable debug output
-codecHelp text generated dynamically from the runtime value
-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)
-keytabRead SPN keys from a keytab file
-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
-portDestination SMB server port (choices: 139, 445; default: 445)
-service-nameName of the service used to trigger the payload
-remote-binary-nameName of the executable uploaded to the target

Related technology: named pipes in SMB and SCMR in DCE/RPC.

After establishing an SMB session, ServiceInstall places the service binary on a share and creates and starts an SCMR service. The main pipe carries execution requests, while three pipe threads relay stdin, stdout, and stderr. On exit, the tool removes the service and uploaded binary. -c provides a path for transferring an additional executable.

The PSEXEC path has two stages. First, it uses a normal SMB session to place the RemCom service binary on a writable share and creates and starts a temporary service through SCMR. It then connects to the named pipes exposed by that service to exchange execution requests and standard input and output, combining RPC service control with SMB pipe transport.

The main communication pipe carries a RemComMessage with the machine name, process ID, command, working directory, and other fields. Stdin, stdout, and stderr use separate pipes. RemoteStdInPipe and output threads handle them concurrently. File uploads and downloads in the interactive shell use another SMB connection, keeping large file data separate from the command stream.

This implementation has many partial states: a pipe connection may fail after the service starts, an output thread may disconnect alone, or a command may fail after an additional executable is transferred. Stop and delete the service, remove the binary, close pipes and threads independently, and handle collisions when reusing a remote binary or service name. The exit code comes from the RemCom response.

SymbolLocationWhere to trace its responsibility
RemComMessage45Structure or constant definition
RemComResponse55Structure or constant definition
PSEXEC67__init__(), run(), openPipe(), doStuff()
Pipes226__init__(), connectPipe()
RemoteStdOutPipe265__init__(), run()
RemoteStdErrPipe383__init__(), run()
RemoteShell471__init__(), connect_transferClient(), do_help(), do_shell(), do_lget(), do_lput(), do_lcd(), emptyline(), default(), send_data()
RemoteStdInPipe580__init__(), run()
ModuleMain names used
impacket.exampleslogger, remcomsvc, serviceinstall
impacketsmb, version
impacket.smbconnectionSMBConnection
impacket.dcerpc.v5transport
impacket.structureStructure
impacket.examples.utilsparse_target
impacket.krb5.keytabKeytab
Import nameDistribution packageImport path and declaration status
sixsixImported directly; declared

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 SMBConnection, dcerpc.v5.transport, examples.remcomsvc, serviceinstall, and threading. The connection order and cleanup of PSEXEC, Pipes, and each stream class matter.