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re  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"Z0e01  W dS  e2y> Z3 ze4 j5ej6kr,d dl7Z7e78  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rt|d d dkrzd}qtt| 	t
t|d d }qt|d dkrt|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=%sd   )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_truezReturn 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-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)
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



(


