o
    j#                     @   sn   d dl Z d dlmZ d dlmZ d dlT G dd dZG dd dZed	kr5e Z	e	
 Zeee dS dS )
    N)logger)LocalWindowsAPI)*c                   @   s   e Zd Zdd Zdd ZdS )	TokenInfoc                 C   s"   || _ || _|| _|| _|| _d S Npiddomainusernamesid
token_type)selfr   r	   r
   r   r    r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pypykatz/commons/winapi/processmanipulator.py__init__   s
   
zTokenInfo.__init__c                 C   s   d| j | j| j| j| jf S )Nz%s:%s:%s:%s:%sr   r   r   r   r   __str__   s   zTokenInfo.__str__N)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdddZd ddZd dd	Zd
d Zd ddZdd Z	dd Z
deeefddZd!ddZd"ddZdd Zdd ZdS )#ProcessManipulatorz
	High level class to adjust privileges and manipulate tokens
	TODO: Currently only working with the current process, doesn't do remote processes!
	TODO: ther ways to get system, eg. creating a service that will launch the new app? like p s e x e c
	Nc                 C   s$   || _ |d ur|| _d S t | _d S r   )r   r   api)r   r   r   r   r   r   r      s   zProcessManipulator.__init__Fc                 C   s"   t d|  | jjj|d|dS )z
		Sets a given privilege
		z)[ProcessManipulator] Setting %s privilegeTenablethread_or_processr   debugr   ntdllRtlAdjustPrivileger   privilige_idr   r   r   r   set_privilege!   s   z ProcessManipulator.set_privilegec                 C   s&   t d|  | jjj|d|d dS )z
		Drops the given privilege
		z*[ProcessManipulator] Dropping %s privilegeFr   Nr   r   r   r   r   drop_privilege(   s   z!ProcessManipulator.drop_privilegec                 C   s   dS )z9
		Lists all available privileges for the current user
		Nr   r   r   r   r   list_privileges/   s   z"ProcessManipulator.list_privilegesc           	      C   s  t d z| t}W n ty* } z|du rt d |	 W Y d}~nd}~ww g }| jj D ]}d}zz| jj	
td|}t dd||f  W n* tyw } zt dd|t|f  W Y d}~W |durr| jj	| q3d}~ww d}zYz
| jj|t}W n6 ty } z*t dd|t|f  W Y d}~W |dur| jj	| W |dur| jj	| q3d}~ww | ||}|| W |dur| jj	| n|dur| jj	| w w W |dur| jj	| q3|dur| jj	| w w |S )	zo
		iterates trough all available processes, fetches all process tokens, gets user information for all tokens
		z*[ProcessManipulator] Listing all tokens...F$Failed to obtain SE_DEBUG privilege!N   2[ProcessManipulator] Proc handle for PID %s is: %s=[ProcessManipulator] Failed to open process pid %s Reason: %sD[ProcessManipulator] Failed get token from process pid %s Reason: %s)r   r   r!   SE_DEBUG	Exceptionerrorr   psapiEnumProcesseskernel32OpenProcessPROCESS_QUERY_INFORMATIONlogstrCloseHandleadvapi32OpenProcessTokenTOKEN_MANIP_ACCESSget_token_infoappend)	r   forceresetoken_infosr   proc_handletoken_handletir   r   r   list_all_tokens6   sh   



z"ProcessManipulator.list_all_tokensc              
   C   s   d }zez| j j }| j jtd|}W n ty" } z|d }~ww z7z(| j j|t}| 	||W W |d ur?| j j
| W |d urL| j j
| S S  tyZ } z|d }~ww |d urg| j j
| w w |d urt| j j
| w w )NF)r   r.   GetCurrentProcessIdr/   r0   r*   r4   r5   r6   r7   r3   )r   r=   r   r;   r>   r   r   r   get_current_token_infoc   s6   z)ProcessManipulator.get_current_token_infoc                 C   sB   | j j|}| j j|}| j jd |\}}}t|||||S r   )r   r4   GetTokenInformation_sidConvertSidToStringSidLookupAccountSidr   )r   r>   r   ptr_sidsid_strnamer	   r   r   r   r   r7   w   s   z!ProcessManipulator.get_token_infoS-1-5-18c                 c   s   z|  t W n ty } ztd |d}~ww g }| jj D ]}d}zz| jj	t
d|}tdd||f  W n* tyh } ztdd|t|f  W Y d}~W |durc| jj| q$d}~ww d}	zsz
| jj|t}	W n6 ty } z*tdd|t|f  W Y d}~W |	dur| jj|	 W |dur| jj| q$d}~ww | jj|	}
| jj|
}||krtd | jjj|	|||d	}|V  W |	dur| jj|	 n|	dur| jj|	 w w W |dur| jj| q$|dur| jj| w w |S )
z
		iterates trough all available processes, fetches all process tokens, checks if sid matches for token, duplicates it and yields them
		also leaks a lot of handles, probably should be cleaned up TODO
		r$   NFr%   r&   r'   r(   z1[ProcessManipulator] Found token with target sid!)dwDesiredAccessImpersonationLevel	TokenType)r!   r)   r*   r   r+   r   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   rC   rD   r   DuplicateTokenEx)r   
target_sidrJ   rK   rL   r;   r<   r   r=   r>   rF   rG   cloned_tokenr   r   r   get_token_for_sid}   sx   




z$ProcessManipulator.get_token_for_sidc                 C   s   | j |ttB ttdD ]2}td z	| jj	| W n t
y5 } ztdd|  W Y d}~qd}~ww td  dS dS )zt
		assigns the token to the thread specified by threadid, if threadid is none then it will use the current thread
		rN   rJ   rK   rL   z7[ProcessManipulator] Setting token to current thread...r%   z,Failed changing the thread token. Reason: %sNz>[ProcessManipulator] Sucsessfully set token to current thread!)rP   TOKEN_QUERYTOKEN_IMPERSONATESecurityDelegationTokenImpersonationr   r   r   r4   SetThreadTokenr*   r1   )r   rN   tokenr;   r   r   r   assign_token_thread_sid   s   

z*ProcessManipulator.assign_token_thread_sidC:\Windows\system32\cmd.exeTc                 C   st   | j |tttdD ].}z
| jj|| W n ty/ } zt	dd|  W Y d}~q	d}~ww t
d  dS dS )zp
		Creates a new process with the token of the target SID 
		TODO: implement non-interactive functionality :(
		rQ   r%   z;Failed creating process with the token obtained. Reason: %sNz2[ProcessManipulator] Sucsessfully created process!)rP   TOKEN_ALL_ACCESSSecurityImpersonationrU   r   r4   CreateProcessWithToken_manipr*   r   r1   r   )r   rN   cmdlineinteractiverW   r;   r   r   r   create_process_for_sid   s   
z)ProcessManipulator.create_process_for_sidc                 C   s   |  d d S )NrI   )rX   r   r   r   r   	getsystem   s   zProcessManipulator.getsystemc                 C   s   | j j  d S r   )r   r4   RevertToSelfr   r   r   r   
dropsystem   s   zProcessManipulator.dropsystem)NN)F)rI   )rI   rY   T)r   r   r   __doc__r   r!   r"   r#   r@   rB   r7   rZ   r[   rP   rX   r_   r`   rb   r   r   r   r   r      s    



-
4
r   __main__)platformpypykatzr   -pypykatz.commons.winapi.local.localwindowsapir   !pypykatz.commons.winapi.constantsr   r   r   pmrB   r?   printr2   r   r   r   r   <module>   s    ?