def sendAndReceive(self, factory): self.sent = Message() self.sent.ParseFromString(factory.setId(555).setSessionId("555").build()) if len(self.reply_with) > 0: return self.reply_with.pop(0) else: return None
def message(self): """ Extracts the Message from a Frame by parsing the payload into a Message object. """ message = Message() message.ParseFromString(self.payload) return message
def testItShouldBuildNullFromArgument(self): argument = Message.Argument(type=Message.Argument.NULL) type = reflection.types.ReflectedType.fromArgument( argument, reflector=self.reflector) assert isinstance(type, reflection.types.ReflectedNull)
def _pb(self): """ Get an Argument representation of the Array, as defined in the drozer protocol. """ argument = Message.Argument(type=Message.Argument.ARRAY) if self._native[0]._pb().type == Message.Argument.ARRAY: argument.array.type = Message.Array.ARRAY elif self._native[0]._pb().type == Message.Argument.NULL: argument.array.type = Message.Array.OBJECT elif self._native[0]._pb().type == Message.Argument.OBJECT: argument.array.type = Message.Array.OBJECT elif self._native[0]._pb().type == Message.Argument.STRING: argument.array.type = Message.Array.STRING elif self._native[0]._pb().type == Message.Argument.PRIMITIVE: argument.array.type = Message.Array.PRIMITIVE for e in self._native: element = argument.array.element.add() element.MergeFrom( ReflectedType.fromNative(e, reflector=self._reflector)._pb()) return argument
def _pb(self): """ Get an Argument representation of the primitive, as defined in the drozer protocol. """ argument = Message.Argument(type=Message.Argument.PRIMITIVE) if self._type == "boolean": argument.primitive.type = Message.Primitive.BOOL argument.primitive.bool = self._native elif self._type == "byte": argument.primitive.type = Message.Primitive.BYTE argument.primitive.byte = self._native elif self._type == "char": argument.primitive.type = Message.Primitive.CHAR argument.primitive.char = self._native elif self._type == "double": argument.primitive.type = Message.Primitive.DOUBLE argument.primitive.double = self._native elif self._type == "float": argument.primitive.type = Message.Primitive.FLOAT argument.primitive.float = self._native elif self._type == "int": argument.primitive.type = Message.Primitive.INT argument.primitive.int = self._native elif self._type == "long": argument.primitive.type = Message.Primitive.LONG argument.primitive.long = self._native elif self._type == "short": argument.primitive.type = Message.Primitive.SHORT argument.primitive.short = self._native return argument
def _pb(self): """ Get the Argument representation of the String, as defined in the drozer protocol. """ return Message.Argument(type=Message.Argument.DATA, data=self._native)
def buildErrorReply(self, error_message): message = Message(id=555, type=Message.REFLECTION_RESPONSE) message.reflection_response.session_id = "555" message.reflection_response.status = Message.ReflectionResponse.ERROR message.reflection_response.errormessage = error_message return message
def buildObjectReply(self, ref): message = Message(id=555, type=Message.REFLECTION_RESPONSE) message.reflection_response.session_id = "555" message.reflection_response.status = Message.ReflectionResponse.SUCCESS message.reflection_response.result.type = Message.Argument.OBJECT message.reflection_response.result.object.reference = ref return message
def testItShouldBuildStringFromArgument(self): argument = Message.Argument(type=Message.Argument.STRING) argument.string = "Hello, World!" type = reflection.types.ReflectedType.fromArgument( argument, reflector=self.reflector) assert isinstance(type, reflection.types.ReflectedString) assert type.native() == "Hello, World!"
def buildPrimitiveReply(self, type, int): message = Message(id=555, type=Message.REFLECTION_RESPONSE) message.reflection_response.session_id = "555" message.reflection_response.status = Message.ReflectionResponse.SUCCESS message.reflection_response.result.type = Message.Argument.PRIMITIVE message.reflection_response.result.primitive.type = type message.reflection_response.result.primitive.int = int return message
def testItShouldBuildObjectFromArgument(self): argument = Message.Argument(type=Message.Argument.OBJECT) argument.object.reference = 987654321 type = reflection.types.ReflectedType.fromArgument( argument, reflector=self.reflector) assert isinstance(type, reflection.types.ReflectedObject) assert type._ref == 987654321
def testItShouldBuildPrimitiveFromArgument(self): argument = Message.Argument(type=Message.Argument.PRIMITIVE) argument.primitive.type = Message.Primitive.BOOL argument.primitive.bool = True type = reflection.types.ReflectedType.fromArgument( argument, reflector=self.reflector) assert isinstance(type, reflection.types.ReflectedPrimitive) assert type.type() == "boolean" assert type.native() == True
def _pb(self): """ Get an Argument representation of the Object, as defined in the drozer protocol. """ argument = Message.Argument(type=Message.Argument.OBJECT) argument.object.reference = self._ref return argument
class MockReflector(reflection.Reflector): def __init__(self, session): reflection.Reflector.__init__(self, session) self.sent = None self.reply_with = [] def buildErrorReply(self, error_message): message = Message(id=555, type=Message.REFLECTION_RESPONSE) message.reflection_response.session_id = "555" message.reflection_response.status = Message.ReflectionResponse.ERROR message.reflection_response.errormessage = error_message return message def buildObjectReply(self, ref): message = Message(id=555, type=Message.REFLECTION_RESPONSE) message.reflection_response.session_id = "555" message.reflection_response.status = Message.ReflectionResponse.SUCCESS message.reflection_response.result.type = Message.Argument.OBJECT message.reflection_response.result.object.reference = ref return message def buildPrimitiveReply(self, type, int): message = Message(id=555, type=Message.REFLECTION_RESPONSE) message.reflection_response.session_id = "555" message.reflection_response.status = Message.ReflectionResponse.SUCCESS message.reflection_response.result.type = Message.Argument.PRIMITIVE message.reflection_response.result.primitive.type = type message.reflection_response.result.primitive.int = int return message def buildSuccessReply(self): message = Message(id=555, type=Message.REFLECTION_RESPONSE) message.reflection_response.session_id = "555" message.reflection_response.status = Message.ReflectionResponse.SUCCESS return message def replyWith(self, message): self.reply_with.append(message) def sendAndReceive(self, factory): self.sent = Message() self.sent.ParseFromString(factory.setId(555).setSessionId("555").build()) if len(self.reply_with) > 0: return self.reply_with.pop(0) else: return None
def testItShouldBuildObjectArrayFromArgument(self): argument = Message.Argument(type=Message.Argument.ARRAY) argument.array.type = Message.Array.OBJECT for i in range(0, 3): element = argument.array.element.add(type=Message.Argument.OBJECT) element.object.reference = i type = reflection.types.ReflectedType.fromArgument( argument, reflector=self.reflector) assert isinstance(type, reflection.types.ReflectedArray) assert len(type.native()) == 3 for i in range(0, 3): assert isinstance(type.native()[i], reflection.types.ReflectedObject) assert type.native()[i]._ref == i
def testItShouldBuildStringArrayFromArgument(self): argument = Message.Argument(type=Message.Argument.ARRAY) argument.array.type = Message.Array.STRING for i in range(0, 3): element = argument.array.element.add(type=Message.Argument.STRING) element.string = "Hello, World! (" + str(i) + ")" type = reflection.types.ReflectedType.fromArgument( argument, reflector=self.reflector) assert isinstance(type, reflection.types.ReflectedArray) assert len(type.native()) == 3 for i in range(0, 3): assert isinstance(type.native()[i], reflection.types.ReflectedString) assert type.native()[i].native( ) == "Hello, World! (" + str(i) + ")"
def testItShouldRaiseTypeErrorIfArrayIsMixedTypes(self): argument = Message.Argument(type=Message.Argument.ARRAY) argument.array.type = Message.Array.PRIMITIVE for i in range(0, 3): element = argument.array.element.add( type=Message.Argument.PRIMITIVE) element.primitive.type = Message.Primitive.BOOL element.primitive.bool = True for i in range(0, 3): element = argument.array.element.add(type=Message.Argument.OBJECT) element.object.reference = i try: type = reflection.types.ReflectedType.fromArgument( argument, reflector=self.reflector) assert False, "should have caused a TypeError building a mixed array" except TypeError as e: assert str(e) == "mismatched array element types"
def testItShouldBuildPrimitiveArrayFromArgument(self): argument = Message.Argument(type=Message.Argument.ARRAY) argument.array.type = Message.Array.PRIMITIVE for i in range(0, 3): element = argument.array.element.add( type=Message.Argument.PRIMITIVE) element.primitive.type = Message.Primitive.BOOL element.primitive.bool = True type = reflection.types.ReflectedType.fromArgument( argument, reflector=self.reflector) assert isinstance(type, reflection.types.ReflectedArray) assert len(type.native()) == 3 for i in range(0, 3): assert isinstance(type.native()[i], reflection.types.ReflectedPrimitive) assert type.native()[i].type() == "boolean"
def testItShouldShowTheMessageTypeAsSystemResponse(self): message = Message(id=1, type=Message.SYSTEM_RESPONSE).SerializeToString() assert frame.Frame(2, len(message), message).messageType() == "SYSTEM_RESPONSE"
def testItShouldExtractTheMessage(self): message = Message(id=1, type=Message.REFLECTION_REQUEST).SerializeToString() assert isinstance( frame.Frame(2, len(message), message).message(), Message)
def testItShouldShowTheMessageTypeAsReflectionResponse(self): message = Message( id=1, type=Message.REFLECTION_RESPONSE).SerializeToString() assert frame.Frame(2, len(message), message).messageType() == "REFLECTION_RESPONSE"
def _pb(self): """ Get an Argument representation of the null, as defined in the drozer protocol. """ return Message.Argument(type=Message.Argument.NULL)
def testItShouldBuildFrameFromMessage(self): message = Message(id=1, type=Message.REFLECTION_REQUEST).SerializeToString() assert frame.Frame.fromMessage(message).length == len(message)
class SystemRequestFactory: """ The SystemRequestFactory provides a DSL for building SystemRequest messages. """ def __init__(self, request_type): self.builder = Message(type=Message.SYSTEM_REQUEST) self.builder.system_request.type = request_type def addDevice(self, device): """ Set the Device specified in the message. """ self.builder.system_request.device.id = device.device_id self.builder.system_request.device.manufacturer = device.manufacturer self.builder.system_request.device.model = device.model self.builder.system_request.device.software = device.software return self def addDeviceId(self, device_id): """ Set the Device identifier specified in the message. This sets all other device fields (manufacturer, model and software to "N/A"). """ self.builder.system_request.device.id = device_id self.builder.system_request.device.manufacturer = "N/A" self.builder.system_request.device.model = "N/A" self.builder.system_request.device.software = "N/A" return self def build(self): """ Serialize the built Message to a String, using the Protocol Buffer format. """ return self.builder.SerializeToString() def getId(self): """ Get the Identifier assigned to the message. """ return self.builder.id @classmethod def listDevices(cls): """ Helper method to build a request that the server list all known devices. """ builder = SystemRequestFactory(Message.SystemRequest.LIST_DEVICES) return builder @classmethod def listSessions(cls): """ Helper method to build a request that the server list all established sessions. """ builder = SystemRequestFactory(Message.SystemRequest.LIST_SESSIONS) return builder @classmethod def ping(cls): """ Helper method to build a ping request. """ builder = SystemRequestFactory(Message.SystemRequest.PING) return builder def setId(self, message_id): """ Set the identifier of the message. """ self.builder.id = message_id return self def setPassword(self, password): """ Set the password required to establish a session. """ if password != None: self.builder.system_request.password = password return self def setSessionId(self, session): """ Set session identifier, to route a message correctly on the Agent. """ self.builder.system_request.session_id = session return self @classmethod def startSession(cls, device_id): """ Helper method to build a request to start a session with a device. """ builder = SystemRequestFactory(Message.SystemRequest.START_SESSION) builder.addDeviceId(device_id) return builder @classmethod def stopSession(cls, session): """ Helper method to build a request to stop an established session. """ builder = SystemRequestFactory(Message.SystemRequest.STOP_SESSION) builder.setSessionId(session.session_id) return builder @classmethod def stopSessionId(cls, session_id): """ Helper method to build a request to stop an established session, by identifier only. """ builder = SystemRequestFactory(Message.SystemRequest.STOP_SESSION) builder.setSessionId(session_id) return builder
def __init__(self, request_type): self.builder = Message(type=Message.SYSTEM_REQUEST) self.builder.system_request.type = request_type
def buildSuccessReply(self): message = Message(id=555, type=Message.REFLECTION_RESPONSE) message.reflection_response.session_id = "555" message.reflection_response.status = Message.ReflectionResponse.SUCCESS return message
class ReflectionResponseFactory: """ The ReflectionResponseFactory provides a DSL for building ReflectionResponse messages. """ def __init__(self): self.builder = Message(type=Message.REFLECTION_RESPONSE) self.builder.reflection_response.status = Message.ReflectionResponse.SUCCESS def build(self): """ Serialize the built Message to a String, using the Protocol Buffer format. """ return self.builder.SerializeToString() @classmethod def error(cls, message): """ Helper method to build an error response. """ builder = ReflectionResponseFactory() builder.isError() builder.setErrorMessage(message) return builder @classmethod def fatal(cls, message): """ Helper method to build a fatal error response. """ builder = ReflectionResponseFactory() builder.isFatal() builder.setErrorMessage(message) return builder def inReplyTo(self, message): """ Tag the response as a reply to another message, by setting the message identifiers to be equal and setting the session to keep the flow intact. """ self.builder.id = message.id self.builder.reflection_response.session_id = message.reflection_request.session_id return self def isError(self): """ Indicate an error in the response status code. """ self.builder.reflection_response.status = Message.ReflectionResponse.ERROR def isFatal(self): """ Indicate a fatal error in the response status code. """ self.builder.reflection_response.status = Message.ReflectionResponse.FATAL def isSuccess(self): """ Indicate success in the response status code. """ self.builder.reflection_response.status = Message.ReflectionResponse.SUCCESS def setErrorMessage(self, message): """ Set the error message associated with this response. """ self.builder.reflection_response.errormessage = message return self
def __init__(self, response_type): self.builder = Message(type=Message.SYSTEM_RESPONSE) self.builder.system_response.type = response_type self.builder.system_response.status = Message.SystemResponse.SUCCESS
class SystemResponseFactory: """ The SystemResponseFactory provides a DSL for building SystemResponse messages. """ def __init__(self, response_type): self.builder = Message(type=Message.SYSTEM_RESPONSE) self.builder.system_response.type = response_type self.builder.system_response.status = Message.SystemResponse.SUCCESS def addDevice(self, device): """ Add a Device to the list of devices within the message. """ d = self.builder.system_response.devices.add() d.id = device.device_id d.manufacturer = device.manufacturer d.model = device.model d.software = device.software return d def add_session(self, session): """ Add a Session to the list of Sessions within the message. """ s = self.builder.system_response.sessions.add() s.id = session.session_id s.device_id = session.device.device_id @classmethod def bound(cls, device): """ Helper method to build a message to indicate a Device has successfully bound to the server. """ builder = SystemResponseFactory(Message.SystemResponse.BOUND) builder.addDevice(device) return builder def build(self): """ Serialize the built Message to a String, using the Protocol Buffer format. """ return self.builder.SerializeToString() @classmethod def error(cls, error_type, message): """ Helper method to build an error message, with a particular type and an error message. """ builder = SystemResponseFactory(error_type) builder.isError() builder.setErrorMessage(message) return builder def inReplyTo(self, message): """ Tag the response as a reply to another message, by setting the message identifiers to be equal. """ self.builder.id = message.id return self def isError(self): """ Indicate an error in the response status code. """ self.builder.system_response.status = Message.SystemResponse.ERROR def isSuccess(self): """ Indicate success in the response status code. """ self.builder.system_response.status = Message.SystemResponse.SUCCESS @classmethod def listDevices(cls, devices): """ Helper method to build a DEVICE_LIST response, with a list of devices built from a collection. """ builder = SystemResponseFactory(Message.SystemResponse.DEVICE_LIST) for device in devices: builder.addDevice(device) return builder @classmethod def listSessions(cls, sessions): """ Helper method to build a SESSION_LIST response, with a list of sessions build from a collection. """ builder = SystemResponseFactory(Message.SystemResponse.SESSION_LIST) #for session in sessions: # builder.add_session(session) return builder def setErrorMessage(self, message): """ Set the error message associated with this response. """ self.builder.system_response.error_message = message return self @classmethod def unbound(cls, device): """ Helper method to build a message to indicate a device has successfully unbound from the Server. """ builder = SystemResponseFactory(Message.SystemResponse.UNBOUND) builder.addDevice(device) return builder
def __init__(self): self.builder = Message(type=Message.REFLECTION_RESPONSE) self.builder.reflection_response.status = Message.ReflectionResponse.SUCCESS