1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| import sys, os from struct import unpack try: from StringIO import StringIO except ImportError: from io import StringIO from time import strftime, gmtime from os.path import expanduser import requests
home = expanduser("~") FilePath= home + '/Library/Cookies/Cookies.binarycookies' cookieDict = dict()
try: binary_file=open(FilePath,'rb') except IOError as e: print('File Not Found :'+ FilePath) print('请前往「系统偏好设置」->「安全性与隐私」->「完全磁盘访问权限」,勾选当前的Terminal,以授权访问Cookie文件') sys.exit(0)
file_header=binary_file.read(4)
if str(file_header)!='cook': print("Not a Cookies.binarycookie file") sys.exit(0) num_pages=unpack('>i',binary_file.read(4))[0]
page_sizes=[] for np in range(num_pages): page_sizes.append(unpack('>i',binary_file.read(4))[0]) pages=[] for ps in page_sizes: pages.append(binary_file.read(ps))
for page in pages: page=StringIO(page) page.read(4) num_cookies=unpack('<i',page.read(4))[0] cookie_offsets=[] for nc in range(num_cookies): cookie_offsets.append(unpack('<i',page.read(4))[0])
page.read(4)
cookie='' for offset in cookie_offsets: page.seek(offset) cookiesize=unpack('<i',page.read(4))[0] cookie=StringIO(page.read(cookiesize)) cookie.read(4) flags=unpack('<i',cookie.read(4))[0] cookie_flags='' if flags==0: cookie_flags='' elif flags==1: cookie_flags='Secure' elif flags==4: cookie_flags='HttpOnly' elif flags==5: cookie_flags='Secure; HttpOnly' else: cookie_flags='Unknown' cookie.read(4) urloffset=unpack('<i',cookie.read(4))[0] nameoffset=unpack('<i',cookie.read(4))[0] pathoffset=unpack('<i',cookie.read(4))[0] valueoffset=unpack('<i',cookie.read(4))[0] endofcookie=cookie.read(8) expiry_date_epoch= unpack('<d',cookie.read(8))[0]+978307200 expiry_date=strftime("%a, %d %b %Y ",gmtime(expiry_date_epoch))[:-1] create_date_epoch=unpack('<d',cookie.read(8))[0]+978307200 create_date=strftime("%a, %d %b %Y ",gmtime(create_date_epoch))[:-1] cookie.seek(urloffset-4) url='' u=cookie.read(1) while unpack('<b',u)[0]!=0: url=url+str(u) u=cookie.read(1) cookie.seek(nameoffset-4) name='' n=cookie.read(1) while unpack('<b',n)[0]!=0: name=name+str(n) n=cookie.read(1) cookie.seek(pathoffset-4) path='' pa=cookie.read(1) while unpack('<b',pa)[0]!=0: path=path+str(pa) pa=cookie.read(1) cookie.seek(valueoffset-4) value='' va=cookie.read(1) while unpack('<b',va)[0]!=0: value=value+str(va) va=cookie.read(1) if url==".example.com" : cookieDict[name] = value
binary_file.close()
print("Cookie信息为:") print(cookieDict)
if not cookieDict: print('没有www.example.com的cookie信息') print('请使用Safari打开 https://www.example.com ,刷新一下Cookie') os.system('open -a Safari https://www.example.com/') sys.exit(0)
resp = requests.get(example_url, cookies = cookieDict)
|