def initialize(probePaths = ["../plugins/probes"], codecPaths = ["../plugins/codecs"]):
	# CodecManager logging diversion
	CodecManager.instance().setLogCallback(logging.getLogger("Agent.Codec").debug)
	# ProbeImplementationManager logging diversion
	ProbeImplementationManager.setLogger(logging.getLogger("Agent.Probe"))
	# Loading plugins: probes & codecs
	localPath = os.path.normpath(os.path.realpath(os.path.dirname(sys.modules[globals()['__name__']].__file__)))
	scanPlugins([ ((os.path.isabs(x) and x) or os.path.normpath(os.path.realpath('%s/%s' % (os.getcwd(), x)))) for x in codecPaths], label = "codec")
	scanPlugins([ ((os.path.isabs(x) and x) or os.path.normpath(os.path.realpath('%s/%s' % (os.getcwd(), x)))) for x in probePaths], label = "probe")
Beispiel #2
0
    def onTriSend(self, message, sutAddress):
        try:
            # FIXME:
            # Should go to a configured codec instance instead.
            # (since we modify the message here...)
            if not message.has_key('version'):
                message['version'] = self['version']
            try:
                (encodedMessage,
                 summary) = CodecManager.encode('http.request', message)
            except Exception as e:
                raise ProbeImplementationManager.ProbeException(
                    'Invalid request message format: cannot encode HTTP request:\n%s'
                    % ProbeImplementationManager.getBacktrace())

            # Connect if needed
            if not self.isConnected():
                self.connect()

            # Send our payload
            self._httpConnection.send(encodedMessage)
            self.logSentPayload(summary, encodedMessage,
                                "%s:%s" % self._httpConnection.getpeername())
            # Now wait for a response asynchronously
            self.waitResponse()
        except Exception as e:
            raise ProbeImplementationManager.ProbeException(
                'Unable to send HTTP request: %s' % str(e))
Beispiel #3
0
 def _send(self, conn, data):
     encoder = self['default_encoder']
     if encoder:
         try:
             (data, summary) = CodecManager.encode(encoder, data)
         except Exception:
             raise ProbeImplementationManager.ProbeException(
                 'Cannot encode outgoing message using defaut encoder:\n%s'
                 % ProbeImplementationManager.getBacktrace())
         self.logSentPayload(summary, data,
                             "%s:%s" % conn.socket.getpeername())
     else:
         self.logSentPayload("TCP data", data,
                             "%s:%s" % conn.socket.getpeername())
     conn.socket.send(data)
Beispiel #4
0
    def run(self):
        buf = ''
        while not self._stopEvent.isSet():
            try:
                r, w, e = select.select([self._socket], [], [], 0.1)
                if self._socket in r:
                    read = self._socket.recv(1024 * 1024)
                    buf += read

                    decodedMessage = None

                    self._probe.getLogger().debug(
                        'data received (bytes %d), decoding attempt...' %
                        len(buf))
                    # If we are not disconnected, notify that the codec can still expect more data (complete = False)
                    (status, _, decodedMessage,
                     summary) = CodecManager.incrementalDecode(
                         'http.response', buf, complete=(not read))

                    if status == CodecManager.IncrementalCodec.DECODING_NEED_MORE_DATA:
                        if not read:
                            # We are disconnected.
                            raise Exception(
                                'Unable to decode response: additional data required, but connection lost'
                            )
                        else:
                            # Just wait
                            self._probe.getLogger().info(
                                'Waiting for additional data...')
                    elif status == CodecManager.IncrementalCodec.DECODING_ERROR:
                        raise Exception(
                            'Unable to decode response: decoding error')
                    else:
                        # DECODING_OK
                        fromAddr = "%s:%s" % (self._probe['host'],
                                              self._probe['port'])
                        self._probe.getLogger().debug(
                            'message decoded, enqueuing...')
                        self._probe.logReceivedPayload(summary, buf, fromAddr)
                        self._probe.triEnqueueMsg(decodedMessage, fromAddr)
                        self._stopEvent.set()
            except Exception as e:
                self._probe.getLogger().error(
                    'Error while waiting for http response: %s' % str(e))
                self._stopEvent.set()
        if not self._probe['maintain_connection']:
            # Well, actually this should depends on the HTTP protocol version...
            self._probe.disconnect()
Beispiel #5
0
    def onTriSend(self, message, sutAddress):
        try:
            # FIXME:
            # Should go to a configured codec instance instead.
            # (since we modify the message here... should be a copy instead)
            if not message.has_key('version'):
                message['version'] = self['version']
            if not message.has_key('headers'):
                message['headers'] = {}

            cseq = None

            # Non-strict mode: CSeq management: we add one if none is found
            if not self['strict_mode']:
                # Look for a CSeq
                for k, v in message['headers'].items():
                    if k.lower() == 'cseq':
                        cseq = str(v)
                if cseq is None:
                    # Generate and set a cseq
                    message['headers']['CSeq'] = self.generateCSeq()
                    cseq = str(message['headers']['CSeq'])

            try:
                encodedMessage, summary = CodecManager.encode(
                    'rtsp.request', message)
            except Exception as e:
                raise ProbeImplementationManager.ProbeException(
                    'Invalid request message format: cannot encode RTSP request'
                )

            # Connect if needed
            if not self.isConnected():
                self.connect()

            # Send our payload
            self._connection.send(encodedMessage)
            self.logSentPayload(summary, encodedMessage,
                                "%s:%s" % self._connection.getpeername())
            # Now wait for a response asynchronously
            self.waitResponse(cseq=cseq)
        except Exception as e:
            raise ProbeImplementationManager.ProbeException(
                'Unable to send RTSP request: %s' % str(e))
Beispiel #6
0
    def _preEnqueueMsg(self, conn, msg, addr, disconnected):
        decoder = self['default_decoder']
        if decoder:
            buf = conn.decodingBuffer + msg
            # Loop on multiple possible APDUs
            while buf:
                (status, consumedSize, decodedMessage,
                 summary) = CodecManager.incrementalDecode(
                     decoder, buf, complete=disconnected)
                if status == CodecManager.IncrementalCodec.DECODING_NEED_MORE_DATA:
                    # Do nothing. Just wait for new raw segments.
                    self.getLogger().info(
                        "Waiting for more raw segments to complete incremental decoding (using codec %s)."
                        % decoder)
                    conn.decodingBuffer = buf
                    break
                elif status == CodecManager.IncrementalCodec.DECODING_OK:
                    if consumedSize == 0:
                        consumedSize = len(buf)
                    # Store what was not consumed for later
                    conn.decodingBuffer = buf[consumedSize:]
                    # And raise the decoded message
                    self.logReceivedPayload(summary, buf[:consumedSize], addr)
                    self.triEnqueueMsg(decodedMessage, addr)
                    # make sure we update the local loop buffer, too
                    buf = conn.decodingBuffer
                else:  # status == CodecManager.IncrementalCodec.DECODING_ERROR:
                    self.getLogger().error(
                        "Unable to decode raw data with the default decoder (codec %s). Ignoring the segment."
                        % decoder)
                    break

        else:  # No default decoder
            if not disconnected:
                # disconnected is set if and only if the new data is empty - don't send this empty message
                self.logReceivedPayload("TCP data", msg, addr)
                self.triEnqueueMsg(msg, addr)
Beispiel #7
0
	== See Also ==
	
	The mapping between ASN.1 and Testerman structures is documented [Asn1ToTesterman here].

	"""
	PDU = Snmpv1Asn.Message

	def getSummary(self, message):
		try:
			# message is a sequence (version, community, data = choice(...)).
			return 'SNMP v1 %s' % (message['data'][0])
		except:
			return super.getSummary(message)

CodecManager.registerCodecClass('snmp.v1', Snmpv1Codec)


class Snmpv2cCodec(BerCodec.BerCodec):
	"""
	= Identification and Properties =
	
	Codec IDs: `snmp.v2c`
	
	Properties:
	|| '''Name''' || '''Type''' || '''Default value''' || '''Description''' ||
	
	= Overview =
	
	== Decoding ==
	
Beispiel #8
0
    def __init__(self):
        CodecManager.Codec.__init__(self)
        self.setDefaultProperty('filler', 0x0f)  # Usually 0x0f or 0x00

    def encode(self, template):
        """
		Template is a string of human readable digits
		"""
        return (string2tbcd(template, self['filler']), template)

    def decode(self, data):
        ret = tbcd2string(data)
        return (ret, ret)


CodecManager.registerCodecClass('tbcd', TbcdCodec)


class Packed7bitCodec(CodecManager.Codec):
    """
	Converts a template:
	type octetstring Template;
	"""
    def __init__(self):
        CodecManager.Codec.__init__(self)

    def encode(self, template):
        """
		Template is a string of human readable digits
		"""
        return (encode_7bitpacked(template, ), template)
Beispiel #9
0
##
# The Testerman codec interface to the codec
##

class SipBasicCodec(CodecManager.Codec):
	def encode(self, template):
		return (encodeMessage(template), getSummary(template))

	def decode(self, data):
		ret = decodeMessage(data)
		summary = getSummary(ret)
		return (ret, summary)

if __name__ != '__main__':
	CodecManager.registerCodecClass('sip.basic', SipBasicCodec)

else:


	sets = [

	# Message
	('Message',
	decodeMessage, encodeMessage, [
"""INVITE sip:146127953@esg SIP/2.0
Via: SIP/2.0/UDP 172.16.4.238:57516;branch=z9hG4bK-d8754z-55019e456f11e949-1---d8754z-;rport
Max-Forwards: 70
Contact: <sip:[email protected]:57516>
To: "146127953"<sip:146127953@esg>
From: "Seb"<sip:seb@esg>;tag=f4287c66
Beispiel #10
0
	== See Also ==
	
	The mapping between ASN.1 and Testerman structures is documented [Asn1ToTesterman here].

	"""
    PDU = Snmpv1Asn.Message

    def getSummary(self, message):
        try:
            # message is a sequence (version, community, data = choice(...)).
            return 'SNMP v1 %s' % (message['data'][0])
        except:
            return super.getSummary(message)


CodecManager.registerCodecClass('snmp.v1', Snmpv1Codec)


class Snmpv2cCodec(BerCodec.BerCodec):
    """
	= Identification and Properties =
	
	Codec IDs: `snmp.v2c`
	
	Properties:
	|| '''Name''' || '''Type''' || '''Default value''' || '''Description''' ||
	
	= Overview =
	
	== Decoding ==
	
Beispiel #11
0
##
# SMS-DELIVER
##
class SmsTpduSmsDeliverCodec(CodecManager.Codec):
    def __init__(self):
        CodecManager.Codec.__init__(self)

    def encode(self, template):
        return (encodeSmsDeliver(template), 'SMS-DELIVER')

    def decode(self, data):
        return (decodeSmsDeliver(data), 'SMS-DELIVER')


CodecManager.registerCodecClass('sms.tpdu.SMS-DELIVER', SmsTpduSmsDeliverCodec)

if __name__ == '__main__':
    import binascii

    def o(x):
        return binascii.unhexlify(x.replace(' ', ''))

    def oo(x):
        return binascii.hexlify(x)

    tpduSmsDeliver = "04039177f70010901091215571406fd3373b2c4fcf41311828168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1584c36a3d500"

    print(80 * '-')
    print("SMS TPDU SM-TL Codec unit tests")
    print(80 * '-')
Beispiel #12
0
	
	def decode(self, data):
		d = asn1.decode(self.PDU, data)
		summary = self.getSummary(d)
		return (d, summary)
	
	def getSummary(self, message):
		"""
		You may reimplement this function to get
		a more accurate summary according to the userland PDU.
		@type  message: Testerman userland message
		@param message: decoded message corresponding to your PDU
		"""
		return str(self.PDU.__class__)

"""
# Example:

# TCAP Codec
# To compile TcapAsn: ./py_output.py asn/tcap.asn > TcapAsn.py


import CodecManager

import ber.BerCodec as BerCodec
import ber.TcapAsn as TcapAsn

class TcapCodec(BerCodec.BerCodec):
	PDU = TcapAsn.TCMessage

	def getSummary(self, message):
Beispiel #13
0
            else:
                raise Exception("Invalid header in message (%s)" % str(l))

        ret['body'] = "\r\n".join(lines[i:])

        return (ret, self.getSummary(ret))

    def getSummary(self, template):
        """
		Returns the summary of the template representing an RTSP message.
		(meaningful, short, human-understandable description)
		"""
        return template['method']


CodecManager.registerCodecClass('rtsp.request', RtspRequestCodec)


class RtspResponseCodec(CodecManager.Codec):
    """
	See documentation in RtspRequestCodec.
	"""
    def __init__(self):
        CodecManager.Codec.__init__(self)
        self.setDefaultProperty('lower_case', False)
        self.setDefaultProperty('version', 'RTSP/1.0')

    def encode(self, template):
        status = template['status']
        reason = template['reason']
        version = template.get('version', self[version])
Beispiel #14
0
def instance():
	return CodecManager.instance()
Beispiel #15
0
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
##

##
# Codec manager - view from the TE
##

import CodecManager
import TestermanTCI

from CodecManager import CodecNotFoundException


CodecManager.instance().setLogCallback(lambda x: TestermanTCI.logInternal("CD: %s" % x))

def instance():
	return CodecManager.instance()

def alias(name, codec, **kwargs):
	return instance().alias(name, codec, **kwargs)

def registerCodecClass(name, class_):
	return instance().registerCodecClass(name, class_)

def encode(name, template):
	return instance().encode(name, template)

def decode(name, data):
	return instance().decode(name, data)
Beispiel #16
0
				media = { 'name_transport': line[2:], 'bandwidths': [], 'attributes': []}
				ret['media'].append(media)
				
			if media is None:
				# Session parameters
				if line.startswith('v='):	ret['version'] = line[2:]
				elif line.startswith('o='):	ret['originator'] = line[2:]
				elif line.startswith('s='):	ret['name'] = line[2:]
				elif line.startswith('i='):	ret['information'] = line[2:]
				elif line.startswith('u='):	ret['description_uri'] = line[2:]
				elif line.startswith('e='):	ret['email_address'] = line[2:]
				elif line.startswith('p='):	ret['phone_number'] = line[2:]
				elif line.startswith('c='):	ret['connection'] = line[2:]
				elif line.startswith('k='):	ret['key'] = line[2:]
				elif line.startswith('t='):	ret['time'] = line[2:]
				elif line.startswith('r='):	ret['repeats'].append(line[2:])
				elif line.startswith('b='):	ret['bandwidths'].append(line[2:])
				elif line.startswith('a='):	ret['attributes'].append(line[2:])
			else:
				if line.startswith('i='):	media['title'] = line[2:]
				elif line.startswith('c='):	media['connection'] = line[2:]
				elif line.startswith('k='):	media['key'] = line[2:]
				elif line.startswith('b='):	media['bandwidths'].append(line[2:])
				elif line.startswith('a='):	media['attributes'].append(line[2:])
		
		return (ret, 'SDP')
		
CodecManager.registerCodecClass('sdp', SdpCodec)


Beispiel #17
0
##
# MAP (2+) Codec
# To compile MapAsn: cd ber && ./py_output.py asn/AMP-All.asn > MapAsn.py
##

import CodecManager

import ber.BerCodec as BerCodec
import ber.MapAsn as MapAsn

# SMS management: SRI and MT Forward
class RoutingInfoForSM_ArgCodec(BerCodec.BerCodec):
	PDU = MapAsn.RoutingInfoForSM_Arg
	def getSummary(self, message): return 'RoutingInfoForSM-Arg'
CodecManager.registerCodecClass('map.RoutingInfoForSM-Arg', RoutingInfoForSM_ArgCodec)

class RoutingInfoForSM_ResCodec(BerCodec.BerCodec):
	PDU = MapAsn.RoutingInfoForSM_Res
	def getSummary(self, message): return 'RoutingInfoForSM-Res'
CodecManager.registerCodecClass('map.RoutingInfoForSM-Res', RoutingInfoForSM_ResCodec)

class MT_ForwardSM_ArgCodec(BerCodec.BerCodec):
	PDU = MapAsn.MT_ForwardSM_Arg
	def getSummary(self, message): return 'MT-ForwardSM-Arg'
CodecManager.registerCodecClass('map.MT-ForwardSM-Arg', MT_ForwardSM_ArgCodec)

class MT_ForwardSM_ResCodec(BerCodec.BerCodec):
	PDU = MapAsn.MT_ForwardSM_Res
	def getSummary(self, message): return 'MT-ForwardSM-Res'
CodecManager.registerCodecClass('map.MT-ForwardSM-Res', MT_ForwardSM_ResCodec)
Beispiel #18
0
##
# This file is part of Testerman, a test automation system.
# Copyright (c) 2008-2009 Sebastien Lefevre and other contributors
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
##

##
# ZLIB/GZIP codec.
##

import CodecManager

import zlib

class GZipCodec(CodecManager.Codec):
	def encode(self, template):
		return (zlib.compress(template), 'GZIP data')
	
	def decode(self, data):
		return (zlib.decompress(data), None)

CodecManager.registerCodecClass('gzip', GZipCodec)
Beispiel #19
0
##

import CodecManager

import pickle
import base64
import json

class PickleCodec(CodecManager.Codec):
	def encode(self, template):
		return (pickle.dumps(template), 'pickle data')
	
	def decode(self, data):
		return (pickle.loads(data), None)

CodecManager.registerCodecClass('pickle', PickleCodec)

class Base64Codec(CodecManager.Codec):
	def encode(self, template):
		return (base64.encodestring(template), 'base64 data')
	
	def decode(self, data):
		return (base64.decodestring(data), None)

CodecManager.registerCodecClass('base64', Base64Codec)


class JsonCodec(CodecManager.Codec):
	def encode(self, template):
		return (json.dumps(template), 'json data')
Beispiel #20
0
				raise Exception("Invalid header in message (%s)" % str(l))
		
		ret['body'] = "\r\n".join(lines[i:])
		
		return (ret, self.getSummary(ret))

	def getSummary(self, template):
		"""
		Returns the summary of the template representing an RTSP message.
		(meaningful, short, human-understandable description)
		"""
		return template['method']


		
CodecManager.registerCodecClass('rtsp.request', RtspRequestCodec)


class RtspResponseCodec(CodecManager.Codec):
	"""
	See documentation in RtspRequestCodec.
	"""
	def __init__(self):
		CodecManager.Codec.__init__(self)
		self.setDefaultProperty('lower_case', False)
		self.setDefaultProperty('version', 'RTSP/1.0')
	
	def encode(self, template):
		status = template['status']
		reason = template['reason']
		version = template.get('version', self[version])
Beispiel #21
0
	return '\r\n'.join(ret)


##
# The Testerman codec interface to the codec
##

class SipCodec(CodecManager.Codec):
	def encode(self, template):
		return (encodeMessage(template), 'SIP message')

	def decode(self, data):
		return (decodeMessage(data), 'SIP message')

if __name__ != '__main__':
	CodecManager.registerCodecClass('sip', SipCodec)

else:


	sets = [

	# NameAddr
	("NameAddr",
	decode_NameAddr, encode_NameAddr, [
	'sip:user@domain',
	'sip:123@domain;user=phone',
	'tel:12345',
	'<sip:user@domain>',
	'<sip:123@domain;user=phone>',
	'<tel:12345>',
Beispiel #22
0
				if bl < cl:
					return self.needMoreData()
				elif bl > cl:
					# Truncate the body
					ret['body'] = ret['body'][:cl]
		
		return self.decoded(ret, self.getSummary(ret))

	def getSummary(self, template):
		"""
		Returns the summary of the template representing an RTSP message.
		(meaningful, short, human-understandable description)
		"""
		return '%s %s' % (template.get('method', 'GET'), template['url'])
		
CodecManager.registerCodecClass('http.request', HttpRequestCodec)


class HttpResponseCodec(CodecManager.IncrementalCodec):
	"""
	= Identification and Properties =
	
	Codec IDs: `http.response`

	Properties:
	|| '''Name''' || '''Type''' || '''Default value''' || '''Description''' ||
	|| header_encoding || string  || `'iso8859-1'` || Encoding to use to encode header values. ||

	= Overview =

	This codec enables to encode/decode HTTP 1.0/1.1 responses,
Beispiel #23
0
            if ns: d['ns'] = ns
            return (tag, d)

        if hasElementChild:
            # only element children
            d = {'attributes': attributes, 'children': children}
            if ns: d['ns'] = ns
            return (tag, d)

        # no child, or a single text child
        d = {'attributes': attributes, 'cdata': cdata, 'value': value}
        if ns: d['ns'] = ns
        return (tag, d)


CodecManager.registerCodecClass('xml', XmlCodec)

if __name__ == '__main__':
    CodecManager.alias('xml.noprolog', 'xml', write_prolog=False)
    CodecManager.alias('xml.iso',
                       'xml',
                       encoding="iso-8859-1",
                       write_prolog=True)
    CodecManager.alias('xml.pretty', 'xml', prettyprint=True)

    sampleNoNs = """<?xml version="1.0" encoding="utf-8" ?>
<library owner="John Smith" administrator="Héléna Utf8 and Mickaël Orangina">
	<book isbn="88888-7777788">
		<author>Mickaël Orangina</author>
		<title locale="fr">Tonnerre sous les tropiques</title>
		<title locale="us">Tropic thunder</title>
Beispiel #24
0
# Copyright (c) 2008-2009 Sebastien Lefevre and other contributors
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
##

##
# ZLIB/GZIP codec.
##

import CodecManager

import zlib


class GZipCodec(CodecManager.Codec):
    def encode(self, template):
        return (zlib.compress(template), 'GZIP data')

    def decode(self, data):
        return (zlib.decompress(data), None)


CodecManager.registerCodecClass('gzip', GZipCodec)
Beispiel #25
0
        return (m.toNetwork(), m.summary())

    def incrementalDecode(self, data, complete):
        m = Message()
        try:
            m.fromNetwork(data)
        except IncompleteMessageException:
            return (self.DECODING_NEED_MORE_DATA, 0, None, None)
        except Exception:
            return (self.DECODING_ERROR, getDecodedLength()
                    or len(data), None, None)
        return (self.DECODING_OK, m.getDecodedLength(), m.toUserland(),
                m.summary())


CodecManager.registerCodecClass('sua', SuaCodec)

if __name__ == '__main__':
    print("SUA Codec unit tests")
    print(80 * '-')
    samples = [
        # SUA ASP active / Load-share, routing context 300
        "0100040100000018000b000800000002000600080000012c",
        # SUA ERR / Refused - management blocking, network appearance 0, some diagnostic info
        "0100000000000034000c00080000000d010d0008000000000007001c0100040100000018000b000800000002000600080000012c",
        # MAP SRI
        "01000701000000cc000600080000012c01150008000000000102002c0001000780010012000000040c0001042610101010100000800200080000057880030008000000080103002c0001000780010012000000040c000104262143658739000080020008000007d28003000800000006011600080000000001010008000000ff010b004962434804000200686b1a2818060700118605010101a00d600ba1090607040000010014026c1fa11d02010102012d30158007910026151101008101ff820791261010101010000000"
    ]

    for s in samples:
        print("Testing: %s" % s)
Beispiel #26
0
		"""
		tag = element.tagName
		
		# Now retrieve children, if any
		children = []
		for node in element.childNodes:
			if node.nodeType == node.ELEMENT_NODE:
				children.append(self._decode(node))
			# We should return only if these nodes are the first one.. ?
			elif node.nodeType == node.TEXT_NODE and node.nodeValue.strip():
				return (tag, node.nodeValue.strip())
			# Ignore other final node types (cdata, comments, ...)
		
		return (tag, children) 

CodecManager.registerCodecClass('xer.lite', XerLiteCodec)


if __name__ == '__main__':
	CodecManager.alias('xer.noprolog', 'xer.lite', write_prolog = False)
	CodecManager.alias('xer.iso', 'xer.lite', encoding = "iso-8859-1")
	CodecManager.alias('xer.canonical', 'xer.lite', canonical = True)
	
	sample = """<?xml version="1.0"?>
<library>
	<administrator>Héléna Utf8</administrator>
	<book>
		<isbn>89084-89089</isbn>
		<author>Philippe Kendall</author>
		<author>Mickaël Orangina</author>
		<title>Tropic thunder</title>
Beispiel #27
0
				if bl < cl:
					return self.needMoreData()
				elif bl > cl:
					# Truncate the body
					ret['body'] = ret['body'][:cl]
		
		return self.decoded(ret, self.getSummary(ret))

	def getSummary(self, template):
		"""
		Returns the summary of the template representing an RTSP message.
		(meaningful, short, human-understandable description)
		"""
		return '%s %s' % (template.get('method', 'GET'), template['url'])
		
CodecManager.registerCodecClass('http.request', HttpRequestCodec)


class HttpResponseCodec(CodecManager.IncrementalCodec):
	"""
	= Identification and Properties =
	
	Codec IDs: `http.response`

	Properties:
	|| '''Name''' || '''Type''' || '''Default value''' || '''Description''' ||
	|| header_encoding || string  || `'iso8859-1'` || Encoding to use to encode header values. ||

	= Overview =

	This codec enables to encode/decode HTTP 1.0/1.1 responses,
			cert, ski = SoapSecurity.loadCertFromPem(c)
			certificatesDb[ski] = cert
		
		cert = SoapSecurity.verifyMessage(doc, certificatesDb = certificatesDb)
		
		if not cert:
			raise Exception("This message has not been signed by the claimed party.")

		ret = {}
		ret['message'] = data
		ret['signedBy'] = ('certificate', cert.as_pem().strip())			
		return (ret, "XML data verified as signed by '%s'" % cert.get_subject().as_text())

	

CodecManager.registerCodecClass('soap11.ds', SoapDigitalSignatureCodec)


if __name__ == '__main__':
	CodecManager.alias('ds', 'soap11.ds')
	
	sampleEnv = """<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:ns="http://www.eservglobal.com/homesend/tews/2.0" xmlns:ns1="http://www.eservglobal.com/homesend/types/2.0" xmlns:ns2="http://www.eservglobal.com/homesend/tews/2.1" xmlns:ns3="http://www.eservglobal.com/homesend/types/2.1" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
<library owner="John Smith" administrator="Héléna Utf8 and Mickaël Orangina" xmlns="http://default" xmlns:b="http://base">
	<book isbn="88888-7777788">
		<b:author>Mickaël Orangina</b:author>
		<title locale="fr">Tonnerre sous les tropiques</title>
		<title locale="us">Tropic thunder</title>
		<title locale="es">No <i>habla</i> espagnol</title>
		<b:summary><![CDATA[This is a CDATA section <-> <-- this is a tie fighter]]></b:summary>
Beispiel #29
0
    def run(self):
        buf = ''
        while not self._stopEvent.isSet():
            try:
                r, w, e = select.select([self._socket], [], [], 0.1)
                if self._socket in r:
                    read = self._socket.recv(1024 * 1024)
                    buf += read

                    # In RTSP/1.0, content-length is mandatory if there is a body.
                    decodedMessage = None
                    try:
                        self._probe.getLogger().debug(
                            'data received (bytes %d), decoding attempt...' %
                            len(buf))
                        decodedMessage, summary = CodecManager.decode(
                            'rtsp.response',
                            buf,
                            lower_case=(not self._probe['strict_mode']))
                    except Exception as e:
                        # Incomplete message. Wait for more data.
                        self._probe.getLogger().debug('unable to decode: %s' %
                                                      str(e))
                        pass

                    if decodedMessage:
                        fromAddr = "%s:%s" % self._socket.getpeername()

                        # System log, always
                        self._probe.logReceivedPayload(summary, buf, fromAddr)

                        # Should we check the cseq locally ?
                        if self._cseq is None:
                            # Let the user check the cseq
                            self._probe.getLogger().info(
                                'message decoded, enqueuing without checking CSeq...'
                            )
                            self._probe.triEnqueueMsg(decodedMessage, fromAddr)
                            self._stopEvent.set()
                        else:
                            # Conditional enqueing - let's found the cseq
                            cseq = None
                            for k, v in decodedMessage['headers'].items():
                                # Stop on the first cseq header found
                                if k.lower() == 'cseq':
                                    cseq = v
                                    break
                            if cseq == self._cseq:
                                self._probe.getLogger().info(
                                    'message decoded, CSeq matched, enqueuing...'
                                )
                                self._probe.triEnqueueMsg(
                                    decodedMessage, fromAddr)
                                self._stopEvent.set()
                            else:
                                self._probe.getLogger().warning(
                                    'Invalid CSeq received. Not enqueuing, ignoring message'
                                )
                                buf = ''
                                decodedMessage = None
                                # Wait for a possible next message...

                    elif not read:
                        # Message not decoded, nothing to read anymore.
                        raise Exception(
                            'Incomplete message received, stream interrupted')
                        # .. and we should disconnect, too...

            except Exception as e:
                self._probe.getLogger().error(
                    'Error while waiting for rtsp response: %s' % str(e))
                self._stopEvent.set()
        if not self._probe['maintain_connection']:
            # Well, maintain connection in RTSP ?
            self._probe.disconnect()
Beispiel #30
0
	== See Also ==
	
	The mapping between ASN.1 and Testerman structures is documented [Asn1ToTesterman here].

	"""
	PDU = TcapAsn.TCMessage

	def getSummary(self, message):
		try:
			# message is expected to be a choice (begin/end/continue...)
			return 'TCAP %s' % message[0]
		except:
			return super.getSummary(message)

CodecManager.registerCodecClass('tcap', TcapCodec)

class DialoguePDUCodec(BerCodec.BerCodec):
	PDU = TcapDialoguePdusAsn.DialoguePDU
	def getSummary(self, message): return 'DialoguePDU'
CodecManager.registerCodecClass('tcap.DialoguePDU', DialoguePDUCodec)

if __name__ == '__main__':
	import binascii
	
	tcapBegin = \
	 "62644804000227846b3e283c060700118605010101a031602fa109060704000001001302be222820060704000001010101a015a01380099622123008016901f98106a807000000016c1ca11a02010102013b301204010f0405a3986c36028006a80700000001"

# From gsm_map_with_ussd_string.pcap sample
	tcapBegin2 = \
	 "626a48042f3b46026b3a2838060700118605010101a02d602b80020780a109060704000001001302be1a2818060704000001010101a00da00b80099656051124006913f66c26a12402010102013b301c04010f040eaa180da682dd6c31192d36bbdd468007917267415827f2"
Beispiel #31
0
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
##

##
# Codec manager - view from the TE
##

import CodecManager
import TestermanTCI

from CodecManager import CodecNotFoundException

CodecManager.instance().setLogCallback(
    lambda x: TestermanTCI.logInternal("CD: %s" % x))


def instance():
    return CodecManager.instance()


def alias(name, codec, **kwargs):
    return instance().alias(name, codec, **kwargs)


def registerCodecClass(name, class_):
    return instance().registerCodecClass(name, class_)


def encode(name, template):
Beispiel #32
0
def instance():
    return CodecManager.instance()
Beispiel #33
0
	"""
	def __init__(self):
		CodecManager.Codec.__init__(self)
		self.setDefaultProperty('filler', 0x0f) # Usually 0x0f or 0x00
		
	def encode(self, template):
		"""
		Template is a string of human readable digits
		"""
		return (string2tbcd(template, self['filler']), template)
	
	def decode(self, data):
		ret = tbcd2string(data)
		return (ret, ret)

CodecManager.registerCodecClass('tbcd', TbcdCodec)


class Packed7bitCodec(CodecManager.Codec):
	"""
	Converts a template:
	type octetstring Template;
	"""
	def __init__(self):
		CodecManager.Codec.__init__(self)
		
	def encode(self, template):
		"""
		Template is a string of human readable digits
		"""
		return (encode_7bitpacked(template,), template)
Beispiel #34
0
##
# Sample codecs
##

import CodecManager

import pickle
import base64


class PickleCodec(CodecManager.Codec):
    def encode(self, template):
        return (pickle.dumps(template), 'pickle data')

    def decode(self, data):
        return (pickle.loads(data), None)


CodecManager.registerCodecClass('pickle', PickleCodec)


class Base64Codec(CodecManager.Codec):
    def encode(self, template):
        return (base64.encodestring(template), 'base64 data')

    def decode(self, data):
        return (base64.decodestring(data), None)


CodecManager.registerCodecClass('base64', Base64Codec)
Beispiel #35
0
                ret['media'].append(media)

            if media is None:
                # Session parameters
                if line.startswith('v='): ret['version'] = line[2:]
                elif line.startswith('o='): ret['originator'] = line[2:]
                elif line.startswith('s='): ret['name'] = line[2:]
                elif line.startswith('i='): ret['information'] = line[2:]
                elif line.startswith('u='): ret['description_uri'] = line[2:]
                elif line.startswith('e='): ret['email_address'] = line[2:]
                elif line.startswith('p='): ret['phone_number'] = line[2:]
                elif line.startswith('c='): ret['connection'] = line[2:]
                elif line.startswith('k='): ret['key'] = line[2:]
                elif line.startswith('t='): ret['time'] = line[2:]
                elif line.startswith('r='): ret['repeats'].append(line[2:])
                elif line.startswith('b='): ret['bandwidths'].append(line[2:])
                elif line.startswith('a='): ret['attributes'].append(line[2:])
            else:
                if line.startswith('i='): media['title'] = line[2:]
                elif line.startswith('c='): media['connection'] = line[2:]
                elif line.startswith('k='): media['key'] = line[2:]
                elif line.startswith('b='):
                    media['bandwidths'].append(line[2:])
                elif line.startswith('a='):
                    media['attributes'].append(line[2:])

        return (ret, 'SDP')


CodecManager.registerCodecClass('sdp', SdpCodec)
Beispiel #36
0
	== See Also ==
	
	The mapping between ASN.1 and Testerman structures is documented [Asn1ToTesterman here].

	"""
	PDU = TcapAsn.TCMessage

	def getSummary(self, message):
		try:
			# message is expected to be a choice (begin/end/continue...)
			return 'TCAP %s' % message[0]
		except:
			return super.getSummary(message)

CodecManager.registerCodecClass('tcap', TcapCodec)

class DialoguePDUCodec(BerCodec.BerCodec):
	PDU = TcapDialoguePdusAsn.DialoguePDU
	def getSummary(self, message): return 'DialoguePDU'
CodecManager.registerCodecClass('tcap.DialoguePDU', DialoguePDUCodec)

if __name__ == '__main__':
	import binascii
	
	tcapBegin = \
	 "62644804000227846b3e283c060700118605010101a031602fa109060704000001001302be222820060704000001010101a015a01380099622123008016901f98106a807000000016c1ca11a02010102013b301204010f0405a3986c36028006a80700000001"

# From gsm_map_with_ussd_string.pcap sample
	tcapBegin2 = \
	 "626a48042f3b46026b3a2838060700118605010101a02d602b80020780a109060704000001001302be1a2818060704000001010101a00da00b80099656051124006913f66c26a12402010102013b301c04010f040eaa180da682dd6c31192d36bbdd468007917267415827f2"
Beispiel #37
0
			if ns: d['ns'] = ns
			return (tag, d)

		if hasElementChild:
			# only element children
			d = { 'attributes': attributes, 'children': children }
			if ns: d['ns'] = ns
			return (tag, d)

		# no child, or a single text child
		d = { 'attributes': attributes, 'cdata': cdata, 'value': value }
		if ns: d['ns'] = ns
		return (tag, d)
	

CodecManager.registerCodecClass('xml', XmlCodec)


if __name__ == '__main__':
	CodecManager.alias('xml.noprolog', 'xml', write_prolog = False)
	CodecManager.alias('xml.iso', 'xml', encoding = "iso-8859-1", write_prolog = True)
	CodecManager.alias('xml.pretty', 'xml', prettyprint = True)
	
	sampleNoNs = """<?xml version="1.0" encoding="utf-8" ?>
<library owner="John Smith" administrator="Héléna Utf8 and Mickaël Orangina">
	<book isbn="88888-7777788">
		<author>Mickaël Orangina</author>
		<title locale="fr">Tonnerre sous les tropiques</title>
		<title locale="us">Tropic thunder</title>
		<title locale="es">No <i>habla</i> espagnol</title>
		<summary><![CDATA[This is a CDATA section <-> <-- this is a tie fighter]]></summary>
Beispiel #38
0
		"""
		tag = element.tagName
		
		# Now retrieve children, if any
		children = []
		for node in element.childNodes:
			if node.nodeType == node.ELEMENT_NODE:
				children.append(self._decode(node))
			# We should return only if these nodes are the first one.. ?
			elif node.nodeType == node.TEXT_NODE and node.nodeValue.strip():
				return (tag, node.nodeValue.strip())
			# Ignore other final node types (cdata, comments, ...)
		
		return (tag, children) 

CodecManager.registerCodecClass('xer.lite', XerLiteCodec)


if __name__ == '__main__':
	CodecManager.alias('xer.noprolog', 'xer.lite', write_prolog = False)
	CodecManager.alias('xer.iso', 'xer.lite', encoding = "iso-8859-1")
	CodecManager.alias('xer.canonical', 'xer.lite', canonical = True)
	
	sample = """<?xml version="1.0"?>
<library>
	<administrator>Héléna Utf8</administrator>
	<book>
		<isbn>89084-89089</isbn>
		<author>Philippe Kendall</author>
		<author>Mickaël Orangina</author>
		<title>Tropic thunder</title>
Beispiel #39
0
	return ''.join(ret)

##
# SMS-DELIVER
##		
class SmsTpduSmsDeliverCodec(CodecManager.Codec):
	def __init__(self):
		CodecManager.Codec.__init__(self)

	def encode(self, template):
		return (encodeSmsDeliver(template), 'SMS-DELIVER')

	def decode(self, data):
		return (decodeSmsDeliver(data), 'SMS-DELIVER')

CodecManager.registerCodecClass('sms.tpdu.SMS-DELIVER', SmsTpduSmsDeliverCodec)


if __name__ == '__main__':
	import binascii
	def o(x):
		return binascii.unhexlify(x.replace(' ', ''))

	def oo(x):
		return binascii.hexlify(x)
	
	tpduSmsDeliver = "04039177f70010901091215571406fd3373b2c4fcf41311828168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1582c168bc562b1584c36a3d500"
	
	print 80*'-'
	print "SMS TPDU SM-TL Codec unit tests"
	print 80*'-'
Beispiel #40
0
            certificatesDb[ski] = cert

        cert = SoapSecurity.verifyMessage(doc, certificatesDb=certificatesDb)

        if not cert:
            raise Exception(
                "This message has not been signed by the claimed party.")

        ret = {}
        ret['message'] = data
        ret['signedBy'] = ('certificate', cert.as_pem().strip())
        return (ret, "XML data verified as signed by '%s'" %
                cert.get_subject().as_text())


CodecManager.registerCodecClass('soap11.ds', SoapDigitalSignatureCodec)

if __name__ == '__main__':
    CodecManager.alias('ds', 'soap11.ds')

    sampleEnv = """<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:ns="http://www.eservglobal.com/homesend/tews/2.0" xmlns:ns1="http://www.eservglobal.com/homesend/types/2.0" xmlns:ns2="http://www.eservglobal.com/homesend/tews/2.1" xmlns:ns3="http://www.eservglobal.com/homesend/types/2.1" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
<library owner="John Smith" administrator="Héléna Utf8 and Mickaël Orangina" xmlns="http://default" xmlns:b="http://base">
	<book isbn="88888-7777788">
		<b:author>Mickaël Orangina</b:author>
		<title locale="fr">Tonnerre sous les tropiques</title>
		<title locale="us">Tropic thunder</title>
		<title locale="es">No <i>habla</i> espagnol</title>
		<b:summary><![CDATA[This is a CDATA section <-> <-- this is a tie fighter]]></b:summary>
	</book>
Beispiel #41
0
##
# MAP (2+) Codec
# To compile MapAsn: cd ber && ./py_output.py asn/AMP-All.asn > MapAsn.py
##

import CodecManager

import ber.BerCodec as BerCodec
import ber.MapAsn as MapAsn

# SMS management: SRI and MT Forward
class RoutingInfoForSM_ArgCodec(BerCodec.BerCodec):
	PDU = MapAsn.RoutingInfoForSM_Arg
	def getSummary(self, message): return 'RoutingInfoForSM-Arg'
CodecManager.registerCodecClass('map.RoutingInfoForSM-Arg', RoutingInfoForSM_ArgCodec)

class RoutingInfoForSM_ResCodec(BerCodec.BerCodec):
	PDU = MapAsn.RoutingInfoForSM_Res
	def getSummary(self, message): return 'RoutingInfoForSM-Res'
CodecManager.registerCodecClass('map.RoutingInfoForSM-Res', RoutingInfoForSM_ResCodec)

class MT_ForwardSM_ArgCodec(BerCodec.BerCodec):
	PDU = MapAsn.MT_ForwardSM_Arg
	def getSummary(self, message): return 'MT-ForwardSM-Arg'
CodecManager.registerCodecClass('map.MT-ForwardSM-Arg', MT_ForwardSM_ArgCodec)

class MT_ForwardSM_ResCodec(BerCodec.BerCodec):
	PDU = MapAsn.MT_ForwardSM_Res
	def getSummary(self, message): return 'MT-ForwardSM-Res'
CodecManager.registerCodecClass('map.MT-ForwardSM-Res', MT_ForwardSM_ResCodec)