2020-09-27 04:54:49 -06:00
|
|
|
#!/usr/bin/env python3
|
2015-06-30 10:27:33 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-09-27 04:54:49 -06:00
|
|
|
# ignoblekeyfetch.py
|
2020-09-26 14:22:47 -06:00
|
|
|
# Copyright © 2015-2020 Apprentice Harper et al.
|
2015-06-30 10:27:33 -06:00
|
|
|
|
|
|
|
# Released under the terms of the GNU General Public Licence, version 3
|
|
|
|
# <http://www.gnu.org/licenses/>
|
|
|
|
|
|
|
|
# Based on discoveries by "Nobody You Know"
|
|
|
|
# Code partly based on ignoblekeygen.py by several people.
|
|
|
|
|
|
|
|
# Windows users: Before running this program, you must first install Python.
|
|
|
|
# We recommend ActiveState Python 2.7.X for Windows from
|
|
|
|
# http://www.activestate.com/activepython/downloads.
|
|
|
|
# Then save this script file as ignoblekeyfetch.pyw and double-click on it to run it.
|
|
|
|
#
|
|
|
|
# Mac OS X users: Save this script file as ignoblekeyfetch.pyw. You can run this
|
|
|
|
# program from the command line (python ignoblekeyfetch.pyw) or by double-clicking
|
|
|
|
# it when it has been associated with PythonLauncher.
|
|
|
|
|
|
|
|
# Revision history:
|
|
|
|
# 1.0 - Initial version
|
|
|
|
# 1.1 - Try second URL if first one fails
|
2020-09-27 04:54:49 -06:00
|
|
|
# 2.0 - Python 3 for calibre 5.0
|
2015-06-30 10:27:33 -06:00
|
|
|
|
|
|
|
"""
|
|
|
|
Fetch Barnes & Noble EPUB user key from B&N servers using email and password
|
|
|
|
"""
|
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
2020-09-27 04:54:49 -06:00
|
|
|
__version__ = "2.0"
|
2015-06-30 10:27:33 -06:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Wrap a stream so that output gets flushed immediately
|
|
|
|
# and also make sure that any unicode strings get
|
|
|
|
# encoded using "replace" before writing them.
|
|
|
|
class SafeUnbuffered:
|
|
|
|
def __init__(self, stream):
|
|
|
|
self.stream = stream
|
|
|
|
self.encoding = stream.encoding
|
|
|
|
if self.encoding == None:
|
|
|
|
self.encoding = "utf-8"
|
|
|
|
def write(self, data):
|
2021-11-16 03:09:03 -07:00
|
|
|
if isinstance(data,str) or isinstance(data,unicode):
|
|
|
|
# str for Python3, unicode for Python2
|
2015-06-30 10:27:33 -06:00
|
|
|
data = data.encode(self.encoding,"replace")
|
2021-11-16 03:09:03 -07:00
|
|
|
try:
|
|
|
|
buffer = getattr(self.stream, 'buffer', self.stream)
|
|
|
|
# self.stream.buffer for Python3, self.stream for Python2
|
|
|
|
buffer.write(data)
|
|
|
|
buffer.flush()
|
|
|
|
except:
|
|
|
|
# We can do nothing if a write fails
|
|
|
|
raise
|
2015-06-30 10:27:33 -06:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.stream, attr)
|
|
|
|
|
|
|
|
try:
|
|
|
|
from calibre.constants import iswindows, isosx
|
|
|
|
except:
|
|
|
|
iswindows = sys.platform.startswith('win')
|
|
|
|
isosx = sys.platform.startswith('darwin')
|
|
|
|
|
|
|
|
def unicode_argv():
|
|
|
|
if iswindows:
|
|
|
|
# Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
|
|
|
|
# strings.
|
|
|
|
|
|
|
|
# Versions 2.x of Python don't support Unicode in sys.argv on
|
|
|
|
# Windows, with the underlying Windows API instead replacing multi-byte
|
|
|
|
# characters with '?'. So use shell32.GetCommandLineArgvW to get sys.argv
|
|
|
|
# as a list of Unicode strings and encode them as utf-8
|
|
|
|
|
|
|
|
from ctypes import POINTER, byref, cdll, c_int, windll
|
|
|
|
from ctypes.wintypes import LPCWSTR, LPWSTR
|
|
|
|
|
|
|
|
GetCommandLineW = cdll.kernel32.GetCommandLineW
|
|
|
|
GetCommandLineW.argtypes = []
|
|
|
|
GetCommandLineW.restype = LPCWSTR
|
|
|
|
|
|
|
|
CommandLineToArgvW = windll.shell32.CommandLineToArgvW
|
|
|
|
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
|
|
|
|
CommandLineToArgvW.restype = POINTER(LPWSTR)
|
|
|
|
|
|
|
|
cmd = GetCommandLineW()
|
|
|
|
argc = c_int(0)
|
|
|
|
argv = CommandLineToArgvW(cmd, byref(argc))
|
|
|
|
if argc.value > 0:
|
|
|
|
# Remove Python executable and commands if present
|
|
|
|
start = argc.value - len(sys.argv)
|
|
|
|
return [argv[i] for i in
|
2020-09-26 14:22:47 -06:00
|
|
|
range(start, argc.value)]
|
2015-06-30 10:27:33 -06:00
|
|
|
# if we don't have any arguments at all, just pass back script name
|
|
|
|
# this should never happen
|
2020-09-27 04:54:49 -06:00
|
|
|
return ["ignoblekeyfetch.py"]
|
2015-06-30 10:27:33 -06:00
|
|
|
else:
|
2020-10-04 13:36:12 -06:00
|
|
|
argvencoding = sys.stdin.encoding or "utf-8"
|
2021-11-16 03:09:03 -07:00
|
|
|
return [arg if (isinstance(arg, str) or isinstance(arg,unicode)) else str(arg, argvencoding) for arg in sys.argv]
|
2015-06-30 10:27:33 -06:00
|
|
|
|
|
|
|
|
|
|
|
class IGNOBLEError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def fetch_key(email, password):
|
2015-08-02 04:09:35 -06:00
|
|
|
# change email and password to utf-8 if unicode
|
2020-10-14 09:23:49 -06:00
|
|
|
if type(email)==str:
|
2015-06-30 10:27:33 -06:00
|
|
|
email = email.encode('utf-8')
|
2020-10-14 09:23:49 -06:00
|
|
|
if type(password)==str:
|
2015-06-30 10:27:33 -06:00
|
|
|
password = password.encode('utf-8')
|
2015-08-02 04:09:35 -06:00
|
|
|
|
2015-06-30 10:27:33 -06:00
|
|
|
import random
|
|
|
|
random = "%030x" % random.randrange(16**30)
|
2015-08-02 04:09:35 -06:00
|
|
|
|
2020-10-04 13:36:12 -06:00
|
|
|
import urllib.parse, urllib.request, re
|
2015-08-02 04:09:35 -06:00
|
|
|
|
2015-06-30 10:27:33 -06:00
|
|
|
# try the URL from nook for PC
|
|
|
|
fetch_url = "https://cart4.barnesandnoble.com/services/service.aspx?Version=2&acctPassword="
|
2020-10-04 13:36:12 -06:00
|
|
|
fetch_url += urllib.parse.quote(password,'')+"&devID=PC_BN_2.5.6.9575_"+random+"&emailAddress="
|
|
|
|
fetch_url += urllib.parse.quote(email,"")+"&outFormat=5&schema=1&service=1&stage=deviceHashB"
|
2015-06-30 10:27:33 -06:00
|
|
|
#print fetch_url
|
2015-08-02 04:09:35 -06:00
|
|
|
|
2015-06-30 10:27:33 -06:00
|
|
|
found = ''
|
|
|
|
try:
|
2020-10-04 13:36:12 -06:00
|
|
|
response = urllib.request.urlopen(fetch_url)
|
2015-06-30 10:27:33 -06:00
|
|
|
the_page = response.read()
|
|
|
|
#print the_page
|
|
|
|
found = re.search('ccHash>(.+?)</ccHash', the_page).group(1)
|
|
|
|
except:
|
|
|
|
found = ''
|
|
|
|
if len(found)!=28:
|
|
|
|
# try the URL from android devices
|
|
|
|
fetch_url = "https://cart4.barnesandnoble.com/services/service.aspx?Version=2&acctPassword="
|
2020-10-04 13:36:12 -06:00
|
|
|
fetch_url += urllib.parse.quote(password,'')+"&devID=hobbes_9.3.50818_"+random+"&emailAddress="
|
|
|
|
fetch_url += urllib.parse.quote(email,"")+"&outFormat=5&schema=1&service=1&stage=deviceHashB"
|
2015-06-30 10:27:33 -06:00
|
|
|
#print fetch_url
|
2015-08-02 04:09:35 -06:00
|
|
|
|
2015-06-30 10:27:33 -06:00
|
|
|
found = ''
|
|
|
|
try:
|
2020-10-04 13:36:12 -06:00
|
|
|
response = urllib.request.urlopen(fetch_url)
|
2015-06-30 10:27:33 -06:00
|
|
|
the_page = response.read()
|
2015-08-02 04:09:35 -06:00
|
|
|
#print the_page
|
2015-06-30 10:27:33 -06:00
|
|
|
found = re.search('ccHash>(.+?)</ccHash', the_page).group(1)
|
|
|
|
except:
|
|
|
|
found = ''
|
|
|
|
|
|
|
|
return found
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cli_main():
|
|
|
|
sys.stdout=SafeUnbuffered(sys.stdout)
|
|
|
|
sys.stderr=SafeUnbuffered(sys.stderr)
|
|
|
|
argv=unicode_argv()
|
|
|
|
progname = os.path.basename(argv[0])
|
|
|
|
if len(argv) != 4:
|
2020-09-27 04:54:49 -06:00
|
|
|
print("usage: {0} <email> <password> <keyfileout.b64>".format(progname))
|
2015-06-30 10:27:33 -06:00
|
|
|
return 1
|
|
|
|
email, password, keypath = argv[1:]
|
|
|
|
userkey = fetch_key(email, password)
|
|
|
|
if len(userkey) == 28:
|
|
|
|
open(keypath,'wb').write(userkey)
|
|
|
|
return 0
|
2020-09-27 04:54:49 -06:00
|
|
|
print("Failed to fetch key.")
|
2015-06-30 10:27:33 -06:00
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
def gui_main():
|
|
|
|
try:
|
2020-10-14 09:23:49 -06:00
|
|
|
import tkinter
|
|
|
|
import tkinter.filedialog
|
|
|
|
import tkinter.constants
|
|
|
|
import tkinter.messagebox
|
2015-06-30 10:27:33 -06:00
|
|
|
import traceback
|
|
|
|
except:
|
|
|
|
return cli_main()
|
|
|
|
|
2020-10-14 09:23:49 -06:00
|
|
|
class DecryptionDialog(tkinter.Frame):
|
2015-06-30 10:27:33 -06:00
|
|
|
def __init__(self, root):
|
2020-10-14 09:23:49 -06:00
|
|
|
tkinter.Frame.__init__(self, root, border=5)
|
|
|
|
self.status = tkinter.Label(self, text="Enter parameters")
|
|
|
|
self.status.pack(fill=tkinter.constants.X, expand=1)
|
|
|
|
body = tkinter.Frame(self)
|
|
|
|
body.pack(fill=tkinter.constants.X, expand=1)
|
|
|
|
sticky = tkinter.constants.E + tkinter.constants.W
|
2015-06-30 10:27:33 -06:00
|
|
|
body.grid_columnconfigure(1, weight=2)
|
2020-10-14 09:23:49 -06:00
|
|
|
tkinter.Label(body, text="Account email address").grid(row=0)
|
|
|
|
self.name = tkinter.Entry(body, width=40)
|
2015-06-30 10:27:33 -06:00
|
|
|
self.name.grid(row=0, column=1, sticky=sticky)
|
2020-10-14 09:23:49 -06:00
|
|
|
tkinter.Label(body, text="Account password").grid(row=1)
|
|
|
|
self.ccn = tkinter.Entry(body, width=40)
|
2015-06-30 10:27:33 -06:00
|
|
|
self.ccn.grid(row=1, column=1, sticky=sticky)
|
2020-10-14 09:23:49 -06:00
|
|
|
tkinter.Label(body, text="Output file").grid(row=2)
|
|
|
|
self.keypath = tkinter.Entry(body, width=40)
|
2015-06-30 10:27:33 -06:00
|
|
|
self.keypath.grid(row=2, column=1, sticky=sticky)
|
2020-09-27 04:54:49 -06:00
|
|
|
self.keypath.insert(2, "bnepubkey.b64")
|
2020-10-14 09:23:49 -06:00
|
|
|
button = tkinter.Button(body, text="...", command=self.get_keypath)
|
2015-06-30 10:27:33 -06:00
|
|
|
button.grid(row=2, column=2)
|
2020-10-14 09:23:49 -06:00
|
|
|
buttons = tkinter.Frame(self)
|
2015-06-30 10:27:33 -06:00
|
|
|
buttons.pack()
|
2020-10-14 09:23:49 -06:00
|
|
|
botton = tkinter.Button(
|
2020-09-27 04:54:49 -06:00
|
|
|
buttons, text="Fetch", width=10, command=self.generate)
|
2020-10-14 09:23:49 -06:00
|
|
|
botton.pack(side=tkinter.constants.LEFT)
|
|
|
|
tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
|
|
|
|
button = tkinter.Button(
|
2020-09-27 04:54:49 -06:00
|
|
|
buttons, text="Quit", width=10, command=self.quit)
|
2020-10-14 09:23:49 -06:00
|
|
|
button.pack(side=tkinter.constants.RIGHT)
|
2015-06-30 10:27:33 -06:00
|
|
|
|
|
|
|
def get_keypath(self):
|
2020-10-14 09:23:49 -06:00
|
|
|
keypath = tkinter.filedialog.asksaveasfilename(
|
2020-09-27 04:54:49 -06:00
|
|
|
parent=None, title="Select B&N ePub key file to produce",
|
|
|
|
defaultextension=".b64",
|
2015-06-30 10:27:33 -06:00
|
|
|
filetypes=[('base64-encoded files', '.b64'),
|
|
|
|
('All Files', '.*')])
|
|
|
|
if keypath:
|
|
|
|
keypath = os.path.normpath(keypath)
|
2020-10-14 09:23:49 -06:00
|
|
|
self.keypath.delete(0, tkinter.constants.END)
|
2015-06-30 10:27:33 -06:00
|
|
|
self.keypath.insert(0, keypath)
|
|
|
|
return
|
|
|
|
|
|
|
|
def generate(self):
|
|
|
|
email = self.name.get()
|
|
|
|
password = self.ccn.get()
|
|
|
|
keypath = self.keypath.get()
|
|
|
|
if not email:
|
2020-09-27 04:54:49 -06:00
|
|
|
self.status['text'] = "Email address not given"
|
2015-06-30 10:27:33 -06:00
|
|
|
return
|
|
|
|
if not password:
|
2020-09-27 04:54:49 -06:00
|
|
|
self.status['text'] = "Account password not given"
|
2015-06-30 10:27:33 -06:00
|
|
|
return
|
|
|
|
if not keypath:
|
2020-09-27 04:54:49 -06:00
|
|
|
self.status['text'] = "Output keyfile path not set"
|
2015-06-30 10:27:33 -06:00
|
|
|
return
|
2020-09-27 04:54:49 -06:00
|
|
|
self.status['text'] = "Fetching..."
|
2015-06-30 10:27:33 -06:00
|
|
|
try:
|
|
|
|
userkey = fetch_key(email, password)
|
2020-09-26 14:22:47 -06:00
|
|
|
except Exception as e:
|
2020-09-27 04:54:49 -06:00
|
|
|
self.status['text'] = "Error: {0}".format(e.args[0])
|
2015-06-30 10:27:33 -06:00
|
|
|
return
|
|
|
|
if len(userkey) == 28:
|
|
|
|
open(keypath,'wb').write(userkey)
|
2020-09-27 04:54:49 -06:00
|
|
|
self.status['text'] = "Keyfile fetched successfully"
|
2015-06-30 10:27:33 -06:00
|
|
|
else:
|
2020-09-27 04:54:49 -06:00
|
|
|
self.status['text'] = "Keyfile fetch failed."
|
2015-06-30 10:27:33 -06:00
|
|
|
|
2020-10-14 09:23:49 -06:00
|
|
|
root = tkinter.Tk()
|
2020-09-27 04:54:49 -06:00
|
|
|
root.title("Barnes & Noble ePub Keyfile Fetch v.{0}".format(__version__))
|
2015-06-30 10:27:33 -06:00
|
|
|
root.resizable(True, False)
|
|
|
|
root.minsize(300, 0)
|
2020-10-14 09:23:49 -06:00
|
|
|
DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
|
2015-06-30 10:27:33 -06:00
|
|
|
root.mainloop()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
sys.exit(cli_main())
|
|
|
|
sys.exit(gui_main())
|