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r·e ¡  e  d/¡ e ¡ Ze ejej¡ eej ƒ\Z!Z"Z#Z$e!du rÒd0Z!ej%du rÚe$e_%ej&durâd
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-ipz£IP 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-kzÁUse 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$Ê