<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JEDI Windows API &#187; Sid</title>
	<atom:link href="http://blog.delphi-jedi.net/tag/sid/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.delphi-jedi.net</link>
	<description>Joint Endeavor of Delphi Innovators of Windows Programming</description>
	<lastBuildDate>Sat, 21 Aug 2010 05:44:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>How to get the user&#8217;s SID.</title>
		<link>http://blog.delphi-jedi.net/2008/03/18/how-to-get-the-users-sid/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/18/how-to-get-the-users-sid/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 09:00:19 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[Sid]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/18/how-to-get-the-users-sid/</guid>
		<description><![CDATA[There is not much to write about how you can obtain the user&#8217;s SID. Use these members class TJwSecurityID in unit JwsclSid function JwSecurityCurrentThreadUserSID in unit JwsclKnownSID uses JwsclKnownSid, JwsclSid; var SID : TJwSecurityID; begin &#160; SID := JwSecurityCurrentThreadUserSID; &#160; WriteLn&#40;SID.GetText&#40;true&#41;&#41;; &#160; SID.Free; end; The SID is either obtained from the thread token or &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>There is not much to write about how you can obtain the user&#8217;s SID.</p>
<p><span id="more-78"></span></p>
<p>Use these members</p>
<ul>
<li>class <a href="http://jwscldoc.delphi-jedi.net/JwsclSid.TJwSecurityId.html">TJwSecurityID</a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclSid.TJwSecurityId.html"></a><a href="http://jwscldoc.delphi-jedi.net/JwsclSid.html">JwsclSid</a></li>
<li>function <a href="http://jwscldoc.delphi-jedi.net/JwsclKnownSid.html#JwSecurityCurrentThreadUserSID">JwSecurityCurrentThreadUserSID</a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclKnownSid.html#JwSecurityCurrentThreadUserSID"></a><a href="http://jwscldoc.delphi-jedi.net/JwsclKnownSid.html">JwsclKnownSID</a></li>
</ul>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> JwsclKnownSid, JwsclSid;<br />
<span class="kw1">var</span> SID : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; SID := JwSecurityCurrentThreadUserSID;<br />
&nbsp; <span class="kw3">WriteLn</span><span class="br0">&#40;</span>SID.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; SID.<span class="me1">Free</span>;<br />
<span class="kw1">end</span>;</div>
<p>The SID is either obtained from the thread token or &#8211; if it does not exist &#8211; from the process token.</p>
<p><strong>Tell me how you liked this blog entry by adding a comment.</strong></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/18/how-to-get-the-users-sid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is a SID?</title>
		<link>http://blog.delphi-jedi.net/2008/03/10/what-is-a-sid/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/10/what-is-a-sid/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 22:55:01 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[Administrator]]></category>
		<category><![CDATA[Group]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[Principal]]></category>
		<category><![CDATA[security identifier]]></category>
		<category><![CDATA[Sid]]></category>
		<category><![CDATA[well-known]]></category>
		<category><![CDATA[wellknown]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/10/what-is-a-sid/</guid>
		<description><![CDATA[SID is the short form of Security Identifier. It is used to uniquely name a user (like Alice or Bob), a group (like Users or Administrators) or a system account (like SYSTEM or Network Service). Important to understand is that not only human users get a SID but also system parts like the System or [...]]]></description>
			<content:encoded><![CDATA[<p>SID is the short form of <em><strong>S</strong>ecurity <strong>Id</strong>entifier</em>. It is used to uniquely name a user (like Alice or Bob), a group (like Users or Administrators) or a system account (like SYSTEM or Network Service). Important to understand is that not only human users get a SID but also system parts like the System or Network Service and even computers itself. An inhuman principal does not have a password so it cannot be get logged on in the traditional sense. They just exist as soon as the system is booted up. If you read about a principal in this context, it is either a human user, a group or a system user. Because there is often no need to tell them apart, we just refer to it as a <em>principal</em>.<span id="more-35"></span></p>
<p>A principal is not recognized by its name, but by its unique SID which is a dynamic structure in the C world. It is made of three parts:</p>
<ul>
<li>A revision level that defines the sid version and currently is set to one (1).</li>
<li>A 48 bit number which defines the authority that created the SID. (e.g. Windows NT defines 5)</li>
<li>An array of numbers which uniquely identify the principal within the authority. The last number is sometimes called a relative identifier (RID).</li>
</ul>
<p>A template string of a SID may look like this. (Brackets [ ] define optional parameters):</p>
<blockquote><p>S &#8211; R &#8211; I [- S<small>1</small> [- ... - S<small>n </small>- [RID]]]</p></blockquote>
<ul>
<li>S declares the string as a SID.</li>
<li>R defines the revision number.</li>
<li>S<small>1</small>-&#8230;-S<small>n</small> defines the sub-authority. It is an array of n numbers that identifies a domain or machine.</li>
<li>RID is the relative principal identifier. It is an unique, sequential and increasing number for a principal assigned by the authority. In Windows, known RIDs start with 500 which defines the Administrator account. RIDs of 1000 and above are used for the usual users and groups. The RID is not an independant part of a SID. In fact it is really the last part of the sub-authority array. Thus the Windows API and JWSCL do not have functions to read or alter it directly. To do so you must access the sub-authority array.</li>
</ul>
<p>Windows defines some well known SIDs and some well known RIDs. Well knowns SIDs look the same on every computer because they have no domain or machine identifier. On the other hand there are well known RIDs which are system or domain relative. I write system as in operating system, because even on a multi boot computer the SIDs vary between different operating systems if they are not cloned. Several identical SIDs on different machines may lead to security problems. In this case there is a little helper called <a href="http://technet.microsoft.com/de-de/sysinternals/bb897418(en-us).aspx" title="Go go MS TechNET">NewSID</a> that helps changing the SID.</p>
<p>You can find a list of well known SIDs and RIDs in <a href="http://support.microsoft.com/?scid=kb%3Ben-us%3B243330&amp;x=15&amp;y=12" title="Go to MSDN.">MSDN</a>. Some important well known SIDs are shown here:</p>
<blockquote>
<ul>
<li>S-1-1-0 : The <em>Everyone </em>group</li>
<li>S-1-5-4 : The <em>Interactive</em> identifier which allows interactive logon.</li>
<li>S-1-5-5-#-# : The <em>Session Logon</em> SID (more about it <a href="http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/" title="Go to blog article.">here</a> ) &#8220;#&#8221; is a placeholder for the high and low value of the logon session ID called LUID. It can be retrieved from the principal&#8217;s token.</li>
</ul>
</blockquote>
<p>Here are some important well known RIDs. The ellipsis &lt;&#8230;&gt; represent the system or domain.</p>
<blockquote>
<ul>
<li>S-1-5-&lt;&#8230;&gt;-500 : The <em>Administrator </em>account</li>
<li>S-1-5-&lt;&#8230;&gt;-501 : The <em>Guest </em>account</li>
<li>S-1-5-32 : The <em>Builtin Local</em> groups which identifies a member of a Builtin database</li>
</ul>
</blockquote>
<p>Some well known RIDs are appended to the <em>Builtin</em> SID to define them as special accounts and that they are hard-coded into the system.</p>
<blockquote>
<ul>
<li>S-1-5-32-544 : The Administrators group</li>
<li>S-1-5-32-545 : The Users group</li>
<li>S-1-5-32-546 : The Guest group</li>
</ul>
</blockquote>
<hr size="2" width="100%" />JWSCL provides an easy way to access all these elements.<br />
Let start with the base element <a href="http://jwscldoc.delphi-jedi.net/JwsclSid.TJwSecurityId.html" title="Go to Online Documentation...">TJwSecurityID</a> which resides in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclSid.html" title="Go to Online Documentation...">JwsclSid</a>. It encapsulates the C style structure SID and allows to use it in a more object-oriented way. In this way we get rid of manipulating memory directly.<br />
There are many different ways to create a SID instance in JWSCL:</p>
<ol>
<li>Create a copy of an existing <em>TJwSecurityID </em>instance</li>
<li>Create a copy of an existing SID structure</li>
<li>Create a copy from a nested SID structure within a <a href="http://msdn2.microsoft.com/en-us/library/aa379595.aspx" title="Go to MSDN.">SidAndAttributes</a> structure</li>
<li>Create a SID from an authority and an identifier</li>
<li>Create a well known SID from known constants</li>
<li>Create a SID from a string that represents the SID</li>
<li>Create a SID from the combination of a system and user name</li>
</ol>
<p>These ways are accomplished by the <em>TJwSecurity </em>constructors:</p>
<ol>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SecurityID: TJwSecurityId); </code></li>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SID: PSID); </code></li>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SID: PSidAndAttributes); </code></li>
<li><pre><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const Authorities: TJwSubAuthorityArray; Identifier: TSidIdentifierAuthority);
</code></pre><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const Authorities: array of Cardinal; Identifier: TSidIdentifierAuthority);</code></li>
<li><code>constructor &lt;strong&gt;CreateWellKnownSid&lt;/strong&gt;(WellKnownSidType: TWellKnownSidType; DomainSid: TJwSecurityId = nil);</code></li>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SIDString: TJwString);</code></li>
<li><code>constructor &lt;strong&gt;Create&lt;/strong&gt;(const SystemName, AccountName: TJwString);</code></li>
</ol>
<p><u>1. </u><pre><code>&lt;u&gt;&lt;strong&gt;Create&lt;/strong&gt;(const SecurityID: TJwSecurityId)&lt;/u&gt;
</code></pre>Sometimes it is necessary to get a second copy of an already existing SID instance. Use the copy constructor for that task. It makes a duplicate so you can use the second instance exactly like the first one. This comes really handy if you write a multiple threads environment where threads create and free SID instances. Be aware that you cannot change the SID content afterwards. Most properties do not allow to change their values. So you cannot change the identifier, sub-authorities or the well known SID type. This is because a change may lead to security problems. Consider that you could change a SID while it is used in another part of your application (a thread). If this part does a security access check using your SID instance while you change the SID content, the result are unpredictable or even risky even if you apply thread safety. This is because the AccessCheck could return true even if the original SID does not have access. It only may return access allowed because the SID was changed. If you want to change an existing SID instance, you have to create a copy using one of the constructors. For example get the SubAuthority property, alter it and pass it to one of the conststructors in #4. In this way the existing SID instances won&#8217;t be affected.<u><code></code></u></p>
<p><u><code>2./3. &lt;strong&gt;Create&lt;/strong&gt;(const SID: PSID)/</code><code>&lt;strong&gt;Create&lt;/strong&gt;(const SID: PSidAndAttributes);</code></u><br />
It is quite uncommon to create an instance by using a SID structure. However if you ever encounter such a task you must be aware that the assigned SID is copied (not refered to it) into the instance. The <em>const </em>key-word in front of the parameter name denotes that parameter <em>SID </em>will not be altered internally. Thus there is no other way than to copy it. Also be aware that the SID memory is checked for a correct SID structure; otherwise <span class="normal">EJwsclInvalidSIDException</span> will be raised.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">var</span> SID : PSID;<br />
&nbsp; &nbsp; SIDInstance : TJwSecurityID; </p>
<p><span class="kw1">begin</span><br />
&nbsp; <span class="kw3">GetMem</span><span class="br0">&#40;</span>SID, requestedSIDSize<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; <span class="co1">//obtain SID structure here</span><br />
&nbsp; &nbsp; SIDInstance := TJwSecurityID<span class="br0">&#40;</span>SID<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; <span class="kw3">FreeMem</span><span class="br0">&#40;</span>SID<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; <span class="co1">//do stuff with SIDInstance here&#8230;</span><br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; SIDInstance.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;<br />
<span class="kw1">end</span>;</div>
<p><u>4. <code>&lt;strong&gt;Create&lt;/strong&gt;(const Authorities: </code><code>TJwSubAuthorityArray; Identifier: TSidIdentifierAuthority);</code></u><br />
It is easily possible to create a SID from scratch by using the internal parts of a SID. In the following example we create the well known group SID &#8220;Everybody&#8221;.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> JwaWindows, JwsclSID;<br />
<span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; SIDInstance := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, SECURITY_WORLD_SID_AUTHORITY<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">WriteLn</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; SIDInstance.<span class="me1">Free</span>;<br />
<span class="kw1">end</span>;</div>
<p>The constant five bytes long array identifier <em>SECURITY_WORLD_SID_AUTHORITY</em> from JwaWindows is declared as follow:</p>
<div class="dean_ch" style="white-space: wrap;">SECURITY_WORLD_SID_AUTHORITY: TSidIdentifierAuthority = <span class="br0">&#40;</span>Value: <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
<p>We add a subauthority of zero and get the shown output. The emtpy brackets usually contains the SID&#8217;s attributes which we did not use.</p>
<blockquote><p>Everybody (S-1-1-0) []</p></blockquote>
<p>The second constructors receives an declared array which describes the sub-authoroties. With the help of it we can alter an existing instance. The example shown below demonstrates how to alter the Everybody group by changing the sub authority array.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> JwaWindows, JwsclSID; </p>
<p><span class="kw1">var</span> SIDInstance,SIDInstance2 : TJwSecurityID;<br />
&nbsp; &nbsp; SubAuths : TJwSubAuthorityArray;<br />
<span class="kw1">begin</span><br />
&nbsp; SIDInstance := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, SECURITY_WORLD_SID_AUTHORITY<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; &nbsp; </p>
<p>&nbsp; <span class="co1">//get copy of this array</span><br />
&nbsp; SubAuths := SIDInstance.<span class="me1">SubAuthorityArray</span>; &nbsp;<span class="co1">//increase first array member </span></p>
<p>&nbsp; <span class="kw3">Inc</span><span class="br0">&#40;</span>SubAuths<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span>; </p>
<p>&nbsp; <span class="co1">//create new instance and use the new authorities</span><br />
&nbsp; SIDInstance2 := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span>SubAuths, SIDInstance.<span class="me1">IdentifierAuthority</span><span class="br0">&#41;</span>; </p>
<p>&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance2.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p><u></u></p>
<p><u>5. <code>&lt;strong&gt;CreateWellKnownSid&lt;/strong&gt;(WellKnownSidType: TWellKnownSidType;...);</code></u><br />
<code></code>The way to create a well known SID as shown in part #4 is very inconvenient. Thus the class <em>TJwSecurityID </em>contains a constructor that directly allows us to use one of the well known SID constants declared in JwaWindows or JwaVista (only if you need the new Vista definitions). The enumeration type <code>WELL_KNOWN_SID_TYPE </code>holds all known SIDs.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">type</span><br />
&nbsp; WELL_KNOWN_SID_TYPE = <span class="br0">&#40;</span><br />
&nbsp; &nbsp; WinNullSid <span class="coMULTI">{= 0}</span>,<br />
&nbsp; &nbsp; WinWorldSid <span class="coMULTI">{= 1}</span>,<br />
&nbsp; &nbsp; WinLocalSid <span class="coMULTI">{= 2}</span>,<br />
&nbsp; &nbsp; WinCreatorOwnerSid <span class="coMULTI">{= 3}</span>,<br />
&nbsp; &nbsp; WinCreatorGroupSid <span class="coMULTI">{= 4}</span>,<br />
&nbsp; &nbsp; &#8230;<br />
&nbsp; &nbsp; <span class="co1">//JwaVista</span><br />
&nbsp; &nbsp; WinLowLabelSid <span class="coMULTI">{= 66}</span>,<br />
&nbsp; &nbsp; WinMediumLabelSid <span class="coMULTI">{= 67}</span>,<br />
&nbsp; &nbsp; WinHighLabelSid <span class="coMULTI">{= 68}</span>,<br />
&nbsp; &nbsp; WinSystemLabelSid <span class="coMULTI">{= 69}</span>,<br />
&nbsp; &nbsp; &#8230;<br />
<span class="br0">&#41;</span>;</div>
<p>The CreateWellKnownSid constructor uses the definition from JwaVista. So if you do not use JwaVista in your use clause you have to add it now because the constructor uses the new version. In this case you should add JwaVista in front of JwaWindows since Delphi uses by default an identifier that is declared in the latest included unit.<br />
The code below uses declarations introduced by JwaWindows but not JwaVista.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> &#8230;, JwaVista, JwaWindows, &#8230;;</div>
<p>Of course it is possible to explicitly refer to JwaVista. This task is done by adding the unit name in front of the identifier seperated by a point operator.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> &#8230;,<br />
&nbsp; JwaVista,<br />
&nbsp; JwaWindows, &#8230;;</p>
<p><span class="kw1">var</span> SidType : &lt;code&gt;JwaVista.<span class="me1">TWellKnownSidType</span>; &lt;/code&gt;<br />
<span class="kw1">begin</span><br />
&nbsp; SidType := JwaVista.<span class="me1">WinNullSid</span>;<br />
&#8230;</div>
<p>Creating a well known SID in this way needs getting used to:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp; JwaVista,<br />
&nbsp; JwaWindows,<br />
&nbsp; JwsclSID;</p>
<p><span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; SIDInstance := TJwSecurityID.<span class="me1">CreateWellKnownSid</span><span class="br0">&#40;</span>JwaVista.<span class="me1">WinWorldSid</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p>If you start programming and want to use Vista stuff from the beginning, you should add JwaVista after JwaWindows. In this case you can easily create a well known SID.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp; JwaWindows,<br />
&nbsp; JwaVista,<br />
&nbsp; JwsclSID;</p>
<p><span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; SIDInstance := TJwSecurityID.<span class="me1">CreateWellKnownSid</span><span class="br0">&#40;</span>WinWorldSid<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p><u>6. <code>&lt;strong&gt;Create&lt;/strong&gt;(const SIDString: TJwString);</code></u><code></code><code></code></p>
<p>A very convenient way to create a SID instance is to use the SID string format that was described at the very first part of this discussion.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp; JwaWindows,<br />
&nbsp; JwaVista,<br />
&nbsp; JwsclSID; </p>
<p><span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; SIDInstance := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span><span class="st0">&#8216;S-1-1-0&#8242;</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">except</span><br />
&nbsp; &nbsp; <span class="kw1">on</span> E : EJwsclWinCallFailedException <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">//do error stuff here and exit.</span><br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<p><strong>Be warned</strong> that the constructor may raise an exception if you do not comply with the SID string format.</p>
<p><u></u></p>
<p><u>7. <code>&lt;strong&gt;Create&lt;/strong&gt;(const SystemName, AccountName: TJwString);</code></u></p>
<p>There is sometimes the necessity to get the SID of a user on a specific system. E.g. the user enters his name and the domain or machine name, in this case you can create a SID by a principal&#8217;s name. The following example creates a SID instance from the given user on the local system.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span><br />
&nbsp;JwaWindows,<br />
&nbsp;JwaVista,<br />
&nbsp;JwsclSID; </p>
<p><span class="kw1">var</span> SIDInstance : TJwSecurityID;<br />
&nbsp; &nbsp; UserName &nbsp;: <span class="kw4">String</span>;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw3">ReadLn</span><span class="br0">&#40;</span>UserName<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; SIDInstance := TJwSecurityID.<span class="me1">Create</span><span class="br0">&#40;</span><span class="st0">&#8221;</span>,UserName<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">except</span><br />
&nbsp; &nbsp; <span class="kw1">on</span> E : EJwsclWinCallFailedException <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">//do error stuff here and exit.</span><br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw3">WriteLn</span><span class="br0">&#40;</span>SIDInstance.<span class="me1">getText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&#8230;</div>
<hr size="2" width="100%" />In the end, I want to show you some very useful methods. Here they are :</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">function</span> GetText<span class="br0">&#40;</span>ignoreExceptions: <span class="kw4">boolean</span> = <span class="kw2">False</span><span class="br0">&#41;</span>: TJwString;</div>
<p>Call this method if you need display information of the SID instance. It shows information about the domain and user name, the SID string and attributes if any. Set the parameter <em>ignoreExceptions </em>to false if you want to get an exception if a SID could not be translated into an name. Set it to true if you just want an empty string to be displayed instead. If you don&#8217;t want to use too many exception handling mechanisms, you should set the parameter value to true.</p>
<blockquote><p>Everybody (S-1-1-0) []</p></blockquote>
<p>I already used this method to show you the output earlier in this article.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">property</span> CachedSystemName : TJwString; writable;</div>
<p><em>GetText </em>uses the system or domain name once assigned to the constructor. However sometimes it is necessary to change it. The property CachedSystemName gets or sets this system or domain name.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">property</span> StringSID: TJwString readonly;</div>
<p>This readonly property contains the SID&#8217;s string representation as shown in GetText.</p>
<div class="dean_ch" style="white-space: wrap;">property WellKnownSidType: TWellKnownSidType; readonly;</div>
<p>Use the WellKnownSidType property if you need to know of what well known type the SID consists of. Be aware that you should not rely on it if the return value is WinNullSid, because in this case the SID could also be any other SID. The property returns this value for a NULL SID and also for a unknown SID. Use the boolean property IsWellKnownSID to check for a well known SID.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">property</span> Attributes: <span class="kw4">Cardinal</span>; writable;<br />
<span class="kw1">property</span> AttributesType: TJwSidAttributeSet; writable;</div>
<p>The attributes assigned to a SID can be used in different ways. Some WinAPI functions need them for example. However you can use two versions that are nearly equal. Either you set a bitmask using the property <em>Attributes </em>or you use a enumeration set with <em>AttributesType.</em></p>
<p>Next time I&#8217;ll talk about more JWSCL classes that represent well known SIDs and are defined in the unit JwsclKnownSID.</p>
<p><strong>Tell me how you liked this blog entry by adding a comment.</strong></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/10/what-is-a-sid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use a SecurityAttribute structure</title>
		<link>http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 15:43:25 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[ACE]]></category>
		<category><![CDATA[ACL]]></category>
		<category><![CDATA[CreateFile]]></category>
		<category><![CDATA[descriptor]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[security attribute]]></category>
		<category><![CDATA[Sid]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/</guid>
		<description><![CDATA[Many developers know that mysterious parameter some WinAPI function offer to use. What I am talking about is a parameter called lpSecurityAttributes. The documentation states that it allows to change the security descriptor of the desired element. The most known function is CreateFile which I want to use here. It allows to create a file [...]]]></description>
			<content:encoded><![CDATA[<p>Many developers know that mysterious parameter some WinAPI function offer to use. What I am talking about is a parameter called <em>lpSecurityAttributes</em>. The documentation states that it allows to change the security descriptor of the desired element. The most known function is <em>CreateFile</em> which I want to use here. It allows to create a file and more. But we stay to create a file. Usually we set the parameter <em>lpSecurityAttributes</em> simply to <strong>NULL</strong>/<strong>nil</strong> because this sets the default security configuration we want to use in most of the times (this also includes file/folder inheritance).</p>
<p>However sometimes we want to use our own security configuration to allow other participants to access a resource we created. JWSCL makes this task much more simple than using the security runtime function written in plain C. We do not have to create the security descriptor from scratch. The JWSCL methods allow us to get the default security descriptor and adapt it to our needs.<span id="more-30"></span></p>
<p>Let&#8217;s start with the required classes and methods we need to add another user who wants access.</p>
<ul>
<li style="list-style-type: none; list-style-image: none; list-style-position: outside"></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html" title="Go to JWSCL Online Documentation."><em>TJwSecurityDescriptor</em></a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.html" title="Go to JWSCL Online Documentation."><em>JwsclDescriptor</em></a></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.TJwDAccessControlList.html" title="Go to JWSCL Online Documentation."><em>TJwDAccessControlList</em></a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.html" title="Go to JWSCL Online Documentation."><em>JwsclAcl</em></a></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.TJwDiscretionaryAccessControlEntryAllow.html" title="Go to JWSCL Online Documentation."><em>TJwDiscretionaryAccessControlEntryAllow</em></a> in unit <em>JwsclAcl</em></li>
<li><a href="http://jwscldoc.delphi-jedi.net/JwsclSid.TJwSecurityId.html" title="Go to JWSCL Online Documentation."><em>TJwSecurityID</em></a> in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclSid.html" title="Go to JWSCL Online Documentation."><em>JwsclSid</em></a></li>
</ul>
<p>That&#8217;s all.</p>
<p>Since the system already creates us an adequate security access list we want to continue using it. For this reason <em>TJwSecurityDescriptor</em> implements a constructor called <a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html#CreateDefaultByToken" title="Go to JWSCL Online Documentation.">CreateDefaultByToken</a> that creates such a security access list automatically.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">uses</span> JwaWindows,&#8230;,JwsclDescriptor, JwsclAcl, JwsclTypes, JwsclStrings;<br />
<span class="kw1">var</span> SD : TJwSecurityDescriptor;<br />
<span class="kw1">begin</span><br />
&nbsp; SD := TJwSecurityDescriptor.<span class="me1">CreateDefaultByToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw3">Writeln</span><span class="br0">&#40;</span>SD.<span class="me1">Text</span><span class="br0">&#41;</span>;</div>
<p>The output may look like this, depending on your Windows system.</p>
<blockquote><p>Owner: chris@ Christian (S-1-5-21-2735234258-346234578-4357623456-1000) []<br />
Group: chris@ None (S-1-5-21-2735234258-346234578-4357623456-513) [sidaGroupMandatory]<br />
DACL:<br />
ACE Count: 3<br />
\#0<br />
ClassName: TJwDiscretionaryAccessControlEntryAllow<br />
AceType: Allow<br />
Flags:<br />
Accessmask: No map class given. 268435456, 0&#215;10000000<br />
SID: chris@ Christian (S-1-5-21-2735234258-346234578-4357623456-1000) []<br />
#1<br />
ClassName: TJwDiscretionaryAccessControlEntryAllow<br />
AceType: Allow<br />
Flags:<br />
Accessmask: No map class given. 268435456, 0&#215;10000000<br />
SID: NT-AUTORIT-T@ SYSTEM (S-1-5-18) []<br />
#2<br />
ClassName: TJwDiscretionaryAccessControlEntryAllow<br />
AceType: Allow<br />
Flags:<br />
Accessmask: No map class given. 2684354560, 0xA0000000<br />
SID: (S-1-5-5-0-151391) []</p>
<p>SACL:<br />
ACE Count: 0<br />
\</p></blockquote>
<p>(<em>No map class given</em> is shown because the security descriptor class does not know the type of secured object. A map class (derived from <a href="http://jwscldoc.delphi-jedi.net/JwsclMapping.TJwSecurityGenericMapping.html" title="Go to JWSCL Online Documentation.">TJwSecurityGenericMapping</a>)  can convert the AccessMask to an human readable string)</p>
<p>The system sets the owner to the current token owner of the process or thread. It also adds my user account and the SYSTEM principal with full access (0&#215;10000000 = GENERIC_ALL). The unknown principal with the Sid S-1-5-5-0-151391 describes the loggon session Sid. At a later point we will remove it.<br />
For the discussion we want to add another principal called Alice so she can get read access to the file/folder. Because we need the Alice&#8217;s Sid we have to add another variable called <em>AliceSid</em>.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">var</span> SD : TJwSecurityDescriptor;<br />
&nbsp; &nbsp; AliceSid : TJwSecurityId;<br />
<span class="kw1">begin</span><br />
&nbsp; &nbsp;SD := TJwSecurityDescriptor.<span class="me1">CreateDefaultByToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp;AliceSid := TJwSecurityId.<span class="me1">Create</span><span class="br0">&#40;</span><span class="st0">&#8221;</span>,<span class="st0">&#8216;Alice&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp;SD.<span class="me1">DACL</span>.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_READ, AliceSid, <span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;<span class="kw3">Writeln</span><span class="br0">&#40;</span>SD.<span class="me1">Text</span><span class="br0">&#41;</span>;</div>
<p>In the code above a new class instance of <em>TJwDiscretionaryAccessControlEntryAllow</em> is added to the DACL. Because we create just a simple file we do not need special flags thus an empty flag set [] is applied. The access mask parameter will receive GENERIC_READ as the maximum possible access to this file granted to Alice. The last parameter (OwnSid) is set to <strong>true</strong> and defines that the access control list (here property <em>DACL</em>) destroys the instance <em>AliceSid</em> at the end.</p>
<p>The resulting security descriptor has now a new access control entry. The output is the same like above but with this additional element.</p>
<blockquote><p>ClassName: TJwDiscretionaryAccessControlEntryAllow<br />
AceType: Allow<br />
Flags:<br />
Accessmask: No map class given. 2147483648, 0&#215;80000000<br />
SID: chris@ Alice (S-1-5-21-2735234258-346234578-4357623456-1008) []</p></blockquote>
<p>Now we can arrive at the part where <a href="http://msdn2.microsoft.com/en-us/library/aa363858(VS.85).aspx" title="Go to MSDN."><em>CreateFile</em></a> comes in. How can we create a pointer to a <a href="http://msdn2.microsoft.com/en-us/library/aa379560(VS.85).aspx" title="Go to MSDN.">TSecurityAttribute</a> type? It is really simple! We just have to declare some more helper variables that <em>CreateFile</em> needs and use <a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html#Create_SA" title="Go to JWSCL Online Documentation."><em>Create_SA</em></a>.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">var</span> SD : TJwSecurityDescriptor;<br />
&nbsp; &nbsp; &nbsp;AliceSid : TJwSecurityId;<br />
&nbsp; &nbsp; &nbsp;SecAttrPtr : PSecurityAttributes;<br />
&nbsp; &nbsp; &nbsp;Handle : <span class="kw4">THandle</span>;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw3">DeleteFile</span><span class="br0">&#40;</span><span class="st0">&#8216;testfile&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; SD := TJwSecurityDescriptor.<span class="me1">CreateDefaultByToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; AliceSid := TJwSecurityId.<span class="me1">Create</span><span class="br0">&#40;</span><span class="st0">&#8221;</span>,<span class="st0">&#8216;Alice&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; SD.<span class="me1">DACL</span>.<span class="me1">Add</span><span class="br0">&#40;</span>TJwDiscretionaryAccessControlEntryAllow.<span class="me1">Create</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span><span class="br0">&#93;</span>, GENERIC_READ, AliceSid, <span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; SecAttrPtr := SD.<span class="me1">Create_SA</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; Handle := jwaWindows.<span class="me1">CreateFile</span><span class="br0">&#40;</span><span class="st0">&#8216;testfile&#8217;</span>, FILE_ALL_ACCESS, <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">Pointer</span><span class="br0">&#40;</span>SecAttrPtr<span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CREATE_NEW, FILE_ATTRIBUTE_NORMAL,<span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">if</span> handle = ERROR_INVALID_HANDLE <span class="kw1">then</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw3">RaiseLastOSError</span>;<br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; SD.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;</div>
<p><a href="http://jwscldoc.delphi-jedi.net/JwsclDescriptor.TJwSecurityDescriptor.html#Create_SA" title="Go to JWSCL Online Documentation."><em>Create_SA</em></a> from <em>TJwSecurityDescriptor</em> creates the necessary memory structure (<em>SecurityAttributes</em>) and returns a pointer of type <em>PSecurityAttributes</em>. The pointer is used in CreateFile to apply our own security descriptor. Our access control list of the newly created file will contain all the elements you see above as output. In tests, both the creation flags CREATE_NEW and CREATE_ALWAYS never change the security attributes after the file has already been created. Although MSDN explains it correctly for CREATE_ALWAYS it does not say anything about CREATE_NEW in this context. Because of this we simply delete the file everytime.</p>
<p>The security editor of Windows Explorer shows us the new descriptor.</p>
<p><img src="http://blog.delphi-jedi.net/wp-content/uploads/2008/03/bild-3.jpg" alt="Bild" height="476" width="350" /></p>
<p>As you can see we did not remove the LogonSession Sid from the security descriptor. The logon SID resides in the principal&#8217;s token. It is used to add allow or deny access to a secured object but only for the time the user is logged on. This is because every time the user logs on, a new session Sid is generated. Additionally all calls to the LogonUser API get their own session Sid so a logon Sid is a fine grained access control that allows us not only to restrict access between users but also control access between several different instances of a user (consider the user itself as a class and all user tokens as instances of this class) .<br />
However removing this LogonSID will be our next task because we do not need it here.</p>
<p>JWSCL supports us with a function that returns a <em>TJwSecurityId</em> of the logon session. The function is called <a href="http://jwscldoc.delphi-jedi.net/JwsclKnownSid.html#JwGetLogonSID" title="Go to JWSCL Online Documentation."><em>JwGetLogonSID</em></a> and resides in unit <a href="http://jwscldoc.delphi-jedi.net/JwsclKnownSid.html" title="Go to JWSCL Online Documentation."><em>JwsclKnownSid</em></a> that we have to include additionally. Unfortunately there is a bug in <a href="http://blog.delphi-jedi.net/2008/03/03/jwscl-release-revision-316/" title="Go to downloads.">revision 316</a> that makes it impossible to use it. For this reason I added an adapted version of the whole unit <em>JwsclKnownSid</em> . You can get it <a href="http://blog.delphi-jedi.net/wp-content/uploads/2008/03/jwsclknownsid.zip">here</a>.</p>
<p>Let&#8217;s see how we can get rid of the logon sid.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">var</span> SD : TJwSecurityDescriptor;<br />
&nbsp; &nbsp; LogonSid,<br />
&nbsp; &nbsp; AliceSid : TJwSecurityId;<br />
&nbsp; &nbsp; SecAttrPtr : PSecurityAttributes;<br />
&nbsp; &nbsp; Handle : <span class="kw4">THandle</span>;<br />
&nbsp; &nbsp; PosInt : <span class="kw4">Integer</span>;<br />
<span class="kw1">begin</span><br />
&nbsp; SD := TJwSecurityDescriptor.<span class="me1">CreateDefaultByToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; LogonSid := JwGetLogonSID;</p>
<p>&nbsp; PosInt := SD.<span class="me1">DACL</span>.<span class="me1">FindSID</span><span class="br0">&#40;</span>LogonSid<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">if</span> PosInt &gt;= <span class="nu0">0</span> <span class="kw1">then</span><br />
&nbsp; &nbsp; &nbsp;SD.<span class="me1">DACL</span>.<span class="me1">Remove</span><span class="br0">&#40;</span>PosInt<span class="br0">&#41;</span>;<br />
&nbsp; LogonSid.<span class="me1">Free</span>;<br />
&#8230;</div>
<p><em>JwGetLogonSid </em>returns a new instance that can be used to search for the logon Sid in the DACL. The method <em><a href="http://jwscldoc.delphi-jedi.net/JwsclAcl.TJwSecurityAccessControlList.html#FindSID" title="Go to JWSCL Online Documentation.">FindSID</a> </em>in <em>TJwDAccessControlList</em> goes through the whole access control list and returns the zero based index of the entry we search for. If it were not to be found we would get a negative result value, but this is not the case here (although we check for it because it is good programming style). At the end do not forget to remove the instance.</p>
<p><img src="http://blog.delphi-jedi.net/wp-content/uploads/2008/03/bild-4.jpg" alt="Bild" height="473" width="350" /></p>
<p>The new security descriptor does not contain the annoying logon Sid anymore. Eventually we can use this discussed approach not only for <em>CreateFile</em> but also for <em>CreatePipe</em>, <em>CreateProcess</em>, RegCreateKeyEx, <em>RegSaveKeyEx</em>, <em>CreateFileMapping</em> and many more. I used <em>CreateFile</em> so you can easily look up the descriptor in the security editor of Windows Explorer.</p>
<p>The next article will discuss how we can use inheritance and why there are no inherited access control elements in our created security descriptor although the parent folder hand them down.</p>
<p><strong>Tell me how you liked this blog entry by adding a comment.</strong></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/04/how-to-use-a-security-attribute-structure/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Get the Windows Vista twin token</title>
		<link>http://blog.delphi-jedi.net/2008/03/04/get-the-windows-vista-twin-token/</link>
		<comments>http://blog.delphi-jedi.net/2008/03/04/get-the-windows-vista-twin-token/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 11:29:11 +0000</pubDate>
		<dc:creator>Christian Wimmer</dc:creator>
				<category><![CDATA[JEDI Windows Security Code Lib]]></category>
		<category><![CDATA[integrity]]></category>
		<category><![CDATA[JWSCL]]></category>
		<category><![CDATA[Sid]]></category>
		<category><![CDATA[Token]]></category>
		<category><![CDATA[UAC]]></category>
		<category><![CDATA[Vista]]></category>

		<guid isPermaLink="false">http://blog.delphi-jedi.net/2008/03/04/get-the-windows-vista-twin-token/</guid>
		<description><![CDATA[Windows Vista contains a new feature that allows an administrator to work with less privileges. Every time a user who belongs to the administrator group logs on, the LogonUser API creates two tokens. One tokens contains the real power of the user and the second contains only restricted access. We call such a token restricted [...]]]></description>
			<content:encoded><![CDATA[<p>Windows Vista contains a new feature that allows an administrator to work with less privileges. Every time a user who belongs to the administrator group logs on, the LogonUser API creates two tokens. One tokens contains the real power of the user and the second contains only restricted access. We call such a token restricted token. This feature was implemented way back in Windows 2000 . The changes on the restricted token starts with removed privileges and ends with setting the administrator group in the token groups to use for deny only. A deny only Sid is only used for access control entries that deny access. So in our case the access to a file which allows Administrators full access may be disallowed if there is not any other positive element that grants us the access.<br />
Back to the topic. The token groups contains a special Sid that is called an integrity Sid. The token that has the administrator group enabled receives a high integrity Sid. Tthe medium integrity Sid goes to the groups of the restricted token.<br />
The token returned bei LogonUser is always the restricted one. Although you can retrieve the the twin token, you cannot do anything with it if you are not a SYSTEM process or an administrator. However a SYSTEM process may return the powerful token to create a process with full administrator rights. UAC does not do anything else. It gets the user&#8217;s credentials, logs on and uses the twin token (if any, otherwise it prompts for an administrator account credentials) to create the process.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> JwaWindows, JwsclExceptions, JwsclToken;var Token,<br />
&nbsp; &nbsp; TwinToken : TJwSecurityToken;<br />
<span class="kw1">begin</span><br />
&nbsp; Token := TJwSecurityToken.<span class="me1">CreateTokenEffective</span><span class="br0">&#40;</span>MAXIMUM_ALLOWED<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; TwinToken := Token.<span class="me1">LinkedToken</span>;<br />
&nbsp; &nbsp; PrintTokenInfo<span class="br0">&#40;</span>LToken<span class="br0">&#41;</span>;<br />
&nbsp; <span class="kw1">except</span><br />
&nbsp; &nbsp; <span class="kw1">On</span> E : EJwsclSecurityException <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp; <span class="co1">//error logic here</span><br />
&nbsp; <span class="kw1">end</span>;<br />
&nbsp; <span class="kw1">try</span><br />
&nbsp; &nbsp; <span class="co1">//do stuff here</span><br />
&nbsp; <span class="kw1">finally</span><br />
&nbsp; &nbsp; TwinToken.<span class="me1">Free</span>;<br />
&nbsp; &nbsp; Token.<span class="me1">Free</span>;<br />
&nbsp; <span class="kw1">end</span>;</div>
<p>Both tokens are linked together. Thus the following conditions are true.</p>
<blockquote><p> Token.LinkedToken = TwinToken<br />
TwinToken.LinkedToken = Token</p></blockquote>
<p>You can use the twin token in any way you treat a token if you are powerful enough. Use it in CreateProcess or impersonate the user to do things as the user.</p>
<p>At the end I post a quite useful piece of code that displays primary information about a token.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">uses</span> &#8230;, JwsclSecurityId,&#8230;;<br />
<span class="kw1">procedure</span> PrintTokenInfo<span class="br0">&#40;</span><span class="kw1">const</span> Token : TJwSecurityToken<span class="br0">&#41;</span>;<br />
<span class="kw1">var</span> SID, SID2 : TJwSecurityID;<br />
<span class="kw1">begin</span><br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span><span class="st0">&#8216;Access: &#8216;</span>,JwFormatAccessRights<span class="br0">&#40;</span>Token.<span class="me1">AccessMask</span>,TokenMapping <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; Sid := Token.<span class="me1">TokenUser</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span><span class="st0">&#8216;TokenUser: &#8216;</span>,SID.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; Sid.<span class="me1">Free</span>;</p>
<p>&nbsp; Sid := Token.<span class="me1">TokenOwner</span>;<br />
&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span><span class="st0">&#8216;TokenOwner: &#8216;</span>,SID.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; Sid.<span class="me1">Free</span>;</p>
<p>&nbsp; <span class="kw3">writeln</span><span class="br0">&#40;</span><span class="st0">&#8216;TokenGroups: &#8216;</span><span class="re1">#<span class="nu0">13</span></span><span class="re1">#<span class="nu0">10</span></span>,Token.<span class="me1">TokenGroups</span>.<span class="me1">GetText</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">end</span>;</div>
<p><strong>Tell me how you liked this blog entry by adding a comment.</strong></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.delphi-jedi.net/2008/03/04/get-the-windows-vista-twin-token/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
