<?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>Roshan Bhattarai's Blog -  A blog about PHP, Ajax, JavaScript, CSS and Web 2.0 &#187; php attack</title>
	<atom:link href="http://roshanbh.com.np/category/php-attack/feed" rel="self" type="application/rss+xml" />
	<link>http://roshanbh.com.np</link>
	<description>Useful Tutorials, Scripts , Tips, and Resources for all PHP and Ajax beginners and experts .</description>
	<lastBuildDate>Thu, 10 Jun 2010 11:38:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Prevent form post request from another domain in PHP</title>
		<link>http://roshanbh.com.np/2008/06/prevent-form-post-request-another-domain.html</link>
		<comments>http://roshanbh.com.np/2008/06/prevent-form-post-request-another-domain.html#comments</comments>
		<pubDate>Sun, 22 Jun 2008 09:53:57 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[php attack]]></category>
		<category><![CDATA[tips and technique]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=134</guid>
		<description><![CDATA[HTTP POST request from outside domain is one of the way of attacking your website. A intruder can use JavaScript in other domain or localhost to send the repetitive POST request to your web page  containing PHP script. We must prevent this kind of cross domain form posting which might be harmful of our website. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F06%2Fprevent-form-post-request-another-domain.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F06%2Fprevent-form-post-request-another-domain.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify;">HTTP POST request from outside domain is one of the way of attacking your website. A intruder can use JavaScript in other domain or localhost to send the repetitive POST request to your web page  containing PHP script. We must prevent this kind of cross domain form posting which might be harmful of our website.</p>
<p><span id="more-134"></span></p>
<p>&nbsp;</p>
<h4>Example of form post a spam</h4>
<p>Let&#8217;s suppose that, we have a contact form in our website and we&#8217;re posting the detail of the form to &#8220;contact.php&#8221; file. A intruder can use JavaScript in another domain and can send the repetitive post request by placing &#8220;http://our-site/contact.php&#8221; in the action field of their code and spam our website.</p>
<h4>How to check the form being posted from another domain</h4>
<p style="text-align: justify;">Last time, I&#8217;ve posted the article about <a href="http://roshanbh.com.np/2008/05/useful-server-variables-php.html" target="_blank">useful server variables in PHP</a>. Among them, we can use HTTP_REFERRER server variables to prevent the cross domain form post request. You can look at the  example code in PHP below to check the POST request is from the same domain or different domain.</p>
<pre class="prettyprint" style="overflow:auto">//if example.com is there in HTTP_REFERRER variable
if(strpos($_SERVER['HTTP_REFERER'],'example.com'))
{
  //only process operation here
}</pre>
<p align="justify">HTTP_REFERRER variable is used here to check where the post request came from. Then, along with strpos() function of PHP, we can check weather the  HTTP_REFERRER variable contains our domain as a referrer website or not. If the post request is from our domain then only we can execute the remaining code of our page.</p>
<h4>A better approach</h4>
<p style="text-align: justify;"><span style="color: #ff0000;"><strong></strong></span>The HTTP_REFERRER headers can be disabled or faked and we can&#8217;t rely 100% on it.But, we can also use cookie to check for the cross-site post request forgery. And, you know that cookies are also unreliable anyway.</p>
<p style="text-align: justify;">One of the better approach will be to use use a hidden field in the form which contains the md5() value of a salt (a secret value stored in the database) with another dynamic value like session id or IP address of the user and verifying it with PHP when the post request of that form comes in PHP.</p>
<p align="justify">
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=134&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/06/prevent-form-post-request-another-domain.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>SQL Injection Attack &#8211; Examples and Preventions in PHP</title>
		<link>http://roshanbh.com.np/2007/12/sql-injection-attack-examples-and-preventions-in-php.html</link>
		<comments>http://roshanbh.com.np/2007/12/sql-injection-attack-examples-and-preventions-in-php.html#comments</comments>
		<pubDate>Tue, 18 Dec 2007 18:41:00 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[php attack]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/sql-injection-attack-examples-and-preventions-in-php/</guid>
		<description><![CDATA[What is SQL injection? It is a basically a trick to inject SQL command or query as a input mainly in the form of the POST or GET method in the web pages. Most of the websites takes parameter from the form and make SQL query to the database. For a example, in a product [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2007%2F12%2Fsql-injection-attack-examples-and-preventions-in-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2007%2F12%2Fsql-injection-attack-examples-and-preventions-in-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h4>What is SQL injection?</h4>
<p style="text-align: justify">It is a basically a trick to inject SQL command or query as a input mainly in the form of the POST or GET method in the web pages. Most of the websites takes parameter from the form and make SQL query to the database. For a example, in a product detail page of php, it basically takes a parameter product_id from a GET method and get the detail from database using SQL query. With SQL injection attack, a intruder can send a crafted SQL query from the URL of the product detail page and that could possibly do lots of damage to the database. And even in worse scenario, it could even drop the database table as well.</p>
<p><span id="more-17"></span></p>
<h4>Examples of SQL Injection Attack in PHP:</h4>
<p>Let&#8217;s look at the usual query for user login in PHP,</p>
<blockquote><p><span style="font-size: 85%; color: #660000;">$sql=&#8221;SELECT * FROM tbl_user WHERE username= &#8216;&#8221;.$_POST['username'].&#8221;&#8216;  AND  password= &#8216;&#8221;.$_POST['password'].&#8221;&#8216;&#8221;;<br />
</span><span style="font-size: 85%; color: #660000;">$result=mysql_query($sql);</span></p></blockquote>
<p style="text-align: justify">Well, lots of people thinks that only the valid user can log in inside the system but that&#8217;s not  true.Well anybody can log in to that website with a simple trick.</p>
<p style="text-align: justify">Let&#8217;s suppose that a intruder called SAM injected <span style="color: #660000;">x&#8217; OR &#8216;x&#8217;='x</span> in the username field and <span style="color: #660000;">x&#8217; OR &#8216;x&#8217;='x</span> in the password field. Then the final query will become like this</p>
<blockquote><p><span style="color: #660000;">SELECT * FROM tbl_user WHERE username=&#8217;x&#8217; OR &#8216;x&#8217;='x&#8217; AND password=&#8217;x&#8217; OR &#8216;x&#8217;='x&#8217;;</span></p></blockquote>
<p style="text-align: justify">Well you can see that query is always true and returns the row from the database. As the result , the malicious guy could log in to the system.</p>
<p style="text-align: justify">Now even let&#8217;s look at the worst scenario of the SQL injection attack example. A intruder can even drop a table if the database user has drop privilege into that database.</p>
<p>Let&#8217;s suppose a query in a  product detail page</p>
<blockquote><p><span style="color: #660000;">$sql=&#8221;SELECT * FROM product WHERE product_id= &#8216;&#8221;.$_GET['product_id'].&#8221;&#8216;&#8221;;</span></p></blockquote>
<p>Now its turn of intruder to inject SQL command in the URL of the page, the code might be like this <span style="color: #660000;">10&#8242;; DROP TABLE product; #</span> and the URL looks like this</p>
<blockquote><p><span style="color: #660000;">http://xyz.com/product.php?id=10&#8242;; DROP TABLE product; #</span></p></blockquote>
<p>Now query becomes like this</p>
<blockquote><p><span style="color: #660000;">SELECT * FROM product WHERE product_id=&#8217;10&#8242;; DROP TABLE product; #&#8217;;</span></p></blockquote>
<p>You might be wondering what is the meaning of hash &#8220;#&#8221;, it tell MYSQL server to ignore the rest of the query.In this query, it simply ignore the last single quote (&#8216;) of the query.</p>
<h4>Prevention from Sql Injection Attack in PHP</h4>
<p>To avoid the sql injection attack, please follow the following simple mechanisms in PHP</p>
<p><span style="font-weight: bold">1) </span>Always restrict the length of the fields of form such as don&#8217;t allow more than 20 characters in the fields like username and password with the &#8220;maxlength&#8221; property available in the html form.</p>
<p><span style="font-weight: bold">2)</span> Always validate for the proper input like weather the value is valid email or not,  is numeric or not , valid date or not etc.</p>
<p><span style="font-weight: bold">3)</span> Finally, Always use <span style="color: #660000;">mysql_real_escape_string()</span> function before sending the variable to the SQL query, it ad.  For example</p>
<blockquote><p><span style="color: #660000;">//note you must be connected to the database for using this function</span><br />
<span style="color: #660000;">$username=mysql_real_escape_string($_POST['username']);</span><br />
<span style="color: #660000;">$password=mysql_real_escape_string($_POST['password']);</span></p></blockquote>
<p>if a intruder inject <span style="color: #660000;">&#8216; OR 1</span> in the user name and password field then the value of the $username and $password will become <span style="color: #660000;">\&#8217; OR 1</span> which is not going to harm us anymore.</p>
<h4>Recommended Reading on SQL Injection Attack </h4>
<p><a href="http://unixwiz.net/techtips/sql-injection.html">http://unixwiz.net/techtips/sql-injection.html<br />
</a><a href="http://www.securiteam.com/securityreviews/5DP0N1P76E.html">http://www.securiteam.com/securityreviews/5DP0N1P76E.html</a></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=17&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2007/12/sql-injection-attack-examples-and-preventions-in-php.html/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Cross-site scripting ( xss ) attack by example and prevention in PHP</title>
		<link>http://roshanbh.com.np/2007/12/cross-site-scripting-xss-attack-by-example-and-prevention-in-php.html</link>
		<comments>http://roshanbh.com.np/2007/12/cross-site-scripting-xss-attack-by-example-and-prevention-in-php.html#comments</comments>
		<pubDate>Fri, 07 Dec 2007 17:58:00 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[php attack]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[xss attack]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/cross-site-scripting-xss-attack-by-example-and-prevention-in-php/</guid>
		<description><![CDATA[What are cross-site scripting (XSS ) Attacks? Cross-site scripting attacks are attacks that target the end user instead of your actual site. Vulnerable web applications that don&#8217;t check or validate properly incoming data let arbitrary code to run on a client computer (such as Javascript). The end result can be anything from stealing cookie data [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2007%2F12%2Fcross-site-scripting-xss-attack-by-example-and-prevention-in-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2007%2F12%2Fcross-site-scripting-xss-attack-by-example-and-prevention-in-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h4>What are cross-site scripting (XSS ) Attacks?</h4>
<p style="text-align: justify"><span style="font-size: 100%"><span style="font-family: verdana;">Cross-site scripting attacks are attacks that target the end user instead of your actual site. Vulnerable web applications that don&#8217;t check or validate properly incoming data let arbitrary code to run on a client computer (such as Javascript). The end result can be anything from stealing cookie data or redirecting to a different site, to embedding a browser exploit on a page. Anything that can be done with Javascript (a lot!).</span></span></p>
<p><span id="more-11"></span></p>
<h4>Example of cross-site scripting (xss) attack</h4>
<p><span style="font-size: 100%"><span style="font-family: verdana;">Let us suppose that there is a comment form in the Michael&#8217;s website of a section like photo gallary or article. He created a feature that let his viewers to comment on his photos or article by submitting a form. And he doesnot have much validation in this comment form.</span></span></p>
<p><span style="font-family: verdana;">Now Sam (inturder) visits the </span><span style="font-size: 100%"><span style="font-family: verdana;">Michael</span></span><span style="font-size: 100%"><span style="font-family: verdana;">&#8216;s website and he&#8217;s jealous of </span></span><span style="font-size: 100%"><span style="font-family: verdana;">Michael</span></span><span style="font-size: 100%"><span style="font-family: verdana;">&#8216;s website traffic and wants to steal some of his website&#8217;s traffic. Then he can insert the follow code to his comment form </span></span></p>
<p><span style="font-family: verdana; color: #ff6666;"><span style="font-size: 85%">Hi Michael, very gud job, keep it up! &lt;img src=&#8221;http://google.com/images/logo.gif&#8221; onload=&#8221;window.location=&#8217;http://sam.com/&#8217;&#8221; /&gt;</span> </span></p>
<p><span style="font-family: verdana;">And every time a user visits Michael&#8217;s article or photos, they are rudely redirected to sam&#8217;s site.</span><br />
<!--adsense--></p>
<h4>Prevention from xss attack In php </h4>
<p><span style="font-size: 100%"><span style="font-family: verdana;">To prevent from XSS attacks, you just have to check and validate properly all user inputted data that you plan on using and dont allow html or javascript code to be inserted from that form.</span></span></p>
<p><span style="font-family: verdana;">Or you can you Use htmlspecialchars() to convert HTML characters into HTML entities. So characters like &lt;&gt; that mark the beginning/end of a tag are turned into html entities and you can use strip_tags() to only allow some tags as the function does not strip out harmful attributes like the onclick or onload.</span></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=11&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2007/12/cross-site-scripting-xss-attack-by-example-and-prevention-in-php.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/21 queries in 3.112 seconds using disk: basic
Object Caching 572/604 objects using disk: basic

Served from: roshanbh.com.np @ 2012-02-08 20:59:26 -->
