def writeObjectIdentifier(oid): ''' create tuble of 6 UInt8 with oid values @param oid: tuple of 6 int @return: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) ''' return (UInt8(5), UInt8((oid[0] << 4) & (oid[1] & 0x0f)), UInt8(oid[2]), UInt8(oid[3]), UInt8(oid[4]), UInt8(oid[5]))
def readInteger(s): ''' read integer structure from stream @param s: stream @return: int or long python ''' if not readUniversalTag(s, Tag.BER_TAG_INTEGER, False): raise InvalidExpectedDataException("bad integer tag") size = readLength(s) if size == 1: integer = UInt8() s.readType(integer) return integer.value elif size == 2: integer = UInt16Be() s.readType(integer) return integer.value elif size == 3: integer1 = UInt8() integer2 = UInt16Be() s.readType(integer1) s.readType(integer2) return integer2.value + (integer1.value << 16) elif size == 4: integer = UInt32Be() s.readType(integer) return integer.value else: raise InvalidExpectedDataException("wrong integer size")
def __init__(self): CompositeType.__init__(self) self.len = UInt8(lambda:sizeof(self) - 1) self.code = UInt8() self.padding = (UInt16Be(), UInt16Be(), UInt8()) #read if there is enought data self.protocolNeg = Negotiation(optional = True)
def __init__(self): CompositeType.__init__(self) #preambule self.bMsgtype = UInt8() self.flag = UInt8() self.wMsgSize = UInt16Le(lambda: sizeof(self)) self.errorMessage = LicensingErrorMessage( conditional=lambda: self.bMsgtype == MessageType.ERROR_ALERT)
def __init__(self, optional = False): CompositeType.__init__(self, optional = optional) self.code = UInt8() self.flag = UInt8(0) #always 8 self.len = UInt16Le(0x0008, constant = True) self.selectedProtocol = UInt32Le(conditional = lambda: self.code == NegociationType.TYPE_RDP_NEG_RSP) self.failureCode = UInt32Le(conditional = lambda: self.code == NegociationType.TYPE_RDP_NEG_FAILURE)
def recvServerOrder(self, data): ''' read order receive from server ''' packet_type = UInt8() data.readType(packet_type) if packet_type == UInt8(0): self.expect(3, self.recvFrameBufferUpdateHeader)
def __init__(self, presentation): ''' Constructor ''' RawLayer.__init__(self, LayerMode.NONE, presentation) #last packet version read from header self._lastPacketVersion = UInt8() #length may be coded on more than 1 bytes self._lastShortLength = UInt8()
def writeBoolean(b): ''' return structure that represent boolean in ber specification @param b: boolean @return: ber boolean structure ''' boolean = UInt8(0) if b: boolean = UInt8(0xff) return (writeUniversalTag(Tag.BER_TAG_BOOLEAN, False), writeLength(1), boolean)
def writeLength(size): ''' return strcture length as expected in Ber specification @param size: int or python long @return: UInt8 or (UInt8(0x82), UInt16Be) ''' if size > 0x7f: return (UInt8(0x82), UInt16Be(size)) else: return UInt8(size)
def writeSelection(selection): ''' read per selection structure @param selection: int selection value @return: UInt8 ''' return UInt8(selection)
def writeChoice(choice): ''' read per choice structure @param choice: int choice value @return: UInt8 ''' return UInt8(choice)
def writeNumericString(nStr, minValue): ''' write string in per format @param str: python string to write @param min: min value @return: String type that contain str encoded in per format ''' length = len(nStr) mlength = minValue if length - minValue >= 0: mlength = length - minValue result = [] for i in range(0, length, 2): c1 = ord(nStr[i]) if i + 1 < length: c2 = ord(nStr[i + 1]) else: c2 = 0x30 c1 = (c1 - 0x30) % 10 c2 = (c2 - 0x30) % 10 result.append(UInt8((c1 << 4) | c2)) return (writeLength(mlength), tuple(result))
def writeNumberOfSet(numberOfSet): ''' read per numberOfSet structure @param numberOfSet: int numberOfSet value @return: UInt8 ''' return UInt8(numberOfSet)
def __init__(self, mode): ''' constructor ''' RawLayer.__init__(self, mode) #usefull for rfb protocol self._callbackBody = None #protocol version negociated self._version = ProtocolVersion.RFB003008 #nb security launch by server self._securityLevel = SecurityType.INVALID #shared framebuffer client init message self._sharedFlag = UInt8(False) #server init message #which contain framebuffer dim and pixel format self._serverInit = ServerInit() #client pixel format self._pixelFormat = PixelFormat() #server name self._serverName = String() #nb rectangle self._nbRect = 0 #current rectangle header self._currentRect = Rectangle() #client or server adaptor self._observer = []
def readObjectIdentifier(s, oid): ''' read object identifier @param oid: must be a tuple of 6 elements @param s: Stream @return: true if oid is same as in stream ''' size = readLength(s) if size != 5: raise InvalidValue("size of stream oid is wrong %d != 5"%size) a_oid = [0, 0, 0, 0, 0, 0] t12 = UInt8() s.readType(t12) a_oid[0] = t12.value >> 4 a_oid[1] = t12.value & 0x0f s.readType(t12) a_oid[2] = t12.value s.readType(t12) a_oid[3] = t12.value s.readType(t12) a_oid[4] = t12.value s.readType(t12) a_oid[5] = t12.value if list(oid) != a_oid: raise InvalidExpectedDataException("invalid object identifier")
def __init__(self, name="", options=UInt32Le()): CompositeType.__init__(self) #name of channel self.name = String(name[0:8] + "\x00" * (8 - len(name)), readLen=UInt8(8)) #unknown self.options = options
def __init__(self, incremental=False, x=0, y=0, width=0, height=0): CompositeType.__init__(self) self.incremental = UInt8(incremental) self.x = UInt16Be(x) self.y = UInt16Be(y) self.width = UInt16Be(width) self.height = UInt16Be(height)
def writeEnumerates(enumer): ''' read per enumerate structure @param enumer: int enumerate value @return: UInt8 ''' return UInt8(enumer)
def readLength(s): ''' read length use in per specification @param s: Stream @return: int python ''' byte = UInt8() s.readType(byte) size = None if (byte & UInt8(0x80)) == UInt8(0x80): byte &= ~UInt8(0x80) size = UInt16Be(byte.value << 8) s.readType(byte) size += s.value + byte else: size = UInt16Be(byte.value) return size.value
def readEnumerates(s): ''' read per enumerate format @param s: Stream @return: int that represent enumerate ''' choice = UInt8() s.readType(choice) return choice.value
def send(self, channelId, data): ''' specific send function for channelId @param data: message to send ''' self._transport.send( (self.writeMCSPDUHeader(DomainMCSPDU.SEND_DATA_REQUEST), self._userId, channelId, UInt8(0x70), UInt16Be(sizeof(data)) | UInt16Be(0x8000), data))
def readChoice(s): ''' read per choice format @param s: Stream @return: int that represent choice ''' choice = UInt8() s.readType(choice) return choice.value
def recvFrameBufferUpdateHeader(self, data): ''' read frame buffer update packet header ''' #padding nbRect = UInt16Be() self._nbRect = data.readType((UInt8(), nbRect)) self._nbRect = nbRect.value self.expect(12, self.recvRectHeader)
def readSelection(s): ''' read per selection format @param s: Stream @return: int that represent selection ''' choice = UInt8() s.readType(choice) return choice.value
def readNumberOfSet(s): ''' read per numberOfSet format @param s: Stream @return: int that represent numberOfSet ''' choice = UInt8() s.readType(choice) return choice.value
def readUniversalTag(s, tag, pc): ''' read tag of ber packet @param tag: Tag class attributes @param pc: boolean @return: true if tag is correctly read ''' byte = UInt8() s.readType(byte) return byte == ((Class.BER_CLASS_UNIV | berPC(pc)) | (Tag.BER_TAG_MASK & tag))
def writeLength(value): ''' write length as expected in per specification @param value: int or long python @return: UInt8, UInt16Be depend on value ''' if value > 0x7f: return UInt16Be(value | 0x8000) else: return UInt8(value)
def writeApplicationTag(tag, size): ''' return struct that represent ber application tag @param tag: UINt8 @param size: size to rest of packet ''' if tag > UInt8(30): return (((Class.BER_CLASS_APPL | BerPc.BER_CONSTRUCT) | Tag.BER_TAG_MASK), tag, writeLength(size)) else: return (((Class.BER_CLASS_APPL | BerPc.BER_CONSTRUCT) | (Tag.BER_TAG_MASK & tag)), writeLength(size))
def readApplicationTag(s, tag): ''' read application tag @param s: stream @param tag: tag class attributes @return: length of application packet ''' byte = UInt8() s.readType(byte) if tag > UInt8(30): if byte != ((Class.BER_CLASS_APPL | BerPc.BER_CONSTRUCT) | Tag.BER_TAG_MASK): raise InvalidExpectedDataException() s.readType(byte) if byte != tag: raise InvalidExpectedDataException("bad tag") else: if byte != ((Class.BER_CLASS_APPL | BerPc.BER_CONSTRUCT) | (Tag.BER_TAG_MASK & tag)): raise InvalidExpectedDataException() return readLength(s)
def __init__(self): CompositeType.__init__(self) self.BitsPerPixel = UInt8(32) self.Depth = UInt8(24) self.BigEndianFlag = UInt8(False) self.TrueColorFlag = UInt8(True) self.RedMax = UInt16Be(255) self.GreenMax = UInt16Be(255) self.BlueMax = UInt16Be(255) self.RedShift = UInt8(16) self.GreenShift = UInt8(8) self.BlueShift = UInt8(0) self.padding = (UInt16Be(), UInt8())