コード例 #1
0
    def hasReceivedPing(self, timeout=1.0):
        """
		Waits to receive "ping" event until the end of the timeout.
		
		@param timeout: time to wait response in second (default=1s)
		@type timeout: float
		
		@return: an event matching with the template or None otherwise
		@rtype: templatemessage
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        tpl_rsp = TestTemplatesLib.TemplateMessage()

        if self.cfg['agent-support']:
            layer_agent = TestTemplatesLib.TemplateLayer('AGENT')
            layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
            layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
            tpl_rsp.addLayer(layer_agent)

        tpl_rsp.addLayer(AdapterIP.ip(more=AdapterIP.received()))
        tpl_rsp.addLayer(AdapterTCP.tcp(more=AdapterTCP.received()))
        if self.cfg['ssl-support']:
            tpl_rsp.addLayer(AdapterSSL.ssl(more=AdapterSSL.received()))
        tpl_rsp.addLayer(
            templates.ws(opcode=codec.WEBSOCKET_OPCODE_PING,
                         more=templates.received()))
        return self.received(expected=tpl_rsp, timeout=timeout)
コード例 #2
0
	def hasReceivedHttpResponse(self, httpCode="200", httpPhrase="OK", headers=None, timeout=1.0):
		"""
		Wait to receive "http response" until the end of the timeout.
		
		@param httpCode: http code (default=200)
		@type httpCode: string
		
		@param httpPhrase: http phrase (default=OK)
		@type httpPhrase: string
		
		@param timeout: time to wait in seconds (default=1s)
		@type timeout: float
	
		@return: http response
		@rtype:	template	
		"""
		TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout)
		
		tpl = TestTemplatesLib.TemplateMessage()
		
		if self.cfg['agent-support']:
			layer_agent= TestTemplatesLib.TemplateLayer('AGENT')
			layer_agent.addKey(name='name', data=self.cfg['agent']['name'] )
			layer_agent.addKey(name='type', data=self.cfg['agent']['type'] )
			tpl.addLayer( layer_agent )
			
		tpl.addLayer( AdapterIP.ip( more=AdapterIP.received() ) )
		tpl.addLayer( AdapterTCP.tcp(more=AdapterTCP.received()) )
		if self.ADP_HTTP.tcp().cfg['ssl-support']:
			tpl.addLayer( AdapterSSL.ssl(more=AdapterSSL.received()) )
		tpl.addLayer( AdapterHTTP.response( version="HTTP/1.1", code=httpCode, phrase=httpPhrase, headers=headers ) )
		
		return self.hasReceivedResponse(expected=tpl, timeout=timeout)
コード例 #3
0
    def isHandshakeSuccessful(self, timeout=1.0):
        """
		Wait to receive "handshake successful" event until the end of the timeout.
		
		@param timeout: time to wait response in second (default=1s)
		@type timeout: float
		
		@return: an event matching with the template or None otherwise
		@rtype: templatemessage
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        tpl_rsp = TestTemplatesLib.TemplateMessage()

        if self.cfg['agent-support']:
            layer_agent = TestTemplatesLib.TemplateLayer('AGENT')
            layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
            layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
            tpl_rsp.addLayer(layer_agent)

        tpl_rsp.addLayer(AdapterIP.ip(more=AdapterIP.received()))
        tpl_rsp.addLayer(AdapterTCP.tcp(more=AdapterTCP.received()))
        if self.cfg['ssl-support']:
            tpl_rsp.addLayer(AdapterSSL.ssl(more=AdapterSSL.received()))
        tpl_rsp.addLayer(AdapterHTTP.response())
        tpl_rsp.addLayer(templates.ws(more=templates.handshake_ok()))
        return self.received(expected=tpl_rsp, timeout=timeout)
コード例 #4
0
    def getTemplateRequest(self,
                           httpMethod="GET",
                           httpUri="/",
                           httpVersion='HTTP/1.1',
                           httpHeaders={},
                           httpBody=None):
        """
		"""
        tpl = TestTemplates.TemplateMessage()

        if self.cfg['agent-support']:
            layer_agent = TestTemplates.TemplateLayer('AGENT')
            layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
            layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
            tpl.addLayer(layer_agent)

        tpl.addLayer(AdapterIP.ip(more=AdapterIP.received()))
        tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received()))
        if self.ADP_TCP.cfg['ssl-support']:
            tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received()))

        headers = {}
        headers.update(httpHeaders)
        tpl.addLayer(
            templates.request(method=httpMethod,
                              uri=httpUri,
                              version=httpVersion,
                              headers=headers,
                              body=httpBody))

        return tpl
コード例 #5
0
    def hasReceivedResponse(self, expected, timeout=1.0):
        """
		Wait response until the end of the timeout.
		
		@param expected: response template
		@type expected: templatemessage

		@param timeout: time to wait in seconds (default=1s)
		@type timeout: float
	
		@return: response
		@rtype: template	
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        tpl = TestTemplates.TemplateMessage()

        if self.cfg['agent-support']:
            layer_agent = TestTemplates.TemplateLayer('AGENT')
            layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
            layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
            tpl.addLayer(layer_agent)

        tpl.addLayer(AdapterIP.ip(more=AdapterIP.received()))
        tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received()))
        tpl.addLayer(AdapterTelnet.dataIncoming())
        tpl.addLayer(expected)

        evt = self.received(expected=tpl, timeout=timeout)

        return evt
コード例 #6
0
	def getExpectedTemplate(self, event, versionIp=None, sourceIp=None, destinationIp=None, sourcePort=None, destinationPort=None):
		"""
		Return an expected template with ip and tcp layers
		"""
		# prepare layers
		defaultVer = self.cfg['sock-family']
		if versionIp is not None:
			defaultVer = versionIp
		layer_ip = AdapterIP.ip( source=sourceIp, destination=destinationIp, version=defaultVer ) 		
		
		# tcp
		layer_tcp = AdapterTCP.tcp(source=sourcePort, destination=destinationPort)

		# ssh
		layer_ssh = templates.ssh(more=event)

		# prepare template
		if self.cfg['agent-support']:
			layer_agent= TestTemplatesLib.TemplateLayer('AGENT')
			layer_agent.addKey(name='name', data=self.cfg['agent']['name'] )
			layer_agent.addKey(name='type', data=self.cfg['agent']['type'] )

		tpl = TestTemplatesLib.TemplateMessage()
		if self.cfg['agent-support']:
			tpl.addLayer(layer=layer_agent)
		tpl.addLayer(layer=layer_ip)
		tpl.addLayer(layer=layer_tcp)
		tpl.addLayer(layer=layer_ssh)
		return tpl
コード例 #7
0
    def hasReceivedHttpRequest(self,
                               httpMethod="GET",
                               httpUri="/",
                               httpVersion='HTTP/1.1',
                               timeout=1.0,
                               httpHeaders={},
                               httpBody=None):
        """
		Wait to receive "http request" until the end of the timeout.

		@param httpMethod: http method (default=GET)
		@type httpMethod: string

		@param httpUri: http phrase (default=OK)
		@type httpUri: string

		@param httpVersion: http version (default=HTTP/1.1)
		@type httpVersion: string

		@param httpHeaders: expected http headers
		@type httpHeaders: dict

		@param httpBody: expected body (default=None)
		@type httpBody: string/none
		
		@param timeout: time to wait in seconds (default=1s)
		@type timeout: float
		
		@return: http response
		@rtype:	   template	  
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        tpl = TestTemplates.TemplateMessage()

        if self.cfg['agent-support']:
            layer_agent = TestTemplates.TemplateLayer('AGENT')
            layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
            layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
            tpl.addLayer(layer_agent)

        tpl.addLayer(AdapterIP.ip(more=AdapterIP.received()))
        tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received()))
        if self.ADP_TCP.cfg['ssl-support']:
            tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received()))
        headers = {}
        headers.update(httpHeaders)
        tpl.addLayer(
            templates.request(method=httpMethod,
                              uri=httpUri,
                              version=httpVersion,
                              headers=headers,
                              body=httpBody))

        return self.hasReceivedRequest(expected=tpl, timeout=timeout)
