bz23 bz23 - 1 year ago 161 Question

inject cookie into Chrome using Windows Form App in VB

I need to inject a cookie into FireFox and Chrome via a Windows Form App written in VB. I have not been able to find any code or documentation online for this. The sample below is how I did it for IE, but I do not know where to begin with FireFox or Chrome. Any help would be appreciated.

Option Explicit On
Module createCookie
Private Declare Function InternetSetCookie Lib "wininet.dll" _
Alias "InternetSetCookieA" _
(ByVal lpszUrlName As String, _
ByVal lpszCookieName As String, _
ByVal lpszCookieData As String) As Boolean

Private Declare Function InternetGetCookie Lib "wininet.dll" _
Alias "InternetGetCookieA" _
(ByVal lpszUrlName As String, _
ByVal lpszCookieName As String, _
ByVal lpszCookieData As String, _
lpdwSize As Long) As Boolean

Public Function setCookie(cookieExpTimeGMT As String)
Dim bRet As Boolean

bRet = InternetSetCookie("", "CookieName", "Cookie Value" & "; expires = " & cookieExpTimeGMT)

Return bRet
End Function
End Module

Answer Source

I have found a solution to my question. Firefox and Chrome both use SQLite DB files to store the cookies. One can use System.Data.SQLite (google search for download) to write a cookie into the DB file. Expiration date is different from Firefox and Chrome. Chrome is number of Micro Seconds from 1/1/1601 and Firefox is number of seconds from 1/1/1970.

Imports System.Data.SQLite
Imports System.Environment

Module createCookieChrome
Public Function setChromeCookie(newValue As String, orgValue As String, cookieExpTimeGMT As String, hostName As String, cookieName As String, currentTime As String)
    Dim fRtn As Boolean = False
    Dim chromeCookiePath As String, strDb As String

    chromeCookiePath = GetFolderPath(SpecialFolder.LocalApplicationData) & "\Google\Chrome\User Data\Default\Cookies"
    If chromeCookiePath = Nothing Then
        fRtn = False
            strDb = "Data Source=" + chromeCookiePath + ";pooling=false"
            Dim SQLConn As SQLiteConnection = New SQLiteConnection(strDb)
            Dim SQLcmd As SQLiteCommand = SQLConn.CreateCommand()

            SQLcmd.CommandText = "INSERT INTO `cookies`(`creation_utc`,`host_key`,`name`,`value`,`path`,`expires_utc`,`secure`,`httponly`,`last_access_utc`,`has_expires`,`persistent`,`priority`,`encrypted_value`) " & "VALUES (" & currentTime & ",'." & hostName + "','" & cookieName & "','" & newValue & orgValue & "','/'," & cookieExpTimeGMT & ",1,1," & currentTime & ",1,1,1,'')"
            Dim rtV As Int64 = SQLcmd.ExecuteNonQuery()
            fRtn = True
        Catch e As Exception
            fRtn = False
        End Try
    End If

    Return fRtn

End Function
End Module
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download