iOS Cookies.binarycookies(俗称63数据)存取

 

iOS 系统会自动将Safari或APP中网络请求的cookie保存为文件,APP的cookie保存路径为
APP的沙盒路径:var/mobile/Containers/Data/Application/x-x-x/Library/Cookies/Cookies.binarycookies

Cookies.binarycookies是二进制文件,文件结构有个魔术头:字符串:“cook”,二进制为636f6f6b,所以
Cookies.binarycookies数据又被称为63数据。

要对文件内容进行存取,首先得了解文件结构:

字段大小端类型大小备注
Magic大端UTF-84"cook", no terminator
Number of pages大端Unsigned Int4 
Page N size大端Unsigned Int4Repeat for N pages
Page  Page N sizePage N content


Page(可以理解为cookie属性中domain,即相同domain的cookie保存在一个page里

字段大小端类型大小备注
Header大端 40x00000100
Number of cookies小端Unsigned Int4 
Cookie N offset小端Unsigned Int4Repeat for N cookies
Footer  40x00000000
Cookie N  Cookie N sizeCookie N content


Cookie

字段大小端类型大小备注
Size小端Unsigned Int4Size in bytes
Version小端Unsigned Int40 or 1
Flags小端Bit field4isSecure = 1, isHTTPOnly = 1 << 2, unknown1 = 1 << 3, unknown2 = 1 << 4
Has port小端Unsigned Int40 or 1
URL Offset小端Unsigned Int4Offset from the start of the cookie
Name Offset小端Unsigned Int4Offset from the start of the cookie
Path Offset小端Unsigned Int4Offset from the start of the cookie
Value Offset小端Unsigned Int4Offset from the start of the cookie
Comment Offset小端Unsigned Int4Offset from the start of the cookie, 0x00000000 if not present
Comment URL Offset小端Unsigned Int4Offset from the start of the cookie, 0x00000000 if not present
Expiration小端Double8Number of seconds since 00:00:00 UTC on 1 January 2001
Creation小端Double8Number of seconds since 00:00:00 UTC on 1 January 2001
Port小端Unsigned Int2Only present if the "Has port" field is 1
Comment小端String Null-terminated, optional
Comment URL小端String Null-terminated, optional
URL小端String Null-terminated
Name小端String Null-terminated
Path小端String Null-terminated
Value小端String Null-terminated


上面的Creation字段指的是,保存cookie这一时刻相对于2001/1/1 00:00:00的时间戳
Expiration指:如果cookie有max-age属性,Creation + max-age;如果cookie有expires属性,为expires转为时间戳

了解文件结构后,具体存取就很简单了,只要按照结构进行字节操作就可以了,不再赘述。