%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/samba/emulate/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/samba/emulate/__pycache__/traffic.cpython-312.pyc

�

�I�d�S�
��ddlZddlZddlZddlZddlZddlZddlZddlmZm	Z	ddl
mZmZm
Z
mZddlmZddlmZddlmZddlZddlmZddlmZdd	lmZmZmZdd
lmZddlmZddlm Z dd
lm!Z!ddl"m#Z#ddl$Z$ddl%m&Z&m'Z'm(Z(ddl)m*Z*ddl+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1ddl2m3Z3ddl2m4Z4ddl5m6Z6ddl7m8Z8ddl9Z9dZ:dZ;dZ<dZ=dddddddddd�	Z>ddddddddd�Z?hd�Z@dZAdeAzZBdZCdZDe8eE� �ZFd!�ZGd"�ZHd#�ZIGd$�d%eJ�ZKGd&�d'eL�ZMd(�ZNd)�ZOGd*�d+eL�ZPGd,�d-eL�ZQGd.�d/eL�ZRGd0�d1eR�ZS�dd2�ZTd3�ZUd4�ZVd5�ZWGd6�d7eL�ZX�dd8�ZYid9d:�d;d<�d=d<�d>d<�d?d<�d@d<�dAd:�dBd<�dCd<�dDd<�dEd<�dFd<�dGd:�dHd:�dId<�dJd:�ZZidKdL�dMdN�dOdP�dQdR�dSdT�dUdV�dWdX�dYdZ�d[d\�d]d^�d_d`�dadb�dcdd�dedf�dgdh�didj�dkdl�idmdn�dodp�dqdr�dsdt�dudv�dwdx�dydz�d{d|�d}d~�dd��d�d��d�d��d�d��d�dX�d�d��d�dZ�d�d���id�d��d�d��d�d��d�d��d�d��d�d��d�d��d�dl�d�dn�d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d���id�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d“d�dēd�dƓd�dȓd�dʓd�d̓d�dΓ�id�dГd�dғd�dԓd�d֓d�dؓd�dړd�dܓd�dޓd�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d��d�d�d�d�d�d�d�d�d���Z[d��Z\d��Z]�dd��Z^�dd��Z_									�dd��Z`d��Zad��Zb�d�Zce�d�d�Zd�d�Ze	�d�d�Zf�d�Zg�d�Zh�d�Zi�d�d�Zj�d	�Zk�d�d
�Zl	�d�d�Zm�d�Zn�d
�Zo�d�Zp	�d�d�ZqG�d��deL�Zr�d�Zs�d�Zt�d�Zu�d�Zv�d�Zw�d�Zxy(�N)�ECHILD�ESRCH)�OrderedDict�Counter�defaultdict�
namedtuple)�query)�traffic_packets)�SamDB)�LdbError)�ClientConnection)�security�drsuapi�lsa)�netlogon)�netr_Authenticator)�srvsvc)�samr)�
drs_DsBind)�Credentials�DONT_USE_KERBEROS�MUST_USE_KERBEROS)�system_session)�UF_NORMAL_ACCOUNT�UF_SERVER_TRUST_ACCOUNT�UF_TRUSTED_FOR_DELEGATION�UF_WORKSTATION_TRUST_ACCOUNT)�SEC_CHAN_BDC)�gensec)�sd_utils)�
get_string)�get_samba_logger�ga2U0*�3?�-��?)	��dns�0��smb�0x72��ldapr(�r-�3�r-�2��cldapr/��dcerpc�11�r5�14��nbnsr()�r'�1�r-r<�r-�4�r-�5�r3rA�r5�12�r5�13�r5�15>r*�smb2�browser�smb_netlogong$@)i�������)�namec��|tkrF|st|tj��yt|t	|�ztj��yy)a�Print a formatted debug message to standard error.


    :param level: The debug level, message will be printed if it is <= the
                  currently set debug level. The debug level can be set with
                  the -d option.
    :param msg:   The message to be logged, can contain C-Style format
                  specifiers
    :param args:  The parameters required by the format specifiers
    ��fileN)�DEBUG_LEVEL�print�sys�stderr�tuple)�level�msg�argss   �7/usr/lib/python3/dist-packages/samba/emulate/traffic.py�debugrZes9��
�����#�C�J�J�'��#��d��#�#�*�*�5�	�c�B�tjd��}td|dd�d|dd�d�dtj��|D]}t|tj�	��ttj�	�tjj�y
)zL Print an unformatted log message to stderr, containing the line number
    r#)�limit� rz	:�z )�endrPrON)�	traceback�
extract_stackrRrSrT�flush)rX�tb�as   rY�debug_linenorfws{��
�	 �	 �q�	)�B�	�!�!�u�Q�x��A��q��3�9<��z�z���"��
�a�c�j�j�!�"�	�s�z�z���J�J���r[c�`��|r'd}|D]}|dz
}||z}|dz}�dd|zz��fd�}|Sd�}|S)z�Return a function that prints a coloured line to stderr. The colour
    of the line depends on a sort of hash of the integer arguments.���z
[38;5;%dm�c�h��tdkDr(|D]"}t��|�d�tj���$yy)NrzrO�rQrRrSrT)rXre�prefixs  �rY�pzrandom_colour_print.<locals>.p�s3����Q���I�A��F�A�6�S�Z�Z�H�I�r[c�\�tdkDr#|D]}t|tj���yy)NrrOrl)rXres  rYrnzrandom_colour_print.<locals>.p�s+���Q���.�A��!�#�*�*�-�.�r[�)�seeds�s�xrnrms    @rY�random_colour_printrt�s`���
����	�A�
��G�A�
��F�A�
��H�A�	�!�B��F�+��	I�
�H�	.�

�Hr[c��eZdZy)�FakePacketErrorN)�__name__�
__module__�__qualname__rpr[rYrvrv�s��r[rvc�d�eZdZdZdZd�Zed��Zdd�Zd�Z	d�Z
d�Zd	�Zd
�Z
d�Zd�Zdd�Zy
)�PacketzDetails of a network packet�
�	timestamp�ip_protocol�
stream_number�src�dest�protocol�opcode�desc�extra�	endpointsc
�*�||_||_||_||_||_||_||_||_|	|_|j|jkr|j|jf|_	y|j|jf|_	y�Nr|)
�selfr}r~rr�r�r�r�r�r�s
          rY�__init__zPacket.__init__�s{��"���&���*��������	� ��
������	���
��8�8�d�i�i��"�h�h��	�	�2�D�N�"�i�i����2�D�Nr[c���|jd�jd�}|dd\}}}}}}}	}
|dd}t|�}t|�}t|�}||||||||	|
|�	S)N�
�	�)�rstrip�split�float�int)�cls�line�fieldsr}r~rr�r�r�r�r�r�s            rY�	from_linezPacket.from_line�s������T�"�(�(��.������	��	�	�	�	
�	�	�	
��q�r�
���)�$�	��#�h���4�y���9�k�=�#�t��V�T�5�2�	2r[c�
�dj|j�}|j|z}|d||j|jxsd|j
|j|j|j|j|f	zfS)z5Format the packet as a traffic_summary line.
        r�z%f	%s	%s	%d	%d	%s	%s	%s	%s�)
�joinr�r}r~rr�r�r�r�r�)r��time_offsetr��ts    rY�
as_summaryzPacket.as_summary�s~���	�	�$�*�*�%���N�N�[�(���7���!�!��#�#�)�r������������������	�	�		r[c
�&�d|j|j|j|jxsd|j|j
|j|j|jr$ddj|j�zdzf	zSdf	zS)Nz:%.3f: %d -> %d; ip %s; strm %s; prot %s; op %s; desc %s %sr$�«r^�»r�)
r}r�r�r~rr�r�r�r�r��r�s rY�__str__zPacket.__str__�s���L�������4�9�9�d�6F�6F�6M�#��#�#�T�]�]�D�K�K����8<�
�
�$����$�*�*�-�-��4�M�M�	N�IK�M�M�	Nr[c��d|zS)Nz<Packet @%s>rpr�s rY�__repr__zPacket.__repr__�s
����$�$r[c���|j|j|j|j|j|j
|j|j|j|j�	Sr�)
�	__class__r}r~rr�r�r�r�r�r�r�s rY�copyzPacket.copy�sU���~�~�d�n�n�"�.�.�"�0�0�"�h�h�"�i�i�"�m�m�"�k�k�"�i�i�"�j�j�*�	*r[c�<�|j�d|j��}|S)N�:�r�r�)r�r�s  rY�as_packet_typezPacket.as_packet_type�s���}�}�d�k�k�2���r[c�z�|j|jf}|tvr	t|S|tvr
t|Sy)z�A positive number means we think it is a client; a negative number
        means we think it is a server. Zero means no idea. range: -1 to 1.
        �)r�r��CLIENT_CLUES�SERVER_CLUES)r��keys  rY�client_scorezPacket.client_score�sB���}�}�d�k�k�*���,����$�$��,�� ��%�%�%�r[c
���d|j�d|j��}	tt|�}|jdk7rtdd|j�d	|���tj�}	||||�rKtj�}||z
}td
||j|j|j|fz�yy#t$r5}td|j�d|��tj��Yd}~yd}~wwxYw#t$rU}tj�}||z
}td||j|j|j||fz�Yd}~yd}~wwxYw)z�Send the packet over the network, if required.

        Some packets are ignored, i.e. for  protocols not handled,
        server response messages, or messages that are generated by the
        protocol layer associated with other packets.
        �packet_�_z
Conversation(z) Missing handler rON�kerberosr#z) Calling handler z%f	%s	%s	%s	%f	True	z%f	%s	%s	%s	%f	False	%s)r�r��getattrr
�AttributeErrorrR�conversation_idrSrTrZ�time�	Exception)	r��conversation�context�fn_name�fn�e�startr`�durations	         rY�playzPacket.playsH��%)�M�M�4�;�;�?��	���'�2�B��=�=�J�&��!�"�2�2�G�=�
>��	�	���	.��$��g�.��i�i�k����;���2��L�8�8�$�-�-��{�{�H�.�.�/�/���	���/�/��:��z�z�
#�
��		��,�	.��)�)�+�C��U�{�H��1���4�4�d�m�m��;�;��!�-�-�
.�
.��	.�s1�C�.AD�	D�
+C=�=D�	E#�AE�E#c�4�|j|jz
Sr��r}�r��others  rY�__cmp__zPacket.__cmp__+s���~�~����/�/r[Nc�B�t|j|j�Sr�)�is_a_real_packetr�r�)r��missing_packet_statss  rY�is_really_a_packetzPacket.is_really_a_packet.s����
�
�t�{�{�;�;r[)r�r�)rwrxry�__doc__�	__slots__r��classmethodr�r�r�r�r�r�r�r�r�r�rpr[rYr{r{�sT��%�	�I�3� �2��2�&� N�%�	*��	�&.�P0�<r[r{c���|tvry|dk(r|dk(ryd|�d|��}tt|d�}|�)tj	d|zt
j��y|tjuryy	)
zdIs the packet one that can be ignored?

    If so removing it will have no effect on the replay
    Fr-r�r�r�Nzmissing packet %srOT)�SKIPPED_PROTOCOLSr�r
�LOGGERrZrSrT�null_packet)r�r�r�r�s    rYr�r�2sp��
�$�$���6��f��l��� (�&�1�G�	��'�4�	0�B�	�z����(�7�2�����D��	�_�
(�
(�(��r[c�4�|dk(ry||fdvryt||�S)z�Return true if a packet generates traffic in its own right. Some of
    these will generate traffic in certain contexts (e.g. ldap unbind
    after a bind) but not if the conversation consists only of these packets.
    �waitF)�r�r�r0rG�r5�16)r�r�s  rY�is_a_traffic_generating_packetr�Hs3��
�6����&����
��H�f�-�-r[c
���eZdZdZddddddddddej
j
d�ddf
d�Zd�Zd�Z	d�Z
d�Zd	�Zd
�Z
dd�Zdd�Zdd
�Zdd�Zdd�Zdd�Zdd�Zdd�Zd�Zd�Zd�Zd�Zy)�
ReplayContextaWState/Context for a conversation between an simulated client and a
       server. Some of the context is shared amongst all conversations
       and should be generated before the fork, while other context is
       specific to a particular conversation and should be generated
       *after* the fork, in generate_process_local_config().
    N�DOMAINc��||_d|_||_||_|rt|_nt|_|	|_|
|_||_	||_
||_||_|jd�|_|
|_||_d|_d|_d|_d|_d|_d|_d|_d|_||_|j5�y)N�realmF)�server�netlogon_connection�creds�lpr�kerberos_stater�ou�base_dn�domain�statsdir�global_tempdir�
domain_sid�getr��instance_id�badpassword_frequency�last_lsarpc_bad�last_lsarpc_named_bad�last_simple_bind_bad�
last_bind_bad�last_srvsvc_bad�last_drsuapi_bad�last_netlogon_bad�last_samlogon_bad�total_conversations�generate_ldap_search_tables)r�r�r�r�r�r��prefer_kerberos�tempdirr�r�r�r�r�r�s              rYr�zReplayContext.__init__as���)/���(,�� �(-��
�(*����"3�D��"3�D��(*���(/���(.���(0��
�(/���(2���(*���w���
�(3���)>��"�(-���(-��"�(-��!�(-���(-���(-���(-���(-���(;�� ��(�(�*r[c�l�t�}td|jz||j|j��}|j|j
�tjdgdg��}i}dgi}|D]�}t|j�}djd�|jd�D��j�}|j|g�}	|	j|�|j!d	�s��|dj|���t#|j%��D]n}
|
d
ddk7r�|
dd
}|d
ddk(r|dd
}|d
ddk(r�t'd�D]:}|dz
}||
k7r&||vr"t)d
|
�d|��t*j,���3||
||<�<�p||_||_i|_|j|j
�tjdgd��}djd�|D��}
dj5|
�|j2d<d}dD]%}|dj5||j
��z
}�'dj5|�|j2d<d|j2d<|jdtj6dg��}dj5|dd�|j2d<y) N�	ldap://%s)�url�session_info�credentialsr�zpaged_results:1:1000�dn)�scope�controls�attrs�invocationId�,c3�BK�|]}|j�dd���y�w)Nr#)�lstrip��.0rss  rY�	<genexpr>z<ReplayContext.generate_ldap_search_tables.<locals>.<genexpr>�s����E�!�q�x�x�z�"�1�~�E�s�zCN=NTDS Settings,rLz,DC�zdn_map collision r^rOz"(objectclass=groupPolicyContainer))r�r��
expressionr�c3�DK�|]}dj|d����y�w)z(distinguishedName={0})r�N)�format)rrWs  rYrz<ReplayContext.generate_ldap_search_tables.<locals>.<genexpr>�s ����X�S�6�=�=�c�$�i�H�X�s� z(|{0})�gPCFileSysPath)zDomain Controllers,ztraffic_replay,r�z(distinguishedName={0}{1})�gpLinkz'(objectCategory=pKICertificateTemplate)�pKIExtendedKeyUsage�highestCommittedUSN)r�r�z(usnChanged>={0})r�
usnChanged)rrr�r�r��search�	domain_dn�ldb�
SCOPE_SUBTREE�strr�r�r��upper�
setdefault�append�
startswith�list�keys�rangerRrSrT�dn_map�attribute_clue_map�search_filtersr
�
SCOPE_BASE)r��session�db�resrr�rr��patternr'�krn�i�
gpos_by_dn�ou_strr�s                rYr�z)ReplayContext.generate_ldap_search_tables�s��� �"��
�{�T�[�[�0� '�#�z�z��g�g���
�i�i�����!�/�/�"8�!9�#�f��&�����B�
���	>�A��Q�T�T��B��h�h�E�r�x�x��}�E�E�K�K�M�G��#�#�G�R�0�C��J�J�r�N��}�}�0�1�"�>�2�9�9�"�=�
	>��f�k�k�m�$�	&�A����v�����#�2��A��B�C�&�E�/��c�r�F���B�C�&�E�/��1�X�
&���U�
����6�a�6�k��a��;�"�z�z�+��"�1�I��q�	�

&�
	&����"4���
!����i�i�����c�.?�.?��v�#G��I���W�W�X�TW�X�X�
�19���
�0K����,�-���@�	N�B��2�9�9�"�b�l�l�n�M�M�F�	N�(0����(?����H�%�

6�	
���1�2�
�i�i��#�.�.�9N�8O�i�P���&�&�s�1�v�.C�'D�E�	
���L�)r[c���|jj�D]}||vs�|j|cS|dk(r<tj�|jz}t	|j
|�}d|zSy)NzDC,DCz((&(sAMAccountName=%s)(objectClass=user))z(objectClass=*))rr�randomr��	user_namer�)r�r��dn_sigr�r��random_user_id�account_names       rY�guess_search_filterz!ReplayContext.guess_search_filter�s|���&�&�+�+�-�	0�C��e�|��*�*�3�/�/�	0��W��#�]�]�_�t�/G�/G�G�N�$�T�%5�%5�~�F�L�=��L�L�!r[c�,�g|_g|_g|_g|_g|_g|_g|_|j|_|j|_|j|_	|j|_
t|jd|jz�|_|jj!d|j�|jj!d|j�|jj!d|j�|jj!dd�d|_d|j�d	|j$��|_d|j�d	|j$��|_|j+�|j-�y)
Nzconversation-%dzprivate dirzlock dirzstate directoryztls verify peer�no_checkz/root/ncalrpc_as_system�cn=r)�ldap_connections�dcerpc_connections�lsarpc_connections�lsarpc_connections_named�drsuapi_connections�srvsvc_connections�
samr_contexts�netbios_name�machinepass�username�userpass�
mk_masked_dirr�r�r�r��set�
remoteAddressr��samlogon_dn�user_dn�generate_machine_creds�generate_user_creds)r��accountr�s   rY�generate_process_local_configz+ReplayContext.generate_process_local_config�sB��(*���(*���(*���(*��%�(*�� �(*���(*���(/�(<�(<���(/�(;�(;���(/�(8�(8��
�(/�(8�(8��
�$�T�%8�%8�%6�%1�%A�%A�&B�C���	
�����M�4�<�<�0������J����-������%�t�|�|�4������%�z�2�6���#�0�0�$�'�'�;��� $�}�}�d�g�g�7���	
�#�#�%�� � �"r[c��|s;|jr-tj�|jkr	||�d}nd}||�}||fS#t$rY�wxYw)aExecute the supplied logon function, randomly choosing the
           bad credentials.

           Based on the frequency in badpassword_frequency randomly perform the
           function with the supplied bad credentials.
           If run with bad credentials, the function is re-run with the good
           credentials.
           failed_last_time is used to prevent consecutive bad credential
           attempts. So the over all bad credential frequency will be lower
           than that requested, but not significantly.
        TF)r�r*r�)r��f�good�bad�failed_last_time�results      rY�with_random_bad_credentialsz)ReplayContext.with_random_bad_credentialssi�� ��*�*��
�
��$�"<�"<�<���c�F�
$(� �#(� ��4����(�)�)��!����s�A
�
	A�Ac�>�t�|_|jj|j�|jj	|j
�|jj
|j�|jj|j�|jj|j�|jj|j�t�|_|jj|j�|jj	|j
�|jj
|jdd�|jj|j�|jj|j�t�|_|jj|j�|jj	|j
�|jj
|j�|jj|j�|jj!|jj#�t$j&z�|jj|j�|jj)|j*�t�|_|j,j|j�|j,j	|j
�|j,j
|jdd�|j,j|j�|j,j!|j,j#�t$j&z�|j,j|j�|j,j)|j*�y)a;Generate the conversation specific user Credentials.

        Each Conversation has an associated user account used to simulate
        any non Administrative user traffic.

        Generates user credentials with good and bad passwords and ldap
        simple bind credentials with good and bad passwords.
        N���)r�
user_creds�guessr��set_workstationr:�set_passwordr=�set_usernamer<�
set_domainr��set_kerberos_stater��user_creds_bad�simple_bind_creds�set_gensec_features�get_gensec_featuresr�FEATURE_SEAL�set_bind_dnrB�simple_bind_creds_badr�s rYrDz!ReplayContext.generate_user_creds1s���&�-��������d�g�g�&����'�'��(9�(9�:����$�$�T�]�]�3����$�$�T�]�]�3����"�"�4�;�;�/����*�*�4�+>�+>�?�)�m������!�!�$�'�'�*����+�+�D�,=�,=�>����(�(����s��);�<����(�(����7����.�.�t�/B�/B�C�"-�������$�$�T�W�W�-����.�.�t�/@�/@�A����+�+�D�M�M�:����+�+�D�M�M�:����2�2��"�"�6�6�8�6�;N�;N�N�	P����1�1�$�2E�2E�F����*�*�4�<�<�8�%0�]��"��"�"�(�(����1��"�"�2�2�4�3D�3D�E��"�"�/�/��
�
�c�r�0B�C��"�"�/�/��
�
�>��"�"�6�6��&�&�:�:�<����
 �	!�	
�"�"�5�5�d�6I�6I�J��"�"�.�.�t�|�|�<r[c��t�|_|jj|j�|jj	|j
�|jj
t�|jj|j�|jj|j
dz�|jj|j�|jj|j�t�|_|jj|j�|jj	|j
�|jj
t�|jj|jdd�|jj|j
dz�|jj|j�y)z�Generate the conversation specific machine Credentials.

        Each Conversation has an associated machine account.

        Generates machine credentials with good and bad passwords.
        �$NrO)r�
machine_credsrQr�rRr:�set_secure_channel_typerrSr;rTrUr�rVr��machine_creds_badr�s rYrCz$ReplayContext.generate_machine_creds_si��)�]������ � ����)����*�*�4�+<�+<�=����2�2�<�@����'�'��(8�(8�9����'�'��(9�(9�C�(?�@����%�%�d�k�k�2����-�-�d�.A�.A�B�!,�������$�$�T�W�W�-����.�.�t�/@�/@�A����6�6�|�D����+�+�D�,<�,<�S�b�,A�B����+�+�D�,=�,=��,C�D����1�1�$�2E�2E�Fr[c��|jj|�}|rtj|�S|j	�}|r8||j
vr"tj|j
|�S|dd}|r�8|jS)N�)rr�r*�choicerrr�)r�r$�
attributes�	attr_clues    rY�get_matching_dnzReplayContext.get_matching_dnxsz���+�+�/�/�
�;�	���=�=��+�+��-�-�/����$�+�+�%��}�}�T�[�[��%9�:�:��a�b�k�G�	��|�|�r[c���d}|jr|s|jdStd|jz|df|j�}|jj	|�|S)Nz$12345678-1234-abcd-ef00-01234567cffb���zncacn_ip_tcp:%sr_)r4r
r�r�r)r��new�guid�cs    rY�get_dcerpc_connectionz#ReplayContext.get_dcerpc_connection�s_��5���"�"�3��*�*�2�.�.��.����<�"�A�Y����
1�����&�&�q�)��r[c����jr|s�jdS�fd�}�j|�j�j�j�\}�_�jj|�|S)Nrjc�`��tjd�jz�j|�S�Nzncacn_np:%s)rr�r��r�r�s �rY�connectz4ReplayContext.get_srvsvc_connection.<locals>.connect�s)����=�=��$�+�+�!>�!%���!&�(�
(r[)r8rMrPrWr�r�r�rkrsrms`   rY�get_srvsvc_connectionz#ReplayContext.get_srvsvc_connection�sw����"�"�3��*�*�2�.�.�	(�
�,�,�W�-1�_�_�-1�-@�-@�-1�-A�-A�
C�	"��D� �	
���&�&�q�)��r[c����jr|s�jdS�fd�}�j|�j�j�j�\}�_�jj|�|S)Nrjc�l��d}tjd�j�d|�d��j|�S)Nzschannel,seal,sign�
ncacn_ip_tcp:�[�]�r�lsarpcr�r�)r��binding_optionsr�s  �rYrsz4ReplayContext.get_lsarpc_connection.<locals>.connect�s1���2�O��:�:�#�{�{�O�=�"�g�g�#�%�
%r[)r5rMr`rbr�rrts`   rY�get_lsarpc_connectionz#ReplayContext.get_lsarpc_connection�sy����"�"�3��*�*�2�.�.�	%�
�,�,�W�-1�-?�-?�-1�-C�-C�-1�-A�-A�
C�	"��D� �	
���&�&�q�)��r[c����jr|s�jdS�fd�}�j|�j�j�j�\}�_�jj|�|S)Nrjc�`��tjd�jz�j|�Srqr{rrs �rYrsz?ReplayContext.get_lsarpc_named_pipe_connection.<locals>.connect�s)����:�:�m�t�{�{�;�"�g�g�#�%�
%r[)r6rMr`rbr�rrts`   rY� get_lsarpc_named_pipe_connectionz.ReplayContext.get_lsarpc_named_pipe_connection�sy����(�(���0�0��4�4�	%�
�,�,�W�-1�-?�-?�-1�-C�-C�-1�-G�-G�
I�	(��D�&�	
�%�%�,�,�Q�/��r[c����jr|s�jd}|S�fd�}�j|�j�j�j�\}�_t|�\}}||f}�jj
|�|S)zget a (drs, drs_handle) tuplerjc�p��d}d�j�d|�d�}tj|�j|�S)N�sealrxryrz)r�rr�)r�r}�binding_stringr�s   �rYrsz:ReplayContext.get_drsuapi_connection_pair.<locals>.connect�s/���$�O�"�k�k�?�<�N��?�?�>�4�7�7�E�B�Br[)r7rMrPrWr�rr)r�rk�unbindrmrs�drs�
drs_handle�supported_extensionss`       rY�get_drsuapi_connection_pairz)ReplayContext.get_drsuapi_connection_pair�s�����#�#�C��(�(��,�A��H�	C�
�,�,�W�-1�_�_�-1�-@�-@�-1�-B�-B�
D�	%��d�#�.8��_�*��)�
�*���� � �'�'��*��r[c�z���jr|s�jdS�fd�}�fd�}|r;�j|�j�j�j�\}�_n:�j|�j
�j�j�\}�_�jj|�|S)Nrjc�N��td�jz|�j��S)a$
            To run simple bind against Windows, we need to run
            following commands in PowerShell:

                Install-windowsfeature ADCS-Cert-Authority
                Install-AdcsCertificationAuthority -CAType EnterpriseRootCA
                Restart-Computer

            z
ldaps://%s�r�r��rr�r�rrs �rY�simple_bindz6ReplayContext.get_ldap_connection.<locals>.simple_bind�s'��������3�%*� �G�G�%�
%r[c�N��td�jz|�j��S)Nr�r�r�rrs �rY�	sasl_bindz4ReplayContext.get_ldap_connection.<locals>.sasl_bind�s%�����t�{�{�2�%*� �G�G�%�
%r[)	r3rMrXr]r�rPrWr�r)r�rk�simpler�r��samdbs`     rY�get_ldap_connectionz!ReplayContext.get_ldap_connection�s����� � ���(�(��,�,�	%�	%���0�0��15�1G�1G�15�1K�1K�15�1J�1J�L�
/�U�D�-��0�0��15���15�1D�1D�15�1C�1C�E�
(�U�D�&�	
���$�$�U�+��r[c���|jr|rE|jjt|j|j|j
���|jdS)N)r�r�rj)r9r�SamrContextr�r�r�)r�rks  rY�get_samr_contextzReplayContext.get_samr_contextsK���!�!�S����%�%��D�K�K�D�G�G�4�:�:�F�
H��!�!�"�%�%r[c�����jr�jS�fd�}�j|�j�j�j�\}�_|�_|S)Nc�`��tjd�jz�j|�S)Nzncacn_ip_tcp:%s[schannel,seal])rr�r�rrs �rYrsz6ReplayContext.get_netlogon_connection.<locals>.connects/����$�$�%E�&*�k�k�&3�%)�W�W�%*�,�
,r[)r�rMr`rbr�)r�rsrms`  rY�get_netlogon_connectionz%ReplayContext.get_netlogon_connectionsf����#�#��+�+�+�	,�
�,�,�W�-1�-?�-?�-1�-C�-C�-1�-C�-C�
E�	$��D�"�
$%�� ��r[c��|jdfS)N�A�r�r�s rY�guess_a_dns_lookupz ReplayContext.guess_a_dns_lookup$s���
�
�C� � r[c��|jj�}t�}|dD�cgc]}t|t�r|n
t|���!c}|j_|d|_t�}||fScc}w)N�
credentialr})	r`�new_client_authenticatorr�
isinstancer��ord�cred�datar})r��auth�currentrs�
subsequents     rY�get_authenticatorzReplayContext.get_authenticator'sy���!�!�:�:�<��%�'��&*�<�&8�:�!"�#-�Q��"4�Q�#�a�&�@�:����� ��-���'�)�
���$�$��:s�$A:c���tjj|j|�}t	|d�}|j�D]\}}t
|�d|��|���|j�y)z�Write arbitrary key/value pairs to a file in our stats directory in
        order for them to be picked up later by another process working out
        statistics.�wz: rON)�os�pathr�r��open�itemsrR�close)r��filename�kwargsrHr%�vs      rY�write_statszReplayContext.write_stats1sY���7�7�<�<��
�
�x�8����3����L�L�N�	-�D�A�q��a��#�!�,�	-�	���	r[r�)F)FF)rwrxryr�r��environr�r�r�r/rFrMrDrCrhrnrur~r�r�r�r�r�r�r�r�rpr[rYr�r�Zs�������%)�'+�!%������
�
���x�0� �!�*+�XPF�n!�$#�>*�8,=�\G�2�,��$�(�$�.$�L&��$!�%�r[r�c�$�eZdZdZdd�Zd�Zd�Zy)r�z5State/Context associated with a samr connection.
    Nc��d|_d|_d|_d|_d|_d|_d|_||_||_||_	yr�)
�
connection�handle�
domain_handler��group_handle�user_handle�ridsr�r�r�)r�r�r�r�s    rYr�zSamrContext.__init__?sK��!���!���!���!���!���!���!��	�#������"��
r[c��|js>tjd|jz|j|j��|_|jS)Nzncacn_ip_tcp:%s[seal])�lp_ctxr�)r�rr�r�r�r�s rY�get_connectionzSamrContext.get_connectionKs?�����"�i�i�'�4�;�;�7��w�w� �J�J�(�D�O�
���r[c��|js5|j�}|jdtj�|_|jSr�)r�r��Connect2r�SEC_FLAG_MAXIMUM_ALLOWED)r�rms  rY�
get_handlezSamrContext.get_handleTs9���{�{��#�#�%�A��*�*�T�8�+L�+L�M�D�K��{�{�r[�NN)rwrxryr�r�r�r�rpr[rYr�r�<s���
#��r[r�c�p�eZdZdZ		dd�Zd�Zd�Z	dd�Zd�ZeZ	d�Z
d	�Zd
�Zd�Z
dd�Zdd
�Zd�Zd�Zy)�ConversationzADetails of a converation between a simulated client and a server.Nc��||_||_g|_t|�|_d|_||_|D]}|j|��y)Nr�)�
start_timer��packetsrtrW�client_balancer��add_short_packet)r�r�r��seqr�rns      rYr�zConversation.__init__]sR��$���"������&�y�1���!���.����	&�A�!�D�!�!�1�%�	&r[c��|j�|j�yy|j�y|j|jz
S)Nrrjr_)r�r�s  rYr�zConversation.__cmp__hsB���?�?�"����'������#������!1�!1�1�1r[c�p�|j�}|j�|j|_|j�|j|_|j|jk7r%t	d|j�d|j����|xj|jzc_|j
|jdk(r$|xj|j�zc_n#|xj|j�z
c_|j�r|jj|�yy)zmAdd a packet object to this conversation, making a local copy with
        a conversation-relative timestamp.NzConversation endpoints z don't matchpacket endpoints r)r�r�r}r�rvr�r�r�r�r�r)r��packetrns   rY�
add_packetzConversation.add_packetqs���
�K�K�M���?�?�"��k�k�D�O��>�>�!��[�[�D�N��;�;�$�.�.�(�!�#'�>�>�1�;�;�#@�A�
A�	
���t���&���5�5�A�K�K��N�"����1�>�>�#3�3�����1�>�>�#3�3�����!��L�L����"�"r[c��|r
t||�sy|j�\}}|s||}}||f}	tj|	d�}
tj|d�}t||jz
|d|||||
|�	}|j|jdk(r$|xj|j�zc_	n#|xj|j�z
c_	|j�r|jj|�yy)z�Create a packet from a timestamp, and 'protocol:opcode' pair, and a
        (possibly empty) list of extra data. If client is True, assume
        this packet is from the client to the server.
        Nr��06r)r��guess_client_server�OP_DESCRIPTIONSr��IP_PROTOCOLSr{r�r�r�r�r�r�r�r)
r�r}r�r�r��client�skip_unused_packetsr�r�r�r�r~r�s
             rYr�zConversation.add_short_packet�s����'7��&�'I���,�,�.�	��T���c��C��� ���"�"�3��+��"�&�&�x��6���	�D�O�O�3�[��C�� �&�$��7���:�:��)�)�!�,�,����6�#6�#6�#8�8�����6�#6�#6�#8�8���$�$�&��L�L����'�'r[c�v�d|j|j|jt|j�fzS)Nz-<Conversation %s %s starting %.3f %d packets>)r�r�r��lenr�r�s rYr�zConversation.__str__�s5��?��%�%�t�~�~�t����T�\�\�"�$�$�	%r[c�,�t|j�Sr�)�iterr�r�s rY�__iter__zConversation.__iter__�s���D�L�L�!�!r[c�,�t|j�Sr�)r�r�r�s rY�__len__zConversation.__len__�s���4�<�<� � r[c��t|j�dkry|jdj|jdjz
S)Nr#rrj)r�r�r}r�s rY�get_durationzConversation.get_duration�s>���t�|�|��q� ���|�|�B��)�)�D�L�L��O�,E�,E�E�Er[c�r�|jD�cgc]}|j|j���c}Scc}wr�)r�r�r�)r�rns  rY�replay_as_summary_linesz$Conversation.replay_as_summary_lines�s'��7;�|�|�D�!����T�_�_�-�D�D��Ds�"4c��|j}tj�|z
}||z
}|tz
}|dkDrtj|�tj�|z
|z
}|j	d||fz�d}	d}
tj�}|j
D]�}tj�|z
}||jz
}||	kDr|}	|dkr\|tz
}|dkDrMtj|�tj�|z
}||jz
|
kDr||jz
}
|j||���|	||
fS)zMReplay the conversation at the right time.
        (We're already in a fork).rzstarting %s [miss %.3f]r�)r�r��SLEEP_OVERHEAD�sleeprWr�r}r�)
r�r�r�rEr��now�gap�
sleep_time�miss�max_gap�max_sleep_miss�p_startrns
             rY�replay_with_delayzConversation.replay_with_delay�s0��
�O�O���i�i�k�E�!���#�g���>�)�
���>��J�J�z�"��	�	��e�#�q�(�����*�d�D�\�9�:������)�)�+�����
	"�A��)�)�+��'�C�����#�C��W�}����Q�w�!�T�N�2�
���>��J�J�z�*��	�	��g�-�A��1�;�;���7�)*�Q�[�[���
�F�F�4��!�
	"���n�,�,r[c�~�|j\}}|jdkr||fS|jdk(r	||k(r||fS||fS)zhHave a go at deciding who is the server and who is the client.
        returns (client, server)
        r)r�r�)r��server_cluere�bs    rYr�z Conversation.guess_client_server�sQ���~�~���1�����"��q�6�M�
���!�#��q�(8��q�6�M��1�v�
r[c���|jD�cgc]}||jcxkr|ks�nn|��c}|_|jr|jdj|_yd|_ycc}w)z�Prune any packets outside the time window we're interested in

        :param s: start of the window
        :param e: end of the window
        rN�r�r}r�)r�rrr�rns    rY�forget_packets_outside_windowz*Conversation.forget_packets_outside_window�sQ��$(�<�<�I�a�1����3H�q�3H��I���7;�|�|�$�,�,�q�/�3�3�������Js
�A,�A,c��|jD]}|xj|zc_�|j�|xj|zc_yy)z=Adjust the packet start times relative to the new start time.Nr�)r�r�rns   rY�renormalise_timeszConversation.renormalise_times�sB�����	&�A�
�K�K�:�%�K�	&��?�?�&��O�O�z�)�O�'r[)NNrpN)TTr�r�)rwrxryr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rpr[rYr�r�[s\��K�<>�!%�	&�2�#�6;?�(�8%�
�H�"�!�F�
E�!-�F�"N�*r[r�c�.�eZdZdZdd�Zd�Zdd�Zdd�Zy)�	DnsHammerzOA lightweight conversation that generates a lot of dns:0 packets on
    the flyNc��t||z�}t|�D�cgc]}tjd|���c}|_|jj�||_||_d|_|j|��|_
ycc}w)Nr��
query_file)r�rr*�uniform�times�sort�rater�r��_get_query_choices�
query_choices)r��dns_rater�r��nr&s      rYr�zDnsHammer.__init__sq����8�#�$��;@��8�D�a�f�n�n�Q��1�D��
��
�
������	� ��
����!�4�4�
�4�K����Es�Bc�`�dt|j�|j|jfzS)Nz-<DnsHammer %d packets over %.1fs (rate %.2f)>)r�r�r�r�r�s rYr�zDnsHammer.__str__s)��?��T�Z�Z��$�-�-����;�<�	=r[c�P�|r�t|d�5}|j�}ddd�g}j�D]Y}|j�}|s�|j	d�r�(|jd�}t
|�dk(sJ�|j|��[|Sgd�S#1swY�}xYw)z�
        Read dns query choices from a file, or return default

        rname may contain format string like `{realm}`
        realm can be fetched from context.realm
        r#N�#r�)
)r�{realm}r��yes)r_r�NSr	)r#�	*.{realm}r��no)rdrr
r)�
�_msdcs.{realm}r�r	)�rr
r	)��nx.realm.comr�r)�rr
r)��*.nx.realm.comr�r)�rr
r)r��read�
splitlines�striprr�r�r)r�r�rH�text�choicesr�rXs       rYrzDnsHammer._get_query_choicess�����j�#�&�
 �!��v�v�x��
 ��G����)�
)���z�z�|������� 4��:�:�c�?�D��t�9��>�)�>��N�N�4�(�
)��N��
�
 �
 �s�B�B%c
��|sJ�|jsJ�tj�}|jD]�}tj�|z
}||z
}|tz
}|dkDrtj|�tj|j�\}}}	}
|j|j��}d}tj�}	t||	�}
|
dk(r
t|
�sd}tj�}||z
}td||||fz���y#t$rd}Y�;wxYw#tj�}||z
}td||||fz�wxYw)Nrr�Tr	Fz%f	DNS	dns	%s	%f	%s	)
r�r�r�r�r�r*rerr
�	dns_queryr�r�rR)r�r�r�r�r�r�r�r��rname�rtype�exist�success�packet_start�answersr`r�s                rY�replayzDnsHammer.replay4sC����w��}�}��}��	�	������	W�A��)�)�+��%�C��c�'�C��~�-�J��A�~��
�
�:�&�*0�-�-��8J�8J�*K�'�F�E�5�%��L�L�w�}�}�L�5�E��G��9�9�;�L�

W�#�E�5�1���E�>�#�g�,�#�G��i�i�k����-���2�c�6�8�W�5U�U�V�+	W�� �
 ���
 ���i�i�k����-���2�c�6�8�W�5U�U�V�s$�D�D&�#D)�%D&�&D)�)-Er�)rwrxryr�r�r�rr#rpr[rYr�r�s���L�=��BWr[r�c���tt�}g}|D]�}t|t�rt	|�}td|j��tj��|D]T}tj|�}|jdk(r|dk7r||jxxdz
cc<�D|j|��V|j���|sgdfStd�|D��}t!d�|D��}td	tj��t#�}	t%|�D]f\}
}|xj&|zc_|	j)|j*�}|�t-|
d
z��}||	|j*<|j/|��hg}|	j1�D]"}t3|�dk7s�|j|��$t5||z
�}
t3|	�|
z}|||
|fS)zLLoad a summary traffic summary file and generated Converations from it.
    z
Ingesting rOr'�includer_rc3�4K�|]}|j���y�wr�r��rrns  rYrz#ingest_summaries.<locals>.<genexpr>fs����2�Q�Q�[�[�2���c3�4K�|]}|j���y�wr�r�r's  rYrz#ingest_summaries.<locals>.<genexpr>gs����3�a�a�k�k�3�r(z$gathering packets into conversationsr#)r�)rr�r�rr�rRrMrSrTr{r�r�r�rr��min�maxr�	enumerater}r�r�r�r��valuesr�r�)�files�dns_mode�
dns_countsr�rHr�rnr��last_packet�
conversationsr&rm�conversation_listr��
mean_intervals               rY�ingest_summariesr5Ps����S�!�J��G�
����a����Q��A�
����(�s�z�z�:��	"�D�� � ��&�A��z�z�U�"�x�9�'<��1�8�8�$��)�$����q�!�	"�	
���	����1�u���2�'�2�2�J��3�7�3�3�K�	�
0�s�z�z�B��M�M��'�"����1�	���z�!�����a�k�k�*���9��a�!�e�5�A�)*�M�!�+�+�&�	���Q��
���
�
!�
!�
#�(���q�6�Q�;��$�$�Q�'�(��[�:�-�.�H��
�&��1�M��m�X�z�A�Ar[c��t�}|D]}|j|j��|r|jd�dSy)Nr_r)r�updater��most_common)r2�	addressesrms   rY�guess_server_addressr:�sH���	�I�
�&��������%�&���$�$�Q�'��*�*�r[c�f�i}|j�D]\}}dj|�}|||<�|S�Nr�)r�r�)rs�yr%r��k2s     rY�stringify_keysr?�s=��
�A����	����1�
�Y�Y�q�\����"���
�Hr[c��i}|j�D]-\}}tt|�jd��}|||<�/|Sr<)r�rUrr�)rsr=r%r�r�s     rY�unstringify_keysrA�sG��
�A����	����1��#�a�&�,�,�t�$�%����!���
�Hr[c�R�eZdZd
d�Zdd�Zd�Zd�Z					dd�Zd�Zd�Z			d
d	�Z
y)�TrafficModelc�v�i|_i|_||_tt�|_d|_ddg|_y)Nr�rr_)�ngrams�
query_detailsrrr��dns_opcounts�cumulative_duration�packet_rate)r�rs  rYr�zTrafficModel.__init__�s9�����������'��,���#&�� ��q�6��r[Nc
��|�i}d}d}tf|jdz
z}t|�}|j�D]\}}|j|xx|z
cc<�t|�dkDro|dj}	d}
|	dz}|D]3}|
t|�z
}
t||jdj�}�5|
|jd<||	z
|jd<|D�]]}|j|�\}
}||j�z
}tf|jdz
z}|D�]}|j|
k7r�|j|z
}|j}|tkDr]dtj td|t"z��z}|j$j'|g�j)|�|dd|fz}|j+�}|j,j'|g�j)t/|j0��|j$j'|g�j)|�|dd|fz}����`|xj2|z
c_|j$j'|g�j)t�y)Nr�r_rg�������?rj�wait:%dr%)�
NON_PACKETrr:r�rGr�r�r+r�r}rIr�r�r��WAIT_THRESHOLD�math�log�
WAIT_SCALErErrr�rFrUr�rH)r�r2rG�prev�cum_durationr�r�r%r��first�total�lastrmr�rn�elapsedr��short_ps                  rY�learnzTrafficModel.learn�sj�����L������m�t�v�v��z�*��%�m�4�� �&�&�(�	&�D�A�q����a� �A�%� �	&��}���!�!�!�$�/�/�E��E��3�;�D�"�
:����Q�����4����2��!8�!8�9��
:�#(�D���Q��"&��,�D���Q���	+�A��2�2�6�:�N�F�F��A�N�N�,�,�L��-�4�6�6�A�:�.�C��
+���5�5�F�?���+�+��,���{�{���^�+�$�����S�5<�z�5I�2K�)L�M�D��K�K�*�*�3��3�:�:�4�@��a�b�'�T�G�+�C��*�*�,���"�"�-�-�g�.0�2�28�&��q�w�w��2H����&�&�s�B�/�6�6�w�?��!�"�g��
�*��#
+�		+�.	
� � �L�0� ������s�B�'�.�.�z�:r[c���i}|jj�D]-\}}dj|�}tt	|��||<�/i}|j
j�D]#\}}tt	d�|D���||<�%|||j|jtd�}|j|d<t|t�rt|d�}tj||d��y)Nr�c3�FK�|]}|rdj|�nd���y�w)r�r$N)r�rs  rYrz$TrafficModel.save.<locals>.<genexpr>�s'����,8�01�=>�D�I�I�a�L�3�,F�,8�s�!)rErFrHrI�versionr'r�r#)�indent)rEr�r��dictrrFrHrI�CURRENT_MODEL_VERSIONrGr�rr��json�dump)r�rHrEr%r�rF�ds       rY�savezTrafficModel.save�s������K�K�%�%�'�	)�D�A�q��	�	�!��A��W�Q�Z�(�F�1�I�	)��
��&�&�,�,�.�	9�D�A�q�#�G�,8�56�,8�%8� 9�M�!��	9�
�*�#'�#;�#;��+�+�,�
���$�$��%���a����Q���A��	�	�!�Q�q�!r[c	�0�t|t�rt|�}tj|�}	|d}|t
krt
d|t
fz��	|dj�D]�\}}tt|�jd��}|jj|g�}|j�D]#\}}|jt|�g|z��%|j���|dj�D]�\}}|jjt|�g�}|j�D]V\}}|dk(r|jdg|z��!|jtt|�jd��g|z��X|j���d	|vr2|d	j�D]\}}|j |xx|z
cc<�|d
|_|d|_y#t$rt
dt
z��wxYw)Nr[z4the model file is version %d; version %d is requiredz=the model file lacks a version number; version %d is requiredrEr�rFr$rpr'rHrI)r�rr�r_�load�REQUIRED_MODEL_VERSION�
ValueError�KeyErrorr�rUr�rEr�extendr�rFrGrHrI)	r�rHrar[r%r�r-rn�counts	         rYrdzTrafficModel.load�s���a����Q��A��I�I�a�L��		;��	�l�G��/�/� �":�")�+A�!B�"C�D�D�0��h�K�%�%�'�	�D�A�q��c�!�f�l�l�4�(�)�A��[�[�+�+�A�r�2�F��G�G�I�
0���5��
�
�s�1�v�h��.�/�
0��K�K�M�	��o�&�,�,�.�	�D�A�q��'�'�2�2�3�q�6�2�>�F��G�G�I�
G���5���8��M�M�2�$��,�/��M�M�5��Q����d�);�#<�"=��"E�F�	
G�

