Exemplo n.º 1
0
    def test_addDynamicSize(self):
        d = Defragmenter()

        d.addDynamicSize(10, 2, 2)

        ret = d.getMessage()
        self.assertIsNone(ret)

        d.addData(10, bytearray(
            b'\xee\xee' +   # header bytes
            b'\x00\x00' +   # remaining length
            # next message
            b'\xff\xff' +   # header bytes
            b'\x00\x01' +   # remaining length
            b'\xf0'))

        ret = d.getMessage()
        self.assertIsNotNone(ret)
        msgType, data = ret
        self.assertEqual(10, msgType)
        self.assertEqual(bytearray(b'\xee\xee\x00\x00'), data)

        ret = d.getMessage()
        self.assertIsNotNone(ret)
        msgType, data = ret
        self.assertEqual(10, msgType)
        self.assertEqual(bytearray(b'\xff\xff\x00\x01\xf0'), data)

        ret = d.getMessage()
        self.assertIsNone(ret)
Exemplo n.º 2
0
    def process(self, state):
        """Connect to a server"""
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        sock.connect((self.hostname, self.port))

        defragmenter = Defragmenter()
        defragmenter.addStaticSize(ContentType.alert, 2)
        defragmenter.addStaticSize(ContentType.change_cipher_spec, 1)
        defragmenter.addDynamicSize(ContentType.handshake, 1, 3)

        state.msg_sock = MessageSocket(sock, defragmenter)

        state.msg_sock.version = self.version
Exemplo n.º 3
0
    def process(self, state):
        """Connect to a server"""
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        sock.connect((self.hostname, self.port))

        defragmenter = Defragmenter()
        defragmenter.addStaticSize(ContentType.alert, 2)
        defragmenter.addStaticSize(ContentType.change_cipher_spec, 1)
        defragmenter.addDynamicSize(ContentType.handshake, 1, 3)

        state.msg_sock = MessageSocket(sock, defragmenter)

        state.msg_sock.version = self.version
Exemplo n.º 4
0
    def test_addDynamicSize_with_incomplete_payload(self):
        d = Defragmenter()

        d.addDynamicSize(10, 2, 2)

        d.addData(10, bytearray(b'\xee\xee\x00\x01'))

        self.assertIsNone(d.getMessage())

        d.addData(10, bytearray(b'\x99'))

        msgType, data = d.getMessage()
        self.assertEqual(10, msgType)
        self.assertEqual(bytearray(b'\xee\xee\x00\x01\x99'), data)
Exemplo n.º 5
0
    def process(self, state):
        """Connect to a server."""
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        sock.connect((self.hostname, self.port))
        # disable Nagle - we handle buffering and flushing ourselves
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

        # allow for later buffering of writes to the socket
        sock = BufferedSocket(sock)

        defragmenter = Defragmenter()
        defragmenter.addStaticSize(ContentType.alert, 2)
        defragmenter.addStaticSize(ContentType.change_cipher_spec, 1)
        defragmenter.addDynamicSize(ContentType.handshake, 1, 3)

        state.msg_sock = MessageSocket(sock, defragmenter)

        state.msg_sock.version = self.version
Exemplo n.º 6
0
    def test_addDynamicSize_with_two_streams(self):
        d = Defragmenter()

        d.addDynamicSize(9, 0, 3)
        d.addDynamicSize(10, 2, 2)

        d.addData(10, bytearray(b'\x44\x44\x00\x04'))
        d.addData(9, bytearray(b'\x00\x00\x02'))

        self.assertIsNone(d.getMessage())

        d.addData(9, bytearray(b'\x09'*2))
        d.addData(10, bytearray(b'\x10'*4))

        msgType, data = d.getMessage()
        self.assertEqual(msgType, 9)
        self.assertEqual(data, bytearray(b'\x00\x00\x02\x09\x09'))

        msgType, data = d.getMessage()
        self.assertEqual(msgType, 10)
        self.assertEqual(data, bytearray(b'\x44'*2 + b'\x00\x04' + b'\x10'*4))
Exemplo n.º 7
0
    def test_addDynamicSize_with_incomplete_header(self):
        d = Defragmenter()

        d.addDynamicSize(10, 2, 2)

        d.addData(10, bytearray(b'\xee'))

        self.assertIsNone(d.getMessage())

        d.addData(10, bytearray(b'\xee'))

        self.assertIsNone(d.getMessage())

        d.addData(10, bytearray(b'\x00'))

        self.assertIsNone(d.getMessage())

        d.addData(10, bytearray(b'\x00'))

        ret = d.getMessage()
        self.assertIsNotNone(ret)
        msgType, data = ret
        self.assertEqual(10, msgType)
        self.assertEqual(bytearray(b'\xee\xee\x00\x00'), data)
Exemplo n.º 8
0
    def test_addDynamicSize_with_invalid_offset(self):
        d = Defragmenter()

        with self.assertRaises(ValueError):
            d.addDynamicSize(1, -1, 2)
Exemplo n.º 9
0
    def test_addDynamicSize_with_double_type(self):
        d = Defragmenter()

        d.addDynamicSize(1, 0, 1)
        with self.assertRaises(ValueError):
            d.addDynamicSize(1, 2, 2)
Exemplo n.º 10
0
    def scan(self):
        """Perform a scan on server."""
        defragger = Defragmenter()
        defragger.addStaticSize(ContentType.change_cipher_spec, 1)
        defragger.addStaticSize(ContentType.alert, 2)
        defragger.addDynamicSize(ContentType.handshake, 1, 3)

        try:
            raw_sock = socket.create_connection((self.host, self.port), 5)
        except socket.error as e:
            return [e]

        sock = MessageSocket(raw_sock, defragger)

        if self.hostname is not None:
            client_hello = self.hello_gen(bytearray(self.hostname, 'utf-8'))
        else:
            client_hello = self.hello_gen(None)

        # record layer version - TLSv1.x
        # use the version from configuration, if present, or default to the
        # RFC recommended (3, 1) for TLS and (3, 0) for SSLv3
        if hasattr(client_hello, 'record_version'):
            sock.version = client_hello.record_version
        elif hasattr(self.hello_gen, 'record_version'):
            sock.version = self.hello_gen.record_version
        elif client_hello.client_version > (3, 1):  # TLS1.0
            sock.version = (3, 1)
        else:
            sock.version = client_hello.client_version

        # we don't want to send invalid messages (SSLv2 hello in SSL record
        # layer), so set the record layer version to SSLv2 if the hello is
        # of SSLv2 format
        if client_hello.ssl2:
            sock.version = (0, 2)

        # save the record version used in the end for later analysis
        client_hello.record_version = sock.version

        messages = [client_hello]

        handshake_parser = HandshakeParser()

        try:
            sock.sendMessageBlocking(client_hello)
        except socket.error as e:
            messages.append(e)
            return messages
        except TLSAbruptCloseError as e:
            sock.sock.close()
            messages.append(e)
            return messages

        # get all the server messages that affect connection, abort as soon
        # as they've been read
        try:
            while True:
                header, parser = sock.recvMessageBlocking()

                if header.type == ContentType.alert:
                    alert = Alert()
                    alert.parse(parser)
                    alert.record_version = header.version
                    messages += [alert]
                elif header.type == ContentType.handshake:
                    msg = handshake_parser.parse(parser)
                    msg.record_version = header.version
                    messages += [msg]
                    if isinstance(msg, ServerHelloDone):
                        return messages
                else:
                    raise TypeError("Unknown content type: {0}".format(
                        header.type))
        except (TLSAbruptCloseError, TLSIllegalParameterException, ValueError,
                TypeError, socket.error, SyntaxError) as e:
            messages += [e]
            return messages
        finally:
            try:
                sock.sock.close()
            except (socket.error, OSError):
                pass
Exemplo n.º 11
0
    def scan(self):
        """Perform a scan on server."""
        defragger = Defragmenter()
        defragger.addStaticSize(ContentType.change_cipher_spec, 1)
        defragger.addStaticSize(ContentType.alert, 2)
        defragger.addDynamicSize(ContentType.handshake, 1, 3)

        try:
            raw_sock = socket.create_connection((self.host, self.port), 5)
        except socket.error as e:
            return [e]

        sock = MessageSocket(raw_sock, defragger)

        if self.hostname is not None:
            client_hello = self.hello_gen(bytearray(self.hostname,
                                                    'utf-8'))
        else:
            client_hello = self.hello_gen(None)

        # record layer version - TLSv1.x
        # use the version from configuration, if present, or default to the
        # RFC recommended (3, 1) for TLS and (3, 0) for SSLv3
        if hasattr(client_hello, 'record_version'):
            sock.version = client_hello.record_version
        elif hasattr(self.hello_gen, 'record_version'):
            sock.version = self.hello_gen.record_version
        elif client_hello.client_version > (3, 1):  # TLS1.0
            sock.version = (3, 1)
        else:
            sock.version = client_hello.client_version

        # we don't want to send invalid messages (SSLv2 hello in SSL record
        # layer), so set the record layer version to SSLv2 if the hello is
        # of SSLv2 format
        if client_hello.ssl2:
            sock.version = (0, 2)

        # save the record version used in the end for later analysis
        client_hello.record_version = sock.version

        messages = [client_hello]

        handshake_parser = HandshakeParser()

        try:
            sock.sendMessageBlocking(client_hello)
        except socket.error as e:
            messages.append(e)
            return messages
        except TLSAbruptCloseError as e:
            sock.sock.close()
            messages.append(e)
            return messages

        # get all the server messages that affect connection, abort as soon
        # as they've been read
        try:
            while True:
                header, parser = sock.recvMessageBlocking()

                if header.type == ContentType.alert:
                    alert = Alert()
                    alert.parse(parser)
                    alert.record_version = header.version
                    messages += [alert]
                elif header.type == ContentType.handshake:
                    msg = handshake_parser.parse(parser)
                    msg.record_version = header.version
                    messages += [msg]
                    if isinstance(msg, ServerHelloDone):
                        return messages
                else:
                    raise TypeError("Unknown content type: {0}"
                                    .format(header.type))
        except (TLSAbruptCloseError, TLSIllegalParameterException,
                ValueError, TypeError, socket.error, SyntaxError) as e:
            messages += [e]
            return messages
        finally:
            try:
                sock.sock.close()
            except (socket.error, OSError):
                pass