<?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>Syncio Blog - VB.NET</title>
    <link>http://sync-io.net/go/blog/</link>
    <description>fixitchris@twitter</description>
    <language>en-us</language>
    <copyright>Chris Misztur</copyright>
    <lastBuildDate>Wed, 16 Jul 2008 15:21:58 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>chris@sync-io.net</managingEditor>
    <webMaster>chris@sync-io.net</webMaster>
    <item>
      <trackback:ping>http://sync-io.net/go/blog/Trackback.aspx?guid=d5a63229-e712-42ec-a294-770972c12b7b</trackback:ping>
      <pingback:server>http://sync-io.net/go/blog/pingback.aspx</pingback:server>
      <pingback:target>http://sync-io.net/go/blog/PermaLink,guid,d5a63229-e712-42ec-a294-770972c12b7b.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://sync-io.net/go/blog/CommentView,guid,d5a63229-e712-42ec-a294-770972c12b7b.aspx</wfw:comment>
      <wfw:commentRss>http://sync-io.net/go/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d5a63229-e712-42ec-a294-770972c12b7b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Otto's recent post on <a href="http://blogs.technet.com/otto/archive/2007/11/09/find-the-event-that-triggered-your-task.aspx">event
log triggers</a> explains how to: start a program, send an email or show a message
on the occurence of an event in the Event Log.  This is a great step forward,
however, some might feel that it is not enough to deploy a large scale logging and
notification environment with a focus on autonomy. 
</p>
        <p>
The alternative is to develop a piece of code that will subscribe to the <strong>__InstanceCreationEvent</strong> of
the <strong>Forwarded Events</strong> log.  The obstacle to using the existing <strong>System.Diagnostics.Eventlog</strong> class
is that the class does not interface with the <a href="http://msdn.microsoft.com/en-us/library/aa385225(VS.85).aspx">channeled
structure of the new Event Log</a> in Vista/2008.  (<em>The Forwarded Event log
is not visible to Eventlog.GetEventLogs() because it is a channel.</em>)  
</p>
        <p>
The following steps rectify this shortcoming:
</p>
        <address>1.   Create an overlapping ForwardedEvents classic log.
</address>
        <address>
          <font size="2">      <font face="Courier New">EventLog.CreateEventSource("ForwardedEvents",
"ForwardedEvents")</font></font>
        </address>
        <address>
          <font face="Courier New">
          </font> 
</address>
        <address>2.   Export and Remove ForwardedEvents channel from registry.
</address>
        <address>      <font face="Courier New">HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Channels\ForwardedEvents</font></address>
        <address> 
</address>
        <address>3.   Recreate the Source Initiated subscription on the server.   
</address>
        <address> 
</address>
        <address>4.   Restart the Windows Event Collector service on the server.
</address>
        <address> 
</address>
        <address>5.   Restart the WINRM service on the client.
</address>
        <address> 
</address>
        <p>
          <font size="2">If everything worked, you should see incoming events in the <strong>ForwardedEvents</strong> log. 
Notice that the log name does not contain a space anymore.  This was done to
match the full name of our new event log to that of the removed channel.</font>
        </p>
        <p>
          <font size="2">
            <strong>ForwardedEvents</strong> log will now be part of the <strong>Eventlog.GetEventLogs()</strong> array. 
You can deploy a <strong>ManagementEventWatcher</strong> to successfully intercept,
parse and take action on incoming events.
</font>
        </p>
        <font color="#a31515" size="2">
          <font color="#a31515" size="2">
          </font>
        </font>
        <img width="0" height="0" src="http://sync-io.net/go/blog/aggbug.ashx?id=d5a63229-e712-42ec-a294-770972c12b7b" />
      </body>
      <title>EventCollector: Intercepting the Forwarded Events log with System.Diagnostics.Eventlog class (Post 2)</title>
      <guid isPermaLink="false">http://sync-io.net/go/blog/PermaLink,guid,d5a63229-e712-42ec-a294-770972c12b7b.aspx</guid>
      <link>http://sync-io.net/go/blog/2008/07/16/EventCollectorInterceptingTheForwardedEventsLogWithSystemDiagnosticsEventlogClassPost2.aspx</link>
      <pubDate>Wed, 16 Jul 2008 15:21:58 GMT</pubDate>
      <description>&lt;p&gt;
