o
    j6f                  	   @   s  d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZmZmZmZmZ d dlmZ d dlmZ edZG dd deZeZG d	d
 d
eZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3d Z4d!Z5d"Z6d#Z7d$Z8d%Z9dZ:dZ;d&Z<d'Z=d(Z>d)Z?d*Z@d+ZAd$ZBd"ZCd,ZDd-ZEd%ZFd#ZGd.ZHdZId ZJdZKd/ZLdZMd0ZNd1ZOd2ZPd3ZQd4ZRd5ZSdZTdZUd6ZVdZWdZXdZYdZZdZ[dZ\d7Z]d8Z^dZ_dZ`d Zad9Zbd3Zcd ZddZedZfdZgdZhdZidZjdZkd:Zld;Zmd8ZndZoG d<d= d=eZpG d>d? d?eZqG d@dA dAeZrG dBdC dCeZsG dDdE dEeZtG dFdG dGeZuG dHdI dIeZvG dJdK dKeZwG dLdM dMeZxG dNdO dOeZyG dPdQ dQeZzG dRdS dSeZ{G dTdU dUeZ|G dVdW dWeZ}G dXdY dYeZ~G dZd[ d[eZG d\d] d]eZG d^d_ d_eZG d`da daeZG dbdc dceZG ddde deeZG dfdg dgeZG dhdi dieZG djdk dkeZG dldm dmeZG dndo doeZG dpdq dqeZG drds dseZG dtdu dueZG dvdw dweZG dxdy dyeZG dzd{ d{eZG d|d} d}eZG d~d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZeefeefeefeefeefeefeefeefdZdd Ze	e	e)fddZdd Ze	e	e)e	fddZd e	d e	fddZe	dfddZdd Zdd Zdd ZdS )    )system_errors)	ULONGLONGUINTUSHORTLPWSTRDWORDULONGNULL)NDRCALL	NDRSTRUCTNDRUNION
NDRPOINTERNDRUniConformantArray)DCERPCException)uuidtup_to_bin)z$12345678-1234-ABCD-EF00-0123456789ABz1.0c                   @   s   e Zd ZdddZdd ZdS )DCERPCSessionErrorNc                 C   s   t | ||| d S )N)r   __init__)selferror_string
error_codepacket r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/impacket/dcerpc/v5/rprn.pyr   #   s   zDCERPCSessionError.__init__c                 C   sF   | j }|tjv rtj| d }tj| d }d| j ||f S d| j  S )Nr      z'RPRN SessionError: code: 0x%x - %s - %sz+RPRN SessionError: unknown error code: 0x%x)r   r   ERROR_MESSAGES)r   keyerror_msg_shorterror_msg_verboser   r   r   __str__&   s   

