o
    Žõ±j·  ã                   @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZ d dlmZ d dl m!Z!m"Z"m#Z# G dd„ dƒZ$dd„ Z%e&dkrˆdZ'e  (e%e'ƒ¡ dS dS )é    N)Úhashes)ÚSMBDCEFactory)ÚDCERPC5Connection)ÚSMBConnection)Úgkdi)ÚEPM)ÚDCERPCException)Úlogger)Ú
pprint_exc)ÚNULL)ÚRPC_C_AUTHN_LEVEL_NONEÚRPC_C_AUTHN_LEVEL_PKT_INTEGRITYÚRPC_C_AUTHN_LEVEL_PKT_PRIVACYr   ÚRPC_C_AUTHN_GSS_NEGOTIATEÚRPC_C_AUTHN_LEVEL_CONNECT)Ú
DCERPCAuth)ÚDCERPCTarget)ÚDPAPINGBlobÚdecrypt_blobÚGroupKeyEnvelopec                   @   st   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zeddede	de	fdd„ƒZ
eddede	de	fdd„ƒZdd„ Zdd„ Zd	S )ÚGKDIMgrc                 C   s   d | _ tj| _d | _d | _d S ©N)Úservice_pipenamer   ÚMSRPC_UUID_GKDIÚservice_uuidÚdceÚhandle©Úself© r   ú–/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/aiosmb/dcerpc/v5/interfaces/gkdimgr.pyÚ__init__   s   
zGKDIMgr.__init__c                 Ã   s   �| S r   r   r   r   r   r    Ú
__aenter__   s   €zGKDIMgr.__aenter__c                 Ã   s   �|   ¡ I d H  dS ©N)TN)Úclose)r   Úexc_typeÚexcÚ	tracebackr   r   r    Ú	__aexit__   s   €zGKDIMgr.__aexit__c              
   Ã   sd   �z| j rz| j  ¡ I d H  W W dS    Y W dS W d S  ty1 } z
d |fW  Y d }~S d }~ww r#   )r   Ú
disconnectÚ	Exception)r   Úer   r   r    r$   #   s   €þû€ÿzGKDIMgr.closeNTFÚ
connectionÚopenÚperform_dummyc              
   Ã   s    �z8t ƒ }| |_|d u r|j t¡ |j ¡ I d H \}}|d ur"|‚|j t ƒ j¡I d H \}}|d ur5|‚|d fW S  tyO } z
d|fW  Y d }~S d }~ww )NF)r   r   Úset_auth_levelr   ÚconnectÚbindr   r*   )r,   Ú
auth_levelr-   r.   ÚserviceÚ_Úerrr+   r   r   r    Úfrom_rpcconnection.   s"   €
€ÿzGKDIMgr.from_rpcconnectionc              
   Ã   sn   �zt | ƒ}tj| ¡ |||d�I dH \}}|dur|‚|dfW S  ty6 } z
d|fW  Y d}~S d}~ww )z–
		Creates the connection to the service using an established SMBConnection.
		This connection will use the given SMBConnection as transport layer.
		)r2   r-   r.   N)r   r   r6   Úget_dce_rpcr*   )r,   r2   r-   r.   Úrpctransportr3   r5   r+   r   r   r    Úfrom_smbconnectionE   s   € 
€ÿzGKDIMgr.from_smbconnectionc           	      Ã   s`   �t |tjƒr
|j}t | j|||||¡I d H \}}|d ur"d |fS d |d ¡}t 	|¡d fS )Nó    ÚppbOut)
Ú
isinstanceÚuuidÚUUIDÚbytes_ler   ÚhGetKeyr   Újoinr   Úunpack)	r   ÚtargetSDÚrootKeyÚL0KeyIDÚL1KeyIDÚL2KeyIDÚrespr5   Úgke_rawr   r   r    ÚGetKeyU   s   € zGKDIMgr.GetKeyc                 Ã   s`   �t  |¡}|j ¡ }|  ||jj|jj|jj|jj	¡I d H \}}|d ur)d |fS t
||ƒd fS r   )r   rB   Úprotection_descriptorÚget_target_sdrJ   Úkey_identifierÚroot_key_identifierÚl0Úl1Úl2r   )r   ÚdataÚblobÚ	target_sdÚrkr5   r   r   r    Úncrypt_unprotect_secret_   s   €

