Billps Tudios

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 25 September 2009

SQLite C Code to Read Cookies

Posted on 07:13 by Unknown

While most of the technology leaking from my brain is for a wide audience, today’s post is very technical in nature. I expected the information below to be available online but since it wasn’t I felt obligated to share my discovery with others.

One of the features of my WinPatrol program is to help manage and remove unwanted internet cookies. Starting with Firefox3 I wasn’t able to read cookies because of the change from a cookies.txt to a SQLite database format.  Luckily, the code to access to access SQLite files is public domain and freely accessible. I was hesitant to include the code to access SQLite files fearing it would increase the small size of WinPatrol.exe.  Since SQLite is completely public domain I was able to strip it down to include only those functions I needed.

The next step was figure out the format used by Firefox and find some sample code for accessing SQLite in C/C++. This wasn’t as easy as I had expected but by piecing together little bits of knowledge here and there I was eventually successful. Apparently, not many programmers are still using C/C++ but if you do here’s an example of how to read cookie information stored locally by both Firefox 3.x and Google’s Chrome browsers.

The following is pretty much the identical code used by the soon to be released, WinPatrol 17 to read cookie data used by Firefox3 and Chrome. My actual code includes a little more error checking and validation. If you’re using a different programming language you’ll still want to note the column or field names used in the SQLite table.

There are two based functions that you’ll need.

//  Firefox3 uses the table name of "moz_cookies"
//  Chrome uses table name of just "cookies"
//  This routine opens up the cookies table and passes a callback routine
//  send requested cookies, one row at a time.
// IN: the fullpath to the cookie file name
// IN: the defined type of if it’s a Chrome or Firefox3 type
int    GetSQLiteCookies( LPSTR szCookieFile, int iType )
{
    char       *zErrMsg = 0;
    int         rc;
    sqlite3    *dbCookie;   // defined in sqlite.h
    TCHAR      szSQLiteExec[MAX_SQLITEEXE]; 

    rc = sqlite3_open(szCookieFile, &dbCookie);
    if( rc )
    {
        sqlite3_close(dbCookie);
        return 0;
    }

    // Create SQL statement depending on the browser 
    if(iType == COOKIE_FIREFOX)
          StringCbCopy(szSQLite ,MAX_SQLEXEC, TEXT( "SELECT * FROM moz_cookies") );
    else    // then it much be Chrome
          StringCbCopy(szSQLite ,MAX_SQLEXEC, TEXT( "SELECT * FROM cookies") );

    rc = sqlite3_exec(dbCookie, szSqLiteExec, CookieCallback, 0, &zErrMsg); 
    if( rc!=SQLITE_OK ) 
    {
        sqlite3_free(zErrMsg); 
    }
    sqlite3_close(dbCookie);

    return 1;
}

The next routine is a “Callback” routine.  The result of calling the sqlite3_exe is above is that SQLite will call our Callback routine for every cookie or “row” in the database.  Each call will contain information about each cookie in a array.

// IN iCount = number of columns in this row
// IN szColValue = The data in the column
// IN szColName = Name of column
static int CookieCallback(void *NotUsed, int iCount, char **szColValue, char **szColName)
{
    int    i;
    char    *zErrMsg = 0; 
    COOKIEINFO   ciCookie;  // WinPatrol specific cookie structure
    SYSTEMTIME        myTime;
    memset(&ciCookie, 0, sizeof(COOKIEINFO));

// I’m going to go through each column and check to see
// see if the name matches one of the cookie fields and if
// it matches we grab the value and store it in our cookie
// structure. The actual code is a little more optimize but I’ve broken
// out each test to make it a little clearer.
// Some column names are different firefox vs chrome

  for(i=0; i<iCount; i++)
  {
      if(!lstrcmpi(TEXT("name"), szColName[i] ))
          StringCbCopy(ciCookie.szName,MAX_COOKIE_NAME, szColValue[i] );

      if(!lstrcmpi(TEXT("value"), szColName[i] ))
          StringCbCopy(ciCookie.szValue,MAX_COOKIE_VALUE, szColValue[i] );

      if(!lstrcmpi(TEXT("path"), szColName[i] ))
          StringCbCopy(ciCookie.szPath,MAX_PATH, szColValue[i] );

      if(!lstrcmpi(TEXT("host"), szColName[i] ))  // firefox3
          StringCbCopy(ciCookie.szDomain, MAX_COOKIE_NAME, szColValue[i] );
      if(!lstrcmpi(TEXT("host_key"), szColName[i] ))   // chrome
          StringCbCopy(ciCookie.szDomain, MAX_COOKIE_NAME, szColValue[i] );

      // firefox3 uses expiry, chrome uses expires_utc
      if ( (!lstrcmpi(TEXT("expiry"), szColName[i] )) || (!lstrcmpi(TEXT("expires_utc"), szColName[i] )))
      {
            long lUnixTime;
            lUnixTime = atol( szColValue[i] );
            UnixTimeToSystemTime((time_t)lUnixTime, &myTime);
            GetLocalTimeString(&myTime, ciCookie.szDate);
      }

      if(!lstrcmpi(TEXT("isSecure"), szColName[i] ))    //firefox 3 field name.
                ciCookie.bSecure = (BOOL)szColValue[i];
      if(!lstrcmpi(TEXT("secure"), szColName[i] ))        // chrome field name
                ciCookie.bSecure = (BOOL)szColValue[i];

  }

// Here's where I call another routine which adds my cookie structure
// to a list of cookies so I can display them in WinPatrol.
// your method of storing each cookie will vary.

  return 0;
}

 

I’m now a big fan so if you’re interested in using SQLite you can find everything you need to get started at http://www.sqlite.org/. I didn’t have time to get a book but for more C/C++ code I recommend The Definitive Guide to SQLite

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Support for Downloads without Surprises
    Last week I posted a message about the  packaging of ad supported programs along with popular software. It appears to be a common practice t...
  • Employee Manual to Prevent Cryptolocker and More
    A common way computers are infected or compromised has always been a simple yet well thought out deception. It can happen to anyone and the ...
  • What on Earth is “Cloud” Computing
    The new big buzz word in the computing world is “Cloud” computing. In the past I’ve been critical of the concept and even poked fun at Cloud...
  • Why Dancing with the Stars is #1
    Are you wondering why the heck I’m talking about TV Shows? Well, it’s still a little all about tech and some of my past experiences. In the ...
  • WinPatrol Cloud Edition Public Beta
    WinPatrol Adds the Newest Technology Available. You’ll now have access the knowledge of thousands of WinPatrol users shared in real-tim...
  • SQLite C Code to Read Cookies
    While most of the technology leaking from my brain is for a wide audience, today’s post is very technical in nature. I expected the informat...
  • Free #1 Tweak to Improve Windows Performance
    Every year billions of dollars are spent by folks just trying to improve the performance of their computers. Over the last couple years ther...
  • Forget the DVD & Save on your New Laptop
    I’ve written before about the coming death of the CD/DVD ’s for data storage.  While that time hasn’t come yet for desktop there is one real...
  • TWITTER ALERT
    Anyone who uses Twitter, DO NOT use the Twitter web interface until further notice. There is a code injection vulnerability being used tha...
  • Security Software Doesn’t Fix Human Nature
    If you’re interested in PC Security, you’ll want to check a look at research recently compiled by the folks at PC Pitstop. Even I was surpri...

Categories

  • 2007
  • 3G
  • AAPL
  • ABC
  • accelerometer
  • Achilles
  • Acrobat
  • Activex
  • adobe
  • Ads
  • advertising
  • Adware
  • Adwords
  • Airlines
  • Albany Medical Center
  • algorithm
  • Amazon
  • amber alert
  • AMUST
  • Animation
  • antimalware
  • Antivirus 2009
  • antivirus2008
  • AOL
  • Apple
  • applets
  • AQuantive
  • archive
  • Aruba
  • ASC
  • Ask.com
  • ATI
  • Audio
  • Autorun
  • AutoUpdate
  • autoupdates
  • AVG
  • Azure
  • backup
  • badware
  • Bakugan
  • Baseball
  • battery
  • Ben Edelman
  • Beta
  • BillP
  • Birthdayware
  • Bitlocker
  • Blackberry
  • BlackViper
  • bloatware
  • Blogger
  • Blogs
  • Blogspot
  • Blu-ray
  • Bluehoo
  • bluetooth
  • boinc
  • Bonjour
  • Brazil
  • break
  • Breakaway games
  • Brookman
  • Browser wars
  • C64
  • camera
  • Carpal Tunnel
  • CBS News
  • cell phone
  • CES
  • charity
  • Child Safety
  • chinese
  • Chris Cook
  • Christmas
  • Chrome
  • CIPAV
  • clampi
  • Cloud
  • CNet
  • codec
  • comodo
  • conficker
  • Control Panel
  • copy
  • coupon
  • craplets
  • crapware
  • Crawford
  • credit
  • credit card
  • credit cards
  • ctfmon
  • daylight savings time
  • Dell
  • demo
  • Discount
  • Disney
  • DNS
  • Dollar
  • Domain
  • Donna
  • Doubleclick
  • Downadup
  • Dreamscene
  • droid
  • DVD
  • Dvorak
  • Easter egg
  • eclipse
  • Ed Bott
  • Edelman
  • egreeting
  • Email
  • Environment
  • Epilepsy
  • EU
  • eWeek
  • explorer
  • facebook
  • false positive
  • false-positive
  • FBI
  • file types
  • finnish
  • FiOS
  • Firefox
  • fireworks
  • fix
  • flash
  • Flashpix
  • Fort Drum
  • foxit
  • fraud
  • FTC
  • games
  • garmin
  • Gateway
  • GE
  • George Bush
  • Germany
  • global
  • Godmode
  • Google
  • Google Research
  • GotoMyPC
  • gps
  • green
  • Groceries
  • Habitat
  • Hacks
  • hallmark
  • Halo
  • hard drive
  • Harry McCracken
  • Harry Potter
  • Harvard
  • HD-DVD
  • help
  • hidden files
  • Hijack
  • History
  • Hiton
  • homeland security
  • Honor Flight
  • hosts
  • Hubble
  • IAC
  • ICANN
  • IE
  • IE6
  • IE7
  • IE8
  • installers
  • interface
  • Internet
  • IPAddress
  • iPhone
  • iPod
  • Iraq
  • iTouch
  • iTunes
  • java
  • Kaspersky
  • Kazaa
  • kenmore
  • key logger
  • keygen
  • Keylogger
  • Kosovo
  • LA
  • lady chalupa
  • langa
  • Laptop
  • lawsuit
  • Legoland
  • Levar Burton
  • Linksys
  • Little League
  • Live Writer
  • Live.com
  • localize
  • Logo
  • London
  • LOP
  • lottery
  • Lucasfilm
  • Macintosh
  • Malware
  • Marie Domingo
  • Mary
  • McCracken
  • Media
  • Memorial Day
  • mgrs.exe
  • Micosoft
  • Microsoft
  • Microsoft Surface
  • MiFi
  • mit
  • moon
  • Mossberg
  • Mothers Day
  • MPack
  • MSFT
  • msn
  • MTV
  • Multicore
  • Music
  • MVP
  • MVP09
  • nasa
  • NBC
  • Nero
  • Netbook
  • Network
  • network solution
  • New York
  • newsletter
  • Nintendo
  • Nintendo Wii
  • NNEDV
  • Norton
  • NYAG
  • OAuth
  • obama
  • Office
  • OLPC
  • Olympics
  • OpenDNS
  • oprah
  • optimize
  • optout
  • Paperghost
  • passwords
  • Patch
  • Patriot Flight
  • PC Guy
  • pc pitstop
  • PC World
  • pcmag
  • PCWorld
  • PDC
  • PDF
  • pedipaws
  • performance
  • phishing
  • photos
  • Photoshop
  • Pinnacle
  • Piracy
  • Pirillo
  • pogue
  • Porn
  • pornware
  • postcard
  • prediction
  • prefetch
  • Preview
  • Price
  • privacy
  • Prodigy
  • Programming
  • PSP
  • Public Relations
  • Pytlovany
  • Q-Link
  • Quicktime
  • quotes
  • radio
  • realnetworks
  • realplayer
  • RegCleaner
  • RegCure
  • regedit
  • Registry
  • registry cleaner
  • Release
  • remove
  • Research
  • return policy
  • review
  • RIAA
  • Rivera
  • RMS
  • Road Runner
  • rogue
  • router
  • RTM
  • Rumor
  • safari
  • safety
  • sale
  • Sales
  • Santa Monica
  • scam
  • Schenectady
  • Scoble
  • Scott Dunn
  • Scotty
  • sd
  • Search
  • Sears
  • Security
  • Services
  • seti
  • ShellExecute
  • Shirt
  • SimCity
  • site advisor
  • slingbox
  • snopes
  • social engineering
  • social network
  • solid state disk
  • Sounds
  • Sp3
  • space station
  • SPAM
  • spamhaus
  • Special
  • speedtest
  • Spyware
  • SSD
  • Startup
  • Stats
  • Steve Bass
  • stopbadware
  • storm
  • STS-125
  • Sugar
  • Sunbelt
  • support
  • Symantec
  • tagged
  • Task Catcher
  • Task Scheduler
  • taskbar
  • Tax
  • Techorati
  • techwatch
  • teens
  • temp
  • Thinkpad
  • Thurrott
  • tinyurl
  • Tips
  • TiVo
  • TLD
  • Today Show
  • Toolbar
  • toolbars
  • top ten
  • topten
  • toys
  • Translator
  • transunion
  • Tree
  • Trend Micro
  • tricks
  • trillian
  • Trojan
  • tweaks
  • twitter
  • UAC
  • UI
  • Ultimate
  • Unbox
  • Unboxed
  • update
  • Updates
  • upgrade
  • url
  • USB
  • Utility
  • Valentine
  • Verizon
  • versions
  • Veteran
  • Video Games
  • Vista
  • Vulnerability
  • wall-e
  • war
  • Washington
  • web2.0
  • Webslice
  • WGA
  • Widget
  • WiFi
  • Wii
  • WiiItis
  • wiimote
  • Win7
  • Windows 7
  • Windows Secrets
  • Windows Update
  • Windows7
  • WinPartrol
  • WinPatrol
  • winpatrolflash
  • WinPatrolToGo
  • Winter
  • Wireless
  • Wristband
  • WSJ
  • WWII
  • x64
  • Xbox
  • XO
  • XO Laptop
  • XOActivity
  • Xobni
  • xolaptop
  • XP
  • XP SP3
  • xp3
  • Yahoo
  • Zero Day
  • Zone Alarm
  • Zwinky

Blog Archive

  • ►  2013 (31)
    • ►  November (2)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
    • ►  July (3)
    • ►  June (5)
    • ►  May (2)
    • ►  April (3)
    • ►  March (2)
    • ►  February (5)
    • ►  January (5)
  • ►  2012 (30)
    • ►  December (3)
    • ►  November (3)
    • ►  October (2)
    • ►  September (2)
    • ►  August (2)
    • ►  July (3)
    • ►  June (2)
    • ►  May (1)
    • ►  April (4)
    • ►  March (4)
    • ►  February (2)
    • ►  January (2)
  • ►  2011 (28)
    • ►  December (4)
    • ►  November (2)
    • ►  October (4)
    • ►  September (2)
    • ►  August (2)
    • ►  July (2)
    • ►  June (2)
    • ►  May (2)
    • ►  April (2)
    • ►  March (2)
    • ►  February (3)
    • ►  January (1)
  • ►  2010 (44)
    • ►  December (2)
    • ►  November (3)
    • ►  October (3)
    • ►  September (4)
    • ►  August (3)
    • ►  July (3)
    • ►  June (3)
    • ►  May (4)
    • ►  April (4)
    • ►  March (3)
    • ►  February (3)
    • ►  January (9)
  • ▼  2009 (90)
    • ►  December (6)
    • ►  November (8)
    • ►  October (6)
    • ▼  September (4)
      • Free #1 Tweak to Improve Windows Performance
      • SQLite C Code to Read Cookies
      • Four Secret Reasons Why Win7 is Ten Times Better
      • WinPatrol PLUS “Back to School” Special
    • ►  August (4)
    • ►  July (12)
    • ►  June (6)
    • ►  May (11)
    • ►  April (7)
    • ►  March (9)
    • ►  February (9)
    • ►  January (8)
  • ►  2008 (122)
    • ►  December (9)
    • ►  November (11)
    • ►  October (14)
    • ►  September (6)
    • ►  August (9)
    • ►  July (9)
    • ►  June (10)
    • ►  May (13)
    • ►  April (8)
    • ►  March (10)
    • ►  February (10)
    • ►  January (13)
  • ►  2007 (155)
    • ►  December (15)
    • ►  November (14)
    • ►  October (12)
    • ►  September (14)
    • ►  August (12)
    • ►  July (13)
    • ►  June (11)
    • ►  May (19)
    • ►  April (17)
    • ►  March (21)
    • ►  February (7)
Powered by Blogger.

About Me

Unknown
View my complete profile