Beispiel #1
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 #2
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 #3
0
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1C
and this is the second one

0
"""

	print (80*'-')
	print ("HTTP Codec unit tests")
	print (80*'-')
	samples = [	
		('http.response', '\r\n'.join(httpResponse10.splitlines())),
		('http.response', '\r\n'.join(httpResponse11.splitlines())),
	]

	for codec, s in samples:
		print ()
		print (80*'-')
		print ("Testing:\n%s" % s)
		(_, _, decoded, summary) = CodecManager.incrementalDecode(codec, s, complete = True)
		print ("Decoded:\n%s\nSummary: %s" % (decoded, summary))
		(reencoded, summary) = CodecManager.encode(codec, decoded)
		print ("Reencoded:\n%s\nSummary: %s" % (reencoded, summary))
		print ("Original :\n%s" % s)

Beispiel #4
0
        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)
        s = binascii.unhexlify(s)
        (_, consumed, decoded,
         summary) = CodecManager.incrementalDecode('sua', s)
        print("Decoded: %s\nSummary: %s" % (decoded, summary))
        print("Consumed %s bytes ouf of %s" % (consumed, len(s)))
        (reencoded, summary) = CodecManager.encode('sua', decoded)
        print("Reencoded: %s\nSummary: %s" %
              (binascii.hexlify(reencoded), summary))
        print("Original : %s" % binascii.hexlify(s))
        assert (s == reencoded)
Beispiel #5
0
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1C
and this is the second one

0
"""

	print 80*'-'
	print "HTTP Codec unit tests"
	print 80*'-'
	samples = [	
		('http.response', '\r\n'.join(httpResponse10.splitlines())),
		('http.response', '\r\n'.join(httpResponse11.splitlines())),
	]

	for codec, s in samples:
		print
		print 80*'-'
		print "Testing:\n%s" % s
		(_, _, decoded, summary) = CodecManager.incrementalDecode(codec, s, complete = True)
		print "Decoded:\n%s\nSummary: %s" % (decoded, summary)
		(reencoded, summary) = CodecManager.encode(codec, decoded)
		print "Reencoded:\n%s\nSummary: %s" % (reencoded, summary)
		print "Original :\n%s" % s