Пример #1
0
 def writeSuppressOutput(self, stream: BytesIO, pdu):
     Uint8.pack(int(pdu.allowDisplayUpdates), stream)
     stream.write(b"\x00" * 3)
     Uint16LE.pack(pdu.left, stream)
     Uint16LE.pack(pdu.top, stream)
     Uint16LE.pack(pdu.right, stream)
     Uint16LE.pack(pdu.bottom, stream)
Пример #2
0
 def writeData(self, stream: BytesIO, pdu: X224DataPDU):
     """
     Write a Data PDU onto the provided stream
     """
     header = (pdu.header << 4) | int(pdu.roa)
     stream.write(Uint8.pack(header))
     stream.write(Uint8.pack(int(pdu.eot) << 7))
Пример #3
0
 def sendMessage(self, data: bytes, messageType: PlayerMessageType,
                 timeStamp: int):
     stream = BytesIO()
     Uint8.pack(messageType, stream)
     Uint64LE.pack(timeStamp, stream)
     stream.write(data)
     self.previous.send(stream.getvalue())
Пример #4
0
 def writeError(self, stream: BytesIO, pdu: X224ErrorPDU):
     """
     Write an error PDU onto the provided stream
     """
     stream.write(Uint8.pack(pdu.header))
     stream.write(Uint16LE.pack(pdu.destination))
     stream.write(Uint8.pack(pdu.cause))
Пример #5
0
    def write(self, pdu: MCSPDU) -> bytes:
        """
        Encode an MCS PDU into raw bytes
        :param pdu: the MCSPDU to encode
        :return: The raw bytes to send
        """
        if pdu.header not in self.writers:
            raise UnknownPDUTypeError(
                "Trying to write unknown MCS PDU type %s" % pdu.header,
                pdu.header)

        stream = BytesIO()

        if pdu.header in [
                MCSPDUType.CONNECT_INITIAL, MCSPDUType.CONNECT_RESPONSE
        ]:
            stream.write(
                Uint8.pack(ber.Class.BER_CLASS_APPL | ber.PC.BER_CONSTRUCT
                           | ber.Tag.BER_TAG_MASK))
            stream.write(Uint8.pack(pdu.header))
        else:
            stream.write(
                Uint8.pack((pdu.header << 2) | self.headerOptions[pdu.header]))

        self.writers[pdu.header](stream, pdu)
        return stream.getvalue()
Пример #6
0
 def writeMouseEvent(self, event):
     rawData = BytesIO()
     Uint8.pack(event.rawHeaderByte, rawData)
     Uint16LE.pack(event.pointerFlags, rawData)
     Uint16LE.pack(event.mouseX, rawData)
     Uint16LE.pack(event.mouseY, rawData)
     return rawData.getvalue()
Пример #7
0
 def writeMouseEvent(self, event: FastPathMouseEvent) -> bytes:
     stream = BytesIO()
     Uint8.pack(event.rawHeaderByte, stream)
     Uint16LE.pack(event.pointerFlags, stream)
     Uint16LE.pack(event.mouseX, stream)
     Uint16LE.pack(event.mouseY, stream)
     return stream.getvalue()
Пример #8
0
    def writeFileDescription(self, description: PlayerFileDescription,
                             stream: BytesIO):
        path = description.path.encode()

        Uint32LE.pack(len(path), stream)
        stream.write(path)
        Uint8.pack(int(description.isDirectory), stream)
Пример #9
0
    def writeFileBothDirectoryInformation(self, information: List[FileBothDirectoryInformation], stream: BytesIO):
        dataList: [bytes] = []

        for info in information:
            substream = BytesIO()
            fileName = info.fileName.encode("utf-16le")
            shortName = info.shortName.encode("utf-16le")

            Uint32LE.pack(info.fileIndex, substream)
            Uint64LE.pack(info.creationTime, substream)
            Uint64LE.pack(info.lastAccessTime, substream)
            Uint64LE.pack(info.lastWriteTime, substream)
            Uint64LE.pack(info.lastChangeTime, substream)
            Uint64LE.pack(info.endOfFilePosition, substream)
            Uint64LE.pack(info.allocationSize, substream)
            Uint32LE.pack(info.fileAttributes, substream)
            Uint32LE.pack(len(fileName), substream)
            Uint32LE.pack(info.eaSize, substream)
            Uint8.pack(len(shortName), substream)
            # stream.write(b"\x00") # reserved
            substream.write(shortName.ljust(24, b"\x00")[: 24])
            substream.write(fileName)

            dataList.append(substream.getvalue())

        self.writeFileInformationList(dataList, stream)
Пример #10
0
    def writeBody(self, stream: BytesIO, pdu: FastPathPDU):
        bodyStream = BytesIO()
        SignedFastPathParser.writeBody(self, bodyStream, pdu)
        body = bodyStream.getvalue()

        Uint16LE.pack(0x10, stream)
        Uint8.pack(FIPSVersion.TSFIPS_VERSION1, stream)
        Uint8.pack(self.crypter.getPadLength(self.eventData), stream)
        stream.write(body)
Пример #11
0
 def writeConnectionPDU(self, stream: BytesIO, header: X224PDUType,
                        destination: int, source: int, options: int):
     """
     Write a connection PDU (connectionRequest/connectionConfirm/disconnectRequest) in the provided byte stream.
     """
     stream.write(Uint8.pack(header))
     stream.write(Uint16BE.pack(destination))
     stream.write(Uint16BE.pack(source))
     stream.write(Uint8.pack(options))
Пример #12
0
    def writeHeader(self, stream: BytesIO, pdu: FastPathPDU):
        header = (pdu.header & 0xc0) | self.getHeaderFlags()
        eventCount = len(pdu.events)

        if eventCount <= 15 and self.mode == ParserMode.CLIENT:
            header |= eventCount << 2

        Uint8.pack(header, stream)
        self.writeLength(stream, pdu)
Пример #13
0
    def writeUnicodeEvent(self, event: FastPathUnicodeEvent):
        stream = BytesIO()
        Uint8.pack(
            int(event.released) |
            (FastPathInputType.FASTPATH_INPUT_EVENT_UNICODE << 5), stream)

        if isinstance(event.text, bytes):
            stream.write(event.text[:2].ljust(2, b"\x00"))
        elif isinstance(event.text, str):
            stream.write(event.text[:1].ljust(1, "\x00").encode("utf-16le"))

        return stream.getvalue()
Пример #14
0
 def write(self, pdu: DynamicChannelPDU) -> bytes:
     stream = BytesIO()
     header = pdu.cbid
     header |= pdu.sp << 2
     header |= pdu.cmd << 4
     Uint8.pack(header, stream)
     if isinstance(pdu, CreateResponsePDU):
         self.writeChannelId(stream, pdu.cbid, pdu.channelId)
         Uint32LE.pack(pdu.creationStatus, stream)
     else:
         raise NotImplementedError()
     return stream.getvalue()
Пример #15
0
    def writeShareDataHeader(self, stream: BytesIO, header, dataLength):
        substream = BytesIO()
        substream.write(Uint32LE.pack(header.shareID))
        substream.write(b"\x00")
        substream.write(Uint8.pack(header.streamID))
        substream.write(Uint16LE.pack(header.uncompressedLength))
        substream.write(Uint8.pack(header.subtype))
        substream.write(Uint8.pack(header.compressedType))
        substream.write(Uint16LE.pack(header.compressedLength))
        substream = substream.getvalue()

        self.writeShareControlHeader(stream, header, dataLength + len(substream))
        stream.write(substream)
Пример #16
0
    def write(self, pdu):
        """
        Write a negotiation request.
        :param pdu: the request PDU.
        :type pdu: NegotiationRequestPDU
        :return: str
        """
        stream = BytesIO()

        if pdu.cookie is not None:
            stream.write(pdu.cookie + b"\r\n")

        if pdu.flags is not None and pdu.requestedProtocols is not None:
            Uint8.pack(NegotiationType.TYPE_RDP_NEG_REQ, stream)
            Uint8.pack(pdu.flags, stream)
            Uint16LE.pack(8, stream)
            Uint32LE.pack(pdu.requestedProtocols, stream)

            if pdu.correlationFlags is not None and pdu.correlationID is not None:
                Uint8.pack(NegotiationType.TYPE_RDP_CORRELATION_INFO, stream)
                Uint8.pack(pdu.correlationFlags, stream)
                Uint16LE.pack(36, stream)
                stream.write(pdu.correlationID)
                stream.write(b"\x00" * 16)

        return stream.getvalue()
Пример #17
0
    def writeDirectoryControlRequest(self, pdu: Union[DeviceIORequestPDU, DeviceQueryDirectoryRequestPDU], stream: BytesIO):
        self.minorFunctionsForParsingResponse[pdu.completionID] = pdu.minorFunction

        if pdu.minorFunction == MinorFunction.IRP_MN_NOTIFY_CHANGE_DIRECTORY:
            stream.write(pdu.payload)
        else:
            self.informationClassForParsingResponse[pdu.completionID] = pdu.informationClass
            path = (pdu.path + "\x00").encode("utf-16le")

            Uint32LE.pack(pdu.informationClass, stream)
            Uint8.pack(pdu.initialQuery, stream)
            Uint32LE.pack(len(path), stream)
            stream.write(b"\x00" * 23)
            stream.write(path)
Пример #18
0
    def writePersistentCacheKeys(self, s: BytesIO, pdu: PersistentCacheKeysPDU):
        # Only send the first PDU with an empty list and drop the rest.
        # TODO: Find a way to cleanly drop the entire packet instead.
        Uint16LE.pack(0, s)
        Uint16LE.pack(0, s)
        Uint16LE.pack(0, s)
        Uint16LE.pack(0, s)
        Uint16LE.pack(0, s)

        Uint16LE.pack(0, s)
        Uint16LE.pack(0, s)
        Uint16LE.pack(0, s)
        Uint16LE.pack(0, s)
        Uint16LE.pack(0, s)
        Uint8.pack(pdu.mask, s)
        s.write(b'\x00'*3)
Пример #19
0
    def write(self, pdu: X224PDU) -> bytes:
        """
        Encode the provided X224 pdu into a byte stream.
        :return: The bytes to send to the previous layer
        """

        stream = BytesIO()

        if pdu.header == X224PDUType.X224_TPDU_DATA:
            length = 2
        elif pdu.header in [
                X224PDUType.X224_TPDU_CONNECTION_REQUEST,
                X224PDUType.X224_TPDU_CONNECTION_CONFIRM,
                X224PDUType.X224_TPDU_DISCONNECT_REQUEST
        ]:
            length = len(pdu.payload) + 6
        elif pdu.header == X224PDUType.X224_TPDU_ERROR:
            length = len(pdu.payload) + 4
        else:
            raise UnknownPDUTypeError(
                "Trying to write unknown X224 PDU type: %s" %
                (pdu.header if pdu.header in X224PDUType else hex(pdu.header)),
                pdu.header)

        stream.write(Uint8.pack(length))
        self.writers[pdu.header](stream, pdu)
        stream.write(pdu.payload)
        return stream.getvalue()
