<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>IIrrelevant - Firewall</title>
    <link>http://www.milkcarton.com/blog/</link>
    <description>Irrelevant musings about software development</description>
    <language>en-us</language>
    <copyright>Dan Morphis</copyright>
    <lastBuildDate>Mon, 08 Oct 2007 02:50:55 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@milkcarton.com</managingEditor>
    <webMaster>blog@milkcarton.com</webMaster>
    <item>
      <trackback:ping>http://www.milkcarton.com/blog/Trackback.aspx?guid=f1de834d-6c68-4cdf-bbd3-caa00629d883</trackback:ping>
      <pingback:server>http://www.milkcarton.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.milkcarton.com/blog/PermaLink,guid,f1de834d-6c68-4cdf-bbd3-caa00629d883.aspx</pingback:target>
      <dc:creator>Dan Morphis</dc:creator>
      <wfw:comment>http://www.milkcarton.com/blog/CommentView,guid,f1de834d-6c68-4cdf-bbd3-caa00629d883.aspx</wfw:comment>
      <wfw:commentRss>http://www.milkcarton.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=f1de834d-6c68-4cdf-bbd3-caa00629d883</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you've read <a href="http://blogs.msdn.com/oldnewthing/">Raymond Chen's blog</a> long
enough, then you know trying to change system stuff directly in Windows registry
is discouraged, if not frowned upon.  So when I kept hacking away at the registry
trying to get some Windows Firewall exceptions for XP and Vista created, I decided
to take a step back and see what Windows's API's are out there to do this.
</p>
        <p>
Doing some Google searches doesn't reveal much (which is why I decided to blog this),
except these two hidden gems <a href="http://www.codeproject.com/cs/internet/Syslogd.asp">Syslog
daemon for Windows Eventlog</a>, and <a href="http://www.developmentnow.com/g/36_2004_12_0_0_31409/Adding-a-port-to-the-XP-Firewall.htm">Adding
a port to the XP Firewall</a>.  Both of these gave me pointers in the right direction
to create this gem:
</p>
        <pre>
          <span style="color: teal"> 1</span>
          <span style="color: blue">private</span>
          <span style="color: blue">static</span>
          <span style="color: blue">void</span> ExceptionToFirewall(<span style="color: blue">bool</span> add, <span style="color: blue">string</span> imageFileName, <span style="color: blue">string</span> name) <span style="color: teal"> 2</span> { <span style="color: teal"> 3</span> Type
netFwMgrType = Type.GetTypeFromProgID(<span style="color: maroon">"HNetCfg.FwMgr"</span>); <span style="color: teal"> 4</span> INetFwMgr
mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); <span style="color: teal"> 5</span><span style="color: teal"> 6</span> INetFwProfile
curProfile = mgr.LocalPolicy.CurrentProfile; <span style="color: teal"> 7</span><span style="color: blue">if</span> (add) <span style="color: teal"> 8</span> { <span style="color: teal"> 9</span> Type
NetFwAuthorizedApplicationType = Type.GetTypeFromProgID(<span style="color: maroon">"HNetCfg.FwAuthorizedApplication"</span>, <span style="color: maroon">false</span>); <span style="color: teal"> 10</span> INetFwAuthorizedApplication
app = (INetFwAuthorizedApplication)Activator.CreateInstance(NetFwAuthorizedApplicationType); <span style="color: teal"> 11</span><span style="color: teal"> 12</span> app.Name
= name; <span style="color: teal"> 13</span> app.ProcessImageFileName = imageFileName; <span style="color: teal"> 14</span> app.Enabled
= <span style="color: maroon">true</span>; <span style="color: teal"> 15</span> app.RemoteAddresses
= <span style="color: maroon">"*"</span>; <span style="color: teal"> 16</span> app.Scope
= NET_FW_SCOPE_.NET_FW_SCOPE_ALL; <span style="color: teal"> 17</span><span style="color: teal"> 18</span> curProfile.AuthorizedApplications.Add(app); <span style="color: teal"> 19</span> } <span style="color: teal"> 20</span><span style="color: blue">else</span><span style="color: teal"> 21</span> { <span style="color: teal"> 22</span> curProfile.AuthorizedApplications.Remove(imageFileName); <span style="color: teal"> 23</span> } <span style="color: teal"> 24</span> }</pre>
        <pre class="code"> </pre>
        <p>
