Exemplo n.º 1
0
Arquivo: ber.py Projeto: anees042/rdpy
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")
Exemplo n.º 2
0
 def recvConnectionRequest(self, data):
     '''
     read connection confirm packet
     next state is send connection confirm
     @param data: stream
     @see : http://msdn.microsoft.com/en-us/library/cc240470.aspx
     '''
     message = TPDUConnectMessage()
     data.readType(message)
     if message.code != MessageType.X224_TPDU_CONNECTION_REQUEST:
         raise InvalidExpectedDataException("Expect connection packet")
     
     if not message.protocolNeg._is_readed or message.protocolNeg.failureCode._is_readed:
         raise InvalidExpectedDataException("Too older rdp client")
     
     self._requestedProtocol = message.protocolNeg.selectedProtocol
     
     if not self._requestedProtocol | Protocols.PROTOCOL_SSL:
         #send error message and quit
         message = TPDUConnectMessage()
         message.code = MessageType.X224_TPDU_CONNECTION_CONFIRM
         message.protocolNeg.code = NegociationType.TYPE_RDP_NEG_FAILURE
         message.protocolNeg.failureCode = NegotiationFailureCode.SSL_REQUIRED_BY_SERVER
         self._transport.send(message)
         raise InvalidExpectedDataException("rdpy needs ssl client compliant")
     
     self._selectedProtocol = Protocols.PROTOCOL_SSL
     self.sendConnectionConfirm()
Exemplo n.º 3
0
 def recvConnectionConfirm(self, data):
     '''
     recv connection confirm message
     next state is recvData 
     call connect on presentation layer if all is good
     @param data: Stream that contain connection confirm
     @see: response -> http://msdn.microsoft.com/en-us/library/cc240506.aspx
     @see: failure ->http://msdn.microsoft.com/en-us/library/cc240507.aspx
     '''
     message = TPDUConnectMessage()
     data.readType(message)
     if message.code != MessageType.X224_TPDU_CONNECTION_CONFIRM:
         raise InvalidExpectedDataException("invalid TPDU header code X224_TPDU_CONNECTION_CONFIRM != %d"%message.code)
     #check presence of negotiation response
     if not message.protocolNeg._is_readed:
         raise InvalidExpectedDataException("server must support negotiation protocol to use SSL")
     
     if message.protocolNeg.failureCode._is_readed:
         raise InvalidExpectedDataException("negotiation failure code %x"%message.protocolNeg.failureCode.value)
     
     self._selectedProtocol = message.protocolNeg.selectedProtocol
     
     if self._selectedProtocol != Protocols.PROTOCOL_SSL:
         raise InvalidExpectedDataException("only ssl protocol is supported in RDPY version")
     
     #_transport is TPKT and transport is TCP layer of twisted
     self._transport.transport.startTLS(ClientTLSContext())
     
     self.setNextState(self.recvData)
     #connection is done send to presentation
     LayerAutomata.connect(self)
Exemplo n.º 4
0
Arquivo: ber.py Projeto: anees042/rdpy
def readBoolean(s):
    '''
    return boolean
    @param s: stream
    @return: boolean
    '''
    if not readUniversalTag(s, Tag.BER_TAG_BOOLEAN, False):
        raise InvalidExpectedDataException("bad boolean tag")
    size = readLength(s)
    if size != 1:
        raise InvalidExpectedDataException("bad boolean size")
    b = UInt8()
    s.readType(b)
    return bool(b.value)
Exemplo n.º 5
0
Arquivo: per.py Projeto: anees042/rdpy
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")
Exemplo n.º 6
0
    def read(self, s):
        '''
        read type from stream s if conditional
        is true and check constantness
        @param s: Stream
        '''
        self._is_readed = self._conditional()
        if not self._is_readed:
            return

        #not constant mode direct reading
        if not self._constant:
            self.__read__(s)
            return

        #constant mode
        old = deepcopy(self)
        self.__read__(s)
        #check constant value
        if old != self:
            #rollback read value
            s.pos -= sizeof(self)
            raise InvalidExpectedDataException(
                "%s const value expected %s != %s" %
                (self.__class__, old.value, self.value))
Exemplo n.º 7
0
Arquivo: ber.py Projeto: anees042/rdpy
def readOctetString(s):
    '''
    read ber string structure
    @param s: stream
    @return: String
    '''
    if not readUniversalTag(s, Tag.BER_TAG_OCTET_STRING, False):
        raise InvalidExpectedDataException("unexpected ber tag")
    size = readLength(s)
    return String(s.read(size.value))
Exemplo n.º 8
0
Arquivo: ber.py Projeto: anees042/rdpy
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)
Exemplo n.º 9
0
Arquivo: ber.py Projeto: anees042/rdpy
def readEnumerated(s):
    '''
    read enumerated structure
    @param s: Stream
    @return: int or long
    '''
    if not readUniversalTag(s, Tag.BER_TAG_ENUMERATED, False):
        raise InvalidExpectedDataException("invalid ber tag")
    if readLength(s) != 1:
        raise InvalidSize("enumerate size is wrong")
    enumer = UInt8()
    s.readType(enumer)
    return enumer.value
Exemplo n.º 10
0
 def recvData(self, data):
     '''
     read data header from packet
     and pass to presentation layer
     @param data: stream
     '''
     header = TPDUDataHeader()
     data.readType(header)
     if header.messageType == MessageType.X224_TPDU_DATA:
         LayerAutomata.recv(self, data)
     elif header.messageType == MessageType.X224_TPDU_ERROR:
         raise Exception("receive error from tpdu layer")
     else:
         raise InvalidExpectedDataException("unknow tpdu code %s"%header.messageType)
