Skip to content

Impacket addcomputer.py

Back to the Category Index / Back to the Command Index

Creates, deletes, and checks domain computer accounts. In addition to SAMR and LDAPS, it can set LDAP attributes on accounts created through SAMR or on existing accounts.

ItemDetails
Implementationexamples/addcomputer.py
CategoryActive Directory / LDAP / Account Management
Size498 lines
DistributionInstalled as a script through scripts=examples/*.py in setup.py

Check python examples/addcomputer.py -h for the current option definitions.

ArgumentDescription
-domain-netbiosThe domain’s NetBIOS name. Specify it if the DC handles multiple domains.
-computer-nameName of the computer account to add. Generated randomly if omitted.
-computer-passPassword to set. Generated randomly if omitted.
-no-addSet only the password of an existing computer account, without creating a new one.
-deleteDelete an existing computer account.
-tsAdd timestamps to logs.
-debugEnable debug logging.
-methodOperation method. SAMR_LDAP sets LDAP attributes after SAMR creation; LDAP_FINALIZE sets attributes on an existing account. Choices: SAMR, SAMR_LDAP, LDAPS, LDAP_FINALIZE. Default: SAMR.
-portConnection port: 139, 445, or 636. Defaults are 445 for SAMR and 636 for LDAPS.
-baseDNLDAP base DN. Derived from the specified account’s domain if omitted.
-computer-groupDestination for the computer account. Defaults to CN=Computers.
-hashesNTLM hashes in LMHASH:NTHASH format.
-no-passSkip the interactive password prompt.
-kUse Kerberos authentication. If the ccache has no credentials, use the command-line values.
-aesKeyA 128- or 256-bit AES key for Kerberos authentication.
-dc-hostHostname of the domain controller to connect to. Derived from the account’s domain if omitted.
-dc-ipIP address of the domain controller to connect to.
accountAccount used for authentication, in [domain/]username[:password] format.

The arguments determine the account, DC, computer name, and password. -method then selects run_samr(), run_ldaps(), or LDAPFinalizeComputerAccount(). SAMR_LDAP sets LDAP attributes after run_samr() creates the account. If no name is given, it generates a random one.

Computer account creation has two paths: SAMR and LDAPS. The SAMR path opens a domain handle over RPC and sets the account type and initial password for a machine account. The LDAPS path adds a directory object and sets attributes such as sAMAccountName, userAccountControl, and DNS hostname. Although both paths add an account, they encounter different server-side authorization checks and require different ports. After creation, SAMR_LDAP calls LDAPFinalizeComputerAccount() to set the DNS hostname, SPN, and other attributes. LDAP_FINALIZE applies only those settings to an existing computer account.

run() first chooses the action (add, delete, or existence check) and -method, then passes them to the selected backend. If no computer name is given, it generates a random name ending in $, and it also generates a password if none is supplied. Because later steps need these generated values, avoid exposing them in logs. Return only the values associated with the successfully created object to the user.

The target DN and -computer-group matter for LDAP; the domain NetBIOS name and RID/domain handle matter for SAMR. The methods can set different attributes, so check the attributes after creation, behavior for an existing name, and any object left behind if LDAP setup fails after SAMR creation.

SymbolLocationEntry Point for Tracing Behavior
ADDCOMPUTER46__init__(), run_samr(), run_ldaps(), LDAPFinalizeComputerAccount(), LDAPComputerExists(), LDAPGetComputer(), generateComputerName(), doSAMRAdd(), run()
ModuleMain Names Used
impacketversion
impacket.exampleslogger
impacket.examples.utilsinit_ldap_session, ldap3_kerberos_login, parse_identity
impacket.dcerpc.v5epm, samr, transport
impacket.spnegoSPNEGO_NegTokenInit, TypesMech
Import NameDistribution PackageUsage / Declaration Status
ldap3ldap3Imported directly; declared dependency

Output, Side Effects, and Required Privileges

Section titled “Output, Side Effects, and Required Privileges”
  • Some operations change the target’s state. Confirm the target name, SID, and path before making changes.
  • On failure, use protocol status to determine whether name resolution, connection, authentication, service state, or permissions stopped the operation.

This command depends on dcerpc.v5.samr / transport / epm, ldap3, and examples.utils.init_ldap_session. ADDCOMPUTER owns the operation branches. Keep the default container, NetBIOS domain, and LDAP base DN semantics consistent between the two paths.