<?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>Seree Woradechjamroen &#187; sql injection</title>
	<atom:link href="http://www.iamseree.com/tag/sql-injection/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iamseree.com</link>
	<description>Keep learning everyday, willing to win and take action</description>
	<lastBuildDate>Mon, 02 Jan 2012 19:00:27 +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>Prevent your .NET application from SQL Injection</title>
		<link>http://www.iamseree.com/application-development/prevent-your-net-application-from-sql-injection</link>
		<comments>http://www.iamseree.com/application-development/prevent-your-net-application-from-sql-injection#comments</comments>
		<pubDate>Mon, 03 Sep 2007 12:53:56 +0000</pubDate>
		<dc:creator>Seree</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql injection]]></category>
		<category><![CDATA[vb.net]]></category>

		<guid isPermaLink="false">http://www.iamseree.com/?p=19</guid>
		<description><![CDATA[<p>Hello everyone,</p> <p>Now I&#8217;ll talking about a technique that script kiddies widely used to attack to the first wall of your application. If you&#8217;re a rookie for security topics on development then you may never heard about this before. In my .NET courses training experiences, most of my trainees never know about this issue before [...]]]></description>
			<content:encoded><![CDATA[<p>Hello everyone,</p>
<p>Now I&#8217;ll talking about a technique that script kiddies widely used to attack to the first wall of your application. If you&#8217;re a rookie for security topics on development then you may never heard about this before. In my .NET courses training experiences, most of my trainees never know about this issue before and they feel very surprises when I&#8217;ve hacked into their system in no time.</p>
<p><strong><span style="text-decoration: underline;">What is SQL injection?</span></strong></p>
<p>Straightly, It&#8217;s something like you try to inject some unexpected characters into SQL querying process to gain the out-of-case result.</p>
<p>Let&#8217;s see it in more detail!</p>
<p>What should you do If you want to coding your application to authenticate user&#8217;s credential that kept in database?</p>
<p>So easy, right? I&#8217;m just querying the result from database with this simple SQL query and a few line of code.</p>
<blockquote><p>Dim strSQL As String = &#8220;Select COUNT(*) From Members WHERE LoginName=&#8217;&#8221; &amp; txtLoginName.Text &amp; &#8220;&#8216; AND Password=&#8217;&#8221; &amp; txtPassword.Text &amp; &#8220;&#8216;&#8221;</p>
<p>Dim cmd As New SqlCommand(strSQL, con)</p>
<p>cmd.ExecuteScalar()</p></blockquote>
<p>It works perfectly! but how the it&#8217;ll handle if a hard core user input something unexpected into login name just like the following</p>
<blockquote><p>xyz&#8217; OR &#8217;1&#8242;=&#8217;1</p></blockquote>
<p>When it concatenate into SQL string. It&#8217;ll result in to&#8230;<span id="more-19"></span></p>
<blockquote><p>Select COUNT(*) From Members<br />
WHERE LoginName=&#8217;<span style="color: #ff0000;">xyz&#8217; OR &#8217;1&#8242;=&#8217;1</span>&#8216; AND Password=&#8217;<span style="color: #ff0000;">1234</span>&#8216;</p></blockquote>
<p>Yeah, you can see that <em><span style="color: #ff0000;">OR &#8217;1&#8242;=&#8217;1&#8242;</span></em> which always result in TRUE. So, the hard core user can authenticate to the application without knowing of any user&#8217;s login name or password.</p>
<p><!--adsense--></p>
<p><strong><span style="text-decoration: underline;">How can I prevent SQL injection?</span></strong></p>
<p>Yeah, it&#8217;s very easy to do. Just use the technique named as &#8220;Parameterized Query&#8221;.</p>
<p>OMG! What&#8217;s about it? I never heard about those &#8220;Parameterized Query&#8221;.<br />
You get me in trouble again!</p>
<p>Not that serious, It&#8217;s very easy to implement this technique as .NET alraedy provide the framework for you. Just do the following two steps.</p>
<p>1. When you want to create the dynamic SQL query string just like this case. You should use parameter instead of concatenate the variables yourself.</p>
<blockquote><p>Select COUNT(*) From Members WHERE LoginName=<span style="color: #ff0000;">@LoginName</span> AND Password=<span style="color: #ff0000;">@Pwd</span></p></blockquote>
<p>We call <span style="color: #ff0000;">@LoginName</span> and <span style="color: #ff0000;">@Pwd</span> as parameter.</p>
<p>2. Before executing the command. Please specify the value for each parameter first.</p>
<blockquote><p>cmd.Parameters.AddWithValue(&#8220;<span style="color: #ff0000;">@LoginName</span>&#8220;, txtLoginName.Text)</p>
<p>cmd.Parameters.AddWithValue(&#8220;<span style="color: #ff0000;">@Pwd</span>&#8220;, txtPassword.Text)</p>
<p>cmd.ExecuteScalar()</p></blockquote>
<p>When the command was executed. All parameters will be transformed into the value that suitable for the data type of those database. The good things you get here is that. For string (varchar) data type, generally it should open and close with single quote &#8221; &#8216; &#8220;. (You can see this code of the first code block) But not for parameterized query, as it will do automatically internal. So, you don&#8217;t have to pay your attention to those data type symbol for each database. (Especially datetime data type) and another point, the generated SQL query will never been attacked by SQL injection anymore as it know now how to handle those type of technique.</p>
<p>This is all about that!</p>
<p>For more information about SQL injection, please <a href="http://en.wikipedia.org/wiki/SQL_Injection" target="_blank" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/SQL_Injection?referer=');">visit here</a>.</p>
<p><!--adsense--></p>
<div align="left" style="float: ; padding: 5px 5px 0px 0px;"><a name="fb_share" type="button" share_url="http://www.iamseree.com/application-development/prevent-your-net-application-from-sql-injection"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.iamseree.com/application-development/prevent-your-net-application-from-sql-injection/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