Exemplo n.º 11
0
def readConferenceCreateResponse(s):
    '''
    read response from server
    and return server settings read from this response
    @param s: Stream
    @return: ServerSettings 
    '''
    per.readChoice(s)
    per.readObjectIdentifier(s, t124_02_98_oid)
    per.readLength(s)
    per.readChoice(s)
    per.readInteger16(s, 1001)
    per.readInteger(s)
    per.readEnumerates(s)
    per.readNumberOfSet(s)
    per.readChoice(s)
    if not per.readOctetStream(s, h221_sc_key, 4):
        raise InvalidExpectedDataException("cannot read h221_sc_key")
    return readServerDataBlocks(s)
Exemplo n.º 12
0
Arquivo: mcs.py Projeto: anees042/rdpy
    def recvData(self, data):
        '''
        main receive method
        @param data: Stream 
        '''
        opcode = UInt8()
        data.readType(opcode)

        if self.readMCSPDUHeader(opcode,
                                 DomainMCSPDU.DISCONNECT_PROVIDER_ULTIMATUM):
            print "receive DISCONNECT_PROVIDER_ULTIMATUM"
            self.close()

        elif not self.readMCSPDUHeader(opcode,
                                       DomainMCSPDU.SEND_DATA_INDICATION):
            raise InvalidExpectedDataException("invalid expected mcs opcode")

        userId = UInt16Be()
        channelId = UInt16Be()
        flags = UInt8()
        length = UInt8()

        data.readType((userId, channelId, flags, length))

        if length & UInt8(0x80) == UInt8(0x80):
            lengthP2 = UInt8()
            data.readType(lengthP2)
            length = UInt16Be(length.value & 0x7f << 8 | lengthP2.value)

        #channel id doesn't match a requested layer
        if not self._channelIdsRequest.has_key(channelId):
            print "receive data for an unrequested layer"
            return

        #channel id math an unconnected layer
        if not self._channelIdsRequest[channelId]:
            print "receive data for an unconnected layer"
            return

        self._channelIds[channelId].recv(data)
Exemplo n.º 13
0
Arquivo: mcs.py Projeto: anees042/rdpy
    def recvChannelJoinConfirm(self, data):
        '''
        receive a channel join confirm from server
        @param data: Stream
        '''
        opcode = UInt8()
        confirm = UInt8()
        data.readType((opcode, confirm))
        if not self.readMCSPDUHeader(opcode,
                                     DomainMCSPDU.CHANNEL_JOIN_CONFIRM):
            raise InvalidExpectedDataException("invalid MCS PDU")
        userId = UInt16Be()
        channelId = UInt16Be()
        data.readType((userId, channelId))
        #save state of channel
        self._channelIdsRequest[channelId] = confirm == 0
        if confirm == 0:
            print "server accept channel %d" % channelId.value
        else:
            print "server refused channel %d" % channelId.value

        self.connectNextChannel()
Exemplo n.º 14
0
Arquivo: mcs.py Projeto: anees042/rdpy
    def recvAttachUserConfirm(self, data):
        '''
        receive an attach user confirm
        @param data: Stream
        '''
        opcode = UInt8()
        confirm = UInt8()
        data.readType((opcode, confirm))
        if not self.readMCSPDUHeader(opcode, DomainMCSPDU.ATTACH_USER_CONFIRM):
            raise InvalidExpectedDataException("invalid MCS PDU")
        if confirm != 0:
            raise Exception("server reject user")
        if opcode & UInt8(2) == UInt8(2):
            data.readType(self._userId)

        #build channel list because we have user id
        #add default channel + channels accepted by gcc connection sequence
        self._channelIds[
            self._userId + Channel.
            MCS_USERCHANNEL_BASE] = None  #TODO + [(x, False) for x in self._serverSettings.channelsId])

        self.connectNextChannel()
Exemplo n.º 15
0
Arquivo: mcs.py Projeto: anees042/rdpy
    def recvConnectResponse(self, data):
        '''
        receive mcs connect response from server
        @param data: Stream
        '''
        ber.readApplicationTag(data, Message.MCS_TYPE_CONNECT_RESPONSE)
        ber.readEnumerated(data)
        ber.readInteger(data)
        self.readDomainParams(data)
        if not ber.readUniversalTag(data, ber.Tag.BER_TAG_OCTET_STRING, False):
            raise InvalidExpectedDataException("invalid expected ber tag")
        gccRequestLength = ber.readLength(data)
        if data.dataLen() != gccRequestLength:
            raise InvalidSize("bad size of gcc request")
        self._serverSettings = gcc.readConferenceCreateResponse(data)

        #send domain request
        self.sendErectDomainRequest()
        #send attach user request
        self.sendAttachUserRequest()
        #now wait user confirm from server
        self.setNextState(self.recvAttachUserConfirm)
Exemplo n.º 16
0
Arquivo: ber.py Projeto: anees042/rdpy
def readLength(s):
    '''
    read length of ber structure
    length be on 1 2 or 3 bytes
    @param s: stream
    @return: int or python long
    '''
    size = None
    length = UInt8()
    s.readType(length)
    byte = length.value
    if (byte & 0x80):
        byte &= 0x80
        if byte == 1:
            size = UInt8()
        elif byte == 2:
            size = UInt16Be()
        else:
            raise InvalidExpectedDataException("ber length may be 1 or 2")
        s.readType(size)
    else:
        size = length
    return size.value