�K�K�M�	��A�:��%����(�
*���1��!�!�!�$��)�$�
*�$%�%:�#;�� ��]�+����5�	;� �":�"8�":�;�;�	;�s�"G9�9Hc���g}tf|jdz
z}|�|dz
}	tj|jj|tf��}|tk(ry||kr	|Stj�|kDr#t
d||fztj��	|Sdtjdd�z}t
d|ztj��||jvr#tj|j|�}	ng}	|jd	d�\}
}|
d
k(rEt|�tj�z}tj|�t|zz}
||
z
}nWtj t"�}tj|�|z}
||
z
}|�||kDr	|S||k\r|j%||
||	f�|dd|fz}|ddddk(r"|d
dddk(rtf|jdz
z}���)zUConstruct an individual conversation packet sequence from the
        model.
        r_Nz"ending after %s (persistence %.1f)rOrKr�ztrying %s instead of endr�r����zwait:rj)rLrr*rerEr�rRrSrT�	randrangerFr�r�rN�exprPr��NO_WAIT_LOG_TIME_RANGEr)r�r}�	hard_stop�replay_speed�
ignore_before�persistencermr�rnr�r�r��
log_wait_timer��log_waits               rY�construct_conversation_sequencez,TrafficModel.construct_conversation_sequences���
���m�t�v�v��z�*��� �%��M�M���
�
�d�k�k�o�o�c�J�=�A�B�A��J���}�,��F��E�=�=�?�[�0��>�#�{�AS�S�"�z�z�+��>��;�� 0� 0��B� 7�7���0�1�4�3�:�:�F��D�&�&�&��
�
�d�&8�&8��&;�<���� �w�w�s�A���H�f��6�!� #�F��f�m�m�o� =�
��x�x�
�.�*�|�2K�L���T�!�	�!�>�>�+A�B���x�x��)�L�8���T�!�	��(�Y��-B�����
�-��H�H�i��6�5�A�B��a�b�'�Q�D�.�C��2�w�r��{�g�%�#�b�'�"�1�+��*@�"�m�t�v�v��z�2��Kr[c�0�|j\}}||z|zSr��rI)r��scale�rate_n�rate_ts    rY�scale_to_packet_ratez!TrafficModel.scale_to_packet_ratePs ���*�*�����v�~��&�&r[c�0�|j\}}||z|zSr�rx)r��ppsrzr{s    rY�packet_rate_to_scalez!TrafficModel.packet_rate_to_scaleTs ���*�*�����f��v�%�%r[c��d|z}t||z�}g}d}||krmtj||�}	|j|	||d|��}
|
D]\}}}
}t	||
�s�n�M|j|
�|t
|
�z
}||kr�m|j|�}td||t
|�||fztj��|j�|S)z<Generate a list of conversation descriptions from the model.�	r)rprqrrrszGwe have %d packets (target %d) in %d conversations at %.1f/s (scale %f)rO)r�r*r�rvr�rr�rrRrSrTr�)r�rIr�rqrs�lead_in�target_packetsr2�	n_packetsr�rmr}r�r�r�rys                rY�generate_conversation_sequencesz,TrafficModel.generate_conversation_sequencesXs
���h�,���[�8�3�4���
��	��.�(��N�N�G�8�X�6�E��4�4�U�?G�BN�CD�AL�	5�N�A�78�
�2�	�8�V�U�1�(�F�C��
��� � ��#���Q���I�%�.�(�(�)�)�+�6��
��(�.�#�m�:L�*�E�3�3��:�:�	�	�����r[)rdr�)r�Nr_rr)r_r)rwrxryr�rXrbrdrvr|rr�rpr[rYrCrC�sH��"�0;�d"�2&,�P9<�26�56�67�45�	4�l'�&�ST�45�%r[rCc�t�g}|D]0}|s�t|dd||f|�}|dz
}|j|��2|S)Nrr_)r�r)r�r�r�r2rrrms      rY�seq_to_conversationsr��sS���M�
�$����Q�q�T�!�W�v�v�&6��:�A��a�K�F�� � ��#�	$�
�r[r'r6�rpc_netlogonr�r�r*rIr-r3r|rr5�epmrrJrKrr:)rJ�0x01zHost Announcement (0x01))rJ�0x02zRequest Announcement (0x02))rJ�0x08zBrowser Election Request (0x08))rJ�0x09zGet Backup List Request (0x09))rJ�0x0cz$Domain/Workgroup Announcement (0x0c))rJ�0x0fz Local Master Announcement (0x0f)r2�
searchRequestrB�
searchResDone)r5r(�Requestr4�BindrC�Bind_ackrE�Bind_nakr7�
Alter_contextrG�Alter_context_respr��AUTH3)r5r1�Responser&r	r;�response)rr(�DsBind)rrD�DsCrackNames)rrF�DsWriteAccountSpn)rr<�DsUnbind)rr1�
DsReplicaSync)rr/�DsGetNCChanges)rr?�DsReplicaUpdateRefs)r�r/�Mapr�r�r,�bindRequestr=�bindResponser0�
unbindRequestr.r>�searchResEntryr@)r-r�z*** Unknown ***)r|r8�lsa_LookupNames)r|rH�lsa_LookupSids)r|�39�lsa_QueryTrustedDomainInfoBySid)r|�40�lsa_SetTrustedDomainInfo)r|�6�lsa_OpenPolicy)r|�76�lsa_LookupSids3)r|�77�lsa_LookupNames4r9)r:r<)r��21�NetrLogonDummyRoutine1)r��26�NetrServerAuthenticate3)r��29�NetrLogonGetDomainInfo)r��30�NetrServerPasswordSet2)r�r��NetrLogonSamLogonEx)r�r��DsrEnumerateDomainTrusts)r��45�NetrLogonSamLogonWithFlags)r�r?�NetrServerReqChallenge)rr(�Connect)rr��GetAliasMembership)r�17�LookupNames)r�18�
LookupRids)r�19�	OpenGroup)rr<�Close)r�25�QueryGroupMember)r�34�OpenUser)r�36�
QueryUserInfo)rr��GetGroupsForUser)rr/�
QuerySecurity)rrA�LookupDomain)r�64�Connect5)rr��EnumDomains)r�7�
OpenDomain)r�8�QueryDomainInfo)r*�0x04zClose (0x04))r*�0x24zLocking AndX (0x24))r*�0x2ezRead AndX (0x2e))r*�0x32z
Trans2 (0x32))r*�0x71zTree Disconnect (0x71)r)zNegotiate Protocol (0x72))r*�0x73zSession Setup AndX (0x73))r*�0x74zLogoff AndX (0x74))r*�0x75zTree Connect AndX (0x75))r*�0xa2zNT Create AndX (0xa2))rIr(�NegotiateProtocol)rIr6�Ioctl)rIr8�Find)rIr��GetInfo)rIr��Break)rIr<�SessionSetup)rIr1�
SessionLogoff)rIr/�TreeConnect�TreeDisconnect�Create�Readz$SAM LOGON request from client (0x12)z3SAM Active Directory Response - user unknown (0x17)�NetShareGetInfo�
NetSrvGetInfo))rIr?)rIrA)rIr�)rIr�)rK�0x12)rK�0x17)rr�)rr�c���|jdd�\}}tj||fd�}tj|d�}||d|||||g}	|	j	|�dj|	�S)Nr�r_r�r�r�)r�r�r�r�rhr�)
rnr}r�r�r�r�r�r�r~r�s
          rY�expand_short_packetr��so���w�w�s�A���H�f�����&�1�2�6�D��"�"�8�T�2�K��{�B��T�8�V�T�J�D��K�K����9�9�T�?�r[c��tjj�tjj�t	j
d�y)z�Signal handler closes standard out and error.

    Triggered by a sigterm, ensures that the log messages are flushed
    to disk and not lost.
    rN)rSrTr��stdoutr��_exit)�signal�frames  rY�flushing_signal_handlerr�	s/���J�J�����J�J�����H�H�Q�Kr[c���|dztjdd�z}tjj	�tj