zDCERPCSessionError.__str__)NNN)__name__
__module____qualname__r   r   r   r   r   r   r   "   s    
r   c                   @      e Zd ZdeffZdS )PSTRING_HANDLEDataN)r   r    r!   STRING_HANDLEreferentr   r   r   r   r#   4       r#          i  i   i0        @   i  i  r      i  i  i  i  i   i   i   i   i   i   i   l        i   @i    i         i   i   i   i   iwwiwi   pi   i    i  @ i  p i   i   i   i   i @  i   i   i   i    c                   @   s   e Zd ZdZdd ZdS )PRINTER_HANDLE))r$   z20s=b""c                 C   s   | j du rdS dS )NTr+   r*   )_isNDR64)r   r   r   r   getAlignment   s   
zPRINTER_HANDLE.getAlignmentN)r   r    r!   	structurer2   r   r   r   r   r0      s    r0   c                   @      e Zd ZdZdS )
BYTE_ARRAYcNr   r    r!   itemr   r   r   r   r5          r5   c                   @   r"   )PBYTE_ARRAYr$   N)r   r    r!   r5   r&   r   r   r   r   r:      r'   r:   c                   @      e Zd ZdefdeffZdS )DEVMODE_CONTAINERcbBufpDevModeN)r   r    r!   r   r:   r3   r   r   r   r   r<          r<   c                   @   s:   e Zd ZdefdefdefdefdefdefdeffZdS )	SPLCLIENT_INFO_1dwSizepMachineName	pUserName
dwBuildNumdwMajorVersiondwMinorVersionwProcessorArchitectureN)r   r    r!   r   r   r   r3   r   r   r   r   r@      s    r@   c                   @   r"   )PSPLCLIENT_INFO_1r$   N)r   r    r!   r@   r&   r   r   r   r   rH      r'   rH   c                   @   r"   )SPLCLIENT_INFO_2notUsedN)r   r    r!   r   r3   r   r   r   r   rI      r'   rI   c                   @   r"   )PSPLCLIENT_INFO_2r$   N)r   r    r!   rI   r&   r   r   r   r   rK      r'   rK   c                   @   sL   e Zd Zdefdefdefdefdefdefdefdefdefd	eff
Zd
S )SPLCLIENT_INFO_3cbSizedwFlagsrB   rC   rD   rE   rF   rG   hSplPrinterN)	r   r    r!   r   r   r   r   r   r3   r   r   r   r   rL      s    rL   c                   @   r"   )PSPLCLIENT_INFO_3r$   N)r   r    r!   rL   r&   r   r   r   r   rP      r'   rP   c                   @   r"   )DRIVER_INFO_1pNameN)r   r    r!   r%   r3   r   r   r   r   rQ      r'   rQ   c                   @   r"   )PDRIVER_INFO_1r$   N)r   r    r!   rQ   r&   r   r   r   r   rS      r'   rS   c                   @   s4   e Zd ZdefdefdefdefdefdeffZdS )DRIVER_INFO_2cVersionrR   pEnvironmentpDriverPath	pDataFilepConfigFileN)r   r    r!   r   r   r3   r   r   r   r   rT          rT   c                   @   r"   )PDRIVER_INFO_2r$   N)r   r    r!   rT   r&   r   r   r   r   r[      r'   r[   c                   @   s(   e Zd ZdeffZdefdefdZdS )DRIVER_INFO_UNIONtagpNotUsedLevel2)r   r-   N)r   r    r!   r   	commonHdrrS   r[   unionr   r   r   r   r\     s    
r\   c                   @   r;   )DRIVER_CONTAINERLevel
DriverInfoN)r   r    r!   r   r\   r3   r   r   r   r   rb     r?   rb   c                   @   s.   e Zd ZdeffZdefdefdefdZdS )CLIENT_INFO_UNIONr]   pClientInfo1	pNotUsed1	pNotUsed2)r   r-      N)	r   r    r!   r   r`   rH   rK   rP   ra   r   r   r   r   re     s    
re   c                   @   r;   )SPLCLIENT_CONTAINERrc   
ClientInfoN)r   r    r!   r   re   r3   r   r   r   r   rj     r?   rj   c                   @   r4   )USHORT_ARRAYz<HNr7   r   r   r   r   rl   &  r9   rl   c                   @   r"   )PUSHORT_ARRAYr$   N)r   r    r!   rl   r&   r   r   r   r   rm   )  r'   rm   c                   @   s4   e Zd ZdefdefdefdefdefdeffZdS )RPC_V2_NOTIFY_OPTIONS_TYPEType	Reserved0	Reserved1	Reserved2CountpFieldsN)r   r    r!   r   r   rm   r3   r   r   r   r   rn   .  rZ   rn   c                   @   r"   )!PRPC_V2_NOTIFY_OPTIONS_TYPE_ARRAYr$   N)r   r    r!   rn   r&   r   r   r   r   ru   8  r'   ru   c                   @   s(   e Zd ZdefdefdefdeffZdS )RPC_V2_NOTIFY_OPTIONSVersionReservedrs   pTypesN)r   r    r!   r   ru   r3   r   r   r   r   rv   >      rv   c                   @   r"   )PRPC_V2_NOTIFY_OPTIONSr$   N)r   r    r!   rv   r&   r   r   r   r   r{   F  r'   r{   c                   @   s2   e Zd ZdZdefdefdefdefdeffZdS )RpcEnumPrintersr   FlagsNamerc   pPrinterEnumr=   N)r   r    r!   opnumr   r%   r:   r3   r   r   r   r   r|   P      r|   c                   @   (   e Zd ZdefdefdefdeffZdS )RpcEnumPrintersResponser   	pcbNeeded
pcReturned	ErrorCodeNr   r    r!   r:   r   r   r3   r   r   r   r   r   Z  rz   r   c                   @   s,   e Zd ZdZdefdefdefdeffZdS )RpcOpenPrinterr   pPrinterName	pDatatypepDevModeContainerAccessRequiredN)	r   r    r!   r   r%   r   r<   r   r3   r   r   r   r   r   b  s    r   c                   @   r;   )RpcOpenPrinterResponsepHandler   Nr   r    r!   r0   r   r3   r   r   r   r   r   k  r?   r   c                   @   s   e Zd ZdZdeffZdS )RpcClosePrinter   	phPrinterN)r   r    r!   r   r0   r3   r   r   r   r   r   r  s    r   c                   @   r;   )RpcClosePrinterResponser   r   Nr   r   r   r   r   r   x  r?   r   c                   @   s8   e Zd ZdZdefdefdefdefdefdeffZdS )	-RpcRemoteFindFirstPrinterChangeNotificationExA   hPrinterfdwFlags
fdwOptionspszLocalMachinedwPrinterLocalpOptionsN)	r   r    r!   r   r0   r   r   r{   r3   r   r   r   r   r     s    r   c                   @   r"   )5RpcRemoteFindFirstPrinterChangeNotificationExResponser   Nr   r    r!   r   r3   r   r   r   r   r     r'   r   c                   @   s2   e Zd ZdZdefdefdefdefdeffZ	dS )RpcOpenPrinterExE   r   r   r   r   pClientInfoN)
r   r    r!   r   r%   r   r<   r   rj   r3   r   r   r   r   r     r   r   c                   @   r;   )RpcOpenPrinterExResponser   r   Nr   r   r   r   r   r     r?   r   c                   @   2   e Zd ZdZdefdefdefdefdeffZdS )RpcEnumPrinterDrivers
   rR   rV   rc   pDriversr=   N	r   r    r!   r   r%   r   r   r:   r3   r   r   r   r   r     r   r   c                   @   r   )RpcEnumPrinterDriversResponser   r   r   r   Nr   r   r   r   r   r     rz   r   c                   @   r   )RpcGetPrinterDriverDirectory   rR   rV   rc   pDriverDirectoryr=   Nr   r   r   r   r   r     r   r   c                   @   s"   e Zd ZdefdefdeffZdS )$RpcGetPrinterDriverDirectoryResponser   r   r   Nr   r   r   r   r   r     s
    r   c                   @   s&   e Zd ZdZdefdefdeffZdS )RpcAddPrinterDriverExY   rR   pDriverContainerdwFileCopyFlagsN)r   r    r!   r   r%   rb   r   r3   r   r   r   r   r     s    r   c                   @   r"   )RpcAddPrinterDriverExResponser   Nr   r   r   r   r   r     r'   r   )r   r   r   r   r   r   r   r   c                 C   s(   | t kr| S | dd  dkr| d S | S )N )r	   )stringr   r   r   checkNullString  s
   r   c                 C   sJ   t  }t||d< ||d< |tu rt|d d< n||d< ||d< | |S )a  
    RpcOpenPrinter retrieves a handle for a printer, port, port monitor, print job, or print server.
    Full Documentation: https://msdn.microsoft.com/en-us/library/cc244808.aspx

    :param DCERPC_v5 dce: a connected DCE instance.
    :param string printerName: A string for a printer connection, printer object, server object, job object, port
    object, or port monitor object. This MUST be a Domain Name System (DNS), NetBIOS, Internet Protocol version 4
    (IPv4), Internet Protocol version 6 (IPv6), or Universal Naming Convention (UNC) name that remote procedure
    call (RPC) binds to, and it MUST uniquely identify a print server on the network.
    :param string pDatatype: A string that specifies the data type to be associated with the printer handle.
    :param DEVMODE_CONTAINER pDevModeContainer: A DEVMODE_CONTAINER structure. This parameter MUST adhere to the specification in
    DEVMODE_CONTAINER Parameters (section 3.1.4.1.8.1).
    :param int accessRequired: The access level that the client requires for interacting with the object to which a
    handle is being opened.

    :return: a RpcOpenPrinterResponse instance, raises DCERPCSessionError on error.
    r   r   r   r>   r   )r   r   r	   request)dceprinterNamer   r   accessRequiredr   r   r   r   hRpcOpenPrinter  s   
r   c                 C   s   t  }||d< | |S )a  
    RpcClosePrinter closes a handle to a printer object, server object, job object, or port object.
    Full Documentation: https://msdn.microsoft.com/en-us/library/cc244768.aspx

    :param DCERPC_v5 dce: a connected DCE instance.
    :param PRINTER_HANDLE phPrinter: A handle to a printer object, server object, job object, or port object.

    :return: a RpcClosePrinterResponse instance, raises DCERPCSessionError on error.
    r   )r   r   )r   r   r   r   r   r   hRpcClosePrinter
  s   

r   c                 C   sb   t  }t||d< ||d< |tu rt|d d< n||d< ||d< |tu r(td||d< | |S )a&  
    RpcOpenPrinterEx retrieves a handle for a printer, port, port monitor, print job, or print server
    Full Documentation: https://msdn.microsoft.com/en-us/library/cc244809.aspx

    :param DCERPC_v5 dce: a connected DCE instance.
    :param string printerName: A string for a printer connection, printer object, server object, job object, port
    object, or port monitor object. This MUST be a Domain Name System (DNS), NetBIOS, Internet Protocol version 4
    (IPv4), Internet Protocol version 6 (IPv6), or Universal Naming Convention (UNC) name that remote procedure
    call (RPC) binds to, and it MUST uniquely identify a print server on the network.
    :param string pDatatype: A string that specifies the data type to be associated with the printer handle.
    :param DEVMODE_CONTAINER pDevModeContainer: A DEVMODE_CONTAINER structure. This parameter MUST adhere to the specification in
    DEVMODE_CONTAINER Parameters (section 3.1.4.1.8.1).
    :param int accessRequired: The access level that the client requires for interacting with the object to which a
    handle is being opened.
    :param SPLCLIENT_CONTAINER pClientInfo: This parameter MUST adhere to the specification in SPLCLIENT_CONTAINER Parameters.

    :return: a RpcOpenPrinterExResponse instance, raises DCERPCSessionError on error.
    r   r   r   r>   r   zpClientInfo cannot be NULLr   )r   r   r	   	Exceptionr   )r   r   r   r   r   r   r   r   r   r   hRpcOpenPrinterEx  s   
r   c                 C   sT   t  }||d< ||d< ||d< ||d< |tu rtdt||d< ||d< | |S )a  
    creates a remote change notification object that monitors changes to printer objects and sends change notifications
    to a print client using either RpcRouterReplyPrinter (section 3.2.4.1.2) or RpcRouterReplyPrinterEx (section 3.2.4.1.4)
    Full Documentation: https://msdn.microsoft.com/en-us/library/cc244813.aspx

    :param DCERPC_v5 dce: a connected DCE instance.
    :param PRINTER_HANDLE hPrinter: A handle to a printer or server object.
    :param int fdwFlags: Flags that specify the conditions that are required for a change notification object to enter a signaled state.
    :param int fdwOptions: The category of printers for which change notifications are returned.
    :param string pszLocalMachine: A string that represents the name of the client computer.
    :param int dwPrinterLocal: An implementation-specific unique value that MUST be sufficient for the client to determine
    whether a call to RpcReplyOpenPrinter by the server is associated with the hPrinter parameter in this call.
    :param RPC_V2_NOTIFY_OPTIONS pOptions:  An RPC_V2_NOTIFY_OPTIONS structure that specifies printer or job members that the client listens to for notifications.

    :return: a RpcRemoteFindFirstPrinterChangeNotificationExResponse instance, raises DCERPCSessionError on error.
    r   r   r   r   zpszLocalMachine cannot be NULLr   r   )r   r	   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   .hRpcRemoteFindFirstPrinterChangeNotificationEx=  s   
r   c              
   C   s   t  }||d< ||d< t|d< ||d< d}z| | W n" ty> } zt|ddk r. | d }W Y d}~nd}~ww t  }||d< ||d< ||d< ||d	< d
| |d< | |S )a  
    RpcEnumPrinters enumerates available printers, print servers, domains, or print providers.
    Full Documentation: https://msdn.microsoft.com/en-us/library/cc244794.aspx

    :param DCERPC_v5 dce: a connected DCE instance.
    :param int flags: The types of print objects that this method enumerates. The value of this parameter is the
    result of a bitwise OR of one or more of the Printer Enumeration Flags (section 2.2.3.7).
    :param string name: NULL or a server name parameter as specified in Printer Server Name Parameters (section 3.1.4.1.4).
    :param level: The level of printer information structure.

    :return: a RpcEnumPrintersResponse instance, raises DCERPCSessionError on error.
    r}   r~   r   rc   r   ERROR_INSUFFICIENT_BUFFERr   Nr=      a)r|   r	   r   r   strfind
get_packet)r   flagsnamelevelr   bytesNeededer   r   r   hRpcEnumPrinters[  s*   
r   c                 C   s,   t  }t||d< ||d< ||d< | |S )a~  
    RpcAddPrinterDriverEx installs a printer driver on the print server
    Full Documentation: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/b96cc497-59e5-4510-ab04-5484993b259b

    :param DCERPC_v5 dce: a connected DCE instance.
    :param pName
    :param pDriverContainer
    :param dwFileCopyFlags

    :return: raises DCERPCSessionError on error.
    rR   r   r   )r   r   r   )r   rR   r   r   r   r   r   r   hRpcAddPrinterDriverEx  s
   
r   c              
   C      t  }t||d< ||d< ||d< t|d< d|d< z| | W n" tyB } zt|ddk r2 | d }W Y d	}~nd	}~ww t  }t||d< ||d< ||d< d
| |d< ||d< | |S )a  
    RpcEnumPrinterDrivers enumerates the printer drivers installed on a specified print server.
    Full Documentation: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/857d00ac-3682-4a0d-86ca-3d3c372e5e4a

    :param DCERPC_v5 dce: a connected DCE instance.
    :param pName
    :param pEnvironment
    :param Level
    :param pDrivers
    :param cbBuf
    :param pcbNeeded
    :param pcReturned

    :return: raises DCERPCSessionError on error.
    rR   rV   rc   r   r   r=   r   r   Nr   )r   r   r	   r   r   r   r   r   r   rR   rV   rc   r   r   r   r   r   r   hRpcEnumPrinterDrivers  s*   
r   c              
   C   r   )a  
    RpcGetPrinterDriverDirectory retrieves the path of the printer driver directory.
    Full Documentation: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/9df11cf4-4098-4852-ad72-d1f75a82bffe

    :param DCERPC_v5 dce: a connected DCE instance.
    :param pName
    :param pEnvironment
    :param Level
    :param pDriverDirectory
    :param cbBuf
    :param pcbNeeded

    :return: raises DCERPCSessionError on error.
    rR   rV   rc   r   r   r=   r   r   Nr   )r   r   r	   r   r   r   r   r   r   r   r   r   hRpcGetPrinterDriverDirectory  s*   
r   N)impacketr   impacket.dcerpc.v5.dtypesr   r   r   r   r   r   r	   impacket.dcerpc.v5.ndrr
   r   r   r   r   impacket.dcerpc.v5.rpcrtr   impacket.uuidr   MSRPC_UUID_RPRNr   r%   r#   JOB_ACCESS_ADMINISTERJOB_ACCESS_READJOB_EXECUTEJOB_READ	JOB_WRITEJOB_ALL_ACCESSPRINTER_ACCESS_ADMINISTERPRINTER_ACCESS_USEPRINTER_ACCESS_MANAGE_LIMITEDPRINTER_ALL_ACCESSPRINTER_EXECUTEPRINTER_READPRINTER_WRITESERVER_ACCESS_ADMINISTERSERVER_ACCESS_ENUMERATESERVER_ALL_ACCESSSERVER_EXECUTESERVER_READSERVER_WRITESPECIFIC_RIGHTS_ALLSTANDARD_RIGHTS_ALLSTANDARD_RIGHTS_EXECUTESTANDARD_RIGHTS_READSTANDARD_RIGHTS_REQUIREDSTANDARD_RIGHTS_WRITESYNCHRONIZEDELETEREAD_CONTROL	WRITE_DACWRITE_OWNERGENERIC_READGENERIC_WRITEGENERIC_EXECUTEGENERIC_ALLPRINTER_CHANGE_SET_PRINTERPRINTER_CHANGE_DELETE_PRINTERPRINTER_CHANGE_PRINTERPRINTER_CHANGE_ADD_JOBPRINTER_CHANGE_SET_JOBPRINTER_CHANGE_DELETE_JOBPRINTER_CHANGE_WRITE_JOBPRINTER_CHANGE_JOB!PRINTER_CHANGE_SET_PRINTER_DRIVERPRINTER_CHANGE_TIMEOUTPRINTER_CHANGE_ALLPRINTER_CHANGE_ALL_2!PRINTER_CHANGE_ADD_PRINTER_DRIVER$PRINTER_CHANGE_DELETE_PRINTER_DRIVERPRINTER_CHANGE_PRINTER_DRIVERPRINTER_CHANGE_ADD_FORMPRINTER_CHANGE_DELETE_FORMPRINTER_CHANGE_SET_FORMPRINTER_CHANGE_FORMPRINTER_CHANGE_ADD_PORTPRINTER_CHANGE_CONFIGURE_PORTPRINTER_CHANGE_DELETE_PORTPRINTER_CHANGE_PORT"PRINTER_CHANGE_ADD_PRINT_PROCESSOR%PRINTER_CHANGE_DELETE_PRINT_PROCESSORPRINTER_CHANGE_PRINT_PROCESSORPRINTER_CHANGE_ADD_PRINTER(PRINTER_CHANGE_FAILED_CONNECTION_PRINTERPRINTER_CHANGE_SERVERPRINTER_ENUM_LOCALPRINTER_ENUM_CONNECTIONSPRINTER_ENUM_NAMEPRINTER_ENUM_REMOTEPRINTER_ENUM_SHAREDPRINTER_ENUM_NETWORKPRINTER_ENUM_EXPANDPRINTER_ENUM_CONTAINERPRINTER_ENUM_ICON1PRINTER_ENUM_ICON2PRINTER_ENUM_ICON3PRINTER_ENUM_ICON8PRINTER_ENUM_HIDEPRINTER_NOTIFY_CATEGORY_2DPRINTER_NOTIFY_CATEGORY_ALLPRINTER_NOTIFY_CATEGORY_3DAPD_STRICT_UPGRADEAPD_STRICT_DOWNGRADEAPD_COPY_ALL_FILESAPD_COPY_NEW_FILESAPD_COPY_FROM_DIRECTORYAPD_DONT_COPY_FILES_TO_CLUSTERAPD_COPY_TO_ALL_SPOOLERSAPD_INSTALL_WARNED_DRIVERAPD_RETURN_BLOCKING_STATUS_CODEr0   r5   r:   r<   r@   rH   rI   rK   rL   rP   rQ   rS   rT   r[   r\   rb   re   rj   rl   rm   rn   ru   rv   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   OPNUMSr   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s>  $		



	

	
		
$
$)