def test_clean_bin(): assert strutils.clean_bin(b"one") == b"one" assert strutils.clean_bin(b"\00ne") == b".ne" assert strutils.clean_bin(b"\nne") == b"\nne" assert strutils.clean_bin(b"\nne", False) == b".ne" assert strutils.clean_bin(u"\u2605".encode("utf8")) == b"..." assert strutils.clean_bin(u"one") == u"one" assert strutils.clean_bin(u"\00ne") == u".ne" assert strutils.clean_bin(u"\nne") == u"\nne" assert strutils.clean_bin(u"\nne", False) == u".ne" assert strutils.clean_bin(u"\u2605") == u"\u2605"
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 = strutils.clean_bin(text.decode(encoding, "strict")) except UnicodeDecodeError: text = strutils.clean_bin(text).decode(encoding, "strict") clean_line.append((style, text)) yield clean_line
def dump(self, data, hexdump): if hexdump: for line in strutils.hexdump(data): self("\t%s %s %s" % line) else: for i in strutils.clean_bin(data).split("\n"): self("\t%s" % i)
def tcp_message(self, flow): self.run_scripts("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(strutils.clean_bin(message.content), "debug")
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, strutils.clean_bin(tcp_msg.message)))
def __repr__(self): ret = repr(self.header) if self.payload: ret = ret + "\nPayload:\n" + strutils.clean_bin(self.payload).decode("ascii") return ret
def __repr__(self): ret = repr(self.header) if self.payload: ret = ret + "\nPayload:\n" + strutils.clean_bin( self.payload).decode("ascii") return ret