j	�t
j�}|dk7r|S	tj|�||f}d}	|dd}
t|
|||��}tjtjt�|j||�tjj�t
jd�tj j#|j$d|j&z�}t)|d�}
	tjj�t
jd�|
t_t1j0�|z
}|
|z
}|t2z
}|dkDrt1j4|�|j7||�
�\}}}t9d|z�t9d|z�t9d
|z�tj
j�tjj�t
jB|	�y	#t*$r#}t,j/d|z�Yd	}~��d	}~wwxYw#t:$ryd}	t9dt
j<�fztj
��t?j@tj
�tj
j	�Y��wxYw#tj
j�tjj�t
jB	�wxYw)z8Fork a new process and replay the conversation sequence.��ri�)r�r�zstats-conversation-%dr�r_�stdout closing failed with %sN)r�r�zMaximum lag: %fz
Start lag: %fzMax sleep miss: %fz*EXCEPTION in child PID %d, conversation %srO)"r*�randintrSr�rcrTr��fork�seedr�r��SIGTERMr�rF�stdinr�r�r�r�r�r��IOErrorr��infor�r�r�r�rRr��getpidra�	print_excr�)�csr�r�rE�	client_id�	server_idr��pidr��statusr�rmr�rHr�r�r�r��max_lag�	start_lagr�s                     rY�replay_seq_in_forkrs����t��f�n�n�Q��4�4�D��J�J�����J�J����
�'�'�)�C�
�a�x��
�
*����D���	�*�	����q�E�!�H����I�2�y�I���
�
�f�n�n�&=�>��-�-�g�q�9��	�	����
������7�7�<�<�� 0� 0�2I� !� 1� 1�32�3����3���	=��J�J�����H�H�Q�K���
��i�i�k�E�!���#�g���>�)�
���>��J�J�z�"�-.�-@�-@�u�IP�.A�.R�*���N�
��'�)�*�
�o�	�)�*�
�"�^�3�4�	�
�
�����
�
����
������3�	=��K�K�7�!�;�<�<��	=�� ����
�;�r�y�y�{�A�>N�N��:�:�	����C�J�J�'��
�
������	�
�
�����
�
����
�����sR�5C"J�3I(�BJ�(	J�1J�	J�J�J�A>L�L�L�L�AM/c��tjj�tjj�t	j
�}|dk7r|Stjj�t	jd�	tjj�t	jd�tjj|jd�}t|d�t_	d}tjtj t"�t%|||��}|j'|��tjj�tjj�t	j2|�y#t$r#}tjd|z�Yd}~��d}~wwxYw#t($rXd}t+dt	j,�ztj�	�t/j0tj�Y��wxYw#tjj�tjj�t	j2�wxYw)
Nrr_r�z	stats-dnsr�r�)r�z)EXCEPTION in child PID %d, the DNS hammerrO)rSr�rcrTr�r�rr�rr��warnr�r�r�r�r�r�r�r�r#r�rRrrarr�)	rr�r�r�rr�r�r	�hammers	         rY�dnshammer_in_forkrSs����J�J�����J�J����
�'�'�)�C�
�a�x��
��I�I�O�O���H�H�Q�K�9��
�
����
������w�w�|�|�G�,�,�k�:�H��h��$�C�J�
����
�
�f�n�n�&=�>��8�X�*�E���
�
�g�
�&�	�
�
�����
�
����
������%�9����3�a�7�8�8��9���(���
�:�b�i�i�k�J��:�:�	����C�J�J�'�	(��	�
�
�����
�
����
�����s?�3F�?A
G
�	G�$G�G�
AH+�(H.�*H+�+H.�.AJc
�p�td|||t|�d�|
��}t|�t|�kr"tdt|�t|�fz��tj�t|�dz}tj
�|z}
|�|ddd|z}t
d|ztj��t
d||zztj��t
d	|ztj��|
|zd
z}tjdt|�|fz�|jdt|�td
�|D����i}	|rt||||��}d||<t|�D]#\}}||}|dz}t||
|||�}|||<�%tj
�}t
d||
z
|z||
z
fztj��tj
�|kr�|r�tj d�	tj"dtj$�\}}|rM|j-|d�}t.dkDr*t
d||t|�fztj��|	r|dk7rntj
�|kr|r��|jdt|���dD�]v}t
dt|�|fztj��|D]}	tj6||��tj d�tj
�dz}|r�	tj"dtj$�\}}dk7r�|j-|d�}|�_t
d|z�tjj;�tj<j;�tj>d�t
d||t|�fztj��tj
�|k\rn|r��|sntj d���y|r't
dt|�ztj��	tj@dd�y#t&$r}|j(t*k7r�Yd}~���d}~wwxYw#t0$r3t
dtj��t3j4�Y��?wxYw#t&$r}|j(t8k7r�Yd}~��d}~wwxYw#t&$r}|j(t*k7r�Yd}~���d}~wwxYw#tB$rt
dtj��YywxYw#|jdt|���dD�]�}t
dt|�|fztj��|D]C}	tj6||��#t&$r}|j(t8k7r�Yd}~�=d}~wwxYwtj d�tj
�dz}|�r	tj"dtj$�\}}n*#t&$r}|j(t*k7r�Yd}~nd}~wwxYwdk7r�|j-|d�}|�_t
d|z�tjj;�tj<j;�tj>d�t
d||t|�fztj��tj
�|k\rn|r��|sntj d����|r't
dt|�ztj��	tj@dd�w#tB$rt
dtj��YwwxYwxYw) N)r�r�r�r�z(we have %d accounts but %d conversationsg{�G�z�?rjrzWe will start in %.1f secondsrOzWe will stop after %.1f secondszruntime %.1f secondsr%z6Replaying traffic for %u conversations over %d seconds�
intentionsc3�2K�|]}t|����y�wr�)r�rs  rYrzreplay.<locals>.<genexpr>�s����+M�q�C��F�+M�s�)�Planned_conversations�Planned_packetsr�r_r#z,all forks done in %.1f seconds, waiting %.1fg�~j�t�h?z-process %d finished conversation %d; %d to gozEXCEPTION in parent�
unfinished)�Unfinished_conversations)�rr�zkilling %d children with -%d��?zchildren is %s, no pid foundz)kill -%d %d KILLED conversation; %d to goz%d children are missingzignoring fake ^Crp)"r�r�rfr��setpgrpr�rRrSrTr�rr��sumrr,rr��waitpid�WNOHANG�OSError�errnor�poprQr�rar�killrrcr�r��killpg�KeyboardInterrupt)�conversation_seq�hostr�r��accountsr�dns_query_filer��latency_timeout�stop_on_any_errorr�r��delayr�r`�childrenrr&rrErr�r	r�rmrrs                          rYr#r#us����&�4�"'�!�03�4D�0E�&�%�	&�G��8�}�s�+�,�,��D��x�=�#�.>�*?�@�A�C�	C�
�J�J�L�
� �!�D�(�E��I�I�K�%��E���$�B�'��+�A�.��@��	�
)�E�
1��z�z��	�
+�x�%�/?�
@��z�z��	�
 �8�
