HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/samba/emulate/__pycache__/traffic.cpython-310.pyc
o

�/adS�
@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@dZAdeAZBdZCdZDe8eEd �ZFd!d"�ZGd#d$�ZHd%d&�ZIGd'd(�d(eJ�ZKGd)d*�d*eL�ZMd+d,�ZNd-d.�ZOGd/d0�d0eL�ZPGd1d2�d2eL�ZQGd3d4�d4eL�ZRGd5d6�d6eR�ZS�dCd8d9�ZTd:d;�ZUd<d=�ZVd>d?�ZWGd@dA�dAeL�ZX�dDdCdD�ZYidEdF�dGdH�dIdH�dJdH�dKdH�dLdH�dMdF�dNdH�dOdH�dPdH�dQdH�dRdH�dSdF�dTdF�dUdH�dVdF�ZZidWdX�dYdZ�d[d\�d]d^�d_d`�dadb�dcdd�dedf�dgdh�didj�dkdl�dmdn�dodp�dqdr�dsdt�dudv�dwdx�idydz�d{d|�d}d~�dd��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�d��d�dd�d�d��d�df�d�d���id�d��d�d��d�d��d�d��d�d��d�d��d�d��d�dx�d�dz�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�d�Z\�d�d�Z]�dE�d	�d
�Z^�dF�d�d�Z_								�	
�dG�d�d�Z`�d�d�Za�d�d�Zb�d�d�Zce�d�d�Zd�d�d�Ze�	�dH�d�d�Zf�d�d�Zg�d�d �Zh�d!�d"�Zi�dI�d%�d&�Zj�d'�d(�Zk�dH�d)�d*�Zl�	�dH�d+�d,�Zm�d-�d.�Zn�d/�d0�Zo�d1�d2�Zp�	�dH�d3�d4�ZqG�d5�d6��d6eL�Zr�d7�d8�Zs�d9�d:�Zt�d;�d<�Zu�d=�d>�Zv�d?�d@�Zw�dA�dB�ZxdS(J�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�r4�14��nbnsr')�r&�1�r,r;�r,�4�r,�5�r2r@�r4�12�r4�13�r4�15>r)�smb2�browser�smb_netlogong$@)i���������namecGs<|tkr|st|tjd�dSt|t|�tjd�dSdS)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�args�rX�7/usr/lib/python3/dist-packages/samba/emulate/traffic.py�debuges
�rZcGsftjdd�}td|dd|ddfdtjd�|D]	}t|tjd�qttjd�tj��d	S)
zK Print an unformatted log message to stderr, contaning the line number
    r")�limitz %s:%s r�� )�endrOrNN)�	traceback�
extract_stackrQrRrS�flush)rW�tb�arXrXrY�debug_linenows��rdcsR|r#d}|D]}|d7}||9}|d;}qdd|��fdd�}|Sdd�}|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�cs0tdkr|D]}td�|ftjd�qdSdS)Nrz	%s%srN�rPrQrRrS�rWrc��prefixrXrY�p�s
�zrandom_colour_print.<locals>.pcWs(tdkr|D]}t|tjd�qdSdS)NrrNrhrirXrXrYrl�s
�rX)�seeds�s�xrlrXrjrY�random_colour_print�s

�rpc@seZdZdS)�FakePacketErrorN)�__name__�
__module__�__qualname__rXrXrXrYrq�srqc@steZdZdZdZdd�Zedd��Zddd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zddd�ZdS)�PacketzDetails of a network packet�
�	timestamp�ip_protocol�
stream_number�src�dest�protocol�opcode�desc�extra�	endpointsc

Csf||_||_||_||_||_||_||_||_|	|_|j|jkr*|j|jf|_	dS|j|jf|_	dS�Nrv)
�selfrwrxryrzr{r|r}r~rrXrXrY�__init__�szPacket.__init__c
Csh|�d��d�}|dd�\}}}}}}}	}
|dd�}t|�}t|�}t|�}||||||||	|
|�	S)N�
�	�)�rstrip�split�float�int)�cls�line�fieldsrwrxryrzr{r|r}r~rrXrXrY�	from_line�s"
��zPacket.from_line�cCsFd�|j�}|j|}|d||j|jpd|j|j|j|j|j	|f	fS)z5Format the packet as a traffic_summary line.
        r�z%f	%s	%s	%d	%d	%s	%s	%s	%s�)
�joinrrwrxryrzr{r|r}r~)r��time_offsetr�trXrXrY�
as_summary�s
��zPacket.as_summaryc
CsLd|j|j|j|jpd|j|j|j|j|jr"dd�	|j�df	Sdf	S)Nz:%.3f: %d -> %d; ip %s; strm %s; prot %s; op %s; desc %s %sr#�«r]�»r�)
rwrzr{rxryr|r}r~rr��r�rXrXrY�__str__�s����zPacket.__str__cCsd|S)Nz<Packet @%s>rXr�rXrXrY�__repr__�szPacket.__repr__cCs,|�|j|j|j|j|j|j|j|j|j	�	Sr�)
�	__class__rwrxryrzr{r|r}r~rr�rXrXrY�copy�s�zPacket.copycCsd|j|jf}|S)Nz%s:%s�r|r})r�r�rXrXrY�as_packet_type�szPacket.as_packet_typecCs2|j|jf}|tvrt|S|tvrt|SdS)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|r}�CLIENT_CLUES�SERVER_CLUES)r��keyrXrXrY�client_score�s
zPacket.client_scorec	Csd|j|jf}ztt|�}Wnty.}ztd|j|ftjd�WYd}~dSd}~ww|jdkr>t	dd|j|f�t
�
�}z"||||�rbt
�
�}||}td||j|j|j|f�WdSWdSty�}zt
�
�}||}td	||j|j|j||f�WYd}~dSd}~ww)
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_%s_%sz#Conversation(%s) Missing handler %srNN�kerberosr"z#Conversation(%s) Calling handler %sz%f	%s	%s	%s	%f	True	z%f	%s	%s	%s	%f	False	%s)r|r}�getattrr	�AttributeErrorrQ�conversation_idrRrSrZ�time�	Exception)	r��conversation�context�fn_name�fn�e�startr^�durationrXrXrY�playsJ����
�
���
����zPacket.playcCs|j|jSr��rw�r��otherrXrXrY�__cmp__+�zPacket.__cmp__NcCst|j|j�Sr�)�is_a_real_packetr|r})r��missing_packet_statsrXrXrY�is_really_a_packet.szPacket.is_really_a_packet)r�r�)rrrsrt�__doc__�	__slots__r��classmethodr�r�r�r�r�r�r�r�r�r�rXrXrXrYru�s


(rucCsj|tvrdS|dkr|dkrdSd||f}tt|d�}|dur,tjd|tjd�dS|tjur3dSdS)	zdIs the packet one that can be ignored?

    If so removing it will have no effect on the replay
    Fr,r�r�Nzmissing packet %srNT)�SKIPPED_PROTOCOLSr�r	�LOGGERrZrRrS�null_packet)r|r}r�r�rXrXrYr�2s
r�cCs&|dkrdS||fdvrdSt||�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�r/rF�r4�16)r�r�rXrXrY�is_a_traffic_generating_packetHs

r�c
@s�eZdZdZddddddddddej�d�ddf
dd�Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
d+dd�Zd,dd�Zd,dd�Zd,dd�Zd,dd�Zd-dd�Zd-dd �Zd,d!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�ZdS).�
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�DOMAINcCs�||_d|_||_||_|rt|_nt|_|	|_|
|_||_	||_
||_||_|�
d�|_|
|_||_d|_d|_d|_d|_d|_d|_d|_d|_||_|��dS)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�rXrXrYr�as4zReplayContext.__init__cCs
t�}td|j||j|jd�}|j|��tjdgdgd�}i}dgi}|D]-}t	|j
�}d�dd	�|�d�D���
�}|�|g�}	|	�|�|�d
�rS|d�|�q&t|���D]L}
|
dd�dkreqZ|
dd�}|dd�dkr�|dd�}|dd�dksstd
�D] }|d7}||
kr�||vr�td|
|ftjd�q�||
||<q�qZ||_||_i|_|j|��tjdgdd�}d�dd	�|D��}
d�|
�|jd<d}dD]}|d�||���7}q�d�|�|jd<d|jd<|jdtjdgd�}d�|dd�|jd<dS) N�	ldap://%s)�url�session_info�credentialsr�zpaged_results:1:1000�dn)�scope�controls�attrs�invocationId�,css �|]}|��dd�VqdS)Nr")�lstrip��.0rorXrXrY�	<genexpr>�s�z<ReplayContext.generate_ldap_search_tables.<locals>.<genexpr>zCN=NTDS Settings,rKz,DC�zdn_map collison %s %srNz"(objectclass=groupPolicyContainer))r�r��
expressionr�css�|]
}d�|d�VqdS)z(distinguishedName={0})r�N)�format)r�rVrXrXrYr��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)rr
r�r�r��search�	domain_dn�ldb�
SCOPE_SUBTREE�strr�r�r��upper�
setdefault�append�
startswith�list�keys�rangerQrRrS�dn_map�attribute_clue_map�search_filtersr��
SCOPE_BASE)r��session�db�resrr�rr��patternr&�krl�i�
gpos_by_dn�ou_strr�rXrXrYr��sn
�
��


�������z)ReplayContext.generate_ldap_search_tablescCsT|j��D]
}||vr|j|Sq|dkr(t��|j}t|j|�}d|SdS)NzDC,DCz((&(sAMAccountName=%s)(objectClass=user))z(objectClass=*))rr�randomr��	user_namer�)r�r��dn_sigr�r��random_user_id�account_namerXrXrY�guess_search_filter�s�z!ReplayContext.guess_search_filtercCs�g|_g|_g|_g|_g|_g|_g|_|j|_|j|_|j	|_	|j
|_
t|jd|j
�|_|j�d|j�|j�d|j�|j�d|j�|j�dd�d|_d|j|jf|_d|j	|jf|_|��|��dS)	Nzconversation-%dzprivate dirzlock dirzstate directoryztls verify peer�no_checkz/root/ncalrpc_as_system�cn=%s,%s)�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�rXrXrY�generate_process_local_config�s:��
�
�z+ReplayContext.generate_process_local_configcCsR|s!|jrt��|jkrz||�Wn	tyYnwd}nd}||�}||fS)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�rr�)r��f�good�bad�failed_last_time�resultrXrXrY�with_random_bad_credentialss�z)ReplayContext.with_random_bad_credentialscCs�t�|_|j�|j�|j�|j�|j�|j�|j�|j	�|j�
|j�|j�|j
�t�|_|j�|j�|j�|j�|j�|jdd��|j�|j	�|j�|j
�t�|_|j�|j�|j�|j�|j�|j�|j�|j	�|j�|j��tjB�|j�|j
�|j�|j�t�|_|j�|j�|j�|j�|j�|jdd��|j�|j	�|j�|j��tjB�|j�|j
�|j�|j�dS)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_dnr'�simple_bind_creds_badr�rXrXrYr)1sF	���z!ReplayContext.generate_user_credscCs�t�|_|j�|j�|j�|j�|j�t�|j�|j	�|j�
|jd�|j�|j�|j�
|j�t�|_|j�|j�|j�|j�|j�t�|j�|j	dd��|j�
|jd�|j�
|j�dS)z�Generate the conversation specific machine Credentials.

        Each Conversation has an associated machine account.

        Generates machine credentials with good and bad passwords.
        �$Nr2)r�
machine_credsr4r�r5r�set_secure_channel_typerr6r r7r8r�r9r��machine_creds_badr�rXrXrYr(_sz$ReplayContext.generate_machine_credscCsV|j�|�}|r
t�|�S|��}|r(||jvr t�|j|�S|dd�}|s|jS)N�)rr�r�choicer�rr�)r�r�
attributes�	attr_cluerXrXrY�get_matching_dnxs	

�zReplayContext.get_matching_dnFcCs@d}|jr|s|jdStd|j|df|j�}|j�|�|S)Nz$12345678-1234-abcd-ef00-01234567cffb���zncacn_ip_tcp:%sr\)rrr�r�r�)r��new�guid�crXrXrY�get_dcerpc_connection�s



�z#ReplayContext.get_dcerpc_connectionc�L�jr
|s
�jdS�fdd�}��|�j�j�j�\}�_�j�|�|S)NrJc�t�d�j�j|�S�Nzncacn_np:%s)rr�r��r�r�rXrY�connect���z4ReplayContext.get_srvsvc_connection.<locals>.connect)rr1r3r:r�r��r�rKrSrMrXr�rY�get_srvsvc_connection��

��z#ReplayContext.get_srvsvc_connectioncrO)NrJcsd}t�d�j|f�j|�S)Nzschannel,seal,sign�ncacn_ip_tcp:%s[%s]�r�lsarpcr�r�)r��binding_optionsr�rXrYrS�s��z4ReplayContext.get_lsarpc_connection.<locals>.connect)rr1rBrDr�r�rUrXr�rY�get_lsarpc_connection�s

��z#ReplayContext.get_lsarpc_connectioncrO)NrJcst�d�j�j|�SrQrYrRr�rXrYrS�rTz?ReplayContext.get_lsarpc_named_pipe_connection.<locals>.connect)rr1rBrDr�r�rUrXr�rY� get_lsarpc_named_pipe_connection�rWz.ReplayContext.get_lsarpc_named_pipe_connectioncsd�jr|s�jd}|S�fdd�}��|�j�j�j�\}�_t|�\}}||f}�j�|�|S)zget a (drs, drs_handle) tuplerJcs"d}d�j|f}t�|�j|�S)N�sealrX)r�rr�)r�r[�binding_stringr�rXrYrS�s
�z:ReplayContext.get_drsuapi_connection_pair.<locals>.connect)rr1r3r:r�rr�)r�rK�unbindrMrS�drs�
drs_handle�supported_extensionsrXr�rY�get_drsuapi_connection_pair�s

��z)ReplayContext.get_drsuapi_connection_paircsz�jr
|s
�jdS�fdd�}�fdd�}|r'��|�j�j�j�\}�_n��|�j�j�j�\}�_�j�|�|S)NrJc�td�j|�jd�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��r
r�r�rRr�rXrY�simple_bind�s

�z6ReplayContext.get_ldap_connection.<locals>.simple_bindcre)Nr�rfrgrRr�rXrY�	sasl_bind�s
�z4ReplayContext.get_ldap_connection.<locals>.sasl_bind)	rr1r;r@r�r3r:r�r�)r�rK�simplerhri�samdbrXr�rY�get_ldap_connection�s&

�
���z!ReplayContext.get_ldap_connectioncCs0|jr|r|j�t|j|j|jd��|jdS)N)r�r�rJ)rr��SamrContextr�r�r�)r�rKrXrXrY�get_samr_contexts

�
zReplayContext.get_samr_contextcs>�jr�jS�fdd�}��|�j�j�j�\}�_|�_|S)NcrP)Nzncacn_ip_tcp:%s[schannel,seal])rr�r�rRr�rXrYrSs��z6ReplayContext.get_netlogon_connection.<locals>.connect)r�r1rBrDr�)r�rSrMrXr�rY�get_netlogon_connections��z%ReplayContext.get_netlogon_connectioncCs
|jdfS)N�A�r�r�rXrXrY�guess_a_dns_lookup$�
z ReplayContext.guess_a_dns_lookupcCs>|j��}t�}dd�|dD�|j_|d|_t�}||fS)NcSs"g|]
}t|t�r|nt|��qSrX)�
isinstancer��ordr�rXrXrY�
<listcomp>*s�z3ReplayContext.get_authenticator.<locals>.<listcomp>�
credentialrw)rB�new_client_authenticatorr�cred�datarw)r��auth�current�
subsequentrXrXrY�get_authenticator's

�
zReplayContext.get_authenticatorcKsLtj�|j|�}t|d�}|��D]\}}td||f|d�q|��dS)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%s: %srNN)�os�pathr�r��open�itemsrQ�close)r��filename�kwargsr,r�vrXrXrY�write_stats1s

zReplayContext.write_statsr�)F)FF)rrrsrtr�r��environr�r�r�rr+r1r)r(rIrNrVr\r]rdrlrnrorrr~r�rXrXrXrYr�ZsD

�,W.


	




&
r�c@s*eZdZdZd	dd�Zdd�Zdd�ZdS)
rmz5State/Context associated with a samr connection.
    NcCs@d|_d|_d|_d|_d|_d|_d|_||_||_||_	dSr�)
�
connection�handle�
domain_handler��group_handle�user_handle�ridsr�r�r�)r�r�r�r�rXrXrYr�?s
zSamrContext.__init__cCs(|jstjd|j|j|jd�|_|jS)Nzncacn_ip_tcp:%s[seal])�lp_ctxr�)r�rr�r�r�r�rXrXrY�get_connectionKs�zSamrContext.get_connectioncCs$|js|��}|�dtj�|_|jSr�)r�r��Connect2r
�SEC_FLAG_MAXIMUM_ALLOWED)r�rMrXrXrY�
get_handleTszSamrContext.get_handle�NN)rrrsrtr�r�r�r�rXrXrXrYrm<s

	rmc@s�eZdZdZ		ddd�Zdd�Zdd	�Z	
d dd�Zd
d�ZeZ	dd�Z
dd�Zdd�Zdd�Z
d!dd�Zd"dd�Zdd�Zdd�ZdS)#�ConversationzADetails of a converation between a simulated client and a server.NrXcCs@||_||_g|_t|�|_d|_||_|D]}|j|�qdS)Nr�)�
start_timer��packetsrprV�client_balancer��add_short_packet)r�r�r��seqr�rlrXrXrYr�]s
�zConversation.__init__cCs6|jdur|jdurdSdS|jdurdS|j|jS)NrrJr\)r�r�rXrXrYr�hs


zConversation.__cmp__cCs�|��}|jdur
|j|_|jdur|j|_|j|jkr&td|j|jf��|j|j8_|j|jdkr@|j|��8_n	|j|��7_|��rU|j	�
|�dSdS)zmAdd a packet object to this conversation, making a local copy with
        a conversation-relative timestamp.Nz8Conversation endpoints %s don't matchpacket endpoints %sr)r�r�rwr�rqrzr�r�r�r�r�)r��packetrlrXrXrY�
add_packetqs 


��zConversation.add_packetTc

Cs�|r	t||�s	dS|��\}}|s||}}||f}	t�|	d�}
t�|d�}t||j|d|||||
|�	}|j|jdkrG|j	|�
�8_	n	|j	|�
�7_	|��r\|j�
|�dSdS)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_PROTOCOLSrur�rzr�r�r�r�r�r�)
r�rwr|r}r�client�skip_unused_packetsrzr{r�r~rxr�rXrXrYr��s$
��zConversation.add_short_packetcCsd|j|j|jt|j�fS)Nz-<Conversation %s %s starting %.3f %d packets>)r�r�r��lenr�r�rXrXrYr��s
��zConversation.__str__cC�
t|j�Sr�)�iterr�r�rXrXrY�__iter__�rszConversation.__iter__cCr�r�)r�r�r�rXrXrY�__len__�rszConversation.__len__cCs*t|j�dkr	dS|jdj|jdjS)Nr"rrJ)r�r�rwr�rXrXrY�get_duration�szConversation.get_durationcs�fdd��jD�S)Ncsg|]}|��j��qSrX)r�r��r�rlr�rXrYrv��z8Conversation.replay_as_summary_lines.<locals>.<listcomp>)r�r�rXr�rY�replay_as_summary_lines�sz$Conversation.replay_as_summary_linesc
Cs�|j}t��|}||}|t}|dkrt�|�t��||}|�d||f�d}	d}
t��}|jD]=}t��|}||j}||	krI|}	|dkrm|t}|dkrmt�|�t��|}||j|
krm||j}
|�||�q6|	||
fS)zMReplay the conversation at the right time.
        (We're already in a fork).rzstarting %s [miss %.3f]r�)r�r��SLEEP_OVERHEAD�sleeprVr�rwr�)
r�r�r�r*r��now�gap�
sleep_time�miss�max_gap�max_sleep_miss�p_startrlrXrXrY�replay_with_delay�s2






zConversation.replay_with_delaycCs>|j\}}|jdkr||fS|jdkr||kr||fS||fS)zhHave a go at deciding who is the server and who is the client.
        returns (client, server)
        r)r�r�)r��server_cluerc�brXrXrYr��s

z Conversation.guess_client_servercs:��fdd�|jD�|_|jr|jdj|_dSd|_dS)z�Prune any packets outside the timne window we're interested in

        :param s: start of the window
        :param e: end of the window
        cs*g|]}�|jkr�krnn|�qSrXr�r��r�rnrXrYrv�s*z>Conversation.forget_packets_outside_window.<locals>.<listcomp>rN�r�rwr�)r�rnr�rXr�rY�forget_packets_outside_window�s"z*Conversation.forget_packets_outside_windowcCs:|jD]	}|j|8_q|jdur|j|8_dSdS)z=Adjust the packet start times relative to the new start time.Nr�)r�r�rlrXrXrY�renormalise_times�s


�zConversation.renormalise_times)NNrXN)TTr�r�)rrrsrtr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rXrXrXrYr�[s&
�	
�

#	r�c@s6eZdZdZddd�Zdd�Zddd�Zdd	d
�ZdS)�	DnsHammerzOA lightweight conversation that generates a lot of dns:0 packets on
    the flyNcsRt|��}�fdd�t|�D�|_|j��||_�|_d|_|j|d�|_dS)Ncsg|]}t�d���qS)r)r�uniform)r�r
�r�rXrYrvr�z&DnsHammer.__init__.<locals>.<listcomp>r��
query_file)	r�r�times�sort�rater�r��_get_query_choices�
query_choices)r��dns_rater�r��nrXr�rYr�s
zDnsHammer.__init__cCsdt|j�|j|jfS)Nz-<DnsHammer %d packets over %.1fs (rate %.2f)>)r�r�r�r�r�rXrXrYr�s�zDnsHammer.__str__cCs�|rCt|d��}|��}Wd�n1swYg}|��D]}|��}|r@|�d�s@|�d�}t|�dks;J�|�|�q!|Sgd�S)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}rp�yes)r\r��NSr�)r"�	*.{realm}rp�no)rEr�r�r�)�
�_msdcs.{realm}rpr�)�r�r�r�)��nx.realm.comrpr�)�r�r�r�)��*.nx.realm.comrpr�)�r�r�r�)r��read�
splitlines�stripr�r�r�r�)r�r�r,�text�choicesr�rWrXrXrYr�s
�

�zDnsHammer._get_query_choicesc
Cs|sJ�|js	J�t��}|jD]t}t��|}||}|t}|dkr)t�|�t�|j�\}}}	}
|j|jd�}d}t��}z1zt	||	�}
|
dkrQt
|
�sQd}Wnty]d}YnwWt��}||}td||||f�qt��}||}td||||f�wdS)NrrqTr�Fz%f	DNS	dns	%s	%f	%s	)
r�r�r�r�r�rrFr�r��	dns_queryr�r�rQ)r�r�r�r�r�r�r�r}�rname�rtype�exist�success�packet_start�answersr^r�rXrXrY�replay4s:



�����zDnsHammer.replayr�)rrrsrtr�r�r�r�r�rXrXrXrYr�s
	
!r��countcCsptt�}g}|D]<}t|t�rt|�}td|jftjd�|D]}t	�
|�}|jdkr:|dkr:||jd7<q |�
|�q |��q|sKgdfStdd�|D��}td	d�|D��}td
tjd�t�}	t|�D]&\}
}|j|8_|	�|j�}|dur�t|
dd
�}||	|j<|�|�qkg}|	��D]
}t|�dkr�|�
|�q�t||�}
t|	�|
}|||
|fS)zLLoad a summary traffic summary file and generated Converations from it.
    zIngesting %srNr&�includer\rcs��|]}|jVqdSr�r�r�rXrXrYr�f��z#ingest_summaries.<locals>.<genexpr>csr�r�r�r�rXrXrYr�gr�z$gathering packets into conversationsNr")r�)rr�rtr�r�rQrMrRrSrur�r|r}r�r��min�maxr�	enumeraterwr�r�r�r��valuesr�r�)�files�dns_mode�
dns_countsr�r,r�rlr��last_packet�
conversationsr
rM�conversation_listr��
mean_intervalrXrXrY�ingest_summariesPsB




�r�cCs2t�}|D]}|�|j�q|r|�d�dSdS)Nr\r)r�updater��most_common)r��	addressesrMrXrXrY�guess_server_address�s�r�cCs,i}|��D]
\}}d�|�}|||<q|S�Nr�)r�r�)ro�yrr��k2rXrXrY�stringify_keys�s


r�cCs4i}|��D]\}}tt|��d��}|||<q|Sr�)r�rTr�r�)ror�rr�r�rXrXrY�unstringify_keys�s

r�c@sdeZdZddd�Zifdd�Zdd�Zdd	�Z	
			
	
ddd�Zdd�Zdd�Z			
ddd�Z
dS)�TrafficModelrEcCs0i|_i|_||_tt�|_d|_ddg|_dS)Nr�rr\)�ngrams�
query_detailsr�rr��dns_opcounts�cumulative_duration�packet_rate)r�r�rXrXrYr��s
zTrafficModel.__init__c	Cs�d}d}tf|jd}t|�}|��D]
\}}|j||7<qt|�dkrS|dj}	d}
|	d}|D]}|
t|�7}
t||jdj	�}q5|
|j
d<||	|j
d<|D]q}|�|�\}
}||��7}tf|jd}|D]W}|j
|
krvqn|j	|}|j	}|tkr�dt�td|t��}|j�|g��|�|dd�|f}|��}|j�|g��t|j��|j�|g��|�|dd�|f}qnqU|j|7_|j�|g��t�dS)Nr�r\rg�������?rJ�wait:%dr$)�
NON_PACKETr�r�r�rr�r�r�r�rwrr�r�rz�WAIT_THRESHOLD�math�log�
WAIT_SCALErr�r�r�rrTrr)r�r�r�prev�cum_durationr�r�rr��first�total�lastrMr�rl�elapsedr��short_prXrXrY�learn�sP




���zTrafficModel.learncCs�i}|j��D]\}}d�|�}tt|��||<qi}|j��D]\}}ttdd�|D���||<q |||j|jtd�}|j	|d<t
|t�rKt|d�}t
j||dd�dS)	Nr�css"�|]}|rd�|�ndVqdS)r�r#N)r�r�rXrXrYr��s��z$TrafficModel.save.<locals>.<genexpr>)rrrr�versionr&rr")�indent)rr�r��dictrrrr�CURRENT_MODEL_VERSIONrrtr�r��json�dump)r�r,rrr�r�drXrXrY�save�s&

��


zTrafficModel.savec	Cs�t|t�r	t|�}t�|�}z|d}|tkrtd|tf��Wn
ty-tdt��w|d��D]+\}}t	t|��
d��}|j�|g�}|��D]\}}|�
t|�g|�qL|��q4|d��D]6\}}|j�t|�g�}|��D] \}}|dkr�|�
dg|�qw|�
t	t|��
d��g|�qw|��qfd	|vr�|d	��D]
\}}|j||7<q�|d
|_|d|_dS)Nrz4the model file is version %d; version %d is requiredz=the model file lacks a version number; version %d is requiredrr�rr#rXr&rr)rtr�r�r�load�REQUIRED_MODEL_VERSION�
ValueError�KeyErrorr�rTr�rr��extendr�rrrr)	r�r,rrrr�r�rlr�rXrXrYr�sB

����
 

zTrafficModel.loadr�Nr\rcCs�g}tf|jd}|dur|d}	t�|j�|tf��}|tkrN||kr)	|St��|kr=td||ftjd�	|Sdt�	dd�}td	|tjd�||j
vr\t�|j
|�}	ng}	|�d
d�\}
}|
dkr�t|�t��}t
�|�t|}
||
7}n(tjt�}t
�|�|}
||
7}|dur�||kr�	|S||kr�|�||
||	f�|dd�|f}|ddd�d
kr�|ddd�d
kr�tf|jd}q)zUConstruct an individual conversation packet sequence from the
        model.
        r\NTz"ending after %s (persistence %.1f)rNrr��ztrying %s instead of end�:r����zwait:rJ)rr�rrFrr�rQrRrS�	randrangerr�r�r�expr
r��NO_WAIT_LOG_TIME_RANGEr�)r�rw�	hard_stop�replay_speed�
ignore_before�persistencerMr�rlrr|r}�
log_wait_timer��log_waitrXrXrY�construct_conversation_sequencesL#���


�(�z,TrafficModel.construct_conversation_sequencecCs|j\}}|||Sr��r)r��scale�rate_n�rate_trXrXrY�scale_to_packet_rateN�
z!TrafficModel.scale_to_packet_ratecCs|j\}}|||Sr�r-)r��ppsr/r0rXrXrY�packet_rate_to_scaleRr2z!TrafficModel.packet_rate_to_scalecCs�d|}t||�}g}d}||krCt�||�}	|j|	||d|d�}
|
D]
\}}}
}t||
�r2nq%q|�|
�|t|
�7}||ks|�|�}td||t|�||ft	j
d�|��|S)z<Generate a list of conversation descriptions from the model.�	r)r&r'r(r)zGwe have %d packets (target %d) in %d conversations at %.1f/s (scale %f)rN)r�rr�r,r�r�r�r4rQrRrSr�)r�rr�r'r)�lead_in�target_packetsr��	n_packetsr�rMrwr|r}rr.rXrXrY�generate_conversation_sequencesVs<�	
�
�

���z,TrafficModel.generate_conversation_sequences)rE)r�Nr\rr)r\r)rrrsrtr�rrrr,r1r4r9rXrXrXrYr��s 
0(
�6�r�r\cCs@g}|D]}|rt|dd||f|�}|d7}|�|�q|S)Nrr\)r�r�)r�r�r�r�rnrMrXrXrY�seq_to_conversations~s
�r:r&r5�rpc_netlogonr�r�r)rHr,r2rZrr4�epmrrIrJrr9)rI�0x01zHost Announcement (0x01))rI�0x02zRequest Announcement (0x02))rI�0x08zBrowser Election Request (0x08))rI�0x09zGet Backup List Request (0x09))rI�0x0cz$Domain/Workgroup Announcement (0x0c))rI�0x0fz Local Master Announcement (0x0f)r1�
searchRequestrA�
searchResDone)r4r'�Requestr3�BindrB�Bind_ackrD�Bind_nakr6�
Alter_contextrF�Alter_context_respr��AUTH3)r4r0�Responser%rr:�response)rr'�DsBind)rrC�DsCrackNames)rrE�DsWriteAccountSpn)rr;�DsUnbind)rr0�
DsReplicaSync)rr.�DsGetNCChanges)rr>�DsReplicaUpdateRefs)r<r.�Mapr�r�r+�bindRequestr<�bindResponser/�
unbindRequestr-r=�searchResEntryr?)r,r�z*** Unknown ***)rZr7�lsa_LookupNames)rZrG�lsa_LookupSids)rZ�39�lsa_QueryTrustedDomainInfoBySid)rZ�40�lsa_SetTrustedDomainInfo)rZ�6�lsa_OpenPolicy)rZ�76�lsa_LookupSids3)rZ�77�lsa_LookupNames4r8)r9r;)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)rr@�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))rHr'�NegotiateProtocol)rHr5�Ioctl)rHr7�Find)rHr��GetInfo)rHrw�Break)rHr;�SessionSetup)rHr0�
SessionLogoff)rHr.�TreeConnect�TreeDisconnect�Create�Readz$SAM LOGON request from client (0x12)z3SAM Active Directory Response - user unknown (0x17)�NetShareGetInfo�
NetSrvGetInfo))rHr>)rHr@)rHr`)rHr�)rJ�0x12)rJ�0x17)rr�)rrfc
CsT|�dd�\}}t�||fd�}t�|d�}||d|||||g}	|	�|�d�|	�S)Nr!r\r�r�r�)r�r�r�r�rr�)
rlrwrzr{rr|r}r~rxr�rXrXrY�expand_short_packet�s

r�cCs"tj��tj��t�d�dS)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)rRrSr��stdoutr��_exit)�signal�framerXrXrY�flushing_signal_handlers

r�c
Cs*|dt�dd�}tj��tj��t��}|dkr|Sz�z�t�|�||f}d}	|dd}
t	|
|||d�}t
�
t
jt�|�
||�tj��t�d�tj�|jd|j�}t|d�}
ztj��t�d�Wnty�}z
t�d|�WYd	}~nd	}~ww|
t_t��|}|
|}|t}|dkr�t�|�|j||d
�\}}}td|�td|�td
|�Wn#ty�d}	tdt��|ftjd�t� tj�tj��YnwWtj��tj��t�!|	�d	SWtj��tj��t�!|	�d	Stj��tj��t�!|	�w)z8Fork a new process and replay the conversation sequence.��ri�)r�r�zstats-conversation-%drr\�stdout closing failed with %sN)r�r�zMaximum lag: %fz
Start lag: %fzMax sleep miss: %fz*EXCEPTION in child PID %d, conversation %srN)"r�randintrRr�rarSr��fork�seedr�r��SIGTERMr�r+�stdinr�r�r�r�r�r��IOErrorr��infor�r�r�r�rQr��getpidr_�	print_excr�)�csr�r�r*�	client_id�	server_idr��pidr��statusr�rMr�r,r�r�r�r��max_lag�	start_lagr�rXrXrY�replay_seq_in_forksv




�

��
����
	
�


�
r�c	
Csxtj��tj��t��}|dkr|Stj��t�d�ztj��t�d�WntyC}z
t	�
d|�WYd}~nd}~wwtj�|j
d�}t|d�t_zYzd}t�tjt�t|||d�}|j|d�Wnty�d}tdt��tjd	�t�tj�YnwWtj��tj��t�|�dSWtj��tj��t�|�dStj��tj��t�|�w)
Nrr\r�z	stats-dnsrr�)r�z)EXCEPTION in child PID %d, the DNS hammerrN)rRr�rarSr�r�r�r�r�r��warnr�r�r�r�r�r�r�r�r�r�rQr�r_r�r�)	r�r�r�r�r�r�r�r��hammerrXrXrY�dnshammer_in_forkRsP




�����

�


�
r�Fc
Ks"td |||t|�d�|
��}t|�t|�kr"tdt|�t|�f��t��t|�d}t��|}
|dur@|ddd|}td|tjd�td||tjd�td	|tjd�|
|d
}t	�
dt|�|f�|jdt|�td
d�|D��d�i}�z�z�|r�t
||||d�}d||<t|�D]\}}||}|d}t||
|||�}|||<q�t��}td||
|||
ftjd�t��|k�r%|�r%t�d�zt�dtj�\}}Wnty�}z|jtkr�WYd}~n3d}~ww|�r|�|d�}tdk�rtd||t|�ftjd�|	�r|dk�rn	t��|k�r%|s�Wnt�y;tdtjd�t��YnwW|jdt|�d�dD]�}tdt|�|ftjd�|D]&}z	t�||�W�qYt�y}z|jtk�rt�WYd}~�qYd}~wwt�d�t��d}|�r�zt�dtj�\}}Wnt�y�}z
|jtk�r��WYd}~nd}~ww|dk�r�|�|d�}|du�r�td|�tj��tj��t�d�td||t|�ftjd�t��|k�r�n|�s�|�s�nt�d��qH|�rtdt|�tjd�z	t� dd�WdSt!�y'tdtjd�YdSw|jdt|�d�dD]�}tdt|�|ftjd�|D]&}z	t�||�W�qDt�yj}z|jtk�r_�WYd}~�qDd}~wwt�d�t��d}|�r�zt�dtj�\}}Wnt�y�}z
|jtk�r��WYd}~nd}~ww|dk�r�|�|d�}|du�r�td|�tj��tj��t�d�td||t|�ftjd�t��|k�r�n|�sy|�s�nt�d��q3|�r�tdt|�tjd�zt� dd�Wwt!�ytdtjd�Yww)!N)r�r�r�r�z(we have %d accounts but %d conversationsg{�G�z�?rJrzWe will start in %.1f secondsrNzWe will stop after %.1f secondszruntime %.1f secondsr$z6Replaying traffic for %u conversations over %d seconds�
intentionscss�|]}t|�VqdSr�)r�r�rXrXrYr��s�zreplay.<locals>.<genexpr>)�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)�r�r5zkilling %d children with -%d��?zchildren is %s, no pid foundz)kill -%d %d KILLED conversation; %d to goz%d children are missingzignoring fake ^CrX)"r�r�rr��setpgrpr�rQrRrSr�r�r��sumr�r�r�r��waitpid�WNOHANG�OSError�errnor�poprPr�r_r��killrrar�r��killpg�KeyboardInterrupt)�conversation_seq�hostr�r��accountsr��dns_query_filer��latency_timeout�stop_on_any_errorr�r��delayr�r^�childrenr�r
r�r*r�r�r�r�rMrnrXrXrYr�us|������
���
��

