示例#1
0
def test_clean_bin():
    assert utils.clean_bin(b"one") == b"one"
    assert utils.clean_bin(b"\00ne") == b".ne"
    assert utils.clean_bin(b"\nne") == b"\nne"
    assert utils.clean_bin(b"\nne", False) == b".ne"
    assert utils.clean_bin(u"\u2605".encode("utf8")) == b"..."

    assert utils.clean_bin(u"one") == u"one"
    assert utils.clean_bin(u"\00ne") == u".ne"
    assert utils.clean_bin(u"\nne") == u"\nne"
    assert utils.clean_bin(u"\nne", False) == u".ne"
    assert utils.clean_bin(u"\u2605") == u"\u2605"
示例#2
0
def test_cleanBin():
    assert utils.clean_bin(b"one") == b"one"
    assert utils.clean_bin(b"\00ne") == b".ne"
    assert utils.clean_bin(b"\nne") == b"\nne"
    assert utils.clean_bin(b"\nne", False) == b".ne"
    assert utils.clean_bin(u"\u2605".encode("utf8")) == b"..."

    assert utils.clean_bin(u"one") == u"one"
    assert utils.clean_bin(u"\00ne") == u".ne"
    assert utils.clean_bin(u"\nne") == u"\nne"
    assert utils.clean_bin(u"\nne", False) == u".ne"
    assert utils.clean_bin(u"\u2605") == u"\u2605"
示例#3
0
def safe_to_print(lines, encoding="utf8"):
    """
    Wraps a content generator so that each text portion is a *safe to print* unicode string.
    """
    for line in lines:
        clean_line = []
        for (style, text) in line:
            try:
                text = clean_bin(text.decode(encoding, "strict"))
            except UnicodeDecodeError:
                text = clean_bin(text).decode(encoding, "strict")
            clean_line.append((style, text))
        yield clean_line
示例#4
0
def safe_to_print(lines, encoding="utf8"):
    """
    Wraps a content generator so that each text portion is a *safe to print* unicode string.
    """
    for line in lines:
        clean_line = []
        for (style, text) in line:
            try:
                text = clean_bin(text.decode(encoding, "strict"))
            except UnicodeDecodeError:
                text = clean_bin(text).decode(encoding, "strict")
            clean_line.append((style, text))
        yield clean_line
示例#5
0
    def __call__(self):
        self.connect()

        buf = memoryview(bytearray(self.chunk_size))

        client = self.client_conn.connection
        server = self.server_conn.connection
        conns = [client, server]

        try:
            while True:
                r = ssl_read_select(conns, 10)
                for conn in r:
                    dst = server if conn == client else client

                    size = conn.recv_into(buf, self.chunk_size)
                    if not size:
                        conns.remove(conn)
                        # Shutdown connection to the other peer
                        if isinstance(conn, SSL.Connection):
                            # We can't half-close a connection, so we just close everything here.
                            # Sockets will be cleaned up on a higher level.
                            return
                        else:
                            dst.shutdown(socket.SHUT_WR)

                        if len(conns) == 0:
                            return
                        continue

                    tcp_message = TcpMessage(
                        self.client_conn, self.server_conn,
                        self.client_conn if dst == server else self.server_conn,
                        self.server_conn if dst == server else self.client_conn,
                        buf[:size].tobytes())
                    self.channel.ask("tcp_message", tcp_message)
                    dst.sendall(tcp_message.message)

                    if self.logging:
                        # log messages are prepended with the client address,
                        # hence the "weird" direction string.
                        if dst == server:
                            direction = "-> tcp -> {}".format(repr(self.server_conn.address))
                        else:
                            direction = "<- tcp <- {}".format(repr(self.server_conn.address))
                        data = clean_bin(tcp_message.message)
                        self.log(
                            "{}\r\n{}".format(direction, data),
                            "info"
                        )

        except (socket.error, TcpException, SSL.Error) as e:
            six.reraise(
                ProtocolException,
                ProtocolException("TCP connection closed unexpectedly: {}".format(repr(e))),
                sys.exc_info()[2]
            )
示例#6
0
    def __call__(self):
        self.connect()

        buf = memoryview(bytearray(self.chunk_size))

        client = self.client_conn.connection
        server = self.server_conn.connection
        conns = [client, server]

        try:
            while not self.channel.should_exit.is_set():
                r = ssl_read_select(conns, 10)
                for conn in r:
                    dst = server if conn == client else client

                    size = conn.recv_into(buf, self.chunk_size)
                    if not size:
                        conns.remove(conn)
                        # Shutdown connection to the other peer
                        if isinstance(conn, SSL.Connection):
                            # We can't half-close a connection, so we just close everything here.
                            # Sockets will be cleaned up on a higher level.
                            return
                        else:
                            dst.shutdown(socket.SHUT_WR)

                        if len(conns) == 0:
                            return
                        continue

                    tcp_message = TcpMessage(
                        self.client_conn, self.server_conn, self.client_conn if
                        dst == server else self.server_conn, self.server_conn
                        if dst == server else self.client_conn,
                        buf[:size].tobytes())
                    self.channel.ask("tcp_message", tcp_message)
                    dst.sendall(tcp_message.message)

                    if self.logging:
                        # log messages are prepended with the client address,
                        # hence the "weird" direction string.
                        if dst == server:
                            direction = "-> tcp -> {}".format(
                                repr(self.server_conn.address))
                        else:
                            direction = "<- tcp <- {}".format(
                                repr(self.server_conn.address))
                        data = clean_bin(tcp_message.message)
                        self.log("{}\r\n{}".format(direction, data), "info")

        except (socket.error, TcpException, SSL.Error) as e:
            six.reraise(
                ProtocolException,
                ProtocolException(
                    "TCP connection closed unexpectedly: {}".format(repr(e))),
                sys.exc_info()[2])
示例#7
0
 def tcp_message(self, flow):
     self.run_script_hook("tcp_message", flow)
     message = flow.messages[-1]
     direction = "->" if message.from_client else "<-"
     self.add_event("{client} {direction} tcp {direction} {server}".format(
         client=repr(flow.client_conn.address),
         server=repr(flow.server_conn.address),
         direction=direction,
     ), "info")
     self.add_event(clean_bin(message.content), "debug")
示例#8
0
def tcp_message(ctx, tcp_msg):
    modified_msg = tcp_msg.message.replace("foo", "bar")

    is_modified = False if modified_msg == tcp_msg.message else True
    tcp_msg.message = modified_msg

    print("[tcp_message{}] from {} {} to {} {}:\r\n{}".format(
        " (modified)" if is_modified else "",
        "client" if tcp_msg.sender == tcp_msg.client_conn else "server",
        tcp_msg.sender.address,
        "server" if tcp_msg.receiver == tcp_msg.server_conn else "client",
        tcp_msg.receiver.address, clean_bin(tcp_msg.message)))
示例#9
0
def tcp_message(ctx, tcp_msg):
    modified_msg = tcp_msg.message.replace("foo", "bar")

    is_modified = False if modified_msg == tcp_msg.message else True
    tcp_msg.message = modified_msg

    print("[tcp_message{}] from {} {} to {} {}:\r\n{}".format(
        " (modified)" if is_modified else "",
        "client" if tcp_msg.sender == tcp_msg.client_conn else "server",
        tcp_msg.sender.address,
        "server" if tcp_msg.receiver == tcp_msg.server_conn else "client",
        tcp_msg.receiver.address, clean_bin(tcp_msg.message)))
示例#10
0
 def human_readable(self):
     ret = self.header.human_readable()
     if self.payload:
         ret = ret + "\nPayload:\n" + utils.clean_bin(self.payload)
     return ret
示例#11
0
 def __repr__(self):
     ret = repr(self.header)
     if self.payload:
         ret = ret + "\nPayload:\n" + utils.clean_bin(
             self.payload).decode("ascii")
     return ret
示例#12
0
 def __repr__(self):
     ret = repr(self.header)
     if self.payload:
         ret = ret + "\nPayload:\n" + utils.clean_bin(self.payload).decode("ascii")
     return ret