<?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; Secure cookie</title>
	<atom:link href="https://www.exploresecurity.com/tag/secure-cookie/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>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>