Otto's recent post on &lt;a href="http://blogs.technet.com/otto/archive/2007/11/09/find-the-event-that-triggered-your-task.aspx"&gt;event
log triggers&lt;/a&gt;&amp;nbsp;explains how to: start a program, send an email or show a message
on the occurence of an event in the Event Log.&amp;nbsp; This is a great step forward,
however, some might feel that it is not enough to deploy a large scale logging and
notification environment with a&amp;nbsp;focus on autonomy.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
The alternative is to develop a piece of code that will subscribe to the &lt;strong&gt;__InstanceCreationEvent&lt;/strong&gt; of
the &lt;strong&gt;Forwarded Events&lt;/strong&gt; log.&amp;nbsp; The obstacle to using the existing &lt;strong&gt;System.Diagnostics.Eventlog&lt;/strong&gt; class
is that the class does not interface with the &lt;a href="http://msdn.microsoft.com/en-us/library/aa385225(VS.85).aspx"&gt;channeled
structure of the new Event Log&lt;/a&gt; in Vista/2008.&amp;nbsp; (&lt;em&gt;The Forwarded Event log
is not visible to Eventlog.GetEventLogs() because it is a channel.&lt;/em&gt;)&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
The following steps rectify this shortcoming:
&lt;/p&gt;
&lt;address&gt;1.&amp;nbsp;&amp;nbsp;&amp;nbsp;Create an overlapping ForwardedEvents classic log.
&lt;/address&gt;
&lt;address&gt;&lt;font size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font face="Courier New"&gt;EventLog.CreateEventSource("ForwardedEvents",
"ForwardedEvents")&lt;/font&gt;&lt;/font&gt;
&lt;/address&gt;
&lt;address&gt;&lt;font face="Courier New"&gt;&lt;/font&gt;&amp;nbsp;
&lt;/address&gt;
&lt;address&gt;2.&amp;nbsp;&amp;nbsp;&amp;nbsp;Export and Remove ForwardedEvents channel from registry.
&lt;/address&gt;
&lt;address&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font face="Courier New"&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Channels\ForwardedEvents&lt;/font&gt;
&lt;/address&gt;
&lt;address&gt;&amp;nbsp;
&lt;/address&gt;
&lt;address&gt;3.&amp;nbsp;&amp;nbsp;&amp;nbsp;Recreate the Source Initiated subscription on the server.&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/address&gt;
&lt;address&gt;&amp;nbsp;
&lt;/address&gt;
&lt;address&gt;4.&amp;nbsp;&amp;nbsp;&amp;nbsp;Restart the Windows Event Collector service on the server.
&lt;/address&gt;
&lt;address&gt;&amp;nbsp;
&lt;/address&gt;
&lt;address&gt;5.&amp;nbsp;&amp;nbsp;&amp;nbsp;Restart the WINRM service on the client.
&lt;/address&gt;
&lt;address&gt;&amp;nbsp;
&lt;/address&gt;
&lt;p&gt;
&lt;font size=2&gt;If everything worked, you should see incoming events in the &lt;strong&gt;ForwardedEvents&lt;/strong&gt; log.&amp;nbsp;
Notice that the log name does not contain a space anymore.&amp;nbsp; This was done to
match the full name of our new event log to that of the removed channel.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=2&gt;&lt;strong&gt;ForwardedEvents&lt;/strong&gt; log will now be part of the&amp;nbsp;&lt;strong&gt;Eventlog.GetEventLogs()&lt;/strong&gt; array.&amp;nbsp;
You can deploy a &lt;strong&gt;ManagementEventWatcher&lt;/strong&gt; to successfully intercept,
parse and take action on incoming events.
&lt;/p&gt;
&gt;&lt;font color=#a31515 size=2&gt;&lt;font color=#a31515 size=2&gt;&lt;/font&gt;&lt;/font&gt;&lt;img width="0" height="0" src="http://sync-io.net/go/blog/aggbug.ashx?id=d5a63229-e712-42ec-a294-770972c12b7b" /&gt;</description>
      <comments>http://sync-io.net/go/blog/CommentView,guid,d5a63229-e712-42ec-a294-770972c12b7b.aspx</comments>
      <category>Logging</category>
      <category>Server2008</category>
      <category>VB.NET</category>
    </item>
    <item>
      <trackback:ping>http://sync-io.net/go/blog/Trackback.aspx?guid=2e4ee447-abe3-4ba5-98c0-f8a8d1876179</trackback:ping>
      <pingback:server>http://sync-io.net/go/blog/pingback.aspx</pingback:server>
      <pingback:target>http://sync-io.net/go/blog/PermaLink,guid,2e4ee447-abe3-4ba5-98c0-f8a8d1876179.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://sync-io.net/go/blog/CommentView,guid,2e4ee447-abe3-4ba5-98c0-f8a8d1876179.aspx</wfw:comment>
      <wfw:commentRss>http://sync-io.net/go/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=2e4ee447-abe3-4ba5-98c0-f8a8d1876179</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I recently came across MalwareDomains.com.  They provide a list of domain names
