<?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>Explore Security &#187; Cookies</title>
	<atom:link href="https://www.exploresecurity.com/tag/cookies/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.exploresecurity.com</link>
	<description>IT security tools, techniques and commentary</description>
	<lastBuildDate>Wed, 15 Jun 2022 09:21:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6.1</generator>
		<item>
		<title>Session Fixation and XSS Working Hand-in-Hand</title>
		<link>https://www.exploresecurity.com/session-fixation-and-xss-working-hand-in-hand/</link>
		<comments>https://www.exploresecurity.com/session-fixation-and-xss-working-hand-in-hand/#comments</comments>
		<pubDate>Sat, 06 Sep 2014 22:05:41 +0000</pubDate>
		<dc:creator>Jerome</dc:creator>
				<category><![CDATA[Penetration Testing]]></category>
		<category><![CDATA[Web Applications]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[session fixation]]></category>
		<category><![CDATA[web apps]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.exploresecurity.com/?p=212</guid>
		<description><![CDATA[Often a combination of security flaws come together to produce a unique attack vector. Individually the flaws may not amount to much but together they make an interesting combo. This is invariably more interesting from a pentesting point of view because you know that a tool couldn&#8217;t positively find it. Session fixation is one such [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Often a combination of security flaws come together to produce a unique attack vector. Individually the flaws may not amount to much but together they make an interesting combo. This is invariably more interesting from a pentesting point of view because you know that a tool couldn&#8217;t positively find it. Session fixation is one such scenario because usually a few requirements must be met for the attack to work. I thought I&#8217;d write up a recent session fixation flaw because the act of forcing the cookie onto the victim involved a little twist on overwriting session cookies that made a reflective XSS attack last a lot longer while also laughing in the face of <code>httponly</code>.<span id="more-212"></span></p>
<p>As long as an attacker can remotely force the victim to use a known session cookie that becomes authenticated, you&#8217;ve found session fixation, but it&#8217;s one of those flaws that can be achieved in multiple ways that are subtly different from one another. In this particular instance the session fixation attack ran like this:</p>
<h3>1. The attacker makes up a session cookie</h3>
<p>Yes, the application accepted client-generated cookies.</p>
<h3>2. The attacker makes a specific request using the fabricated cookie</h3>
<p>This was a bit odd but if the session cookie from a request was not recognised by the application (whether it was made up or, more usually, it had just been set by the previous response page) then a background XHR request was made that effectively &#8220;authorised&#8221; the cookie. Okay, whatever, so the attacker does this.</p>
<h3>3. The login page suffered from XSS so the attacker crafts a malicious link to set the known cookie on the victim.</h3>
<p>The malicious link was something like this (I&#8217;ve removed the URL-encoding to make it easier to read):</p>
<p style="padding-left: 30px;"><code>https://www.example.com/login?param="&gt;&lt;script&gt;document.cookie="PHPSESSID=attackerCookie; path=/login; expires=Tue, 06-Aug-2024 00:00:01 GMT"&lt;/script&gt;</code></p>
<p>In more &#8220;traditional&#8221; session fixation the attacker&#8217;s cookie is a parameter in the request that the attacker tricks the victim into making, so using XSS seems a bit like cheating. Unfortunately (for me) the application didn&#8217;t accept a session cookie in the &#8220;traditional&#8221; way and XSS was my only option. OWASP does credit XSS in its description of <a href="https://www.owasp.org/index.php/Session_fixation">session fixation</a> but, that aside, not only did XSS help the session fixation attack but the session fixation flaw helped the XSS attack&#8230;One reason for this was that a simple <code>document.cookie</code> session hijack through XSS was restricted by the response to the above request:</p>
<p style="padding-left: 30px;"><code>Set-Cookie: PHPSESSID=serverCookie; path=/; secure; HttpOnly</code></p>
<p>Because of the <code>httponly</code> flag, the XSS payload could not pull out the session cookie and send it to the attacker. Of course, many other interesting XSS options are still possible, such as rewriting the form&#8217;s <code>action</code> attribute so that the login credentials would be sent to the attacker. However, one advantage of the session fixation approach (apart from the interest of seeing it working) is that <em>nothing</em> is sent to the attacker. Furthermore, as I&#8217;ll explain later (and you may have already spotted from the XSS payload) this attack has the potential to be more long-term than knowing the username and password.</p>
<p>The <code>httponly</code> flag has another effect: the XSS payload can neither read it nor overwrite it. This behaviour isn&#8217;t standardised, it&#8217;s one of those grey areas that Michal Zalewski covers <a href="https://code.google.com/p/browsersec/wiki/Part2#Same-origin_policy_for_cookies">so</a> <a href="http://lcamtuf.coredump.cx/tangled/">well</a> but in this case it&#8217;s not so grey. IE 10, Chrome 37 and Firefox 30 all behaved in the same way. But you might have noticed that the XSS payload included a <code>path</code> of /login when it set the session cookie. This is where the attacker wins as now the browser doesn&#8217;t see this as an overwrite but as a different cookie altogether&#8230;</p>
<h3>4. The victim logs in and the session cookie becomes authenticated</h3>
<p>The server-generated session cookie included a <code>path=/</code> directive so when the victim logs in to /login the attacker&#8217;s cookie has precedence (in that it&#8217;s listed first) because the path is a more specific match to the target page:</p>
<p style="padding-left: 30px;"><code>Cookie: PHPSESSID=attackerCookie; PHPSESSID=serverCookie</code></p>
<p>The application processed the first cookie with the login, thus the attacker&#8217;s fabricated cookie became authenticated and was associated with the victim&#8217;s account. Of course if the session cookie had been changed after authentication, which is best practice, the attack would have failed. Note that the attacker can also set the <code>domain</code> attribute of the cookie to .example.com to try to widen the impact of the attack.</p>
<h3>5. The attacker uses the known session cookie to masquerade as the victim</h3>
<p>The last thing to mention is the long-term nature of this attack. Although logging off did detach the session cookie from the victim&#8217;s account, leaving it unauthenticated, the application did not clear it (again, best practice). So the next time the website is visited, the attacker&#8217;s session cookie will again be offered, accepted and authenticated. Because the XSS payload effectively makes the cookie permanent by setting a long expiry date, the attacker has access to the account of anyone that logs in using the compromised browser in the future. Of course, the persistence of the attack dies as soon as the browser&#8217;s cookie cache is cleared &#8211; but how often does that happen? For what is, after all, a <em>reflected</em> XSS attack, you&#8217;d be certain to get a good return. Indeed, if the browser is shared among users (e.g. at home or at internet kiosks), a single XSS attack can exploit multiple users of the website, making it a one-to-many attack, which you don&#8217;t tend to associate with reflective XSS.</p>
<p>I&#8217;m not claiming any of this is particularly novel, by the way. I just enjoyed finding the XSS path trick for myself and using it with session fixation to poke fun at <code>httponly</code> and to create a more persistent version of a reflective XSS attack. It also demonstrated that best practice points, although seemingly trivial when taken alone, can help to stop or mitigate more complex attacks.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.exploresecurity.com/session-fixation-and-xss-working-hand-in-hand/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Rough Guide to the Secure Cookie</title>
		<link>https://www.exploresecurity.com/a-rough-guide-to-the-secure-cookie/</link>
		<comments>https://www.exploresecurity.com/a-rough-guide-to-the-secure-cookie/#comments</comments>
		<pubDate>Thu, 26 Sep 2013 00:08:54 +0000</pubDate>
		<dc:creator>Jerome</dc:creator>
				<category><![CDATA[Web Applications]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[Same Origin Policy]]></category>
		<category><![CDATA[Secure cookie]]></category>
		<category><![CDATA[SOP]]></category>

		<guid isPermaLink="false">http://www.exploresecurity.com/?p=59</guid>
		<description><![CDATA[On a recent pentest, I reported that the session management cookie was not being set with the secure flag (surprise, surprise)&#8230; But in this case it didn&#8217;t seem to matter so much. Why? Because port 80 was shut: the whole application was served over HTTPS so the cookie would never have the chance to escape over HTTP, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>On a recent pentest, I reported that the session management cookie was not being set with the <code>secure</code> flag (surprise, surprise)&#8230;</p>
<div id="attachment_111" class="wp-caption alignnone" style="width: 543px"><a href="http://www.exploresecurity.com/wp-content/uploads/2013/07/cookie1.png"><img class="size-full wp-image-111" alt="No secure flag" src="http://www.exploresecurity.com/wp-content/uploads/2013/07/cookie1.png" width="533" height="178" /></a><p class="wp-caption-text">No secure flag</p></div>
<p>But in this case it didn&#8217;t seem to matter so much. Why? Because port 80 was shut: the whole application was served over HTTPS so the cookie would never have the chance to escape over HTTP, even if an attacker could engineer such a scenario &#8211; for example, by enticing the victim to click on a HTTP link (although more on this shortly). This is because the TCP connection must be set up with the 3-way handshake before any application data is sent over it &#8211; and since the port was closed, the connection falls at the first hurdle <sup>[1]</sup>.<span id="more-59"></span></p>
<p>Nevertheless I still reported the missing <code>secure</code> flag because the application was in a test environment and what if port 80 was open on the live server? Now the cookie is more vulnerable. And there&#8217;s another consideration. While port 80 on the host under scrutiny (e.g. <em>secure.example.com</em>) was filtered, port 80 may have been open on another related host (e.g. <em>test.secure.example.com</em> or <em>www.example.com</em>). If the attacker used a HTTP link to another such host instead, then when the link was clicked, the cookie could be transmitted. You&#8217;ll notice a &#8220;could&#8221; there &#8211; it depends on any accompanying <code>domain</code> and <code>path</code> restrictions on the cookie when it was set (and possibly how the browser interprets them).</p>
<div id="attachment_123" class="wp-caption alignnone" style="width: 667px"><a href="http://www.exploresecurity.com/wp-content/uploads/2013/09/cookie2.png"><img class="size-full wp-image-123" alt="foo.example.com sets a cookie over HTTPS with domain=.example.com" src="http://www.exploresecurity.com/wp-content/uploads/2013/09/cookie2.png" width="657" height="132" /></a><p class="wp-caption-text">foo.example.com sets a cookie over HTTPS with domain=.example.com</p></div>
<div id="attachment_122" class="wp-caption alignnone" style="width: 438px"><a href="http://www.exploresecurity.com/wp-content/uploads/2013/09/cookie3.png"><img class="size-full wp-image-122" alt="A request to bar.example.com over HTTP includes the cookie" src="http://www.exploresecurity.com/wp-content/uploads/2013/09/cookie3.png" width="428" height="128" /></a><p class="wp-caption-text">A request to bar.example.com over HTTP includes the cookie</p></div>
<p>The classic protocol-host-port tuple of the Same Origin Policy (SOP) doesn&#8217;t apply to the scoping of cookies. If it was, we wouldn&#8217;t need the <code>secure</code> flag because, assuming the cookie was set as <code>secure</code> over HTTPS, the request over HTTP would be a change of protocol and port, so it wouldn&#8217;t be transmitted. Cookie security pre-dates SOP as we know it (or sort of know it!) and its definition of &#8220;same origin&#8221; is different. I recommend Michal Zalewski&#8217;s <a style="line-height: 1.714285714; font-size: 1rem;" href="https://code.google.com/p/browsersec/wiki/Part2#Same-origin_policy_for_cookies" target="_blank"><em>Browser Security Handbook</em></a><span style="line-height: 1.714285714; font-size: 1rem;"> or his book </span><a style="line-height: 1.714285714; font-size: 1rem;" href="http://lcamtuf.coredump.cx/tangled/" target="_blank"><em>The Tangled Web</em></a> for more details, then fire up your web proxy and experiment!  And it was while doing just this that I discovered another side to the <code>secure</code> cookie.</p>
<p>I was testing the rules applied to JavaScript&#8217;s access to cookies (as opposed to the rules applied to transmission). I normally think of <code>httponly</code> as the principal attribute governing access to cookies by JavaScript but that&#8217;s not so. The <code>domain</code> and <code>path</code> attributes also play a part so that the browser&#8217;s policy for cookie access by JavaScript mirrors the transmission policy, which makes sense when you think about it. For example, if a cookie was set with the domain restricted to <em>foo.example.com</em>, it wouldn&#8217;t be transmitted with a request to <em>bar.example.com</em> so why should JavaScript from <em>bar.example.com</em> be able to access that cookie?</p>
<p>For the same reason, the <code>secure</code> flag also has a role in determining JavaScript&#8217;s access to cookies. I found that a cookie set as <code>secure</code> but not <code>httponly</code> was inaccessible to JavaScript delivered over HTTP from the same <em>domain</em> and <em>path</em>. This was independent of whether the cookie was set over HTTP or HTTPS. Again, the reason is because the cookie would not be transmitted under those conditions.</p>
<p>This all makes complete sense, it&#8217;s just that I&#8217;ve not seen any reference to this behaviour in the likes of the <a style="line-height: 1.714285714; font-size: 1rem;" href="http://mdsec.net/wahh/" target="_blank"><i>Web Application Hacker&#8217;s Handbook</i></a>, <i style="line-height: 1.714285714; font-size: 1rem;">The Tangled Web</i> or the cookie <a style="line-height: 1.714285714; font-size: 1rem;" href="http://tools.ietf.org/rfc/rfc6265.txt" target="_blank">RFC 6265</a>. I guess it&#8217;s just implicit from the specification but sometimes it&#8217;s not until you think things through that the implicit becomes obvious! One thing to note, though, is that although the HTTP-delivered JavaScript could not access the <code>secure</code> cookie, it could <em>overwrite</em> the cookie &#8211; what&#8217;s known as &#8220;clobbering&#8221;.)</p>
<p>Incidentally, Internet Explorer 10 seems to have corrected the bug noted in the <em>Browser Security Handbook</em>:</p>
<p style="padding-left: 30px;"><em>There is no way to limit cookies to a single DNS name only, other than by not specifying <code>domain=</code> value at all &#8211; and even this does not work in Microsoft Internet Explorer&#8230;</em></p>
<p>It appears this statement covered IE versions 6, 7 and 8. Well, I can&#8217;t speak for 9 but with IE10 I noticed that a cookie from <em>foo.example.com</em> with no <code>domain</code> attribute wasn&#8217;t later sent to <em>example.com</em>.</p>
<p>To finish off, remember that you can only set the <code>secure</code> flag if the application is designed with this in mind since any HTTP request will be missing cookies that have been set as <code>secure</code>. An obvious side effect of this, for example, could be that the newly authenticated session management cookie set over HTTPS is withheld over HTTP, effectively logging off the user. Of course, whether or not the application should revert to HTTP following secure authentication is another matter!</p>
<p>===== UPDATE =====</p>
<p><sup>[1]</sup>   Dafydd Stuttard, aka PortSwigger, pointed out a neat bypass using the link <em>http://www.example.com:443</em>. Now the TCP connection succeeds and the browser sends the HTTP request. The SSL connection will of course fail but it&#8217;s too late, the session management cookie has gone up in the plain text request. (This is why he wrote the <em>Web Application Hacker&#8217;s Handbook</em> and I didn&#8217;t.) He also noted that since the attacker is in a position to sniff, having seen the SYN to port 80, the SYN/ACK could be spoofed in order to trick the browser into thinking the port is open. Thinking about this further, in an open Wi-Fi network the attacker could do this without even being an active man-in-the-middle using the <a href="http://airpwn.sourceforge.net/Documentation.html">Airpwn</a> principle.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.exploresecurity.com/a-rough-guide-to-the-secure-cookie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
