Skip to content

Impacket reg.py

Back to the category index / Back to the command index

Uses Remote Registry RPC to query, modify, and save registry data and to back up the SAM, SYSTEM, and SECURITY hives.

ItemDetails
Implementationexamples/reg.py
CategorySecrets / DPAPI / registry / file analysis
Size703 lines
DistributionInstalled as a script through scripts=examples/*.py in setup.py

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

Subcommands: query, add, delete, save, backup

ArgumentDescription
target[[domain/]username[:password]@]
-debugEnable debug output
-tsAdd a timestamp to each log message
-keyNameFull subkey path, including a valid root key; local root keys: HKLM, HKU, HKCU, HKCR; required
-vValue name to query; if omitted, return all value names under -keyName
-veQuery the default value or an empty value name
-sQuery all subkeys and value names recursively
-vtValue type to set (default: REG_SZ; types: REG_NONE, REG_SZ, REG_EXPAND_SZ, REG_BINARY, REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_LINK, REG_MULTI_SZ, REG_QWORD)
-vdRegistry value data to set; for REG_MULTI_SZ, specify this option once per line (may be repeated)
--persistentKeep the created key through reboot; by default, create a volatile key
-vaDelete all values under the key
-oOutput UNC path where the target must save registry exports; required
-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 the winreg endpoint over SMB/RPC. If RemoteRegistry is stopped, it temporarily changes its startup state through SCMR. The subcommand opens hive or key handles, converts value types, enumerates recursively, or saves data. On exit, it closes handles and restores the service state if needed.

Remote Registry operations use the winreg RPC interface, but the target service may be stopped. RemoteOperations reads the current RemoteRegistry startup mode and state through SCMR, then enables and starts it temporarily only if needed. It separates service preparation from registry actions so it can restore the original state on exit.

RegHandler resolves the root hive and key path. query enumerates subkeys and values and decodes them by type; add and delete create or remove keys and values. Conversion from CLI text to wire values differs for REG_SZ, DWORD, binary, and multi-string data. save calls the server-side hive save API, so target paths and permissions matter, unlike local file output.

backup takes a consistent set of SAM, SYSTEM, and SECURITY hives through a special path. Close key and service handles regardless of whether an action succeeds, and restore RemoteRegistry if it was started, unless --persistent is specified. Even if an access denial interrupts a recursive query, distinguish retrieved branches from the failed key.

SymbolLocationWhere to trace its responsibility
RemoteOperations50__init__(), getRRP(), connectWinReg(), enableRegistry(), finish()
RegHandler139__init__(), connect(), run(), triggerWinReg(), save(), query(), add(), delete()
ModuleMain names used
impacketversion
impacket.dcerpc.v5rpcrt, rrp, scmr, transport
impacket.exampleslogger
impacket.examples.utilsparse_target
impacket.system_errorsERROR_NO_MORE_ITEMS
impacket.structurehexdump
impacket.winregistryformat_multi_sz
impacket.smbconnectionSMBConnection, SessionError
impacket.dcerpc.v5.dtypesREAD_CONTROL

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.
  • Options may create or update local files. Check how existing files are handled and the permissions on output containing secrets.
  • 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.
  • Cleanup-related methods include finish(). Check that new early returns and exception paths reach it too.

The code depends on dcerpc.v5.rrp, scmr, transport, SMBConnection, and the registry value formatter. RemoteOperations handles the service lifecycle, while RegHandler handles actions.