Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DataObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _setup(self):
print(self.file_path)
p = Path(self.file_path)
self.name = p.name
if p.suffix == "":
if not p.suffix:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function File._setup refactored with the following changes:

raise FileNotFoundError(
f"{self.file_path} has to be a file not a folder"
)
Expand Down Expand Up @@ -67,11 +67,11 @@ def __init__(self):
def add_target(self, target):
# A target is a tuple in the form ["type", "include_subfolders", "target_path"]
target_type = target[0]
include_subfolders = target[1]
target_path = target[2]
if target_type == "File":
self._add_target_file(target_path=target_path)
else:
include_subfolders = target[1]
Comment on lines -70 to +74
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AttackTargets.add_target refactored with the following changes:

self._add_target_folder(target_path=target_path,
include_subfolders=include_subfolders)

Expand Down
126 changes: 57 additions & 69 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,15 @@ def __init__(self, view, password_file, save_destination, hash_type, targets):

def run(self): # .start initiates this
self.view.clear_text_output() # clear the previous output if any
self.view.insert_text_message(
f"### INITIALISING ###",
update_idle=True
)
self.view.insert_text_message("### INITIALISING ###", update_idle=True)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DictionaryAttack.run refactored with the following changes:

targets = self.attack_targets.get_targets()
for target in targets:
if isinstance(target, File):
self._attack_file(target)
else:
self._attack_folder(target)
self.view.set_current_target("---------")
self.view.insert_text_message(
f"### PROGRAM END ###"
)
self.view.insert_text_message("### PROGRAM END ###")
return None

def _attack_folder(self, target_folder):
Expand All @@ -72,10 +67,8 @@ def _attack_file(self, target_file):
)
self.view.set_current_target(target_file)

is_supported_type = self._is_supported_file(target_file)
if is_supported_type:
pw_required = self._attempt_open_file(target_file)
if pw_required:
if is_supported_type := self._is_supported_file(target_file):
if pw_required := self._attempt_open_file(target_file):
Comment on lines -75 to +71
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DictionaryAttack._attack_file refactored with the following changes:

self.view.insert_text_message(
f"{target_file} is password protected."
)
Expand Down Expand Up @@ -121,65 +114,61 @@ def _crack_file(self, target_file):
found = False
last_attempt = 0
found_password = ""
# dont use readlines as it will store the whole file in memory..
# just read line by line
pws = open(self.password_file, "r", encoding="latin1")
for attempt, password in enumerate(pws, 1):
password = password.strip()
if target_file.file_type == ".rar":
try:
# issue rarfile doesnt handle latin1 characters
# so use a different password file or amend the rockyouone
rar_obj = rarfile.RarFile(target_file.file_path, pwd=password)
found = True
found_password = password
last_attempt = attempt
rar_obj.extractall(self.save_destination)
break
except RuntimeError as err:
if str(err).startswith("Bad password"):
update_idle = (attempt % 5 == 0)
self.view.insert_text_message(
f"Attempt {attempt}: '{password}' failed.",
update_idle=update_idle
)
if attempt % 250 == 0:
self.view.clear_text_output()
continue
else:
self.view.display_messagebox(err, "showerror")
with open(self.password_file, "r", encoding="latin1") as pws:
for attempt, password in enumerate(pws, 1):
password = password.strip()
if target_file.file_type == ".rar":
try:
# issue rarfile doesnt handle latin1 characters
# so use a different password file or amend the rockyouone
rar_obj = rarfile.RarFile(target_file.file_path, pwd=password)
found = True
found_password = password
last_attempt = attempt
rar_obj.extractall(self.save_destination)
break

elif target_file.file_type == ".zip":
try:
password_byte = bytes(password, "latin1")
ZipFile(target_file.file_path).extractall(path=self.save_destination,
pwd=password_byte)
found = True
found_password = password_byte.decode("latin1")
last_attempt = attempt
break
except RuntimeError as err:
if str(err).startswith("Bad password"):
update_idle = (attempt % 5 == 0)
self.view.insert_text_message(
f"Attempt {attempt}: '{password}' failed.",
update_idle=update_idle
)
if attempt % 250 == 0:
self.view.clear_text_output()
else:
self.view.display_messagebox(err, "showerror")
except RuntimeError as err:
if str(err).startswith("Bad password"):
update_idle = (attempt % 5 == 0)
self.view.insert_text_message(
f"Attempt {attempt}: '{password}' failed.",
update_idle=update_idle
)
if attempt % 250 == 0:
self.view.clear_text_output()
continue
else:
self.view.display_messagebox(err, "showerror")
break

