Advertisement
Guest User

WinINETCache

a guest
Jan 28th, 2011
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.95 KB | None | 0 0
  1. public class WinINETCache
  2. {
  3.     // Fields
  4.     private const int CACHEGROUP_FLAG_FLUSHURL_ONDELETE = 2;
  5.     private const int CACHEGROUP_SEARCH_ALL = 0;
  6.     private const int ERROR_FILE_NOT_FOUND = 2;
  7.     private const int ERROR_INSUFFICENT_BUFFER = 0x7a;
  8.     private const int ERROR_NO_MORE_ITEMS = 0x103;
  9.  
  10.     // Methods
  11.     public static void ClearCacheItems(bool bClearFiles, bool bClearCookies)
  12.     {
  13.         if (!bClearCookies && !bClearFiles)
  14.         {
  15.             throw new ArgumentException("You must call ClearCacheItems with at least one target");
  16.         }
  17.         if (Environment.OSVersion.Version.Major > 5)
  18.         {
  19.             VistaClearTracks(bClearFiles, bClearCookies);
  20.         }
  21.         else
  22.         {
  23.             if (bClearCookies)
  24.             {
  25.                 ClearCookiesForHost("*");
  26.             }
  27.             if (bClearFiles)
  28.             {
  29.                 long lpGroupId = 0L;
  30.                 int lpdwFirstCacheEntryInfoBufferSize = 0;
  31.                 int cb = 0;
  32.                 IntPtr zero = IntPtr.Zero;
  33.                 IntPtr hFind = IntPtr.Zero;
  34.                 bool flag = false;
  35.                 hFind = FindFirstUrlCacheGroup(0, 0, IntPtr.Zero, 0, ref lpGroupId, IntPtr.Zero);
  36.                 int num4 = Marshal.GetLastWin32Error();
  37.                 if (((hFind != IntPtr.Zero) && (0x103 != num4)) && (2 != num4))
  38.                 {
  39.                     do
  40.                     {
  41.                         flag = DeleteUrlCacheGroup(lpGroupId, 2, IntPtr.Zero);
  42.                         num4 = Marshal.GetLastWin32Error();
  43.                         if (!flag && (2 == num4))
  44.                         {
  45.                             flag = FindNextUrlCacheGroup(hFind, ref lpGroupId, IntPtr.Zero);
  46.                             num4 = Marshal.GetLastWin32Error();
  47.                         }
  48.                     }
  49.                     while (flag || ((0x103 != num4) && (2 != num4)));
  50.                 }
  51.                 hFind = FindFirstUrlCacheEntryEx(null, 0, WININETCACHEENTRYTYPE.ALL, 0L, IntPtr.Zero, ref lpdwFirstCacheEntryInfoBufferSize, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  52.                 num4 = Marshal.GetLastWin32Error();
  53.                 if ((IntPtr.Zero != hFind) || (0x103 != num4))
  54.                 {
  55.                     cb = lpdwFirstCacheEntryInfoBufferSize;
  56.                     zero = Marshal.AllocHGlobal(cb);
  57.                     hFind = FindFirstUrlCacheEntryEx(null, 0, WININETCACHEENTRYTYPE.ALL, 0L, zero, ref lpdwFirstCacheEntryInfoBufferSize, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  58.                     num4 = Marshal.GetLastWin32Error();
  59.                     do
  60.                     {
  61.                         INTERNET_CACHE_ENTRY_INFOA internet_cache_entry_infoa = (INTERNET_CACHE_ENTRY_INFOA) Marshal.PtrToStructure(zero, typeof(INTERNET_CACHE_ENTRY_INFOA));
  62.                         lpdwFirstCacheEntryInfoBufferSize = cb;
  63.                         if (WININETCACHEENTRYTYPE.COOKIE_CACHE_ENTRY != (internet_cache_entry_infoa.CacheEntryType & WININETCACHEENTRYTYPE.COOKIE_CACHE_ENTRY))
  64.                         {
  65.                             flag = DeleteUrlCacheEntry(internet_cache_entry_infoa.lpszSourceUrlName);
  66.                         }
  67.                         flag = FindNextUrlCacheEntryEx(hFind, zero, ref lpdwFirstCacheEntryInfoBufferSize, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  68.                         num4 = Marshal.GetLastWin32Error();
  69.                         if (!flag && (0x103 == num4))
  70.                         {
  71.                             break;
  72.                         }
  73.                         if (!flag && (lpdwFirstCacheEntryInfoBufferSize > cb))
  74.                         {
  75.                             cb = lpdwFirstCacheEntryInfoBufferSize;
  76.                             zero = Marshal.ReAllocHGlobal(zero, (IntPtr) cb);
  77.                             flag = FindNextUrlCacheEntryEx(hFind, zero, ref lpdwFirstCacheEntryInfoBufferSize, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
  78.                         }
  79.                     }
  80.                     while (flag);
  81.                     Marshal.FreeHGlobal(zero);
  82.                 }
  83.             }
  84.         }
  85.     }
  86.  
  87.     public static void ClearCookies()
  88.     {
  89.         ClearCacheItems(false, true);
  90.     }
  91.  
  92.     [CodeDescription("Delete all permanent WinINET cookies for sHost; won't clear memory-only session cookies. Supports hostnames with an optional leading wildcard, e.g. *example.com. NOTE: Will not work on VistaIE Protected Mode cookies.")]
  93.     public static void ClearCookiesForHost(string sHost)
  94.     {
  95.         string str;
  96.         INTERNET_CACHE_ENTRY_INFOA internet_cache_entry_infoa;
  97.         bool flag;
  98.         sHost = sHost.Trim();
  99.         if (sHost.Length < 1)
  100.         {
  101.             return;
  102.         }
  103.         if (sHost == "*")
  104.         {
  105.             str = string.Empty;
  106.             if (Environment.OSVersion.Version.Major > 5)
  107.             {
  108.                 VistaClearTracks(false, true);
  109.                 return;
  110.             }
  111.         }
  112.         else
  113.         {
  114.             str = sHost.StartsWith("*") ? sHost.Substring(1).ToLower() : ("@" + sHost.ToLower());
  115.         }
  116.         int lpdwFirstCacheEntryInfoBufferSize = 0;
  117.         int cb = 0;
  118.         IntPtr zero = IntPtr.Zero;
  119.         IntPtr hFind = IntPtr.Zero;
  120.         if ((FindFirstUrlCacheEntry("cookie:", IntPtr.Zero, ref lpdwFirstCacheEntryInfoBufferSize) == IntPtr.Zero) && (0x103 == Marshal.GetLastWin32Error()))
  121.         {
  122.             return;
  123.         }
  124.         cb = lpdwFirstCacheEntryInfoBufferSize;
  125.         zero = Marshal.AllocHGlobal(cb);
  126.         hFind = FindFirstUrlCacheEntry("cookie:", zero, ref lpdwFirstCacheEntryInfoBufferSize);
  127.     Label_00C2:
  128.         internet_cache_entry_infoa = (INTERNET_CACHE_ENTRY_INFOA) Marshal.PtrToStructure(zero, typeof(INTERNET_CACHE_ENTRY_INFOA));
  129.         lpdwFirstCacheEntryInfoBufferSize = cb;
  130.         if (WININETCACHEENTRYTYPE.COOKIE_CACHE_ENTRY == (internet_cache_entry_infoa.CacheEntryType & WININETCACHEENTRYTYPE.COOKIE_CACHE_ENTRY))
  131.         {
  132.             bool flag2;
  133.             if (str.Length == 0)
  134.             {
  135.                 flag2 = true;
  136.             }
  137.             else
  138.             {
  139.                 string str2 = Marshal.PtrToStringAnsi(internet_cache_entry_infoa.lpszSourceUrlName);
  140.                 int index = str2.IndexOf('/');
  141.                 if (index > 0)
  142.                 {
  143.                     str2 = str2.Remove(index);
  144.                 }
  145.                 flag2 = str2.ToLower().EndsWith(str);
  146.             }
  147.             if (flag2)
  148.             {
  149.                 flag = DeleteUrlCacheEntry(internet_cache_entry_infoa.lpszSourceUrlName);
  150.             }
  151.         }
  152.     Label_014A:
  153.         flag = FindNextUrlCacheEntry(hFind, zero, ref lpdwFirstCacheEntryInfoBufferSize);
  154.         if (flag || (0x103 != Marshal.GetLastWin32Error()))
  155.         {
  156.             if (flag || (lpdwFirstCacheEntryInfoBufferSize <= cb))
  157.             {
  158.                 goto Label_00C2;
  159.             }
  160.             cb = lpdwFirstCacheEntryInfoBufferSize;
  161.             zero = Marshal.ReAllocHGlobal(zero, (IntPtr) cb);
  162.             goto Label_014A;
  163.         }
  164.         Marshal.FreeHGlobal(zero);
  165.     }
  166.  
  167.     public static void ClearFiles()
  168.     {
  169.         ClearCacheItems(true, false);
  170.     }
  171.  
  172.     [return: MarshalAs(UnmanagedType.Bool)]
  173.     [DllImport("wininet.dll", EntryPoint="DeleteUrlCacheEntryA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
  174.     private static extern bool DeleteUrlCacheEntry(IntPtr lpszUrlName);
  175.     [return: MarshalAs(UnmanagedType.Bool)]
  176.     [DllImport("wininet.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
  177.     private static extern bool DeleteUrlCacheGroup(long GroupId, int dwFlags, IntPtr lpReserved);
  178.     [DllImport("wininet.dll", EntryPoint="FindFirstUrlCacheEntryA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
  179.     private static extern IntPtr FindFirstUrlCacheEntry([MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern, IntPtr lpFirstCacheEntryInfo, ref int lpdwFirstCacheEntryInfoBufferSize);
  180.     [DllImport("wininet.dll", EntryPoint="FindFirstUrlCacheEntryExA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
  181.     private static extern IntPtr FindFirstUrlCacheEntryEx([MarshalAs(UnmanagedType.LPTStr)] string lpszUrlSearchPattern, int dwFlags, WININETCACHEENTRYTYPE dwFilter, long GroupId, IntPtr lpFirstCacheEntryInfo, ref int lpdwFirstCacheEntryInfoBufferSize, IntPtr lpReserved, IntPtr pcbReserved2, IntPtr lpReserved3);
  182.     [DllImport("wininet.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
  183.     private static extern IntPtr FindFirstUrlCacheGroup(int dwFlags, int dwFilter, IntPtr lpSearchCondition, int dwSearchCondition, ref long lpGroupId, IntPtr lpReserved);
  184.     [return: MarshalAs(UnmanagedType.Bool)]
  185.     [DllImport("wininet.dll", EntryPoint="FindNextUrlCacheEntryA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
  186.     private static extern bool FindNextUrlCacheEntry(IntPtr hFind, IntPtr lpNextCacheEntryInfo, ref int lpdwNextCacheEntryInfoBufferSize);
  187.     [return: MarshalAs(UnmanagedType.Bool)]
  188.     [DllImport("wininet.dll", EntryPoint="FindNextUrlCacheEntryExA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
  189.     private static extern bool FindNextUrlCacheEntryEx(IntPtr hEnumHandle, IntPtr lpNextCacheEntryInfo, ref int lpdwNextCacheEntryInfoBufferSize, IntPtr lpReserved, IntPtr pcbReserved2, IntPtr lpReserved3);
  190.     [return: MarshalAs(UnmanagedType.Bool)]
  191.     [DllImport("wininet.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
  192.     private static extern bool FindNextUrlCacheGroup(IntPtr hFind, ref long lpGroupId, IntPtr lpReserved);
  193.     internal static string GetCacheItemInfo(string sURL)
  194.     {
  195.         int lpdwCacheEntryInfoBufferSize = 0;
  196.         int cb = 0;
  197.         IntPtr zero = IntPtr.Zero;
  198.         bool flag = GetUrlCacheEntryInfoA(sURL, zero, ref lpdwCacheEntryInfoBufferSize);
  199.         int num = Marshal.GetLastWin32Error();
  200.         if (flag || (num != 0x7a))
  201.         {
  202.             return string.Format("This URL is not present in the WinINET cache. [Code: {0}]", num);
  203.         }
  204.         cb = lpdwCacheEntryInfoBufferSize;
  205.         zero = Marshal.AllocHGlobal(cb);
  206.         flag = GetUrlCacheEntryInfoA(sURL, zero, ref lpdwCacheEntryInfoBufferSize);
  207.         num = Marshal.GetLastWin32Error();
  208.         if (!flag)
  209.         {
  210.             Marshal.FreeHGlobal(zero);
  211.             return ("GetUrlCacheEntryInfoA with buffer failed. 2=filenotfound 122=insufficient buffer, 259=nomoreitems. Last error: " + num.ToString() + "\n");
  212.         }
  213.         INTERNET_CACHE_ENTRY_INFOA internet_cache_entry_infoa = (INTERNET_CACHE_ENTRY_INFOA) Marshal.PtrToStructure(zero, typeof(INTERNET_CACHE_ENTRY_INFOA));
  214.         lpdwCacheEntryInfoBufferSize = cb;
  215.         long fileTime = (internet_cache_entry_infoa.LastModifiedTime.dwHighDateTime << 0x20) | ((long) ((ulong) internet_cache_entry_infoa.LastModifiedTime.dwLowDateTime));
  216.         long num5 = (internet_cache_entry_infoa.LastAccessTime.dwHighDateTime << 0x20) | ((long) ((ulong) internet_cache_entry_infoa.LastAccessTime.dwLowDateTime));
  217.         long num6 = (internet_cache_entry_infoa.LastSyncTime.dwHighDateTime << 0x20) | ((long) ((ulong) internet_cache_entry_infoa.LastSyncTime.dwLowDateTime));
  218.         long num7 = (internet_cache_entry_infoa.ExpireTime.dwHighDateTime << 0x20) | ((long) ((ulong) internet_cache_entry_infoa.ExpireTime.dwLowDateTime));
  219.         string[] strArray = new string[] {
  220.             "Url:\t\t", Marshal.PtrToStringAnsi(internet_cache_entry_infoa.lpszSourceUrlName), "\nCache File:\t", Marshal.PtrToStringAnsi(internet_cache_entry_infoa.lpszLocalFileName), "\nSize:\t\t", ((ulong) ((internet_cache_entry_infoa.dwSizeHigh << 0x20) + internet_cache_entry_infoa.dwSizeLow)).ToString("0,0"), " bytes\nFile Extension:\t", Marshal.PtrToStringAnsi(internet_cache_entry_infoa.lpszFileExtension), "\nHit Rate:\t", internet_cache_entry_infoa.dwHitRate.ToString(), "\nUse Count:\t", internet_cache_entry_infoa.dwUseCount.ToString(), "\nDon't Scavenge for:\t", internet_cache_entry_infoa._Union.dwExemptDelta.ToString(), " seconds\nLast Modified:\t", DateTime.FromFileTime(fileTime).ToString(),
  221.             "\nLast Accessed:\t", DateTime.FromFileTime(num5).ToString(), "\nLast Synced:  \t", DateTime.FromFileTime(num6).ToString(), "\nEntry Expires:\t", DateTime.FromFileTime(num7).ToString(), "\n"
  222.          };
  223.         string str = string.Concat(strArray);
  224.         Marshal.FreeHGlobal(zero);
  225.         return str;
  226.     }
  227.  
  228.     [return: MarshalAs(UnmanagedType.Bool)]
  229.     [DllImport("wininet.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
  230.     private static extern bool GetUrlCacheEntryInfoA(string lpszUrlName, IntPtr lpCacheEntryInfo, ref int lpdwCacheEntryInfoBufferSize);
  231.     private static void VistaClearTracks(bool bClearFiles, bool bClearCookies)
  232.     {
  233.         int num = 0;
  234.         if (bClearCookies)
  235.         {
  236.             num = 2;
  237.         }
  238.         if (bClearFiles)
  239.         {
  240.             num = 0x100c;
  241.         }
  242.         try
  243.         {
  244.             using (Process.Start("rundll32.exe", "inetcpl.cpl,ClearMyTracksByProcess " + num.ToString()))
  245.             {
  246.             }
  247.         }
  248.         catch (Exception exception)
  249.         {
  250.             FiddlerApplication.DoNotifyUser("Failed to launch ClearMyTracksByProcess.\n" + exception.Message, "Error");
  251.         }
  252.     }
  253.  
  254.     // Nested Types
  255.     [StructLayout(LayoutKind.Sequential)]
  256.     private class INTERNET_CACHE_ENTRY_INFOA
  257.     {
  258.         public uint dwStructureSize;
  259.         public IntPtr lpszSourceUrlName;
  260.         public IntPtr lpszLocalFileName;
  261.         public WinINETCache.WININETCACHEENTRYTYPE CacheEntryType;
  262.         public uint dwUseCount;
  263.         public uint dwHitRate;
  264.         public uint dwSizeLow;
  265.         public uint dwSizeHigh;
  266.         public FILETIME LastModifiedTime;
  267.         public FILETIME ExpireTime;
  268.         public FILETIME LastAccessTime;
  269.         public FILETIME LastSyncTime;
  270.         public IntPtr lpHeaderInfo;
  271.         public uint dwHeaderInfoSize;
  272.         public IntPtr lpszFileExtension;
  273.         public WinINETCache.WININETCACHEENTRYINFOUNION _Union;
  274.     }
  275.  
  276.     [StructLayout(LayoutKind.Explicit)]
  277.     private struct WININETCACHEENTRYINFOUNION
  278.     {
  279.         // Fields
  280.         [FieldOffset(0)]
  281.         public uint dwExemptDelta;
  282.         [FieldOffset(0)]
  283.         public uint dwReserved;
  284.     }
  285.  
  286.     private enum WININETCACHEENTRYTYPE
  287.     {
  288.         ALL = 0x31003d,
  289.         COOKIE_CACHE_ENTRY = 0x100000,
  290.         EDITED_CACHE_ENTRY = 8,
  291.         None = 0,
  292.         NORMAL_CACHE_ENTRY = 1,
  293.         SPARSE_CACHE_ENTRY = 0x10000,
  294.         STICKY_CACHE_ENTRY = 4,
  295.         TRACK_OFFLINE_CACHE_ENTRY = 0x10,
  296.         TRACK_ONLINE_CACHE_ENTRY = 0x20,
  297.         URLHISTORY_CACHE_ENTRY = 0x200000
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement