从 Internet Explorer 检索所有 cookie [英] Retrieve ALL cookies from Internet Explorer

查看:21
本文介绍了从 Internet Explorer 检索所有 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索与我打开的特定页面相关联的所有 cookie(我已通过身份验证).有几个与网页相关联的 cookie,我需要检索每个 cookie 以便稍后进行 POST.

我尝试了几种方法,但都没有给我完整的列表.到目前为止,我已经使用 VBA 编写了代码,但我也可以使用 .NET 编写代码.

第一次尝试,在获得指向 IE 的指针后.

arraycookie = Split(ie.document.Cookie, ";")对于 i = LBound(arraycookie) 到 UBound(arraycookie)Debug.Print arraycookie(i)接下来我

这给了我一些饼干,但不是全部.我可以在开发人员工具 (F12) 中查看 cookie,并且我确认没有 cookie 标记了 HTTP only Flag.见下图.

我还尝试了 InternetGetCookie windows API.它返回一个 cookie,但只有一个,而且无论名称是什么都是相同的(例如下面的 FedAuth).

私有子GetCookieAttempt()将 sCookieVal 调暗为字符串 * 256Dim bRet 为布尔值bRet = InternetGetCookie("https://mywebsiteaddresshere.com", _联邦认证",sCookieVal,255)如果 bRet = False 那么MsgBox "失败"别的MsgBox sCookieVal万一结束子

这只是一个猜测(知道我怎么知道吗?),但从我读到的内容来看,cookie 可能是受保护的.我查看了 IEGetProtectedModeCookie API,但我无法让它返回 cookie 信息.

不知道为什么我可以通过开发者工具看到这一点,但是当我尝试公开信息时,它并没有返回所有内容.

任何帮助将不胜感激:)

谢谢!

<小时>

编辑

所以在深入研究这个问题之后,我想我会用不同的浏览器尝试相同的网页,然后再次查看 cookie 详细信息.

使用 Firebug,我试图检索的 cookie 现在显示 HTTPOnly 标志.据我了解,我可以将 InternetGetCookieEx 与指定的 INTERNET_COOKIE_HTTPONLY 标志一起使用.但是我无法让它返回任何东西.

有人有我可以遵循的工作样本吗?

解决方案

尝试从 shell:cookies 文件夹中检索 IE cookie.以下面的代码为例:

选项显式子 GetIECookies()将 sCookiesPath 调暗为字符串Dim oCookies 作为对象Dim oFSO 作为对象将文件夹变暗为对象变暗文件Dim sContent 作为字符串Dim a() As String昏暗的我昏暗物品Dim aCookies()' 读取 IE cookie 文件sCookiesPath = CreateObject("shell.application").Namespace("shell:Cookies").self.PathSet oCookies = CreateObject("Scripting.Dictionary")Set oFSO = CreateObject("Scripting.FileSystemObject")设置 oFolder = oFSO.GetFolder(sCookiesPath)对于 oFolder.Files 中的每个 oFile如果 LCase(oFSO.GetExtensionName(oFile.Name)) = "txt" 然后With oFile.OpenAsTextStream(1, 0) ' 只读,asciisContent = .ReadAll.关闭结束于sContent = Replace(sContent, vbCr, "")' 在文件中拆分 cookiea = Split(sContent, vbLf & "*" & vbLf)对于 i = 0 到 UBound(a) - 1oCookies.Add oCookies.Count, a(i)下一个万一下一个' 解析数据,重新打包成二维数组aItems = oCookies.Items()如果 UBound(aItems) = -1 那么MsgBox "未找到 cookie"别的ReDim aCookies(1 to UBound(aItems) + 1, 1 to 6)对于 i = 1 到 UBound(aItems) + 1a = Split(aItems(i - 1), vbLf)aCookies(i, 1) = a(0)aCookies(i, 2) = a(1)aCookies(i, 3) = a(2)aCookies(i, 4) = GetInetCookieFlags(a(3))aCookies(i, 5) = ConvDT(a(4), a(5))aCookies(i, 6) = ConvDT(a(6), a(7))下一个' 输出使用 ThisWorkbook.Sheets(1).Cells.Delete.Range("A1:F1") = Array("Name", "Value", "Host/Path", "Flags", "Expiration", "Created")输出 .Range("A2"), aCookies结束于万一结束子函数 ConvDT(sLowNTFmt As String, sHighNTFmt As String) 作为日期将 dNTFmt 调暗为两倍将 dUnixFmt 调暗为双倍' FILETIME 格式是自 1601 年 1 月 1 日 (UTC) 00:00 以来的 100 纳秒滴答数.dNTFmt = sHighNTFmt * 4294967296# + sLowNTFmt' Unix 时间格式是自 1970 年 1 月 1 日 00:00 以来的秒数dUnixFmt = 0.0000001 * dNTFmt - 11644473600#' VB 时间格式是自 1900 年 1 月 1 日 00:00 以来的天数ConvDT = CDate(dUnixFmt/86400 + 25569)结束函数函数 GetInetCookieFlags(sFlags As String) As StringDim lFlags as long暗淡的旗帜' 重置第 32 位以避免溢出如果 sFlags >= 2147483648# 那么 lFlags = CLng(sFlags - 2147483648#) 否则 lFlags = CLng(sFlags)' 将标志位转换为字符串表示使用 CreateObject("Scripting.Dictionary")对于数组中的每个 aFlag( _数组(&H1,是安全的"),_数组(&H2,是会话"),_数组(&H10,第三方"),_数组(&H20,需要提示"),_数组(&H40,评估 P3P"),_阵列(&H80,应用P3P"),_数组(&H100,P3P 启用"),_数组(&H200,受限"),_数组(&H400, "IE6"), _阵列(&H800,是遗产"),_数组(&H1000,非脚本"),_数组(&H2000, "HTTPONLY"), _数组(&H4000,仅限主机"),_数组(&H8000,仅适用于主机"),_阵列(&H20000,限制区"),_数组(&H20000000,所有饼干"),_数组(&H40000000,没有回调"),_阵列(&H80000000,ECTX 3RDPARTY")_)如果 lFlags 和 aFlag(0) 那么 .Add .Count, aFlag(1)下一个GetInetCookieFlags = Join(.Items(), vbCrLf)结束于结束函数子输出(oDstRng 作为范围,aCells 作为变体)与 oDstrng.Parent.Select随着 .Resize( _UBound(aCells, 1) - LBound(aCells, 1) + 1, _UBound(aCells, 2) - LBound(aCells, 2) + 1 _).NumberFormat = "@".Value = aCells.Columns.AutoFit结束于结束于结束子

删除所有 cookie 并导航到

关于代码的一些说明.

它仅解析 Cookies 文件夹中的文件,而不解析 CookiesLow 中的文件,后者用于在低权限下运行的应用程序.它仅检索存储在文件夹中的持久性 cookie,而不检索会话 cookie,后者存储在内存中并且只能由创建它们的进程访问.时间采用 UTC.

文件中cookie的结构如下:

Cookie 名称饼干值设置 cookie 的 Web 服务器的主机/路径旗帜到期时间(低)到期时间(高)创建时间(低)创建时间(高)记录分隔符 (*)

标志定义为 wininet.dll 头文件:

#define INTERNET_COOKIE_IS_SECURE 0x00000001#define INTERNET_COOKIE_IS_SESSION 0x00000002#define INTERNET_COOKIE_THIRD_PARTY 0x00000010#define INTERNET_COOKIE_PROMPT_REQUIRED 0x00000020#define INTERNET_COOKIE_EVALUATE_P3P 0x00000040#define INTERNET_COOKIE_APPLY_P3P 0x00000080#define INTERNET_COOKIE_P3P_ENABLED 0x00000100#define INTERNET_COOKIE_IS_RESTRICTED 0x00000200#define INTERNET_COOKIE_IE6 0x00000400#define INTERNET_COOKIE_IS_LEGACY 0x00000800#define INTERNET_COOKIE_NON_SCRIPT 0x00001000#define INTERNET_COOKIE_HTTPONLY 0x00002000#define INTERNET_COOKIE_HOST_ONLY 0x00004000#define INTERNET_COOKIE_APPLY_HOST_ONLY 0x00008000#define INTERNET_COOKIE_RESTRICTED_ZONE 0x00020000#define INTERNET_COOKIE_ALL_COOKIES 0x20000000#define INTERNET_COOKIE_NO_CALLBACK 0x40000000#define INTERNET_COOKIE_ECTX_3RDPARTY 0x80000000

I'm trying to retrieve all cookies that are associated with a specific page I have open (I've already authenticated). There are several cookies associated with the webpage, I need to retrieve each cookie in order to do a POST later on.

I've tried several approaches, but none have given me the full list. I've written the code thus far in VBA, but I'm fine with it being in .NET as well.

First attempt, after getting the pointer to IE.

arraycookie = Split(ie.document.Cookie, ";")

For i = LBound(arraycookie) To UBound(arraycookie)
    Debug.Print arraycookie(i)
Next i

This gives me some of the cookies, but not all of them. I can review the cookies in the developer tools (F12) and I confirmed no cookies have the HTTP only Flag marked. See the picture below.

I also tried the InternetGetCookie windows API. It's returning a cookie, but only one, and it's the same one no matter what the name is (e.g. FedAuth below).

Private Sub GetCookieAttempt()
Dim sCookieVal As String * 256
Dim bRet As Boolean
bRet = InternetGetCookie("https://mywebsiteaddresshere.com", _
    "FedAuth", sCookieVal, 255)
    If bRet = False Then
        MsgBox "Failed"
    Else
        MsgBox sCookieVal
    End If
End Sub

This is just a guess (any idea how I could find out?), but from what I read it may be that the cookie is protected. I took a look at the IEGetProtectedModeCookie API, but I couldn't get it to return cookie information.

Not sure why I can see this through developer tools, but when I try and expose the information, it doesn't return everything.

Any help would be greatly appreciated :)

Thanks!


Edit

So after digging around this issue I thought I'd try the same webpage with a different browser and review the cookie details again.

Using Firebug the cookies I was trying to retrieve is now showing an HTTPOnly flag. From what I understand I can use InternetGetCookieEx with the INTERNET_COOKIE_HTTPONLY flag specified. However I can't get it to return anything.

Does anyone have a working sample I can follow?

解决方案

Try to retrieve IE cookies from shell:cookies folder. There is the below code as an example:

Option Explicit

Sub GetIECookies()

    Dim sCookiesPath As String
    Dim oCookies As Object
    Dim oFSO As Object
    Dim oFolder As Object
    Dim oFile
    Dim sContent As String
    Dim a() As String
    Dim i As Long
    Dim aItems
    Dim aCookies()

    ' read IE cookie files
    sCookiesPath = CreateObject("shell.application").Namespace("shell:Cookies").self.Path
    Set oCookies = CreateObject("Scripting.Dictionary")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(sCookiesPath)
    For Each oFile In oFolder.Files
        If LCase(oFSO.GetExtensionName(oFile.Name)) = "txt" Then
            With oFile.OpenAsTextStream(1, 0) ' read-only, ascii
                sContent = .ReadAll
                .Close
            End With
            sContent = Replace(sContent, vbCr, "")
            ' split cookies within file
            a = Split(sContent, vbLf & "*" & vbLf)
            For i = 0 To UBound(a) - 1
                oCookies.Add oCookies.Count, a(i)
            Next
        End If
    Next
    ' parse data, repack to 2d array
    aItems = oCookies.Items()
    If UBound(aItems) = -1 Then
        MsgBox "No cookies found"
    Else
        ReDim aCookies(1 To UBound(aItems) + 1, 1 To 6)
        For i = 1 To UBound(aItems) + 1
            a = Split(aItems(i - 1), vbLf)
            aCookies(i, 1) = a(0)
            aCookies(i, 2) = a(1)
            aCookies(i, 3) = a(2)
            aCookies(i, 4) = GetInetCookieFlags(a(3))
            aCookies(i, 5) = ConvDT(a(4), a(5))
            aCookies(i, 6) = ConvDT(a(6), a(7))
        Next
        ' output
        With ThisWorkbook.Sheets(1)
            .Cells.Delete
            .Range("A1:F1") = Array("Name", "Value", "Host/Path", "Flags", "Expiration", "Created")
            Output .Range("A2"), aCookies
        End With
    End If

End Sub

Function ConvDT(sLowNTFmt As String, sHighNTFmt As String) As Date

    Dim dNTFmt As Double
    Dim dUnixFmt As Double

    ' FILETIME format is the number of 100 nanosecond ticks since 00:00 1 Jan, 1601 (UTC).
    dNTFmt = sHighNTFmt * 4294967296# + sLowNTFmt
    ' Unix time format is the number of seconds since 00:00 1 Jan 1970
    dUnixFmt = 0.0000001 * dNTFmt - 11644473600#
    ' VB time format is the number of days since 00:00 1 Jan 1900
    ConvDT = CDate(dUnixFmt / 86400 + 25569)

End Function

Function GetInetCookieFlags(sFlags As String) As String

    Dim lFlags As Long
    Dim aFlag

    ' reset bit 32 to avoid overflow
    If sFlags >= 2147483648# Then lFlags = CLng(sFlags - 2147483648#) Else lFlags = CLng(sFlags)
    ' convert flags bits to string representation
    With CreateObject("Scripting.Dictionary")
        For Each aFlag In Array( _
            Array(&H1, "IS SECURE"), _
            Array(&H2, "IS SESSION"), _
            Array(&H10, "THIRD PARTY"), _
            Array(&H20, "PROMPT REQUIRED"), _
            Array(&H40, "EVALUATE P3P"), _
            Array(&H80, "APPLY P3P"), _
            Array(&H100, "P3P ENABLED"), _
            Array(&H200, "IS RESTRICTED"), _
            Array(&H400, "IE6"), _
            Array(&H800, "IS LEGACY"), _
            Array(&H1000, "NON SCRIPT"), _
            Array(&H2000, "HTTPONLY"), _
            Array(&H4000, "HOST ONLY"), _
            Array(&H8000, "APPLY HOST ONLY"), _
            Array(&H20000, "RESTRICTED ZONE"), _
            Array(&H20000000, "ALL COOKIES"), _
            Array(&H40000000, "NO CALLBACK"), _
            Array(&H80000000, "ECTX 3RDPARTY") _
        )
            If lFlags And aFlag(0) Then .Add .Count, aFlag(1)
        Next
        GetInetCookieFlags = Join(.Items(), vbCrLf)
    End With

End Function

Sub Output(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize( _
            UBound(aCells, 1) - LBound(aCells, 1) + 1, _
            UBound(aCells, 2) - LBound(aCells, 2) + 1 _
        )
            .NumberFormat = "@"
            .Value = aCells
            .Columns.AutoFit
        End With
    End With

End Sub

The output for me after deleting all cookies and navigating to https://stackoverflow.com/ is as follows:

Some notes regarding the code.

It parses files in Cookies folder only, but not in CookiesLow which is for applications running under low privileges. It retrieves persistent cookies stored in the folder only, but not session cookies, which are stored in memory and can be accessed only by the process that created them. Time is in UTC.

The structure of a cookie within a file is as follows:

Cookie name
Cookie value
Host/path for the web server setting the cookie
Flags
Exirpation time (low)
Expiration time (high)
Creation time (low)
Creation time (high)
Record delimiter (*)

Flags are defined as per wininet.dll headers:

#define INTERNET_COOKIE_IS_SECURE                0x00000001
#define INTERNET_COOKIE_IS_SESSION               0x00000002
#define INTERNET_COOKIE_THIRD_PARTY              0x00000010
#define INTERNET_COOKIE_PROMPT_REQUIRED          0x00000020
#define INTERNET_COOKIE_EVALUATE_P3P             0x00000040
#define INTERNET_COOKIE_APPLY_P3P                0x00000080
#define INTERNET_COOKIE_P3P_ENABLED              0x00000100
#define INTERNET_COOKIE_IS_RESTRICTED            0x00000200
#define INTERNET_COOKIE_IE6                      0x00000400
#define INTERNET_COOKIE_IS_LEGACY                0x00000800
#define INTERNET_COOKIE_NON_SCRIPT               0x00001000
#define INTERNET_COOKIE_HTTPONLY                 0x00002000
#define INTERNET_COOKIE_HOST_ONLY                0x00004000
#define INTERNET_COOKIE_APPLY_HOST_ONLY          0x00008000
#define INTERNET_COOKIE_RESTRICTED_ZONE          0x00020000
#define INTERNET_COOKIE_ALL_COOKIES              0x20000000
#define INTERNET_COOKIE_NO_CALLBACK              0x40000000
#define INTERNET_COOKIE_ECTX_3RDPARTY            0x80000000

这篇关于从 Internet Explorer 检索所有 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