Advertisement
Guest User

Untitled

a guest
Sep 8th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. import sys
  2. import sqlite3
  3. import win32crypt
  4. from shutil import copy, move
  5.  
  6.  
  7. class CookieManager():
  8.     """docstring for CookieManager"""
  9.     def __init__(self):
  10.         self.conn = ''
  11.         self.cursor = ''
  12.         self.COOKIE_FILE = r"C:\Users\David\AppData\Local\Google\Chrome\User Data\Default\Cookies"
  13.         self.HOST_KEYS = ".www.html-kit.com", ".html-kit.com"
  14.         self.init_db()
  15.  
  16.  
  17.     def decrypt_cookies(self, query_result):
  18.         cookies = []
  19.         for host_key, name, value, expires_utc, encrypted in query_result:
  20.             if encrypted:
  21.                 decrypted_descr, decrypted = win32crypt.CryptUnprotectData(encrypted, None, None, None, 0)
  22.             else:
  23.                 decrypted_descr, decrypted = u"", u""
  24.             cookies.append((host_key, name, expires_utc, decrypted, decrypted_descr, value))
  25.             # Idx:              0       1        2           3            4            5
  26.         return cookies
  27.  
  28.  
  29.     def init_db(self):
  30.         self.conn = sqlite3.connect(self.COOKIE_FILE)
  31.         self.cursor = self.conn.cursor()
  32.  
  33.  
  34.     def shutdown_db(self, commit=False):
  35.         self.cursor.close()
  36.         if commit:
  37.             self.conn.commit()
  38.         self.conn.close()
  39.    
  40.     def get_cookies(self):
  41.         query_tmpl = "SELECT host_key, name, value, expires_utc, encrypted_value FROM cookies WHERE host_key IN {:}"
  42.         self.cursor.execute(query_tmpl.format(self.HOST_KEYS))
  43.         result = self.cursor.fetchall()
  44.         cookies = self.decrypt_cookies(result)
  45.         return cookies
  46.  
  47.     def update_cookie(self, cookie):
  48.         decrypted_length = len(cookie[3])
  49.         encrypted = win32crypt.CryptProtectData(cookie[3][:decrypted_length // 2], cookie[4], None, None, None, 0)
  50.         self.cursor.execute("UPDATE cookies SET encrypted_value = ?, expires_utc = ? WHERE host_key = ? AND name = ?", (buffer(encrypted), cookie[2], cookie[0], cookie[1]))
  51.  
  52. if __name__ == '__main__':
  53.     #Init DB
  54.     manager = CookieManager()
  55.    
  56.     cookies = manager.get_cookies()
  57.    
  58.     for cookie in cookies:
  59.         print cookie
  60.         if cookie[1] == 'test2':
  61.             #Modify Cookie
  62.             cookie = (cookie[0],cookie[1],cookie[2],'test3',u"")
  63.             manager.update_cookie(cookie)
  64.    
  65.     # Commit
  66.     manager.shutdown_db(True)
  67.    
  68.     # Should check on http://www.html-kit.com/tools/cookietester/
  69.    
  70.     # Open again to check changes
  71.     manager = CookieManager()
  72.    
  73.     cookies = manager.get_cookies()
  74.     manager.shutdown_db(False)
  75.     for cookie in cookies:
  76.         print cookie
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement