<?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>Casaba Security &#187; Code Review</title>
	<atom:link href="http://www.casaba.com/blog/category/code-review/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.casaba.com/blog</link>
	<description>Building and breaking software and robots</description>
	<lastBuildDate>Wed, 11 Jan 2012 18:08:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Microsoft &#8220;Roslyn&#8221; based REPL injection.</title>
		<link>http://www.casaba.com/blog/2011/12/microsoft-roslyn-based-repl-injection/</link>
		<comments>http://www.casaba.com/blog/2011/12/microsoft-roslyn-based-repl-injection/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 18:05:12 +0000</pubDate>
		<dc:creator>John Hernandez</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Security Testing]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.casaba.com/blog/?p=277</guid>
		<description><![CDATA[Microsoft recently released their new Compiler API codename &#8220;Roslyn&#8221;. If you haven&#8217;t checked it out yet you should. Here&#8217;s the link: http://msdn.microsoft.com/en-us/roslyn/. I wanted to get my hands a little dirty and play with the new API. I&#8217;ve been meaning to look into Managed DLL injection for a while to get code execution for process [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft recently released their new Compiler API codename &#8220;Roslyn&#8221;. If you haven&#8217;t checked it out yet you should. Here&#8217;s the link: http://msdn.microsoft.com/en-us/roslyn/.</p>
<p>I wanted to get my hands a little dirty and play with the new API. I&#8217;ve been meaning to look into Managed DLL injection for a while to get code execution for process interrogation. There are times when we&#8217;re testing that we want to interrogate a process for framework level information. For whatever reasons we sometimes can&#8217;t compile the target with hooks. So it would be nice to have a way to execute code. Roslyn’s CSX files look like a great way to accomplish this so that&#8217;s what I&#8217;m trying to expose. </p>
<p>Currently this only works on 32 bit processes.  </p>
<p>Let&#8217;s start by describing the architecture as there are 3 things going on. The major components are the Injector, Unmanaged Injectee and Managed Injectee. The injector is the controller in this scenario; he&#8217;s responsible for the injection into the managed process and communication between the components. Communication is handled via named pipes. </p>
<p>The injector uses a well-documented dll injection technique via CreateRemoteThread and LoadLibrary. This loads the unmanaged dll into the Managed process. The unmanaged DLL actually handles the Managed DLL injection. I wont go into unmanaged dll injection as it&#8217;s pretty well document technique. I assume the reader understands these concepts. </p>
<p>From this point I assume the unmanaged DLL has been injected into the managed process. </p>
<p>After the unmanaged DLL is injected I need to make sure the correct version of the CLR is loaded. To accomplish this use the CLR hosting API’s to determine the version of the CLR that is loaded by the process (Provided there is one loaded). The host process must be running .Net 4.0 to support the Roslyn API. Because the early versions of the hosting API&#8217;s are deprecated I need to check to see if the .net 4.0 mscoree is loaded &#8220;msvcr100_clr0400.dll&#8221;. I check via a GetModuleHandle. If it exists we know we are running .Net 4.0 and know the CLR is already running. Two birds down with a single stone. </p>
<pre>
hMod = GetModuleHandle(L"msvcr100_clr0400.dll");
</pre>
<p>Once we know the CLR is loaded and it’s 4.0 we can get a handle to the CLSID_CLRMetaHost via:</p>
<pre>
hr = CLRCreateInstance( CLSID_CLRMetaHost,
IID_ICLRMetaHost,
(LPVOID*)&amp;pMetaHost );
</pre>
<p>From the meta host we can get a handle to the running RunTimeHost via:</p>
<pre>
ICLRRuntimeHost *pClrHost = NULL;
runTimeInfo-&gt;GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID *)&amp;pClrHost);
</pre>
<p>This will return a handle to the current RuntimeHost (Or load the runtime if it isn’t running). The next call is to load my Managed DLL plus call the entry method.</p>
<pre>
pClrHost-&gt;ExecuteInDefaultAppDomain(L"InjectedManagedDll_Net_4.dll", L"InjectedManagedDll_Net_4.InjectedClass", L"Test", L"TestArg" , &amp;ret);
</pre>
<p>This loads the Managed DLL into the process. Once the Managed DLL’s Test method is called I create a managed thread.</p>
<pre>
public static int Test(string param)
{
new Thread(new ThreadStart(ThreadFunc)).Start();
return 666;
}
</pre>
<p>This thread then generates a few more threads and sets up the NamedPipe communication pipe and reports to the server things are setup.</p>
<pre>
static void ThreadFunc()
{
try
{
PipeClient.Instance.Start("CNIPipe");
}
catch (Exception e)
{
PipeClient.Instance.LogMessageToServer(e.Message);
}
}
</pre>
<p>I then expose some simple messages back and forth between the injector and injectee and expose a simple REPL loosely based on this guy’s implementation: http://visualstudiomagazine.com/articles/2011/11/16/the-roslyn-scripting-api.aspx.</p>
<pre>
private ScriptHost()
{

HashSetassemblys = new HashSet();
assemblys.Add(Assembly.GetCallingAssembly());
assemblys.Add(Assembly.GetEntryAssembly());
assemblys.Add(Assembly.GetExecutingAssembly());

Listnamespaces = new List() { "System", "System.Collections", "System.Collections.Generic" };

ScriptEngine = new ScriptEngine(assemblys.ToList(), namespaces);

Session = Session.Create(this);
}

