Skip to content

Impacket tstool.py

Back to the category index / Back to the command index

Uses Terminal Services RPC to list sessions and processes and to disconnect, log off, or terminate them.

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

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

Subcommands: qwinsta, tasklist, taskkill, tscon, tsdiscon, logoff, shutdown, msg, shadow

ArgumentDescription
target[[domain/]username[:password]@]
-debugEnable debug output
-tsAdd a timestamp to each log message
-vEnable verbose output
-pidProcess ID (PID)
-nameProcess image name; internally runs tasklist to obtain its PID
-sourceSource session ID; required
-destDestination session ID; required
-passwordPassword for the destination session
-sessionSession ID to disconnect; required
-logoffForce sessions to log off
-shutdownShut down the system
-rebootReboot after shutdown
-poweroffPower off after shutdown
-titleOptional MessageBox title
-messageMessageBox contents; required
-controlRequest control of the session (view-only by default)
-promptRequest user permission (silent by default)
-fileSave invitation to this file (default: invite.msrcIncident)
-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
-portDestination SMB server port (choices: 139, 445; default: 445)

The tool connects to an SMB session and the Terminal Services endpoint. TSHandler.run() dispatches qwinsta, tasklist, taskkill, tscon, tsdiscon, logoff, shutdown, msg, and shadow. When needed, LSA resolves SIDs to usernames.

Terminal Services exposes sessions and processes through its own RPC interface. This command establishes an SMB/RPC transport and dispatches session enumeration, process enumeration, session control, and system actions to different TSTS requests according to the subcommand. TSHandler.run() maps CLI actions to protocol helpers.

qwinsta collects session IDs, states, and station or user information. tasklist uses LSA lookups to add names to SIDs in process records. taskkill, tscon, tsdiscon, and logoff resolve existing IDs or names before changing state. msg and shadow also handle variable-length text, permission or control flags, and response codes.

Sessions can change during enumeration, so an ID may no longer exist when used for an action. Distinguish RPC transport errors, missing target sessions, access denials, and user rejections. Shutdown and power-off affect every session, so validate arguments and prepare auditable logs before making protocol calls.

SymbolLocationWhere to trace its responsibility
TSHandler59__init__(), connect(), run(), get_session_list(), enumerate_sessions_config(), enumerate_sessions_info(), do_qwinsta(), lookupSids(), sidToUser(), do_tasklist(), do_taskkill(), do_tscon(), and others
ModuleMain names used
impacketLOG, version
impacket.exampleslogger
impacket.examples.utilsparse_target
impacket.smbconnectionSMBConnection
impacket.dcerpc.v5lsad, lsat, transport, tsts
impacket.dcerpc.v5.rpcrtDCERPCException, RPC_C_AUTHN_GSS_NEGOTIATE, RPC_C_AUTHN_LEVEL_PKT_PRIVACY
impacket.dcerpc.v5.dtypesMAXIMUM_ALLOWED
impacket.dcerpc.v5.tstsSHADOW_CONTROL_REQUEST, SHADOW_PERMISSION_REQUEST, SHADOW_REQUEST_RESPONSE

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.
  • On failure, use protocol status codes to determine whether name resolution, connection, authentication, service state, or permissions caused the stop.

The code depends on dcerpc.v5.tsts, transport, lsad, lsat, and SMBConnection. Validate arguments specific to each subcommand before the RPC call, and close session handles after each action.