To use this, you'll need to add a reference to COM component HNetCfg.FwMgr (Guid "{304CE942-6E39-40D8-943A-B913C40C9CD4}", file
path C:\windows\system32\hnetcfg.dll).
</p>
        <p>
One note, don't use the IpVersion property of INetFwAuthorizedApplication, under Windows
Vista it throws a NotImplimentedException.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2007%2f10%2f08%2fWindows%2bFirewall%2bExceptions.aspx">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2007%2f10%2f08%2fWindows%2bFirewall%2bExceptions.aspx" border="0" />
          </a>  
</p>
        <div class="wlWriterSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:b8ea0826-2453-4df0-abd7-4f69de44893a" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">del.icio.us
Tags: <a href="http://del.icio.us/popular/C#" rel="tag">C#</a>, <a href="http://del.icio.us/popular/Windows" rel="tag">Windows</a>, <a href="http://del.icio.us/popular/Firewall" rel="tag">Firewall</a></div>
        <img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=f1de834d-6c68-4cdf-bbd3-caa00629d883" />
      </body>
      <title>Creating Windows Firewall Exceptions</title>
      <guid isPermaLink="false">http://www.milkcarton.com/blog/PermaLink,guid,f1de834d-6c68-4cdf-bbd3-caa00629d883.aspx</guid>
      <link>http://www.milkcarton.com/blog/2007/10/08/Creating+Windows+Firewall+Exceptions.aspx</link>
      <pubDate>Mon, 08 Oct 2007 02:50:55 GMT</pubDate>
      <description>&lt;p&gt;
