Skip to content

Commit cb0c337

Browse files
exploidesolardiz
authored andcommitted
kwallet2john.py: compatibility with python3 and python2
prior to this commit, the script only works with python2 fixes #5811
1 parent 7bb06e5 commit cb0c337

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

run/kwallet2john.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import struct
1616
from binascii import hexlify
1717

18-
KWMAGIC = "KWALLET\n\r\0\r\n"
18+
KWMAGIC = b"KWALLET\n\r\0\r\n"
1919
KWMAGIC_LEN = 12
2020
KWALLET_VERSION_MAJOR = 0
2121
KWALLET_VERSION_MINOR = 0
@@ -107,19 +107,21 @@ def process_file(filename):
107107
# read salt
108108
salt_filename = os.path.splitext(filename)[0] + ".salt"
109109
try:
110-
salt = open(salt_filename).read()
111-
except:
112-
sys.stderr.write("%s : unable to read salt from %s\n" % (filename, salt_filename))
110+
with open(salt_filename, "rb") as f:
111+
salt = f.read()
112+
except FileNotFoundError:
113+
sys.stderr.write("%s : cannot find file %s which is required to process a salted wallet\n" % (filename, salt_filename))
113114
sys.exit(8)
115+
114116
salt_len = len(salt)
115117
iterations = PBKDF2_SHA512_ITERATIONS # is this fixed?
116118
sys.stdout.write("%s:$kwallet$%ld$%s$%d$%d$%s$%s" %
117119
(os.path.basename(filename), encrypted_size,
118-
hexlify(encrypted), kwallet_minor_version, salt_len,
119-
salt.encode("hex"), iterations))
120+
hexlify(encrypted).decode('ascii'), kwallet_minor_version, salt_len,
121+
hexlify(salt).decode('ascii'), iterations))
120122
sys.stdout.write(":::::%s\n" % filename)
121123
else:
122-
sys.stdout.write("%s:$kwallet$%ld$%s" % (os.path.basename(filename), encrypted_size, hexlify(encrypted)))
124+
sys.stdout.write("%s:$kwallet$%ld$%s" % (os.path.basename(filename), encrypted_size, hexlify(encrypted).decode('ascii')))
123125
sys.stdout.write(":::::%s\n" % filename)
124126

125127
fd.close()
@@ -130,5 +132,5 @@ def process_file(filename):
130132
sys.stderr.write("Usage: %s <.kwl file(s)>\n" % sys.argv[0])
131133
sys.exit(1)
132134

133-
for i in range(1, len(sys.argv)):
134-
process_file(sys.argv[i])
135+
for file in sys.argv[1:]:
136+
process_file(file)

0 commit comments

Comments
 (0)