def diff(self, file, display_diff=False): """This function returns True is the given `file` is a phpsploit session which differs from current session. Otherwise, False is returned. Additionally, if `display_diff` is set, the session differences will be displayed in common unix `diff` style. """ # non-failing copy.deepcopy(self) equivalent: diff = self._obj_value(self._raw_value(self)) diff.update(file) diff = decolorize(diff).splitlines() orig = decolorize(self).splitlines() retval = diff != orig if display_diff: color = {' ': '%Reset', '-': '%Red', '+': '%Green', '?': '%Pink'} for line in difflib.Differ().compare(orig, diff): # dont be too much verbose... if line.startswith('?'): continue print(colorize(color[line[0]], line)) return retval
def diff(self, file=None, display_diff=False): """This function returns True is the given `file` is a phpsploit session which differs from current session. Otherwise, False is returned. Additionally, if `display_diff` is set, the session differences will be displayed in common unix `diff` style. """ if isinstance(file, Session): diff = self.deepcopy(file) else: if file is None: diff = Session() diff.File = self.File else: diff = self.deepcopy() diff.update(file) diff = decolorize(diff).splitlines() orig = decolorize(self).splitlines() if display_diff: color = {' ': '%Reset', '-': '%Red', '+': '%Green', '?': '%Pink'} if file is None: difflines = difflib.Differ().compare(diff, orig) else: difflines = difflib.Differ().compare(orig, diff) for line in difflines: # dont be too much verbose... if line.startswith('?'): continue print(colorize(color[line[0]], line)) return diff != orig
# if at least one owner/group is not '?', use unix-like formatter if any((x[2] + x[3]) != '??' for x in lines): rows_hdr = ["Mode", "Owner", "Group", "Size", "Last Modified", "Name"] rows = ([l[0], l[2], l[3], l[4], l[5], l[6]] for l in lines) # otherwise, use windows-like formatter else: rows_hdr = ["Mode", "Size", "Last Modified", "Name"] rows = ([x[1], x[4], x[5], x[6]] for x in lines) # format rows the right way rows = sorted(rows, key=(lambda elem: elem[-1])) rows.insert(0, rows_hdr) rows.insert(1, [("-" * len(elem)) for elem in rows_hdr]) # format and display output title header = "Listing: %s" % target if regex: header += " (matching r'%s')" % colorize("%White", regex) print("\n" + header + "\n" + ("=" * len(decolorize(header))) + "\n") widths = [max(map(len, col)) for col in zip(*rows)] for i, row in enumerate(rows): if i > 0: if row[0].startswith('d'): row[-1] = colorize("%BoldBlue", row[-1]) elif not row[0].startswith('-'): row[-1] = colorize("%BoldPink", row[-1]) print(" ".join((val.ljust(width) for val, width in zip(row, widths)))) print()
rows_hdr = ["Mode", "Owner", "Group", "Size", "Last Modified", "Name"] rows = ([l[0], l[2], l[3], l[4], l[5], l[6]] for l in lines) # otherwise, use windows-like formatter else: rows_hdr = ["Mode", "Size", "Last Modified", "Name"] rows = ([x[1], x[4], x[5], x[6]] for x in lines) # format rows the right way rows = sorted(rows, key=(lambda elem: elem[-1])) rows.insert(0, rows_hdr) rows.insert(1, [("-" * len(elem)) for elem in rows_hdr]) # format and display output title header = "Listing: %s" % target if regex: header += " (matching r'%s')" % colorize("%White", regex) print("\n" + header + "\n" + ("=" * len(decolorize(header))) + "\n") widths = [max(map(len, col)) for col in zip(*rows)] for i, row in enumerate(rows): if i > 0: if row[0].startswith('d'): row[-1] = colorize("%BoldBlue", row[-1]) elif not row[0].startswith('-'): row[-1] = colorize("%BoldPink", row[-1]) print(" ".join((val.ljust(width) for val, width in zip(row, widths)))) print() sys.exit(status)