Kindle DRM Removal Using SCons

I have a Kindle. I like it a lot, but it has some issues. The recent Macmillan vs. Amazon fight is one. Another is the DRM on every file purchased from Amazon. If I buy a book, I want to know that I can read it forever. Luckily, smart people figured out how to break the DRM on most Kindle books a long time ago. A few books are in another format called Topaz that until recently hadn’t been broken. I just found a book I wanted that was in that format, so I went and looked again, and it turns out someone finally cracked it about a month ago.

I’m not going to link to any of the actual tools here, but they shouldn’t be too hard to find. My contribution is an SConstruct file for the SCons build system that automatically copies all my books to my computer and removes the DRM all in one step.  It’s not your typical use of a build system, but when I thought about what I was trying to do, I realized it was really just a simple dependency graph, something SCons is perfect for.

The complete process is:

  1. Plug Kindle into computer via USB
  2. In a shell, cd <path/where/this/file/lives>
  3. scons

This will automatically get any new books, and remove the DRM (and in Topaz’s case, convert them into svg files viewable in a browser)

Installation is not difficult either:

  1. Install SCons
  2. Put this SConstruct file in the directory where you want to save your books
  3. Get your Kindle’s PID and put it in the SConstruct file (the DRM tools will tell you how to do that)
  4. Edit the location your Kindle gets mounted to. The one I have in there is for a Mac, but except for that path I don’t see any reason this won’t all work on any OS
  5. Put the DRM tools in a tools subdirectory

SConstruct file after the jump

import os
env = Environment()
# Put your Kindle's PID here
env['KINDLECODE'] = '1234567890'
# Put the path to your Kindle when attached via USB
# (This is where it shows up on a Mac)
env['DEVICEDIR'] = '/Volumes/Kindle/documents'
# Topaz dedrm only wants the first 8 chars of the kindle PID
env['MOBIDEDRM'] = 'tools/MOBI/'
env['TOPAZDEDRM'] = topazprogs + ''
env['TOPAZ2SVG'] = topazprogs + ''
env['TOPAZ2HTML'] = topazprogs + ''
if os.path.exists(env['DEVICEDIR']):
    kindle = Glob(env['DEVICEDIR'] + '/*.azw') + Glob(env['DEVICEDIR'] + '/*.azw1')
    for k in kindle:
        Command('DRM/', k, Copy("$TARGET", "$SOURCE"))
azw = Glob('DRM/*.azw')
mobi = []
for a in azw:
    base,ext = os.path.splitext(
    mobi += env.Command('#Mobi/'+base+'.mobi', a,
                        '$MOBIDEDRM $SOURCE $TARGET "$KINDLECODE"')
Ignore(mobi, env['MOBIDEDRM'])
azw1 = Glob('DRM/*.azw1')
topaz = []
for a in azw1:
    base,ext = os.path.splitext(
    topazdir = env.Dir('#Topaz/'+base+'/')
    dat = env.Command(topazdir.File('dict0000.dat'), a,
                      '$TOPAZDEDRM -v -v -d -o "'+topazdir.path+'" -p "$SHORTKINDLECODE" $SOURCE')
    svg = env.Command(topazdir.Dir('svg'), dat,
                      '$TOPAZ2SVG "'+topazdir.path+'"')
    topaz += svg
    #html = env.Command('#Topaz/'+base+'/book.html', topazdir,
    #                   '$TOPAZ2HTML $SOURCE')
    #topaz += html
    #env.Depends(html, svg)
env.Alias('all', mobi)
env.Alias('all', topaz)
env.Alias('mobi', mobi)
env.Alias('topaz', topaz)

Tags: , , , , , ,

Sunday, February 21st, 2010 Uncategorized

3 Comments to Kindle DRM Removal Using SCons

  • Ogre says:

    I believe the tools that I didn’t link to also work with books downloaded for Kindle for PC. I’m not completely sure, but I think the recent liberation of Topaz files was actually due to the release of Kindle for PC making it that much easier.

  • why why says:

    Hello,I wonder whether it can used to remove the DRM from books downloaded by kindle 3 which has latest firmware, have you ever tested?

  • The latest tools will work with Mobipocket and Topaz ebooks from a Kindle 3. See my blog for details and links.

    — Alf.

  • Leave a Reply