that have been associated with malware.  The list contains over 21000 valid entries
and I have decided to integrate it with ISA 2006 since it's rare to find such a free
resource that is actually kept up to date.
</p>
        <p>
The import tool is available here: <a href="http://sync-io.net/IsaTools.aspx">http://sync-io.net/IsaTools.aspx</a><a href="http://sync-io.net/Public/ISA_MalwareDomains_BETA.zip" target="_blank" rel="nofollow"><span class="yshortcuts" id="lw_1208912100_0"></span></a></p>
        <p>
Basically, you're able to import the domain.txt file into ISA as a URL or DNS set. 
Then you just set your deny access rule and away you go.  This utility can run
as a scheduled task.
</p>
        <img width="0" height="0" src="http://sync-io.net/go/blog/aggbug.ashx?id=2e4ee447-abe3-4ba5-98c0-f8a8d1876179" />
      </body>
      <title>Stopping Malware with ISA 2006</title>
      <guid isPermaLink="false">http://sync-io.net/go/blog/PermaLink,guid,2e4ee447-abe3-4ba5-98c0-f8a8d1876179.aspx</guid>
      <link>http://sync-io.net/go/blog/2008/04/23/StoppingMalwareWithISA2006.aspx</link>
      <pubDate>Wed, 23 Apr 2008 01:09:16 GMT</pubDate>
      <description>&lt;p&gt;
I recently came across MalwareDomains.com.&amp;nbsp; They provide a list of domain names
that have been associated with malware.&amp;nbsp; The list contains over 21000 valid entries
and I have decided to integrate it with ISA 2006 since it's rare to find such a free
resource that is actually kept up to date.
&lt;/p&gt;
&lt;p&gt;
The import tool is available here: &lt;a href="http://sync-io.net/IsaTools.aspx"&gt;http://sync-io.net/IsaTools.aspx&lt;/a&gt;&lt;a href="http://sync-io.net/Public/ISA_MalwareDomains_BETA.zip" target=_blank rel=nofollow&gt;&lt;span class=yshortcuts id=lw_1208912100_0&gt;&lt;/span&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Basically, you're able to import the domain.txt file into ISA as a URL or DNS set.&amp;nbsp;
Then you just set your deny access rule and away you go.&amp;nbsp; This utility can run
as a scheduled task.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://sync-io.net/go/blog/aggbug.ashx?id=2e4ee447-abe3-4ba5-98c0-f8a8d1876179" /&gt;</description>
      <comments>http://sync-io.net/go/blog/CommentView,guid,2e4ee447-abe3-4ba5-98c0-f8a8d1876179.aspx</comments>
      <category>Malware</category>
      <category>VB.NET</category>
      <category>ISA</category>
    </item>
    <item>
      <trackback:ping>http://sync-io.net/go/blog/Trackback.aspx?guid=d71d00da-05d7-4832-86fc-eeaf416cd7be</trackback:ping>
      <pingback:server>http://sync-io.net/go/blog/pingback.aspx</pingback:server>
      <pingback:target>http://sync-io.net/go/blog/PermaLink,guid,d71d00da-05d7-4832-86fc-eeaf416cd7be.aspx</pingback:target>
      <dc:creator>Chris</dc:creator>
      <wfw:comment>http://sync-io.net/go/blog/CommentView,guid,d71d00da-05d7-4832-86fc-eeaf416cd7be.aspx</wfw:comment>
      <wfw:commentRss>http://sync-io.net/go/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d71d00da-05d7-4832-86fc-eeaf416cd7be</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The following code has been tested on Windows 2003 SP2.  The calling user
account must have 'Assign Primary Token' and 'Increase Quota' permissions to
properly execute CreateProcessAsUser; see MS KB285879.  
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Function</span> RunProc(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ByVal</span> CMD <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">String</span>, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ByVal</span> ARG <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">String</span>) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">String</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Dim</span> er <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span> Int16 <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Dim</span> exitCode <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span> System.UInt32 <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Convert.ToUInt32(123) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Dim</span> saThreadAttributes <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span> SECURITY_ATTRIBUTES <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">New</span> SECURITY_ATTRIBUTES
saThreadAttributes.nLength <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Marshal.SizeOf(saThreadAttributes) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Dim</span> impToken <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span> System.IntPtr <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> IntPtr.Zero <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Dim</span> priToken <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span> System.IntPtr <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> IntPtr.Zero <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">If</span> LogonUser(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"DomainUser"</span>, <span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Domain"</span>, <span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Passwd"</span>,
LogonType.LOGON32_LOGON_INTERACTIVE, _ LogonProvider.LOGON32_PROVIDER_DEFAULT, impToken) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Then</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">If</span> DuplicateTokenEx(impToken,
&amp;H2000000, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Nothing</span>,
SecurityImpersonationLevel.SecurityDelegation, TOKEN_TYPE.TokenPrimary, priToken) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Then</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Dim</span> pi <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span> PROCESS_INFORMATION <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">New</span> PROCESS_INFORMATION <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Dim</span> si <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span> STARTUPINFO <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">New</span> STARTUPINFO
si.cb <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Marshal.SizeOf(si)
si.lpDesktop <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> IntPtr.Zero <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Dim</span> saProcessAttributes <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">As</span> SECURITY_ATTRIBUTES <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">New</span> SECURITY_ATTRIBUTES
saProcessAttributes.nLength <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Marshal.SizeOf(saProcessAttributes) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">If</span><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Not</span> CreateProcessAsUser(priToken,
CMD, CMD &amp; <span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"
"</span> &amp; ARG, saProcessAttributes, _ saThreadAttributes, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">False</span>,
0, IntPtr.Zero, <span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"c:\"</span>,
si, pi) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Then</span> er <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Marshal.GetLastWin32Error
RunProc <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"err
runas "</span> &amp; er) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Else</span> WaitForSingleObject(pi.hProcess,
Infinite) GetExitCodeProcess(pi.hProcess, exitCode) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">End</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">If</span> CloseHandle(priToken)
CloseHandle(impToken) CloseHandle(pi.hProcess) CloseHandle(pi.hThread) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">End</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">If</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">End</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">If</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">End</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Function</span></span>
        </pre>
        <img width="0" height="0" src="http://sync-io.net/go/blog/aggbug.ashx?id=d71d00da-05d7-4832-86fc-eeaf416cd7be" />
      </body>
      <title>CreateProcessAsUser from System Service (VB.NET)</title>
      <guid isPermaLink="false">http://sync-io.net/go/blog/PermaLink,guid,d71d00da-05d7-4832-86fc-eeaf416cd7be.aspx</guid>
      <link>http://sync-io.net/go/blog/2008/03/27/CreateProcessAsUserFromSystemServiceVBNET.aspx</link>
      <pubDate>Thu, 27 Mar 2008 21:00:07 GMT</pubDate>
      <description>&lt;p&gt;
