-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.py
183 lines (161 loc) · 5.63 KB
/
cookies.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python2
'''
usage: COMMAND FILE
the output format is:
Server name
(path , cookie_name , cookie_value , expiry , last_visited , secure , only_send_to_creator)
sorry, i didn't find that what is the version information using for....
'''
import sys
import struct
class cookie_records_data():
def __init__(self,path_t):
self.path=path_t
self.name=''
self.value=''
self.expiry_date=0
self.last_used_date=0
self.unknow=''
self.secure='FALSE' # 9BA9 anquan:fou jinfasong:shi A9 anquan:fou jinfasong:fou
self.only_send='FALSE'
class cookie_domain_data:
name=[]
cookies=[]
domain_level=-1
domain=[]
## du to the expiry_date
dic_fucker={}
#class cookie_domain(cookie_domain_data):
# def getdomain_by_str():
def read_domains():
domain_name_buf=[]
domain_cookie_buf=[]
domain_path_buf=[]
tag_id=''
while 1:
tag_id=fp.read(1)
if not tag_id:break
if tag_id=='\x01':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
domain_name_buf.append(read_record(str_len,''))
elif tag_id=='\x02':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
domain_path_buf.append(read_record(str_len,''))
elif tag_id=='\x03':
##read cookie
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
domain_cookie_buf.append(read_record(str_len,domain_path_buf[:]))
elif tag_id=='\x85':
if domain_cookie_buf!=[]:
domain_class=cookie_domain_data()
domain_class.cookies=domain_cookie_buf
domain_class.domain_level=''
domain_class.name=domain_name_buf[:]
domain_class.name.reverse()
domain.append(domain_class)
domain_path_buf=[]
domain_cookie_buf=[]
elif tag_id=='\x84':
##domain back
if domain_name_buf!=[]:
domain_name_buf.pop(-1)
else:
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
read_record(str_len,'')
def read_record(len_f,path_t):
cookies_class=cookie_records_data(path_t)
while len_f>0:
#print('len_fsssss='+str(len_f))
domain_len=0
tag_id=fp.read(1)
len_f-=1
if tag_id=='\x1E':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
len_f-=2
#print('sdfsdf',str_len)
domain_name=fp.read(str_len)
len_f-=str_len
return domain_name
elif tag_id=='\x10':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
len_f-=2
cookie_name=fp.read(str_len)
len_f-=str_len
cookies_class.name=cookie_name
continue
elif tag_id=='\x11':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
len_f-=2
cookie_value=fp.read(str_len)
len_f-=str_len
cookies_class.value=cookie_value
continue
elif tag_id=='\x12':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
len_f-=2
cookie_expiry=int("%d"%struct.unpack('>q',fp.read(str_len)))
len_f-=str_len
if cookie_expiry!=0:
cookies_class.expiry_date=cookie_expiry
dic_fucker[cookie_name]=cookie_expiry
else:
cookies_class.expiry_data=dic_fucker[cookie_name]
continue
elif tag_id=='\x13':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
len_f-=2
#print(cookie_name)
cookie_last_used=int("%d"%struct.unpack('>q',fp.read(str_len)))
len_f-=str_len
cookies_class.last_used_date=cookie_last_used
continue
elif tag_id=='\x1D':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
len_f-=2
path=fp.read(str_len)
len_f-=str_len
return path
elif tag_id=='\x28':
str_len=int("%d"%struct.unpack('>H',fp.read(2))) #str length
len_f-=2
fp.read(str_len)
len_f-=str_len
elif tag_id=='\x99': # MSB_VALUE(0x80) | 0x19
cookies_class.secure='TRUE'
elif tag_id=='\x9B': # MSB_VALUE(0x80) | 0x1B
cookies_class.only_send='TRUE'
return cookies_class
def output_result():
for i in domain:
print('.'.join(i.name))
for j in i.cookies:
print(j.path,j.name,j.value,j.expiry_date,j.last_used_date,j.secure,j.only_send)
def to_netscape(tofile):
filelines=[]
for i in domain:
for j in i.cookies:
line='\t'.join([
'.'.join(i.name),
str(j.only_send),
'/'+'/'.join(j.path),
str(j.secure),
str(j.expiry_date),
str(j.name),
str(j.value)
])
filelines.append(line+'\n')
fw=open(tofile,'w')
fw.writelines(filelines)
fw.close()
if __name__ == "__main__":
if len(sys.argv)<=1:
sfile=r'data/cookies4.dat'
tofile=r'data/cookies.txt'
else:
sfile=sys.argv[1]
tofile=sys.argv[2]
fp=open(sfile,'rb')
fp.seek(12)
read_domains()
#output_result()
to_netscape(tofile)