<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>Win32</title><link>http://blogs.clearscreen.com/jfbonnin/category/199.aspx</link><description>Win32</description><managingEditor>Jose Fco. Bonnin</managingEditor><dc:language>en-US</dc:language><generator>.Text Version 0.95.2004.102</generator><item><dc:creator>Jose Fco. Bonnin</dc:creator><title>Google Safe Browsing API with Internet Explorer</title><link>http://blogs.clearscreen.com/jfbonnin/archive/2007/07/08/6579.aspx</link><pubDate>Sun, 08 Jul 2007 16:57:00 GMT</pubDate><guid>http://blogs.clearscreen.com/jfbonnin/archive/2007/07/08/6579.aspx</guid><wfw:comment>http://blogs.clearscreen.com/jfbonnin/comments/6579.aspx</wfw:comment><comments>http://blogs.clearscreen.com/jfbonnin/archive/2007/07/08/6579.aspx#Feedback</comments><slash:comments>25</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/jfbonnin/comments/commentRss/6579.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/jfbonnin/services/trackbacks/6579.aspx</trackback:ping><description>&lt;p&gt;These days lot of bloggers have been writing about the new API of Google: &lt;a href="http://code.google.com/apis/safebrowsing/" target="_blank"&gt;Google Safe Browsing API.&lt;/a&gt; &lt;em&gt;The Safe Browsing API is an experimental API that enables client applications to check URLs against Google's constantly updated blacklists of suspected phishing and malware pages.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;I think this is a cool API and if you want to take profit of it you already have it with &lt;a href="http://www.firefox.com" target="_blank"&gt;FireFox&lt;/a&gt;. But, I like Internet Explorer so I woud like to use both together. &lt;/p&gt; &lt;p&gt;If you take a look to the documentation, you will see that its usage is actually very easy. The idea is that you get a list of URL hashes, which are marked as phishing or malware; then you only need to validate the url's you want to check against these hash tables. I'm not going to enter in too much details since you can read the guide that Google provides you.&lt;/p&gt; &lt;p&gt;What I've done is let's say: a  "proof of concept", that it's possible to use Google Safe Browsing with IE. I've built an implementation of this using C#.  The code you can &lt;a href="http://blogs.clearscreen.com/jfbonnin/files/GoogleSafeBrowsing.zip"&gt;download&lt;/a&gt; consists in two main parts: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;A Windows Service that hosts a &lt;a href="http://wcf.netfx3.com/" target="_blank"&gt;WCF Service&lt;/a&gt; with the logic to perform updates and the validation against the hash tables.  &lt;/li&gt;&lt;li&gt;An &lt;a href="http://msdn2.microsoft.com/en-us/library/bb250436.aspx" target="_blank"&gt;BHO (Browser Helper Object)&lt;/a&gt; that queries the information to the WCF Service.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;The BHO is configured as an add-on for IE. It just handles the BeforeNavigate2 event and checks the URL making a call to the published service, if the URL is safe then nothing happens otherwise the navigation is cancelled and the message displayed:&lt;/p&gt; &lt;p&gt; &lt;/p&gt;&lt;img src="http://blogs.clearscreen.com/jfbonnin/files/MalwareScreenshot.png" border="0" /&gt;  &lt;p&gt; &lt;/p&gt; &lt;p&gt;The code for the BHO is pretty simple, the first you need to do is to declare the interface &lt;a href="http://msdn2.microsoft.com/en-us/library/aa768220.aspx" target="_blank"&gt;IObjectWithSite&lt;/a&gt; to make available for .NET&lt;/p&gt; &lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color: white; word-wrap: break-word"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008080"&gt;1&lt;/span&gt; &lt;span style="color: #000000"&gt;[ComImport]
&lt;/span&gt;&lt;span style="color: #008080"&gt;2&lt;/span&gt; &lt;span style="color: #000000"&gt;[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
&lt;/span&gt;&lt;span style="color: #008080"&gt;3&lt;/span&gt; &lt;span style="color: #000000"&gt;[Guid(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;FC4801A3-2BA9-11CF-A229-00AA003D7352&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)]
&lt;/span&gt;&lt;span style="color: #008080"&gt;4&lt;/span&gt; &lt;span style="color: #000000"&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="color: #000000"&gt; IObjectWithSite
&lt;/span&gt;&lt;span style="color: #008080"&gt;5&lt;/span&gt; &lt;span style="color: #000000"&gt;{
&lt;/span&gt;&lt;span style="color: #008080"&gt;6&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; SetSite([In, MarshalAs(UnmanagedType.IUnknown)] Object pUnkSite);
&lt;/span&gt;&lt;span style="color: #008080"&gt;7&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; GetSite(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt;&lt;span style="color: #000000"&gt; Guid riid, [MarshalAs(UnmanagedType.IUnknown)] &lt;/span&gt;&lt;span style="color: #0000ff"&gt;out&lt;/span&gt;&lt;span style="color: #000000"&gt; Object ppvSite);
&lt;/span&gt;&lt;span style="color: #008080"&gt;8&lt;/span&gt; &lt;span style="color: #000000"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt; Once you have done this you need to do a class that implements it, this class will be the one loaded by IE and where you will capture the events.&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color: white; word-wrap: break-word"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008080"&gt; 1&lt;/span&gt; &lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt;&lt;span style="color: #000000"&gt; BalearesOnNet.GoogleSafeBrowsing.BHO
&lt;/span&gt;&lt;span style="color: #008080"&gt; 2&lt;/span&gt; &lt;span style="color: #000000"&gt;{
&lt;/span&gt;&lt;span style="color: #008080"&gt; 3&lt;/span&gt; &lt;span style="color: #000000"&gt;    [ComVisible(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;&lt;span style="color: #000000"&gt;),
&lt;/span&gt;&lt;span style="color: #008080"&gt; 4&lt;/span&gt; &lt;span style="color: #000000"&gt;    ClassInterface(ClassInterfaceType.None), Guid(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;D5423C28-959D-4909-BB9B-431286B62483&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)]
&lt;/span&gt;&lt;span style="color: #008080"&gt; 5&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; SafeBrowsingBHO : IObjectWithSite
&lt;/span&gt;&lt;span style="color: #008080"&gt; 6&lt;/span&gt; &lt;span style="color: #000000"&gt;    {
&lt;/span&gt;&lt;span style="color: #008080"&gt; 7&lt;/span&gt; &lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;const&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; bhoRegistryKey &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;@"&lt;/span&gt;&lt;span style="color: #800000"&gt;SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;;
&lt;/span&gt;&lt;span style="color: #008080"&gt; 8&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt; 9&lt;/span&gt; &lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; SHDocVw.WebBrowser webBrowser;
&lt;/span&gt;&lt;span style="color: #008080"&gt;10&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;11&lt;/span&gt; &lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;#region&lt;/span&gt;&lt;span style="color: #000000"&gt; IObjectWithSite Members&lt;/span&gt;&lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;12&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;13&lt;/span&gt; &lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; SetSite(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; pUnkSite)
&lt;/span&gt;&lt;span style="color: #008080"&gt;14&lt;/span&gt; &lt;span style="color: #000000"&gt;        {
&lt;/span&gt;&lt;span style="color: #008080"&gt;15&lt;/span&gt; &lt;span style="color: #000000"&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (pUnkSite &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;)
&lt;/span&gt;&lt;span style="color: #008080"&gt;16&lt;/span&gt; &lt;span style="color: #000000"&gt;            {
&lt;/span&gt;&lt;span style="color: #008080"&gt;17&lt;/span&gt; &lt;span style="color: #000000"&gt;                webBrowser.BeforeNavigate2 &lt;/span&gt;&lt;span style="color: #000000"&gt;-=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; DWebBrowserEvents2_BeforeNavigate2EventHandler(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;&lt;span style="color: #000000"&gt;.webBrowser_BeforeNavigate2);
&lt;/span&gt;&lt;span style="color: #008080"&gt;18&lt;/span&gt; &lt;span style="color: #000000"&gt;                webBrowser &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;;
&lt;/span&gt;&lt;span style="color: #008080"&gt;19&lt;/span&gt; &lt;span style="color: #000000"&gt;            }
&lt;/span&gt;&lt;span style="color: #008080"&gt;20&lt;/span&gt; &lt;span style="color: #000000"&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;21&lt;/span&gt; &lt;span style="color: #000000"&gt;            {
&lt;/span&gt;&lt;span style="color: #008080"&gt;22&lt;/span&gt; &lt;span style="color: #000000"&gt;                webBrowser &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; (SHDocVw.WebBrowser)pUnkSite;
&lt;/span&gt;&lt;span style="color: #008080"&gt;23&lt;/span&gt; &lt;span style="color: #000000"&gt;                webBrowser.BeforeNavigate2 &lt;/span&gt;&lt;span style="color: #000000"&gt;+=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; DWebBrowserEvents2_BeforeNavigate2EventHandler(webBrowser_BeforeNavigate2);
&lt;/span&gt;&lt;span style="color: #008080"&gt;24&lt;/span&gt; &lt;span style="color: #000000"&gt;            }
&lt;/span&gt;&lt;span style="color: #008080"&gt;25&lt;/span&gt; &lt;span style="color: #000000"&gt;        }
&lt;/span&gt;&lt;span style="color: #008080"&gt;26&lt;/span&gt; &lt;span style="color: #000000"&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt; The other interesting part of it, is how to cancel the navigation and modify the HtmlDocument, with our custom html.&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color: white; word-wrap: break-word"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008080"&gt; 1&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; webBrowser_BeforeNavigate2(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; pDisp, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; URL, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; Flags, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; TargetFrameName, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; PostData, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; Headers, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;&lt;span style="color: #000000"&gt; Cancel)
&lt;/span&gt;&lt;span style="color: #008080"&gt; 2&lt;/span&gt; &lt;span style="color: #000000"&gt;{
&lt;/span&gt;&lt;span style="color: #008080"&gt; 3&lt;/span&gt; &lt;span style="color: #000000"&gt;    ....
&lt;/span&gt;&lt;span style="color: #008080"&gt; 4&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt; 5&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (result &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; UrlValidationResult.Malware &lt;/span&gt;&lt;span style="color: #000000"&gt;||&lt;/span&gt;&lt;span style="color: #000000"&gt; result &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; UrlValidationResult.BlackList)
&lt;/span&gt;&lt;span style="color: #008080"&gt; 6&lt;/span&gt; &lt;span style="color: #000000"&gt;    {
&lt;/span&gt;&lt;span style="color: #008080"&gt; 7&lt;/span&gt; &lt;span style="color: #000000"&gt;        IHTMLDocument2 doc &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webBrowser.Document &lt;/span&gt;&lt;span style="color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="color: #000000"&gt; IHTMLDocument2;
&lt;/span&gt;&lt;span style="color: #008080"&gt; 8&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt; 9&lt;/span&gt; &lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (doc &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;)
&lt;/span&gt;&lt;span style="color: #008080"&gt;10&lt;/span&gt; &lt;span style="color: #000000"&gt;        {
&lt;/span&gt;&lt;span style="color: #008080"&gt;11&lt;/span&gt; &lt;span style="color: #000000"&gt;            doc.clear();
&lt;/span&gt;&lt;span style="color: #008080"&gt;12&lt;/span&gt; &lt;span style="color: #000000"&gt;            doc.writeln(result &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; UrlValidationResult.Malware &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; Resources.MalwareWarning : Resources.BlackWarning);
&lt;/span&gt;&lt;span style="color: #008080"&gt;13&lt;/span&gt; &lt;span style="color: #000000"&gt;            doc.close();
&lt;/span&gt;&lt;span style="color: #008080"&gt;14&lt;/span&gt; &lt;span style="color: #000000"&gt;        }
&lt;/span&gt;&lt;span style="color: #008080"&gt;15&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;16&lt;/span&gt; &lt;span style="color: #000000"&gt;        Cancel &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;&lt;span style="color: #000000"&gt;;
&lt;/span&gt;&lt;span style="color: #008080"&gt;17&lt;/span&gt; &lt;span style="color: #000000"&gt;    }
&lt;/span&gt;&lt;span style="color: #008080"&gt;18&lt;/span&gt; &lt;span style="color: #000000"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The WCF Service exposes only two methods: "public void Update()" and "public UrlValidationResult ValidateUrl(Uri uri)". This service what it does is to obtain and keep updated the hash tables doing incremental updates, as well as the logic to validate the url's against these tables. The tables are stored in an &lt;a href="http://msdn2.microsoft.com/en-us/library/system.io.isolatedstorage.aspx" target="_blank"&gt;IsolatedStorage&lt;/a&gt; to avoid obtaining them from internet every time, nevertheless the tables are loaded and operated in memory.&lt;/p&gt;
&lt;p&gt;In order to validate a url you need to perform some steps, first of all you need to obtain a 128 bit MD5 Hash of the URL you want to check, then you need to get the string representation of this hash. With .NET you can accomplish this easily.&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color: white; word-wrap: break-word"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008080"&gt; 1&lt;/span&gt; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; GetHash(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; url)
&lt;/span&gt;&lt;span style="color: #008080"&gt; 2&lt;/span&gt; &lt;span style="color: #000000"&gt;{
&lt;/span&gt;&lt;span style="color: #008080"&gt; 3&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;&lt;span style="color: #000000"&gt;[] hashBytes;
&lt;/span&gt;&lt;span style="color: #008080"&gt; 4&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; (MD5 md5 &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; MD5.Create())
&lt;/span&gt;&lt;span style="color: #008080"&gt; 5&lt;/span&gt; &lt;span style="color: #000000"&gt;    {
&lt;/span&gt;&lt;span style="color: #008080"&gt; 6&lt;/span&gt; &lt;span style="color: #000000"&gt;        hashBytes &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(url));
&lt;/span&gt;&lt;span style="color: #008080"&gt; 7&lt;/span&gt; &lt;span style="color: #000000"&gt;    }
&lt;/span&gt;&lt;span style="color: #008080"&gt; 8&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt; 9&lt;/span&gt; &lt;span style="color: #000000"&gt;    StringBuilder sb &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; StringBuilder(&lt;/span&gt;&lt;span style="color: #800080"&gt;32&lt;/span&gt;&lt;span style="color: #000000"&gt;);
&lt;/span&gt;&lt;span style="color: #008080"&gt;10&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;11&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; length &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; hashBytes.Length;
&lt;/span&gt;&lt;span style="color: #008080"&gt;12&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;for&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; i &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;; i &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt; length; i&lt;/span&gt;&lt;span style="color: #000000"&gt;++&lt;/span&gt;&lt;span style="color: #000000"&gt;)
&lt;/span&gt;&lt;span style="color: #008080"&gt;13&lt;/span&gt; &lt;span style="color: #000000"&gt;    {
&lt;/span&gt;&lt;span style="color: #008080"&gt;14&lt;/span&gt; &lt;span style="color: #000000"&gt;        sb.Append(hashBytes[i].ToString(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;x2&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;));
&lt;/span&gt;&lt;span style="color: #008080"&gt;15&lt;/span&gt; &lt;span style="color: #000000"&gt;    }
&lt;/span&gt;&lt;span style="color: #008080"&gt;16&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;17&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; sb.ToString();
&lt;/span&gt;&lt;span style="color: #008080"&gt;18&lt;/span&gt; &lt;span style="color: #000000"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt; Google suggest you also to perform several lookups from the same URL to get an accurated result, which consists of: the exact hostname in the URL, up to 4 hostnames formed by starting with the last 4 components and successively removing the leading component. In addition for the path you should try at most 6 different strings: the exact path of the url, including query parameters; the exact path of the url, without query parameters and the 4 paths formed by starting at the root (/) and succesively appending path components, including a trailing slahsh. A sample displays better what this means.&lt;/p&gt;
&lt;p&gt;For the url http://a.b.c.d.e.f.g/1.html&lt;/p&gt;
&lt;p&gt;a.b.c.d.e.f.g/1.html 
&lt;/p&gt;&lt;p&gt;a.b.c.d.e.f.g/ 
&lt;/p&gt;&lt;p&gt;c.d.e.f.g/1.html 
&lt;/p&gt;&lt;p&gt;c.d.e.f.g/ 
&lt;/p&gt;&lt;p&gt;d.e.f.g/1.html 
&lt;/p&gt;&lt;p&gt;d.e.f.g/ 
&lt;/p&gt;&lt;p&gt;e.f.g/1.html 
&lt;/p&gt;&lt;p&gt;e.f.g/ 
&lt;/p&gt;&lt;p&gt;f.g/1.html 
&lt;/p&gt;&lt;p&gt;f.g/ 
&lt;/p&gt;&lt;p&gt;*(Note that b.c.d.e.f.g, is skipped since we'll take only the last 5 hostname components, and the full hostname) 
&lt;/p&gt;&lt;p&gt;Another interesting feature is that you can verify that the tables obtained in the requests come from Google, this is obtained by requesting a pair of keys, client key and wrapped key. The wrapped key must be sent along the requests for updates, then Google will include a MAC (Message Authentication Code) in the header of each response following the structure "[mac=dRalfTU+bXwUhlk0NCGJtQ==]". In order to validate this mac you need to do again a 128 bit MD5 Hash with the following information: client_key|separator|table data|separator|client_key. Where the separator is the string:coolgoog: - that is a colon followed by "coolgoog" followed by a colon. To be honest I got a bit stuck here, I've tried a few ways to verify a MAC but there is something I'm missing and I cannot get the expected result. Maybe I try again when I come back from holidays.
&lt;/p&gt;&lt;p&gt;You can &lt;a href="http://blogs.clearscreen.com/jfbonnin/files/GoogleSafeBrowsing.zip"&gt;download&lt;/a&gt; the code and test it, but remember that this code is provided as is and cannot be considered finished code. There is room enough for improvement in many areas including exception handling that has not been considered for this sample.
&lt;/p&gt;&lt;p&gt;In order to test you will need to install and start the windows service included, before start it be sure that you include your own key in the appSettings section of the file "BalearesOnNet.GoogleSafeBrowsing.Service.exe.config". The BHO is configured to be installed when you compile it with Visual Studio, you can disable this option by unchecking the option "Register for COM interop" in the properties of the project.
&lt;/p&gt;&lt;p&gt;I hope you like it.&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/jfbonnin/aggbug/6579.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>These days lot of bloggers have been writing about the new API of Google: <a href="http://code.google.com/apis/safebrowsing/" target="_blank">Google Safe Browsing API.</a> <em>The Safe Browsing API is an experimental API that enables client applications to check URLs against Google's constantly updated blacklists of suspected phishing and malware pages.</em></p> <p>I think this is a cool API and if you want to take profit of it you already have it with <a href="http://www.firefox.com" target="_blank">FireFox</a>. But, I like Internet Explorer so I woud like to use both together. </p> <p>If you take a look to the documentation, you will see that its usage is actually very easy. The idea is that you get a list of URL hashes, which are marked as phishing or malware; then you only need to validate the url's you want to check against these hash tables. I'm not going to enter in too much details since you can read the guide that Google provides you.</p> <p>What I've done is let's say: a  "proof of concept", that it's possible to use Google Safe Browsing with IE. I've built an implementation of this using C#.  The code you can <a href="http://blogs.clearscreen.com/jfbonnin/files/GoogleSafeBrowsing.zip">download</a> consists in two main parts: </p> <ul> <li>A Windows Service that hosts a <a href="http://wcf.netfx3.com/" target="_blank">WCF Service</a> with the logic to perform updates and the validation against the hash tables.  </li><li>An <a href="http://msdn2.microsoft.com/en-us/library/bb250436.aspx" target="_blank">BHO (Browser Helper Object)</a> that queries the information to the WCF Service.</li></ul> <p> </p> <p>The BHO is configured as an add-on for IE. It just handles the BeforeNavigate2 event and checks the URL making a call to the published service, if the URL is safe then nothing happens otherwise the navigation is cancelled and the message displayed:</p> <p> </p><img src="http://blogs.clearscreen.com/jfbonnin/files/MalwareScreenshot.png" border="0" />  <p> </p> <p>The code for the BHO is pretty simple, the first you need to do is to declare the interface <a href="http://msdn2.microsoft.com/en-us/library/aa768220.aspx" target="_blank">IObjectWithSite</a> to make available for .NET</p> <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color: white; word-wrap: break-word"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080">1</span> <span style="color: #000000">[ComImport]
</span><span style="color: #008080">2</span> <span style="color: #000000">[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
</span><span style="color: #008080">3</span> <span style="color: #000000">[Guid(</span><span style="color: #800000">"</span><span style="color: #800000">FC4801A3-2BA9-11CF-A229-00AA003D7352</span><span style="color: #800000">"</span><span style="color: #000000">)]
</span><span style="color: #008080">4</span> <span style="color: #000000"></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">interface</span><span style="color: #000000"> IObjectWithSite
</span><span style="color: #008080">5</span> <span style="color: #000000">{
</span><span style="color: #008080">6</span> <span style="color: #000000">    </span><span style="color: #0000ff">void</span><span style="color: #000000"> SetSite([In, MarshalAs(UnmanagedType.IUnknown)] Object pUnkSite);
</span><span style="color: #008080">7</span> <span style="color: #000000">    </span><span style="color: #0000ff">void</span><span style="color: #000000"> GetSite(</span><span style="color: #0000ff">ref</span><span style="color: #000000"> Guid riid, [MarshalAs(UnmanagedType.IUnknown)] </span><span style="color: #0000ff">out</span><span style="color: #000000"> Object ppvSite);
</span><span style="color: #008080">8</span> <span style="color: #000000">}</span></div></pre></div>
<p> Once you have done this you need to do a class that implements it, this class will be the one loaded by IE and where you will capture the events.</p>
<div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color: white; word-wrap: break-word"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080"> 1</span> <span style="color: #0000ff">namespace</span><span style="color: #000000"> BalearesOnNet.GoogleSafeBrowsing.BHO
</span><span style="color: #008080"> 2</span> <span style="color: #000000">{
</span><span style="color: #008080"> 3</span> <span style="color: #000000">    [ComVisible(</span><span style="color: #0000ff">true</span><span style="color: #000000">),
</span><span style="color: #008080"> 4</span> <span style="color: #000000">    ClassInterface(ClassInterfaceType.None), Guid(</span><span style="color: #800000">"</span><span style="color: #800000">D5423C28-959D-4909-BB9B-431286B62483</span><span style="color: #800000">"</span><span style="color: #000000">)]
</span><span style="color: #008080"> 5</span> <span style="color: #000000">    </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> SafeBrowsingBHO : IObjectWithSite
</span><span style="color: #008080"> 6</span> <span style="color: #000000">    {
</span><span style="color: #008080"> 7</span> <span style="color: #000000">        </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> bhoRegistryKey </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">@"</span><span style="color: #800000">SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects</span><span style="color: #800000">"</span><span style="color: #000000">;
</span><span style="color: #008080"> 8</span> <span style="color: #000000">
</span><span style="color: #008080"> 9</span> <span style="color: #000000">        </span><span style="color: #0000ff">private</span><span style="color: #000000"> SHDocVw.WebBrowser webBrowser;
</span><span style="color: #008080">10</span> <span style="color: #000000">
</span><span style="color: #008080">11</span> <span style="color: #000000">        </span><span style="color: #0000ff">#region</span><span style="color: #000000"> IObjectWithSite Members</span><span style="color: #000000">
</span><span style="color: #008080">12</span> <span style="color: #000000">
</span><span style="color: #008080">13</span> <span style="color: #000000">        </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> SetSite(</span><span style="color: #0000ff">object</span><span style="color: #000000"> pUnkSite)
</span><span style="color: #008080">14</span> <span style="color: #000000">        {
</span><span style="color: #008080">15</span> <span style="color: #000000">            </span><span style="color: #0000ff">if</span><span style="color: #000000"> (pUnkSite </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">16</span> <span style="color: #000000">            {
</span><span style="color: #008080">17</span> <span style="color: #000000">                webBrowser.BeforeNavigate2 </span><span style="color: #000000">-=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> DWebBrowserEvents2_BeforeNavigate2EventHandler(</span><span style="color: #0000ff">this</span><span style="color: #000000">.webBrowser_BeforeNavigate2);
</span><span style="color: #008080">18</span> <span style="color: #000000">                webBrowser </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">;
</span><span style="color: #008080">19</span> <span style="color: #000000">            }
</span><span style="color: #008080">20</span> <span style="color: #000000">            </span><span style="color: #0000ff">else</span><span style="color: #000000">
</span><span style="color: #008080">21</span> <span style="color: #000000">            {
</span><span style="color: #008080">22</span> <span style="color: #000000">                webBrowser </span><span style="color: #000000">=</span><span style="color: #000000"> (SHDocVw.WebBrowser)pUnkSite;
</span><span style="color: #008080">23</span> <span style="color: #000000">                webBrowser.BeforeNavigate2 </span><span style="color: #000000">+=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> DWebBrowserEvents2_BeforeNavigate2EventHandler(webBrowser_BeforeNavigate2);
</span><span style="color: #008080">24</span> <span style="color: #000000">            }
</span><span style="color: #008080">25</span> <span style="color: #000000">        }
</span><span style="color: #008080">26</span> <span style="color: #000000">...</span></div></pre></div>
<p> The other interesting part of it, is how to cancel the navigation and modify the HtmlDocument, with our custom html.</p>
<div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color: white; word-wrap: break-word"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080"> 1</span> <span style="color: #0000ff">void</span><span style="color: #000000"> webBrowser_BeforeNavigate2(</span><span style="color: #0000ff">object</span><span style="color: #000000"> pDisp, </span><span style="color: #0000ff">ref</span><span style="color: #000000"> </span><span style="color: #0000ff">object</span><span style="color: #000000"> URL, </span><span style="color: #0000ff">ref</span><span style="color: #000000"> </span><span style="color: #0000ff">object</span><span style="color: #000000"> Flags, </span><span style="color: #0000ff">ref</span><span style="color: #000000"> </span><span style="color: #0000ff">object</span><span style="color: #000000"> TargetFrameName, </span><span style="color: #0000ff">ref</span><span style="color: #000000"> </span><span style="color: #0000ff">object</span><span style="color: #000000"> PostData, </span><span style="color: #0000ff">ref</span><span style="color: #000000"> </span><span style="color: #0000ff">object</span><span style="color: #000000"> Headers, </span><span style="color: #0000ff">ref</span><span style="color: #000000"> </span><span style="color: #0000ff">bool</span><span style="color: #000000"> Cancel)
</span><span style="color: #008080"> 2</span> <span style="color: #000000">{
</span><span style="color: #008080"> 3</span> <span style="color: #000000">    ....
</span><span style="color: #008080"> 4</span> <span style="color: #000000">
</span><span style="color: #008080"> 5</span> <span style="color: #000000">    </span><span style="color: #0000ff">if</span><span style="color: #000000"> (result </span><span style="color: #000000">==</span><span style="color: #000000"> UrlValidationResult.Malware </span><span style="color: #000000">||</span><span style="color: #000000"> result </span><span style="color: #000000">==</span><span style="color: #000000"> UrlValidationResult.BlackList)
</span><span style="color: #008080"> 6</span> <span style="color: #000000">    {
</span><span style="color: #008080"> 7</span> <span style="color: #000000">        IHTMLDocument2 doc </span><span style="color: #000000">=</span><span style="color: #000000"> webBrowser.Document </span><span style="color: #0000ff">as</span><span style="color: #000000"> IHTMLDocument2;
</span><span style="color: #008080"> 8</span> <span style="color: #000000">
</span><span style="color: #008080"> 9</span> <span style="color: #000000">        </span><span style="color: #0000ff">if</span><span style="color: #000000"> (doc </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)
</span><span style="color: #008080">10</span> <span style="color: #000000">        {
</span><span style="color: #008080">11</span> <span style="color: #000000">            doc.clear();
</span><span style="color: #008080">12</span> <span style="color: #000000">            doc.writeln(result </span><span style="color: #000000">==</span><span style="color: #000000"> UrlValidationResult.Malware </span><span style="color: #000000">?</span><span style="color: #000000"> Resources.MalwareWarning : Resources.BlackWarning);
</span><span style="color: #008080">13</span> <span style="color: #000000">            doc.close();
</span><span style="color: #008080">14</span> <span style="color: #000000">        }
</span><span style="color: #008080">15</span> <span style="color: #000000">
</span><span style="color: #008080">16</span> <span style="color: #000000">        Cancel </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">;
</span><span style="color: #008080">17</span> <span style="color: #000000">    }
</span><span style="color: #008080">18</span> <span style="color: #000000">}</span></div></pre></div>
<p>The WCF Service exposes only two methods: "public void Update()" and "public UrlValidationResult ValidateUrl(Uri uri)". This service what it does is to obtain and keep updated the hash tables doing incremental updates, as well as the logic to validate the url's against these tables. The tables are stored in an <a href="http://msdn2.microsoft.com/en-us/library/system.io.isolatedstorage.aspx" target="_blank">IsolatedStorage</a> to avoid obtaining them from internet every time, nevertheless the tables are loaded and operated in memory.</p>
<p>In order to validate a url you need to perform some steps, first of all you need to obtain a 128 bit MD5 Hash of the URL you want to check, then you need to get the string representation of this hash. With .NET you can accomplish this easily.</p>
<div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color: white; word-wrap: break-word"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080"> 1</span> <span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> GetHash(</span><span style="color: #0000ff">string</span><span style="color: #000000"> url)
</span><span style="color: #008080"> 2</span> <span style="color: #000000">{
</span><span style="color: #008080"> 3</span> <span style="color: #000000">    </span><span style="color: #0000ff">byte</span><span style="color: #000000">[] hashBytes;
</span><span style="color: #008080"> 4</span> <span style="color: #000000">    </span><span style="color: #0000ff">using</span><span style="color: #000000"> (MD5 md5 </span><span style="color: #000000">=</span><span style="color: #000000"> MD5.Create())
</span><span style="color: #008080"> 5</span> <span style="color: #000000">    {
</span><span style="color: #008080"> 6</span> <span style="color: #000000">        hashBytes </span><span style="color: #000000">=</span><span style="color: #000000"> md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(url));
</span><span style="color: #008080"> 7</span> <span style="color: #000000">    }
</span><span style="color: #008080"> 8</span> <span style="color: #000000">
</span><span style="color: #008080"> 9</span> <span style="color: #000000">    StringBuilder sb </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> StringBuilder(</span><span style="color: #800080">32</span><span style="color: #000000">);
</span><span style="color: #008080">10</span> <span style="color: #000000">
</span><span style="color: #008080">11</span> <span style="color: #000000">    </span><span style="color: #0000ff">int</span><span style="color: #000000"> length </span><span style="color: #000000">=</span><span style="color: #000000"> hashBytes.Length;
</span><span style="color: #008080">12</span> <span style="color: #000000">    </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; i </span><span style="color: #000000">&lt;</span><span style="color: #000000"> length; i</span><span style="color: #000000">++</span><span style="color: #000000">)
</span><span style="color: #008080">13</span> <span style="color: #000000">    {
</span><span style="color: #008080">14</span> <span style="color: #000000">        sb.Append(hashBytes[i].ToString(</span><span style="color: #800000">"</span><span style="color: #800000">x2</span><span style="color: #800000">"</span><span style="color: #000000">));
</span><span style="color: #008080">15</span> <span style="color: #000000">    }
</span><span style="color: #008080">16</span> <span style="color: #000000">
</span><span style="color: #008080">17</span> <span style="color: #000000">    </span><span style="color: #0000ff">return</span><span style="color: #000000"> sb.ToString();
</span><span style="color: #008080">18</span> <span style="color: #000000">}</span></div></pre></div>
<p> Google suggest you also to perform several lookups from the same URL to get an accurated result, which consists of: the exact hostname in the URL, up to 4 hostnames formed by starting with the last 4 components and successively removing the leading component. In addition for the path you should try at most 6 different strings: the exact path of the url, including query parameters; the exact path of the url, without query parameters and the 4 paths formed by starting at the root (/) and succesively appending path components, including a trailing slahsh. A sample displays better what this means.</p>
<p>For the url http://a.b.c.d.e.f.g/1.html</p>
<p>a.b.c.d.e.f.g/1.html 
</p><p>a.b.c.d.e.f.g/ 
</p><p>c.d.e.f.g/1.html 
</p><p>c.d.e.f.g/ 
</p><p>d.e.f.g/1.html 
</p><p>d.e.f.g/ 
</p><p>e.f.g/1.html 
</p><p>e.f.g/ 
</p><p>f.g/1.html 
</p><p>f.g/ 
</p><p>*(Note that b.c.d.e.f.g, is skipped since we'll take only the last 5 hostname components, and the full hostname) 
</p><p>Another interesting feature is that you can verify that the tables obtained in the requests come from Google, this is obtained by requesting a pair of keys, client key and wrapped key. The wrapped key must be sent along the requests for updates, then Google will include a MAC (Message Authentication Code) in the header of each response following the structure "[mac=dRalfTU+bXwUhlk0NCGJtQ==]". In order to validate this mac you need to do again a 128 bit MD5 Hash with the following information: client_key|separator|table data|separator|client_key. Where the separator is the string:coolgoog: - that is a colon followed by "coolgoog" followed by a colon. To be honest I got a bit stuck here, I've tried a few ways to verify a MAC but there is something I'm missing and I cannot get the expected result. Maybe I try again when I come back from holidays.
</p><p>You can <a href="http://blogs.clearscreen.com/jfbonnin/files/GoogleSafeBrowsing.zip">download</a> the code and test it, but remember that this code is provided as is and cannot be considered finished code. There is room enough for improvement in many areas including exception handling that has not been considered for this sample.
</p><p>In order to test you will need to install and start the windows service included, before start it be sure that you include your own key in the appSettings section of the file "BalearesOnNet.GoogleSafeBrowsing.Service.exe.config". The BHO is configured to be installed when you compile it with Visual Studio, you can disable this option by unchecking the option "Register for COM interop" in the properties of the project.
</p><p>I hope you like it.</p><img src ="http://blogs.clearscreen.com/jfbonnin/aggbug/6579.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Jose Fco. Bonnin</dc:creator><title>Hook Keyboard and Mouse</title><link>http://blogs.clearscreen.com/jfbonnin/archive/2007/06/20/6494.aspx</link><pubDate>Wed, 20 Jun 2007 01:19:00 GMT</pubDate><guid>http://blogs.clearscreen.com/jfbonnin/archive/2007/06/20/6494.aspx</guid><wfw:comment>http://blogs.clearscreen.com/jfbonnin/comments/6494.aspx</wfw:comment><comments>http://blogs.clearscreen.com/jfbonnin/archive/2007/06/20/6494.aspx#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/jfbonnin/comments/commentRss/6494.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/jfbonnin/services/trackbacks/6494.aspx</trackback:ping><description>&lt;p&gt;In the forums of &lt;a href="http://www.baleareson.net/"&gt;Baleares on .NET&lt;/a&gt; (Spanish) a member asked a general question about hooking functions, avoiding the easy joke about what hooking functions are, we could define them basically as a technique which allows putting your own code between the caller and the destination. &lt;/p&gt; &lt;p&gt;One of the easiest and common hooks is the one to capture keyboard and mouse events, something that you can easily implement in .NET making use of InteropServices.  &lt;/p&gt;&lt;p&gt;We will see with a sample how to build a class that intercepts the mouse and keyboard messages and raises a .net event, which users can handle (see below to download the full source code).  &lt;/p&gt;&lt;p&gt;The first thing is to obtain the functions to install and remove the hooks, which we can find in the user32.dll. &lt;/p&gt; &lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color: white; word-wrap: break-word"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008080"&gt;1&lt;/span&gt; &lt;span style="color: #000000"&gt;[DllImport(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;user32.dll&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, CharSet &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; CharSet.Auto, CallingConvention &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; CallingConvention.StdCall)]
&lt;/span&gt;&lt;span style="color: #008080"&gt;2&lt;/span&gt; &lt;span style="color: #000000"&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;extern&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; SetWindowsHookEx(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; idHook, HookProc lpfn, IntPtr hMod, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; dwThreadId);
&lt;/span&gt;&lt;span style="color: #008080"&gt;3&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;4&lt;/span&gt; &lt;span style="color: #000000"&gt;[DllImport(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;user32.dll&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, CharSet &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; CharSet.Auto, CallingConvention &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; CallingConvention.StdCall)]
&lt;/span&gt;&lt;span style="color: #008080"&gt;5&lt;/span&gt; &lt;span style="color: #000000"&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;extern&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;&lt;span style="color: #000000"&gt; UnhookWindowsHookEx(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; idHook);
&lt;/span&gt;&lt;span style="color: #008080"&gt;6&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;7&lt;/span&gt; &lt;span style="color: #000000"&gt;[DllImport(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;user32.dll&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, CharSet &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; CharSet.Auto, CallingConvention &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; CallingConvention.StdCall)]
&lt;/span&gt;&lt;span style="color: #008080"&gt;8&lt;/span&gt; &lt;span style="color: #000000"&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;extern&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; CallNextHookEx(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; idHook, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; nCode, IntPtr wParam, IntPtr lParam);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can find detailed information about what every parameter is in the &lt;a href="http://msdn2.microsoft.com/en-us/library/" target="_blank"&gt;MSDN&lt;/a&gt;, but just as a fast review: idHook is the type of hook to install, lpfn is a pointer to our hook method, hMod is a handle to the DLL that will contain the method pointed by lpfn and finally dwThreadId, which specifies the identifer of the thread with which the hook method is to be associated. The type we have set to lpfn is HookProc, as you will see next, this is just a delegate with the next signature: public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);&lt;/p&gt;
&lt;p&gt;Another important thing to stand up is the third DllImport, what we are going to do is to install and remove hooks from a hook chain, because of that it will be necessary to call the next installed hooks, otherwise we will prevent they are executed. &lt;/p&gt;
&lt;p&gt;To install the hook the only thing we need to do is to call SetWiwnodwsHookEx with the rigth parameters.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color: white; word-wrap: break-word"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008080"&gt; 1&lt;/span&gt; &lt;span style="color: #000000"&gt;HookProc mouseHookProc &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; HookProc(MouseHookProc);
&lt;/span&gt;&lt;span style="color: #008080"&gt; 2&lt;/span&gt; &lt;span style="color: #000000"&gt;...
&lt;/span&gt;&lt;span style="color: #008080"&gt; 3&lt;/span&gt; &lt;span style="color: #000000"&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; InstallMouseHook()
&lt;/span&gt;&lt;span style="color: #008080"&gt; 4&lt;/span&gt; &lt;span style="color: #000000"&gt;{
&lt;/span&gt;&lt;span style="color: #008080"&gt; 5&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #000000"&gt; idHookMouse &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; NativeMethods.SetWindowsHookEx(NativeConstants.WH_MOUSE_LL, mouseHookProc, Marshal.GetHINSTANCE(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;&lt;span style="color: #000000"&gt;.GetType().Module), &lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;);
&lt;/span&gt;&lt;span style="color: #008080"&gt; 6&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (idHookMouse &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;)
&lt;/span&gt;&lt;span style="color: #008080"&gt; 7&lt;/span&gt; &lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; ApplicationException(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;MouseHook cannot be set&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
&lt;/span&gt;&lt;span style="color: #008080"&gt; 8&lt;/span&gt; &lt;span style="color: #000000"&gt;}
&lt;/span&gt;&lt;span style="color: #008080"&gt; 9&lt;/span&gt; &lt;span style="color: #000000"&gt;...
&lt;/span&gt;&lt;span style="color: #008080"&gt;10&lt;/span&gt; &lt;span style="color: #000000"&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; MouseHookProc(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; nCode, IntPtr wParam, IntPtr lParam)
&lt;/span&gt;&lt;span style="color: #008080"&gt;11&lt;/span&gt; &lt;span style="color: #000000"&gt;{
&lt;/span&gt;&lt;span style="color: #008080"&gt;12&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (nCode &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;)
&lt;/span&gt;&lt;span style="color: #008080"&gt;13&lt;/span&gt; &lt;span style="color: #000000"&gt;    {
&lt;/span&gt;&lt;span style="color: #008080"&gt;14&lt;/span&gt; &lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; NativeMethods.CallNextHookEx(NativeConstants.WH_MOUSE_LL, nCode, wParam, lParam);
&lt;/span&gt;&lt;span style="color: #008080"&gt;15&lt;/span&gt; &lt;span style="color: #000000"&gt;    }
&lt;/span&gt;&lt;span style="color: #008080"&gt;16&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;17&lt;/span&gt; &lt;span style="color: #000000"&gt;    {
&lt;/span&gt;&lt;span style="color: #008080"&gt;18&lt;/span&gt; &lt;span style="color: #000000"&gt;        MSLLHOOKSTRUCT mouseHookStruct &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(MSLLHOOKSTRUCT));
&lt;/span&gt;&lt;span style="color: #008080"&gt;19&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;20&lt;/span&gt; &lt;span style="color: #000000"&gt;        HookMouseEventArgs args &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; HookMouseEventArgs();
&lt;/span&gt;&lt;span style="color: #008080"&gt;21&lt;/span&gt; &lt;span style="color: #000000"&gt;        args.Point &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; Point(mouseHookStruct.pt.x, mouseHookStruct.pt.y);
&lt;/span&gt;&lt;span style="color: #008080"&gt;22&lt;/span&gt; &lt;span style="color: #000000"&gt;        args.Message &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; wParam.ToInt32();
&lt;/span&gt;&lt;span style="color: #008080"&gt;23&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;24&lt;/span&gt; &lt;span style="color: #000000"&gt;        OnHookMouse(args);
&lt;/span&gt;&lt;span style="color: #008080"&gt;25&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;26&lt;/span&gt; &lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; NativeMethods.CallNextHookEx(NativeConstants.WH_MOUSE_LL, nCode, wParam, lParam);
&lt;/span&gt;&lt;span style="color: #008080"&gt;27&lt;/span&gt; &lt;span style="color: #000000"&gt;    }
&lt;/span&gt;&lt;span style="color: #008080"&gt;28&lt;/span&gt; &lt;span style="color: #000000"&gt;}
&lt;/span&gt;&lt;span style="color: #008080"&gt;29&lt;/span&gt; &lt;span style="color: #000000"&gt;...
&lt;/span&gt;&lt;span style="color: #008080"&gt;30&lt;/span&gt; &lt;span style="color: #000000"&gt;[StructLayout(LayoutKind.Sequential)]
&lt;/span&gt;&lt;span style="color: #008080"&gt;31&lt;/span&gt; &lt;span style="color: #000000"&gt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; MSLLHOOKSTRUCT
&lt;/span&gt;&lt;span style="color: #008080"&gt;32&lt;/span&gt; &lt;span style="color: #000000"&gt;{
&lt;/span&gt;&lt;span style="color: #008080"&gt;33&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; POINT pt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;34&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; mouseData;
&lt;/span&gt;&lt;span style="color: #008080"&gt;35&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; flags;
&lt;/span&gt;&lt;span style="color: #008080"&gt;36&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; time;
&lt;/span&gt;&lt;span style="color: #008080"&gt;37&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; dwExtraInfo;
&lt;/span&gt;&lt;span style="color: #008080"&gt;38&lt;/span&gt; &lt;span style="color: #000000"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the excerpt of the above code we set a global hook to intercept all the messages of the mouse. When there is a message the method MouseHookProc is called and we will fire an event with the HookMouseEventArgs class as parameters, after that we take care of call the next hook in the chain. In the line 18 you can see how we convert the pointer lParam received as parameter to an struct of type MSLLHOOKSTRUCT. This class contains some valuable information like "mouseData" that represents the message type (left button down, mouse wheel ...), or "pt" that represents the point in which is the mouse.&lt;/p&gt;
&lt;p&gt;The last thing you need to know is that you must remove the hook as soon as possible, since it's resource consuming. This can be accomplished as follows:&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;&lt;pre style="background-color: white; word-wrap: break-word"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008080"&gt;1&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; RemoveMouseHook()
&lt;/span&gt;&lt;span style="color: #008080"&gt;2&lt;/span&gt; &lt;span style="color: #000000"&gt;{
&lt;/span&gt;&lt;span style="color: #008080"&gt;3&lt;/span&gt; &lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (idHookMouse &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;)
&lt;/span&gt;&lt;span style="color: #008080"&gt;4&lt;/span&gt; &lt;span style="color: #000000"&gt;        NativeMethods.UnhookWindowsHookEx(idHookMouse);
&lt;/span&gt;&lt;span style="color: #008080"&gt;5&lt;/span&gt; &lt;span style="color: #000000"&gt;
&lt;/span&gt;&lt;span style="color: #008080"&gt;6&lt;/span&gt; &lt;span style="color: #000000"&gt;    mouseHookProc &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;;
&lt;/span&gt;&lt;span style="color: #008080"&gt;7&lt;/span&gt; &lt;span style="color: #000000"&gt;    idHookMouse &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;;
&lt;/span&gt;&lt;span style="color: #008080"&gt;8&lt;/span&gt; &lt;span style="color: #000000"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These are the basis about how to build your own class to handle Mouse Hooks, if you want you can download the full source code from &lt;a href="http://blogs.clearscreen.com/jfbonnin/files/HookKeyboardAndMouse.zip" target="_blank"&gt;here&lt;/a&gt;, which shows you how to do the same with the Keyboard. &lt;/p&gt;
&lt;p&gt;I hope you find interesting and remember that you can complete all the information in our bible: the MSDN.&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/jfbonnin/aggbug/6494.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>In the forums of <a href="http://www.baleareson.net/">Baleares on .NET</a> (Spanish) a member asked a general question about hooking functions, avoiding the easy joke about what hooking functions are, we could define them basically as a technique which allows putting your own code between the caller and the destination. </p> <p>One of the easiest and common hooks is the one to capture keyboard and mouse events, something that you can easily implement in .NET making use of InteropServices.  </p><p>We will see with a sample how to build a class that intercepts the mouse and keyboard messages and raises a .net event, which users can handle (see below to download the full source code).  </p><p>The first thing is to obtain the functions to install and remove the hooks, which we can find in the user32.dll. </p> <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color: white; word-wrap: break-word"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080">1</span> <span style="color: #000000">[DllImport(</span><span style="color: #800000">"</span><span style="color: #800000">user32.dll</span><span style="color: #800000">"</span><span style="color: #000000">, CharSet </span><span style="color: #000000">=</span><span style="color: #000000"> CharSet.Auto, CallingConvention </span><span style="color: #000000">=</span><span style="color: #000000"> CallingConvention.StdCall)]
</span><span style="color: #008080">2</span> <span style="color: #000000"></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">extern</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> SetWindowsHookEx(</span><span style="color: #0000ff">int</span><span style="color: #000000"> idHook, HookProc lpfn, IntPtr hMod, </span><span style="color: #0000ff">int</span><span style="color: #000000"> dwThreadId);
</span><span style="color: #008080">3</span> <span style="color: #000000">
</span><span style="color: #008080">4</span> <span style="color: #000000">[DllImport(</span><span style="color: #800000">"</span><span style="color: #800000">user32.dll</span><span style="color: #800000">"</span><span style="color: #000000">, CharSet </span><span style="color: #000000">=</span><span style="color: #000000"> CharSet.Auto, CallingConvention </span><span style="color: #000000">=</span><span style="color: #000000"> CallingConvention.StdCall)]
</span><span style="color: #008080">5</span> <span style="color: #000000"></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">extern</span><span style="color: #000000"> </span><span style="color: #0000ff">bool</span><span style="color: #000000"> UnhookWindowsHookEx(</span><span style="color: #0000ff">int</span><span style="color: #000000"> idHook);
</span><span style="color: #008080">6</span> <span style="color: #000000">
</span><span style="color: #008080">7</span> <span style="color: #000000">[DllImport(</span><span style="color: #800000">"</span><span style="color: #800000">user32.dll</span><span style="color: #800000">"</span><span style="color: #000000">, CharSet </span><span style="color: #000000">=</span><span style="color: #000000"> CharSet.Auto, CallingConvention </span><span style="color: #000000">=</span><span style="color: #000000"> CallingConvention.StdCall)]
</span><span style="color: #008080">8</span> <span style="color: #000000"></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">extern</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> CallNextHookEx(</span><span style="color: #0000ff">int</span><span style="color: #000000"> idHook, </span><span style="color: #0000ff">int</span><span style="color: #000000"> nCode, IntPtr wParam, IntPtr lParam);</span></div></pre></div>
<p>You can find detailed information about what every parameter is in the <a href="http://msdn2.microsoft.com/en-us/library/" target="_blank">MSDN</a>, but just as a fast review: idHook is the type of hook to install, lpfn is a pointer to our hook method, hMod is a handle to the DLL that will contain the method pointed by lpfn and finally dwThreadId, which specifies the identifer of the thread with which the hook method is to be associated. The type we have set to lpfn is HookProc, as you will see next, this is just a delegate with the next signature: public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);</p>
<p>Another important thing to stand up is the third DllImport, what we are going to do is to install and remove hooks from a hook chain, because of that it will be necessary to call the next installed hooks, otherwise we will prevent they are executed. </p>
<p>To install the hook the only thing we need to do is to call SetWiwnodwsHookEx with the rigth parameters.</p>
<p></p>
<div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color: white; word-wrap: break-word"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080"> 1</span> <span style="color: #000000">HookProc mouseHookProc </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> HookProc(MouseHookProc);
</span><span style="color: #008080"> 2</span> <span style="color: #000000">...
</span><span style="color: #008080"> 3</span> <span style="color: #000000"></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> InstallMouseHook()
</span><span style="color: #008080"> 4</span> <span style="color: #000000">{
</span><span style="color: #008080"> 5</span> <span style="color: #000000">    </span><span style="color: #000000"> idHookMouse </span><span style="color: #000000">=</span><span style="color: #000000"> NativeMethods.SetWindowsHookEx(NativeConstants.WH_MOUSE_LL, mouseHookProc, Marshal.GetHINSTANCE(</span><span style="color: #0000ff">this</span><span style="color: #000000">.GetType().Module), </span><span style="color: #800080">0</span><span style="color: #000000">);
</span><span style="color: #008080"> 6</span> <span style="color: #000000">    </span><span style="color: #0000ff">if</span><span style="color: #000000"> (idHookMouse </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">)
</span><span style="color: #008080"> 7</span> <span style="color: #000000">        </span><span style="color: #0000ff">throw</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> ApplicationException(</span><span style="color: #800000">"</span><span style="color: #800000">MouseHook cannot be set</span><span style="color: #800000">"</span><span style="color: #000000">);
</span><span style="color: #008080"> 8</span> <span style="color: #000000">}
</span><span style="color: #008080"> 9</span> <span style="color: #000000">...
</span><span style="color: #008080">10</span> <span style="color: #000000"></span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> MouseHookProc(</span><span style="color: #0000ff">int</span><span style="color: #000000"> nCode, IntPtr wParam, IntPtr lParam)
</span><span style="color: #008080">11</span> <span style="color: #000000">{
</span><span style="color: #008080">12</span> <span style="color: #000000">    </span><span style="color: #0000ff">if</span><span style="color: #000000"> (nCode </span><span style="color: #000000">&lt;</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">)
</span><span style="color: #008080">13</span> <span style="color: #000000">    {
</span><span style="color: #008080">14</span> <span style="color: #000000">        </span><span style="color: #0000ff">return</span><span style="color: #000000"> NativeMethods.CallNextHookEx(NativeConstants.WH_MOUSE_LL, nCode, wParam, lParam);
</span><span style="color: #008080">15</span> <span style="color: #000000">    }
</span><span style="color: #008080">16</span> <span style="color: #000000">    </span><span style="color: #0000ff">else</span><span style="color: #000000">
</span><span style="color: #008080">17</span> <span style="color: #000000">    {
</span><span style="color: #008080">18</span> <span style="color: #000000">        MSLLHOOKSTRUCT mouseHookStruct </span><span style="color: #000000">=</span><span style="color: #000000"> (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, </span><span style="color: #0000ff">typeof</span><span style="color: #000000">(MSLLHOOKSTRUCT));
</span><span style="color: #008080">19</span> <span style="color: #000000">
</span><span style="color: #008080">20</span> <span style="color: #000000">        HookMouseEventArgs args </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> HookMouseEventArgs();
</span><span style="color: #008080">21</span> <span style="color: #000000">        args.Point </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Point(mouseHookStruct.pt.x, mouseHookStruct.pt.y);
</span><span style="color: #008080">22</span> <span style="color: #000000">        args.Message </span><span style="color: #000000">=</span><span style="color: #000000"> wParam.ToInt32();
</span><span style="color: #008080">23</span> <span style="color: #000000">
</span><span style="color: #008080">24</span> <span style="color: #000000">        OnHookMouse(args);
</span><span style="color: #008080">25</span> <span style="color: #000000">
</span><span style="color: #008080">26</span> <span style="color: #000000">        </span><span style="color: #0000ff">return</span><span style="color: #000000"> NativeMethods.CallNextHookEx(NativeConstants.WH_MOUSE_LL, nCode, wParam, lParam);
</span><span style="color: #008080">27</span> <span style="color: #000000">    }
</span><span style="color: #008080">28</span> <span style="color: #000000">}
</span><span style="color: #008080">29</span> <span style="color: #000000">...
</span><span style="color: #008080">30</span> <span style="color: #000000">[StructLayout(LayoutKind.Sequential)]
</span><span style="color: #008080">31</span> <span style="color: #000000"></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> MSLLHOOKSTRUCT
</span><span style="color: #008080">32</span> <span style="color: #000000">{
</span><span style="color: #008080">33</span> <span style="color: #000000">    </span><span style="color: #0000ff">public</span><span style="color: #000000"> POINT pt;
</span><span style="color: #008080">34</span> <span style="color: #000000">    </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> mouseData;
</span><span style="color: #008080">35</span> <span style="color: #000000">    </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> flags;
</span><span style="color: #008080">36</span> <span style="color: #000000">    </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> time;
</span><span style="color: #008080">37</span> <span style="color: #000000">    </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> dwExtraInfo;
</span><span style="color: #008080">38</span> <span style="color: #000000">}</span></div></pre></div>
<p>In the excerpt of the above code we set a global hook to intercept all the messages of the mouse. When there is a message the method MouseHookProc is called and we will fire an event with the HookMouseEventArgs class as parameters, after that we take care of call the next hook in the chain. In the line 18 you can see how we convert the pointer lParam received as parameter to an struct of type MSLLHOOKSTRUCT. This class contains some valuable information like "mouseData" that represents the message type (left button down, mouse wheel ...), or "pt" that represents the point in which is the mouse.</p>
<p>The last thing you need to know is that you must remove the hook as soon as possible, since it's resource consuming. This can be accomplished as follows:</p>
<div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:635063e8-01fa-45b7-a760-e33b3667e6ed" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color: white; word-wrap: break-word"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080">1</span> <span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> RemoveMouseHook()
</span><span style="color: #008080">2</span> <span style="color: #000000">{
</span><span style="color: #008080">3</span> <span style="color: #000000">    </span><span style="color: #0000ff">if</span><span style="color: #000000"> (idHookMouse </span><span style="color: #000000">&gt;</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">)
</span><span style="color: #008080">4</span> <span style="color: #000000">        NativeMethods.UnhookWindowsHookEx(idHookMouse);
</span><span style="color: #008080">5</span> <span style="color: #000000">
</span><span style="color: #008080">6</span> <span style="color: #000000">    mouseHookProc </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">;
</span><span style="color: #008080">7</span> <span style="color: #000000">    idHookMouse </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">;
</span><span style="color: #008080">8</span> <span style="color: #000000">}</span></div></pre></div>
<p>These are the basis about how to build your own class to handle Mouse Hooks, if you want you can download the full source code from <a href="http://blogs.clearscreen.com/jfbonnin/files/HookKeyboardAndMouse.zip" target="_blank">here</a>, which shows you how to do the same with the Keyboard. </p>
<p>I hope you find interesting and remember that you can complete all the information in our bible: the MSDN.</p><img src ="http://blogs.clearscreen.com/jfbonnin/aggbug/6494.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Jose Fco. Bonnin</dc:creator><title>Registry Handling</title><link>http://blogs.clearscreen.com/jfbonnin/archive/2007/01/30/6015.aspx</link><pubDate>Tue, 30 Jan 2007 21:43:00 GMT</pubDate><guid>http://blogs.clearscreen.com/jfbonnin/archive/2007/01/30/6015.aspx</guid><wfw:comment>http://blogs.clearscreen.com/jfbonnin/comments/6015.aspx</wfw:comment><comments>http://blogs.clearscreen.com/jfbonnin/archive/2007/01/30/6015.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://blogs.clearscreen.com/jfbonnin/comments/commentRss/6015.aspx</wfw:commentRss><trackback:ping>http://blogs.clearscreen.com/jfbonnin/services/trackbacks/6015.aspx</trackback:ping><description>&lt;p&gt;These days I’ve been bothered by a virus that decided to live on my laptop. &lt;/p&gt;
&lt;p&gt;I’m not going to explain how it arrived there, because I get really mad, I’m just going to give a common sense suggestion: never, never let your laptop to other people, doesn’t matter how friends you are …. &lt;/p&gt;
&lt;p&gt;In any case there was an interesting part on all this, since the virus had nice things to learn which I’m going to try to reproduce using .NET: &lt;/p&gt;
&lt;p&gt;The virus introduced lot of keys in the registry; one of the most interesting was added to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify. Maybe in another post I will explain why this one is interesting, but first let’s try to add or modify registry keys from C#.&lt;/p&gt;
&lt;p&gt;The .NET Framework, once more, has done most of the work for us. We only need to take a look to the Microsoft.Win32 namespace and there we will find the classes "Registry" and "RegistryKey", which makes extremely easy to work with the Windows Registry. &lt;/p&gt;
&lt;p&gt;Take a look to the next code: &lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;

&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;string&lt;/span&gt; keyName &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;@"Software\Microsoft\Windows NT\CurrentVersion\WinLogon\Notify"&lt;/span&gt;;

RegistryKey notifyKey &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; Registry.LocalMachine.OpenSubKey(keyName, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;true&lt;/span&gt;); 

RegistryKey testKey &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; notifyKey.CreateSubKey(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"TestKey"&lt;/span&gt;); 

testKey.SetValue(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"Asynchronous"&lt;/span&gt;, 0x00000001, RegistryValueKind.DWord); 

testKey.SetValue(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"DllName"&lt;/span&gt;, &lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"FakeVirus.dll"&lt;/span&gt;, RegistryValueKind.String); 

testKey.SetValue(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"Impersonate"&lt;/span&gt;, 0x00000000, RegistryValueKind.DWord); 

testKey.SetValue(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"Logoff"&lt;/span&gt;, &lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"Logoff"&lt;/span&gt;, RegistryValueKind.String); 

testKey.SetValue(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"Logon"&lt;/span&gt;, &lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"Logon"&lt;/span&gt;, RegistryValueKind.String); 
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;As you already deducted we have created a new sub key called TestKey and introduced in it 5 different values, Asynchronous, DllName, Impersonate … very easy, isn't it?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;With the virus I wasn’t able to delete the registry entries added because they were regenerated immediately after any kind of change. This was done monitoring the entries, something that cannot be done directly with the managed classes but Windows supports through its API. &lt;/p&gt;
&lt;p&gt;Reproduce this mechanism is not a big problem since even if .NET doesn’t have an equivalent method we can use p/invoke (Platform Invoke) to use the unmanaged functions of the Win32 API. For more information about it you can check the next link: &lt;a href="http://msdn2.microsoft.com/en-us/library/ms724892.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms724892.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I add here the most interesting part of the code in order you can build your own monitor:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;

&lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;// P/Invoke methods and constants &lt;/span&gt;

&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;class&lt;/span&gt; NativeMethods
{
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;private&lt;/span&gt; NativeMethods()
   {
   } 

   [DllImport(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"advapi32.dll"&lt;/span&gt;, SetLastError &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;true&lt;/span&gt;)]
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;extern&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; RegNotifyChangeKeyValue(
      IntPtr hKey,
      [MarshalAs(UnmanagedType.Bool)] 
      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;bool&lt;/span&gt; watchSubtree,
      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; dwNotifyFilter, 
      IntPtr hEvent, 
      [MarshalAs(UnmanagedType.Bool)] 
      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;bool&lt;/span&gt; fAsynchronous 

   ); 

   [DllImport(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"advapi32.dll"&lt;/span&gt;, SetLastError &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;true&lt;/span&gt;)]
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;extern&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; RegOpenKeyEx(
      IntPtr hKey,
      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;string&lt;/span&gt; subKey,
      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;uint&lt;/span&gt; options,
      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; sam,
      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;out&lt;/span&gt; IntPtr phkResult 

   ); 

   [DllImport(&lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"advapi32.dll"&lt;/span&gt;, SetLastError &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;true&lt;/span&gt;)]
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;extern&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; RegCloseKey(
      IntPtr hKey 

   ); 

   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; HKEY_CLASSES_ROOT &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;unchecked&lt;/span&gt;((&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt;)0x80000000);
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; HKEY_CURRENT_USER &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;unchecked&lt;/span&gt;((&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt;)0x80000001);
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; HKEY_LOCAL_MACHINE &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;unchecked&lt;/span&gt;((&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt;)0x80000002);
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; HKEY_USERS &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;unchecked&lt;/span&gt;((&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt;)0x80000003);
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; HKEY_PERFORMANCE_DATA= &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;unchecked&lt;/span&gt;((&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt;)0x80000004);
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; HKEY_CURRENT_CONFIG &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;unchecked&lt;/span&gt;((&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt;)0x80000005);
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; HKEY_DYN_DATA &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;unchecked&lt;/span&gt;((&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt;)0x80000006); 

   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; KEY_NOTIFY &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; 0x0010; 

   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; REG_NOTIFY_CHANGE_NAME &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; 0x00000001;
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; REG_NOTIFY_CHANGE_ATTRIBUTES &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; 0x00000002;
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; REG_NOTIFY_CHANGE_LAST_SET &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; 0x00000004;
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;internal&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;const&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; REG_NOTIFY_CHANGE_SECURITY &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; 0x00000008;
} 

.................................................................................................................... 

IntPtr parentKey &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;new&lt;/span&gt; IntPtr(NativeMethods.HKEY_LOCAL_MACHINE); 

&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;string&lt;/span&gt; subKey &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px"&gt;"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify\TestKey"&lt;/span&gt; 

IntPtr monitoredKey; 

&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;try&lt;/span&gt; 

{ 

   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;int&lt;/span&gt; ret &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; NativeMethods.RegOpenKeyEx(parentKey, subKey, 0, NativeMethods.KEY_NOTIFY, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;out&lt;/span&gt; monitoredKey);
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;if&lt;/span&gt; (ret !&lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; 0)
   {
      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;throw&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;new&lt;/span&gt; Win32Exception(ret);
   } 

   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;using&lt;/span&gt; (AutoResetEvent monitorEvent &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;new&lt;/span&gt; AutoResetEvent(&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;false&lt;/span&gt;))
   {
      ret &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; NativeMethods.RegNotifyChangeKeyValue(parentKey, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;true&lt;/span&gt;, 
                        NativeMethods.REG_NOTIFY_CHANGE_NAME &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;|&lt;/span&gt; NativeMethods.REG_NOTIFY_CHANGE_ATTRIBUTES &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;|&lt;/span&gt; 
                        NativeMethods.REG_NOTIFY_CHANGE_LAST_SET &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;|&lt;/span&gt; NativeMethods.REG_NOTIFY_CHANGE_SECURITY, 
                        monitorEvent.Handle, &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;true&lt;/span&gt;); 

      &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;if&lt;/span&gt; (ret !&lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; 0)
      {
         &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;throw&lt;/span&gt; &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;new&lt;/span&gt; Win32Exception(ret);
      }  

      monitorEvent.WaitOne();
      &lt;span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;// Do something after the registry has changed &lt;/span&gt;

   } 

} 

&lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;finally&lt;/span&gt;
{
   parentKey &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; IntPtr.Zero;
   &lt;span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;if&lt;/span&gt; (monitoredKey !&lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; IntPtr.Zero)
   {
      NativeMethods.RegCloseKey(monitoredKey);
      monitoredKey &lt;span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px"&gt;=&lt;/span&gt; IntPtr.Zero;
   } 

} 
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Nice, don't you think?&lt;/p&gt;&lt;img src ="http://blogs.clearscreen.com/jfbonnin/aggbug/6015.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>These days I’ve been bothered by a virus that decided to live on my laptop. </p>
<p>I’m not going to explain how it arrived there, because I get really mad, I’m just going to give a common sense suggestion: never, never let your laptop to other people, doesn’t matter how friends you are …. </p>
<p>In any case there was an interesting part on all this, since the virus had nice things to learn which I’m going to try to reproduce using .NET: </p>
<p>The virus introduced lot of keys in the registry; one of the most interesting was added to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify. Maybe in another post I will explain why this one is interesting, but first let’s try to add or modify registry keys from C#.</p>
<p>The .NET Framework, once more, has done most of the work for us. We only need to take a look to the Microsoft.Win32 namespace and there we will find the classes "Registry" and "RegistryKey", which makes extremely easy to work with the Windows Registry. </p>
<p>Take a look to the next code: </p>
<pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px">

<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">string</span> keyName <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">@"Software\Microsoft\Windows NT\CurrentVersion\WinLogon\Notify"</span>;

RegistryKey notifyKey <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> Registry.LocalMachine.OpenSubKey(keyName, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">true</span>); 

RegistryKey testKey <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> notifyKey.CreateSubKey(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"TestKey"</span>); 

testKey.SetValue(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"Asynchronous"</span>, 0x00000001, RegistryValueKind.DWord); 

testKey.SetValue(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"DllName"</span>, <span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"FakeVirus.dll"</span>, RegistryValueKind.String); 

testKey.SetValue(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"Impersonate"</span>, 0x00000000, RegistryValueKind.DWord); 

testKey.SetValue(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"Logoff"</span>, <span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"Logoff"</span>, RegistryValueKind.String); 

testKey.SetValue(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"Logon"</span>, <span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"Logon"</span>, RegistryValueKind.String); 
</span></pre>
<p>As you already deducted we have created a new sub key called TestKey and introduced in it 5 different values, Asynchronous, DllName, Impersonate … very easy, isn't it?</p>
<p> </p>
<p>With the virus I wasn’t able to delete the registry entries added because they were regenerated immediately after any kind of change. This was done monitoring the entries, something that cannot be done directly with the managed classes but Windows supports through its API. </p>
<p>Reproduce this mechanism is not a big problem since even if .NET doesn’t have an equivalent method we can use p/invoke (Platform Invoke) to use the unmanaged functions of the Win32 API. For more information about it you can check the next link: <a href="http://msdn2.microsoft.com/en-us/library/ms724892.aspx">http://msdn2.microsoft.com/en-us/library/ms724892.aspx</a></p>
<p>I add here the most interesting part of the code in order you can build your own monitor:</p>
<pre><span style="color: Black; background-color: Transparent; font-family: Courier New; font-size: 11px">

<span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px">// P/Invoke methods and constants </span>

<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">class</span> NativeMethods
{
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">private</span> NativeMethods()
   {
   } 

   [DllImport(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"advapi32.dll"</span>, SetLastError <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">true</span>)]
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">static</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">extern</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> RegNotifyChangeKeyValue(
      IntPtr hKey,
      [MarshalAs(UnmanagedType.Bool)] 
      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">bool</span> watchSubtree,
      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> dwNotifyFilter, 
      IntPtr hEvent, 
      [MarshalAs(UnmanagedType.Bool)] 
      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">bool</span> fAsynchronous 

   ); 

   [DllImport(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"advapi32.dll"</span>, SetLastError <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">true</span>)]
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">static</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">extern</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> RegOpenKeyEx(
      IntPtr hKey,
      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">string</span> subKey,
      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">uint</span> options,
      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> sam,
      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">out</span> IntPtr phkResult 

   ); 

   [DllImport(<span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"advapi32.dll"</span>, SetLastError <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">true</span>)]
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">static</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">extern</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> RegCloseKey(
      IntPtr hKey 

   ); 

   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> HKEY_CLASSES_ROOT <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">unchecked</span>((<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span>)0x80000000);
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> HKEY_CURRENT_USER <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">unchecked</span>((<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span>)0x80000001);
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> HKEY_LOCAL_MACHINE <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">unchecked</span>((<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span>)0x80000002);
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> HKEY_USERS <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">unchecked</span>((<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span>)0x80000003);
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> HKEY_PERFORMANCE_DATA= <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">unchecked</span>((<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span>)0x80000004);
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> HKEY_CURRENT_CONFIG <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">unchecked</span>((<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span>)0x80000005);
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> HKEY_DYN_DATA <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">unchecked</span>((<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span>)0x80000006); 

   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> KEY_NOTIFY <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> 0x0010; 

   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> REG_NOTIFY_CHANGE_NAME <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> 0x00000001;
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> REG_NOTIFY_CHANGE_ATTRIBUTES <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> 0x00000002;
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> REG_NOTIFY_CHANGE_LAST_SET <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> 0x00000004;
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">internal</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">const</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> REG_NOTIFY_CHANGE_SECURITY <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> 0x00000008;
} 

.................................................................................................................... 

IntPtr parentKey <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">new</span> IntPtr(NativeMethods.HKEY_LOCAL_MACHINE); 

<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">string</span> subKey <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: #666666; background-color: #e4e4e4; font-family: Courier New; font-size: 11px">"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify\TestKey"</span> 

IntPtr monitoredKey; 

<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">try</span> 

{ 

   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">int</span> ret <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> NativeMethods.RegOpenKeyEx(parentKey, subKey, 0, NativeMethods.KEY_NOTIFY, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">out</span> monitoredKey);
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">if</span> (ret !<span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> 0)
   {
      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">throw</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">new</span> Win32Exception(ret);
   } 

   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">using</span> (AutoResetEvent monitorEvent <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">new</span> AutoResetEvent(<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">false</span>))
   {
      ret <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> NativeMethods.RegNotifyChangeKeyValue(parentKey, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">true</span>, 
                        NativeMethods.REG_NOTIFY_CHANGE_NAME <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">|</span> NativeMethods.REG_NOTIFY_CHANGE_ATTRIBUTES <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">|</span> 
                        NativeMethods.REG_NOTIFY_CHANGE_LAST_SET <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">|</span> NativeMethods.REG_NOTIFY_CHANGE_SECURITY, 
                        monitorEvent.Handle, <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">true</span>); 

      <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">if</span> (ret !<span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> 0)
      {
         <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">throw</span> <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">new</span> Win32Exception(ret);
      }  

      monitorEvent.WaitOne();
      <span style="color: Green; background-color: Transparent; font-family: Courier New; font-size: 11px">// Do something after the registry has changed </span>

   } 

} 

<span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">finally</span>
{
   parentKey <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> IntPtr.Zero;
   <span style="color: Blue; background-color: Transparent; font-family: Courier New; font-size: 11px">if</span> (monitoredKey !<span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> IntPtr.Zero)
   {
      NativeMethods.RegCloseKey(monitoredKey);
      monitoredKey <span style="color: Red; background-color: Transparent; font-family: Courier New; font-size: 11px">=</span> IntPtr.Zero;
   } 

} 
</span></pre>
<p> </p>
<p>Nice, don't you think?</p><img src ="http://blogs.clearscreen.com/jfbonnin/aggbug/6015.aspx" width = "1" height = "1" /></body></item></channel></rss>