Thank you to anyone who has already donated - your generous donations helped make three months of treatment possible.
My brother Nate continues to fight stage IV Hodgkin's lymphoma. He's just 31, with a wife and baby girl. They have no active income (since he's been unable to return to work), no insurance, and cannot afford the treatment he needs. Nate and his family need your help. Please consider a donation, every dollar helps. Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 |
#! /usr/bin/python # -*- coding: utf-8 -*- # unswindle.pyw, version 6-rc1 # Copyright © 2009 i♥cabbages # Released under the terms of the GNU General Public Licence, version 3 or # later. <http://www.gnu.org/licenses/> # To run this program install a 32-bit version of Python 2.6 from # <http://www.python.org/download/>. Save this script file as unswindle.pyw. # Find and save in the same directory a copy of mobidedrm.py. Double-click on # unswindle.pyw. It will run Kindle For PC. Open the book you want to # decrypt. Close Kindle For PC. A dialog will open allowing you to select the # output file. And you're done! # Revision history: # 1 - Initial release # 2 - Fixes to work properly on Windows versions >XP # 3 - Fix minor bug in path extraction # 4 - Fix error opening threads; detect Topaz books; # detect unsupported versions of K4PC # 5 - Work with new (20091222) version of K4PC # 6 - Detect and just copy DRM-free books """ Decrypt Kindle For PC encrypted Mobipocket books. """ __license__ = 'GPL v3' import sys import os import re import tempfile import shutil import subprocess import struct import hashlib import ctypes from ctypes import * from ctypes.wintypes import * import binascii import _winreg as winreg import Tkinter import Tkconstants import tkMessageBox import tkFileDialog import traceback # # _extrawintypes.py UBYTE = c_ubyte ULONG_PTR = PULONG = ULONG_PTR PVOID = LPVOID LPCTSTR = LPTSTR = c_wchar_p LPBYTE = c_char_p SIZE_T = c_uint SIZE_T_p = # # _ntdll.py NTSTATUS = DWORD ntdll = windll.ntdll _fields_ = # NTSTATUS WINAPI NtQueryInformationProcess( # __in HANDLE ProcessHandle, # __in PROCESSINFOCLASS ProcessInformationClass, # __out PVOID ProcessInformation, # __in ULONG ProcessInformationLength, # __out_opt PULONG ReturnLength # ); NtQueryInformationProcess = ntdll.NtQueryInformationProcess NtQueryInformationProcess.argtypes = NtQueryInformationProcess.restype = NTSTATUS # # _kernel32.py INFINITE = 0xffffffff CREATE_UNICODE_ENVIRONMENT = 0x00000400 DEBUG_ONLY_THIS_PROCESS = 0x00000002 DEBUG_PROCESS = 0x00000001 THREAD_GET_CONTEXT = 0x0008 THREAD_QUERY_INFORMATION = 0x0040 THREAD_SET_CONTEXT = 0x0010 THREAD_SET_INFORMATION = 0x0020 EXCEPTION_BREAKPOINT = 0x80000003 EXCEPTION_SINGLE_STEP = 0x80000004 EXCEPTION_ACCESS_VIOLATION = 0xC0000005 DBG_CONTINUE = 0x00010002L DBG_EXCEPTION_NOT_HANDLED = 0x80010001L EXCEPTION_DEBUG_EVENT = 1 CREATE_THREAD_DEBUG_EVENT = 2 CREATE_PROCESS_DEBUG_EVENT = 3 EXIT_THREAD_DEBUG_EVENT = 4 EXIT_PROCESS_DEBUG_EVENT = 5 LOAD_DLL_DEBUG_EVENT = 6 UNLOAD_DLL_DEBUG_EVENT = 7 OUTPUT_DEBUG_STRING_EVENT = 8 RIP_EVENT = 9 _fields_ = DataBlob_p = _fields_ = LPSECURITY_ATTRIBUTES = _fields_ = LPSTARTUPINFO = _fields_ = LPPROCESS_INFORMATION = EXCEPTION_MAXIMUM_PARAMETERS = 15 pass EXCEPTION_RECORD._fields_ = _fields_ = _fields_ = _fields_ = _fields_ = _fields_ = _fields_ = _fields_ = _fields_ = _fields_ = _fields_ = _anonymous_ = ('u',) _fields_ = LPDEBUG_EVENT = CONTEXT_X86 = 0x00010000 CONTEXT_i386 = CONTEXT_X86 CONTEXT_i486 = CONTEXT_X86 CONTEXT_CONTROL = (CONTEXT_i386 | 0x0001) # SS:SP, CS:IP, FLAGS, BP CONTEXT_INTEGER = (CONTEXT_i386 | 0x0002) # AX, BX, CX, DX, SI, DI CONTEXT_SEGMENTS = (CONTEXT_i386 | 0x0004) # DS, ES, FS, GS CONTEXT_FLOATING_POINT = (CONTEXT_i386 | 0x0008L) # 387 state CONTEXT_DEBUG_REGISTERS = (CONTEXT_i386 | 0x0010L) # DB 0-3,6,7 CONTEXT_EXTENDED_REGISTERS = (CONTEXT_i386 | 0x0020L) CONTEXT_FULL = (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS) CONTEXT_ALL = (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS | CONTEXT_EXTENDED_REGISTERS) SIZE_OF_80387_REGISTERS = 80 _fields_ = MAXIMUM_SUPPORTED_EXTENSION = 512 _fields_ = LPCONTEXT = _fields_ = LPLDT_ENTRY = kernel32 = windll.kernel32 # BOOL WINAPI CloseHandle( # __in HANDLE hObject # ); CloseHandle = kernel32.CloseHandle CloseHandle.argtypes = CloseHandle.restype = BOOL # BOOL WINAPI CreateProcess( # __in_opt LPCTSTR lpApplicationName, # __inout_opt LPTSTR lpCommandLine, # __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes, # __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, # __in BOOL bInheritHandles, # __in DWORD dwCreationFlags, # __in_opt LPVOID lpEnvironment, # __in_opt LPCTSTR lpCurrentDirectory, # __in LPSTARTUPINFO lpStartupInfo, # __out LPPROCESS_INFORMATION lpProcessInformation # ); CreateProcess = kernel32.CreateProcessW CreateProcess.argtypes = CreateProcess.restype = BOOL # HANDLE WINAPI OpenThread( # __in DWORD dwDesiredAccess, # __in BOOL bInheritHandle, # __in DWORD dwThreadId # ); OpenThread = kernel32.OpenThread OpenThread.argtypes = OpenThread.restype = HANDLE # BOOL WINAPI ContinueDebugEvent( # __in DWORD dwProcessId, # __in DWORD dwThreadId, # __in DWORD dwContinueStatus # ); ContinueDebugEvent = kernel32.ContinueDebugEvent ContinueDebugEvent.argtypes = ContinueDebugEvent.restype = BOOL # BOOL WINAPI DebugActiveProcess( # __in DWORD dwProcessId # ); DebugActiveProcess = kernel32.DebugActiveProcess DebugActiveProcess.argtypes = DebugActiveProcess.restype = BOOL # BOOL WINAPI GetThreadContext( # __in HANDLE hThread, # __inout LPCONTEXT lpContext # ); GetThreadContext = kernel32.GetThreadContext GetThreadContext.argtypes = GetThreadContext.restype = BOOL # BOOL WINAPI GetThreadSelectorEntry( # __in HANDLE hThread, # __in DWORD dwSelector, # __out LPLDT_ENTRY lpSelectorEntry # ); GetThreadSelectorEntry = kernel32.GetThreadSelectorEntry GetThreadSelectorEntry.argtypes = GetThreadSelectorEntry.restype = BOOL # BOOL WINAPI ReadProcessMemory( # __in HANDLE hProcess, # __in LPCVOID lpBaseAddress, # __out LPVOID lpBuffer, # __in SIZE_T nSize, # __out SIZE_T *lpNumberOfBytesRead # ); ReadProcessMemory = kernel32.ReadProcessMemory ReadProcessMemory.argtypes = ReadProcessMemory.restype = BOOL # BOOL WINAPI SetThreadContext( # __in HANDLE hThread, # __in const CONTEXT *lpContext # ); SetThreadContext = kernel32.SetThreadContext SetThreadContext.argtypes = SetThreadContext.restype = BOOL # BOOL WINAPI WaitForDebugEvent( # __out LPDEBUG_EVENT lpDebugEvent, # __in DWORD dwMilliseconds # ); WaitForDebugEvent = kernel32.WaitForDebugEvent WaitForDebugEvent.argtypes = WaitForDebugEvent.restype = BOOL # BOOL WINAPI WriteProcessMemory( # __in HANDLE hProcess, # __in LPVOID lpBaseAddress, # __in LPCVOID lpBuffer, # __in SIZE_T nSize, # __out SIZE_T *lpNumberOfBytesWritten # ); WriteProcessMemory = kernel32.WriteProcessMemory WriteProcessMemory.argtypes = WriteProcessMemory.restype = BOOL # BOOL WINAPI FlushInstructionCache( # __in HANDLE hProcess, # __in LPCVOID lpBaseAddress, # __in SIZE_T dwSize # ); FlushInstructionCache = kernel32.FlushInstructionCache FlushInstructionCache.argtypes = FlushInstructionCache.restype = BOOL # # debugger.py FLAG_TRACE_BIT = 0x100 pass self.process_info = process_info self.pid = process_info.dwProcessId self.tid = process_info.dwThreadId self.hprocess = process_info.hProcess self.hthread = process_info.hThread self._threads = self._processes = self._bps = self._inactive = if : buf = ref = buf else: size = buf = ref = copied = rv = if not rv: addr = raise if copied.value != size: raise if : return buf.raw return buf hprocess = self.hprocess if bytev is None: byte = bytev = byte.value else: byte = = (bytev, callback) byte.value = 0xcc copied = rv = if not rv: addr = raise if copied.value != 1: raise rv = if not rv: raise return for addr, (bytev, callback) in : hprocess = self.hprocess hthread = self.hthread bytev, callback = = byte = copied = rv = if not rv: raise if copied.value != 1: raise rv = if not rv: raise context = rv = if not rv: raise context.Eip = addr context.EFlags |= FLAG_TRACE_BIT rv = if not rv: raise return hthread = self.hthread hprocess = self.hprocess try: pbi = rv = if rv != 0: raise return pbi.PebBaseAddress except DebuggerError: pass try: context = rv = if not rv: raise entry = rv = if not rv: raise low, mid, high = entry.BaseLow, entry.BaseMid, entry.BaseHi fsbase = low | (mid << 16) | (high << 24) pebaddr = return pebaddr.value except DebuggerError: pass return 0x7ffdf000 addr = + (2 * 4) baseaddr = return baseaddr.value event = finished = False while not finished: rv = if not rv: raise self.pid = pid = event.dwProcessId self.tid = tid = event.dwThreadId self.hprocess = self.hthread = status = DBG_CONTINUE evid = event.dwDebugEventCode if evid == EXCEPTION_DEBUG_EVENT: first = event.Exception.dwFirstChance record = event.Exception.ExceptionRecord exid = record.ExceptionCode flags = record.ExceptionFlags addr = record.ExceptionAddress if exid == EXCEPTION_BREAKPOINT: if addr in self._bps: elif exid == EXCEPTION_SINGLE_STEP: else: status = DBG_EXCEPTION_NOT_HANDLED elif evid == LOAD_DLL_DEBUG_EVENT: hfile = event.LoadDll.hFile if hfile is not None: rv = if not rv: raise elif evid == CREATE_THREAD_DEBUG_EVENT: info = event.CreateThread self.hthread = info.hThread = self.hthread elif evid == EXIT_THREAD_DEBUG_EVENT: hthread = if hthread is not None: rv = if not rv: raise elif evid == CREATE_PROCESS_DEBUG_EVENT: info = event.CreateProcessInfo self.hprocess = info.hProcess = self.hprocess elif evid == EXIT_PROCESS_DEBUG_EVENT: hprocess = if hprocess is not None: rv = if not rv: raise if pid == self.process_info.dwProcessId: finished = True rv = if not rv: raise return True # # unswindle.py KINDLE_REG_KEY = \ r'Software\Classes\Amazon.KindleForPC.content\shell\open\command' pass HOOKS = return (hexdigest in cls.HOOKS) return (addr - 0x00400000) + self.baseaddr self.book_path = None self.book_pid = None self.baseaddr = hooks = for addr, mname in : context.Eax = 1 return context.Eip += 2 context.Eax = 0 return addr = try: path = except DebuggerError: pgrest = 0x1000 - (addr.value & 0xfff) path = path = if u'\0' in path: path = if . not in ('.prc', '.pdb', '.mobi'): return self.book_path = path addr = context.Esp + addr = pid = pid = print pid self.book_pid = pid letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789" crc = (~)&0xFFFFFFFF crc = crc ^ (crc >> 16) res = s l = for i in (0,1): b = crc & 0xff pos = (b // l) ^ (b % l) res += crc >>= 8 return res self.data = data header = if != 'BOOKMOBI': raise self.nsections = nsections = self.sections = sections = for i in : offset, a1, a2, a3, a4 = \ flags, val = a1, ((a2 << 16) | (a3 << 8) | a4) sect = self.crypto_type = if (snum + 1) == self.nsections: endoff = else: endoff = off = return self._exepath = self._hexdigest = self._exedir = self._mobidedrmpath = basedir = .__file__ basedir = for basename in ('mobidedrm', 'mobidedrm.py', 'mobidedrm.pyw'): path = if : return path raise path = None for root in (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE): try: regkey = path = break except WindowsError: pass else: raise if '"' in path: path = . return path path = self._exepath sha1 = with as f: data = while data: data = hexdigest = if not : raise return hexdigest with as f: magic = if magic == 'TPZ0': return True return False with as f: crypto = .crypto_type return (crypto == 0) creation_flags = (CREATE_UNICODE_ENVIRONMENT | DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS) startup_info = process_info = path = pid = None try: rv = if not rv: raise debugger = grabber = path = grabber.book_path pid = grabber.book_pid finally: if process_info.hThread is not None: if process_info.hProcess is not None: if path is None: raise if : raise return (path, pid) if : else: return # darkreverser didn't protect mobidedrm's script execution to allow # importing, so we have to just run it in a subprocess if pid is None: raise with as tmpf: tmppath = tmpf.name args = mobidedrm = output = if not : try: except OSError: pass raise return label = self.text = root = progname = try: unswindler = inpath, pid = outpath = if not outpath: return 0 except UnswindleError, e: return 1 except Exception: text = . return 1 progname = args = if != 1: return 1 outpath = unswindler = inpath, pid = return 0 if __name__ == '__main__': |