def getExpectedTemplate(self, tpl, versionIp=None, sourceIp=None, destinationIp=None, sourcePort=None, destinationPort=None): """ Return an expected template with ip and udp layers """ # prepare layers defaultVer = "%s" % self.cfg['sock-family'] if versionIp is not None: defaultVer = versionIp layer_ip = AdapterIP.ip( source=sourceIp, destination=destinationIp, version=defaultVer ) layer_udp = templates.udp(source=sourcePort, destination=destinationPort) layer_udp.addMore(more=tpl) # 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_udp) return tpl
def onInactivityTimeout(self): """ """ tpl = TestTemplatesLib.TemplateMessage() tpl.addLayer(layer=AdapterIP.ip(more=AdapterIP.received())) tpl.addLayer(layer=templates.udp(more=templates.received())) self.onInactivity(lower=tpl)
def encapsule(self, ip_event, udp_event, src_ip=None, src_port=None): """ """ # prepare layers src = self.sourceIp dst = self.cfg['dst-ip'] srcP = self.sourcePort dstP = self.cfg['dst-port'] if src_ip is not None: src = src_ip dst = self.sourceIp if src_port is not None: srcP = src_port dstP = self.sourcePort layer_ip = AdapterIP.ip( source=src, destination=dst, version= "%s" % self.cfg['sock-family'], more=ip_event ) layer_udp = templates.udp(source=srcP, destination=dstP) layer_udp.addMore(more=udp_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_udp) return tpl
def getExpectedTemplate(self, tpl, versionIp=None, sourceIp=None, destinationIp=None, sourcePort=None, destinationPort=None): """ Return an expected template with ip and udp 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) layer_udp = templates.udp(source=sourcePort, destination=destinationPort) layer_udp.addMore(more=tpl) # prepare template tpl = TestTemplatesLib.TemplateMessage() tpl.addLayer(layer=layer_ip) tpl.addLayer(layer=layer_udp) return tpl
def encapsule(self, ip_event, udp_event, src_ip=None, src_port=None): """ """ # prepare layers src = self.sourceIp srcP = self.sourcePort dst = '' dstP = '' if src_ip is not None: src = src_ip dst = self.sourceIp if src_port is not None: srcP = src_port dstP = self.sourcePort layer_ip = AdapterIP.ip(source=src, destination=dst, version=self.cfg['sock-family'], more=ip_event) layer_udp = templates.udp(source=srcP, destination=dstP) layer_udp.addMore(more=udp_event) # prepare template tpl = TestTemplatesLib.TemplateMessage() tpl.addLayer(layer=layer_ip) tpl.addLayer(layer=layer_udp) return tpl
def hasReceivedPacket(self, timeout=1.0, dstIp=None, srcIp=None, srcPort=None, dstPort=None, data=None, controlBits=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 dstIp: destination ip @type dstIp: none/string/operators @param srcIp: source ip @type srcIp: none/string/operators @param srcPort: source port @type srcPort: none/integer/operators @param dstPort: destination port @type dstPort: none/integer/operators @param data: upper data @type data: none/string/operators @param controlBits: control bits @type controlBits: none/string/operators @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) # prepare the expected template ether_tpl = AdapterEthernet.ethernet() expected = self.encapsule(layer=ether_tpl) ip_tpl = AdapterIP.ip(source=srcIp, destination=dstIp) expected.addLayer(ip_tpl) tcp_tpl = templates.tcp(source=srcPort, destination=dstPort, control_bits=controlBits) expected.addLayer(tcp_tpl) # wait evt = self.received(expected=expected, timeout=timeout) if evt is None: return None return evt
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
def getExpectedTemplate(self, layer_rtp, versionIp=None, sourceIp=None, destinationIp=None, sourcePort=None, destinationPort=None): """ """ layer_ip = AdapterIP.ip(source=sourceIp, destination=destinationIp, version=versionIp) layer_udp = AdapterUDP.udp(source=sourcePort, destination=destinationPort) # prepare template tpl_msg = TestTemplatesLib.TemplateMessage() tpl_msg.addLayer(layer=layer_ip) tpl_msg.addLayer(layer=layer_udp) tpl_msg.addLayer(layer=layer_rtp) return tpl_msg
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
def onReceiving(self, data, fromAddr, toAddr, lower): """ Function to overwrite Called on incoming data @param data: data received @type data: string @param lower: template data received @type lower: templatemessage """ # extract transport info self.lastActivity = time.time() srcIp, srcPort = fromAddr dstIp, dstPort = toAddr # the separator feature is disabled, to nothing if self.cfg['sep-disabled']: try: self.handleIncomingData(data=data, lower=lower) except Exception as e: self.error( "on handle incoming udp data, upper layer problem: %s" % str(e)) else: # bufferize data if self.buf.has_key(fromAddr): self.buf[fromAddr] = ''.join([self.buf[fromAddr], data]) else: self.buf[fromAddr] = data # split data with the separator datas = self.buf[fromAddr].split(self.cfg['sep-in']) for d in datas[:-1]: udp_data = d + self.cfg['sep-in'] udp_data_size = len(udp_data) # construct high template and log it tpl = TestTemplatesLib.TemplateMessage() tpl.addLayer( layer=AdapterIP.ip(source=srcIp, destination=dstIp, more=AdapterIP.received(), version=self.cfg['ip-version'])) if self.logEventReceived: udp_layer = templates.udp( source=srcPort, destination=dstPort, more=templates.received( data=udp_data, data_length=str(udp_data_size))) else: udp_layer = templates.udp( source=srcPort, destination=dstPort, more=templates.received( data_length=str(udp_data_size))) tpl.addLayer(layer=udp_layer) if self.logEventReceived: tpl.addRaw(raw=udp_data) self.logRecvEvent(shortEvt='data reassembled', tplEvt=tpl) # handle data self.handleIncomingData(udp_data, lower=tpl) self.buf[fromAddr] = datas[-1]
def hasReceivedResponse(self, timeout=1.0, leap=None, version=None, mode=None, stratum=None, poll=None, precision=None, rootDelay=None, rootDispersion=None, refId=None, refTimestamp=None, origTimestamp=None, recvTimestamp=None, txTimestamp=None, destTimestamp=None, offset=None, delay=None, leapMsg=None, modeMsg=None, stratumMsg=None, refIdMsg=None): """ Waits to receive "ntp response" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @param offset: offset (default=None) @type offset: integer/none @param delay: delay (default=None) @type delay: integer/none @param mode: mode (default=None) @type mode: integer/none @param modeMsg: mode tetx (default=None) @type modeMsg: string/none @param version: version (default=None) @type version: integer/none @param stratumMsg: stratum text (default=None) @type stratumMsg: string/none @param stratum: stratum (default=None) @type stratum: integer/none @param leap: leap (default=None) @type leap: integer /none @param leapMsg: leap text (default=None) @type leapMsg: string/none @param poll: poll (default=None) @type poll: integer/none @param precision: precision (default=None) @type precision: integer/none @param rootDelay: rootDelay (default=None) @type rootDelay: integer/none @param rootDispersion: rootDispersion (default=None) @type rootDispersion: integer/none @param refId: refId (default=None) @type refId: integer/none @param refIdMsg: refId text (default=None) @type refIdMsg: string /none @param refTimestamp: refTimestamp (default=None) @type refTimestamp: integer/none @param origTimestamp: origTimestamp (default=None) @type origTimestamp: integer/none @param recvTimestamp: recvTimestamp (default=None) @type recvTimestamp: integer/none @param txTimestamp: txTimestamp (default=None) @type txTimestamp: integer/none @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapter.check_timeout(caller=TestAdapter.caller(), timeout=timeout) layer_ip = AdapterIP.ip() layer_udp = AdapterUDP.udp() layer_ntp = templates.msg(leap=leap, version=version, mode=mode, stratum=stratum, poll=poll, precision=precision, rootDelay=rootDelay, rootDispersion=rootDispersion, refId=refId, refTimestamp=refTimestamp, origTimestamp=origTimestamp, recvTimestamp=recvTimestamp, txTimestamp=txTimestamp, destTimestamp=destTimestamp, offset=offset, delay=delay, leapMsg=leapMsg, modeMsg=modeMsg, stratumMsg=stratumMsg, refIdMsg=refIdMsg) 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(layer=layer_ip) tpl.addLayer(layer=layer_udp) tpl.addLayer(layer=layer_ntp) return self.received(expected=tpl, timeout=timeout)
def hasReceivedPacket(self, timeout=1.0, icmpCode=None, icmpType=None, icmpId=None, icmpSeq=None, icmpData=None, icmpTimeOrig=None, icmpTimeReceive=None, icmpTimeTransmit=None, icmpMask=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 icmpCode: icmp code @type icmpCode: none/string/operators @param icmpType: icmp type @type icmpType: none/string/operators @param icmpId: icmp identifier @type icmpId: none/string/operators @param icmpSeq: icmp sequence number @type icmpSeq: none/string/operators @param icmpData: icmp data @type icmpData: none/string/operators @param icmpTimeOrig: originate timestamp @type icmpTimeOrig: none/string/operators @param icmpTimeReceive: receive timestamp @type icmpTimeReceive: none/string/operators @param icmpTimeTransmit: transmit timestamp @type icmpTimeTransmit: none/string/operators @param icmpMask: icmp mask @type icmpMask: none/string/operators @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) expected = 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']) expected.addLayer(layer_agent) # add layer ethernet layer_ether = AdapterEthernet.ethernet() expected.addLayer(layer=layer_ether) # add layer ip layer_ip = AdapterIP.ip() expected.addLayer(layer=layer_ip) # add layer icmp layer_icmp = templates.icmp(code=icmpCode, tp=icmpType, id=icmpId, seq_num=icmpSeq, data=icmpData, time_orig=icmpTimeOrig, time_rx=icmpTimeReceive, time_tx=icmpTimeTransmit, mask=icmpMask) expected.addLayer(layer=layer_icmp) # try to match the template evt = self.received(expected=expected, timeout=timeout) return evt