コード例 #8
0
	def encapsule(self, ip_event, ssh_event):
		"""
		encapsule template
		"""
		# prepare layers
		# ip 
		src = self.sourceIp
		dst = self.cfg['dst-ip']
		srcP = self.sourcePort
		dstP = self.cfg['dst-port']
		if ip_event == AdapterIP.received():
			src = self.cfg['dst-ip']
			dst = self.sourceIp
			srcP = self.cfg['dst-port']
			dstP = self.sourcePort
		layer_ip = AdapterIP.ip( source=src, destination=dst, version=self.cfg['sock-family'], more=ip_event ) 
		
		# tcp
		if ip_event == AdapterIP.received():
			moreTcp = AdapterTCP.received()
		else:
			moreTcp = AdapterTCP.sent()
		layer_tcp = AdapterTCP.tcp(source=srcP, destination=dstP, more=moreTcp)
		
		# ssh 
		layer_ssh = templates.ssh()
		layer_ssh.addMore(more=ssh_event)
		
		# prepare template
		if self.cfg['agent-support']:
			layer_agent= TestTemplatesLib.TemplateLayer('AGENT')
			layer_agent.addKey(name='name', data=self.cfg['agent']['name'] )
			layer_agent.addKey(name='type', data=self.cfg['agent']['type'] )
		
		tpl = TestTemplatesLib.TemplateMessage()
		if self.cfg['agent-support']:
			tpl.addLayer(layer=layer_agent)
		tpl.addLayer(layer=layer_ip)
		tpl.addLayer(layer=layer_tcp)
		tpl.addLayer(layer=layer_ssh)
		return tpl
コード例 #9
0
    def hasReceivedData(self, timeout=1.0, dataExpected=None):
        """
		Waits to receive "data" event until the end of the timeout
		
		@param timeout: time max to wait to receive event in second (default=1s)
		@type timeout: float	

		@param dataExpected: data expected (default=None)
		@type dataExpected:	string/operators/none			
		
		@return: an event matching with the template or None otherwise
		@rtype: templatemessage
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        layer_ip = AdapterIP.ip()
        layer_tcp = AdapterTCP.tcp()

        tpl = TestTemplatesLib.TemplateMessage()

        if self.cfg['agent-support']:
            layer_agent = TestTemplatesLib.TemplateLayer('AGENT')
            layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
            layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
            tpl.addLayer(layer_agent)

        tpl.addLayer(layer=layer_ip)
        tpl.addLayer(layer=layer_tcp)

        if self.tcp.cfg['proxy-enabled']:
            layer_sock = AdapterSOCKS.socks(more=AdapterSOCKS.received())
            tpl.addLayer(layer=layer_sock)

        layer_telnet = templates.telnet()
        if dataExpected is not None:
            layer_telnet.addKey(name='data', data=dataExpected)
        tpl.addLayer(layer=layer_telnet)
        evt = self.received(expected=tpl, timeout=timeout)
        return evt
コード例 #10
0
    def hasReceivedRequest(self, expected, timeout=1.0):
        """
		Wait to receive "response" until the end of the timeout.
		
		@param expected: response template
		@type expected: templatemessage/templatelayer

		@param timeout: time to wait in seconds (default=1s)
		@type timeout: float
	
		@return: http response or none otherwise
		@rtype:	templatemessage/templatelayer/none	
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        if expected is None:
            raise Exception(
                'has received response: expected template cannot be empty')
        tpl_expected = expected
        if isinstance(expected, TestTemplates.TemplateLayer):
            tpl_expected = TestTemplates.TemplateMessage()

            if self.cfg['agent-support']:
                layer_agent = TestTemplates.TemplateLayer('AGENT')
                layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
                layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
                tpl_expected.addLayer(layer_agent)

            tpl_expected.addLayer(AdapterIP.ip(more=AdapterIP.received()))
            tpl_expected.addLayer(AdapterTCP.tcp(more=AdapterTCP.received()))

            if self.ADP_TCP.cfg['ssl-support']:
                tpl_expected.addLayer(
                    AdapterSSL.ssl(more=AdapterSSL.received()))
            tpl_expected.addLayer(expected)
        evt = self.received(expected=tpl_expected, timeout=timeout)
        if evt is None:
            return None
        return evt
コード例 #11
0
    def hasReceivedData(self, data="", timeout=1.0):
        """
		Wait response until the end of the timeout.
		
		@param data: data
		@type data: string

		@param timeout: time to wait in seconds (default=1s)
		@type timeout: float
	
		@return: response
		@rtype: template	
		"""
        TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(),
                                     timeout=timeout)

        tpl_expected = templates_catalyst.catalyst_data(data=data)

        tpl = TestTemplates.TemplateMessage()

        if self.cfg['agent-support']:
            layer_agent = TestTemplates.TemplateLayer('AGENT')
            layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
            layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
            tpl.addLayer(layer_agent)

        tpl.addLayer(AdapterIP.ip(more=AdapterIP.received()))
        tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received()))
        tpl.addLayer(AdapterTelnet.dataIncoming())
        tpl.addLayer(tpl_expected)

        evt = self.received(expected=tpl, timeout=timeout)

        ret = None
        if evt is not None:
            ret = evt.get('TELNET', 'data')
        return ret
コード例 #12
0
    def hasReceivedRestResponse(self,
                                httpCode="200",
                                httpPhrase="OK",
                                httpVersion='HTTP/1.1',
                                timeout=1.0,
                                httpHeaders={},
                                httpBody=None):
        """
		Wait to receive "rest response" until the end of the timeout.
		
		@param httpCode: http code (default=200)
		@type httpCode: string
		
		@param httpPhrase: http phrase (default=OK)
		@type httpPhrase: string
		
		@param httpVersion: http version (default=HTTP/1.1)
		@type httpVersion: string
		
		@param httpHeaders: expected http headers
		@type httpHeaders: dict
		
		@param httpBody: expected body (default=None)
		@type httpBody: string/none
		
		@param timeout: time to wait in seconds (default=1s)
		@type timeout: float

		@return: http response
		@rtype:	template	
		"""
        TestAdapter.check_timeout(caller=TestAdapter.caller(), timeout=timeout)

        tpl = TestTemplates.TemplateMessage()

        if self.cfg['agent-support']:
            layer_agent = TestTemplates.TemplateLayer('AGENT')
            layer_agent.addKey(name='name', data=self.cfg['agent']['name'])
            layer_agent.addKey(name='type', data=self.cfg['agent']['type'])
            tpl.addLayer(layer_agent)

        tpl.addLayer(AdapterIP.ip(more=AdapterIP.received()))
        tpl.addLayer(AdapterTCP.tcp(more=AdapterTCP.received()))
        if self.ADP_HTTP.tcp().cfg['ssl-support']:
            tpl.addLayer(AdapterSSL.ssl(more=AdapterSSL.received()))
        headers = {
            'content-type':
            TestOperators.Contains(needle='application/json',
                                   AND=True,
                                   OR=False)
        }
        headers.update(httpHeaders)
        tpl.addLayer(
            AdapterHTTP.response(version=httpVersion,
                                 code=httpCode,
                                 phrase=httpPhrase,
                                 headers=headers,
                                 body=httpBody))
        tpl.addLayer(templates.response())

        return self.hasReceivedResponse(expected=tpl, timeout=timeout)