<?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>Checkmate &#187; Sql Injection</title>
	<atom:link href="http://niiconsulting.com/checkmate/category/sql-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://niiconsulting.com/checkmate</link>
	<description>An Information Security Blog by NII Consulting</description>
	<lastBuildDate>Fri, 02 Dec 2011 08:26:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>SQL Injection in Stored Procedure &amp; Preventing from the same</title>
		<link>http://niiconsulting.com/checkmate/2009/09/30/sql-injection-stored-procedure-prevention/</link>
		<comments>http://niiconsulting.com/checkmate/2009/09/30/sql-injection-stored-procedure-prevention/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 10:03:51 +0000</pubDate>
		<dc:creator>Dhiraj Ranka</dc:creator>
				<category><![CDATA[Secure Coding]]></category>
		<category><![CDATA[Sql Injection]]></category>
		<category><![CDATA[stored procedures]]></category>

		<guid isPermaLink="false">http://niiconsulting.com/checkmate/?p=58</guid>
		<description><![CDATA[Following is the small example of creating a stored procedure. ==================================================================== CREATE PROC sp_login (@loginid nvarchar(25),@password nvarchar(25)) AS DECLARE @SQLString VARCHAR(500) DECLARE @loginid VARCHAR(64) DECLARE @password VARCHAR(64) /* Build the SQL string once.*/ SET @SQLString = ‘SELECT * from cust_users WHERE login_id = ‘+ ””+@loginid+”” + ‘AND password = ‘+ ””+@password+”” EXECUTE sp_executesql @SQLString ==================================================================== [...]]]></description>
			<content:encoded><![CDATA[<p>Following is the small example of creating a stored procedure.</p>
<p>====================================================================</p>
<p>CREATE PROC sp_login (@loginid nvarchar(25),@password nvarchar(25))<br />
AS<br />
DECLARE @SQLString VARCHAR(500)<br />
DECLARE @loginid VARCHAR(64)<br />
DECLARE @password VARCHAR(64)</p>
<p>/* Build the SQL string once.*/</p>
<p>SET @SQLString = ‘SELECT * from cust_users WHERE login_id = ‘+ ””+@loginid+”” + ‘AND password = ‘+ ””+@password+””</p>
<p>EXECUTE sp_executesql @SQLString<br />
<span id="more-58"></span><br />
====================================================================</p>
<p>Your ASP.NET Code would look like this:</p>
<p>oCmd.CommandText = “sp_login”;<br />
oCmd.CommandType = CommandType.StoredProcedure;<br />
oCmd.Parameters.Add( “@loginId”, strUserName);<br />
oCmd.Paramerters.Add( “@password”, strPassword);<br />
oCon.Open();<br />
string result = (string)oCmd.ExecuteScalar();<br />
oCon.Close();</p>
<p>====================================================================</p>
<p>If the user input is as follows:<br />
loginId = ‘ OR 1=1 –<br />
password = junk</p>
<p>SQL injection will not work and ASP.NET will throw an exception</p>
<p>“Unclosed quotation mark after the character string ‘ OR 1=1 — and password=junk’.<br />
Incorrect syntax near ‘ OR 1=1 — and password=junk’.”</p>
<p>In this case you can use</p>
<p><strong>loginID = ” OR 1=1–</strong></p>
<p>password = junk</p>
<p>Two single quotations are used to complete where clause with null condition and OR is used to make the condition true always.</p>
<p>If you use <strong><em>sp_executesql</em></strong> this will definitely leads to the SQL Injection.</p>
<p>See more on this <a href="http://msdn.microsoft.com/en-us/library/ms188001.aspx">http://msdn.microsoft.com/en-us/library/ms188001.aspx</a></p>
<p><strong>Solution :</strong></p>
<p>Instead one should use the same stored procedure which he has created, for passing parameters.</p>
<p><strong>exec sp_login ‘param1′, ‘param2′</strong></p>
<p>param1 – would be loginID</p>
<p>param2 – would be password</p>
<p>And you are stored procedure would look like this i.e. with out <strong><em>sp_executesql</em></strong></p>
<p>====================================================================</p>
<p>CREATE PROC sp_login</p>
<p>@loginid VARCHAR(64)<br />
@password VARCHAR(64)</p>
<p>AS</p>
<p>BEGIN<br />
SELECT * FROM cust_users WHERE loginid=@loginid AND password=@password<br />
END</p>
<p>====================================================================</p>
<p><strong>This will avoid the possible SQL Injection</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://niiconsulting.com/checkmate/2009/09/30/sql-injection-stored-procedure-prevention/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

