Removing eBook DRM Without OCR or GUIs

by lol-md5 (lol-md5@riseup.net)

I've seen a few articles in here for removing DRM on eBooks, but they're all terrible because they use Optical Character Recognition (OCR).

OCR is not only inaccurate and slow, but you also lose all the images in the books unless you manually extract them too.  I've been using Calibre for a long time to DeDRM eBooks, but I decided I wanted a way to do it without GUIs.  I tried using the DeDRM_tools from ApprenticeAlf1, but they didn't work on the command line for some reason.  So here's a hacky solution that uses the Calibre plugin but without actually using or opening Calibre.  You still need to install it though.2

First, extract the plugin from the DeDRM_tools ZIP file:

$ unzip DeDRM_tools_6.5.5.zip DeDRM_calibre_plugin/DeDRM_plugin.zip
$ mkdir -p calibre_plugins/dedrm
$ touch calibre_plugins/__init__.py # make it a package
$ unzip DeDRM_calibre_plugin/DeDRM_plugin.zip -d calibre_ plugins/dedrm

Then copy this file to ~/.config/calibre/plugins/dedrm.json:

{
  "serials": [],
  "bandnkeys": {},
  "androidkeys": {},
  "configured": true,
  "pids": []
}

Edit it accordingly.  PIDs are Mobipocket DRM PIDs.  Serials are Kindle eBook reader serials, without spaces.  DeDRM Tools supports other DRM formats, but I don't have access to all of the files necessary to use them.  If you use Kindle for Android, Barnes and Noble eBooks, "eReader eBooks" (whatever that means), Adobe Digital Editions eBooks, or Kindle for Mac/PC eBooks, open:

Calibre->Preferences->Plugins->Load Plugin from file->DeDRM_plugin.zip

Then head to "Plugins - File type plugins" and double-click "DeDRM Plugin".

Now you'll need this script:

#!/usr/bin/env python
#
# Example:
# ./decrypt.py ~/Documents/Ebooks/Ready\ Player\ One.azw3 RP1-noDRM.azw3.azw3
# 
from __future__ import print_function
import sys, os

path = os.environ.get('CALIBRE_PYTHON_PATH', '/usr/lib/calibre')
if path not in sys.path:
   sys.path.insert(0, path)

sys.resources_location = os.environ.get('CALIBRE_RESOURCES_PATH', '/usr/share/calibre')

sys.extensions_location = os.environ.get('CALIBRE_EXTENSIONS_PATH', '/usr/lib/calibre/calibre/plugins')

sys.executables_location = os.environ.get('CALIBRE_EXECUTABLES_PATH', '/usr/bin')

from calibre_plugins import dedrm

def decrypt(input_filename, output_filename):
   plugin = dedrm.DeDRM('DeDRM_calibre_plugin/DeDRM_plugin.zip')
   plugin.initialize()

   os.rename(plugin.run(input_filename), output_filename)

if __name__ == '__main__':
   try:
	   decrypt(sys.argv[1], sys.argv[2])
   except IndexError:
	   print (
            	'Usage:',
           	sys.argv[0],
            	'<input_filename>',
            	'<output_filename>',
           	file = sys.stderr)

Now just run it:

$ ./decrypt.py ReadyPlayerOne.azw3 RP1-noDRM.azw3

The no-DRM version will be output to ./RP1-noDRM.azw3.

Now you can read it using ebook-viewer RP1-noDRM.azw3 (GUI app).

How This Can Be Used to Steal from Amazon

  1. Buy any eBook (yes, you have to have enough money for it).
  2. Head to: www.amazon.com/myx and click the "Content" tab.
  3. Click the "..." button next to the title of the book you want, and click "Download and transfer via USB".
  4. Select a Kindle whose serial number you have entered into your dedrm.json (you can also do Kindle for PC if you don't have a Kindle).
  5. Click the "..." button again and click "Refund".  You can select any reason, but I always select "Digital rights restrictions".
  6. DeDRM the eBook using the steps above.

Please support eBook authors though, and don't use this method if you can afford to pay.

References

  1. DeDRM_tools from ApprenticeAlf: github.com/apprenticeharper/DeDRM_tools
  2. Calibre: calibre-ebook.com

Code: decrypt.py

Return to $2600 Index