Пример #20
0
    def writeClientCoreData(self, stream: BytesIO, core: ClientCoreData):
        stream.write(Uint32LE.pack(core.version))
        stream.write(Uint16LE.pack(core.desktopWidth))
        stream.write(Uint16LE.pack(core.desktopHeight))
        stream.write(Uint16LE.pack(core.colorDepth))
        stream.write(Uint16LE.pack(core.sasSequence))
        stream.write(Uint32LE.pack(core.keyboardLayout))
        stream.write(Uint32LE.pack(core.clientBuild))
        stream.write(encodeUTF16LE(core.clientName).ljust(32, b"\x00")[: 32])
        stream.write(Uint32LE.pack(core.keyboardType))
        stream.write(Uint32LE.pack(core.keyboardSubType))
        stream.write(Uint32LE.pack(core.keyboardFunctionKey))
        stream.write(core.imeFileName)

        try:
            stream.write(Uint16LE.pack(core.postBeta2ColorDepth))
            stream.write(Uint16LE.pack(core.clientProductId))
            stream.write(Uint32LE.pack(core.serialNumber))
            stream.write(Uint16LE.pack(core.highColorDepth))
            stream.write(Uint16LE.pack(core.supportedColorDepths))
            stream.write(Uint16LE.pack(core.earlyCapabilityFlags))
            stream.write(encodeUTF16LE(core.clientDigProductId).ljust(64, b"\x00")[: 64])
            stream.write(Uint8.pack(core.connectionType))
            stream.write(b"\x00")
            stream.write(Uint32LE.pack(core.serverSelectedProtocol))
            stream.write(Uint32LE.pack(core.desktopPhysicalWidth))
            stream.write(Uint32LE.pack(core.desktopPhysicalHeight))
            stream.write(Uint16LE.pack(core.desktopOrientation))
            stream.write(Uint32LE.pack(core.desktopScaleFactor))
            stream.write(Uint32LE.pack(core.deviceScaleFactor))
        except struct.error:
            # We tried to write an optional field which was not present. Stop writing beyond this point.
            pass
Пример #21
0
    def write(self, pdu):
        """
        Write a negotiation response.
        :param pdu: the response PDU.
        :type pdu: NegotiationResponsePDU
        :return: str
        """
        stream = BytesIO()

        if pdu.flags is not None and pdu.selectedProtocols is not None:
            Uint8.pack(NegotiationType.TYPE_RDP_NEG_RSP, stream)
            Uint8.pack(pdu.flags, stream)
            Uint16LE.pack(8, stream)
            Uint32LE.pack(pdu.selectedProtocols, stream)

        return stream.getvalue()
Пример #22
0
 def writeChannelId(self, stream: BytesIO, cbid: int, channelId: int):
     if cbid == CbId.ONE_BYTE:
         return Uint8.pack(channelId, stream)
     elif cbid == CbId.TWO_BYTE:
         return Uint16LE.pack(channelId, stream)
     elif cbid == CbId.FOUR_BYTES:
         return Uint16LE.pack(channelId, stream)
     else:
         raise ValueError(f"Invalid channel id length: {cbid}")
Пример #23
0
    def write(self, pdu: LicensingPDU) -> bytes:
        """
        Encode a RDPLicensingPDU into a byte stream to send to the previous layer.
        """
        stream = BytesIO()
        stream.write(Uint8.pack(pdu.header))
        stream.write(Uint8.pack(pdu.flags))
        substream = BytesIO()

        if isinstance(pdu, LicenseErrorAlertPDU):
            self.writeErrorAlert(substream, pdu)
        else:
            raise UnknownPDUTypeError(
                "Trying to write unknown RDP Licensing PDU: {}".format(
                    pdu.header), pdu.header)

        stream.write(Uint16LE.pack(len(substream.getvalue()) + 4))
        stream.write(substream.getvalue())
        return stream.getvalue()
Пример #24
0
    def writeGeneralCapability(self, capability: GeneralCapability, stream: BytesIO):
        """
        https://msdn.microsoft.com/en-us/library/cc240549.aspx
        """
        substream = BytesIO()
        Uint16LE.pack(capability.capabilityType, stream)
        Uint16LE.pack(capability.majorType, substream)
        Uint16LE.pack(capability.minorType, substream)
        Uint16LE.pack(capability.protocolVersion, substream)
        substream.write(b"\00" * 2)  # pad2octetsA
        Uint16LE.pack(capability.generalCompressionTypes, substream)
        Uint16LE.pack(capability.extraFlags, substream)
        Uint16LE.pack(capability.updateCapabilityFlag, substream)
        Uint16LE.pack(capability.remoteUnshareFlag, substream)
        Uint16LE.pack(capability.generalCompressionLevel, substream)
        Uint8.pack(capability.refreshRectSupport, substream)
        Uint8.pack(capability.suppressOutputSupport, substream)

        Uint16LE.pack(len(substream.getvalue()) + 4, stream)
        stream.write(substream.getvalue())
Пример #25
0
    def write(self, pdu: TPKTPDU) -> bytes:
        """
        Encode a TPKTPDU into bytes to send on the network.
        """

        stream = BytesIO()
        stream.write(Uint8.pack(pdu.header))
        stream.write(b"\x00")
        stream.write(Uint16BE.pack(len(pdu.payload) + 4))
        stream.write(pdu.payload)

        return stream.getvalue()
Пример #26
0
    def writeBitmapCapability(self, capability: BitmapCapability, stream: BytesIO):
        substream = BytesIO()
        Uint16LE.pack(capability.capabilityType, stream)

        Uint16LE.pack(capability.preferredBitsPerPixel, substream)
        Uint16LE.pack(capability.receive1BitPerPixel, substream)
        Uint16LE.pack(capability.receive4BitsPerPixel, substream)
        Uint16LE.pack(capability.receive8BitsPerPixel, substream)
        Uint16LE.pack(capability.desktopWidth, substream)
        Uint16LE.pack(capability.desktopHeight, substream)
        substream.write(b"\x00"*2)  # pad2octets
        Uint16LE.pack(capability.desktopResizeFlag, substream)
        Uint16LE.pack(capability.bitmapCompressionFlag, substream)
        Uint8.pack(capability.highColorFlags, substream)
        Uint8.pack(capability.drawingFlags, substream)
        Uint16LE.pack(capability.multipleRectangleSupport, substream)

        substream.write(b"\x00" * 2)  # pad2octetsB

        Uint16LE.pack(len(substream.getvalue()) + 4, stream)
        stream.write(substream.getvalue())
Пример #27
0
    def write(self, event: FastPathOutputUpdateEvent):

        stream = BytesIO()
        Uint8.pack(event.header, stream)

        if event.compressionFlags is not None:
            Uint8.pack(event.compressionFlags if event.compressionFlags else 0, stream)

        updateStream = BytesIO()

        if isinstance(event, FastPathBitmapEvent):
            self.writeBitmapEvent(updateStream, event)
        elif isinstance(event, FastPathOrdersEvent):
            self.writeOrdersEvent(updateStream, event)
        else:
            # Means it's simply a FastPathOutputUpdateEvent, this needs to be the last elif.
            updateStream.write(event.payload)

        updateData = updateStream.getvalue()
        Uint16LE.pack(len(updateData), stream)
        stream.write(updateData)
        return stream.getvalue()
Пример #28
0
    def write(self, pdu: X224PDU) -> bytes:
        """
        Encode the provided X224 pdu into a byte stream.
        :return: The bytes to send to the previous layer
        """

        stream = BytesIO()
        stream.write(Uint8.pack(pdu.length))

        if pdu.header not in self.writers:
            raise UnknownPDUTypeError("Trying to write unknown X224 PDU type: %s" % (pdu.header if pdu.header in X224PDUType else hex(pdu.header)), pdu.header)

        self.writers[pdu.header](stream, pdu)
        stream.write(pdu.payload)
        return stream.getvalue()
Пример #29
0
 def writeScanCodeEvent(self, event: FastPathScanCodeEvent) -> bytes:
     stream = BytesIO()
     Uint8.pack(event.rawHeaderByte | int(event.isReleased), stream)
     Uint8.pack(event.scanCode, stream)
     return stream.getvalue()
Пример #30
0
    def writeBody(self, stream: BytesIO, pdu: FastPathPDU):
        eventCount = len(pdu.events)

        if self.mode == ParserMode.CLIENT and eventCount > 15:
            Uint8.pack(eventCount, stream)