+��z�z���(�
�S�
 �C�
�K�K�H�
�!�
"�H�-�.�/�����.1�2B�.C�(+�+M�<L�+M�(M��O��H�Y7��#�H�h��/=�?�C��H�S�M��/�0�	&�E�A�r��q�k�G��A��I�$�R����)�L�C�%�H�S�M�		&�
�I�I�K��
�<��5�y�5� �!�e�)�,�-��:�:�	��i�i�k�C��H��J�J�u��
� �j�j��R�Z�Z�8���V�
��L�L��d�+����?��'���C��M�2�3�:=�*�*�F�%��1����i�i�k�C��H�*	���L�58��]�	�	D��!	�A��1���M�1�%�&�-0�Z�Z�
9��
����G�G�C��O�
�
�J�J�s�O��)�)�+��/�C���"$�*�*�R����"<�K�C���!�8� ���S�$�/�A��y��<�x�G�H��
�
�(�(�*��
�
�(�(�*�������&��s�C��M�2�3� #�z�z�+��9�9�;�#�%��%�(���J�J�q�M�C!	�F��+�c�(�m�;��z�z�
#�	7��I�I�a��O��C�
��7�7�f�$����
����
�#�#�*�*�5����������w�w�%�'��(�������w�w�&�(��)����@!�	7��$�3�:�:�6�	7��c	���L�58��]�	�	D��!	�A��1���M�1�%�&�-0�Z�Z�
9��
����G�G�C��O�����w�w�%�'��(����
�
�J�J�s�O��)�)�+��/�C���"$�*�*�R����"<�K�C������w�w�&�(��)�����!�8� ���S�$�/�A��y��<�x�G�H��
�
�(�(�*��
�
�(�(�*�������&��s�C��M�2�3� #�z�z�+��9�9�;�#�%��%�(���J�J�q�M�C!	�F��+�c�(�m�;��z�z�
#�	7��I�I�a��O�� �	7��$�3�:�:�6�	7�s�B2R�'Q0�.A(R�(S�/'T�T0�0	R�9R�
R�R�R�8S�U�S�U�	T�#S=�=T�	T-�T(�(T-�0$U�U�A^5�,W�^5�	W*�W%� ^5�%W*�*3^5�'Y�^5�	Y-	�Y(	�#^5�(Y-	�-B?^5�.A^5�4^�
^5�$^2�/^5�1^2�2^5c�B�t�}td|z|dg||��}|S)Nr�zmodules:paged_searches)r�r��optionsr�r�)rr)r%r�r�r rs     rY�openLdbr.s1����G�
�K�$�&�$�1�2�!��	�C�
�Jr[c�.�d||j�fzS)z(Generate an ou name from the instance idz#ou=instance-%d,ou=traffic_replay,%s)r)rr�s  rY�ou_namer0s$��0�K�4A�C�M�M�O�4E�E�Er[c�J�t||�}	|j|jdd�ddd��	|j|dd��|S#t$r}|j\}}|dk7r�Yd}~�;d}~wwxYw#t$r }|j\}}|dk7r�Yd}~|Sd}~wwxYw)z�Create an ou, all created user and machine accounts will belong to it.

    This allows all the created resources to be cleaned up easily.
    rr_�organizationalunit)r��objectclass�DN)r0�addr�rrX�rr�r�r�r	r�s      rY�	create_our7s���

