|
1 | 1 | import os |
2 | 2 | import sys |
3 | | -import json |
4 | | -from xmltodict import parse |
5 | 3 | from time import sleep |
6 | 4 | from csv import reader |
7 | 5 | from base64 import b64encode |
@@ -381,28 +379,30 @@ def extract_password(self, context): |
381 | 379 | xml_doc_path = os.path.abspath(self.local_export_path + "/" + self.export_name) |
382 | 380 | xml_tree = ElementTree.parse(xml_doc_path) |
383 | 381 | root = xml_tree.getroot() |
384 | | - to_string = ElementTree.tostring(root, encoding="UTF-8", method="xml") |
385 | | - xml_to_dict = parse(to_string) |
386 | | - dump = json.dumps(xml_to_dict) |
387 | | - obj = json.loads(dump) |
388 | | - |
389 | | - if len(obj["KeePassFile"]["Root"]["Group"]["Entry"]): |
390 | | - for obj2 in obj["KeePassFile"]["Root"]["Group"]["Entry"]: |
391 | | - for password in obj2["String"]: |
392 | | - if password["Key"] == "Password": |
393 | | - context.log.highlight(str(password["Key"]) + " : " + str(password["Value"]["#text"])) |
394 | | - else: |
395 | | - context.log.highlight(str(password["Key"]) + " : " + str(password["Value"])) |
396 | | - context.log.highlight("") |
397 | | - if len(obj["KeePassFile"]["Root"]["Group"]["Group"]): |
398 | | - for obj2 in obj["KeePassFile"]["Root"]["Group"]["Group"]: |
399 | | - try: |
400 | | - for obj3 in obj2["Entry"]: |
401 | | - for password in obj3["String"]: |
402 | | - if password["Key"] == "Password": |
403 | | - context.log.highlight(str(password["Key"]) + " : " + str(password["Value"]["#text"])) |
404 | | - else: |
405 | | - context.log.highlight(str(password["Key"]) + " : " + str(password["Value"])) |
406 | | - context.log.highlight("") |
407 | | - except KeyError: |
408 | | - pass |
| 382 | + |
| 383 | + root_entries = root.find("./Root/Entry") |
| 384 | + if root_entries is not None: |
| 385 | + for entry in root_entries: |
| 386 | + if entry is not None: |
| 387 | + self.print_password(context, entry) |
| 388 | + else: |
| 389 | + context.log.highlight("None") |
| 390 | + |
| 391 | + objects = root.findall("./Root/Group") |
| 392 | + while objects: |
| 393 | + current_object = objects.pop(0) |
| 394 | + for entry in current_object.findall("./Entry"): |
| 395 | + self.print_password(context, entry) |
| 396 | + for history in entry.findall("./History"): |
| 397 | + for history_entry in history.findall("./Entry"): |
| 398 | + self.print_password(context, history_entry) |
| 399 | + objects.extend(current_object.findall("./Group")) |
| 400 | + |
| 401 | + def print_password(self, context, entries): |
| 402 | + for entry in entries.findall("./String"): |
| 403 | + key = entry.find("./Key") |
| 404 | + value = entry.find("./Value") |
| 405 | + key_text = key.text if key is not None else "None" |
| 406 | + value_text = value.text if value is not None and value.text is not None else "None" |
| 407 | + context.log.highlight(f"{key_text} : {value_text}") |
| 408 | + context.log.highlight("-------------------------------------") |
0 commit comments