o
    Žõ±j   ã                   @   s–   d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ G dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ dƒZdS )é    N)ÚThread)ÚProcessÚQueue)Úlogger)ÚNetLocalGroupGetMembers)Ú
is_port_up)ÚUniversalEncoderc                   @   s(   e Zd Zg d¢ddfdd„Zdd„ ZdS )	ÚLocalGroupEnumThread©zRemote Desktop UsersÚAdministratorszDistributed COM Usersé   Tc                 C   s,   t  | ¡ || _|| _|| _|| _|| _d S ©N)r   Ú__init__ÚinQÚoutQÚtimeoutÚ	pre_checkÚgroups)Úselfr   r   r   r   r   © r   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pypykatz/remote/live/localgroup/enumerator.pyr      s   

zLocalGroupEnumThread.__init__c              
   C   s¤   	 | j  ¡ }|s
d S | jdu rt|d| jd�du rq z| jD ]}t||dd�D ]}| j |||f¡ q'qW n t	yP } zt
 dt|ƒ ¡ W Y d }~q d }~ww q)NTi½  )r   Fé   )ÚlevelzLocalGroupEnumThread error: %s)r   Úgetr   r   r   r   r   r   ÚputÚ	Exceptionr   ÚdebugÚstr)r   ÚtargetÚ	groupnameÚgroupÚer   r   r   Úrun   s&   


ÿÿ€þõzLocalGroupEnumThread.runN©Ú__name__Ú
__module__Ú__qualname__r   r"   r   r   r   r   r	      s    r	   c                   @   s   e Zd Zddd„Zdd„ ZdS )	ÚLocalGroupEnumProcr   Tc                 C   s8   t  | ¡ || _|| _|| _g | _|| _|| _|| _d S r   )	r   r   r   r   Ú	threadcntÚthreadsr   r   r   )r   r   r   r(   r   r   r   r   r   r   r   %   s   

zLocalGroupEnumProc.__init__c                 C   s^   t | jƒD ]}t| j| j| j| j| jd�}d|_| 	¡  | j
 |¡ q| j
D ]}| ¡  q&d S )N©r   r   r   T)Úranger(   r	   r   r   r   r   r   ÚdaemonÚstartr)   ÚappendÚjoin)r   ÚiÚtr   r   r   r"   /   s   

ÿzLocalGroupEnumProc.runN)r   Tr#   r   r   r   r   r'   $   s    

r'   c                   @   s&   e Zd Zd	dd„Zdd„ Zdd„ ZdS )
Ú	LGResProcNFc                 C   s&   t  | ¡ || _i | _|| _|| _d S r   )r   r   r   ÚresultsÚout_fileÚto_json)r   r   r4   r5   r   r   r   r   9   s
   

zLGResProc.__init__c                 C   s   d S r   r   ©r   r   r   r   Úsetup@   s   zLGResProc.setupc              	   C   s”  |   ¡  	 | j ¡ }|snL|\}}}| jdu r.|| jvr!g | j|< | j|  || ¡ g¡ n*d|||j|jt	|j
ƒf }| jd urT|| jvrKg | j|< | j|  |¡ nt|ƒ q| jd u re| jdu red S t d¡ | jd ur¼t| jddd��;}| jdu r‹| tj| jtddd	�¡ n| jD ]}| j| D ]}| d
||f ¡ q•qŽW d   ƒ d S W d   ƒ d S 1 sµw   Y  d S ttj| jtddd	�ƒ d S )NTz%s %s %s %s %sFzWriting results...ÚwÚ )Únewlineé   )ÚclsÚindentÚ	sort_keysz%s %s
)r7   r   r   r5   r3   r.   Úto_dictÚdomainÚusernamer   Úsidr4   Úprintr   ÚinfoÚopenÚwriteÚjsonÚdumpsr   )r   Úresultr   r   r    ÚfÚresr   r   r   r"   C   sB   







ì



ÿýþ"üzLGResProc.run)NF)r$   r%   r&   r   r7   r"   r   r   r   r   r2   8   s    
r2   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚLocalGroupEnumeratorc                 C   sT   g | _ tƒ | _tƒ | _g | _d | _d| _d| _d| _d| _	d | _
d| _g d¢| _d S )Nr;   r   TFr
   )Úhostsr   r   r   ÚagentsÚresult_processÚagent_proccntÚagent_threadcntr   r   r4   r5   r   r6   r   r   r   r   l   s   zLocalGroupEnumerator.__init__c                 C   s<   d}dg}|  ||¡D ]}| j |d d d d… ¡ qd S )Nz(&(sAMAccountType=805306369))ÚsAMAccountNameÚ
attributeséÿÿÿÿ)ÚpagedsearchrM   r.   )r   ÚldapÚldap_filterrS   Úentryr   r   r   Úload_targets_ldap~   s
   ÿz&LocalGroupEnumerator.load_targets_ldapc                 C   sX   t |dƒ�}|D ]}| ¡ }|dkrq| j |¡ qW d   ƒ d S 1 s%w   Y  d S )NÚrr9   )rE   ÚstriprM   r.   )r   ÚfilenamerJ   Úliner   r   r   Úload_targets_file…   s   ü"ÿz&LocalGroupEnumerator.load_targets_filec                 C   s   |  j |7  _ d S r   )rM   )r   Útargetsr   r   r   Úload_tagets�   s   z LocalGroupEnumerator.load_tagetsc              	   C   sê   t | j| j| jd�| _d| j_| j ¡  t| jƒD ]}t	| j
| j| j| j| j| jd�}d|_| ¡  | j |¡ qt d¡ | jD ]}| j
 |¡ qA| jD ]}t| jƒD ]}| j
 d ¡ qTqM| jD ]}| ¡  qa| j d ¡ | j ¡  d S )N)r4   r5   Tr*   z === Enumerating local groups ===)r2   r   r4   r5   rO   r,   r-   r+   rP   r'   r   rQ   r   r   r   rN   r.   r   rD   rM   r   r/   )r   r0   Úpr1   Úar   r   r   r"   �   s&   
 


ÿ

zLocalGroupEnumerator.runN)r$   r%   r&   r   rY   r^   r`   r"   r   r   r   r   rL   k   s    rL   )rG   Ú	threadingr   Úmultiprocessingr   r   Úpypykatzr   Ú4pypykatz.commons.winapi.local.function_defs.netapi32r   Ú"pypykatz.remote.live.common.commonr   Úpypykatz.commons.commonr   r	   r'   r2   rL   r   r   r   r   Ú<module>   s    3