o
    jOk                     @   sD  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZmZmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ G dd dZG dd dZ G dd dZ!G dd dZ"G dd dZ#G dd dZ$G dd dZ%G dd dZ&G dd dZ'G dd dZ(dS )    N)List)TicketEncryptedData	krb5_pvnoKrbCredInfoEncryptionKeyKRBCREDTicketFlagsEncKrbCredPart)dt_to_kerbtime)EncryptionTypeMESSAGE_TYPE)logger)core)hashlib)Kirbi)KerberosSPNc                   @   sB   e Zd Zdd Zedd Zdd Zedefdd	Zd
d Z	dS )Headerc                 C   s   d | _ d | _d | _d S Ntagtaglentagdataself r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/minikerberos/common/ccache.py__init__   s   
zHeader.__init__c                 C   s   t | }g }| t| k r>t }tj|dddd|_tj|dddd|_	||j	|_
|| | t| k s|S )z#
		returns a list of header tags
		   bigF	byteordersigned)ioBytesIOtelllenr   int
from_bytesreadr   r   r   append)datareaderheadershr   r   r   parse   s   

zHeader.parsec                 C   s:   | j jdddd}|t| jjdddd7 }|| j7 }|S Nr   r   Fr    )r   to_bytesr&   r   r   tr   r   r   r1   *   s   
zHeader.to_bytesr+   c                 C   s
   t | S r   )r   r/   r+   r   r   r   r(   0   s   
zHeader.from_bytesc                 C   s*   d| j  }|d| j 7 }|d| j 7 }|S )Nztag: %s
ztaglen: %s
ztagdata: %s
r   r2   r   r   r   __str__4   s   
zHeader.__str__N)
__name__
__module____qualname__r   staticmethodr/   r1   bytesr(   r5   r   r   r   r   r      s    
r   c                   @   sB   e Zd Zdd ZedejfddZdd Zede	fd	d
Z
dS )DateTimec                 C      d | _ d | _d S r   )time_offsetusec_offsetr   r   r   r   r   ;      
zDateTime.__init__r,   c                 C   s:   t  }tj| dddd|_tj| dddd|_|S N   r   Fr    )r;   r'   r(   r)   r=   r>   )r,   dr   r   r   r/   ?   s   zDateTime.parsec                 C   s,   | j jdddd}|| jjdddd7 }|S r@   )r=   r1   r>   r2   r   r   r   r1   F   s   zDateTime.to_bytesr+   c                 C      t t| S r   )r;   r/   r#   r$   r4   r   r   r   r(   K      zDateTime.from_bytesNr6   r7   r8   r   r9   r#   r$   r/   r1   r:   r(   r   r   r   r   r;   :   s    r;   c                   @   sz   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zede	fddZ
edd Zedd Zedd Zdd Zdd ZdS )
Credentialc                 C   sL   d | _ d | _d | _d | _d | _d | _d | _g | _d | _g | _	d | _
d | _d S r   )clientserverkeytimeis_skeytktflagsnum_addressaddrsnum_authdataauthdataticketsecond_ticketr   r   r   r   r   R   s   
zCredential.__init__c                 C   s   t | j j}t|d d }t|d d }|dkr&|d d d }n|d d d }|d }|tjj	krY|d d d	d  }|d d d d	 }d
||||
 |
 f S |d d d d }|d d dd  }d||||
 |
 f S )Nenc-partetypesnamename-string   r   realmcipheriz$krb5tgs$%s$%s$%s$%s$%s   z$krb5tgs$%s$*%s$%s$spn*$%s$%s)r   loadrQ   to_asn1nativer'   r&   r   AES256_CTS_HMAC_SHA1_96valuehex)r   restgs_encryption_typer3   tgs_name_string	tgs_realmtgs_checksumtgs_encrypted_data2r   r   r   to_hash`   s   zCredential.to_hashc                 C      t ddd}i }t|d< tjj|d< | jj |d< | j d |d< t	
