Advertisement
MilkBubblesPaste

DB Encryption Python

Jan 31st, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 KB | None | 0 0
  1. #!/bin/python
  2. # I use this to secure the AIDE database. When run with: -d <passphrase> /var/lib/aide.db.gz.enc
  3. # it will decrypt the database, run aide and then re-encrypt the DB.
  4. # for first setup do: -e <passphrase> <path to database>.
  5.  
  6.  
  7. from Crypto.Cipher import AES
  8. from subprocess import call
  9. import base64
  10. import os, sys, random, struct, hashlib,md5
  11.  
  12. def usage():
  13.     print "Encryption"
  14.     print ""
  15.     print "___________________________________"
  16.     print "|  Aide database encryption tool  |"
  17.     print "___________________________________"
  18.     print "\n Usage: " + sys.argv[0] + " <options> <passkey> <filename> \n"
  19.     print "Options:"
  20.     print "     -e: encrypt a file, output encrypted file as <filename>.enc "
  21.     print "     -d: decrypt a file. Takes a .enc file and decrypts it"
  22.  
  23. def run_aide():
  24.         try:
  25.             call(["aide"])
  26.             return
  27.         except:
  28.             print "Aide failed to run"
  29.             sys.exit(2)
  30.  
  31. def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
  32.     #key = sys.argv[2]
  33.     if not out_filename:
  34.         out_filename = in_filename + '.enc'
  35.     try:
  36.         iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
  37.         encryptor = AES.new(key, AES.MODE_CBC, iv)
  38.         filesize = os.path.getsize(in_filename)
  39.         with open(in_filename, 'rb') as infile:
  40.             with open(out_filename, 'wb') as outfile:
  41.                 outfile.write(struct.pack('<Q', filesize))
  42.                 outfile.write(iv)
  43.                 while True:
  44.                     chunk = infile.read(chunksize)
  45.                     if len(chunk) == 0:
  46.                         break
  47.                     elif len(chunk) % 16 != 0:
  48.                         chunk += ' ' * (16 - len(chunk) % 16)
  49.                     outfile.write(encryptor.encrypt(chunk))
  50.         os.remove(in_filename)
  51.     except:
  52.         print "File Encryption Failed"
  53.         sys.exit(2)
  54.    
  55.  
  56. def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
  57.     #key = sys.argv[2]
  58.     if not out_filename:
  59.         out_filename = os.path.splitext(in_filename)[0]
  60.  
  61.     with open(in_filename, 'rb') as infile:
  62.         origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
  63.         iv = infile.read(16)
  64.         decryptor = AES.new(key, AES.MODE_CBC, iv)
  65.  
  66.         with open(out_filename, 'wb') as outfile:
  67.             while True:
  68.                 chunk = infile.read(chunksize)
  69.                 if len(chunk) == 0:
  70.                     break
  71.                 outfile.write(decryptor.decrypt(chunk))
  72.             outfile.truncate(origsize)
  73.     try:
  74.         call(["gzip", "-lq" , out_filename])
  75.         print out_filename
  76.         os.remove(in_filename)
  77.         run_aide()
  78.         encrypt_file(key, out_filename)
  79.         return
  80.     except:
  81.         print "incorrect passphrase"  
  82.         sys.exit(2)
  83.  
  84.  
  85. def check_user_input():
  86.     if len (sys.argv) == 1:
  87.         usage()
  88.         sys.exit(2)
  89.     else:
  90.         return
  91.  
  92. def check_infile():
  93.     input_file = sys.argv[3]
  94.     if os.path.isfile(input_file) == True:
  95.         return
  96.     else:
  97.         print "File does not exist"
  98.         sys.exit(2)
  99.  
  100. if __name__ == '__main__':
  101.    
  102.     decider = sys.argv[1]
  103.     if decider == "-e":
  104.         # md5 hases the passkey to ensure 16 byte length
  105.         keys = sys.argv[2]
  106.         m = md5.new()
  107.         m.update(keys)
  108.         key = m.hexdigest()
  109.         # end md5 conversion
  110.         check_user_input()
  111.         check_infile()
  112.         input_file = sys.argv[3]
  113.         #print "encrypting"
  114.         encrypt_file(key,input_file)
  115.     if decider == "-d":
  116.          # md5 hashes the passkey to ensure 16 byte length
  117.         keys = sys.argv[2]
  118.         m = md5.new()
  119.         m.update(keys)
  120.         key = m.hexdigest()
  121.         # end md5 conversion
  122.         check_user_input()
  123.         check_infile()
  124.         input_file = sys.argv[3]
  125.         #print "decrypting"
  126.         decrypt_file(key,input_file)
  127.         #run_aide()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement