o
    àý°j–$  ã                
   @   s‚  d dl mZ d dl mZ d dl mZ 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 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d� 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� ejd&dd'd� ejd(dd)d*d� e d+¡Zejd,dd-d.d� ejd/dd0d1d� eejƒd2krÕe ¡  e  d2¡ e !¡ Z"e #e"j$e"j%¡ ee"j&e"j'e"j(e"j)e"j*ƒ\Z+Z,Z-Z.Z.e"_*e+d3k�re /d4¡ e  d2¡ zee,e-e+e"ƒZ0e0 1¡  W dS  e2�y> Z3 ze 4¡ j5ej6k�r,d dl7Z7e7 8¡  e 9e:e3ƒ¡ W Y dZ3[3dS dZ3[3ww dS )5é    )Údivision)Úprint_function)Úunicode_literalsN)Údatetime)Úversion)ÚUF_ACCOUNTDISABLE)Úlogger)Úparse_identityÚ
ldap_login)ÚldapÚldapasn1c                   @   s0   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	S )
Ú
GetADUsersc                 C   sê   || _ || _|| _|| _d | _d| _d| _|j| _|j	| _
|j| _|j| _|j| _|j| _|jd ur<|j d¡\| _| _| j d¡}d| _|D ]}|  jd| 7  _qG| jd d… | _g d¢| _g d¢| _d d	d
„ t| jƒD ƒ¡| _d S )NÚ ú:Ú.zdc=%s,éÿÿÿÿ)ÚNameÚEmailÚPasswordLastSetÚ	LastLogon)é   é   é   r   ú c                 S   s   g | ]
\}}d ||f ‘qS )z	{%d:%ds} © )Ú.0ÚnumÚwidthr   r   úŠ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/../../../bin/GetADUsers.pyÚ
<listcomp>H   s    z'GetADUsers.__init__.<locals>.<listcomp>)ÚoptionsÚ_GetADUsers__usernameÚ_GetADUsers__passwordÚ_GetADUsers__domainÚ_GetADUsers__targetÚ_GetADUsers__lmhashÚ_GetADUsers__nthashÚaesKeyÚ_GetADUsers__aesKeyÚkÚ_GetADUsers__doKerberosÚdc_ipÚ_GetADUsers__kdcIPÚdc_hostÚ_GetADUsers__kdcHostÚuserÚ_GetADUsers__requestUserÚallÚ_GetADUsers__allÚhashesÚsplitÚbaseDNÚ_GetADUsers__headerÚ_GetADUsers__colLenÚjoinÚ	enumerateÚ_GetADUsers__outputFormat)ÚselfÚusernameÚpasswordÚdomainÚcmdLineOptionsÚdomainPartsÚir   r   r   Ú__init__*   s.   


 zGetADUsers.__init__c                 C   s   | d8 } | d } | S )Nl     }*{Gíi€–˜ r   )Útr   r   r   ÚgetUnixTimeJ   s   zGetADUsers.getUnixTimec              
   C   s   t |tjƒdur
d S d}d}d}d}zš|d D ]†}t|d ƒdkr=|d d  ¡  d	¡ d
¡du r<|d d  ¡  d	¡}qt|d ƒdkret|d d ƒdkrRd}qtt |  	t
t|d d ƒƒ¡¡ƒ}qt|d ƒdkr�t|d d ƒdkrzd}qtt |  	t
t|d d ƒƒ¡¡ƒ}qt|d ƒdkr�t|d d ƒ}qt| jj||||gŽ ƒ W d S  tyÏ } ztjddd� t dt|ƒ ¡ W Y d }~d S d }~ww )NTr   zN/AÚ
attributesÚtypeÚsAMAccountNameÚvalsr   zutf-8ú$FÚ
pwdLastSetÚ0z<never>Ú	lastLogonÚmailÚ	Exception)Úexc_infoz-Skipping item, cannot process due to error %s)Ú
isinstancer   ÚSearchResultEntryÚstrÚasOctetsÚdecodeÚendswithr   ÚfromtimestamprD   ÚintÚprintr:   ÚformatrN   ÚloggingÚdebugÚerror)r;   ÚitemrG   rJ   rM   rL   Ú	attributeÚer   r   r   ÚprocessRecordP   s<    €&&€€ýzGetADUsers.processRecordc                 C   s  t | j| j| j| j| j| j| j| j| j	| j
| jƒ}|j| _t d| j ¡ t| jj| jŽ ƒ td dd„ | jD ƒ¡ƒ | jrAd}ndt }| jd urR|d| j 7 }n|d7 }zt d	| ¡ tjd
d�}|j|g d¢d|g| jd� W n	 tjy|   ‚ w | ¡  d S )Nz)Querying %s for information about domain.z  c                 S   s   g | ]}d | ‘qS )ú-r   )r   ÚitemLenr   r   r   r   x   s    z"GetADUsers.run.<locals>.<listcomp>z)(&(sAMAccountName=*)(objectCategory=user)zN(&(sAMAccountName=*)(mail=*)(!(UserAccountControl:1.2.840.113556.1.4.803:=%d))z(sAMAccountName:=%s))ú)zSearch Filter=%séd   )Úsize)rG   rJ   rM   rL   r   )ÚsearchFilterrE   Ú	sizeLimitÚsearchControlsÚperRecordCallback)r
   r$   r5   r,   r.   r*   r!   r"   r#   r%   r&   r(   Ú_dstHostrZ   ÚinforX   r:   rY   r6   r8   r7   r2   r   r0   r[   r   ÚSimplePagedResultsControlÚsearchr`   ÚLDAPSearchErrorÚclose)r;   ÚldapConnectionrf   Úscr   r   r   Úrunp   s,   2


þÿzGetADUsers.runN)Ú__name__Ú
__module__Ú__qualname__rB   ÚstaticmethodrD   r`   rr   r   r   r   r   r   )   s     
 r   Ú__main__Tz$Queries target domain for users data)Úadd_helpÚdescriptionÚtargetÚstorezdomain[/username[:password]])ÚactionÚhelpz-userr<   z Requests data for specific user )r|   Úmetavarr}   z-allÚ
store_truez Return all users, including those with no email addresses and disabled accounts. When used with -user it will return user's info even if the account is disabledz-tsz&Adds timestamp to every logging outputz-debugzTurn DEBUG output ONÚ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)Ú
connectionz-dc-ipz
ip addresszoIP Address of the domain controller. If ommited it use the domain part (FQDN) specified in the target parameterz-dc-hostÚhostnamez|Hostname of the domain controller to use. If ommited, the domain part (FQDN) specified in the account parameter will be usedé   r   zDomain should be specified!);Ú
__future__r   r   r   ÚargparserZ   Úsysr   Úimpacketr   Úimpacket.dcerpc.v5.samrr   Úimpacket.examplesr   Úimpacket.examples.utilsr	   r
   Úimpacket.ldapr   r   r   rs   rX   ÚBANNERÚArgumentParserÚparserÚadd_argumentÚadd_argument_groupÚgroupÚlenÚargvÚ
print_helpÚexitÚ
parse_argsr    ÚinitÚtsr[   rz   r3   Úno_passr'   r)   r>   r<   r=   Ú_ÚcriticalÚexecuterrr   rN   r_   Ú	getLoggerÚlevelÚDEBUGÚ	tracebackÚ	print_excr\   rR   r   r   r   r   Ú<module>   sb   
h



(


€üÏ