From d9330ef698c34bae3a3587433363ac17aa9f132b Mon Sep 17 00:00:00 2001 From: deix Date: Mon, 28 Aug 2017 17:20:38 +0200 Subject: [PATCH] Use the "with" keyword when dealing with file objects It is good practice to use the "with" keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks --- Wifite.py | 5 ++--- py/Aircrack.py | 6 ++---- py/Aireplay.py | 13 +++++-------- py/AttackWPA.py | 5 ++--- py/CrackResult.py | 10 ++++------ py/Reaver.py | 11 ++++------- 6 files changed, 19 insertions(+), 31 deletions(-) diff --git a/Wifite.py b/Wifite.py index c9cf2e9..49f6a83 100755 --- a/Wifite.py +++ b/Wifite.py @@ -45,9 +45,8 @@ class Wifite(object): if not os.path.exists(name): Color.pl('{!} {O}file {C}%s{O} not found{W}' % name) return - f = open(name, 'r') - json = loads(f.read()) - f.close() + with open(name, 'r') as fid: + json = loads(fid.read()) for (index, item) in enumerate(json): Color.pl('\n{+} Cracked target #%d:' % (index + 1)) cr = CrackResult.load(item) diff --git a/py/Aircrack.py b/py/Aircrack.py index aedcca9..f4b78c8 100644 --- a/py/Aircrack.py +++ b/py/Aircrack.py @@ -39,10 +39,8 @@ class Aircrack(object): def get_key_hex_ascii(self): if not self.is_cracked(): raise Exception('Cracked file not found') - f = open(self.cracked_file, 'r') - hex_raw = f.read() - f.close() - + with open(self.cracked_file, 'r') as fid: + hex_raw = fid.read() hex_key = '' ascii_key = '' while len(hex_raw) > 0: diff --git a/py/Aireplay.py b/py/Aireplay.py index 5249254..7ad8d2f 100644 --- a/py/Aireplay.py +++ b/py/Aireplay.py @@ -96,14 +96,11 @@ class Aireplay(Thread): while self.pid.poll() is None: time.sleep(0.1) if not os.path.exists(self.output_file): continue - # Read output file - f = open(self.output_file, "r") - lines = f.read() - f.close() - # Clear output file - f = open(self.output_file, "w") - f.write("") - f.close() + # Read output file & clear output file + with open(self.output_file, "r+") as fid: + lines = fid.read() + fid.seek(0) + fid.truncate() for line in lines.split("\n"): line = line.replace("\r", "").strip() if line == "": continue diff --git a/py/AttackWPA.py b/py/AttackWPA.py index fbbae6f..114b64d 100644 --- a/py/AttackWPA.py +++ b/py/AttackWPA.py @@ -189,9 +189,8 @@ class AttackWPA(Attack): Color.pl("") # Check crack result if os.path.exists(key_file): - f = open(key_file, "r") - key = f.read().strip() - f.close() + with open(key_file, "r") as fid: + key = fid.read().strip() os.remove(key_file) Color.pl("{+} {G}Cracked WPA Handshake{W} PSK: {G}%s{W}\n" % key) diff --git a/py/CrackResult.py b/py/CrackResult.py index 5a80d6d..0e8768c 100644 --- a/py/CrackResult.py +++ b/py/CrackResult.py @@ -27,17 +27,15 @@ class CrackResult(object): name = CrackResult.cracked_file json = [] if os.path.exists(name): - f = open(name, 'r') - text = f.read() - f.close() + with open(name, 'r') as fid: + text = fid.read() try: json = loads(text) except Exception, e: Color.pl('{!} error while loading %s: %s' % (name, str(e))) json.append(self.to_dict()) - f = open(name, 'w') - f.write(dumps(json, indent=2)) - f.close() + with open(name, 'w') as fid: + fid.write(dumps(json, indent=2)) Color.pl('{+} saved crack result to {C}%s{W} ({G}%d total{W})' % (name, len(json))) diff --git a/py/Reaver.py b/py/Reaver.py index 0b40c76..8cbb982 100644 --- a/py/Reaver.py +++ b/py/Reaver.py @@ -240,10 +240,8 @@ class Reaver(Attack): out = self.get_stdout() # Clear output file - f = open(self.stdout_file, 'w') - f.write('') - f.close() - + with open(self.stdout_file, 'w'): + pass # CHECK FOR CRACK (pin, psk, ssid) = Reaver.get_pin_psk_ssid(out) @@ -394,9 +392,8 @@ class Reaver(Attack): ''' Gets output from stdout_file ''' if not self.stdout_file: return '' - f = open(self.stdout_file, 'r') - stdout = f.read() - f.close() + with open(self.stdout_file, 'r') as fid: + stdout = fid.read() return stdout.strip()