The following code has been tested on Windows 2003 SP2.&amp;nbsp;&amp;nbsp;The calling user
account must have&amp;nbsp;'Assign Primary Token' and&amp;nbsp;'Increase Quota'&amp;nbsp;permissions&amp;nbsp;to
properly execute CreateProcessAsUser; see MS KB285879.&amp;nbsp; 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Function&lt;/span&gt; RunProc(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ByVal&lt;/span&gt; CMD &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;String&lt;/span&gt;, &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ByVal&lt;/span&gt; ARG &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;String&lt;/span&gt;) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;String&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Dim&lt;/span&gt; er &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; Int16 &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Dim&lt;/span&gt; exitCode &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; System.UInt32 &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Convert.ToUInt32(123) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Dim&lt;/span&gt; saThreadAttributes &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; SECURITY_ATTRIBUTES &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;New&lt;/span&gt; SECURITY_ATTRIBUTES
saThreadAttributes.nLength &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Marshal.SizeOf(saThreadAttributes) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Dim&lt;/span&gt; impToken &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; System.IntPtr &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; IntPtr.Zero &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Dim&lt;/span&gt; priToken &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; System.IntPtr &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; IntPtr.Zero &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;If&lt;/span&gt; LogonUser(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"DomainUser"&lt;/span&gt;, &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Domain"&lt;/span&gt;, &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Passwd"&lt;/span&gt;,
LogonType.LOGON32_LOGON_INTERACTIVE, _ LogonProvider.LOGON32_PROVIDER_DEFAULT, impToken) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Then&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;If&lt;/span&gt; DuplicateTokenEx(impToken,
&amp;amp;H2000000, &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Nothing&lt;/span&gt;,
SecurityImpersonationLevel.SecurityDelegation, TOKEN_TYPE.TokenPrimary, priToken) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Then&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Dim&lt;/span&gt; pi &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; PROCESS_INFORMATION &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;New&lt;/span&gt; PROCESS_INFORMATION &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Dim&lt;/span&gt; si &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; STARTUPINFO &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;New&lt;/span&gt; STARTUPINFO
si.cb &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Marshal.SizeOf(si)
si.lpDesktop &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; IntPtr.Zero &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Dim&lt;/span&gt; saProcessAttributes &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;As&lt;/span&gt; SECURITY_ATTRIBUTES &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;New&lt;/span&gt; SECURITY_ATTRIBUTES
saProcessAttributes.nLength &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Marshal.SizeOf(saProcessAttributes) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;If&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Not&lt;/span&gt; CreateProcessAsUser(priToken,
CMD, CMD &amp;amp; &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"
"&lt;/span&gt; &amp;amp; ARG, saProcessAttributes, _ saThreadAttributes, &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;False&lt;/span&gt;,
0, IntPtr.Zero, &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"c:\"&lt;/span&gt;,
si, pi) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Then&lt;/span&gt; er &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Marshal.GetLastWin32Error
RunProc &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"err
runas "&lt;/span&gt; &amp;amp; er) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Else&lt;/span&gt; WaitForSingleObject(pi.hProcess,
Infinite) GetExitCodeProcess(pi.hProcess, exitCode) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;End&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;If&lt;/span&gt; CloseHandle(priToken)
CloseHandle(impToken) CloseHandle(pi.hProcess) CloseHandle(pi.hThread) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;End&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;If&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;End&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;If&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;End&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Function&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;img width="0" height="0" src="http://sync-io.net/go/blog/aggbug.ashx?id=d71d00da-05d7-4832-86fc-eeaf416cd7be" /&gt;</description>
      <comments>http://sync-io.net/go/blog/CommentView,guid,d71d00da-05d7-4832-86fc-eeaf416cd7be.aspx</comments>
      <category>Access Denied</category>
      <category>VB.NET</category>
    </item>
  </channel>
</rss>