<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Xylot Design &#187; injection</title>
	<atom:link href="http://xylotdesign.wordpress.com/tag/injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://xylotdesign.wordpress.com</link>
	<description>useful scripts, ideas, and tips!</description>
	<lastBuildDate>Fri, 21 Mar 2008 20:39:45 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='xylotdesign.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/d47e221117c84d128bbe00e39a6140a7?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Xylot Design &#187; injection</title>
		<link>http://xylotdesign.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://xylotdesign.wordpress.com/osd.xml" title="Xylot Design" />
		<item>
		<title>PHP: 10 Security Mistakes &amp; Oversights</title>
		<link>http://xylotdesign.wordpress.com/2008/03/15/php-10-security-mistakes-oversights/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/15/php-10-security-mistakes-oversights/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 00:32:34 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[cross-site]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[mistakes]]></category>
		<category><![CDATA[oversights]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sessions]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=8</guid>
		<description><![CDATA[In my years as a web developer, there are many security issues with which I have had to become familiar. There are a lot of people out their with malicious intent. One of them could come across your site and expose a flaw that allows them access to crucial information, or the entire site itself. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=8&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In my years as a web developer, there are many security issues with which I have had to become familiar. There are a lot of people out their with malicious intent. One of them could come across your site and expose a flaw that allows them access to crucial information, or the entire site itself. Don&#8217;t let this happen! Below are the <b>10</b> most common errors programmers make that could prove fatal.<span id="more-8"></span></p>
<h1>1. SQL Injection</h1>
<p>SQL injection is the act of maliciously adding SQL code that is then processed along with SQL code. This happens when the programmer neglects to properly check user input or query string variables before using it in SQL statements.</p>
<p>For example:</p>
<pre>$id = $_GET['id'];
$result = mysql_query("SELECT * FROM tblComments WHERE fldCommentID = $id");</pre>
<p>You may be asking yourself, &#8220;What&#8217;s wrong with the above code?&#8221; Plenty&#8230; just enough for any hacker to exploit it. What would happen if the visitor entered in the URL <b>page.php?id=0 UNION SELECT * FROM tblAdminUsers</b> ? Because the <b>$id</b> variable is not properly checked for injected code, it is added to the SQL query and processed. Here is what the resulting SQL query would look like:</p>
<pre>SELECT * FROM tblComments WHERE fldCommentID = 0 UNION SELECT * FROM tblAdminUsers</pre>
<p>It is a common enough table naming convention to give it a try, and the results would be damaging, exposing information you never intended to become public. Knowing that $id is supposed to be an integer, one approach to preventing injection is to get the integer value of the variable.</p>
<pre>$id = intval($_GET['id']);</pre>
<p>The above code would then only return a number, and therefore be safe.</p>
<p>There are many other methods of injection, however, so keep your eyes open for any possible vulnerabilities!</p>
<h1>2. Cross-Site Scripting</h1>
<p>Cross-site scripting, or XSS, is the act of inputting HTML or JavaScript code such that it is called and then run. Failing to properly check for HTML tags will allow this to happen. A good example is if you echo a variable taken from the query string. If the user altered the query string such that it included JavaScript code, the possibilities would be endless for hacking and other mischief.</p>
<pre>page.php?something=&lt;script&gt;alert("hello");&lt;/script&gt;</pre>
<p>Visiting the above URL could potentially run the script, as it could be echoed in its entirety. Once that doorway is open, anything is then possible. Suppose the visitor is posting a comment and include JavaScript in his comment. Without proper checking, it would affect anybody viewing the page on which the comment then appears.</p>
<p>One possible solution would be to escape all tag brackets (<b>&lt;</b> and <b>&gt;</b>) with their HTML entity equivalent (<b>&lt;</b> and <b>&gt;</b>) or, if there is no reason for anybody to be using such characters, replace them with nothing at all.</p>
<h1>3. GET Variable Manipulation</h1>
<p>If a site has a form on it that sends to another page via GET method (like PayPal does), it quite possible to go through all the hard work of constructing a URL and altering a variable or two in the process. Consider the following code:</p>
<pre>&lt;p&gt;Checkout below!&lt;/p&gt;
&lt;form action="checkout.php" method="get"&gt;
&lt;input type="hidden" name="grandtotal" value="1234.00" /&gt;
&lt;input type="submit" value="Checkout" /&gt;
&lt;/form&gt;</pre>
<p>If the visitor simply clicked on the <b>Checkout</b> button, she would then be taken to <b>checkout.php?grandtotal=1234.00</b> where, presumable, the site would then deduct $1,234.00 from a stored credit card or account balance of some sort, and so forth. One method of simple hacking would involve examining the form code, and then manually entering in a new URL: <b>checkout.php?grandtotal=5.00</b> where the visitor would then only have to pay $5.00. Such oversights could prove extremely costly.</p>
<p>To fix such a mistake, the grand total could be stored in a database or session variable, or passed via POST instead of GET; however, a better solution would be to double-check the grand total sent to checkout.php, comparing it to what it should be. It is well worth the extra effort and work to prevent losing thousands or even millions of dollar in revenue.</p>
<h1>4. System Calls</h1>
<p>The <b>exec()</b>, <b>passthru()</b> and <b>system()</b> functions all allow you to execte system commands from within your PHP code. Failing to check the commands processed by these functions can potentially result in hackers accessing private information by altering the input. Two function exist to assist in your fight against malicious destruction: <b>escapeshellarg()</b> and <b>escapeshellcmd()</b>.</p>
<p>The <b>escapeshellarg()</b> function removes potentially harmful parts of user input by adding single quotes around the string and escaping any single quotes within. It would be used like so:</p>
<pre>$command_fixed = escapeshellarg($command);</pre>
<p>The <b>escapeshellcmd()</b> function is similar to the former, except that it only escapes characters that have a special meaning to the operating system at hand.</p>
<h1>5. File Uploads</h1>
<p>When uploading content to a site, PHP creates a file but does not automatically check the validity of the file name, the file type, or the size. A user could create his own form specifying some other file to upload instead.</p>
<p>The functions <b>move_uploaded_file()</b> and <b>is_uploaded_file()</b> are useful to assist in solving this problem, but it is best to check the <b>$_FILES</b> global array to ensure the file size and type are allowable for your application.</p>
<h1>6. Includes</h1>
<p>PHP allows you to include external files as if it were within the page itself, and is useful for code that is used extensively, class definitions, and for better organization of your site. However, if the included files are dependent upon user input, this could prove to be a big mistake if not properly checked. If the user visits <b>index.php?p=article</b>, let&#8217;s say our <b>index.php</b> page runs the following:</p>
<pre>include_once($_GET['p'].'php');</pre>
<p>Then a page called article.php would be called up and run. If, however, the user visits <b>index.php?p=http://mysite.com/mycode</b>, then the code would include <b>http://mysite.com/mycode.php</b>, which could potentially contain malicious and destructive code. The code from that page would be run as though it were running on the site&#8217;s own server, which is never a good idea.</p>
<p>Even if you restricted all included files to begin with something like &#8220;include-&#8221; (so that <b>include-articles.php</b> would be called up instead), any hacker could go through the trouble of registering a domain name beginning with &#8220;include-&#8221; and doing the same thing as before.</p>
<p>The only solution is to check the query string variable for illegal characters, such as slashes, colons, and even periods.</p>
<p><u><b>NOTE:</b></u> Make sure all included files end in the proper extension, whether it&#8217;s .php for PHP or .aspx/.asp for ASP. Otherwise, if a visitor somehow finds out what files you are including, they could view a file such as <b>lib.inc</b>, as opposed to <b>lib.inc.php</b>, which would not expose the PHP code within.</p>
<h1>7. Unrestricted Access and Permissions</h1>
<p>If your site has an administration section, it is best to only allow certain IP addresses (ones you know you will be on when logging in). Another good idea an Apache <b>.htaccess</b> file with a username and password. This is a common mistake not native to PHP; it could happen to anybody.</p>
<p>On a similar note, never <i><b>EVER</b></i> back up PHP files by changing or adding an extension. This will most likely result in the PHP code being displayed in its raw format, exposed to the entire world, rather than parsed by the server. Servers usually only recognize certain extensions for certain languages (like .php for PHP, etc.) and any other extension wil display as an ASCII text file. NOT a good idea.</p>
<h1>8. Session IDs</h1>
<p>It is very easy for any hacker to hijack another user&#8217;s session, if they know that user&#8217;s session ID, and potentially see information that they really shouldn&#8217;t. While not completely avoidable, there are steps you can take to increase security.</p>
<p>One solution is to regenerate the session ID upon login using the <b>session_regenerate_id()</b> function. A hacker could try to set her own session ID prior to login; regenerating the session ID would prevent this action from doing anything significant.</p>
<h1>9. Error Reporting</h1>
<p>Before your site goes live, you should get rid of all <b>display_errors()</b> functions, as well as set the <b>display_errors</b> variable in your <b>php.ini</b> file to <b>0</b>. Errors that are displayed could reveal crucial information about your database structure, connection string, and other credentials.</p>
<p>If you still want to be able to view errors, you can set the <b>error_log</b> variable in <b>php.ini</b> to <b>1</b> and check your PHP error log frequently for caught errors. You can also create your own code to handle errors.</p>
<h1>10. Unvalidated Access to Protected Areas</h1>
<p>I&#8217;ve been guilty of this once before. If your site has an administration or members-only area, each and every single page that should be available to members only, should be protected properly.</p>
<p>At the top of every protected page, make sure that their login credentials are validated, and that the session or cookie variable you use for logging in users is still flagged and active. Otherwise, users would be able to view certain information without ever logging in. Nobody should be able to view any protected pages by bypassing the login screen.</p>
<h1>Conclusion</h1>
<p>There are so many ways that people can hack into your code and your site. The above covers just a handful of mistakes you could make in your code that would expose vulnerabilities and crucial information. Be safe, be informed, and be cautious!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=8&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/15/php-10-security-mistakes-oversights/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>
	</item>
	</channel>
</rss>