��k�	"�B������r�x�x��Q�'��*� 4�6�	7������r� 4�6�	7��I�����f�f�����R�<���������f�f�����R�<���
�I���s.�(A�A9�	A6�A1�1A6�9	B"�B�B"�ConversationAccounts)r:r;r<r=c��g}td|dz�D]9}t||�}t||�}t||||�}|j	|��;|S)z;Generate a series of unique machine and user account names.r_)r�machine_namer+r8r)	rr��number�passwordr&r&r:r<rEs	         rY�generate_replay_accountsr=<sa���H�
�1�f�q�j�
!�!��#�K��3���[�!�,��&�|�X�x�'/�1������ �
!��Or[c���t||�}d|�d|��}dt|�zjd�}|rttt
z�}ntt�}|j|dd|z||d��y)	z"Create a machine account via ldap.r2r�"%s"�	utf-16-le�computerz%s$�r�r3�sAMAccountName�userAccountControl�
unicodePwdN)r0r!�encoderrrrr5)	rr�r:r;�traffic_accountr�r��utf16pw�account_controlss	         rY�create_machine_accountrJJs���
��k�	"�B�#�R�	(�B��
�;�/�/�7�7��D�G���8�6� 7�8���;�<���C�G�G��!��,�.�.��
� r[c��t||�}d|�d|��}dt|�zjd�}|j|d|t	t
�|d��t
j|�}|j|d�y)	zCreate a user account via ldap.r2rr?r@�userrBz(A;;WP;;;PS)N)	r0r!rFr5rrr �SDUtils�dacl_add_ace)rr�r<r=r�rBrH�sdutilss        rY�create_user_accountrPcs|��	��k�	"�B�$�b�)�G��
�8�,�,�4�4�[�A�G��C�G�G���"�!�"3�4��
�����s�#�G�����.�1r[c�X�t||�}d|�d|��}|j|d|d��y)zCreate a group via ldap.r2r�group)r�r3rCN)r0r5)rr�rMr�r�s     rY�create_grouprSus6��
��k�	"�B��R�	 �B��C�G�G����
�r[c��d||fzS)z-Generate a user name based in the instance idz
STGU-%d-%drp�r�r&s  rYr+r+�����;��*�*�*r[c��|jdj|�|g��}|D�chc]}t||���c}Scc}w)z(Search objectclass, return attr in a setz(objectClass={}))rr�)rr
r)rr3�attr�objs�objs     rY�search_objectclassr[�sC���3�:�:�%�,�,�[�9��f��D�'+�+�s�C��D�	�N�+�+��+s�Ac���t|d��}d}t|dd�D]I}t||�}||vs�t||||�|dz
}|dzdk(s�0tjd||fz��K|S)zAdd users to the serverrL�r3rrjr_�2zCreated %u/%u users)r[rr+rPr�r)rr�r;r<�existing_objects�usersr&rMs        rY�generate_usersra�s���)�#�6�B��
�E�
�6�1�b�
!�E����a�(���'�'���[�$��A��Q�J�E��r�z�Q�����1�U�F�O�C�D�
E��Lr[c�"�|rd||fzSd||fzS)z1Generate a machine account name from instance id.z
STGM-%d-%dzPC-%d-%drp)r�r&rGs   rYr:r:�s)����{�A�.�.�.��[�!�,�,�,r[c���t|d��}d}t|dd�D]N}t|||�}|dz|vs�t|||||�|dz
}|dzdk(s�5tjd||fz��P|S)	z"Add machine accounts to the serverrAr]rrjr_r_r^zCreated %u/%u machine accounts)r[rr:rJr�r)	rr�r;r<rGr_�addedr&rMs	         rY�generate_machine_accountsre�s���*�#�:�F��
�E�
�6�1�b�
!�P���K��O�<���#�:�-�-�"�3��T�8�#2�
4��Q�J�E��r�z�Q�����<��v��N�O�P��Lr[c��d||fzS)z'Generate a group name from instance id.z
STGG-%d-%drprUs  rY�
group_namerg�rVr[c���t|d��}d}t|dd�D]H}t||�}||vs�t|||�|dz
}|dzdk(s�/tjd||fz��J|S)z3Create the required number of groups on the server.rRr]rrjr_r�zCreated %u/%u groups)r[rrgrSr�r)rr�r;r_�groupsr&rMs       rY�generate_groupsrj�s���)�#�7�C��
�F�
�6�1�b�
!�G���+�q�)���'�'���k�4�0��a�K�F���}��!����2�f�f�5E�E�F�
G��Mr[c��t||�}	|j|dg�y#t$r}|j\}}|dk7r�Yd}~yd}~wwxYw)z7Remove the created accounts and groups from the server.z
tree_delete:1� N)r0�deleterrXr6s      rY�clean_up_accountsrn�sR��	��k�	"�B����
�
�2��(�)�����f�f�����R�<�����s�#�	A�A�Ac	�<�d}	d}
d}t||�tjd�t||||�}tjd�t	|||||�}|dkDr"tjd�t|||�}
|dkDrWtjd�t
||
||||�}
tjd�t|||
�|
j�}	|
dkDr|dk(r||
k7rtjd�tjd|||
|	fz�y	)
zTGenerate the required users and groups, allocating the users to
       those groups.rzGenerating dummy user accountsz!Generating dummy machine accountszGenerating dummy groupszAssigning users to groupszAdding users to groupsz(The added groups will contain no membersz:Added %d users (%d machines), %d groups and %d membershipsN)
r7r�rrarerj�GroupAssignments�add_users_to_groupsrT�warning)rr�r<�number_of_users�number_of_groups�group_memberships�max_members�machine_accounts�traffic_accounts�memberships_added�groups_added�computers_added�users_added�assignmentss              rY�generate_users_and_groupsr~�s'�����L��O�
�c�;��
�K�K�0�1� ��k�?�H�M�K�
�K�K�3�4�/��[�0@�(�0@�B�O��!�����-�.�&�s�K�9I�J���1�����/�0�&�'7�'3�'6�'2�'8�'2�4��	���,�-��C��k�:�'�-�-�/���q��[�A�-��<�'����A�B�
�K�K�L��o�|�"�$�$�%r[c�N�eZdZd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
y)
rpc��d|_|j|�|j||�||_t	t
�|_|j|||||�y)Nr)ri�generate_group_distribution�generate_user_distributionrvrrr}�
assign_groups)r�rtrzrsr|rurvs       rYr�zGroupAssignments.__init__s[����
��(�(�)9�:��'�'��9J�K�&���&�t�,������+�\�?�&�(9�	;r[c�p�g}t|�}|dk(ryd}|D]}||z
}|j||z��|S)Nrr�)rr)r��weights�distrT�
cumulative�probabilitys      rY�cumulative_distributionz(GroupAssignments.cumulative_distributionsR�����G����A�:���
�"�	,�K��+�%�J��K�K�
�U�*�+�	,��r[c���|dkDrd}n|dkDrd}n
|dkDrd}nd}g}td|dz�D](}tj|�}|j|��*|j	|�|_y	)
zAProbability distribution of a user belonging to a group.
        i@KLg@i��g@i�g@g�?r_N)rr*�
paretovariaterr��	user_dist)r��	num_users�num_memberships�shaper�rsrns       rYr�z+GroupAssignments.generate_user_distribution(s���W�$��E�
�w�
&��E�
�v�
%��E��E����q�)�a�-�(�	�A��$�$�U�+�A��N�N�1��	�
�5�5�g�>��r[c��g}td|dz�D]}d|dzz}|j|��||_|j|�|_y)z6Probability distribution of a group containing a user.r_g�������?N)rr�
group_weightsr��
group_dist)r�rr�rsrns     rYr�z,GroupAssignments.generate_group_distributionBsW��
���q�!�a�%��	�A��Q��V��A��N�N�1��	�
%����6�6�w�?��r[c���tj|jtj��}tj|jtj��}||fS)z2Returns a randomly generated user-group membership)�bisectr�r*r��r�rLrRs   rY�generate_random_membershipz+GroupAssignments.generate_random_membershipPsB���}�}�T�^�^�V�]�]�_�=���
�
�d�o�o�v�}�}��?���U�{�r[c� �|j|Sr�)r})r�rRs  rY�users_in_groupzGroupAssignments.users_in_group\s������&�&r[c�6�|jj�Sr�)r}rr�s rY�
get_groupszGroupAssignments.get_groups_s�����$�$�&�&r[c���t|j|�}||k\rZtjdj	||��d|j
|dz
<|j
|j
�}||_yy)z?Prevent the group's membership from exceeding the max specifiedzGroup {0} has {1} membersrr_N)r�r}r�rr
r�r�r�)r�rRrv�num_members�new_dists     rY�cap_group_membershipz%GroupAssignments.cap_group_membershipbsq���$�*�*�5�1�2���+�%��K�K�3�:�:�5�+�N�O�-.�D���u�q�y�)��3�3�D�4F�4F�G�H�&�D�O�&r[c���||j|vr3|j|j|�|xjdz
c_|jr|j	||j�yy)Nr_)r}rrirvr�r�s   rY�add_assignmentzGroupAssignments.add_assignmentnsc���t�'�'��.�.����U�#�*�*�4�0��J�J�!�O�J�����%�%�e�T�-=�-=�>�r[c��|dkrytjt|�t|�t|�zz�}|jrt	||j|z�}||z
dz
}||z
dz
}|j�|krJ|j
�\}}	|	|kDs||kDr|j|dz|	dz�|j�|kr�Iyy)aAllocate users to groups.

        The intention is to have a few users that belong to most groups, while
        the majority of users belong to a few groups.

        A few groups will contain most users, with the remaining only having a
        few users.
        rNr_)rN�ceilr�rvr*rTr�r�)