public object Execute(string code)
{
return ScriptEngine.Execute(code, Session);
}
</pre>
<p>This gets you a basic REPL inside another process. Next steps include making sure the communication API between the host and injectee are more well formed and able to handle both 32 and 64 bit processes. Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2011/12/microsoft-roslyn-based-repl-injection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>X5S V2.0&#8230;. its coming!</title>
		<link>http://www.casaba.com/blog/2011/01/x5s-v2-0-its-coming/</link>
		<comments>http://www.casaba.com/blog/2011/01/x5s-v2-0-its-coming/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 16:59:22 +0000</pubDate>
		<dc:creator>John Hernandez</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Nebulous]]></category>
		<category><![CDATA[Security Testing]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://www.casaba.com/blog/?p=233</guid>
		<description><![CDATA[So, It&#8217;s been awhile since we&#8217;ve done any public updates to X5S. Over the last year, I&#8217;ve improved the algorithm and process significantly. Be on the look out, it should be released within the next couple of weeks (Sometime in Jan. 11). Some of the improvements include: * Better Algorithms for doing checks * Better [...]]]></description>
			<content:encoded><![CDATA[<p>So, It&#8217;s been awhile since we&#8217;ve done any public updates to X5S. Over the last year, I&#8217;ve improved the algorithm and process significantly. Be on the look out, it should be released within the next couple of weeks (Sometime in Jan. 11).</p>
<p>Some of the improvements include:<br />
* Better Algorithms for doing checks<br />
* Better output format .. Now uses a tree view.. Going to add better support for reporting too..<br />
* Cleaner UI (Easier to use)<br />
* Re-factored the code to be cleaner/make more sense and easier to maintain. It&#8217;s much easier to understand/work with.. before was mostly prototyped code/ Alpha code.<br />
* Changed how test cases are defined for more control over the types of injects<br />
* Added a fuzzing mode that will take data from a file and inject it where canaries would normally be injected. (This can be slow with lots of injections)<br />
* Added a replay from Fiddler capture.. (Replays the capture while fuzzing/injecting on the requests). </p>
<p>* many many more minor/significant changes..  =)</p>
<p>Check back soon for a release date!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2011/01/x5s-v2-0-its-coming/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Asp .Net MVC Security Review Checklist</title>
		<link>http://www.casaba.com/blog/2010/10/asp-net-mvc-security-review-checklist/</link>
		<comments>http://www.casaba.com/blog/2010/10/asp-net-mvc-security-review-checklist/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 18:15:49 +0000</pubDate>
		<dc:creator>John Hernandez</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">https://www.casabasecurity.com/blog/?p=225</guid>
		<description><![CDATA[Here’s a little checklist I put together for ASP .Net MVC. It includes the high level stuff to look at when reviewing a MVC application. In order to fully understand/consume the info it requires at least a basic understanding of MVC architecture which can be gained from any introduction document. Some of this stuff could [...]]]></description>
			<content:encoded><![CDATA[<p>Here’s a little checklist I put together for ASP .Net MVC. It includes the high level stuff to look at when reviewing a MVC application. In order to fully understand/consume the info it requires at least a basic understanding of MVC architecture which can be gained from any introduction document. Some of this stuff could be automated through something like FxCop.</p>
<p>Below is an outline of the different components of MVC that are important to look into when reviewing ASP .Net MVC applications. It’s also important to review the normal MVC spec’s to understand context, but here is a “security” minded checklist.</p>
<h2>ViewData Dictionary</h2>
<p>This is used to pass data from a Controller to the View. It’s extremely important to verify the ViewData on both the View and Controller. The ViewData is a dictionary for passing untyped data to the view. It’s important to make a recommendation that either data is encoded on the input or the output and try not to mix the two as you can get yourself in trouble. I think output encoding is easier to manage than input.<br />
Look at the controller actions and the parameters they receive. If they are strings, verify on the output or input encoding occurs for the appropriate uses.  A grep to look for:</p>
<p>ViewData\[“”</p>
<h2>ViewData.Model</h2>
<p>The ViewData.Model is a method for passing TYPED objects back to the view. You can specify a ViewPage&lt;T&gt; where T can be a typed to the value of ViewData.Model. In your controller when you return a View you can pass the object to the page. Example would be return View(“Index”, objOfTypeT)</p>
<p>So it’s important to understand the type being used in this case as unencoded data can make it to the view from this object.</p>
<h2>AntiForgeryToken Attribute</h2>
<p>Any post which would require Anti-CSRF protections should include the AntiForgeryToken Attribute on the controller Action where the post submits to. There is also a requirement on the View side of things to ensure that the AntiForgeryToken is actually used. Make sure that Html.AntiForgeryToken() method is being called within the form definition on the view.</p>
<h2>Public Methods in the Controllers</h2>
<p>Any public methods contained in the controller are accessible via crafted URLs. Verify there are no unintended consequences from public methods, also validate that authZ occurs on appropriate methods.</p>
<h2>Direct browsing View pages</h2>
<p>The View directory for the MVC application contains a web.config for blocking access to view pages among other view wide configurations. It’s important to note that by default .aspx pages are blocked, however if there is a custom extension being used you will to ensure they are explicitly blocked via config. Below are the relevant keys</p>
<p>IIS 6<br />
&lt;add path="*.aspx" verb="*" type="System.Web.HttpNotFoundHandler"/&gt;</p>
<p>IIS 7<br />
&lt;add name="BlockViewHandler" path="*.aspx" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/&gt;</p>
<h2>Controller Action Parameters</h2>
<p>Action parameters can be complex types. For example you could have Action with a method signature like below.</p>
<p>[AcceptVerbs("POST")]<br />
public ActionResult Edit(Product product)</p>
<p>In this case Product could have some public properties associated with it like {Name, Id, etc.}. If Price was meant to be set later by the db after a look occurred on the product id you could end up in trouble depending on the logic.</p>
<p>The root problem here is if the complex type exposes a property that was not supposed to be set by the client you could inadvertently end up with a tainted object. Also it introduces a place where unencoded data could make it to the view depending on how the object is used.<br />
Here is a link that explains it better and more in detail.<br />
<a href="http://www.codethinked.com/post/2009/01/08/ASPNET-MVC-Think-Before-You-Bind.aspx">http://www.codethinked.com/post/2009/01/08/ASPNET-MVC-Think-Before-You-Bind.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2010/10/asp-net-mvc-security-review-checklist/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Microsoft CCI Framework for Deobfuscating .Net binaries. (Part 3)</title>
		<link>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-3/</link>
		<comments>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-3/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 18:04:51 +0000</pubDate>
		<dc:creator>John Hernandez</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Malware]]></category>
		<category><![CDATA[Nebulous]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[Babel]]></category>
		<category><![CDATA[CCI]]></category>
		<category><![CDATA[Deobfuscation]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://www.casabasecurity.com/blog/?p=188</guid>
		<description><![CDATA[Renaming parts of the assembly. So I promised this last week, but I&#8217;ve been busy on a new project. Below is some code that shows renaming of methods. This is a solution to renaming classes within namespaces. It iterates over each namespace renaming classes from class1 -&#62; classN. This is more useful for human readability [...]]]></description>
			<content:encoded><![CDATA[<p>Renaming parts of the assembly.</p>
<p>So I promised this last week, but I&#8217;ve been busy on a new project. Below is some code that shows renaming of methods. This is a solution to renaming classes within namespaces. It iterates over each namespace renaming classes from class1 -&gt; classN. This is more useful for human readability and tracing logic. I leave it as an exercise to the reader to figure out how to rename other parts of the assembly. But hey if you really need it an get stuck, let me know!</p>
<p>I&#8217;ll be posting a tool at some point that does all these different actions for you. Hopefully I&#8217;ll have a early release out by mid next month. I&#8217;m currently learning WPF well enough to build in visulalizations of the control flow graph. That way after a mutator is applied you can visually see the results.</p>
<p>There is a dictionary in the mutator class that uses the namespace string as a key in order to know which class # i left off at. I test on the string length &lt; 2 because the obfuscators I&#8217;ve seen that do this trick tend to just rename everything to some obscure unicode code point of length 1. Just a easy stop condition. You can use any stop condition that suits your needs.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p188code2'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1882"><td class="code" id="p188code2"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> NamespaceTypeDefinition Visit<span style="color: #008000;">&#40;</span>NamespaceTypeDefinition namespaceTypeDefinition<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span> key <span style="color: #008000;">=</span> namespaceTypeDefinition<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainingUnitNamespace</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>classDict<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
     classDict<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>key, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>namespaceTypeDefinition<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
     <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> classDict<span style="color: #008000;">&#91;</span>key<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
     namespaceTypeDefinition<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">host</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NameTable</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetNameFor</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Class{0}&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     i<span style="color: #008000;">++;</span>
     classDict<span style="color: #008000;">&#91;</span>key<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Visit</span><span style="color: #008000;">&#40;</span>namespaceTypeDefinition<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2010/02/microsoft-cci-framework-for-deobfuscating-net-binaries-part-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Preventing Security Development Errors: Lessons Learned at Windows Live by Using ASP.NET MVC</title>
		<link>http://www.casaba.com/blog/2009/11/preventing-security-development-errors-lessons-learned-at-windows-live-by-using-asp-net-mvc/</link>
		<comments>http://www.casaba.com/blog/2009/11/preventing-security-development-errors-lessons-learned-at-windows-live-by-using-asp-net-mvc/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 21:42:45 +0000</pubDate>
		<dc:creator>Chris Weber</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Casaba had the opportunity to contribute to a new Microsoft paper regarding ASP.NET MVC security. It&#039;s online through the SDL pages, and here&#39;s the paper&#39;s direct link. A short summary of the paper follows. The SDL preaches &#039;secure by default&#039;. When Windows Live moved to ASP.Net MVC, they used that opportunity to build mitigations into [...]]]></description>
			<content:encoded><![CDATA[<p>Casaba had the opportunity to contribute to a new Microsoft paper regarding ASP.NET MVC security.  It&#039;s online through the <a href="http://www.microsoft.com/security/sdl/"> SDL pages</a>, and here&#39;s the paper&#39;s <a href="http://go.microsoft.com/?linkid=9695423">direct link</a>.  A short summary of the paper follows.</p>
<p>The SDL preaches &#039;secure by default&#039;. When Windows Live moved to ASP.Net MVC, they used that opportunity to build mitigations into the framework that prevent developers from making accidental errors which result in security flaws. Specifically, they targeted these three security issues – XSRF, Open redirects and JSON hijacking. </p>
<p>For XSRF, the mitigation was that all HTTP requests are checked for a canary by default except for HTTP GET requests. Developers can also opt-out specific pages or functionality. This automatic ‘on-by-default’ canary checking prevents accidental errors which lead to XSRF. </p>
<p>For Open redirects, Windows Live added a wrapper around the Redirect result in ASP.Net MVC which checks a list of approved domains. This way when a developer called Redirect and forgot to ensure it was safe, the wrapper would cover them automatically. </p>
<p>For JSON hijacking, they ensure that the JSON result included a canary check by default.  This prevented developers from being able to return JSON without a canary, thus preventing JSON hijacking.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2009/11/preventing-security-development-errors-lessons-learned-at-windows-live-by-using-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>useUnsafeHeaderParsing = what?</title>
		<link>http://www.casaba.com/blog/2008/06/useunsafeheaderparsing-what/</link>
		<comments>http://www.casaba.com/blog/2008/06/useunsafeheaderparsing-what/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 22:02:31 +0000</pubDate>
		<dc:creator>Chris Weber</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Security Testing]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[HTTP]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[As software security people we usually like input restrictions to be tight. With .Net&#39;s HttpWebRequestElement.UseUnsafeHeaderParsing Property you can loosen up the way HTTP requests get parsed. Setting this property ignores validation errors that occur during HTTP parsing. The documentation from MSDN makes it pretty clear. When this property is set to &#039;true&#039; then many HTTP [...]]]></description>
			<content:encoded><![CDATA[<p>
As software security people we usually like input restrictions to be tight.  With .Net&#39;s <a href="http://msdn.microsoft.com/en-us/library/system.net.configuration.httpwebrequestelement.useunsafeheaderparsing(VS.80).aspx" target="_blank">HttpWebRequestElement.UseUnsafeHeaderParsing</a> Property you can loosen up the way HTTP requests get parsed.
</p>
<p>
Setting this property ignores validation errors that occur during HTTP parsing.  The documentation from MSDN makes it pretty clear.  When this property is set to &#039;true&#039; then many HTTP RFC violations will be relaxed and ignored.
</p>
<blockquote style="font-family: Courier New;"><p>
When this property is set to false, the following validations are performed during HTTP parsing:</p>
<p>    *  In end-of-line code, use CRLF; using CR or LF alone is not allowed.<br />
    *  Headers names should not have spaces in them.<br />
    *  If multiple status lines exist, all additional status lines are treated as malformed header name/value pairs.<br />
    *  The status line must have a status description, in addition to a status code.<br />
    *  Header names cannot have non-ASCII chars in them. This validation is performed whether this property is set to true or false.</p>
<p>When a protocol violation occurs, a WebException exception is thrown with the status set to ServerProtocolViolation. If the UseUnsafeHeaderParsing property is set to true, validation errors are ignored.</p>
<p>Setting this property to true has security implications, so it should only be done if backward compatibility with a server is required.
</p>
</blockquote>
<p>Let&#039;s keep an eye out for this option when it&#039;s set either programmatically or through web.config.</p>
<p><code><br />
&lt;configuration&gt;<br />
    &lt;system.net&gt;<br />
        &lt;settings&gt;<br />
            &lt;httpWebRequest useUnsafeHeaderParsing=”true” /&gt;<br />
        &lt;/settings&gt;<br />
    &lt;/system.net&gt;<br />
&lt;/configuration&gt;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2008/06/useunsafeheaderparsing-what/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling Unicode when marshalling from .Net to a platform invoke</title>
		<link>http://www.casaba.com/blog/2008/04/handling-unicode-when-marshalling-from-net-to-a-platform-invoke/</link>
		<comments>http://www.casaba.com/blog/2008/04/handling-unicode-when-marshalling-from-net-to-a-platform-invoke/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 05:09:56 +0000</pubDate>
		<dc:creator>Chris Weber</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Security Testing]]></category>
		<category><![CDATA[Unicode]]></category>
		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[By default, the .Net runtime will marshall a string (and files in a value type) as a LPStr to a platform invoke (p/invoke) function. By default the .Net framework and runtime handles strings as UTF-16. That&#39;s two bytes representing a single Unicode &#39;code point&#39;, and more familiar, a single character. An LPStr on the other [...]]]></description>
			<content:encoded><![CDATA[<p>By default, the .Net runtime will marshall a string (and files in a value type) as a LPStr to a platform invoke (p/invoke) function. By default the .Net framework and runtime handles strings as UTF-16.  That&#39;s two bytes representing a single Unicode &#39;code point&#39;, and more familiar, a single character. An LPStr on the other hand, is an ANSI character, so in order to convert, the runtime will perform a <strong>best-fit conversion</strong> to the classic windows-1252 code page.  This conversion is well-documented here:</p>
<p><a href="http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt">http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt</a></p>
<p>This might not be so surprising to people in tune with Unicode, but it&#39;s can lead to huge security problems when security filters are at risk. For example, if you&#39;re performing HTML filtering or file canonicalization, you need to perform so <strong>after the conversion </strong>to LPStr.</p>
<p>This default marshalling behavior is documented at:  <a href="http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute(VS.71).aspx">http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute(VS.71).aspx</a></p>
<p>To properly and more safely <strong>deal with this</strong>, you can use the MarshallAsAttribute class to specify a <strong>LPWStr </strong>type instead of a LPStr.  For example:</p>
<p>	[MarshalAs(UnmanagedType.LPWStr)]</p>
<p>Because LPWStr is a pointer to a null-terminated array of Unicode characters, this ensures the Unicode code points are preserved across the marshalling.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2008/04/handling-unicode-when-marshalling-from-net-to-a-platform-invoke/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open redirects &#8211; what&#8217;s the problem?</title>
		<link>http://www.casaba.com/blog/2008/03/open-redirects-whats-the-problem/</link>
		<comments>http://www.casaba.com/blog/2008/03/open-redirects-whats-the-problem/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 16:16:01 +0000</pubDate>
		<dc:creator>Chris Weber</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Security Testing]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Been getting this question a bit lately. First off, what&#039;s an open redirect? It&#039;s a function in your application which sends the user to some other location. The redirect could be a response from the server, such as an HTTP 301 or 302 response code, or a META redirect. The redirect can be delivered in [...]]]></description>
			<content:encoded><![CDATA[<p>Been getting this question a bit lately.  First off, what&#039;s an open redirect?  It&#039;s a function in your application which sends the user to some other location.  The redirect could be a response from the server, such as an HTTP 301 or 302 response code, or a META redirect.  The redirect can be delivered in several forms, the important part is that when an attacker can control the redirect location, they can exploit it for nefarious purposes &#8211; usually this means spam or phishing attacks.</p>
<p>For example, your application takes a request from the user, maybe it&#039;s a GET request for a certain page.  Included in the request is a value indicating the location where the user should be redirected once they&#039;ve finished on the page.  So, the user requests a page like:</p>
<p><code>http://somesite.tld/page.aspx?name=value&amp;returnUrl=http://somesite.tld/referringpage.aspx</code></p>
<p>As you can see, the <strong>returnUrl </strong>takes a value of the redirect location.  Then your code acts on it somewhere by redirecting the user with something like:<br />
<code><br />
Response.Redirect(returnUrl);<br />
</code></p>
<p><strong>Spammers and phishers</strong> love this, it gives them <strong>good camouflage</strong>.  For example:</p>
<p><code>http://somesite.tld/page.aspx?name=value&amp;returnUrl=http://evil.tld/installMalware.bad</code></p>
<p>Now imagine the spammer has crafted up a nice email that looks like it originates from somesite.tld, includes all the logos, fonts, etc.  They coerce the victim into clicking this link by saying something like &#8220;your account needs immediate attention&#8221; or &#8220;you&#039;ve won 500 points&#8221;.  User clicks the link, gets redirected to evil.tld, and may not realize that the domain has switched before they say <strong>Yes</strong> to install the thing that the spammer wants them to download.</p>
<p>Tricky, right.  In fact this is a favorite of spam, malware, and phish, next to the old XSS bug.</p>
<p><strong>What&#039;s the solution</strong><br />
Well, simply, don&#039;t redirect openly, rather, implement a SafeRedirect() function that looks something like:</p>
<p><code><br />
public static SafeRedirect(string url) {<br />
// check that protocol is either http:// https:// ftp:// or other specific protos you want to allow<br />
// check that domain is in fact yourdomain.tld<br />
// If these checks pass, then you can go ahead<br />
Response.Redirect(returnUrl);<br />
// If the checks fail, you can try to clean up the URL, but probably best to just fail and redirect to a safe landing page<br />
}<br />
</code><br />
That&#039;s about all there is too it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casaba.com/blog/2008/03/open-redirects-whats-the-problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