��
�������
�����
���




�������
�����
���




�����r�cCs"t�}td||dg||d�}|S)Nr�zmodules:paged_searches)r�r��optionsr�r�)rr
)r�r�r�rr�rXrXrY�openLdbs�r�cCsd||��fS)z(Generate an ou name from the instance idz#ou=instance-%d,ou=traffic_replay,%s)r�)r�r�rXrXrY�ou_names�r�c
Cs�t||�}z|�|�dd�ddd��Wnty1}z|j\}}|dkr'�WYd}~nd}~wwz|�|dd��W|StyZ}z|j\}}|dkrO�WYd}~|Sd}~ww)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.
    r�r\�organizationalunit)r��objectclass�DN)r��addr�rrW�r�r�r�r�r��_rXrXrY�	create_ous2
�
���
��

���r��ConversationAccounts)rr r!r"c	CsHg}td|d�D]}t||�}t||�}t||||�}|�|�q	|S)z;Generate a series of unique machine and user account names.r\)r�machine_namerr�r�)	r�r��number�passwordr�r
rr!r*rXrXrY�generate_replay_accounts<s

�r�Tc	Cs`t||�}d||f}dt|��d�}|rtttB�}ntt�}|�|dd|||d��dS)z"Create a machine account via ldap.r�"%s"�	utf-16-le�computerz%s$�r�r��sAMAccountName�userAccountControl�
unicodePwdN)r�r �encoder�rrrr�)	r�r�rr �traffic_accountr�r��utf16pw�account_controlsrXrXrY�create_machine_accountJs
��r�cCs\t||�}d||f}dt|��d�}|�|d|tt�|d��t�|�}|�|d�dS)zCreate a user account via ldap.rr�r��userr�z(A;;WP;;;PS)N)	r�r r�r�r�rr�SDUtils�dacl_add_ace)r�r�r!r"r�r'r��sdutilsrXrXrY�create_user_accountcs
�
	r�cCs,t||�}d||f}|�|d|d��dS)zCreate a group via ldap.r�group)r�r�r�N)r�r�)r�r�rMr�r�rXrXrY�create_groupus
�r�cC�d||fS)z-Generate a user name based in the instance idz
STGU-%d-%drX�r�r
rXrXrYr��rr�r�cs(|jd�|��gd�}�fdd�|D�S)z'Seach objectclass, return attr in a setz(objectClass={}))r�r�csh|]}t|���qSrX)r�)r��obj��attrrXrY�	<setcomp>�r�z%search_objectclass.<locals>.<setcomp>)r�r�)r�r�r�objsrXr�rY�search_objectclass�s
�rcCslt|dd�}d}t|dd�D]%}t||�}||vr3t||||�|d7}|ddkr3t�d||f�q|S)zAdd users to the serverr��r�rrJr\�2zCreated %u/%u users)rrrr�r�r�)r�r�r�r��existing_objects�usersr
rMrXrXrY�generate_users�s
�rcCs|rd||fSd||fS)z1Generate a machine account name from instance id.z
STGM-%d-%dzPC-%d-%drX)r�r
r�rXrXrYr��sr�c	Cstt|dd�}d}t|dd�D])}t|||�}|d|vr7t|||||�|d7}|ddkr7t�d||f�q|S)	z"Add machine accounts to the serverr�rrrJrAr\rzCreated %u/%u machine accounts)rrr�r�r�r�)	r�r�r�r�r�r�addedr
rMrXrXrY�generate_machine_accounts�s
��r
cCr�)z'Generate a group name from instance id.z
STGG-%d-%drXr�rXrXrY�
group_name�r�rcCsjt|dd�}d}t|dd�D]$}t||�}||vr2t|||�|d7}|ddkr2t�d||f�q|S)z3Create the required number of groups on the server.r�rrrJr\r�zCreated %u/%u groups)rrrr�r�r�)r�r�r�r�groupsr
rMrXrXrY�generate_groups�s
�r
c
CsZt||�}z
|�|dg�WdSty,}z|j\}}|dkr!�WYd}~dSd}~ww)z7Remove the created accounts and groups from the server.z
tree_delete:1� N)r��deleterrWr�rXrXrY�clean_up_accounts�s

���rc	Cs�d}	d}
d}t||�t�d�t||||�}t�d�t|||||�}|dkr3t�d�t|||�}
|dkrTt�d�t||
||||�}
t�d�t|||
�|
��}	|
dkre|dkre||
kret�	d�t�d|||
|	f�d	S)
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)
r�r�r�rr
r
�GroupAssignments�add_users_to_groupsr�warning)r�r�r��number_of_users�number_of_groups�group_memberships�max_members�machine_accounts�traffic_accounts�memberships_added�groups_added�computers_added�users_added�assignmentsrXrXrY�generate_users_and_groups�sD


�

�

�
�rc@sdeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dS)rcCsBd|_|�|�|�||�||_tt�|_|�|||||�dS)Nr)r��generate_group_distribution�generate_user_distributionrrrr�
assign_groups)r�rrrrrrrXrXrYr�s


�zGroupAssignments.__init__cCs@g}t|�}|dkrdSd}|D]
}||7}|�||�q|S)Nrr�)r�r�)r��weights�distr�
cumulative�probabilityrXrXrY�cumulative_distributionsz(GroupAssignments.cumulative_distributioncCsj|dkrd}n|dkrd}n	|dkrd}nd}g}td|d�D]}t�|�}|�|�q |�|�|_d	S)
zAProbability distribution of a user belonging to a group.
        i@KLg@i��g@i�g@g�?r\N)rr�
paretovariater�r'�	user_dist)r��	num_users�num_memberships�shaper#rorlrXrXrYr!(s	
z+GroupAssignments.generate_user_distributioncCsDg}td|d�D]
}d|d}|�|�q	||_|�|�|_dS)z6Probability distribution of a group containing a user.r\g�������?N)rr��
group_weightsr'�
group_dist)r�r�r#rorlrXrXrYr Bsz,GroupAssignments.generate_group_distributioncCs,t�|jt���}t�|jt���}||fS)z2Returns a randomly generated user-group membership)�bisectr)rr.�r�r�r�rXrXrY�generate_random_membershipPsz+GroupAssignments.generate_random_membershipcCs
|j|Sr�)r)r�r�rXrXrY�users_in_group\rszGroupAssignments.users_in_groupcCs
|j��Sr�)rrr�rXrXrY�
get_groups_rszGroupAssignments.get_groupscCsPt|j|�}||kr&t�d�||��d|j|d<|�|j�}||_dSdS)z?Prevent the group's membership from exceeding the max specifiedzGroup {0} has {1} membersrr\N)r�rr�r�r�r-r'r.)r�r�r�num_members�new_distrXrXrY�cap_group_membershipbs
�z%GroupAssignments.cap_group_membershipcCsH||j|vr|j|�|�|jd7_|jr"|�||j�dSdS)Nr\)rr�r�rr6r0rXrXrY�add_assignmentns�zGroupAssignments.add_assignmentc
Cs�|dkrdSt�t|�t|�t|��}|jr t||j|�}||d}||d}|��|krR|��\}}	|	|ks@||krJ|�|d|	d�|��|ks2dSdS)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\)r�ceilr�rr�rr1r7)
r�rrrrr�existing_users�existing_groupsr�r�rXrXrYr"zs$����zGroupAssignments.assign_groupscCs|jSr��r�r�rXrXrYr�szGroupAssignments.totalN)rrrsrtr�r'r!r r1r2r3r6r7r"rrXrXrXrYrs!rc
Cs�|��}d}d}|��D]A}|�|�}t|�dkrqtdt|�d�D]*}|||d�}	t||||	�|t|	�7}|d7}|ddkrLt�d||f�q"qdS)zDTakes the assignments of users to groups and applies them to the DB.rr�r\rzAdded %u/%u membershipsN)rr3r2r�r�add_group_membersr�r�)
rr�rrr�r	r�r2�chunk�chunk_of_usersrXrXrYr�s"
���rc
s�t||���fdd�}|t||��}t��}t�||�|_|D]}|t||��}dt|�}	t�|tj	d�||	<q|�
|�dS)z(Adds the given users to group specified.csd|�fS)NrrXrL�r�rXrY�build_dn�r�z#add_group_members.<locals>.build_dnzmember-�memberN)r�rr��Message�Dnr�rr��MessageElement�FLAG_MOD_ADD�modify)
rr�r�r2r@�group_dn�mr�r'�idxrXr?rYr<�s
r<c*Cs�tjj}d}d}d}i}t�}t�}|dur|j}	ndd�}	|	d�dddd�}
dddd�}t�|�D]�}tj�	||�}
t
|
d���}|D]�}z[|�d	��d
�}|d}|d}|d
}t
|d�}t
|d�}t|||�}t||�}||f}|�|g��|�|ddkr�|d7}n|d7}||d7<|�|�|	|�WqDttfy�d|vr�|�dd�\}}||
vr�tt
|�|
|�|
|<n||vr�tt|�||�||<nt|tjd�nt|tjd�YqDwWd�n1s�wYq3||}|dk�rd}n||}|dk�rd}n||}t|�}td|�td||f�td||f�t|
���D]\}}td|�dd�d|f��q4t|���D]\}}td|�dd�d|f��qNtd�i}|D]\}}||v�rxt�||<||�|��qjt|���} | D]W}t||td�}!|!D]I}||f}||}"t|"�}"t|"�}#||}t|"�|#}$t|"d�}%t|"d�}&|"d|"d}'|"d}(t� |d�})td |||)|#||$|%|&|'|(f
��q��q�dS)!z/Generate and print the summary stats for a run.rNcSsdSr�rX)rorXrXrY�tw�szgenerate_stats.<locals>.twz2time	conv	protocol	type	duration	successful	error
)zMaximum lagz	Start lagzMax sleep miss)r�r�r�r
r�r�r\r"rEr�r��Truer!rNzTotal 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�r�gffffff�?rJr�z?%-12s   %4s  %-35s %12d %12d %12.6f %12.6f %12.6f %12.6f %12.6f)!rR�
float_infor�rr$�writer��listdirr�r�r�r�r�r�r�r�r�r�r�
IndexErrorr�rQrSr��sortedr��replacer�
opcode_keyr��calc_percentiler�r�)*r��timing_filer
r�
successful�failed�	latencies�failures�unique_conversationsrJ�float_values�
int_valuesr�r�r,r�r�r�r|�packet_type�latencyr��oprr�r��success_rate�failure_rater��ops�protor��	protocols�packet_typesr�r��mean�median�
percentile�rng�maxvr~rXrXrY�generate_stats�s���



�
������&

��  



�
���rjcCs&zdt|�WSty|YSw)zCSort key for the operation code to ensure that it sorts numericallyz%03d)r�r)r�rXrXrYrRM	s
�rRcCsp|sdSt|�d|}t�|�}t�|�}||kr |t|�S|t|�||}|t|�||}||S)ztCalculate the specified percentile from the list of values.

    Assumes the list is sorted in ascending order.
    rr\)r�r�floorr8r�)r�rgrr,rM�d0�d1rXrXrYrSU	s

rScGs.tjj|�}t�d�}t�|�t�|�|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�r�maskrXrXrYr#g	s



r#r;)r\r")r\r�)	NNNNrNNr$F)T)r�r�)yr�r�rrrrRr�r�rr�collectionsrrrr�dns.resolverrr��
samba.emulater	�samba.samdbr
r�r�samba.dcerpcrr
rrr�samba.dcerpc.netlogonrrr�samba.drs_utilsrr_�samba.credentialsrrr�
samba.authr�
samba.dsdbrrrr�samba.dcerpc.miscr�sambarr�samba.commonr �samba.loggerr!r/rrr�rr�r�r�r
rr%rPrrr�rZrdrpr�rq�objectrur�r�r�rmr�r�r�r�r�r�r�r:r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr�r
rr
rrrrr<rjrRrSr#rXrXrXrY�<module>s��
�

e(N5	a
��������	�
���
������������	�
���
������������������� �!�"�#�$�%�&�'�(�)�*�+�,�-�.�/�0�1�2�3�4�5�6�7�8�9�:�;�<�=�>�?�@�A�B�C�D�E�F�G�H�I�J�K�L�M�N�O�P�Q�R�S�T�U�V
�b
@$�
��	��-