elif target_file.file_type == ".zip":
try:
password_byte = bytes(password, "latin1")
ZipFile(target_file.file_path).extractall(path=self.save_destination,
pwd=password_byte)
found = True
found_password = password_byte.decode("latin1")
last_attempt = attempt
break
except zlib.error as zerr:
if str(zerr).startswith("Error -3"):
# If there are compression errors then skip
continue
else:
except RuntimeError as err:
if str(err).startswith("Bad password"):
update_idle = (attempt % 5 == 0)
self.view.insert_text_message(
f"Attempt {attempt}: '{password}' failed.",
update_idle=update_idle
)
if attempt % 250 == 0:
self.view.clear_text_output()
else:
self.view.display_messagebox(err, "showerror")
break
except zlib.error as zerr:
if str(zerr).startswith("Error -3"):
# If there are compression errors then skip
continue
self.view.display_messagebox(zerr, "showerror")
break

pws.close()
Comment on lines -124 to -182
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DictionaryAttack._crack_file refactored with the following changes:

This removes the following comments ( why? ):

# just read line by line
# dont use readlines as it will store the whole file in memory..

if found:
self.view.insert_text_message(
f"Attempt {last_attempt}: '{found_password}'"
Expand All @@ -199,7 +188,7 @@ def _crack_file(self, target_file):
self.view.insert_text_message(
f"password will be saved to {self.save_destination}"
)
self.view.insert_text_message(f"________________")
self.view.insert_text_message("________________")
self._store_password(found_password, target_file.file_path)

else:
Expand All @@ -211,9 +200,8 @@ def _crack_file(self, target_file):
def _store_password(self, password, target_path):
line = f"{password} | {target_path}\n"
dest = fr"{self.save_destination}\extracted_passwords.txt"
f = open(dest, "a+")
f.write(line)
f.close()
with open(dest, "a+") as f:
f.write(line)
Comment on lines -214 to +204
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DictionaryAttack._store_password refactored with the following changes:

self.view.insert_text_message(
f"Found password stored to {dest}"
)
17 changes: 7 additions & 10 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,11 @@ def __init__(self, parent):
def _windows_dialog(self, is_folder=False, only_text=False):
selection = None
if is_folder:
selection = filedialog.askdirectory()
else:
type1 = ("All Files", "*.*") if not only_text else ("Text Files", "*.txt")
selection = filedialog.askopenfilename(initialdir="/",
title="Select a File",
filetype=[type1])
return selection
return filedialog.askdirectory()
type1 = ("All Files", "*.*") if not only_text else ("Text Files", "*.txt")
return filedialog.askopenfilename(
initialdir="/", title="Select a File", filetype=[type1]
)
Comment on lines -112 to +116
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MainPage._windows_dialog refactored with the following changes:


def _set_dictionary_list(self):
selection = self._windows_dialog(only_text=True)
Expand All @@ -141,8 +139,7 @@ def _add_target_folder_tv(self):
self.tv_target.insert("", "end", values=row)

def _delete_tv_row(self, event):
row = self.tv_target.selection()
if row:
if row := self.tv_target.selection():
Comment on lines -144 to +142
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MainPage._delete_tv_row refactored with the following changes:

self.tv_target.delete(row[0])

def insert_text_message(self, message, update_idle=False):
Expand All @@ -164,7 +161,6 @@ def set_current_target(self, current_target):
self.label_current_target["text"] = current_target

def _launch_dictionary_attack(self):
password_hash_type = "Plain Text" # self.hash.get()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MainPage._launch_dictionary_attack refactored with the following changes:

password_file = self.label_dict_file["text"]
save_dest = self.label_extract_dest["text"]

Expand All @@ -181,6 +177,7 @@ def _launch_dictionary_attack(self):
if not all_targets:
self.display_messagebox("There are no targets", type="showinfo")
else:
password_hash_type = "Plain Text" # self.hash.get()
self.controller.launch_attack(password_file=password_file,
save_destination=save_dest,
hash_type=password_hash_type,
Expand Down