Advertisement
Guest User

Untitled

a guest
Oct 20th, 2015
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. #This script will generate dat files that allow offline play of premium modules for the game Neverwinter Nights.
  2. #YOU MUST ONLY USE THIS SCRIPT TO GENERATE DATS FOR PREMIUM MODULES THAT YOU LEGALLY OWN!
  3. import os, sys
  4. from binascii import unhexlify
  5.  
  6. try:
  7.     from Crypto.Cipher import Blowfish
  8. except ImportError:
  9.     print 'You must have PyCrypto installed to use this script.'
  10.     print "\nIf you're on Windows, the easiest way to get it is to grab an installer from:"
  11.     print 'http://www.voidspace.org.uk/python/modules.shtml#pycrypto'
  12.     sys.exit()
  13.  
  14. #These are only part of the key used to decrypt the module.
  15. #The game generates the last 16 bytes of the key from the MD5 hash over the NWM file for the module.
  16. #Once you have the full key, you can decrypt the HAK with Blowfish in ECB mode, ignoring the first 8 bytes of the file.
  17. keys = [
  18.     [unhexlify('5CA883CE6D9C843BDEA1D7857887FD0DD79742389353CD3DEC03CE150948D79B86201921C26E42EE'), "Neverwinter Nights - Kingmaker.dat"],
  19.     [unhexlify('ECBF9A7539E46510CAA1B63A76A250DDE206EA39200FBCC75D03D6B27583EF08A7FD68DB33AA69A1'), "Neverwinter Nights - ShadowGuard.dat"],
  20.     [unhexlify('72A38B0C496353327D869C3D488477BDBB7C48DC33454078AF2664967F804467F7DA0874F4D87C2D'), "Neverwinter Nights - Witch's Wake.dat"],
  21.     [unhexlify('9437DD56EDD9F0419128C2D54F97E85D954241B5A3A203B6088BC1C7CBEED8D9834E9FAB69416CFC'), "Neverwinter Nights - Pirates of the Sword Coast.dat"],
  22.     [unhexlify('1DEA5E040B43F22F239004385CA1189CFFB1B186E669678E2908944C08220A8C9DC5FEE22784E7D3'), "Neverwinter Nights - Wyvern Crown of Cormyr.dat"],
  23.     [unhexlify('8A835A2D01105CBECE2CD069B848C9BEAA7E57BDAB94CE0D0910BD578D1A0D35C98490FA7B653297'), "Neverwinter Nights - Infinite Dungeons.dat"]
  24.     ]
  25.  
  26. if len(sys.argv) < 2:
  27.     print 'Usage: %s *Key1*' % (os.path.basename(sys.argv[0]))
  28.     print 'Example: %s ABCDE-FGHIJ-KLMNO-PQRST-UVWXY-ZABCD-EFGHI' % (os.path.basename(sys.argv[0]))
  29.     print '\nTo find your CD key, look in "nwncdkey.ini" in the game folder.'
  30.     print 'You want the key listed as "Key1".'
  31.     print'\nYOU MUST ONLY USE THIS SCRIPT TO GENERATE DATS\nFOR PREMIUM MODULES THAT YOU LEGALLY OWN!'
  32.     sys.exit()
  33.  
  34. cdKey = sys.argv[1].upper()
  35. cdKey = cdKey.replace('-','')
  36. if len(cdKey) != 35:
  37.     print 'The CD key(minus dashes) should be 35 characters long'
  38.     sys.exit()
  39. cdKey = cdKey + b'\x00' * (56-len(cdKey)) #Pad the CD key on the right with 0x00 to 56 bytes to get the encryption key for the dat.
  40.  
  41. cipher = Blowfish.new(cdKey, Blowfish.MODE_ECB)
  42.  
  43. print '\nModules:'
  44. for idx, key in enumerate(keys):
  45.     print '[%u] %s' % (idx+1, key[1][:-4])
  46. print '[q] quit'
  47. print'\nYOU MUST ONLY USE THIS SCRIPT TO GENERATE DATS\nFOR PREMIUM MODULES THAT YOU LEGALLY OWN!\n'
  48.  
  49. while(1):
  50.     inp = raw_input("Module choice: ")
  51.     try:
  52.         mnum = int(inp)-1
  53.     except ValueError:
  54.         if inp == 'q':
  55.             sys.exit()
  56.         continue
  57.     if (mnum < 0) or (mnum > 5):
  58.         continue
  59.  
  60.     buff = b'\x01' #Pointer to key data(just this 1 byte)
  61.     buff += keys[mnum][0] #Key data
  62.     buff += b'\x00' * (1024-1-len(buff)) #Rest of the file can be whatever, it's not checked by the game.
  63.     buff += b'\x28' #Hardcoded check in the game for 0x28 here
  64.  
  65.     if len(buff) != 1024:
  66.         print 'Error'
  67.         sys.exit()
  68.  
  69.     with open(keys[mnum][1], 'wb') as fh:
  70.         fh.write(cipher.encrypt(buff))
  71.  
  72.     print 'Created "%s"' % (keys[mnum][1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement