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-8 | 4 | "cook", no terminator |
Number of pages | 大端 | Unsigned Int | 4 | |
Page N size | 大端 | Unsigned Int | 4 | Repeat for N pages |
Page | Page N size | Page N content |
Page(可以理解为cookie属性中domain,即相同domain的cookie保存在一个page里)
字段 | 大小端 | 类型 | 大小 | 备注 |
Header | 大端 | 4 | 0x00000100 | |
Number of cookies | 小端 | Unsigned Int | 4 | |
Cookie N offset | 小端 | Unsigned Int | 4 | Repeat for N cookies |
Footer | 4 | 0x00000000 | ||
Cookie N | Cookie N size | Cookie N content |
Cookie
字段 | 大小端 | 类型 | 大小 | 备注 |
Size | 小端 | Unsigned Int | 4 | Size in bytes |
Version | 小端 | Unsigned Int | 4 | 0 or 1 |
Flags | 小端 | Bit field | 4 | isSecure = 1, isHTTPOnly = 1 << 2, unknown1 = 1 << 3, unknown2 = 1 << 4 |
Has port | 小端 | Unsigned Int | 4 | 0 or 1 |
URL Offset | 小端 | Unsigned Int | 4 | Offset from the start of the cookie |
Name Offset | 小端 | Unsigned Int | 4 | Offset from the start of the cookie |
Path Offset | 小端 | Unsigned Int | 4 | Offset from the start of the cookie |
Value Offset | 小端 | Unsigned Int | 4 | Offset from the start of the cookie |
Comment Offset | 小端 | Unsigned Int | 4 | Offset from the start of the cookie, 0x00000000 if not present |
Comment URL Offset | 小端 | Unsigned Int | 4 | Offset from the start of the cookie, 0x00000000 if not present |
Expiration | 小端 | Double | 8 | Number of seconds since 00:00:00 UTC on 1 January 2001 |
Creation | 小端 | Double | 8 | Number of seconds since 00:00:00 UTC on 1 January 2001 |
Port | 小端 | Unsigned Int | 2 | Only 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转为时间戳
了解文件结构后,具体存取就很简单了,只要按照结构进行字节操作就可以了,不再赘述。