Skip to content

Commit

Permalink
Improve output
Browse files Browse the repository at this point in the history
  • Loading branch information
helviojunior committed Jul 30, 2024
1 parent 042f8fb commit 42ccf77
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
53 changes: 40 additions & 13 deletions knowsmore/cmd/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Credentials(CmdBase):
db = None
out_file = None
out_path = None
out_file_json = None

def __init__(self):
super().__init__('credentials', 'Show cracked credentials')
Expand All @@ -32,6 +33,13 @@ def add_flags(self, flags: _ArgumentGroup):
action='store',
default='',
dest=f'out_file',
help=Color.s(
'Output file to save TXT file'))

flags.add_argument('--save-to-json',
action='store',
default='',
dest=f'out_file_json',
help=Color.s(
'Output file to save JSON data'))

Expand All @@ -56,6 +64,15 @@ def load_from_arguments(self, args: Namespace) -> bool:
self.out_file))
exit(1)

if args.out_file_json is not None and args.out_file_json.strip() != '':
self.out_file_json = Path(args.out_file_json).absolute()

if self.out_file is not None:
if os.path.exists(self.out_file):
Logger.pl('{!} {R}error: out file ({O}%s{R}) already exists {W}\r\n' % (
self.out_file))
exit(1)

if args.out_path is not None and args.out_path.strip() != '':
self.out_path = Path(args.out_path).absolute()

Expand Down Expand Up @@ -95,10 +112,10 @@ def run(self):
'rows': rows
})

if self.out_file is not None:
Color.pl('{?} {W}{D}Credentials saved at {W}{C}%s{W}{D}{W}' % self.out_file)
if self.out_file_json is not None:
Color.pl('{?} {W}{D}Credentials saved at {W}{C}%s{W}{D}{W}' % self.out_file_json)

with open(self.out_file, "a", encoding="UTF-8") as text_file:
with open(self.out_file_json, "a", encoding="UTF-8") as text_file:
text_file.write(json.dumps(
{
'data': data,
Expand All @@ -110,7 +127,7 @@ def run(self):
}
))

elif self.out_path is not None:
elif self.out_path is not None or self.out_file is not None:

for i, d in enumerate(data):
name = f"{i:03}_{Tools.sanitize_filename(d['description'])}"
Expand All @@ -129,15 +146,25 @@ def run(self):
else:
file_data += Tools.get_ansi_tabulated(d['rows'])

o = Ansi2Image(0, 0, font_name=Ansi2Image.get_default_font_name(), font_size=13)
o.loads(file_data)
o.min_margin = 10
o.max_margin = 30
o.calc_size(margin=0.01)
o.save_image(os.path.join(self.out_path, f'{name}.png'), format='PNG')

#with open(os.path.join(self.out_path, f'{name}.ansi.txt'), 'wb') as f:
# f.write(file_data.encode('utf-8', 'ignore'))
if self.out_path is not None:
o = Ansi2Image(0, 0, font_name=Ansi2Image.get_default_font_name(), font_size=13)
o.loads(file_data)
o.min_margin = 10
o.max_margin = 30
o.calc_size(margin=0.01)
o.save_image(os.path.join(self.out_path, f'{name}.png'), format='PNG')

if self.out_file is not None:
name = str(self.out_file).replace(Path(self.out_file).suffix, "").rstrip(". ")

with open(f'{name}.ansi.txt', 'ab') as f:
f.write(file_data.encode('utf-8', 'ignore'))
f.write(b"\n\n")

with open(f'{name}.txt', 'ab') as f:
f.write(f"{d['description']}\n".encode('utf-8', 'ignore'))
f.write(Tools.get_tabulated(d['rows']).encode('utf-8', 'ignore'))
f.write(b"\n\n")

else:

Expand Down
10 changes: 9 additions & 1 deletion knowsmore/util/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,12 @@ def json_serial(obj):
if isinstance(obj, bytes):
return base64.b64encode(obj).decode("UTF-8")

raise TypeError("Type %s not serializable" % type(obj))
raise TypeError("Type %s not serializable" % type(obj))

@staticmethod
def escape_ansi(text):
if text is None:
return ''

pattern = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
return pattern.sub('', text)

0 comments on commit 42ccf77

Please sign in to comment.