If you've read &lt;a href="http://blogs.msdn.com/oldnewthing/"&gt;Raymond Chen's blog&lt;/a&gt; long
enough, then you know&amp;nbsp;trying to change system stuff directly in Windows registry
is discouraged, if not frowned upon.&amp;nbsp; So when I kept hacking away at the registry
trying to get some Windows Firewall exceptions for XP and Vista created, I decided
to take a step back and see what Windows's API's are out there to&amp;nbsp;do this.
&lt;/p&gt;
&lt;p&gt;
Doing some Google searches doesn't reveal much (which is why I decided to blog this),
except these two&amp;nbsp;hidden&amp;nbsp;gems &lt;a href="http://www.codeproject.com/cs/internet/Syslogd.asp"&gt;Syslog
daemon for Windows Eventlog&lt;/a&gt;, and &lt;a href="http://www.developmentnow.com/g/36_2004_12_0_0_31409/Adding-a-port-to-the-XP-Firewall.htm"&gt;Adding
a port to the XP Firewall&lt;/a&gt;.&amp;nbsp; Both of these gave me pointers in the right direction
to create this gem:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: teal"&gt; 1&lt;/span&gt; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ExceptionToFirewall(&lt;span style="color: blue"&gt;bool&lt;/span&gt; add, &lt;span style="color: blue"&gt;string&lt;/span&gt; imageFileName, &lt;span style="color: blue"&gt;string&lt;/span&gt; name) &lt;span style="color: teal"&gt; 2&lt;/span&gt; { &lt;span style="color: teal"&gt; 3&lt;/span&gt; Type
netFwMgrType = Type.GetTypeFromProgID(&lt;span style="color: maroon"&gt;"HNetCfg.FwMgr"&lt;/span&gt;); &lt;span style="color: teal"&gt; 4&lt;/span&gt; INetFwMgr
mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); &lt;span style="color: teal"&gt; 5&lt;/span&gt; &lt;span style="color: teal"&gt; 6&lt;/span&gt; INetFwProfile
curProfile = mgr.LocalPolicy.CurrentProfile; &lt;span style="color: teal"&gt; 7&lt;/span&gt; &lt;span style="color: blue"&gt;if&lt;/span&gt; (add) &lt;span style="color: teal"&gt; 8&lt;/span&gt; { &lt;span style="color: teal"&gt; 9&lt;/span&gt; Type
NetFwAuthorizedApplicationType = Type.GetTypeFromProgID(&lt;span style="color: maroon"&gt;"HNetCfg.FwAuthorizedApplication"&lt;/span&gt;, &lt;span style="color: maroon"&gt;false&lt;/span&gt;); &lt;span style="color: teal"&gt; 10&lt;/span&gt; INetFwAuthorizedApplication
app = (INetFwAuthorizedApplication)Activator.CreateInstance(NetFwAuthorizedApplicationType); &lt;span style="color: teal"&gt; 11&lt;/span&gt; &lt;span style="color: teal"&gt; 12&lt;/span&gt; app.Name
= name; &lt;span style="color: teal"&gt; 13&lt;/span&gt; app.ProcessImageFileName = imageFileName; &lt;span style="color: teal"&gt; 14&lt;/span&gt; app.Enabled
= &lt;span style="color: maroon"&gt;true&lt;/span&gt;; &lt;span style="color: teal"&gt; 15&lt;/span&gt; app.RemoteAddresses
= &lt;span style="color: maroon"&gt;"*"&lt;/span&gt;; &lt;span style="color: teal"&gt; 16&lt;/span&gt; app.Scope
= NET_FW_SCOPE_.NET_FW_SCOPE_ALL; &lt;span style="color: teal"&gt; 17&lt;/span&gt; &lt;span style="color: teal"&gt; 18&lt;/span&gt; curProfile.AuthorizedApplications.Add(app); &lt;span style="color: teal"&gt; 19&lt;/span&gt; } &lt;span style="color: teal"&gt; 20&lt;/span&gt; &lt;span style="color: blue"&gt;else&lt;/span&gt; &lt;span style="color: teal"&gt; 21&lt;/span&gt; { &lt;span style="color: teal"&gt; 22&lt;/span&gt; curProfile.AuthorizedApplications.Remove(imageFileName); &lt;span style="color: teal"&gt; 23&lt;/span&gt; } &lt;span style="color: teal"&gt; 24&lt;/span&gt; }&lt;/pre&gt;&lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;
To use this, you'll need to add a reference to COM component HNetCfg.FwMgr (Guid "{304CE942-6E39-40D8-943A-B913C40C9CD4}",&amp;nbsp;file
path&amp;nbsp;C:\windows\system32\hnetcfg.dll).
&lt;/p&gt;
&lt;p&gt;
One note, don't use the IpVersion property of INetFwAuthorizedApplication, under Windows
Vista it throws a NotImplimentedException.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2007%2f10%2f08%2fWindows%2bFirewall%2bExceptions.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.milkcarton.com%2fblog%2f2007%2f10%2f08%2fWindows%2bFirewall%2bExceptions.aspx" border="0"&gt;&lt;/a&gt;&amp;nbsp; 
&lt;div class="wlWriterSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:b8ea0826-2453-4df0-abd7-4f69de44893a" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;del.icio.us
Tags: &lt;a href="http://del.icio.us/popular/C#" rel="tag"&gt;C#&lt;/a&gt;, &lt;a href="http://del.icio.us/popular/Windows" rel="tag"&gt;Windows&lt;/a&gt;, &lt;a href="http://del.icio.us/popular/Firewall" rel="tag"&gt;Firewall&lt;/a&gt;
&lt;/div&gt;
&gt;
&lt;img width="0" height="0" src="http://www.milkcarton.com/blog/aggbug.ashx?id=f1de834d-6c68-4cdf-bbd3-caa00629d883" /&gt;</description>
      <comments>http://www.milkcarton.com/blog/CommentView,guid,f1de834d-6c68-4cdf-bbd3-caa00629d883.aspx</comments>
      <category>C#</category>
      <category>Firewall</category>
      <category>Windows</category>
    </item>
  </channel>
</rss>