work on bitwise operator
This commit is contained in:
@@ -47,9 +47,10 @@ class SimpleType(Type):
|
|||||||
'''
|
'''
|
||||||
simple type
|
simple type
|
||||||
'''
|
'''
|
||||||
def __init__(self, structFormat, typeSize, value):
|
def __init__(self, structFormat, typeSize, signed, value):
|
||||||
self._typeSize = typeSize
|
self._typeSize = typeSize
|
||||||
self._structFormat = structFormat
|
self._structFormat = structFormat
|
||||||
|
self._signed = signed
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -64,7 +65,7 @@ class SimpleType(Type):
|
|||||||
'''
|
'''
|
||||||
setter of value after check it
|
setter of value after check it
|
||||||
'''
|
'''
|
||||||
if self.__class__.__dict__.has_key("isInRange") and not self.__class__.isInRange(value):
|
if not self.isInRange(value):
|
||||||
raise InvalidValue("value is out of range for %s"%self.__class__)
|
raise InvalidValue("value is out of range for %s"%self.__class__)
|
||||||
self._value = value
|
self._value = value
|
||||||
|
|
||||||
@@ -86,12 +87,69 @@ class SimpleType(Type):
|
|||||||
'''
|
'''
|
||||||
self._value = struct.unpack(self._structFormat,s.read(self._typeSize))[0]
|
self._value = struct.unpack(self._structFormat,s.read(self._typeSize))[0]
|
||||||
|
|
||||||
|
|
||||||
|
def mask(self):
|
||||||
|
'''
|
||||||
|
compute bit mask for type
|
||||||
|
beacause in python all numbers are int long or float
|
||||||
|
'''
|
||||||
|
if not self.__dict__.has_key("_mask"):
|
||||||
|
mask = 0xff
|
||||||
|
for i in range(1, self._typeSize):
|
||||||
|
mask = mask << 8
|
||||||
|
mask |= 0xff
|
||||||
|
self._mask = mask
|
||||||
|
return self._mask
|
||||||
|
|
||||||
|
def isInRange(self, value):
|
||||||
|
'''
|
||||||
|
check if value is in mask range
|
||||||
|
'''
|
||||||
|
if self._signed:
|
||||||
|
return not (value < -(self.mask() >> 1) or value > (self.mask() >> 1))
|
||||||
|
else:
|
||||||
|
return not (value < 0 or value > self.mask())
|
||||||
|
|
||||||
def __sizeof__(self):
|
def __sizeof__(self):
|
||||||
'''
|
'''
|
||||||
return size of type
|
return size of type
|
||||||
'''
|
'''
|
||||||
return self._typeSize
|
return self._typeSize
|
||||||
|
|
||||||
|
def __invert__(self):
|
||||||
|
'''
|
||||||
|
implement not operator
|
||||||
|
'''
|
||||||
|
invert = ~self._value
|
||||||
|
if not self._signed:
|
||||||
|
invert &= self.mask()
|
||||||
|
return self.__class__(invert)
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
'''
|
||||||
|
implement addition operator
|
||||||
|
'''
|
||||||
|
return self.__class__(self._value.__add__(other._value))
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
'''
|
||||||
|
implement sub operator
|
||||||
|
'''
|
||||||
|
return self.__class__(self._value.__sub__(other._value))
|
||||||
|
|
||||||
|
def __and__(self, other):
|
||||||
|
'''
|
||||||
|
implement bitwise and operator
|
||||||
|
'''
|
||||||
|
return self.__class__(self._value.__and__(other._value))
|
||||||
|
|
||||||
|
def __or__(self, other):
|
||||||
|
'''
|
||||||
|
implement bitwise and operator
|
||||||
|
'''
|
||||||
|
return self.__class__(self._value.__or__(other._value))
|
||||||
|
|
||||||
|
|
||||||
class CompositeType(Type):
|
class CompositeType(Type):
|
||||||
'''
|
'''
|
||||||
keep ordering declaration of simple type
|
keep ordering declaration of simple type
|
||||||
@@ -107,7 +165,7 @@ class CompositeType(Type):
|
|||||||
'''
|
'''
|
||||||
magic function to update type list
|
magic function to update type list
|
||||||
'''
|
'''
|
||||||
if name[0] != '_' and (isinstance(value, Type) or isinstance(value, tuple)):
|
if name[0] != '_' and (isinstance(value, Type) or isinstance(value, tuple)) and not self.__dict__.has_key(name):
|
||||||
self._type.append(value)
|
self._type.append(value)
|
||||||
self.__dict__[name] = value
|
self.__dict__[name] = value
|
||||||
|
|
||||||
@@ -142,14 +200,17 @@ class UInt8(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, "B", 1, value)
|
SimpleType.__init__(self, "B", 1, False, value)
|
||||||
|
|
||||||
@staticmethod
|
class SInt8(SimpleType):
|
||||||
def isInRange(value):
|
'''
|
||||||
|
unsigned byte
|
||||||
|
'''
|
||||||
|
def __init__(self, value = 0):
|
||||||
'''
|
'''
|
||||||
return true if value is in UInt8 range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
return not (value < 0 or value > 0xff)
|
SimpleType.__init__(self, "b", 1, True, value)
|
||||||
|
|
||||||
|
|
||||||
class UInt16Be(SimpleType):
|
class UInt16Be(SimpleType):
|
||||||
@@ -160,14 +221,7 @@ class UInt16Be(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, ">H", 2, value)
|
SimpleType.__init__(self, ">H", 2, False, value)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def isInRange(value):
|
|
||||||
'''
|
|
||||||
return true if value is in UInt8 range
|
|
||||||
'''
|
|
||||||
return not (value < 0 or value > 0xffff)
|
|
||||||
|
|
||||||
class UInt16Le(SimpleType):
|
class UInt16Le(SimpleType):
|
||||||
'''
|
'''
|
||||||
@@ -177,14 +231,7 @@ class UInt16Le(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, "<H", 2, value)
|
SimpleType.__init__(self, "<H", 2, False, value)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def isInRange(value):
|
|
||||||
'''
|
|
||||||
return true if value is in UInt8 range
|
|
||||||
'''
|
|
||||||
return not (value < 0 or value > 0xffff)
|
|
||||||
|
|
||||||
class UInt32Be(SimpleType):
|
class UInt32Be(SimpleType):
|
||||||
'''
|
'''
|
||||||
@@ -194,14 +241,7 @@ class UInt32Be(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, ">I", 4, value)
|
SimpleType.__init__(self, ">I", 4, False, value)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def isInRange(value):
|
|
||||||
'''
|
|
||||||
return true if value is in UInt8 range
|
|
||||||
'''
|
|
||||||
return not (value < 0 or value > 0xffffffff)
|
|
||||||
|
|
||||||
class UInt32Le(SimpleType):
|
class UInt32Le(SimpleType):
|
||||||
'''
|
'''
|
||||||
@@ -211,14 +251,7 @@ class UInt32Le(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, "<I", 4, value)
|
SimpleType.__init__(self, "<I", 4, False, value)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def isInRange(value):
|
|
||||||
'''
|
|
||||||
return true if value is in UInt8 range
|
|
||||||
'''
|
|
||||||
return not (value < 0 or value > 0xffffffff)
|
|
||||||
|
|
||||||
class SInt32Le(SimpleType):
|
class SInt32Le(SimpleType):
|
||||||
'''
|
'''
|
||||||
@@ -228,14 +261,7 @@ class SInt32Le(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, "<I", 4, value)
|
SimpleType.__init__(self, "<I", 4, True, value)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def isInRange(value):
|
|
||||||
'''
|
|
||||||
return true if value is in UInt8 range
|
|
||||||
'''
|
|
||||||
return not (value < ~0x7fffffff or value > 0x7fffffff)
|
|
||||||
|
|
||||||
class SInt32Be(SimpleType):
|
class SInt32Be(SimpleType):
|
||||||
'''
|
'''
|
||||||
@@ -245,14 +271,7 @@ class SInt32Be(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, ">I", 4, value)
|
SimpleType.__init__(self, ">I", 4, True, value)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def isInRange(value):
|
|
||||||
'''
|
|
||||||
return true if value is in UInt8 range
|
|
||||||
'''
|
|
||||||
return not (value < ~0x7fffffff or value > 0x7fffffff)
|
|
||||||
|
|
||||||
class UInt24Be(SimpleType):
|
class UInt24Be(SimpleType):
|
||||||
'''
|
'''
|
||||||
@@ -262,14 +281,7 @@ class UInt24Be(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, ">I", 3, value)
|
SimpleType.__init__(self, ">I", 3, False, value)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def isInRange(value):
|
|
||||||
'''
|
|
||||||
return true if value is in UInt8 range
|
|
||||||
'''
|
|
||||||
return not (value < 0 or value > 0xffffff)
|
|
||||||
|
|
||||||
def write(self, s):
|
def write(self, s):
|
||||||
'''
|
'''
|
||||||
@@ -291,14 +303,7 @@ class UInt24Le(SimpleType):
|
|||||||
'''
|
'''
|
||||||
constructor check value range
|
constructor check value range
|
||||||
'''
|
'''
|
||||||
SimpleType.__init__(self, "<I", 3, value)
|
SimpleType.__init__(self, "<I", 3, False, value)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def isInRange(value):
|
|
||||||
'''
|
|
||||||
return true if value is in UInt8 range
|
|
||||||
'''
|
|
||||||
return not (value < 0 or value > 0xffffff)
|
|
||||||
|
|
||||||
def write(self, s):
|
def write(self, s):
|
||||||
'''
|
'''
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
'''
|
'''
|
||||||
@author: sylvain
|
@author: sylvain
|
||||||
'''
|
'''
|
||||||
from rdpy.protocol.network.type import UInt16Le
|
from rdpy.protocol.network.type import UInt8, UInt16Be
|
||||||
from rdpy.utils.const import ConstAttributes
|
from rdpy.utils.const import ConstAttributes
|
||||||
|
from rdpy.protocol.network.error import InvalidExpectedDataException
|
||||||
|
|
||||||
@ConstAttributes
|
@ConstAttributes
|
||||||
class BerPc(object):
|
class BerPc(object):
|
||||||
BER_PC_MASK = UInt16Le(0x20)
|
BER_PC_MASK = UInt8(0x20)
|
||||||
BER_PRIMITIVE = UInt16Le(0x00)
|
BER_PRIMITIVE = UInt8(0x00)
|
||||||
BER_CONSTRUCT = UInt16Le(0x20)
|
BER_CONSTRUCT = UInt8(0x20)
|
||||||
|
|
||||||
def berPC(pc):
|
def berPC(pc):
|
||||||
'''
|
'''
|
||||||
@@ -20,3 +21,32 @@ def berPC(pc):
|
|||||||
else:
|
else:
|
||||||
return BerPc.BER_PRIMITIVE
|
return BerPc.BER_PRIMITIVE
|
||||||
|
|
||||||
|
def readLength(s):
|
||||||
|
'''
|
||||||
|
read length of ber structure
|
||||||
|
length be on 1 2 or 3 bytes
|
||||||
|
'''
|
||||||
|
size = None
|
||||||
|
byte = UInt8()
|
||||||
|
s.readType(byte)
|
||||||
|
if (byte & UInt8(0x80)) == UInt8(0x80):
|
||||||
|
byte &= ~UInt8(0x80)
|
||||||
|
if byte == UInt8(1):
|
||||||
|
size = UInt8()
|
||||||
|
elif byte == UInt8(2):
|
||||||
|
size = UInt16Be()
|
||||||
|
else:
|
||||||
|
raise InvalidExpectedDataException("ber length may be 1 or 2")
|
||||||
|
s.readType(size)
|
||||||
|
else:
|
||||||
|
size = byte
|
||||||
|
return size
|
||||||
|
|
||||||
|
def writeLength(size):
|
||||||
|
'''
|
||||||
|
write length as expected in Ber specification
|
||||||
|
'''
|
||||||
|
if size > UInt16Be(0x7f):
|
||||||
|
return (UInt8(0x82), size)
|
||||||
|
else:
|
||||||
|
return UInt8(size.value)
|
||||||
3
rdpy/protocol/rdp/per.py
Normal file
3
rdpy/protocol/rdp/per.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
'''
|
||||||
|
@author: sylvain
|
||||||
|
'''
|
||||||
@@ -136,12 +136,10 @@ class TPDU(LayerAutomata):
|
|||||||
if negResp.len != UInt16Le(0x0008):
|
if negResp.len != UInt16Le(0x0008):
|
||||||
raise InvalidExpectedDataException("invalid size of negotiation response")
|
raise InvalidExpectedDataException("invalid size of negotiation response")
|
||||||
|
|
||||||
protocol = negResp.protocol
|
self._protocol = negResp.protocol
|
||||||
if protocol != self._protocol:
|
|
||||||
raise NegotiationFailure("protocol negotiation failure")
|
|
||||||
|
|
||||||
#_transport is TPKT and transport is TCP layer of twisted
|
|
||||||
if self._protocol == Protocols.PROTOCOL_SSL:
|
if self._protocol == Protocols.PROTOCOL_SSL:
|
||||||
|
#_transport is TPKT and transport is TCP layer of twisted
|
||||||
self._transport.transport.startTLS(ClientTLSContext())
|
self._transport.transport.startTLS(ClientTLSContext())
|
||||||
else:
|
else:
|
||||||
raise NegotiationFailure("protocol negociation failure")
|
raise NegotiationFailure("protocol negociation failure")
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ class Rfb(RawLayer):
|
|||||||
security handshake for 33 rfb version
|
security handshake for 33 rfb version
|
||||||
server imposed security level
|
server imposed security level
|
||||||
'''
|
'''
|
||||||
|
#TODO!!!
|
||||||
self._version = data.read_beuint32()
|
self._version = data.read_beuint32()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user