tp86 / legimi

Unofficial Legimi ebook downloader for Kindle and Linux
11 stars 0 forks source link

mobi format encryption #8

Closed adam-kulagowski closed 1 year ago

adam-kulagowski commented 1 year ago

Since I don't own Kindle reader I'm curios: Does downloading mobi files for Kindle device ID gets those files in clear format or those files are still encrypted and readable only by that Kindle device?

When supplying android / pocketbook devices S/N - file downloaded by your app are not plain mobi files - there seems to be some DRM on top of them.

tp86 commented 1 year ago

I'm not sure as I don't how exactly Legimi's DRM works. The script is just alternative downloader that works on OSes not supported by "Legimi for Kindle" application. It's not meant to bypass Legimi's DRMs or provide full support for all Legimi features.

However, I guess that this script is only usable with Kindle devices. In the first version it was usable only with my Kindle :wink:.

What I know about Legimi for Kindle is that books can only be opened on specific Kindle (the one for which device id is obtained) and are valid until current subscription period ends. In the next subscription period you need to download book again.

Perhaps for android / pocketbook you should just use official download method as described at https://www.legimi.pl/pobierz-legimi/ - it will definitely work better than my script :wink:

adam-kulagowski commented 1 year ago

I'm doing that but legimi app for android (I'm using (onyx boox poke 4) ) is consuming battery like crazy. Can You check if file downloaded (when using Kindle device ID) are *mobi files? Ie can they be open with normal Linux mobi reader? If you don't have such reader at hand a simple 'file xxx.mobi' should either return: data (which means its encrypted) or mobi/zip file - which means it's unprotected. Each device id used by Your app return different file, even when downloading same book. So it's definitely device based drm. So I'm curious how it looks like for kindle 🙂

tp86 commented 1 year ago

It's encrypted for Kindle as well - file xxx.mobi returns data - which makes perfect sense. Otherwise Legimi couldn't control subscriptions - you could read someone else's book or read it long after subscription expired.

I'm not sure what happens if you just buy a book, without subscription. I guess in this case you get a download link to unencrypted file, since you can buy even without an account. But maybe mobi format is not available then.

I haven't explored other options because I care only about being able to download for Kindle and on unsupported OS (Linux).

adam-kulagowski commented 1 year ago

Which is strange: if You send mobi file to Kindle using linux tools - then there is no DRM in progress. So somehow that mobi file must be encrypted in advance by Legimi server. But headers of that file (at least in my case) does not match any known DRM format. Could you (for sake of curiosity) attach first 128bytes of downloaded mobi file?

If You feel uncomfortable with this topic or feel that we already went to far offtopic here - feel free to say so and/or close this issue anytime. I already feel that I'm overusing your courtesy :)

tp86 commented 1 year ago

So somehow that *mobi file must be encrypted in advance by Legimi server.

There is something about this topic here

More digging reveals more interesting stuff: https://biz.legimi.com/Content/Files/legimi-watermark-ebook-specification.pdf

In this document there is mentioned that

because of the need to make the watermark invisible to the end user, the message should be properly distributed in the whole file

On Kindle there is/was also an option to lend books and looks like it's involved - after subscription period is over, I can't open the book - it says it's expired and I need to download file again. Maybe being able to open a file on a specific device is also linked to this.

I guess Kindle is a special case - it probably looks different for other devices.

But how exactly Kindle reader knows how to handle it properly - I'm not sure. You're right that during copy through USB on Linux there is nothing that alters the file. So, Kindle reader itself must know how to read encrypted file. Maybe that's why ebooks downloaded with my script are not usable for other device kinds. I have reverse-engineered protocol based on traffic exchanged between "Legimi for Kindle" app and server. Apps for different devices may use different messages and different encryption to be handled properly by devices.