-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
raise FileNotFoundError( | ||
f"{self.file_path} has to be a file not a folder" | ||
) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self._add_target_folder(target_path=target_path, | ||
include_subfolders=include_subfolders) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.view.insert_text_message( | ||
f"{target_file} is password protected." | ||
) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
if found: | ||
self.view.insert_text_message( | ||
f"Attempt {last_attempt}: '{found_password}'" | ||
|
@@ -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: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.view.insert_text_message( | ||
f"Found password stored to {dest}" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def _set_dictionary_list(self): | ||
selection = self._windows_dialog(only_text=True) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.tv_target.delete(row[0]) | ||
|
||
def insert_text_message(self, message, update_idle=False): | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
password_file = self.label_dict_file["text"] | ||
save_dest = self.label_extract_dest["text"] | ||
|
||
|
@@ -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, | ||
|
There was a problem hiding this comment.
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:simplify-empty-collection-comparison
)