def __read__(self, s): """ @summary: Read composite type Call read on each ordered sub-type And check read length parameter If an error occurred rollback type already read @param s: Stream @raise InvalidSize: if stream is greater than readLen parameter """ readLen = 0 for name in self._typeName: try: s.readType(self.__dict__[name]) readLen += sizeof(self.__dict__[name]) #read is ok but read out of bound if not self._readLen is None and readLen > self._readLen.value: #roll back s.pos -= sizeof(self.__dict__[name]) #and notify if not optional if not self.__dict__[name]._optional: raise InvalidSize("Impossible to read type %s : read length is too small"%(self.__class__)) except Exception as e: log.error("Error during read %s::%s"%(self.__class__, name)) #roll back already read for tmpName in self._typeName: if tmpName == name: break s.pos -= sizeof(self.__dict__[tmpName]) raise e if not self._readLen is None and readLen < self._readLen.value: log.debug("Still have correct data in packet %s, read %s bytes as padding"%(self.__class__, self._readLen.value - readLen)) s.read(self._readLen.value - readLen)
def onUpdate(self, width, height, x, y, pixelFormat, encoding, data): """ Implement RFBClientObserver interface @param width: width of new image @param height: height of new image @param x: x position of new image @param y: y position of new image @param pixelFormat: pixefFormat structure in rfb.message.PixelFormat @param encoding: encoding type rfb.message.Encoding @param data: image data in accordance with pixel format and encoding """ imageFormat = qtImageFormatFromRFBPixelFormat(pixelFormat) if imageFormat is None: if self._dbm: self._dbm.open_connection() self._obj.error_state = True self._dbm.update_vnc_rdp_object(self._obj) self._dbm.close() log.error("Receive image in bad format") return image = QtGui.QImage(data, width, height, imageFormat) with QtGui.QPainter(self._buffer) as qp: qp.drawImage(x, y, image, 0, 0, width, height) self._complete = True self._controller.close()
def recvData(self, data): """ @summary: Main receive method @param data: {Stream} """ opcode = UInt8() data.readType(opcode) if self.readMCSPDUHeader(opcode.value, DomainMCSPDU.DISCONNECT_PROVIDER_ULTIMATUM): log.info("MCS DISCONNECT_PROVIDER_ULTIMATUM") self._transport.close() return #client case elif not self.readMCSPDUHeader(opcode.value, self._receiveOpcode): raise InvalidExpectedDataException( "Invalid expected MCS opcode receive data") #server user id per.readInteger16(data, Channel.MCS_USERCHANNEL_BASE) channelId = per.readInteger16(data) per.readEnumerates(data) per.readLength(data) #channel id doesn't match a requested layer if not self._channels.has_key(channelId): log.error("receive data for an unconnected layer") return self._channels[channelId].recv(data)
def recvData(self, data): """ @summary: Main receive method @param data: {Stream} """ opcode = UInt8() data.readType(opcode) if self.readMCSPDUHeader(opcode.value, DomainMCSPDU.DISCONNECT_PROVIDER_ULTIMATUM): log.info("MCS DISCONNECT_PROVIDER_ULTIMATUM") self._transport.close() return #client case elif not self.readMCSPDUHeader(opcode.value, self._receiveOpcode): raise InvalidExpectedDataException("Invalid expected MCS opcode receive data") #server user id per.readInteger16(data, Channel.MCS_USERCHANNEL_BASE) channelId = per.readInteger16(data) per.readEnumerates(data) per.readLength(data) #channel id doesn't match a requested layer if not self._channels.has_key(channelId): log.error("receive data for an unconnected layer") return self._channels[channelId].recv(data)
def sendConnectionConfirm(self): """ @summary: Write connection confirm message Start TLS connection Next state is recvData @see : http://msdn.microsoft.com/en-us/library/cc240501.aspx """ message = ServerConnectionConfirm( ) # flags should be 0xf and result should be 0x8 message.protocolNeg.flag.value = 0xf message.protocolNeg.code.value = NegociationType.TYPE_RDP_NEG_RSP message.protocolNeg.selectedProtocol.value = self._selectedProtocol self._transport.send(message) if self._selectedProtocol == Protocols.PROTOCOL_SSL: log.error('error in protocol selection.SSL chosen') return if self._selectedProtocol == Protocols.PROTOCOL_HYBRID: log.debug('hybrid chosen') else: log.error('selected protocol ', self._selectedProtocol) #_transport is TPKT and transport is TCP layer of twisted ctx = (ServerTLSContext(self._serverPrivateKeyFileName, self._serverCertificateFileName)) self._transport.startTLS(ctx) #connection is done send to presentation self.setNextState(self.recvData) self._presentation.connect()
def __write__(self, s): """ @summary: Write all sub-type handle by __setattr__ function Call write on each ordered sub type @param s: Stream """ for name in self._typeName: try: s.writeType(self.__dict__[name]) except Exception as e: log.error("Error during write %s::%s" % (self.__class__, name)) raise e
def __write__(self, s): """ @summary: Write all sub-type handle by __setattr__ function Call write on each ordered sub type @param s: Stream """ for name in self._typeName: try: s.writeType(self.__dict__[name]) except Exception as e: log.error("Error during write %s::%s"%(self.__class__, name)) raise e
def recvServerOrder(self, data): """ @summary: Read order receive from server Main function for bitmap update from server to client @param data: Stream that contains well formed packet """ packetType = UInt8() data.readType(packetType) if packetType.value == 0: self.expect(3, self.recvFrameBufferUpdateHeader) elif packetType.value == 2: self._clientListener.onBell() elif packetType.value == 3: self.expect(7, self.recvServerCutTextHeader) else: log.error("Unknown message type %s" % packetType.value)
def recvServerOrder(self, data): """ @summary: Read order receive from server Main function for bitmap update from server to client @param data: Stream that contains well formed packet """ packetType = UInt8() data.readType(packetType) if packetType.value == 0: self.expect(3, self.recvFrameBufferUpdateHeader) elif packetType.value == 2: self._clientListener.onBell() elif packetType.value == 3: self.expect(7, self.recvServerCutTextHeader) else: log.error("Unknown message type %s"%packetType.value)
def readDataPDU(self, dataPDU): """ @summary: read a data PDU object @param dataPDU: DataPDU object """ if dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SET_ERROR_INFO_PDU: errorMessage = "Unknown code %s"%hex(dataPDU.pduData.errorInfo.value) if data.ErrorInfo._MESSAGES_.has_key(dataPDU.pduData.errorInfo): errorMessage = data.ErrorInfo._MESSAGES_[dataPDU.pduData.errorInfo] log.error("INFO PDU : %s"%errorMessage) elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SHUTDOWN_DENIED: #may be an event to ask to user self._transport.close() elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_UPDATE: self.readUpdateDataPDU(dataPDU.pduData)
def RDPBitmapToQtImage(width, height, bitsPerPixel, isCompress, data): """ @summary: Bitmap transformation to Qt object @param width: width of bitmap @param height: height of bitmap @param bitsPerPixel: number of bit per pixel @param isCompress: use RLE compression @param data: bitmap data """ image = None #allocate if bitsPerPixel == 15: if isCompress: buf = bytearray(width * height * 2) rle.bitmap_decompress(buf, width, height, data, 2) image = QtGui.QImage(buf, width, height, QtGui.QImage.Format_RGB555) else: image = QtGui.QImage(data, width, height, QtGui.QImage.Format_RGB555).transformed(QtGui.QMatrix(1.0, 0.0, 0.0, -1.0, 0.0, 0.0)) elif bitsPerPixel == 16: if isCompress: buf = bytearray(width * height * 2) rle.bitmap_decompress(buf, width, height, data, 2) image = QtGui.QImage(buf, width, height, QtGui.QImage.Format_RGB16) else: image = QtGui.QImage(data, width, height, QtGui.QImage.Format_RGB16).transformed(QtGui.QMatrix(1.0, 0.0, 0.0, -1.0, 0.0, 0.0)) elif bitsPerPixel == 24: if isCompress: buf = bytearray(width * height * 3) rle.bitmap_decompress(buf, width, height, data, 3) image = QtGui.QImage(buf, width, height, QtGui.QImage.Format_RGB888) else: image = QtGui.QImage(data, width, height, QtGui.QImage.Format_RGB888).transformed(QtGui.QMatrix(1.0, 0.0, 0.0, -1.0, 0.0, 0.0)) elif bitsPerPixel == 32: if isCompress: buf = bytearray(width * height * 4) rle.bitmap_decompress(buf, width, height, data, 4) image = QtGui.QImage(buf, width, height, QtGui.QImage.Format_RGB32) else: image = QtGui.QImage(data, width, height, QtGui.QImage.Format_RGB32).transformed(QtGui.QMatrix(1.0, 0.0, 0.0, -1.0, 0.0, 0.0)) else: log.error("Receive image in bad format") image = QtGui.QImage(width, height, QtGui.QImage.Format_RGB32) return image
def readDataPDU(self, dataPDU): """ @summary: read a data PDU object @param dataPDU: DataPDU object """ if dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SET_ERROR_INFO_PDU: errorMessage = "Unknown code %s"%hex(dataPDU.pduData.errorInfo.value) if data.ErrorInfo._MESSAGES_.has_key(dataPDU.pduData.errorInfo): errorMessage = data.ErrorInfo._MESSAGES_[dataPDU.pduData.errorInfo] log.error("INFO PDU : %s"%errorMessage) elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_INPUT: self._listener.onSlowPathInput(dataPDU.pduData.slowPathInputEvents._array) elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SHUTDOWN_REQUEST: log.debug("Receive Shutdown Request") self._transport.close()
def onUpdate(self, width, height, x, y, pixelFormat, encoding, data): """ @summary: Implement RFBClientObserver interface @param width: width of new image @param height: height of new image @param x: x position of new image @param y: y position of new image @param pixelFormat: pixefFormat structure in rfb.message.PixelFormat @param encoding: encoding type rfb.message.Encoding @param data: image data in accordance with pixel format and encoding """ imageFormat = qtImageFormatFromRFBPixelFormat(pixelFormat) if imageFormat is None: log.error("Receive image in bad format") return image = QtGui.QImage(data, width, height, imageFormat) self._widget.notifyImage(x, y, image, width, height)
def readDataPDU(self, dataPDU): """ @summary: read a data PDU object @param dataPDU: DataPDU object """ if dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SET_ERROR_INFO_PDU: errorMessage = "Unknown code %s" % hex( dataPDU.pduData.errorInfo.value) if data.ErrorInfo._MESSAGES_.has_key(dataPDU.pduData.errorInfo): errorMessage = data.ErrorInfo._MESSAGES_[ dataPDU.pduData.errorInfo] log.error("INFO PDU : %s" % errorMessage) elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SHUTDOWN_DENIED: #may be an event to ask to user self._transport.close() elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_UPDATE: self.readUpdateDataPDU(dataPDU.pduData)
def expectedBody(self, data): """ Read header and wait header value to call next state @param data: Stream that length are to header length (1|2|4 bytes) set next state to callBack body when length read from header are received """ bodyLen = None if data.len == 1: bodyLen = UInt8() elif data.len == 2: bodyLen = UInt16Be() elif data.len == 4: bodyLen = UInt32Be() else: log.error("invalid header length") return data.readType(bodyLen) self.expect(bodyLen.value, self._callbackBody)
def readDataPDU(self, dataPDU): """ @summary: read a data PDU object @param dataPDU: DataPDU object """ if dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SET_ERROR_INFO_PDU: errorMessage = "Unknown code %s" % hex( dataPDU.pduData.errorInfo.value) if data.ErrorInfo._MESSAGES_.has_key(dataPDU.pduData.errorInfo): errorMessage = data.ErrorInfo._MESSAGES_[ dataPDU.pduData.errorInfo] log.error("INFO PDU : %s" % errorMessage) elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_INPUT: self._listener.onSlowPathInput( dataPDU.pduData.slowPathInputEvents._array) elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SHUTDOWN_REQUEST: log.debug("Receive Shutdown Request") self._transport.close()
def readDataPDU(self, dataPDU): """ @summary: read a data PDU object @param dataPDU: DataPDU object """ if dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SET_ERROR_INFO_PDU: #ignore 0 error code because is not an error code if dataPDU.pduData.errorInfo.value == 0: return errorMessage = "Unknown code %s"%hex(dataPDU.pduData.errorInfo.value) if data.ErrorInfo._MESSAGES_.has_key(dataPDU.pduData.errorInfo): errorMessage = data.ErrorInfo._MESSAGES_[dataPDU.pduData.errorInfo] log.error("INFO PDU : %s"%errorMessage) elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SHUTDOWN_DENIED: #may be an event to ask to user self._transport.close() elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SAVE_SESSION_INFO: #handle session event self._listener.onSessionReady() elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_UPDATE: self.readUpdateDataPDU(dataPDU.pduData)
def readDataPDU(self, dataPDU): """ @summary: read a data PDU object @param dataPDU: DataPDU object """ if dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SET_ERROR_INFO_PDU: #ignore 0 error code because is not an error code if dataPDU.pduData.errorInfo.value == 0: return errorMessage = "Unknown code %s" % hex( dataPDU.pduData.errorInfo.value) if data.ErrorInfo._MESSAGES_.has_key(dataPDU.pduData.errorInfo): errorMessage = data.ErrorInfo._MESSAGES_[ dataPDU.pduData.errorInfo] log.error("INFO PDU : %s" % errorMessage) elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SHUTDOWN_DENIED: #may be an event to ask to user self._transport.close() elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_SAVE_SESSION_INFO: #handle session event self._listener.onSessionReady() elif dataPDU.shareDataHeader.pduType2.value == data.PDUType2.PDUTYPE2_UPDATE: self.readUpdateDataPDU(dataPDU.pduData)
#for anonymous authentication clientSecurity = rdp.SecurityLevel.RDP_LEVEL_SSL try: opts, args = getopt.getopt(sys.argv[1:], "hl:k:c:o:rn") except getopt.GetoptError: help() for opt, arg in opts: if opt == "-h": help() sys.exit() elif opt == "-l": listen = arg elif opt == "-k": privateKeyFilePath = arg elif opt == "-c": certificateFilePath = arg elif opt == "-o": ouputDirectory = arg elif opt == "-r": clientSecurity = rdp.SecurityLevel.RDP_LEVEL_RDP elif opt == "-n": clientSecurity = rdp.SecurityLevel.RDP_LEVEL_NLA if ouputDirectory is None or not os.path.dirname(ouputDirectory): log.error("%s is an invalid output directory"%ouputDirectory) help() sys.exit() reactor.listenTCP(int(listen), ProxyServerFactory(parseIpPort(args[0]), ouputDirectory, privateKeyFilePath, certificateFilePath, clientSecurity)) reactor.run()
help() sys.exit() elif opt == "-l": listen = arg elif opt == "-k": privateKeyFilePath = arg elif opt == "-c": certificateFilePath = arg #build size map log.info("Build size map") for arg in args: size = readSize(arg) rssFileSizeList.append((size, arg)) log.info("(%s, %s) -> %s" % (size[0], size[1], arg)) log.info("Establish hpfeeds connection") hpc = get_hpc() if hpc: reactor.listenTCP( int(listen), HoneyPotServerFactory(rssFileSizeList, hpc, privateKeyFilePath, certificateFilePath)) reactor.run() else: log.error("Error establishing hpfeeds connection")
def isDirectory(outputDirectory): if outputDirectory is None or not os.path.dirname(outputDirectory): log.error( "{} is an invalid output directory or directory doesn't exist". format(outputDirectory)) return outputDirectory
def isDirectory(outputDirectory): if outputDirectory is None or not os.path.dirname(outputDirectory): log.error("{} is an invalid output directory or directory doesn't exist".format( outputDirectory)) return outputDirectory