o
    j                  
   @   sJ  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ G dd dZed	kr#eej ejd
ddZejdddd ejdddd ejdddd edZejddddd ejddddd ejdddgd dd!d"d# ed$Zejd%dd&d'd ejd(dd)d ejd*dd+d ejd,dd-d.d ee jd/kre  e d/ e Zeejej eej \Z!Z"Z#Z$e!du rd0Z!ej%du re$e_%ej&durd
e_'e#d0kre"d0krej(du rej)d1u rej&du rd d2l*m*Z* e*d3Z#ee"e#e!ej(ej&ej'ej+e,ej-Z.e./e$ej% dS dS )4    N)logger)parse_target)version)bin_to_string)	transportdsspc                   @   sv   e Zd Zejjdejjdejjdejjdejj	dejj
diZ		
dddZdd Zdd Zdd Zdd Zdd ZdS )MachineRolezStandalone WorkstationzDomain-joined WorkstationzStandalone ServerzDomain-joined ServerzBackup Domain ControllerzPrimary Domain Controller NF  c	           	      C   sX   || _ || _|| _d| _d| _|| _|| _|| _|| _|d ur*|	d\| _| _d S d S )Nr	   :)
_MachineRole__username_MachineRole__password_MachineRole__domain_MachineRole__lmhash_MachineRole__nthash_MachineRole__aesKey_MachineRole__doKerberos_MachineRole__kdcHost_MachineRole__portsplit)	selfusernamepassworddomainhashesaesKey
doKerberoskdcHostport r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/../../../bin/machine_role.py__init__3   s   zMachineRole.__init__c              
   C   s   z|  ||}W n ty! } z| t| W Y d }~nd }~ww z| |}W n tyB } z| t| W Y d }~nd }~ww | D ]\}}td||f  qG|  d S )Nz%s: %s)_MachineRole__authenticate	Exception_MachineRole__log_and_exitstr_MachineRole__fetchitemsprint
disconnect)r   
remoteName
remoteHostdceeoutputkeyvaluer   r   r    
print_infoB   s   zMachineRole.print_infoc                 C   s$   |  ||}|  |tj |S )N)_MachineRole__get_transportconnectbindr   MSRPC_UUID_DSSP)r   r*   r+   r,   r   r   r    __authenticateR   s   zMachineRole.__authenticatec                 C   sx   d| }t d|  t|}|| j || t|dr0|| j	| j
| j| j| j| j || j| j | S )Nzncacn_np:%s[\pipe\lsarpc]zStringBinding %sset_credentials)loggingdebugr   DCERPCTransportFactory	set_dportr   setRemoteHosthasattrr7   r   r   r   r   r   r   set_kerberosr   r   get_dce_rpc)r   r*   r+   stringbindingrpctransportr   r   r    __get_transportZ   s   


zMachineRole.__get_transportc                 C   s   i }t |d}| j|d d d  |d< |d d d |d< |d d d |d	< |d d d
 |d< t|d d d |d< |S )N   
DomainInfoDomainInfoBasicr   zMachine RoleDomainNameFlatzNetBIOS Domain NameDomainNameDnszDomain NameDomainForestNamezForest Name
DomainGuidzDomain GUID)r   #hDsRolerGetPrimaryDomainInformationMACHINE_ROLESr   )r   r,   r.   domain_infor   r   r    __fetchi   s   zMachineRole.__fetchc                 C   s   t d|  td d S )Nz Error while enumerating host: %srC   )r8   criticalsysexit)r   errorr   r   r    __log_and_exitu   s   zMachineRole.__log_and_exit)r	   r	   r	   NNFNr
   )__name__
__module____qualname__r   DSROLE_MACHINE_ROLE DsRole_RoleStandaloneWorkstationDsRole_RoleMemberWorkstationDsRole_RoleStandaloneServerDsRole_RoleMemberServer!DsRole_RoleBackupDomainController"DsRole_RolePrimaryDomainControllerrK   r!   r1   r"   r2   r&   r$   r   r   r   r    r   "   s,    
r   __main__Tz=Retrieve a host's role along with its primary domain details.)add_helpdescriptiontargetstorez6[[domain/]username[:password]@]<targetName or address>)actionhelpz-ts
store_truez&Adds timestamp to every logging outputz-debugzTurn DEBUG output ON
connectionz-dc-ipz
ip addresszoIP Address of the domain controller. If ommited it use the domain part (FQDN) specified in the target parameter)rb   metavarrc   z
-target-ipzIP Address of the target machine. If ommited it will use whatever was specified as target. This is useful when target is the NetBIOS name and you cannot resolve itz-port139445?zdestination portz)Destination port to connect to SMB Server)choicesnargsdefaultrf   rc   authenticationz-hasheszLMHASH:NTHASHz$NTLM hashes, format is LMHASH:NTHASHz-no-passz&don't ask for password (useful for -k)z-kzUse Kerberos authentication. Grabs credentials from ccache file (KRB5CCNAME) based on target parameters. If valid credentials cannot be found, it will use the ones specified in the command linez-aesKeyzhex keyz<AES key to use for Kerberos Authentication (128 or 256 bits)rC   r	   F)getpassz	Password:)0rO   r8   argparseimpacket.examplesr   impacket.examples.utilsr   impacketr   impacket.uuidr   impacket.dcerpc.v5r   r   r   rS   r(   BANNERArgumentParserparseradd_argumentadd_argument_groupgrouplenargv
print_helprP   
parse_argsoptionsinittsr9   r`   r   r   r   r*   	target_ipr   kr   no_passrn   dc_ipintr   machine_roler1   r   r   r   r    <module>   sX   
W





8$