<?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>jw0rd.net &#187; MS SQL</title>
	<atom:link href="http://jw0rd.net/category/ms-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://jw0rd.net</link>
	<description>Archived Tech Knowledge</description>
	<lastBuildDate>Mon, 18 May 2009 00:09:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Basic MS SQL Injection</title>
		<link>http://jw0rd.net/2009/04/12/basic-ms-sql-injection/</link>
		<comments>http://jw0rd.net/2009/04/12/basic-ms-sql-injection/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 00:01:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MS SQL]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://jw0rd.net/?p=252</guid>
		<description><![CDATA[The most commonly used entry method for web applications today is through SQL injection. Anyone with just a little knowledge of SQL syntax can obtain vital information from a business or inject malicious code to be rendered when anonymous users visit a page. The purpose of the following article is to educate developers and administrators [...]]]></description>
			<content:encoded><![CDATA[<p>The most commonly used entry method for web applications today is through SQL injection. Anyone with just a little knowledge of SQL syntax can obtain vital information from a business or inject malicious code to be rendered when anonymous users visit a page. The purpose of the following article is to educate developers and administrators of the security vulnerabilities that may exist in their publicly accessible web sites.</p>
<p>Example:<br />
<code>http://jw0rd.com/test2.cfm?id=1</code></p>
<p>A basic database driven ColdFusion page that uses the URL parameter &#8216;id&#8217; in a SQL SELECT statement. This is what the output of the page is supposed to look like:</p>
<p><img src="http://jw0rd.net/wp-content/uploads/2009/04/correct.jpg" alt="correct" title="correct" width="406" height="427" class="alignnone size-full wp-image-254" /></p>
<p>The coding on this page selects a record from the &#8216;articles&#8217; table where id = #URL.id#</p>
<p><code>select * from articles where id = #URL.id#</code></p>
<p>It then outputs the title, category, and article fields for that row where id = #URL.id#. Like a lot of poorly coded web applications there is no sanitation on the URL parameter &#8216;id&#8217;, so lets alter the SQL statement and add a UNION.</p>
<p>URL encoding &#8211; <a href="http://www.w3schools.com/TAGS/ref_urlencode.asp">http://www.w3schools.com/TAGS/ref_urlencode.asp</a><br />
%20 = [space]<br />
%27 = &#8216;</p>
<p><code>http://jw0rd.com/test2.cfm?id=1%20union%20select%201,%27%27,login,password%20from%20admin</code></p>
<p>The full SQL statement is now:<br />
<code>select * from articles union select 1,'',login,password from admin</code></p>
<p>The output on the page is now two records, one from the articles table and one from the admin table.</p>
<p><img src="http://jw0rd.net/wp-content/uploads/2009/04/oops.jpg" alt="oops" title="oops" width="601" height="509" class="alignnone size-full wp-image-266" /></p>
<p>Now let&#8217;s add a select on @@version to determine the version of SQL the database server is running and also SYSTEM_USER to see if its possibly using a high privileged SQL account such as &#8217;sa&#8217;.</p>
<p><code>http://jw0rd.com/test2.cfm?id=1%20union%20select%201,@@version,SYSTEM_USER,%27%27%20from%20admin%20order%20by%20title</code></p>
<p>The full SQL statement is now:<br />
<code>select * from articles union select 1,@@version,SYSTEM_USER,'' from admin order by title</code></p>
<p><img src="http://jw0rd.net/wp-content/uploads/2009/04/oops2.jpg" alt="oops2" title="oops2" width="723" height="555" class="alignnone size-full wp-image-273" /></p>
<p>Now that you get the idea of how to exploit through SQL injection how do you prevent it? ALL variables that can be POST&#8217;d or GET&#8217;d from an anonymous user need to be sanitized. For our ColdFusion example above the use of the &#8216;cfqueryparam&#8217; tag will get the job done -> <a href="http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_18.html">http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_18.html</a></p>
<p>Original, vulnerable SQL Statement in test2.cfm:</p>
<p><code>select * from articles where id = #URL.id#</code></p>
<p>Updated, secure SQL statement:</p>
<p><code>select * from articles where id = &lt;cfqueryparam value="#URL.id#" CFSQLType = "CF_SQL_INTEGER"&gt;</code></p>
<p>Now that we are forcing that variable to be of type integer lets try to inject a SQL statement again.</p>
<p><img src="http://jw0rd.net/wp-content/uploads/2009/04/good.jpg" alt="good" title="good" width="818" height="400" class="alignnone size-full wp-image-291" /></p>
<p>SQL injection is no longer possible through the &#8216;id&#8217; parameter:</p>
<blockquote><p>Invalid data 1 union select 1,&#8221;,login,password from admin for CFSQLTYPE CF_SQL_INTEGER.</p></blockquote>
<p>Another security risk is the raw ColdFusion error that displays a snippet of our code to an anonymous user. Setting up custom error handling so that is not seen is always a good idea but is an entirely different subject.</p>
<p>Hopefully I will have some extra time soon to post a similar article related to basic MySQL injection techniques.</p>
]]></content:encoded>
			<wfw:commentRss>http://jw0rd.net/2009/04/12/basic-ms-sql-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