ûzGKDIMgr.ncrypt_unprotect_secret)NTF)Ú__name__Ú
__module__Ú__qualname__r!   r"   r(   r$   Ústaticmethodr   Úboolr6   r   r9   rJ   rV   r   r   r   r    r      s    
r   c                 Ã   s  �dd l }ddlm} t d¡}| | ¡} |  ¡ }t |¡}| 	¡ I d H \}}|d ur-|‚| 
tƒ j¡I d H \}}|d ur?|‚tj||d�}	t |j¡}
t|
|	ƒ}tj|td�I d H \}}|d urgtdƒ |‚| |¡I d H \}}|d ur{tdƒ |‚t|ƒ t| d¡ƒ d S )	Nr   )ÚSMBConnectionFactoryÁì	  c571d901171889ade6040000000000003082045006092a864886f70d010703a08204413082043d02010231820409a2820405020104308203c704820370010000004b44534b03000000690100000f0000001e00000069d83cd1d10ebfac4620a164e805f56e080300001a0000001a000000444850420001000087a8e61db4b6663cffbbd19c651959998ceef608660dd0f25d2ceed4435e3b00e00df8f1d61957d4faf7df4561b2aa3016c3d91134096faa3bf4296d830e9a7c209e0c6497517abd5a8a9d306bcf67ed91f9e6725b4758c022e0b1ef4275bf7b6c5bfc11d45f9088b941f54eb1e59bb8bc39a0bf12307f5c4fdb70c581b23f76b63acae1caa6b7902d52526735488a0ef13c6d9a51bfa4ab3ad8347796524d8ef6a167b5a41825d967e144e5140564251ccacb83e6b486f6b3ca3f7971506026c0b857f689962856ded4010abd0be621c3a3960a54e710c375f26375d7014103a4b54330c198af126116d2276e11715f693877fad7ef09cadb094ae91e1a15973fb32c9b73134d0b2e77506660edbd484ca7b18f21ef205407f4793a1a0ba12510dbc15077be463fff4fed4aac0bb555be3a6c1b0c6b47b1bc3773bf7e8c6f62901228f8c28cbb18a55ae31341000a650196f931c77a57f2ddf463e5e9ec144b777de62aaab8a8628ac376d282d6ed3864e67982428ebc831d14348f6f2f9193b5045af2767164e1dfc967c1fb3f2e55a4bd1bffe83b9c80d052b985d182ea0adb2a3b7313d3fe14c8484b1e052588b9b7d2bbd2df016199ecd06e1557cd0915b3353bbb64e0ec377fd028370df92b52c7891428cdc67eb6184b523d1db246c32f63078490f00ef8d647d148d47954515e2327cfef98c582664b4c0f6cc4165911914ea310a2766c1ec2f5ba89d34002d975da503cc1809d05249fbad13f038a5a9fcb381601e04ec4b9b4f31f6ef25a3f56356d28590727df18f626a04caa3cd81ec602c22e0172a029256caf961a461e3ab61502ee6f27f1f134a88f37019534c7075cfe990ecfe535d1042ee1964af66a7e9e1167a0ffbd8359a6042a3117102240cf5cfd9c442259f0f3db45d1dd51a10e7f3e314250f2079b6122d84ebd9d405c83826d625c8a57dfe518389d645ee4daec02d48b7470e8419c84ed7bcabaf47ded6a1b1f132010d35004adf37fec61dfbc008fda690389ed945ea96bad5f3acffe0fe01f818cd10d01d58d012a37b98758928531d9bf5a614041f4cfbf4e00450057004c004100500053002e0043004f005200500000004e00450057004c004100500053002e0043004f00520050000000305106092b0601040182374a013044060a2b0601040182374a01013036303430320c035349440c2b532d312d352d32312d39323130343037302d313632323731323839342d333433363534373836332d353132300b060960864801650304012d04281a7b6922fc2450c81354ad9d3ce1020d24c327a071f57de6a74dddf3c0b4acb9cf3104efc06fb18d302b06092a864886f70d010701301e060960864801650304012e3011040c247911a21b5c7f4fd08359f9020110426af21e4b446113ce8c4ea45685ecf21d800047f61d5772452b3dab1efc093692f0e3fa3a16ad0fbdbe0df9e9953a934ff129848d34c1e1bc2f5eaefbe67eaa40f158759144b800325ac1307aafd02ce418badf3a9ed849d4053ffde7c2a3822d169377ebcce5c9a9a1a95261c02d49ba014edf882aa54141d0d9b5aa03155eef9f05a0233dbbd9c45f62f58f407cd25320)Úsmb_connection)r-   zConnection to service failed!zFailed to decrypt secret!z	utf-16-le)r'   Ú!aiosmb.commons.connection.factoryr\   ÚbytesÚfromhexÚfrom_urlÚget_connectionr   r9   r0   Úmapr   r   r   Úfrom_connection_stringr   Úfrom_smb_gssapiÚgssapir   r6   r-   ÚprintrV   Údecode)Úurlr'   r\   rR   r,   Úepmr4   r5   Ú	constringÚtargetÚdcerpc_authÚrpc_connectionr3   Úsecretr   r   r    Úamainp   s4   €



rq   Ú__main__z@smb2+ntlm-password://NEWLAPS\Administrator:Passw0rd!1@10.10.12.2))ÚasyncioÚosr=   Úcryptography.hazmat.primitivesr   Ú0aiosmb.dcerpc.v5.common.connection.smbdcefactoryr   Úaiosmb.dcerpc.v5.connectionr   Úaiosmb.connectionr   Úaiosmb.dcerpc.v5r   Ú'aiosmb.dcerpc.v5.interfaces.endpointmgrr   Úaiosmb.dcerpc.v5.rpcrtr   Úaiosmbr	   Úaiosmb.commons.utils.extbr
   Úaiosmb.dcerpc.v5.dtypesr   r   r   r   r   r   Ú1aiosmb.dcerpc.v5.common.connection.authenticationr   Ú)aiosmb.dcerpc.v5.common.connection.targetr   Ú(aiosmb.dcerpc.v5.common.dpapi_ng._cryptor   r   r   r   rq   rW   rj   Úrunr   r   r   r    Ú<module>   s.    [!þ