2020-09-27 04:54:49 -06:00
|
|
|
#!/usr/bin/env python3
|
2015-03-09 01:38:31 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-12-27 02:45:12 -07:00
|
|
|
# ignoblekeyNookStudy.py
|
2020-09-26 14:22:47 -06:00
|
|
|
# Copyright © 2015-2020 Apprentice Alf, Apprentice Harper et al.
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
# Based on kindlekey.py, Copyright © 2010-2013 by some_updates and Apprentice Alf
|
|
|
|
|
|
|
|
# Released under the terms of the GNU General Public Licence, version 3
|
|
|
|
# <http://www.gnu.org/licenses/>
|
|
|
|
|
|
|
|
# Revision history:
|
|
|
|
# 1.0 - Initial release
|
2015-06-30 10:27:33 -06:00
|
|
|
# 1.1 - remove duplicates and return last key as single key
|
2020-09-27 04:54:49 -06:00
|
|
|
# 2.0 - Python 3 for calibre 5.0
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
"""
|
|
|
|
Get Barnes & Noble EPUB user key from nook Studio log file
|
|
|
|
"""
|
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
2020-10-14 09:23:49 -06:00
|
|
|
__version__ = "2.0"
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import hashlib
|
|
|
|
import getopt
|
|
|
|
import re
|
|
|
|
|
2022-08-06 12:09:30 -06:00
|
|
|
from utilities import SafeUnbuffered
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
try:
|
2022-08-06 12:09:30 -06:00
|
|
|
from calibre.constants import iswindows
|
2015-03-09 01:38:31 -06:00
|
|
|
except:
|
|
|
|
iswindows = sys.platform.startswith('win')
|
|
|
|
|
2022-08-06 12:19:18 -06:00
|
|
|
from argv_utils import unicode_argv
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
class DrmException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Locate all of the nookStudy/nook for PC/Mac log file and return as list
|
|
|
|
def getNookLogFiles():
|
|
|
|
logFiles = []
|
|
|
|
found = False
|
|
|
|
if iswindows:
|
2021-02-24 18:54:39 -07:00
|
|
|
try:
|
|
|
|
import winreg
|
|
|
|
except ImportError:
|
|
|
|
import _winreg as winreg
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
# some 64 bit machines do not have the proper registry key for some reason
|
|
|
|
# or the python interface to the 32 vs 64 bit registry is broken
|
|
|
|
paths = set()
|
|
|
|
if 'LOCALAPPDATA' in os.environ.keys():
|
|
|
|
# Python 2.x does not return unicode env. Use Python 3.x
|
2020-09-27 04:54:49 -06:00
|
|
|
path = winreg.ExpandEnvironmentStrings("%LOCALAPPDATA%")
|
2015-03-09 01:38:31 -06:00
|
|
|
if os.path.isdir(path):
|
|
|
|
paths.add(path)
|
|
|
|
if 'USERPROFILE' in os.environ.keys():
|
|
|
|
# Python 2.x does not return unicode env. Use Python 3.x
|
2020-09-27 04:54:49 -06:00
|
|
|
path = winreg.ExpandEnvironmentStrings("%USERPROFILE%")+"\\AppData\\Local"
|
2015-03-09 01:38:31 -06:00
|
|
|
if os.path.isdir(path):
|
|
|
|
paths.add(path)
|
2020-09-27 04:54:49 -06:00
|
|
|
path = winreg.ExpandEnvironmentStrings("%USERPROFILE%")+"\\AppData\\Roaming"
|
2015-03-09 01:38:31 -06:00
|
|
|
if os.path.isdir(path):
|
|
|
|
paths.add(path)
|
|
|
|
# User Shell Folders show take precedent over Shell Folders if present
|
|
|
|
try:
|
|
|
|
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\")
|
|
|
|
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
|
|
|
if os.path.isdir(path):
|
|
|
|
paths.add(path)
|
|
|
|
except WindowsError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\")
|
|
|
|
path = winreg.QueryValueEx(regkey, 'AppData')[0]
|
|
|
|
if os.path.isdir(path):
|
|
|
|
paths.add(path)
|
|
|
|
except WindowsError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
|
|
|
path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
|
|
|
|
if os.path.isdir(path):
|
|
|
|
paths.add(path)
|
|
|
|
except WindowsError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
|
|
|
|
path = winreg.QueryValueEx(regkey, 'AppData')[0]
|
|
|
|
if os.path.isdir(path):
|
|
|
|
paths.add(path)
|
|
|
|
except WindowsError:
|
|
|
|
pass
|
2015-06-30 10:27:33 -06:00
|
|
|
|
2015-03-09 01:38:31 -06:00
|
|
|
for path in paths:
|
|
|
|
# look for nookStudy log file
|
|
|
|
logpath = path +'\\Barnes & Noble\\NOOKstudy\\logs\\BNClientLog.txt'
|
|
|
|
if os.path.isfile(logpath):
|
|
|
|
found = True
|
2022-03-22 08:49:44 -06:00
|
|
|
print('Found nookStudy log file: ' + logpath, file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
logFiles.append(logpath)
|
|
|
|
else:
|
|
|
|
home = os.getenv('HOME')
|
|
|
|
# check for BNClientLog.txt in various locations
|
|
|
|
testpath = home + '/Library/Application Support/Barnes & Noble/DesktopReader/logs/BNClientLog.txt'
|
|
|
|
if os.path.isfile(testpath):
|
|
|
|
logFiles.append(testpath)
|
2021-12-29 05:00:45 -07:00
|
|
|
print('Found nookStudy log file: ' + testpath, file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
found = True
|
|
|
|
testpath = home + '/Library/Application Support/Barnes & Noble/DesktopReader/indices/BNClientLog.txt'
|
|
|
|
if os.path.isfile(testpath):
|
|
|
|
logFiles.append(testpath)
|
2021-12-29 05:00:45 -07:00
|
|
|
print('Found nookStudy log file: ' + testpath, file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
found = True
|
|
|
|
testpath = home + '/Library/Application Support/Barnes & Noble/BNDesktopReader/logs/BNClientLog.txt'
|
|
|
|
if os.path.isfile(testpath):
|
|
|
|
logFiles.append(testpath)
|
2021-12-29 05:00:45 -07:00
|
|
|
print('Found nookStudy log file: ' + testpath, file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
found = True
|
|
|
|
testpath = home + '/Library/Application Support/Barnes & Noble/BNDesktopReader/indices/BNClientLog.txt'
|
|
|
|
if os.path.isfile(testpath):
|
|
|
|
logFiles.append(testpath)
|
2021-12-29 05:00:45 -07:00
|
|
|
print('Found nookStudy log file: ' + testpath, file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
found = True
|
|
|
|
|
|
|
|
if not found:
|
2021-12-29 05:00:45 -07:00
|
|
|
print('No nook Study log files have been found.', file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
return logFiles
|
|
|
|
|
|
|
|
|
|
|
|
# Extract CCHash key(s) from log file
|
|
|
|
def getKeysFromLog(kLogFile):
|
|
|
|
keys = []
|
|
|
|
regex = re.compile("ccHash: \"(.{28})\"");
|
|
|
|
for line in open(kLogFile):
|
|
|
|
for m in regex.findall(line):
|
|
|
|
keys.append(m)
|
|
|
|
return keys
|
|
|
|
|
|
|
|
# interface for calibre plugin
|
|
|
|
def nookkeys(files = []):
|
|
|
|
keys = []
|
|
|
|
if files == []:
|
|
|
|
files = getNookLogFiles()
|
|
|
|
for file in files:
|
|
|
|
fileKeys = getKeysFromLog(file)
|
|
|
|
if fileKeys:
|
2021-12-29 05:00:45 -07:00
|
|
|
print("Found {0} keys in the Nook Study log files".format(len(fileKeys)), file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
keys.extend(fileKeys)
|
2015-06-30 10:27:33 -06:00
|
|
|
return list(set(keys))
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
# interface for Python DeDRM
|
|
|
|
# returns single key or multiple keys, depending on path or file passed in
|
|
|
|
def getkey(outpath, files=[]):
|
|
|
|
keys = nookkeys(files)
|
|
|
|
if len(keys) > 0:
|
|
|
|
if not os.path.isdir(outpath):
|
|
|
|
outfile = outpath
|
2020-09-26 14:22:47 -06:00
|
|
|
with open(outfile, 'w') as keyfileout:
|
2015-06-30 10:27:33 -06:00
|
|
|
keyfileout.write(keys[-1])
|
2021-12-29 05:00:45 -07:00
|
|
|
print("Saved a key to {0}".format(outfile), file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
else:
|
|
|
|
keycount = 0
|
|
|
|
for key in keys:
|
|
|
|
while True:
|
|
|
|
keycount += 1
|
2020-09-27 04:54:49 -06:00
|
|
|
outfile = os.path.join(outpath,"nookkey{0:d}.b64".format(keycount))
|
2015-03-09 01:38:31 -06:00
|
|
|
if not os.path.exists(outfile):
|
|
|
|
break
|
2020-09-26 14:22:47 -06:00
|
|
|
with open(outfile, 'w') as keyfileout:
|
2015-03-09 01:38:31 -06:00
|
|
|
keyfileout.write(key)
|
2021-12-29 05:00:45 -07:00
|
|
|
print("Saved a key to {0}".format(outfile), file=sys.stderr)
|
2015-03-09 01:38:31 -06:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def usage(progname):
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Finds the nook Study encryption keys.")
|
|
|
|
print("Keys are saved to the current directory, or a specified output directory.")
|
|
|
|
print("If a file name is passed instead of a directory, only the first key is saved, in that file.")
|
|
|
|
print("Usage:")
|
|
|
|
print(" {0:s} [-h] [-k <logFile>] [<outpath>]".format(progname))
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
|
|
|
|
def cli_main():
|
|
|
|
sys.stdout=SafeUnbuffered(sys.stdout)
|
|
|
|
sys.stderr=SafeUnbuffered(sys.stderr)
|
2022-08-06 12:19:18 -06:00
|
|
|
argv=unicode_argv("ignoblekeyNookStudy.py")
|
2015-03-09 01:38:31 -06:00
|
|
|
progname = os.path.basename(argv[0])
|
2020-09-27 04:54:49 -06:00
|
|
|
print("{0} v{1}\nCopyright © 2015 Apprentice Alf".format(progname,__version__))
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(argv[1:], "hk:")
|
2020-09-26 14:22:47 -06:00
|
|
|
except getopt.GetoptError as err:
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Error in options or arguments: {0}".format(err.args[0]))
|
2015-03-09 01:38:31 -06:00
|
|
|
usage(progname)
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
files = []
|
|
|
|
for o, a in opts:
|
|
|
|
if o == "-h":
|
|
|
|
usage(progname)
|
|
|
|
sys.exit(0)
|
|
|
|
if o == "-k":
|
|
|
|
files = [a]
|
|
|
|
|
|
|
|
if len(args) > 1:
|
|
|
|
usage(progname)
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if len(args) == 1:
|
|
|
|
# save to the specified file or directory
|
|
|
|
outpath = args[0]
|
|
|
|
if not os.path.isabs(outpath):
|
|
|
|
outpath = os.path.abspath(outpath)
|
|
|
|
else:
|
|
|
|
# save to the same directory as the script
|
|
|
|
outpath = os.path.dirname(argv[0])
|
|
|
|
|
|
|
|
# make sure the outpath is the
|
|
|
|
outpath = os.path.realpath(os.path.normpath(outpath))
|
|
|
|
|
|
|
|
if not getkey(outpath, files):
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Could not retrieve nook Study key.")
|
2015-03-09 01:38:31 -06:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def gui_main():
|
|
|
|
try:
|
2020-10-14 09:23:49 -06:00
|
|
|
import tkinter
|
|
|
|
import tkinter.constants
|
|
|
|
import tkinter.messagebox
|
2015-03-09 01:38:31 -06:00
|
|
|
import traceback
|
|
|
|
except:
|
|
|
|
return cli_main()
|
|
|
|
|
2020-10-14 09:23:49 -06:00
|
|
|
class ExceptionDialog(tkinter.Frame):
|
2015-03-09 01:38:31 -06:00
|
|
|
def __init__(self, root, text):
|
2020-10-14 09:23:49 -06:00
|
|
|
tkinter.Frame.__init__(self, root, border=5)
|
|
|
|
label = tkinter.Label(self, text="Unexpected error:",
|
|
|
|
anchor=tkinter.constants.W, justify=tkinter.constants.LEFT)
|
|
|
|
label.pack(fill=tkinter.constants.X, expand=0)
|
|
|
|
self.text = tkinter.Text(self)
|
|
|
|
self.text.pack(fill=tkinter.constants.BOTH, expand=1)
|
2015-03-09 01:38:31 -06:00
|
|
|
|
2020-10-14 09:23:49 -06:00
|
|
|
self.text.insert(tkinter.constants.END, text)
|
2015-03-09 01:38:31 -06:00
|
|
|
|
|
|
|
|
2022-08-06 12:19:18 -06:00
|
|
|
argv=unicode_argv("ignoblekeyNookStudy.py")
|
2020-10-14 09:23:49 -06:00
|
|
|
root = tkinter.Tk()
|
2015-03-09 01:38:31 -06:00
|
|
|
root.withdraw()
|
|
|
|
progpath, progname = os.path.split(argv[0])
|
|
|
|
success = False
|
|
|
|
try:
|
|
|
|
keys = nookkeys()
|
|
|
|
keycount = 0
|
|
|
|
for key in keys:
|
2019-06-24 10:49:38 -06:00
|
|
|
print(key)
|
2015-03-09 01:38:31 -06:00
|
|
|
while True:
|
|
|
|
keycount += 1
|
2020-09-27 04:54:49 -06:00
|
|
|
outfile = os.path.join(progpath,"nookkey{0:d}.b64".format(keycount))
|
2015-03-09 01:38:31 -06:00
|
|
|
if not os.path.exists(outfile):
|
|
|
|
break
|
|
|
|
|
2020-09-26 14:22:47 -06:00
|
|
|
with open(outfile, 'w') as keyfileout:
|
2015-03-09 01:38:31 -06:00
|
|
|
keyfileout.write(key)
|
|
|
|
success = True
|
2020-10-14 09:23:49 -06:00
|
|
|
tkinter.messagebox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile))
|
2020-09-26 14:22:47 -06:00
|
|
|
except DrmException as e:
|
2020-10-14 09:23:49 -06:00
|
|
|
tkinter.messagebox.showerror(progname, "Error: {0}".format(str(e)))
|
2015-03-09 01:38:31 -06:00
|
|
|
except Exception:
|
|
|
|
root.wm_state('normal')
|
|
|
|
root.title(progname)
|
|
|
|
text = traceback.format_exc()
|
2020-10-14 09:23:49 -06:00
|
|
|
ExceptionDialog(root, text).pack(fill=tkinter.constants.BOTH, expand=1)
|
2015-03-09 01:38:31 -06:00
|
|
|
root.mainloop()
|
|
|
|
if not success:
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
sys.exit(cli_main())
|
|
|
|
sys.exit(gui_main())
|