r�rtrzrsr|ru�existing_users�existing_groupsrLrRs
          rYr�zGroupAssignments.assign_groupszs�����!��!�I�I��#�$�
�;�
�%��"8�
8�
:�;����� #�$5�$(�$4�$4�7G�$G�!I��*�[�8�A�=��*�\�9�A�=���j�j�l�.�.��9�9�;�K�D�%���&�$��*?��#�#�D�1�H�e�a�i�8�
�j�j�l�.�.r[c��|jSr��rir�s rYrTzGroupAssignments.total�s���z�z�r[N)rwrxryr�r�r�r�r�r�r�r�r�r�rTrpr[rYrprps;��	;��"?�4@�
�'�'�
'�
?�9�Br[rpc�d�|j�}d}d}|j�D]�}|j|�}t|�dk(r�#t	dt|�d�D]N}|||dz}	t||||	�|t|	�z
}|dz
}|dzdk(s�5tjd||fz��P��y)zDTakes the assignments of users to groups and applies them to the DB.rr�r_r^zAdded %u/%u membershipsN)rTr�r�r�r�add_group_membersr�r)
r!r�r}rTrirdrRr��chunk�chunk_of_userss
          rYrqrq�s���
����E�
�E�
�E��'�'�)�H��$�3�3�E�:���~��!�#��
�1�c�.�1�4�8�	H�E�+�E�%�$�,�?�N��b�+�u�n�E��S��(�(�E��Q�J�E��r�z�Q�����5����F�G�	H�Hr[c�j�
�t||��
�
fd�}|t||��}tj�}tj||�|_|D]J}|t
||��}dt|�z}	tj|tjd�||	<�L|j|�y)z(Adds the given users to group specified.c���d|�d���S)Nr2rrp)rMr�s �rY�build_dnz#add_group_members.<locals>.build_dn�s���!�2�&�'r[zmember-�memberN)r0rgr�Message�Dnr�r+r�MessageElement�FLAG_MOD_ADD�modify)r!r�rRr�r��group_dn�mrLrB�idxr�s          @rYr�r��s����
��[�	!�B�(��
�;��6�7�H����
�A��6�6�"�h��A�D��I���9�[�$�7�8���#�d�)�#���#�#�G�S�-=�-=�x�H��#��I�
�I�I�a�Lr[c��tjj}d}d}d}i}t�}t	�}|�
|j
}	nd�}	|	d�dddd�}
dddd�}t
j|�D�]}tjj||�}
t|
d�5}|D]�}	|jd�jd	�}|d
}|d}|d}t|d
�}t|d�}t||z
|�}t||�}||f}|j|g�j!|�|ddk(r|d
z
}n|d
z
}||xxd
z
cc<|j#|�|	|���	ddd���||z
}|dk(rd}n||z}|dk(rd}n||z}t/|�}t+d|z�t+d||fz�t+d||fz�t1|
j3��D](\}}t+d|j5dd�dz|fz��*t1|j3��D](\}}t+d|j5dd�dz|fz��*t+d�i}|D]*\}}||vr
t	�||<||j#|��,t1|j7��} | D]�}t1||t8��}!|!D]�}||f}||}"t1|"�}"t/|"�}#||}t;|"�|#z}$t=|"d�}%t=|"d�}&|"d|"dz
}'|"d}(t>jA|d�})t+d|||)|#||$|%|&|'|(f
z�����y#t$t&f$r�d|vrq|jdd
�\}}||
vrtt|�|
|�|
|<nW||vrtt)|�||�||<n7t+|tj,��nt+|tj,��Y��XwxYw#1swY���xYw) z/Generate and print the summary stats for a run.rNc��yr�rp)rss rY�twzgenerate_stats.<locals>.tw�s��r[z2time	conv	protocol	type	duration	successful	error
)zMaximum lagz	Start lagzMax sleep miss)rrrr#r�r�r_r#rdrr�Truer�rOzTotal conversations:   %10dz-Successful operations: %10d (%.3f per second)z-Failed operations:     %10d (%.3f per second)z%-28s %fr�r^z%-28s %dz�Protocol    Op Code  Description                                Count       Failed         Mean       Median          95%        Range          Max)r�rgffffff�?rjr�z?%-12s   %4s  %-35s %12d %12d %12.6f %12.6f %12.6f %12.6f %12.6f)!rS�
float_infor+rr?�writer��listdirr�r�r�r�r�r�r*rrr5rf�
IndexErrorr�rRrTr��sortedr��replacer�
opcode_keyr�calc_percentiler�r�)*r��timing_filerSrU�
successful�failed�	latencies�failures�unique_conversationsr��float_values�
int_valuesr�r�rHr�r�r�r��packet_type�latencyr��opr%r�r��success_rate�failure_rater2�ops�protor��	protocols�packet_typesr-ri�mean�median�
percentile�rng�maxvr�s*                                          rY�generate_statsr��s������#�#�E��D��J��F��I���H��5����
�
�
��	��B�C�����L�"#��$%��J��J�J�x�(�&5���w�w�|�|�H�h�/��
�$��_�$	5���#
5��"5�#'�;�;�t�#4�#:�#:�4�#@�F�#)�!�9�L�#)�!�9�H�#)�!�9�K�#(����#3�G��f�Q�i�(�A�#&�q�7�{�E�#:�E�#&�q�$�<�D�"�K�0�B��(�(��R�0�7�7��@��a�y�F�*�"�a��
��!��� ����)��(�,�,�\�:��t�H�+#
5�$	5�$	5�&5�P�e�|�H��Q����!�H�,��
��{�����(���,�-�M�	�
'�-�
7�8�	�
9�
��&�'�(�	�
9�
�\�"�#�$��|�)�)�+�,�;���1�
�j�A�I�I�c�3�/�#�5�q�9�9�:�;��z�'�'�)�*�;���1�
�j�A�I�I�c�3�/�#�5�q�9�9�:�;�
�*�+�
�C�"��
��v������C��J��E�
���v����s�x�x�z�"�I�����c�(�m��<��'�	�K��K�(�B�"�2��F����F��V��E�!�"��F��V��u�,�D�(���6�F�(���6�J����f�Q�i�/�C����D�(�,�,�R��4�D��0�� ���������	��
�	���c#�J�/�
5��d�{�#�z�z�#�q�1���1���,�.1�%��(�2>�q�/�/C�L��O��*�_�,/��A��0:�1�
�-?�J�q�M�"�$�S�Z�Z�8��d����4��
5��/$	5�$	5�s2�"P�)CM�-P�BO>	�:P�=O>	�>P�P	c�B�	dt|�zS#t$r|cYSwxYw)zCSort key for the operation code to ensure that it sorts numericallyz%03d)r�rf)r�s rYr�r�M	s)�����A����������s�
��c��|syt|�dz
|z}tj|�}tj|�}||k(r|t	|�S|t	|�||z
z}|t	|�||z
z}||zS)ztCalculate the specified percentile from the list of values.

    Assumes the list is sorted in ascending order.
    rr_)r�rN�floorr�r�)r-r�r%rHrm�d0�d1s       rYr�r�U	s�����	�V��q��J�&�A��
�
�1�
�A��	�	�!��A��A�v��c�!�f�~��	��A���1�q�5�	!�B�	��A���1�q�5�	!�B�
��7�Nr[c��tjj|�}tjd�}tj|�tj|�|S)zuIn a testenv we end up with 0777 directories that look an alarming
    green colour with ls. Use umask to avoid that.�?)r�r�r��umask�mkdir)r�ra�masks   rYr>r>g	s>��	�����d��A�
�8�8�E�?�D��H�H�Q�K��H�H�T�N��Hr[r�)r_r#)r_r�)	NNNNrNNr%F)T)rLrC)yr�r�r*r_rNrSr�rrr�collectionsrrrr�dns.resolverr	r�
samba.emulater
�samba.samdbrrr�samba.dcerpcr
rrrr�samba.dcerpc.netlogonrrr�samba.drs_utilsrra�samba.credentialsrrr�
samba.authr�
samba.dsdbrrrr�samba.dcerpc.miscr�sambarr �samba.commonr!�samba.loggerr"r�r^rer�rLr�r�r�rPrMrorQrwr�rZrfrtr�rv�objectr{r�r�r�r�r�r�r5r:r?rArCr�r�r�r�r�rrr#r.r0r7r8r=rJrPrSr+r[rar:rergrjrnr~rprqr�r�r�r�r>rpr[rY�<module>r�sM��(�	�
���
�
��E�E�+�)��
��)�/�/�!�4���&��O�O�%���+���#�)�
��������
����������
����������	��?��
�
��
�"��"����	�x�	(��6�$
�
�0	�i�	�O<�V�O<�d�,.�$_�F�_�D�&��>d*�6�d*�NKW��KW�\2B�j+�
�
�_�6�_�D��	�4���D�����
�4�	�
�D���D�
��T��
�d���D��
�d��
�4���t���t���D��
�d�� �D�!��&_��3�_��6�_��:�_��9�	_�
�?�_��;�
_��O�_��O�_��Y�_��f�_��j�_��j�_��o�_��*�_��g�_� �Z�!_�"�'�#_�$�*�%_�&�h�'_�(�~�)_�*�*�+_�,�j�-_�.�o�/_�0�&�1_�2�+�3_�4�%�5_�6�b�7_�8�=�9_�:�>�;_�<�?�=_�>�?�?_�@�#�A_�B�?�C_�D�#�E_�F�'�G_�H�&�I_�J�7�K_�L�0�M_�N�%�O_�P�'�Q_�R�(�S_�T�7�U_�V�:�W_�X�4�Y_�Z�5�[_�\�4�]_�^�4�__�`�1�a_�b�6�c_�d�8�e_�f�3�g_�h�I�i_�j�(�k_�l�M�m_�n�L�o_�p�K�q_�r�7�s_�t�&�u_�v�J�w_�x�O�y_�z�&�{_�|�?�}_�~�>�_�@�J�A_�B�=�C_�D�<�E_�F�$�G_�H�^�I_�J�*�K_�L�'�M_�N�_�O_�P�-�Q_�R�0�S_�T�0�U_�V�)�W_�X�/�Y_�Z�,�[_�\�&�]_�^�G�__�`�F�a_�b�I�c_�d�G�e_�f�>�g_�h�?�i_�j�=�k_�l$����D� 5�'�%�}_��D��<�~�F��������"�P7�f�E��:"�"8�#/�0���,0� �22�$	�+�
,��-� /3��"+�
�	�BF�*%�ZQ�v�Q�hH�0�(�D��$
r[

Zerion Mini Shell 1.0