def testCookie(): username = os.environ.get('USERNAME') cookie_file = 'C:/Users/{UserName}/AppData/Local/Google/Chrome/User Data/Default/Cookies'.format( UserName=username) print(cookie_file) con = sqlite3.connect(cookie_file) cursor = con.cursor() domain = "1688" #sql = 'SELECT host_key, name, value, encrypted_value FROM cookies WHERE name = "xxxxx" and host_key="xxxxx";' sql = 'SELECT host_key, name, value, encrypted_value FROM cookies WHERE host_key like "%{}%"'.format( domain) try: if cursor.execute(sql): for en_value in cursor: pwdHash = en_value[3] if pwdHash: ret = CryptUnprotectData(pwdHash, None, None, None, 0) a = bytes.decode(ret[0]) b = bytes.decode(ret[1]) print(a) print(b) except Exception as e: print(e)
def set_cookies(session: Session): ''' read cookies from chrome sqlite db ''' cookie_path = os.path.join(os.environ['localappdata'], 'Google\\Chrome\\User Data\\Default\\Cookies') with contextlib.closing(sqlite3.connect(cookie_path)) as con: cur = con.execute(''' select name, encrypted_value from cookies where host_key like "%google.com"''') for name, encrypted_value in cur: value = CryptUnprotectData(encrypted_value, None, None, None, 0)[1] if name is bytes: name = name.decode() cookie = {"name": name, "value": value.decode()} session.cookies.set(**cookie)
def __get_key(self): with open(self.state_path, "r", encoding="utf-8") as state: return CryptUnprotectData( b64decode(loads( state.read())["os_crypt"]["encrypted_key"])[5:], None, None, None, 0)[1]
def chrome_cookieswin(url): parsed_url = urllib.parse.urlparse(url) if parsed_url.scheme: domain = parsed_url.netloc else: raise urllib.error.URLError("You must include a scheme with your URL.") cookie_file = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" try: conn = sqlite3.connect(cookie_file) host = '.' + '.'.join(domain.split('.')[1:]) sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookie_file) as conn: cu = conn.cursor() cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute( sql).fetchall() } #print(cookies) return cookies except sqlite3.OperationalError: print("Unable to connect to cookie_file at: {}\n".format(cookie_file))
def getcookiefromchrome(host='yaohuo.me'): cookiepath=os.environ['LOCALAPPDATA']+r"\Google\Chrome\User Data\Default\Cookies" sql="select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu=conn.cursor() cookies={name:CryptUnprotectData(encrypted_value)[1].decode() for host_key,name,encrypted_value in cu.execute(sql).fetchall()} return cookies
def getCookieFromChrome(host='yidian88.net'): cookiepath = r"C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute(sql).fetchall() } print(cookies) return cookies
def getcookiefrom360se(host='.oschina.net'): cookiepath = os.environ['APPDATA'] + \ r"\360se6\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host # sql = "select host_key,name,encrypted_value from cookies" with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() # print(cu.execute(sql).fetchall()) cookies = {name: CryptUnprotectData(encrypted_value)[1].decode( ) for host_key, name, encrypted_value in cu.execute(sql).fetchall()} # print(cookies) return cookies
def chrome_cookies_win(host='.oschina.net'): """read from window""" cookiepath = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute(sql).fetchall() } #print(cookies) return cookies
def get_cookie_from_chrome(host): """从Chrome中获取cookie""" cookiepath = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() lst = cu.execute(sql).fetchall() cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in lst } # print(cookies) return cookies
def GetCookieFromChrome(host='.zhihu.com', typeID=1): #C:\Users\Eniru\AppData\Local\Google\Chrome\User Data\Default\Cookies cookiepath = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() if (typeID == 1): sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host #只拿到这几项 cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute( sql).fetchall() } elif (typeID == 2): sql = "select * from cookies where host_key='%s'" % host a = cu.execute(sql).fetchall() cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute( sql).fetchall() } pass return cookies
def get_cookie_from_chrome(host='.eastmoney.com', debug=False): '''从本地chrome浏览器cookies中读取数据,返回dict类型''' from win32.win32crypt import CryptUnprotectData cookiepath = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host #sql="select host_key,name,encrypted_value from cookies " with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute(sql).fetchall() } if debug: print(cookies.keys()) #输出键名列表 return cookies
def get_chrome_cookies(host): """ 获取浏览器cookies :param host: :return: cookies """ cookies_path = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" with sqlite3.connect(cookies_path) as conn: sqlite = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host cu = conn.cursor() for host_key, name, encrypted_valude in cu.execute(sqlite).fetchall(): cookies = {name: CryptUnprotectData(encrypted_valude)[1].decode()} return cookies
def get_cks(sql): cks = [] with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() cookies = cu.execute(sql).fetchall() for host_key, name, encrypted_value in cookies: ck = {} ck['domain'] = host_key ck['name'] = name try: ck['value'] = CryptUnprotectData(encrypted_value)[1].decode() except: ck['value'] = encrypted_value cks.append(ck) return cks
def getcookiefromchrome(): host = '.taobao.com' cookies_str = '' cookiepath = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute(sql).fetchall() } # for key,values in cookies.items(): # cookies_str = cookies_str + str(key)+"="+str(values)+';' return cookies
def get_cookie_from_chrome(host, plain=True): """ 从本地 Chrome 加密的 cookie 文件中读取明文 cookie 数据 注意: 如果输出字典格式(plain=False) 某些字段可能自带双引号(可能会导致转 json 出错) 后续注意相应处理 原因详情自行查询 "cookie 多出双引号" :param host: 指定某个网站 :param plain: (optional) 获取的 cookie 明文是否以 plain 格式输出 True=输出字符串 False=输出字典 :return cookies: <class 'dict'> or <class 'str'> 返回的明文 cookie """ local_state_path = os.environ[ 'LOCALAPPDATA'] + r'\Google\Chrome\User Data\Local State' cookie_path = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" def pull_the_key(base64_encrypted_key): encrypted_key_with_header = base64.b64decode(base64_encrypted_key) encrypted_key = encrypted_key_with_header[5:] real_key = CryptUnprotectData(encrypted_key, None, None, None, 0)[1] return real_key def decrypt_string(origin_key, data): nonce, cipher_bytes = data[3:15], data[15:] aesgcm_key = AESGCM(origin_key) plain_bytes = aesgcm_key.decrypt(nonce, cipher_bytes, None) plain_text = plain_bytes.decode('utf-8') return plain_text with sqlite3.connect(cookie_path) as conn: cu = conn.cursor() sql = f"select host_key,name,encrypted_value from cookies where host_key='{host}'" origin_cookies = cu.execute(sql).fetchall() cu.close() cookies = {} with open(local_state_path, 'r', encoding='utf-8') as f: local_state = json.load(f)['os_crypt']['encrypted_key'] key = pull_the_key(local_state) for host_key, name, encrypted_value in origin_cookies: if encrypted_value[0:3] == b'v10': cookies[name] = decrypt_string(key, encrypted_value) else: cookies[name] = CryptUnprotectData(encrypted_value)[1].decode() if plain: temp = "" for k, v in cookies.items(): temp += f"{k}={v}; " cookies = temp.strip('; ') return cookies
def getcookiefromchrome(host='finaloan-web.yixincapital.com'): flag = True while flag: try: cookiepath = os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() cookies = {name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute(sql).fetchall()} if len(cookies) > 0: flag = False else: print("未获取cookies") time.sleep(60) login() except Exception as e: print("连接数据库失败") return cookies
def getCookieFromChrome(host_key): """ 根据域名host_key获取谷歌浏览器cookie数据 """ cookiepath = os.environ[ 'LOCALAPPDATA'] + r'\Google\Chrome\User Data\Default\Cookies' print(cookiepath) # Chrome的cookie存储在sqlite数据库中,name:value 其中的 # value值是经过CryptprotectData加密的 # 需要使用windows的CryptUnprotectData解密函数进行解密 with sqlite3.connect(cookiepath) as conn: cursor = conn.cursor() querySql = 'select host_key, name, encrypted_value from cookies where host_key="{}"'.format( host_key) cookieData = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cursor.execute( querySql).fetchall() } print(cookieData) return cookieData
def getcookiefromchrome(path, fx=""): if fx == "fx": get_hosts(path, "fx") else: get_hosts(path, "") sql = "select host_key,name,path,encrypted_value from cookies" if fx == "fx": sql = "select host,name,path,value from moz_cookies" con = sqlite3.connect(path) con.row_factory = sqlite3.Row cur = con.cursor() for host in hosts: uk = {} c_str = "" if fx == "fx": cur.execute(sql + " WHERE host LIKE \"" + host + "\"") else: cur.execute(sql + " WHERE host_key LIKE \"" + host + "\"") for row in cur: if fx == "fx": name = row['name'] value = row['value'] host = row['host'] path = row['path'] uk.setdefault(name, value) else: name = row['name'] value = CryptUnprotectData(row['encrypted_value'])[1].decode() host = row['host_key'] path = row['path'] uk.setdefault(name, value) for k, v in uk.items(): c_str += k + "=" + v + "; " print("Host: " + host) print("Path: " + path) print("Cookie: " + c_str) print("=" * 20) cur.close()
def getcookiefromchrome(host='.oschina.net'): f = open("cookies.dat", 'w') cookiepath = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value,path,expires_utc from cookies where host_key like '%%%s%%'" % host print(sql) with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() #cookies={name:CryptUnprotectData(encrypted_value)[1].decode() for host_key,name,encrypted_value in cu.execute(sql).fetchall()} for host_key, name, encrypted_value, path, expires_utc in cu.execute( sql).fetchall(): #print(CryptUnprotectData(encrypted_value)[1].decode()) strValue = CryptUnprotectData(encrypted_value)[1].decode() #acw_tc if expires_utc == 0: continue strCookie = "%s=%s; expires=%s; domain=%s; path=%s\n" % ( name, strValue, getExpires(expires_utc), host_key, path) f.write(strCookie) print(strCookie) #print(cookies) #return cookies f.close()
if config.is_use_chrome_cookies == True and os.name == 'nt': import sqlite3 from win32.win32crypt import CryptUnprotectData host = '.zhihu.com' if config.cookiepath: # 使用自己配置的 cookiepath cookiepath = config.cookiepath if os.path.exists(os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies"): # 使用 Chrome 的 cookiepath cookiepath = os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" elif os.path.exists(os.environ['LOCALAPPDATA'] + r"\CentBrowser\User Data\Default\Cookies"): # 使用百分的 cookiepath cookiepath = os.environ['LOCALAPPDATA'] + r"\CentBrowser\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() cookies = {name: CryptUnprotectData(encrypted_value)[1].decode( ) for host_key, name, encrypted_value in cu.execute(sql).fetchall()} texts = [] for k, v in cookies.items(): # if v.find('"') > -1: # v = v.replace('"', '') texts.append(f'{k}={v}') # print('Authorization maybe you should get it by yourself') hash = '' if config.show_cookies_in_loading == True: print('默认加载 Chrome 的 Cookies(这可以在 config.py 里面修改):' + '; '.join(texts)) headers = { 'Cookie': '; '.join(texts), 'Accept-Encoding': 'gzip', # 'User-Agent': 'com.zhihu.android/Futureve/5.21.2 Mozilla/5.0 (Linux; Android 5.1.1; SM-G925F Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Safari/537.36', 'User-Agent': 'ZhihuHybrid com.zhihu.android/Futureve/5.21.2 Mozilla/5.0 (Linux; Android 5.1.1; SM-G925F Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Safari/537.36',
def chrome_decrypt_win(encrypted_value): return CryptUnprotectData(encrypted_value)[1].decode()
def pull_the_key(base64_encrypted_key): encrypted_key_with_header = base64.b64decode(base64_encrypted_key) encrypted_key = encrypted_key_with_header[5:] real_key = CryptUnprotectData(encrypted_key, None, None, None, 0)[1] return real_key
if os.path.exists(os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" ): # 使用 Chrome 的 cookiepath cookiepath = os.environ[ 'LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies" elif os.path.exists( os.environ['LOCALAPPDATA'] + r"\CentBrowser\User Data\Default\Cookies"): # 使用百分的 cookiepath cookiepath = os.environ[ 'LOCALAPPDATA'] + r"\CentBrowser\User Data\Default\Cookies" sql = "select host_key,name,encrypted_value from cookies where host_key='%s'" % host with sqlite3.connect(cookiepath) as conn: cu = conn.cursor() cookies = { name: CryptUnprotectData(encrypted_value)[1].decode() for host_key, name, encrypted_value in cu.execute(sql).fetchall() } texts = [] for k, v in cookies.items(): # if v.find('"') > -1: # v = v.replace('"', '') texts.append(f'{k}={v}') # print('Authorization maybe you should get it by yourself') hash = '' if config.show_cookies_in_loading == True: print('默认加载 Chrome 的 Cookies(这可以在 config.py 里面修改):' + '; '.join(texts)) headers = { 'Cookie': '; '.join(texts), 'Accept-Encoding': 'gzip',