| j j|d	< |j|d
< t| j j}||fS zf
		Returns the native format of an AS_REP message and the sessionkey in EncryptionKey native format
		rW       rT   rY   pvnomsg-typecrealmr   cnamerQ   rS   r   r   r   
KRB_AS_REPr_   rG   rX   	to_stringr\   r   r[   rQ   r]   r   rI   r   enc_parttgt_repr3   r   r   r   to_tgtr      
zCredential.to_tgtc                 C   rh   ri   rp   rs   r   r   r   to_tgs   rw   zCredential.to_tgsc                 C   s  d| j  | j t| j  d d f }i }t| j	 |d< | j j
 |d< | j  d |d< t| jt|d< | jjdkrStj| jjtjj|d< | jjdkrftj| jjtjj|d	< | jjdkrytj| jjtjj|d
< | jjdkrtj| jjtjj|d< | jj
 |d< | j d |d< i }t|g|d< i }t|d< tjj|d< t| j g|d< t t!j"jt#|$ d|d< t%t&|}||fS )Nz%s@%s_%s   rI   prealmr   pnameflagsauthtime	starttimeendtime
renew-tillsrealmrU   ticket-inforl   rm   ticketsrk   rS   )'rG   rr   rH   r   sha1rQ   r\   	hexdigestr   rI   rX   r   IntegerBitStringrL   castr	   rJ   r}   datetimefromtimestamptimezoneutcr~   r   
renew_tillr   r   r   KRB_CREDr_   r   r[   r   r   NULLr
   dumpr   r   )r   filenamekrbcredinfoenc_krbcredkrbcredkirbir   r   r   to_kirbi   s2   2zCredential.to_kirbir   c                 C   s8  | j j}t }t|d d j}|d d }t|d |d |_t|d d d	krY|d d d
 	 |d 	 krYt
d |d }|d d d
 |d< t||d |_nt|d |d |_t||_t|d |_d|_t|d tjj|_d|_d|_tt|d d  |_t |_|S )NrS   rY   r   r   r{   rz   rU   rV   r   r   z-SNAME contains the realm as well, trimming itrI   r|   r   )kirbiobjr]   rF   r
   r[   CCACHEPrincipal	from_asn1rG   r&   upperr   debugrH   TimesrJ   KeyblockrI   rK   r	   r   r   r   rL   rM   rO   CCACHEOctetStringr   r   rQ   emptyrR   )r   r   cenc_credinfoticket_infor3   r   r   r   
from_kirbi   s(   4

zCredential.from_kirbic                 C   s   t  }t|d |d |_t|d |d |_t|d |_d|_t|d 	t
jj|_d|_d|_t| d d	 |_t |_|S )
Nr{   rz   rU   r   rI   r   r|   rS   rY   )rF   r   r   rG   rH   r   rI   rK   r	   r   r   r   r]   rL   rM   rO   r   rQ   r   rR   )rQ   r+   r   r   r   r   r      s   
zCredential.from_asn1c                 C   s   t  }t| |_t| |_t| |_t| |_t	j
| dddd|_t	j
| dddd|_t	j
| dddd|_t|jD ]}|jt|  qDt	j
| dddd|_t|jD ]}|jt|  qat| |_t| |_|S NrW   r   Fr    rA   little)rF   r   r/   rG   rH   r   rI   r   rJ   r'   r(   r)   rK   rL   rM   rangerN   r*   AddressrO   rP   Authdatar   rQ   rR   )r,   r   _r   r   r   r/      s    zCredential.parsec                   C   s   g dS )N)rG   rH   r~   r   r   r   r   r   r   r   summary_header   s   zCredential.summary_headerc                 C   s   d| j jdd| j j f d| jjdd| jj f | jjdkr,tj| jj nd| jj	dkr=tj| jj	 nd| jj
dkrOtj| jj
 gS dgS )Nz%s@%s/	separatorr   zN/A)rG   rr   rX   rH   rJ   r~   r   r   	isoformatr   r   r   r   r   r   summary   s   ""zCredential.summaryc                 C   s   | j  }|| j 7 }|| j 7 }|| j 7 }|| jjdddd7 }|| jjdddd7 }|| jjdddd7 }| jD ]}|| 7 }q>|| j	jdddd7 }| j
D ]}|| 7 }qU|| j 7 }|| j 7 }|S r   )rG   r1   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   )r   r3   addradr   r   r   r1     s   


zCredential.to_bytesN)r6   r7   r8   r   rg   rv   rx   r   r9   r   r   r   r/   r   r   r1   r   r   r   r   rF   Q   s     



rF   c                   @   s<   e Zd Zdd Zedd Zdd Zedd Zd	d
 ZdS )r   c                 C      d | _ d | _d | _d | _d S r   )keytyperT   keylenkeyvaluer   r   r   r   r        
zKeyblock.__init__c                 C   s2   t  }| d |_d|_t| d |_| d |_|S )Nr   r   r   )r   r   rT   r&   r   r   )r+   kr   r   r   r     s   

zKeyblock.from_asn1c                 C   s   i }| j |d< | j|d< |S )Nr   r   )r   r   r2   r   r   r   r\   (  s   

zKeyblock.to_asn1c                 C   s`   t  }tj| dddd|_tj| dddd|_tj| dddd|_| |j|_|S Nr   r   Tr    F)r   r'   r(   r)   r   rT   r   r   )r,   r   r   r   r   r/   /  s   zKeyblock.parsec                 C   sL   | j jdddd}|| jjdddd7 }|| jjdddd7 }|| j7 }|S r   )r   r1   rT   r   r   r2   r   r   r   r1   8  s
   
zKeyblock.to_bytesN)	r6   r7   r8   r   r9   r   r\   r/   r1   r   r   r   r   r     s    
	
r   c                   @   sb   e Zd Zdd Zedd Zeejejj	fddZ
edd Zd	d
 ZedefddZdS )r   c                 C   r   r   )r}   r~   r   r   r   r   r   r   r   A  r   zTimes.__init__c                 C   s   t  }d| v r| d rt| d nd|_d| v r"| d r"t| d nd|_d| v r3| d r3t| d nd|_d| v rG| d rGt| d |_|S d|_|S )Nr}   r   r~   r   r   )r   r   r}   r~   r   r   )enc_as_rep_partr3   r   r   r   r   G  s&   zTimes.from_asn1c                 C   sJ   t  }t| |_t| |_t| tjdd |_t| tjdd |_|S )NrW   )daysr   )r   r   r}   r~   r   	timedeltar   r   )startr3   r   r   r   
dummy_timeT  s   

zTimes.dummy_timec                 C   sj   t  }tj| dddd|_tj| dddd|_tj| dddd|_tj| dddd|_|S r@   )r   r'   r(   r)   r}   r~   r   r   )r,   r3   r   r   r   r/   ]  s   zTimes.parsec                 C   sX   | j jdddd}|| jjdddd7 }|| jjdddd7 }|| jjdddd7 }|S r@   )r}   r1   r~   r   r   r2   r   r   r   r1   f  s
   zTimes.to_bytesr+   c                 C   rC   r   )r   r/   r#   r$   r4   r   r   r   r(   m  rD   zTimes.from_bytesN)r6   r7   r8   r   r9   r   r   nowr   r   r   r/   r1   r:   r(   r   r   r   r   r   @  s    

r   c                   @   s6   e Zd Zdd Zedd Zdd Zdefdd	Zd
S )r   c                 C   r<   r   )addrtypeaddrdatar   r   r   r   r   r  r?   zAddress.__init__c                 C   .   t  }tj| dddd|_t| |_|S r0   )r   r'   r(   r)   r   r   r/   r   r,   ar   r   r   r/   v     zAddress.parsec                 C   $   | j jdddd}|| j 7 }|S r0   )r   r1   r   r2   r   r   r   r1   }     zAddress.to_bytesr+   c                 C   rC   r   )r   r/   r#   r$   r4   r   r   r   r(        zAddress.from_bytesN)	r6   r7   r8   r   r9   r/   r1   r:   r(   r   r   r   r   r   q  s    
r   c                   @   s>   e Zd Zdd ZedejfddZdd Zde	fd	d
Z
dS )r   c                 C   r<   r   )authtyperP   r   r   r   r   r     r?   zAuthdata.__init__r,   c                 C   r   r0   )r   r'   r(   r)   r   r   r/   rP   r   r   r   r   r/     r   zAuthdata.parsec                 C   r   r0   )r   r1   rP   r2   r   r   r   r1     r   zAuthdata.to_bytesr+   c                 C   rC   r   )r   r/   r#   r$   r4   r   r   r   r(     r   zAuthdata.from_bytesNrE   r   r   r   r   r     s    r   c                   @   sb   e Zd Zdd Zedd Zedd Zddd	Zd
d Zdd Z	edd Z
dd Zdd ZdS )r   c                 C   s   d | _ d | _d | _g | _d S r   )	name_typenum_componentsrX   
componentsr   r   r   r   r     r   zCCACHEPrincipal.__init__c                 C   sN   t  }| d |_t| d |_t||_| d D ]}|jt	| q|S )N	name-typerV   )
r   r   r&   r   r   from_stringrX   r   r*   r   )	principalrX   pcompr   r   r   r     s   
zCCACHEPrincipal.from_asn1c                  C   sB   t  } d| _d| _td| _tdD ]}| jtd q| S )NrW   z
kerbi.corpkerbi)	r   r   r   r   r   rX   r   r   r*   )r   r   r   r   r   r     s   zCCACHEPrincipal.empty-c                 C   s   | dd | jD S )Nc                 S      g | ]}|  qS r   rr   .0r   r   r   r   
<listcomp>      z-CCACHEPrincipal.to_string.<locals>.<listcomp>)joinr   )r   r   r   r   r   rr     s   zCCACHEPrincipal.to_stringc                 C   s&   | j dd | jD d}|| j fS )Nc                 S   r   r   r   r   namer   r   r   r     r   z+CCACHEPrincipal.to_asn1.<locals>.<listcomp>)r   rV   )r   r   rX   rr   r2   r   r   r   r\     s   zCCACHEPrincipal.to_asn1c                 C   s$   d dd | jD d | j  S )Nr   c                 S   r   r   r   r   r   r   r   r     r   z*CCACHEPrincipal.to_spn.<locals>.<listcomp>@)r   r   rX   rr   r   r   r   r   to_spn  s   $zCCACHEPrincipal.to_spnc                 C   sh   t  }tj| dddd|_tj| dddd|_t| |_t	|jD ]}|j
t|  q&|S r@   )r   r'   r(   r)   r   r   r   r/   rX   r   r   r*   )r,   r   r   r   r   r   r/     s   zCCACHEPrincipal.parsec                 C   sV   | j jdddd}|t| jjdddd7 }|| j 7 }| jD ]}|| 7 }q |S r@   )r   r1   r&   r   rX   )r   r3   comr   r   r   r1     s   
zCCACHEPrincipal.to_bytesc                 C   s,   | j  d }|ddd | jD 7 }|S )Nr   c                 S   r   r   r   r   r   r   r   r     r   z+CCACHEPrincipal.__str__.<locals>.<listcomp>)rX   rr   r   r   r2   r   r   r   r5     s   zCCACHEPrincipal.__str__N)r   )r6   r7   r8   r   r9   r   r   rr   r\   r   r/   r1   r5   r   r   r   r   r     s    





	r   c                   @   sn   e Zd Zdd Zedd Zdd Zdd Zed	d
 Zedd Z	edd Z
dd ZdedefddZdS )r   c                 C   r<   r   )lengthr+   r   r   r   r   r     r?   zCCACHEOctetString.__init__c                  C   s   t  } d| _d| _| S )Nr   rj   )r   r   r+   )or   r   r   r     s   zCCACHEOctetString.emptyc                 C   s   | j S r   r4   r   r   r   r   r\     s   zCCACHEOctetString.to_asn1c                 C   s
   | j  S r   )r+   decoder   r   r   r   rr     s   
zCCACHEOctetString.to_stringc                 C   s    t  }|  |_t|j|_|S r   )r   encoder+   r&   r   r+   r   r   r   r   r     s   
zCCACHEOctetString.from_stringc                 C   s2   t  }t| |_t| tr|  |_|S | |_|S r   )r   r&   r   
isinstancestrr   r+   r   r   r   r   r     s   


zCCACHEOctetString.from_asn1c                 C   s0   t  }tj| dddd|_| |j|_|S r@   )r   r'   r(   r)   r   r+   )r,   r   r   r   r   r/     s   zCCACHEOctetString.parsec                 C   sH   t | jtr| j | _t| j| _t| jjdddd}|| j7 }|S r@   )r   r+   r   r   r&   r   r1   r2   r   r   r   r1     s   
zCCACHEOctetString.to_bytes_CCACHEOctetString__oreturnc                 C   s   t |tr|  | kS dS NF)r   r   r1   )r   r   r   r   r   __eq__
  s   
zCCACHEOctetString.__eq__N)r6   r7   r8   r   r9   r   r\   rr   r   r   r/   r1   objectboolr   r   r   r   r   r     s    


	
r   c                   @   s<  e Zd ZdZd@ddZdd Zdd ZdAd
dZd@ddZdAde	fddZ
dd Zdd Zd@dedefddZdBdefddZdd Zdd Zed ejfd!d"Zd#d$ Zd%d& Zed'e	fd(d)Zed*efd+d,Zed-d. Zd/efd0d1Zed2d3 Zd4d5 Zed6d7 Z ed8d9 Z!d:d; Z"d<d= Z#ed>d? Z$dS )CCCACHEz
	As the header is rarely used -mostly static- you'd need to init this object with empty = True to get an object without header already present
	Fc                 C   s0   d | _ g | _d | _g | _|dkr|   d S d S r   )file_format_versionr-   primary_principalcredentials_CCACHE__setup)r   r   r   r   r   r     s   zCCACHE.__init__c                 C   s8   d| _ t }d|_d|_d|_| j| t | _	d S )Ni  rW   ry   s           )
r   r   r   r   r   r-   r*   r   r   r   )r   headerr   r   r   __setup  s   zCCACHE.__setupc                 C   s<   d}|d| j  7 }| jD ]}|d| 7 }q|d| j 7 }|S )Nz== CCACHE ==
zfile_format_version : %s
z%s
zprimary_principal : %s
)r   r-   r   )r   r3   r   r   r   r   r5   '  s   
zCCACHE.__str__Tc                 C      t  }t|d |d |_|dkr|j| _t|d |d |_t||_t|d |_	d|_
t|d tjj|_d|_d|_tt|d	  |_t |_| j| d
S )a  
		Creates credential object from the TGT and adds to the ccache file
		The TGT is basically the native representation of the asn1 encoded AS_REP data that the AD sends upon a succsessful TGT request.
		
		This function doesn't do decryption of the encrypted part of the as_rep object, it is expected that the decrypted XXX is supplied in enc_as_rep_part
		
		override_pp: bool to determine if client principal should be used as the primary principal for the ccache file
		ro   rn   TrU   r   rI   r   r|   rQ   NrF   r   r   rG   r   rH   r   rJ   r   rI   rK   r	   r   r   r   r]   rL   rM   rO   r   r   r   rQ   r   rR   r   r*   )r   as_repr   override_ppr   r   r   r   add_tgt/     	
zCCACHE.add_tgtc                 C   r   )a  
		Creates credential object from the TGS and adds to the ccache file
		The TGS is the native representation of the asn1 encoded TGS_REP data when the user requests a tgs to a specific service principal with a valid TGT
		
		This function doesn't do decryption of the encrypted part of the tgs_rep object, it is expected that the decrypted XXX is supplied in enc_as_rep_part
		
		override_pp: bool to determine if client principal should be used as the primary principal for the ccache file
		ro   rn   TrU   r   rI   r   r|   rQ   Nr   )r   tgs_repenc_tgs_rep_partr   r   r   r   r   add_tgsI  r   zCCACHE.add_tgsr   c                 C   s*   t |}|dkr|j| _| j| d S )NT)rF   r   rG   r   r   r*   )r   r   r   r   r   r   r   	add_kirbid  s   
zCCACHE.add_kirbic                 C   sB   g }| j D ]}|jjdd ddkr|| d  q|S )z
		Returns a list of AS_REP tickets in kirbi format (Kirbi). 
		To determine which ticket are AP_REP we check for the server principal to be the kerberos service
		r   r   krbtgtr   r   )r   rH   rr   lowerfindr*   r   r   tgtscredr   r   r   get_all_tgt_kirbisj  s   
zCCACHE.get_all_tgt_kirbisc                 C   s>   g }| j D ]}|jjdd ddkr||  q|S )z
		Returns a list of AS_REP tickets in native format (dict). 
		To determine which ticket are AP_REP we check for the server principal to be the kerberos service
		r   r   r   r   )r   rH   rr   r   r   r*   rv   r   r   r   r   get_all_tgtv  s   
zCCACHE.get_all_tgtspnstrictc                 C   s   |   }|D ]/\}}d|d d d d |d d  }| t| kr5td|  ||d f  S q|du rCd d td	| fS |d
 d
 |d
 d d fS )Nr   rQ   rU   rV   r   rX   zFound TGS for SPN %sTzNo TGS found for SPN %sr   rW   )get_all_tgsr   r   r   r   r   	Exception)r   r  r  tgsstgs	keystruct
ticket_forr   r   r   get_tgs  s   &zCCACHE.get_tgsNc           	      C   sz  dd |   D }t|dkrd d tdfS |du rh|d u r0td |d d |d d d fS |d u rh|D ]\}}|d d	 t|d
krUtd|  ||d f  S q6td |d d |d d d fS |D ]3\}}t|d t| }d|d d	 d |d  }| | krtd|  ||d f  S qj|du rtd |d d |d d d fS d d td| fS )Nc                 S   s   g | ]}|qS r   r   )r   xr   r   r   r     s    z"CCACHE.get_tgt.<locals>.<listcomp>r   zNo TGT found in CCACHE fileFz0Missing username, returning first availabla TGT!rW   ro   rV   r   zFound TGT for user %sz>Missing domain, user not found, returning first availabla TGT!r   r   rn   z;domain+user combo not found, returning first availabla TGT!zNo TGT found for user %s)	r  r&   r  r   r   r   r   r   r   )	r   usernamedomainr  r  tgtr  our_userr  r   r   r   get_tgt  s4   


zCCACHE.get_tgtc                 C   s>   g }| j D ]}|jjdd ddkr||  q|S )Nr   r   r   r   )r   rH   rr   r   r   r*   rx   )r   r	  r  r   r   r   r    s   
zCCACHE.get_all_tgsc                 C   s"   g }| j D ]	}||  q|S )z
		Returns a list of hashes in hashcat-firendly format for tickets with encryption type 23 (which is RC4)
		all_hashes: overrides the encryption type filtering and returns hash for all tickets

		)r   r*   rg   )r   hashesr  r   r   r   
get_hashes  s   
zCCACHE.get_hashesr,   c                 C   s   t d}tj| dddd|_tj| dddd}t| ||_t| |_	| 
 }| dd | 
 }| |d | 
 |k rot| }t|jjdkrc|jjd  dkrc|jj d	ksi|j| | 
 |k sD|S )
NTr   r   Fr    r   r   krb5_ccache_conf_datazX-CACHECONF:)r   r'   r(   r)   r   r   r/   r-   r   r   r%   seekrF   r&   rH   r   rr   rX   r   r*   )r,   r   hdr_sizeposeofr  r   r   r   r/     s    
$zCCACHE.parsec                 C   sx   | j jdddd}d}| jD ]}|| 7 }q|t|jdddd7 }||7 }|| j 7 }| jD ]}|| 7 }q1|S )Nr   r   Fr    rj   )r   r1   r-   r&   r   r   )r   r3   t_hdrr   r  r   r   r   r1     s   

zCCACHE.to_bytesc                 c   s"    | j D ]
}|j}| V  qd S r   )r   rH   r   )r   r  targetr   r   r   list_targets  s
   
zCCACHE.list_targets	kirbidatac                 C   s2   t | tdu rt| }n| }t }|| |S r   )r   r   r(   r   r   )r  r   ccr   r   r   r     s   
zCCACHE.from_kirbikirbi_filenamec                 C   st   t j| }d }t|d}| }W d    n1 sw   Y  zt|}W |S    t|}t|}Y |S )Nrb)	ospathabspathopenr)   r   r   r   from_b64)r   kf_absr  fccacher   r   r   r   from_kirbifile  s   

zCCACHE.from_kirbifilec                 C   sD   t  }tjtj| d}t|D ]}t|}|| q|S )zk
		Iterates trough all .kirbi files in a given directory and converts all of them into one CCACHE object
		z*.kirbi)	r   r"  r#  r   r$  globr   	from_filer   )directory_pathr  dir_pathr   r   r   r   r   from_kirbidir  s   
zCCACHE.from_kirbidirr-  c                 C   sP   t j|}| jD ]}| \}}d|dd }t j||}|| q	dS )a  
		Converts all credential object in the CCACHE object to the kirbi file format used by mimikatz.
		The kirbi file format supports one credential per file, so prepare for a lot of files being generated.
		
		directory_path: str the directory to write the kirbi files to
		z%s.kirbiz..!N)r"  r#  r$  r   r   replacer   to_file)r   r-  r'  r  r   r   filepathr   r   r   to_kirbidir!  s   
zCCACHE.to_kirbidirc                 C   s8   t | d}t|W  d   S 1 sw   Y  dS )z8
		Parses the ccache file and returns a CCACHE object
		r!  N)r%  r   r/   )r   r(  r   r   r   r,  /  s   $zCCACHE.from_filec                 C   s>   t |d}||   W d   dS 1 sw   Y  dS )z8
		Writes the contents of the CCACHE object to a file
		wbN)r%  writer1   )r   r   r(  r   r   r   r2  7  s   "zCCACHE.to_filec                 C   rC   r   )r   r/   r#   r$   r4   r   r   r   r(   >  rD   zCCACHE.from_bytesc                 C   rC   r   )r   r(   r:   fromhex)hexdatar   r   r   from_hexB  rD   zCCACHE.from_hexc                 C   s   |    S r   )r1   r`   r   r   r   r   to_hexF  s   zCCACHE.to_hexc                 C   s   t |  dS )Nascii)base64	b64encoder1   r   r   r   r   r   to_b64I  s   zCCACHE.to_b64c                 C   rC   r   )r   r(   r<  	b64decode)b64datar   r   r   r&  L  rD   zCCACHE.from_b64)F)Tr   )%r6   r7   r8   __doc__r   r   r5   r   r   r   r   r  r  r   r   r  r  r  r  r9   r#   r$   r/   r1   r  r   r   r*  r/  r4  r,  r2  r(   r9  r:  r>  r&  r   r   r   r   r     sH    
	


(	



r   ))r"  r#   r   r+  r<  typingr   "minikerberos.protocol.asn1_structsr   r   r   r   r   r   r	   r
   minikerberos.common.utilsr   minikerberos.protocol.constantsr   r   minikerberosr   
asn1cryptor   	unicryptor   minikerberos.common.kirbir   minikerberos.common.spnr   r   r;   rF   r   r   r   r   r   r   r   r   r   r   r   <module>   s2    (% G)1?8