<?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>Dimoff&#039;s Tutorials and Articles &#187; Web Development</title>
	<atom:link href="https://www.dimoff.biz/blog/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dimoff.biz/blog</link>
	<description>Tutorials and Articles related to Information Security, HTML, CSS, PHP and JavaScript</description>
	<lastBuildDate>Sat, 28 Nov 2015 19:19:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>Web Development Soup eBook</title>
		<link>https://www.dimoff.biz/blog/web-development-soup/</link>
		<comments>https://www.dimoff.biz/blog/web-development-soup/#comments</comments>
		<pubDate>Sat, 28 Nov 2015 19:18:44 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dimoff.biz/blog/?p=81</guid>
		<description><![CDATA[Visit the book&#8217;s website In Web Development Soup, you will learn important HTML, CSS, JavaScript, and PHP concepts. You will get up-to-date with the recent developments in HTML5 and CSS3&#8230;]]></description>
				<content:encoded><![CDATA[<p><a href="http://wds.dimoff.biz" target="_blank"> Visit the book&#8217;s website</a></p>
<p>In Web Development Soup, you will learn important HTML, CSS, JavaScript, and PHP concepts. You will get up-to-date with the recent developments in HTML5 and CSS3 – the tags, selectors, attributes and APIs they offer. You will get acquainted with Foundation and Twitter Bootstrap which are invaluable when it comes to designing user interfaces. In terms of JavaScript, you will build basic knowledge of recent developments such as Angular.js, jQuery, Node.js, new JavaScript APIs and features. In terms of PHP, you will get a head start on WordPress, Laravel, CakePHP and so on. </p>
<p>Throughout the book, besides explaining different possibilities in the programming languages and giving sample codes we will also be building real and functioning applications such as a voice chat with Node.js, a small marketplace with CakePHP, a simple game with JavaScript and Processing.js, two apps where users can take and manage notes, a screenshot-capturing service in which users can enter a URL address and get a screenshot of the website as our server sees it, and more. </p>
<p>Furthermore, some widgets will be built such as a countdown timer which can be reused in your applications, a PHP script which takes an URL address to a WordPress RSS feed and extracts all relevant information for you to use or displays its contents in a table, amongst others. </p>
<p>Check out the book’s accompanying website: http://wds.dimoff.biz.<br />
In it, you can download all code samples from this book and examine them or run them in a browser. Furthermore, you can do the exercises on the website each time you read a chapter and if you manage to complete all exercises and the final exam without mistakes (you can repeat the exercises as much as you like) you can get a special certificate! </p>
<p>In the bottom of the book, you will find your username and password. Use them to access the code samples and the tests. </p>
<p>Some other useful aspects of web development are also examined in the Web Development Soup &#8211; such as improving your workflow, securing web applications and optimizing your websites in search engines. </p>
<p>Тhe content of the book will serve you well in establishing a base knowledge of the different aspects of web development and guide you to find out the sphere that you most like and will empower your career. </p>
<p>This book will be periodically updated with new content, as well as the accompanying website. New things will be added at least once every few months.</p>
<p>Web Development Soup is sold in Amazon for $3.49</p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-development-soup%2F&#038;t=Web%20Development%20Soup%20eBook&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-development-soup%2F&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwp-content%2Fuploads%2F2015%2F11%2Fwds-logo.png&#038;p&#091;title&#093;=Web%20Development%20Soup%20eBook" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-development-soup%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-development-soup%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-development-soup%2F&#038;title=Web%20Development%20Soup%20eBook" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-development-soup%2F&#038;media=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwp-content%2Fuploads%2F2015%2F11%2Fwds-logo.png&#038;description=Web%20Development%20Soup%20eBook" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-development-soup%2F&#038;title=Web%20Development%20Soup%20eBook" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Web%20Development%20Soup%20eBook&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-development-soup%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/web-development-soup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web Vulnerabilities Explained eBook</title>
		<link>https://www.dimoff.biz/blog/web-vulnerabilities-explained-ebook/</link>
		<comments>https://www.dimoff.biz/blog/web-vulnerabilities-explained-ebook/#comments</comments>
		<pubDate>Tue, 03 Nov 2015 13:11:09 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Information Security]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dimoff.biz/blog/?p=76</guid>
		<description><![CDATA[107 Page eBook This book will be useful to anyone engaged in, or studying, web development or/and penetration testing as well as those interested in information security and in web security, in&#8230;]]></description>
				<content:encoded><![CDATA[<p><strong>107 Page eBook</strong></p>
<p>This book will be useful to anyone engaged in, or studying, web development or/and penetration testing as well as those interested in information security and in web security, in particular.</p>
<p>A wide array of vulnerabilities are discussed including code injections, XSS, Clickjacking, CSRF, DoS, Content Spoofing, Information Leakage along with many other flaws related to various parts of web applications – such as authentication flaws. Each chapter discusses a particular vulnerability and most chapters have code samples which show the type of code that’s susceptible to an attack, as well as sections depicting the ways in which the vulnerability may be prevented or mitigated.</p>
<p>Three interactive quizzes are also included with the book which can help you in the learning process and let you know if you have retained the material.</p>
<p>The book is best read by people who have at least some basic knowledge of one or more of the following languages: HTML, JavaScript, PHP, Node.js</p>
<p><a href="http://resources.infosecinstitute.com/download/web-vulnerabilities-explained-ebook/" target=_blank>Download the Web Vulnerabilities Explained eBook for <strong>FREE</strong></a></p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-vulnerabilities-explained-ebook%2F&#038;t=Web%20Vulnerabilities%20Explained%20eBook&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-vulnerabilities-explained-ebook%2F&#038;p&#091;images&#093;&#091;0&#093;=&#038;p&#091;title&#093;=Web%20Vulnerabilities%20Explained%20eBook" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-vulnerabilities-explained-ebook%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-vulnerabilities-explained-ebook%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-vulnerabilities-explained-ebook%2F&#038;title=Web%20Vulnerabilities%20Explained%20eBook" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-vulnerabilities-explained-ebook%2F&#038;media=&#038;description=Web%20Vulnerabilities%20Explained%20eBook" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-vulnerabilities-explained-ebook%2F&#038;title=Web%20Vulnerabilities%20Explained%20eBook" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Web%20Vulnerabilities%20Explained%20eBook&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fweb-vulnerabilities-explained-ebook%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/web-vulnerabilities-explained-ebook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Website Hacking, Part VII: Common Web Vulnerabilities and Brief Solutions</title>
		<link>https://www.dimoff.biz/blog/website-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions/</link>
		<comments>https://www.dimoff.biz/blog/website-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions/#comments</comments>
		<pubDate>Mon, 23 Feb 2015 19:23:48 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Information Security]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dimoff.biz/blog/?p=68</guid>
		<description><![CDATA[Introduction In this last part of the Website Hacking series, we are going to list 18 common web vulnerabilities and flaws and we are going to briefly provide solutions to them.&#8230;]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>In this last part of the Website Hacking series, we are going to list 18 common web vulnerabilities and flaws and we are going to briefly provide solutions to them. Some of them are described for the first time in the Website Hacking series and some we have discussed before but in greater depth.</p>
<p><strong>1. Saving all user input</strong></p>
<p>If you are using a framework, for example, a PHP framework, you might be tempted to save all user input to your model or database since it has already been validated and escaped. Let us say that you are using CakePHP and have included a registration form using CakePHP’s Form helper.</p>
<p>The registration view may look something like this:</p>
<p>&nbsp;</p>
<p>&lt;?php echo $this-&gt;Form-&gt;create(&#8220;User&#8221;);</p>
<p>echo $this-&gt;Form-&gt;input(&#8220;username&#8221;,</p>
<p>array(&#8220;label&#8221; =&gt; &#8216;Username:&#8217;)</p>
<p>);</p>
<p>echo $this-&gt;Form-&gt;input(&#8220;password&#8221;,</p>
<p>array(&#8220;label&#8221; =&gt; &#8216;Password:&#8217;)</p>
<p>);</p>
<p>echo $this-&gt;Form-&gt;end(      array(&#8216;class&#8217; =&gt; &#8216;button  medium radius success &#8216;,        &#8216;label&#8217; =&gt; &#8220;Register&#8221;        )      );</p>
<p>Now, you might be tempted to save all data from CakePHP’s $this-&gt;request-&gt;data array/method as is if you do not read the docs carefully or view some of the examples provided there (the live blog site).</p>
<p>public function register() {</p>
<p>if ($this-&gt;request-&gt;is(&#8220;post&#8221;)) {<br />
$this-&gt;User-&gt;create();</p>
<p>if ($this-&gt;User-&gt;save($this-&gt;request-&gt;data)) {</p>
<p>$this-&gt;Session-&gt;setFlash(&#8220;Account created. You may log in!&#8221;);<br />
$this-&gt;redirect(array(<br />
&#8216;controller&#8217; =&gt; &#8216;offers&#8217;,<br />
&#8216;action&#8217; =&gt; &#8216;index&#8217;<br />
));<br />
}<br />
}}</p>
<p>You just save all data and thank the framework creators. However, there are at least two things you did wrong:</p>
<p>$this-&gt;request-&gt;data does not contain escaped/sanitized input, just the input from the superglobals.</p>
<p>Firstly, you should use CakePHP’s h() function to prevent people inserting tags in their username: like this h($this-&gt;request-&gt;data)</p>
<p>However, this is not enough and a wrong approach. If you save all user input in your Model (database) the user can add new input tags directly in his browser and try to guess some columns in your users table for which you have not provided an input in the website’s form.</p>
<p>For example, many CakePHP’s applications have “role” column set to user/admin or something similar (it is used in the docs as well).</p>
<p>The user can just open his Developer Tools, find the registration form or right click and select Inspect Element, click on Edit as HTML and add a new input like this:</p>
<p>&lt;input name=”data[User][role]” type=”text”&gt;</p>
<p>&lt;input name=” [User][role]” type=”text”&gt;</p>
<p>Or whatever the current way for forms to interact with your Models is, guess column names and insert values to them.</p>
<p>One way to solve this is to change your column that defines user’s roles and permissions name to something unpredictable. However, that is not the safest approach you can take.</p>
<p>You can either insert the data into the database manually, which will ensure no extra columns will be saved:</p>
<p>$AddUser = array(&#8216;User&#8217; =&gt; array(</p>
<p>&#8216;username&#8217; =&gt; h($user[&#8216;User&#8217;][&#8216;username&#8217;]),</p>
<p>&#8216;password&#8217; =&gt; $user[&#8216;User&#8217;][&#8216;password&#8217;]</p>
<p>));</p>
<p>if ($this-&gt;User-&gt;save($AddUser)) {</p>
<p>Or alternatively, you could still save all user data but set explicitly the values of columns not found in the form:</p>
<p><strong>public</strong> <strong>function</strong> register() {</p>
<p><strong>  if</strong> ($this-&gt;request-&gt;is(&#8220;post&#8221;)) {</p>
<p>$this-&gt;request-&gt;data[&#8216;User&#8217;][&#8216;role&#8217;] = &#8220;user&#8221;;</p>
<p>$this-&gt;User-&gt;create();</p>
<p><strong>if</strong> ($this-&gt;User-&gt;save(h($this-&gt;request-&gt;data))) {</p>
<p>$this-&gt;Session-&gt;setFlash(&#8220;Account created. You may log in!&#8221;);</p>
<p>$this-&gt;redirect(<strong>array</strong>(  &#8216;controller&#8217; =&gt; &#8216;offers&#8217;,  &#8216;action&#8217; =&gt; &#8216;index&#8217;   ));</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p><strong>2. Allowing user access to assets</strong></p>
<p>Many sites work with user input and user data and store this data. Clients can see where their assets are stored, so there is no need for them to guess. For example, a client could see that the images he uploaded were stored in /uploads/{username} because the images he uploaded were loaded to the page from that directory, so if he knows some usernames of different people he could just change the directory name to another user and browse through all of his data without having to brute-force directory names.</p>
<p>The first way to tackle this issue that we discussed before is not enough (adding Options All –Indexes to the .htaccess file).It would prevent users from browsing directories and opening whatever they want but they would still know the directory exists and they can still guess directory names because the server will return a 403 Forbidden (which shows something exists in that path). Furthermore, they could guess file names from some patterns that the file names follow and open them.</p>
<p>Therefore, you need to block access to the files in your uploads directory. If you are storing text files (let us say users can keep notes and view/edit them whenever they want) you could add to your .htaccess the following rule:</p>
<p>RewriteEngine On</p>
<p>RewriteRule ^uploads/.*\.(txt|doc)$ – [F,L,NC]</p>
<p>The F flag would return a 403 Forbidden response, the L flag causes the next rules to stop being processed, and the NC eliminates case-sensitivity.</p>
<p><img src="http://2we26u4fam7n16rz3a44uhbe1bq2.wpengine.netdna-cdn.com/wp-content/uploads/020915_1214_WebsiteHack1.png" alt="" /></p>
<p><strong>Figure 1: The page with only directory listing disabled.</strong></p>
<p><img src="http://2we26u4fam7n16rz3a44uhbe1bq2.wpengine.netdna-cdn.com/wp-content/uploads/020915_1214_WebsiteHack2.png" alt="" /></p>
<p><strong>Figure 2: The page with only directory listing disabled. You cannot browse directories, but if each user has a notes.txt file, you can easily view user’s notes by knowing only their username.</strong></p>
<p><img src="http://2we26u4fam7n16rz3a44uhbe1bq2.wpengine.netdna-cdn.com/wp-content/uploads/020915_1214_WebsiteHack3.png" alt="" /></p>
<p><strong>Figure 3: Trying to access the notes with both directory listing and controlled access to files.</strong></p>
<p>If you use the rewrite rule to disable users from browsing other users notes, your back-end would still be able to access the notes, show them to users or edit them. For example:</p>
<p>&lt;?php</p>
<p>$user = &#8220;Cartman&#8221;;</p>
<p>$userNotes = file_get_contents(dirname(<strong>__FILE__</strong>). &#8220;/yourSite/uploads/<strong>{</strong>$user<strong>}</strong>/notes.txt&#8221;);</p>
<p><strong>echo</strong> &#8220;&lt;p&gt; <strong>$userNotes</strong> &lt;/p&gt;&#8221;;</p>
<p>?&gt;</p>
<p>Where the $user variable would come from a session in a real-world application.</p>
<p><strong>3. Running basic WordPress installation</strong></p>
<p>Common mistakes here are not limiting the login attempts on your wp-admin page. This would allow anyone to brute-force your credentials and destroy your blog/site. This becomes even easier because most people create their master username to be ‘admin’ which involves only brute-forcing the password to get full access to the WP website. Another mistake is that the wp-admin login page is left without a form of CAPTCHA or a protection against bots. This combined with no limitation of login attempts equals certain death of your online presence at some point in the future. You could avoid all 3 of these things and also change the default wp-admin path to be something different as well (obfuscation).</p>
<p><strong>4. Relying too much on IP addresses while having weak bot protection</strong></p>
<p>Most ISPs provide dynamic IP addresses, and the IP address you have banned or stored may already be obsolete in less than a day. Furthermore, it is often not very difficult to change your IP address – use a proxy, release it from the router or from the OS, change locations. There are myriad ways to do it. To prevent bots from causing undesired consequences, it would be better to use alternative ways – enhance your CAPTCHAs, add inputs only bots will fill out, require JavaScript/cookies enabled to submit a form, and so on.</p>
<p><strong>5. Improper redirects</strong></p>
<p>Let us say that you have a redirect page or a GET value (for simplicity’s sake) that redirects users to another page of your site or to another website. However, if you forget to disallow redirects to third-party websites or in case you allow those, if you do not create a warning page before redirecting that will tell the user where they are going and that they are leaving the site – users can easily abuse your site by giving links that seem to be pointing to your site but will redirect users to malicious websites.</p>
<pre>if (isset($_GET['redirect'])) {
		header("Location: " .  $_GET['redirect']);
	}
</pre>
<p>If we have something as simple as this, then users can easily get fooled to enter bad sites by following an URL like this:</p>
<p>http://localhost:8079/<a href="http://resources.infosecinstitute.com/articles/">articles</a>/Website%20Hacking%20Part%20VII/?redirect=http://www.somemalicioussitehere.com</p>
<p><strong>6. Cross Site Request Forgery</strong></p>
<p>If your site allows users to add comments/posts and insert tags such as &lt;img&gt; and load a third-party image, they can provide a link that is not an image but will fool the clients’ browsers (the users that will be reading them) to load the resource and perform an action on a website if they are authenticated in it. For example, if Facebook was sufficed with a couple of GET parameters or a particular URL to follow someone/something on their network, we could have added an image like that:</p>
<pre><img src="http://www.facebook.com/ajax/follow/follow_profile.php?profile_id=11111111111&amp;location=1" alt="my image" />
</pre>
<p>And if the user is currently logged in he would have followed an arbitrary person. Of course, this would not work in this particular situation.</p>
<p><strong>7. Insecure file handling</strong></p>
<p>A common mistake is to trust that a file does not contain something inappropriate. Code can be disguised as an image, so checking the file extension is not enough. At the very least, the MIME type should also be checked. Also, ASCII / text files should be escaped.</p>
<p>Here is an example of such a vulnerability:</p>
<p>&lt;form action=&#8221;&#8221; method=&#8221;POST&#8221; enctype=&#8221;multipart/form-data&#8221;&gt;</p>
<p>&lt;input type=&#8221;file&#8221; name=&#8221;myTxt&#8221;&gt; &lt;br&gt;&lt;br&gt;</p>
<p>&lt;input type=&#8221;submit&#8221; value=&#8221;Upload&#8221;&gt;</p>
<p>&lt;/form&gt;</p>
<p>&lt;script&gt;</p>
<p>$(&#8220;form&#8221;).on(&#8216;submit&#8217;, function() {</p>
<p>if ($(&#8220;form input[type=&#8217;file&#8217;]&#8221;).val().split(&#8220;.&#8221;).pop() === &#8220;txt&#8221;)</p>
<p>{               return true        }        else {               return false;        }</p>
<p>})</p>
<p>&lt;/script&gt;</p>
<p>The vulnerability arises when at some point we display the contents of the .txt file in our page:</p>
<p><strong>if</strong> (isset($_FILES[&#8216;myTxt&#8217;])) {</p>
<p><strong>echo</strong> readfile($_FILES[&#8216;myTxt&#8217;][&#8216;tmp_name&#8217;]);</p>
<p>}</p>
<p>If the file we submit contains the following code:</p>
<p>&lt;script&gt;</p>
<p>alert(document.cookie);</p>
<p>&lt;/script&gt;</p>
<p>Then all user cookies for that website will be shown in an alert.</p>
<p><strong>8. Displaying and trusting HTTP headers</strong></p>
<p>These can be modified by users and can be malicious. For example, if you display the client’s User-Agent header, it might be changed to consist of code which would then be executed in your back-end.</p>
<p>This is also valid for the referrer header, so it should not be used to determine whether the user can access a particular page by itself (for example, checking if the referfer is the login page and assuming the user has logged in successfully since he was redirected to the members area’s index page from the login page).</p>
<p><strong>9. Information disclosure</strong></p>
<p>Your live apps should not be in debug mode. Errors should not be shown.</p>
<p><strong>10. Directory traversal</strong></p>
<p>If you are using some parameter that opens different files on your website based on user input, your back-end should escape special characters such as the . (dot) or / (slash) from the input and preferably use whitelisting.</p>
<p><strong>11. Using HTTP for semi-confidential data</strong></p>
<p>A common flaw is using HTTP for sites that include mechanisms such as registration/login. Even widely used online marketplaces in Bulgaria use simple HTTP (such as http://www.olx.bg ). Using HTTP makes it easy for potential attackers in your network to sniff your traffic and get your credentials with no real efforts. For example, if you login to olx while in a Wi-Fi, you are subject to risk.</p>
<p><strong>13. Sessions can be stolen</strong></p>
<p>Sessions can be stolen, making the attacker login as someone else. There are multiple vectors of defense here – such as checking the IP address, the user agent, and regenerating session, and adding cookies.</p>
<p><strong>14. Be careful which third-party libraries, CDNs and plugins you use</strong></p>
<p>They might be simply outdated, opening a wide variety of security holes, or they might be malicious – giving access to the shady library’s creator to your server.</p>
<p><strong>15. Bots are everywhere</strong></p>
<p>Take care of malicious bots not by banning their IP but by enhancing CAPTCHA, adding hidden form fields that users would not fill, and requiring JavaScript or cookies enabled to submit a form.</p>
<p><strong>16. Use HTTP only cookies</strong></p>
<p>This would reduce the impact of some other attacks – such as XSS</p>
<p><strong>17. Hashing</strong></p>
<p>Hash your passwords and try to avoid md5 or sha-1 algorithms (<a href="https://community.qualys.com/blogs/securitylabs/2014/09/09/sha1-deprecation-what-you-need-to-know">https://community.qualys.com/blogs/securitylabs/2014/09/09/sha1-deprecation-what-you-need-to-know</a>, <a href="http://security.stackexchange.com/questions/33108/why-does-some-popular-software-still-use-md5">http://security.stackexchange.com/questions/33108/why-does-some-popular-software-still-use-md5</a> ). Use salts to prevent attacks with rainbow tables.</p>
<p><strong>18. XSS</strong></p>
<p>Always escape input unless you really, really trust the source (admin panel). You can either remove tags or display them as entities depending on your needs. | PHP: strip_tags($input, $allowedTags); htmlspecialchars($input, ENT_QUOTES); htmlentities($input); |</p>
<p><strong>19. SQL Injection</strong></p>
<p>Use prepared statements or do not perform a query which is not hardcoded without sanitizing it (PHP: PDO class or sanitize with mysqli_real_escape_string($conn, $str) if using mysqli. Do not use mysql_*).</p>
<h2>Conclusion</h2>
<p>This was the last part of the Website Hacking series. We have introduced some new vulnerabilities and briefly discussed them and have summarized our points for everything that we have talked about so far.</p>
<p>We hope that now you will feel more confident when deploying your web apps by putting these strategies in use.</p>
<p>&nbsp;</p>
<p>To download the code that comes with this article and read it from its original place of publishment, please visit: <a target="_blank" href="http://resources.infosecinstitute.com/website-hacking-part-vii-common-web-vulnerabilities-brief-solutions/">http://resources.infosecinstitute.com/website-hacking-part-vii-common-web-vulnerabilities-brief-solutions/</a></p>
<nav class="series-pagination"></nav>
<p>&nbsp;</p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions%2F&#038;t=Website%20Hacking%2C%20Part%20VII%3A%20Common%20Web%20Vulnerabilities%20and%20Brief%20Solutions&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions%2F&#038;p&#091;images&#093;&#091;0&#093;=http%3A%2F%2F2we26u4fam7n16rz3a44uhbe1bq2.wpengine.netdna-cdn.com%2Fwp-content%2Fuploads%2F020915_1214_WebsiteHack1.png&#038;p&#091;title&#093;=Website%20Hacking%2C%20Part%20VII%3A%20Common%20Web%20Vulnerabilities%20and%20Brief%20Solutions" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions%2F&#038;title=Website%20Hacking%2C%20Part%20VII%3A%20Common%20Web%20Vulnerabilities%20and%20Brief%20Solutions" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions%2F&#038;media=http%3A%2F%2F2we26u4fam7n16rz3a44uhbe1bq2.wpengine.netdna-cdn.com%2Fwp-content%2Fuploads%2F020915_1214_WebsiteHack1.png&#038;description=Website%20Hacking%2C%20Part%20VII%3A%20Common%20Web%20Vulnerabilities%20and%20Brief%20Solutions" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions%2F&#038;title=Website%20Hacking%2C%20Part%20VII%3A%20Common%20Web%20Vulnerabilities%20and%20Brief%20Solutions" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Website%20Hacking%2C%20Part%20VII%3A%20Common%20Web%20Vulnerabilities%20and%20Brief%20Solutions&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/website-hacking-part-vii-common-web-vulnerabilities-and-brief-solutions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Laravel 4 Basics</title>
		<link>https://www.dimoff.biz/blog/laravel-4-basics/</link>
		<comments>https://www.dimoff.biz/blog/laravel-4-basics/#comments</comments>
		<pubDate>Sun, 25 Jan 2015 20:42:18 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dimoff.biz/blog/?p=57</guid>
		<description><![CDATA[Installation To install Laravel first you need to download and set up Composer. If you are using Windows you can grab the .exe file and run it which will set&#8230;]]></description>
				<content:encoded><![CDATA[<p><strong>Installation</strong></p>
<p>To install Laravel first you need to download and set up Composer. If you are using Windows you can grab the .exe file and run it which will set Composer up automatically.</p>
<p>For other operating systems, you should follow the instructions at: <a href="https://getcomposer.org/download/" target="_blank" rel="nofollow">https://getcomposer.org/download/</a><br />
To set up your first Laravel project you browse to the folder where composer has been installed (with something like cd E:\Programs\XAMPP\php in your terminal/command prompt and then you enter:</p>
<p>php composer create-project laravel/laravel E:\LaravelBasics\ –prefer-dist</p>
<p>This particular command is telling composer to create a template of a project using the Laravel framework and set it up in the Laravel Basics folder of E: drive.</p>
<p>&nbsp;</p>
<p>This will create all necessary files for a Laravel application.</p>
<p>&nbsp;</p>
<p><strong>Creating a Basic Application using Laravel</strong></p>
<p>Users will see the public folder.</p>
<p>We have moved the LaravelBasics folder but you can see the path to access the application:</p>
<p>http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/</p>
<p>The first thing that you have to do when starting to develop a Laravel application from scratch is to set Laravel to show errors. If you do not set this up you will always get “Whoops, looks like something went wrong” when there is something wrong with your code.</p>
<p>To do this, open app/config/app.php and set ‘debug’ to true.</p>
<p>Now open app/routes.php</p>
<p>Routes basically tell the application which controller should take a particular URL request or process it themselves by interacting with the model and the view (the view is what is going to be displayed to the user)</p>
<p>Add this route to the routes.php</p>
<div id="crayon-54b70b694e550580256172" class="crayon-syntax crayon-theme-ssms2012 crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" data-flexie-id="1" data-flexie-parent="true">
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">
<table class="crayon-table">
<tbody>
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content">
<div class="crayon-num" data-line="crayon-54b70b694e550580256172-1">1</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e550580256172-2">2</div>
<div class="crayon-num" data-line="crayon-54b70b694e550580256172-3">3</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e550580256172-4">4</div>
<div class="crayon-num" data-line="crayon-54b70b694e550580256172-5">5</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e550580256172-6">6</div>
<div class="crayon-num" data-line="crayon-54b70b694e550580256172-7">7</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e550580256172-8">8</div>
<div class="crayon-num" data-line="crayon-54b70b694e550580256172-9">9</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e550580256172-10">10</div>
<div class="crayon-num" data-line="crayon-54b70b694e550580256172-11">11</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e550580256172-12">12</div>
</div>
</td>
<td class="crayon-code">
<div class="crayon-pre">
<div id="crayon-54b70b694e550580256172-1" class="crayon-line"><span class="crayon-e">Route::</span><span class="crayon-e">get</span><span class="crayon-sy">(</span><span class="crayon-s">&#8216;guess/{numbers?}&#8217;</span><span class="crayon-sy">,</span> <span class="crayon-t">function</span><span class="crayon-sy">(</span><span class="crayon-v">$numbers</span><span class="crayon-o">=</span><span class="crayon-cn">6</span><span class="crayon-sy">)</span></div>
<div id="crayon-54b70b694e550580256172-2" class="crayon-line crayon-striped-line"><span class="crayon-sy">{</span></div>
<div id="crayon-54b70b694e550580256172-3" class="crayon-line"><span class="crayon-c">//Generate a random number between 0 and 20</span></div>
<div id="crayon-54b70b694e550580256172-4" class="crayon-line crayon-striped-line"><span class="crayon-v">$luckyNumber</span><span class="crayon-o">=</span> <span class="crayon-e">mt_rand</span><span class="crayon-sy">(</span><span class="crayon-cn">0</span><span class="crayon-sy">,</span><span class="crayon-cn">20</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70b694e550580256172-5" class="crayon-line"><span class="crayon-c">//Create an array with numbers</span></div>
<div id="crayon-54b70b694e550580256172-6" class="crayon-line crayon-striped-line"><span class="crayon-v">$numbers</span><span class="crayon-o">=</span><span class="crayon-e">explode</span><span class="crayon-sy">(</span><span class="crayon-s">&#8220;,&#8221;</span><span class="crayon-sy">,</span> <span class="crayon-v">$numbers</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70b694e550580256172-7" class="crayon-line"><span class="crayon-st">if</span> <span class="crayon-sy">(</span><span class="crayon-e">count</span><span class="crayon-sy">(</span><span class="crayon-v">$numbers</span><span class="crayon-sy">)</span> <span class="crayon-o">&gt;</span><span class="crayon-cn">5</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70b694e550580256172-8" class="crayon-line crayon-striped-line"><span class="crayon-c">// Send to the view only the first 5 numbers</span></div>
<div id="crayon-54b70b694e550580256172-9" class="crayon-line"><span class="crayon-v">$numbers</span><span class="crayon-o">=</span><span class="crayon-e">array_slice</span><span class="crayon-sy">(</span><span class="crayon-v">$numbers</span><span class="crayon-sy">,</span> <span class="crayon-cn">0</span><span class="crayon-sy">,</span> <span class="crayon-cn">5</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70b694e550580256172-10" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70b694e550580256172-11" class="crayon-line"><span class="crayon-k ">return</span> <span class="crayon-e">View::</span><span class="crayon-e">make</span><span class="crayon-sy">(</span><span class="crayon-s">&#8216;guess&#8217;</span><span class="crayon-sy">)</span><span class="crayon-o">-&gt;</span><span class="crayon-e">withNumbers</span><span class="crayon-sy">(</span><span class="crayon-v">$numbers</span><span class="crayon-sy">)</span><span class="crayon-o">-&gt;</span><span class="crayon-e">with</span><span class="crayon-sy">(</span><span class="crayon-s">&#8220;lucky&#8221;</span><span class="crayon-sy">,</span> <span class="crayon-v">$luckyNumber</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70b694e550580256172-12" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>This basically says that if the URL is</p>
<p>http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess/10,12,15</p>
<p>or</p>
<p>http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess/16 the file called guess.php/guess.blade.php has to be shown to the user (you do not have to write the file extension in the <em>View::make()</em>’s argument. A text in {} (curly brackets) means that a random input may follow the request (guess/) in the URL and ? means that it is optional for the URL request to have the random input for which the ? applies. Thus, if we open the URL without providing a number like that:</p>
<p>http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess the view will load with the default number that we have given (6).</p>
<p>If we write <em>Route::get(‘guess/numbers/’)</em>… then onlyhttp://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess/number/ would lead to displaying the ‘guess’ view.</p>
<p><em>withNumbers($number);</em> is a magic method, it is the same as with(‘numbers’, $number) meaning that the View ‘guess’ would be able to use the $numbers variable from the route/URL whenever it wants to use it. It could be explained as with(‘variable name in the view’, ‘variable to pass to the view’).</p>
<p>Then in app/views we create a file called guess.blade.php to make use of the Blade Templating Engine.</p>
<div id="crayon-54b70b694e58d103392027" class="crayon-syntax crayon-theme-ssms2012 crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" data-flexie-id="2" data-flexie-parent="true">
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">
<table class="crayon-table">
<tbody>
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content">
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-1">1</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-2">2</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-3">3</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-4">4</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-5">5</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-6">6</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-7">7</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-8">8</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-9">9</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-10">10</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-11">11</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-12">12</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-13">13</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-14">14</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-15">15</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-16">16</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-17">17</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-18">18</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-19">19</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-20">20</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-21">21</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-22">22</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-23">23</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-24">24</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-25">25</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-26">26</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-27">27</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70b694e58d103392027-28">28</div>
<div class="crayon-num" data-line="crayon-54b70b694e58d103392027-29">29</div>
</div>
</td>
<td class="crayon-code">
<div class="crayon-pre">
<div id="crayon-54b70b694e58d103392027-1" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-o">!</span><span class="crayon-e">DOCTYPE </span><span class="crayon-v">html</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-2" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-e">html </span><span class="crayon-v">lang</span><span class="crayon-o">=</span><span class="crayon-s">&#8220;en&#8221;</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-3" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-v">head</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-4" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-e">meta </span><span class="crayon-v">charset</span><span class="crayon-o">=</span><span class="crayon-s">&#8220;UTF-8&#8243;</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-5" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-v">title</span><span class="crayon-o">&gt;</span><span class="crayon-e">Lucky </span><span class="crayon-e">Number </span><span class="crayon-v">Guesser</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">title</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-6" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">head</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-7" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-e">body </span><span class="crayon-v">style</span><span class="crayon-o">=</span><span class="crayon-s">&#8220;font-size: 1.2em;&#8221;</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-8" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70b694e58d103392027-9" class="crayon-line"></div>
<div id="crayon-54b70b694e58d103392027-10" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-e">div </span><span class="crayon-v">style</span><span class="crayon-o">=</span><span class="crayon-s">&#8220;text-align: center;background-color: #eee;&#8221;</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-11" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-v">h1</span><span class="crayon-o">&gt;</span> <span class="crayon-e">Did </span><span class="crayon-e">you </span><span class="crayon-e">guess </span><span class="crayon-e">the </span><span class="crayon-e">lucky </span><span class="crayon-v">number</span><span class="crayon-sy">?</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">h1</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-12" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-v">p</span><span class="crayon-o">&gt;</span><span class="crayon-e">You </span><span class="crayon-e">can </span><span class="crayon-st">try</span> <span class="crayon-i">maximum</span> <span class="crayon-cn">5</span> <span class="crayon-e">numbers </span><span class="crayon-st">in</span> <span class="crayon-i">a</span> <span class="crayon-v">comma</span><span class="crayon-o">-</span><span class="crayon-e">separated </span><span class="crayon-e">listand </span><span class="crayon-e">the </span><span class="crayon-e">actual </span><span class="crayon-e">lucky </span><span class="crayon-e">number </span><span class="crayon-st">is</span> <span class="crayon-i">between</span> <span class="crayon-cn">0and20</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">p</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-13" class="crayon-line"></div>
<div id="crayon-54b70b694e58d103392027-14" class="crayon-line crayon-striped-line"><span class="crayon-sy">@</span><span class="crayon-st">foreach</span> <span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-v">numbersas</span><span class="crayon-sy">$</span><span class="crayon-v">number</span><span class="crayon-sy">)</span></div>
<div id="crayon-54b70b694e58d103392027-15" class="crayon-line"><span class="crayon-sy">@</span><span class="crayon-st">if</span> <span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-v">number</span><span class="crayon-o">==</span><span class="crayon-sy">$</span><span class="crayon-v">lucky</span><span class="crayon-sy">)</span></div>
<div id="crayon-54b70b694e58d103392027-16" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-e">h1 </span><span class="crayon-v">style</span><span class="crayon-o">=</span><span class="crayon-s">&#8216;background-color: lime;color:#000;padding:10px;&#8217;</span><span class="crayon-o">&gt;</span> <span class="crayon-e">You</span> <span class="crayon-e">said</span> <span class="crayon-sy">{</span><span class="crayon-sy">{</span><span class="crayon-sy">{</span> <span class="crayon-sy">$</span><span class="crayon-i">number</span> <span class="crayon-sy">}</span><span class="crayon-sy">}</span><span class="crayon-sy">}</span><span class="crayon-sy">.</span> <span class="crayon-e">You</span> <span class="crayon-e">guessed</span> <span class="crayon-e">the</span> <span class="crayon-e">lucky</span> <span class="crayon-e">number</span><span class="crayon-o">!</span> <span class="crayon-e">Jackpot</span><span class="crayon-o">!</span> <span class="crayon-e">The</span> <span class="crayon-e">lucky</span> <span class="crayon-e">number</span> <span class="crayon-e">was</span> <span class="crayon-sy">{</span><span class="crayon-sy">{</span> <span class="crayon-sy">$</span><span class="crayon-i">lucky</span> <span class="crayon-sy">}</span><span class="crayon-sy">}</span> <span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">h1</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-17" class="crayon-line"><span class="crayon-sy">@</span><span class="crayon-st">else</span></div>
<div id="crayon-54b70b694e58d103392027-18" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-e">h1 </span><span class="crayon-v">style</span><span class="crayon-o">=</span><span class="crayon-s">&#8216;background-color: crimson;color:#fff;padding:10px;&#8217;</span><span class="crayon-o">&gt;</span> <span class="crayon-e">You</span> <span class="crayon-e">said</span> <span class="crayon-sy">{</span><span class="crayon-sy">{</span><span class="crayon-sy">{</span> <span class="crayon-sy">$</span><span class="crayon-i">number</span> <span class="crayon-sy">}</span><span class="crayon-sy">}</span><span class="crayon-sy">}</span><span class="crayon-sy">.</span> <span class="crayon-e">You </span><span class="crayon-e">did </span><span class="crayon-st">not</span> <span class="crayon-e">manage </span><span class="crayon-st">to</span> <span class="crayon-e">guess </span><span class="crayon-e">the </span><span class="crayon-e">lucky </span><span class="crayon-v">number</span><span class="crayon-sy">.</span> <span class="crayon-e">The</span> <span class="crayon-e">lucky</span> <span class="crayon-e">number</span> <span class="crayon-e">was</span> <span class="crayon-sy">{</span><span class="crayon-sy">{</span> <span class="crayon-sy">$</span><span class="crayon-i">lucky</span> <span class="crayon-sy">}</span><span class="crayon-sy">}</span> <span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">h1</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-19" class="crayon-line"><span class="crayon-sy">@</span><span class="crayon-st">endif</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70b694e58d103392027-20" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70b694e58d103392027-21" class="crayon-line"></div>
<div id="crayon-54b70b694e58d103392027-22" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70b694e58d103392027-23" class="crayon-line"></div>
<div id="crayon-54b70b694e58d103392027-24" class="crayon-line crayon-striped-line"><span class="crayon-sy">@</span><span class="crayon-st">endforeach</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70b694e58d103392027-25" class="crayon-line"></div>
<div id="crayon-54b70b694e58d103392027-26" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">div</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-27" class="crayon-line"></div>
<div id="crayon-54b70b694e58d103392027-28" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">body</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70b694e58d103392027-29" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">html</span><span class="crayon-o">&gt;</span></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>In the Blade’s templating engine {{ }} (double curly brackets) work just like PHP’s echo or print statements and {{{ }}} (triple curly brackets works the same way but they also sanitize the input).</p>
<p>You can see a quick demo in the code above of how to use if/else statements and foreach loops using Blade.</p>
<p>Now if we type an URL like:</p>
<p>http://localhost:8079/current/articles/Laravel%20Basics/LaravelBasics/public/guess/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 only our first 5 could-be-lucky numbers will be compared against the real lucky number.</p>
<p><img class="aligncenter size-full wp-image-845" src="http://images.phpgang.com/2015/01/laravel-basics-demo.jpg" alt="laravel basics demo" width="631" height="299" /></p>
<p>To download the code associated with this article, visit the original place where the article was published: <a href="http://www.phpgang.com/laravel-4-basics-part-1_843.html" target="_blank">http://www.phpgang.com/laravel-4-basics-part-1_843.html</a></p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Flaravel-4-basics%2F&#038;t=Laravel%204%20Basics&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Flaravel-4-basics%2F&#038;p&#091;images&#093;&#091;0&#093;=http%3A%2F%2Fimages.phpgang.com%2F2015%2F01%2Flaravel-basics-demo.jpg&#038;p&#091;title&#093;=Laravel%204%20Basics" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Flaravel-4-basics%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Flaravel-4-basics%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Flaravel-4-basics%2F&#038;title=Laravel%204%20Basics" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Flaravel-4-basics%2F&#038;media=http%3A%2F%2Fimages.phpgang.com%2F2015%2F01%2Flaravel-basics-demo.jpg&#038;description=Laravel%204%20Basics" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Flaravel-4-basics%2F&#038;title=Laravel%204%20Basics" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Laravel%204%20Basics&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Flaravel-4-basics%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/laravel-4-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Website Hacking, Part VI: Input Validation and Filtering in PHP</title>
		<link>https://www.dimoff.biz/blog/website-hacking-part-vi-input-validation-and-filtering-in-php/</link>
		<comments>https://www.dimoff.biz/blog/website-hacking-part-vi-input-validation-and-filtering-in-php/#comments</comments>
		<pubDate>Sun, 25 Jan 2015 17:42:05 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Information Security]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dimoff.biz/blog/?p=52</guid>
		<description><![CDATA[In this part of the series, we are going to examine the different ways to escape HTML characters in PHP in order to add security to your web project. We&#8230;]]></description>
				<content:encoded><![CDATA[<p>In this part of the series, we are going to examine the different ways to escape HTML characters in PHP in order to add security to your web project. We will also give a brief introduction to PHP’s Perl-compatible regular expressions and show how they can be used for input validation. We are also going to examine PHP 5′s built-in input validation and filtering methods (focusing mostly on filter_var).</p>
<p>&nbsp;</p>
<h2>Transforming HTML characters</h2>
<p>If we have some code, for example a search engine in our website which responds to get parameters and has the following snippet:</p>
<p>if (isset($_GET[&#8216;q&#8217;])) {<br />
echo &#8220;500 results for the query &#8221; . $_GET[&#8216;q&#8217;];<br />
}</p>
<p>A legitimate user might get a page resembling something like this:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack1.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack1.png" /></p>
<p>However, any user is going to be able to add tags to the queries and at the very least change drastically the way your page is formatted. For example, he can target particular browsers and send links with malicious GET parameters which would load external JavaScript files.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack2.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack2.png" /></p>
<p>Above is an example of how we can easily change both HTML and CSS on the page (a relatively harmless example).</p>
<h3>htmlspecialchars</h3>
<p>To combat this, we can use htmlspecialchars(), htmlentities() or strip_tags(); htmlspecialchars() takes a string and as optional parameters – ‘flags’, the ‘encoding’ to be used when converting the characters and a ‘double encoding’ option which is set to true by default and when turned off forces PHP not to encode existing HTML entities.</p>
<p>A sample usage would prevent such XSS vulnerabilities and show the tags instead of applying them:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack3.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack3.png" /></p>
<p>if (isset($_GET[&#8216;q&#8217;])) {<br />
echo &#8220;500 results for the query &#8221; . htmlspecialchars($_GET[&#8216;q&#8217;]);<br />
}</p>
<p>However, htmlspecialchars only changes the ampersand, double quotes, and less and greater than symbols by default.</p>
<p>Thus, we could still get undesired effects. For example, here is a sample way to apply tags when the single quotes are not escaped.</p>
<p>Suppose we have the following snippet:</p>
<p>if (isset($_GET[&#8216;q&#8217;])) {<br />
$query = htmlspecialchars($_GET[&#8216;q&#8217;]);<br />
echo &#8220;500 results for the query &#8221; . &#8220;&lt;a href='&#8221; . $query . &#8220;.html&#8217;&gt; $query &lt;/a&gt;&#8221;;</p>
<p>}</p>
<p>A legitimate request would look like this:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack4.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack4.png" /></p>
<p>The line just adds a link to the page that points to an HTML file (it would be dynamically generated) named after the sought keyword and displays the keyword as the text child node of the anchor. You can see that the $query variable is passed to the anchor and to the text which contains the escaped with htmlspecialchars() input.</p>
<p>However, consider if the user tries to see whether the single quote is also escaped and types something like:</p>
<p>http://localhost:8079/Tests/index.php?q=Chocolate’ style=’font-size:5em’</p>
<p>Then the user has successfully closed our anchor tag and added an arbitrary attribute. He can then try to add inline JavaScript and keep on testing for ways to exploit the vulnerability.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack5.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack5.png" /></p>
<p><strong>Figure 1: After the single quote exploit</strong></p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack6.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack6.png" /></p>
<p><strong>Figure 2: Legitimate request (the anchor before the exploit)</strong></p>
<p>To fix this, we just pass the ENT_QUOTES flag.</p>
<p>if (isset($_GET[&#8216;q&#8217;])) {<br />
$query = htmlspecialchars($_GET[&#8216;q&#8217;], ENT_QUOTES);<br />
echo &#8220;500 results for the query &#8221; . &#8220;&lt;a href='&#8221; . $query . &#8220;.html&#8217;&gt; $query &lt;/a&gt;&#8221;;<br />
}</p>
<p>After we escape the single quotes as well, this vulnerability vanishes.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack7.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack7.png" /></p>
<p>To transform an escaped string containing markup to markup, again we use:</p>
<div>
<div id="highlighter_987098" class="syntaxhighlighter  plain">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="plain plain">htmlspecialchars_decode($query);</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h3>Strip Tags</h3>
<p>If you want to be more radical, you can remove all HTML and PHP tags from a string or remove only a selection of them. The built-in function strip_tags() takes a string in which to remove the tags and optionally another string that pinpoints which tags are allowed.</p>
<p>if (isset($_GET[&#8216;q&#8217;])) {<br />
$query = strip_tags($_GET[&#8216;q&#8217;]);<br />
echo &#8220;500 results for the query &#8221; . &#8220;&lt;a href='&#8221; . $query . &#8220;.html&#8217;&gt; $query &lt;/a&gt;&#8221;;</p>
<p>}</p>
<p>The above code results in all tags being removed from the string.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack8.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack8.png" /></p>
<p>Optionally, we can allow any tag we want, but we have to do some manual escaping as users can enter whatever attributes they want. Of course, contemporary browsers have XSS protection, but there are still clients with older systems that could be targeted with such malicious links.</p>
<p>if (isset($_GET[&#8216;q&#8217;])) {<br />
// We are allowing only some of the text formatting tags but any type of attribute may be added to them by the user<br />
$query = strip_tags($_GET[&#8216;q&#8217;], &#8216;&lt;p&gt;&lt;b&gt;&lt;em&gt;&lt;strong&gt;&lt;i&gt;&lt;small&gt;&#8217;);<br />
echo &#8220;500 results for the query &#8221; . $query . &#8220;&lt;/a&gt;&#8221;;</p>
<p>}</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack9.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack9.png" /></p>
<p>We get an error in the console telling us that the XSS Auditor did not execute the script on up to date Chrome, but this would not be necessarily the response all users will get.</p>
<h3>Htmlentities</h3>
<p>Another function you can use is htmlentities()</p>
<p>The difference between htmlspecialchars and htmlentities is that htmlentities translates all HTML characters entity equivalents to the particular entities. This basically mean that it applies also to entities such as &amp;copy; (the copyright symbol), &amp;euro; (the euro symbol) and all others.</p>
<p>For example, if we use htmlspecialchars() and enter the euro sign we will get the following result:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack10.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack10.png" /></p>
<p>However, if we use htmlentities, the euro character will be properly translated to its relevant HTML entity:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack11.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/010715_2257_WebsiteHack11.png" /></p>
<p>It is it important to know the default flag of htmlentities() is ENT_COMPAT, which only converts double quotes (single quotes are not translated like they are in htmlspecialchars()).</p>
<p>Therefore, you also have to use ENT_QUOTES where appropriate:</p>
<p>if (isset($_GET[&#8216;q&#8217;])) {<br />
$query = htmlentities($_GET[&#8216;q&#8217;], ENT_QUOTES);<br />
echo &#8220;500 results for the query &#8221; . &#8220;&lt;a href='&#8221; . $query . &#8220;.html&#8217;&gt; $query &lt;/a&gt;&#8221;;</p>
<p>}</p>
<h2>Validating input</h2>
<p>For most purposes the built-in function filter_var can be used. It is available in servers with greater than 5.2.0 version of PHP. It takes a variable or static input and returns false on failure and the filtered data on success. We can use it for validation and sanitization of input.</p>
<p><strong>It has to be mentioned that filter_var’s validation mechanisms do not only tell you if the input is valid but also sanitize it by removing the illegal characters.</strong></p>
<p>Below is an example of how it works:</p>
<p>if (isset($_POST[&#8216;submit&#8217;])) {<br />
$email = filter_var($_POST[&#8216;email&#8217;], FILTER_VALIDATE_EMAIL);</p>
<p>if ($email) {<br />
echo &#8220;Your email $email is legit. Save it into the database.&#8221;;<br />
}<br />
else<br />
echo &#8220;Your email $email is invalid. Redirect to form.&#8221;;<br />
}<br />
?&gt;<br />
&lt;form method=&#8221;POST&#8221;&gt;<br />
&lt;label for=&#8221;email&#8221;&gt;Email&lt;/label&gt;<br />
&lt;input type=&#8221;text&#8221; name=&#8221;email&#8221;&gt;<br />
&lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; value=&#8221;Send&#8221;&gt;</p>
<p>&lt;/form&gt;</p>
<p>This script will display that the email is legit if it is a valid email or display that the email is invalid if it is not.</p>
<p>There are also validation filters for Booleans (FILTER_VALIDATE_BOOLEAN) which returns true only when the input is one of the following string combinations:</p>
<ul>
<li>“1″</li>
<li>“true”</li>
<li>“on”</li>
<li>“yes”</li>
</ul>
<p>In every other string value it will return false.</p>
<p>Another validation filter is for floating point numbers (FILTER_VALIDATE_FLOAT) which does not return false when:</p>
<ul>
<li>The input is a numeric floating point value (Example: 22.2)</li>
<li>The input is a string containing a floating point value (Example: “22.2″, ’22.2′)</li>
</ul>
<p>Optionally, you can pass an option – FILTER_FLAG_ALLOW_THOUSAND would allow a thousand separator such as a comma (,). FILTER_FLAG_ALLOW_SCIENTIFIC would allow the number to be in scientific notation (e,E) and there is also FILTER_FLAG_ALLOW_FRACTION.</p>
<p>The FILTER_VALIDATE_INT filter would return the filtered integer or false on failure to extract an integer. There are flags to allow octal and hexadecimal numbers (FILTER_FLAG_ALLOW_OCTAL and FILTER_FLAG_ALLOW_HEX) and the ability to extract a number from a specified range. Here is a sample:</p>
<p>Options are passed in a two-dimensional array. The parent array contains the ‘flags’ index and ‘options’ is a nested array with all options where the key is the option name and the value is the value that the option should have. Here is how we can validate an integer to be between 1 and 100 and allow hexadecimal values.</p>
<p>When we pass 120 – false (or 0) is returned and we get a message that the number is invalid:</p>
<p>Your int 0 is invalid. Redirect to form.</p>
<p>If we enter 85, 85 is stored in $int and we get this statement:</p>
<p>Your int 85 is legit. Save it into the database.</p>
<p>Similarly, if we use a hexadecimal value below 100 (let’s say 10), the integer also passes the validation:</p>
<div>
<div id="highlighter_393976" class="syntaxhighlighter  plain">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="plain plain">$int = filter_var("0xA", FILTER_VALIDATE_INT, $options);</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>We are going to show one last example with the URL filter:</p>
<p>if (isset($_POST[&#8216;submit&#8217;])) {</p>
<p>$options = array(</p>
<p>&#8216;flags&#8217; =&gt; FILTER_FLAG_QUERY_REQUIRED</p>
<p>);<br />
$url = filter_var(&#8220;http://www.dimoff.biz&#8221;, FILTER_VALIDATE_URL, $options);</p>
<p>if ($url) {<br />
echo &#8220;Your URL $url is legit. Save it into the database.&#8221;;<br />
}<br />
else<br />
echo &#8220;Your URL $url is invalid. Redirect to form.&#8221;;</p>
<p>}</p>
<p>We are validating an URL and passing a flag to allow only URLs with a query string attached to them (a GET parameter). We get the following response:</p>
<p>Your URL is invalid. Redirect to form.</p>
<p>If we instead try the following URL, we will get a positive response:</p>
<div>
<div id="highlighter_490709" class="syntaxhighlighter  plain">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="plain plain">$url = filter_var("<a href="http://www.dimoff.biz/?id=1">http://www.dimoff.biz/?id=1</a>", FILTER_VALIDATE_URL, $options);</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Your URL http://www.dimoff.biz/?id=1 is legit. Save it into the database.</p>
<p>The drawback of the validation is that internationalized domain names would always fail validation (only Latin URLs will pass the test – those containing ASCII characters).</p>
<p>There are also filters to validate regular expressions and IP addresses (both IPv4 and IPv6).</p>
<p>You can check filter_var_array(), which can filter multiple variables inserted in an array at once.</p>
<h3>3.1 Validation through regular expressions</h3>
<p>There are times when the built-in validations are not sufficient or do not include the validation you require. In such cases you can use preg_replace, preg_match, preg_match_all or preg_grep to do the job.</p>
<p>For example, you may want to allow both Bulgarian and American zip codes. However, Bulgarian zip codes consist of 4 digits, whereas American zip codes consist of 5 digits.</p>
<p>To do this you can use regular expressions:</p>
<p>&lt;?php<br />
if (isset($_POST[&#8216;submit&#8217;])) {<br />
$zip = $_POST[&#8216;zip&#8217;];<br />
$is_zip = preg_match(&#8220;#^[0-9]{4,5}$#&#8221;, $zip);</p>
<p>if ($is_zip) {<br />
echo &#8220;Your zip code $zip is legit. Save it into the database.&#8221;;<br />
}<br />
else<br />
echo &#8220;Your zip code $zip is invalid. Redirect to form.&#8221;;</p>
<p>}<br />
?&gt;</p>
<p>&lt;form method=&#8221;POST&#8221;&gt;<br />
&lt;label for=&#8221;zip&#8221;&gt;Zip Code&lt;/label&gt;<br />
&lt;input type=&#8221;text&#8221; name=&#8221;zip&#8221;&gt;<br />
&lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; value=&#8221;Send&#8221;&gt;</p>
<p>&lt;/form&gt;</p>
<p>This regular expression tests if the input starts with a number which is repeated 4 or 5 and times and then ends. Here are some tests:</p>
<p>Your zip code 23135 is legit. Save it into the database.</p>
<p>Your zip code 2313 is legit. Save it into the database.</p>
<p>Your zip code 231 is invalid. Redirect to form.</p>
<p>Your zip code 231352 is invalid. Redirect to form.</p>
<h3>3.2 Regular Expressions 101</h3>
<p>Regular expressions in PHP must start and end with the same delimiter (usually /expression/ is used).</p>
<p>^ checks whether the input starts with something.</p>
<p>$ checks whether the input ends with something.</p>
<p>A value in square brackets [ ] means one of a particular character, for example [Abc] means the input can either be A or b or c.</p>
<p>[A-Z] means the input can be a single character anything between A to Z, for example M or D.</p>
<p>Uppercase and lowercase characters differ, so you would have to use [A-Za-z] if you wanted any alphabetic character. Similarly you could use 0-9 or d (which is almost the same but it includes some other characters).</p>
<p>? means the character preceding it can be repeated 0 or one time.</p>
<p>+ means it can be repeated one or more times.</p>
<p>* means the item can be repeated 0 or more times.</p>
<p>Alternatively you can provide a minimum number of repetitions [A-Z]{10} ( at least 10 characters) or a minimum and maximum number of characters [0-9]{4,5}.</p>
<p>. stands for any character (.? would mean any character zero or one times).</p>
<p>To escape these characters that are used within regular expressions and test for their literal character, you use backslash ( ). For example, #[A-Z+]#.</p>
<p>There are also some escaped characters with special meanings such as d for digit and s for space.</p>
<p>Characters enclosed in brackets signify that the values should be captured for future use. For example, you may try the following regular expression: preg_replace(“/([0-9]{4,5})-([A-Z]+)/”, “($1)$2″, $input);</p>
<p>If you give it a string such as “5432-PA”, it will transform it to become (5432)PA.</p>
<p>Preg_replace takes a regular expression as a first argument, the replacement string as second, and the variable to look into as third.</p>
<p>In the replacement string, $0 would give the whole original string, $1 would be the first bracketed item, $2 the second and so on.</p>
<p>Also, a way to filter input is using the ^ symbol in the beginning of square brackets in a preg_replace call. It would mean replace everything different than the values following the caret (^).</p>
<p>For example, preg_replace(“/[^w]/”, “”, $input); would cause input such as &lt;script&gt;alert()&lt;/script&gt; to be filtered to “scriptalertscript”</p>
<p>w essentially matches all word characters and we are saying to replace all non-word characters with nothing.</p>
<h2>Conclusion</h2>
<p>We have covered some essential practices when working with input and we hope that you can start creating applications that are a little bit more secure and robust, or refactor existing projects by making them more secure with filter_var, regular expressions, or by filtering possible HTML coming from inputs.</p>
<p>&nbsp;</p>
<p>To download all the code associated with this article and read the original publication, open: <a target="_blank" href="http://resources.infosecinstitute.com/website-hacking-part-vi-input-validation-filtering-php/">http://resources.infosecinstitute.com/website-hacking-part-vi-input-validation-filtering-php/</a></p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vi-input-validation-and-filtering-in-php%2F&#038;t=Website%20Hacking%2C%20Part%20VI%3A%20Input%20Validation%20and%20Filtering%20in%20PHP&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vi-input-validation-and-filtering-in-php%2F&#038;p&#091;images&#093;&#091;0&#093;=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F010715_2257_WebsiteHack1.png&#038;p&#091;title&#093;=Website%20Hacking%2C%20Part%20VI%3A%20Input%20Validation%20and%20Filtering%20in%20PHP" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vi-input-validation-and-filtering-in-php%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vi-input-validation-and-filtering-in-php%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vi-input-validation-and-filtering-in-php%2F&#038;title=Website%20Hacking%2C%20Part%20VI%3A%20Input%20Validation%20and%20Filtering%20in%20PHP" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vi-input-validation-and-filtering-in-php%2F&#038;media=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F010715_2257_WebsiteHack1.png&#038;description=Website%20Hacking%2C%20Part%20VI%3A%20Input%20Validation%20and%20Filtering%20in%20PHP" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vi-input-validation-and-filtering-in-php%2F&#038;title=Website%20Hacking%2C%20Part%20VI%3A%20Input%20Validation%20and%20Filtering%20in%20PHP" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Website%20Hacking%2C%20Part%20VI%3A%20Input%20Validation%20and%20Filtering%20in%20PHP&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-vi-input-validation-and-filtering-in-php%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/website-hacking-part-vi-input-validation-and-filtering-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using PHP and SimpleXML to parse WordPress feeds</title>
		<link>https://www.dimoff.biz/blog/using-php-and-simplexml-to-parse-wordpress-feeds/</link>
		<comments>https://www.dimoff.biz/blog/using-php-and-simplexml-to-parse-wordpress-feeds/#comments</comments>
		<pubDate>Sat, 17 Jan 2015 11:09:51 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.dimoff.biz/blog/?p=46</guid>
		<description><![CDATA[SimpleXML is probably the easiest way to parse a XML document. We have created a class that only has to be instantiated to get the feed’s data and added a&#8230;]]></description>
				<content:encoded><![CDATA[<p>SimpleXML is probably the easiest way to parse a XML document. We have created a class that only has to be instantiated to get the feed’s data and added a utility function to display it in a tabular format (HTML table). We have also added some simple CSS styling to the table to make it look better than the browser’s default rendering. It has to be noted that this is just for demonstration and teaching’s sake and it could be better to use XMLReader instead of SimpleXML for lengthy XML files because SimpleXML processes the inputs at once by writing them to the memory which is not a good idea if you have a big XML file.</p>
<p>&nbsp;</p>
<p>This is a practical tutorial of using SimpleXML to read XML files.</p>
<p>We start with the standard html skeleton, include our WordPressFeed class and start feeding it with feeds.</p>
<div id="crayon-54b70f9732749047488050" class="crayon-syntax crayon-theme-ssms2012 crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" data-flexie-id="1" data-flexie-parent="true">
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">
<table class="crayon-table">
<tbody>
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content">
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-1">1</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-2">2</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-3">3</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-4">4</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-5">5</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-6">6</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-7">7</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-8">8</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-9">9</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-10">10</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-11">11</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-12">12</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-13">13</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-14">14</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-15">15</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-16">16</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-17">17</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-18">18</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-19">19</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-20">20</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-21">21</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-22">22</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-23">23</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-24">24</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-25">25</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-26">26</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-27">27</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-28">28</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-29">29</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-30">30</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-31">31</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-32">32</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-33">33</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-34">34</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-35">35</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-36">36</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-37">37</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-38">38</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-39">39</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-40">40</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-41">41</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-42">42</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-43">43</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-44">44</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-45">45</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-46">46</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-47">47</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-48">48</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-49">49</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-50">50</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-51">51</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-52">52</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-53">53</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-54">54</div>
<div class="crayon-num" data-line="crayon-54b70f9732749047488050-55">55</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732749047488050-56">56</div>
</div>
</td>
<td class="crayon-code">
<div class="crayon-pre">
<div id="crayon-54b70f9732749047488050-1" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-o">!</span><span class="crayon-e">DOCTYPE </span><span class="crayon-v">html</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732749047488050-2" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-o">!</span><span class="crayon-o">&#8211;</span> <span class="crayon-st">To</span> <span class="crayon-e">get </span><span class="crayon-e">the </span><span class="crayon-e">XML </span><span class="crayon-e">feed </span><span class="crayon-i">of</span> <span class="crayon-i">a</span> <span class="crayon-e">WordPress </span><span class="crayon-e">page </span><span class="crayon-e">you </span><span class="crayon-v">enter</span><span class="crayon-o">:</span> <span class="crayon-v">http</span><span class="crayon-o">:</span><span class="crayon-c">//www.page.com/feed or http://www.page.com/comments/feed or the relevant blog URL of the feed &#8211;&gt;</span></div>
<div id="crayon-54b70f9732749047488050-3" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-e">html </span><span class="crayon-v">lang</span><span class="crayon-o">=</span><span class="crayon-s">&#8220;en&#8221;</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732749047488050-4" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-e">head</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732749047488050-5" class="crayon-line"><span class="crayon-ta">&lt;style&gt;</span></div>
<div id="crayon-54b70f9732749047488050-6" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-7" class="crayon-line"><span class="crayon-k ">table.feed_table  </span><span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732749047488050-8" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-9" class="crayon-line"><span class="crayon-e ">font-family</span><span class="crayon-sy">:</span><span class="crayon-s">&#8216;Arial&#8217;</span><span class="crayon-i ">,</span> <span class="crayon-i ">sans-serif</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-10" class="crayon-line crayon-striped-line"><span class="crayon-e ">width</span><span class="crayon-sy">:</span><span class="crayon-i ">75%</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-11" class="crayon-line"><span class="crayon-e ">margin</span><span class="crayon-sy">:</span><span class="crayon-i ">25px</span> <span class="crayon-i ">auto</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-12" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732749047488050-13" class="crayon-line"><span class="crayon-k ">.feed_tabletr:nth-of-type(even) </span><span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732749047488050-14" class="crayon-line crayon-striped-line"><span class="crayon-e ">background-color</span><span class="crayon-sy">:</span><span class="crayon-i ">#eee</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-15" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732749047488050-16" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-17" class="crayon-line"><span class="crayon-k ">.feed_tableth </span><span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732749047488050-18" class="crayon-line crayon-striped-line"><span class="crayon-e ">font-size</span><span class="crayon-sy">:</span><span class="crayon-i ">1.8em</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-19" class="crayon-line"><span class="crayon-e ">padding</span><span class="crayon-sy">:</span><span class="crayon-i ">10px</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-20" class="crayon-line crayon-striped-line"><span class="crayon-e ">color</span><span class="crayon-sy">:</span><span class="crayon-i ">#fff</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-21" class="crayon-line"><span class="crayon-e ">background-color</span><span class="crayon-sy">:</span><span class="crayon-i ">#333</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-22" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732749047488050-23" class="crayon-line"></div>
<div id="crayon-54b70f9732749047488050-24" class="crayon-line crayon-striped-line"><span class="crayon-k ">.feed_table td </span><span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732749047488050-25" class="crayon-line"><span class="crayon-e ">border</span><span class="crayon-sy">:</span><span class="crayon-i ">1px</span> <span class="crayon-i ">solid</span> <span class="crayon-i ">#aaa</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-26" class="crayon-line crayon-striped-line"><span class="crayon-e ">text-align</span><span class="crayon-sy">:</span> <span class="crayon-i ">center</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-27" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732749047488050-28" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-29" class="crayon-line"></div>
<div id="crayon-54b70f9732749047488050-30" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-31" class="crayon-line"><span class="crayon-ta">&lt;/style&gt;</span></div>
<div id="crayon-54b70f9732749047488050-32" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-33" class="crayon-line"></div>
<div id="crayon-54b70f9732749047488050-34" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-e">meta </span><span class="crayon-v">charset</span><span class="crayon-o">=</span><span class="crayon-s">&#8220;UTF-8&#8243;</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732749047488050-35" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-e">title</span><span class="crayon-o">&gt;</span><span class="crayon-e">WordPress</span> <span class="crayon-e">Feed</span> <span class="crayon-e">Fetcher</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-e">title</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732749047488050-36" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-e">head</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732749047488050-37" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-e">body</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732749047488050-38" class="crayon-line crayon-striped-line"><span class="crayon-ta">&lt;?</span><span class="crayon-e">phprequire_once</span><span class="crayon-sy">(</span><span class="crayon-s">&#8220;WordPressFeed.php&#8221;</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-39" class="crayon-line"></div>
<div id="crayon-54b70f9732749047488050-40" class="crayon-line crayon-striped-line"><span class="crayon-c">// Those two lines can be used to display a WP feed in an HTML table</span></div>
<div id="crayon-54b70f9732749047488050-41" class="crayon-line"><span class="crayon-v">$phpgang</span><span class="crayon-o">=</span><span class="crayon-e">newWordPressFeed</span><span class="crayon-sy">(</span><span class="crayon-s">&#8220;http://www.phpgang.com/feed/&#8221;</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-42" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-43" class="crayon-line"><span class="crayon-k ">echo</span><span class="crayon-v">$phpgang</span><span class="crayon-o">-&gt;</span><span class="crayon-e">showTabular</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-44" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-45" class="crayon-line"><span class="crayon-v">$infosec</span><span class="crayon-o">=</span><span class="crayon-e">newWordPressFeed</span><span class="crayon-sy">(</span><span class="crayon-s">&#8220;http://resources.infosecinstitute.com/feed/&#8221;</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-46" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-47" class="crayon-line"><span class="crayon-k ">echo</span><span class="crayon-v">$infosec</span><span class="crayon-o">-&gt;</span><span class="crayon-e">showTabular</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-48" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-49" class="crayon-line"><span class="crayon-v">$phpgang_posts</span><span class="crayon-o">=</span><span class="crayon-v">$phpgang</span><span class="crayon-o">-&gt;</span><span class="crayon-i">rawResults</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732749047488050-50" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-51" class="crayon-line"></div>
<div id="crayon-54b70f9732749047488050-52" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-53" class="crayon-line"><span class="crayon-ta">?&gt;</span></div>
<div id="crayon-54b70f9732749047488050-54" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732749047488050-55" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">body</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732749047488050-56" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-v">html</span><span class="crayon-o">&gt;</span></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>You can see that we have included the styles for the table in the head but you can include them in an external stylesheet and change them easily.<br />
Then we define our class (comments are directly inserted in the code for the class):</p>
<p>Feed’s data can be retrieved the following way:</p>
<div id="crayon-54b70f973277b975531475" class="crayon-syntax crayon-theme-ssms2012 crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" data-flexie-id="2" data-flexie-parent="true">
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">
<table class="crayon-table">
<tbody>
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content">
<div class="crayon-num" data-line="crayon-54b70f973277b975531475-1">1</div>
</div>
</td>
<td class="crayon-code">
<div class="crayon-pre">
<div id="crayon-54b70f973277b975531475-1" class="crayon-line"><span class="crayon-sy">$</span><span class="crayon-v">phpgang</span><span class="crayon-o">=</span><span class="crayon-e">newWordPressFeed</span><span class="crayon-sy">(</span><span class="crayon-s">&#8220;http://www.phpgang.com/feed/&#8221;</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Then the raw results (an array of all posts/articles and their data) can be accessed with the public property:</p>
<div id="crayon-54b70f9732786719998340" class="crayon-syntax crayon-theme-ssms2012 crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" data-flexie-id="3" data-flexie-parent="true">
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">
<table class="crayon-table">
<tbody>
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content">
<div class="crayon-num" data-line="crayon-54b70f9732786719998340-1">1</div>
</div>
</td>
<td class="crayon-code">
<div class="crayon-pre">
<div id="crayon-54b70f9732786719998340-1" class="crayon-line"><span class="crayon-v">$phpgang</span><span class="crayon-o">-&gt;</span><span class="crayon-i">rawResults</span><span class="crayon-sy">;</span></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The utility function to display the data in a table can be used the following way:\</p>
<div id="crayon-54b70f973278f903785199" class="crayon-syntax crayon-theme-ssms2012 crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" data-flexie-id="4" data-flexie-parent="true">
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">
<table class="crayon-table">
<tbody>
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content">
<div class="crayon-num" data-line="crayon-54b70f973278f903785199-1">1</div>
</div>
</td>
<td class="crayon-code">
<div class="crayon-pre">
<div id="crayon-54b70f973278f903785199-1" class="crayon-line"><span class="crayon-k ">echo</span> <span class="crayon-v">$phpgang</span><span class="crayon-o">-&gt;</span><span class="crayon-e">showTabular</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="crayon-54b70f9732798210978943" class="crayon-syntax crayon-theme-ssms2012 crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" data-flexie-id="5" data-flexie-parent="true">
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">
<table class="crayon-table">
<tbody>
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content">
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-1">1</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-2">2</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-3">3</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-4">4</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-5">5</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-6">6</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-7">7</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-8">8</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-9">9</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-10">10</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-11">11</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-12">12</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-13">13</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-14">14</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-15">15</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-16">16</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-17">17</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-18">18</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-19">19</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-20">20</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-21">21</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-22">22</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-23">23</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-24">24</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-25">25</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-26">26</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-27">27</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-28">28</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-29">29</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-30">30</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-31">31</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-32">32</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-33">33</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-34">34</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-35">35</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-36">36</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-37">37</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-38">38</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-39">39</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-40">40</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-41">41</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-42">42</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-43">43</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-44">44</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-45">45</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-46">46</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-47">47</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-48">48</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-49">49</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-50">50</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-51">51</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-52">52</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-53">53</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-54">54</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-55">55</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-56">56</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-57">57</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-58">58</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-59">59</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-60">60</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-61">61</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-62">62</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-63">63</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-64">64</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-65">65</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-66">66</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-67">67</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-68">68</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-69">69</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-70">70</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-71">71</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-72">72</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-73">73</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-74">74</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-75">75</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-76">76</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-77">77</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-78">78</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-79">79</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-80">80</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-81">81</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-82">82</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-83">83</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-84">84</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-85">85</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-86">86</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-87">87</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-88">88</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-89">89</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-90">90</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-91">91</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-92">92</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-93">93</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-94">94</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-95">95</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-96">96</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-97">97</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-98">98</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-99">99</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-100">100</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-101">101</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-102">102</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-103">103</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-104">104</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-105">105</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-106">106</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-107">107</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-108">108</div>
<div class="crayon-num" data-line="crayon-54b70f9732798210978943-109">109</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-54b70f9732798210978943-110">110</div>
</div>
</td>
<td class="crayon-code">
<div class="crayon-pre">
<div id="crayon-54b70f9732798210978943-1" class="crayon-line"><span class="crayon-ta">&lt;?php</span></div>
<div id="crayon-54b70f9732798210978943-2" class="crayon-line crayon-striped-line"><span class="crayon-c">// 30 seconds may not be enough </span></div>
<div id="crayon-54b70f9732798210978943-3" class="crayon-line"><span class="crayon-e">ini_set</span><span class="crayon-sy">(</span><span class="crayon-s">&#8216;max_execution_time&#8217;</span><span class="crayon-sy">,</span> <span class="crayon-cn">90</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-4" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-5" class="crayon-line"><span class="crayon-c">/*</span></div>
<div id="crayon-54b70f9732798210978943-6" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-7" class="crayon-line"><span class="crayon-c">*You can add methods to show in different ways, save to database. Anything you wish.</span></div>
<div id="crayon-54b70f9732798210978943-8" class="crayon-line crayon-striped-line"><span class="crayon-c">*You can also modify the class so it checks whether the elements exist or make it search for attributes or missing elements or whatever you want.</span></div>
<div id="crayon-54b70f9732798210978943-9" class="crayon-line"><span class="crayon-c">*/</span></div>
<div id="crayon-54b70f9732798210978943-10" class="crayon-line crayon-striped-line"><span class="crayon-e">classWordPressFeed</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-11" class="crayon-line"><span class="crayon-c">//$site holds the name of the site (&#8220;phpgang&#8221;, &#8220;lada&#8221;, etc.)</span></div>
<div id="crayon-54b70f9732798210978943-12" class="crayon-line crayon-striped-line"><span class="crayon-m">private</span><span class="crayon-v">$site</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-13" class="crayon-line"><span class="crayon-c">// $feedURL is the argument that you pass to the constructor</span></div>
<div id="crayon-54b70f9732798210978943-14" class="crayon-line crayon-striped-line"><span class="crayon-m">private</span><span class="crayon-v">$feedURL</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-15" class="crayon-line"><span class="crayon-c">// $rawResults is an array with all articles/posts.</span></div>
<div id="crayon-54b70f9732798210978943-16" class="crayon-line crayon-striped-line"><span class="crayon-m">public</span><span class="crayon-v">$rawResults</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-17" class="crayon-line"><span class="crayon-c">//creates $site</span></div>
<div id="crayon-54b70f9732798210978943-18" class="crayon-line crayon-striped-line"><span class="crayon-e">privatefunctiongetSite</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-19" class="crayon-line"><span class="crayon-c">//get only the site&#8217;s name and assign it to $site</span></div>
<div id="crayon-54b70f9732798210978943-20" class="crayon-line crayon-striped-line"><span class="crayon-v">$site_arr</span><span class="crayon-o">=</span><span class="crayon-e">explode</span><span class="crayon-sy">(</span><span class="crayon-s">&#8220;.&#8221;</span><span class="crayon-sy">,</span> <span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-i">feedURL</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-21" class="crayon-line"><span class="crayon-e">array_pop</span><span class="crayon-sy">(</span><span class="crayon-v">$site_arr</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-22" class="crayon-line crayon-striped-line"><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-i">site</span><span class="crayon-o">=</span><span class="crayon-e">array_pop</span><span class="crayon-sy">(</span><span class="crayon-v">$site_arr</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-23" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-24" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732798210978943-25" class="crayon-line"><span class="crayon-c">//gets the feed&#8217;s data upon instantiation</span></div>
<div id="crayon-54b70f9732798210978943-26" class="crayon-line crayon-striped-line"><span class="crayon-e">publicfunction__construct</span><span class="crayon-sy">(</span><span class="crayon-v">$feedURL</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-27" class="crayon-line"><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-i">feedURL</span><span class="crayon-o">=</span><span class="crayon-v">$feedURL</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-28" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-29" class="crayon-line"><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-i">rawResults</span><span class="crayon-o">=</span><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-e">getFeed</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-30" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-31" class="crayon-line"><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-e">getSite</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-32" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-33" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732798210978943-34" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-35" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-36" class="crayon-line crayon-striped-line"><span class="crayon-e">publicfunctionshowTabular</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-37" class="crayon-line"><span class="crayon-c">// shows the feed as a table</span></div>
<div id="crayon-54b70f9732798210978943-38" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-39" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-40" class="crayon-line crayon-striped-line"><span class="crayon-st">if</span> <span class="crayon-sy">(</span><span class="crayon-e">count</span><span class="crayon-sy">(</span><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-i">rawResults</span><span class="crayon-sy">)</span> <span class="crayon-o">&gt;</span><span class="crayon-cn">0</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-41" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-42" class="crayon-line crayon-striped-line"><span class="crayon-ta">?&gt;</span></div>
<div id="crayon-54b70f9732798210978943-43" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-e">h1 </span><span class="crayon-i">style</span><span class="crayon-o">=</span><span class="crayon-s">&#8220;text-align: center;&#8221;</span><span class="crayon-o">&gt;</span><span class="crayon-e">Feed </span><span class="crayon-st">for</span> <span class="crayon-i">Website</span><span class="crayon-o">:</span> <span class="crayon-ta">&lt;?</span><span class="crayon-i">phpecho</span><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-i">site</span><span class="crayon-ta">?&gt;</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-i">h1</span><span class="crayon-o">&gt;</span><span class="crayon-o">&lt;</span><span class="crayon-i">hr</span> <span class="crayon-o">/</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-44" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-e">table </span><span class="crayon-t">class</span><span class="crayon-o">=</span><span class="crayon-s">&#8220;feed_table&#8221;</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-45" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-i">tr</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-46" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span><span class="crayon-i">Title</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-47" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span><span class="crayon-cn">URL</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-48" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span><span class="crayon-i">Author</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-49" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span><span class="crayon-i">Categories</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-50" class="crayon-line crayon-striped-line"><span class="crayon-o">&lt;</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span><span class="crayon-i">Description</span><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-i">th</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-51" class="crayon-line"><span class="crayon-o">&lt;</span><span class="crayon-o">/</span><span class="crayon-i">tr</span><span class="crayon-o">&gt;</span></div>
<div id="crayon-54b70f9732798210978943-52" class="crayon-line crayon-striped-line"><span class="crayon-ta">&lt;?php</span></div>
<div id="crayon-54b70f9732798210978943-53" class="crayon-line"><span class="crayon-st">foreach</span> <span class="crayon-sy">(</span><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-i">rawResultsas</span><span class="crayon-v">$articleElement</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-54" class="crayon-line crayon-striped-line"><span class="crayon-k ">echo</span><span class="crayon-s">&#8220;&lt;tr&gt;&#8221;</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-55" class="crayon-line"><span class="crayon-k ">echo</span><span class="crayon-s">&#8220;&lt;td&gt;{$articleElement[&#8216;title&#8217;]}&lt;/td&gt;&#8221;</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-56" class="crayon-line crayon-striped-line"><span class="crayon-k ">echo</span><span class="crayon-s">&#8220;&lt;td&gt;{$articleElement[&#8216;link&#8217;]}&lt;/td&gt;&#8221;</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-57" class="crayon-line"><span class="crayon-k ">echo</span><span class="crayon-s">&#8220;&lt;td&gt;{$articleElement[&#8216;author&#8217;]}&lt;/td&gt;&#8221;</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-58" class="crayon-line crayon-striped-line"><span class="crayon-c">//create a string from all array indices</span></div>
<div id="crayon-54b70f9732798210978943-59" class="crayon-line"><span class="crayon-v">$categories</span><span class="crayon-o">=</span><span class="crayon-e">implode</span><span class="crayon-sy">(</span><span class="crayon-s">&#8220;, &#8220;</span><span class="crayon-sy">,</span> <span class="crayon-v">$articleElement</span><span class="crayon-sy">[</span><span class="crayon-s">&#8216;categories&#8217;</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-60" class="crayon-line crayon-striped-line"><span class="crayon-c">//remove html tags such as images and links from the description</span></div>
<div id="crayon-54b70f9732798210978943-61" class="crayon-line"><span class="crayon-v">$description</span><span class="crayon-o">=</span><span class="crayon-e">strip_tags</span><span class="crayon-sy">(</span><span class="crayon-v">$articleElement</span><span class="crayon-sy">[</span><span class="crayon-s">&#8216;description&#8217;</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-62" class="crayon-line crayon-striped-line"><span class="crayon-k ">echo</span><span class="crayon-s">&#8220;&lt;td&gt;$categories&lt;/td&gt;&#8221;</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-63" class="crayon-line"><span class="crayon-k ">echo</span><span class="crayon-s">&#8220;&lt;td&gt;$description&lt;/td&gt;&#8221;</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-64" class="crayon-line crayon-striped-line"><span class="crayon-k ">echo</span><span class="crayon-s">&#8220;&lt;/tr&gt;&#8221;</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-65" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-66" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732798210978943-67" class="crayon-line"><span class="crayon-k ">echo</span><span class="crayon-s">&#8220;&lt;/table&gt;&#8221;</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-68" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-69" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-70" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732798210978943-71" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-72" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-73" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732798210978943-74" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-75" class="crayon-line"><span class="crayon-e">privatefunctiongetFeed</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-76" class="crayon-line crayon-striped-line"><span class="crayon-c">//get feed&#8217;s data</span></div>
<div id="crayon-54b70f9732798210978943-77" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-78" class="crayon-line crayon-striped-line"><span class="crayon-c">// if it is an .xml file we need to use simplexml_load_file()</span></div>
<div id="crayon-54b70f9732798210978943-79" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-80" class="crayon-line crayon-striped-line"><span class="crayon-v">$feed</span><span class="crayon-o">=</span><span class="crayon-e">file_get_contents</span><span class="crayon-sy">(</span><span class="crayon-v">$this</span><span class="crayon-o">-&gt;</span><span class="crayon-i">feedURL</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-81" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-82" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-83" class="crayon-line"><span class="crayon-v">$allArticles</span><span class="crayon-o">=</span><span class="crayon-t">array</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-84" class="crayon-line crayon-striped-line"><span class="crayon-v">$xml</span><span class="crayon-o">=</span><span class="crayon-e">simplexml_load_string</span><span class="crayon-sy">(</span><span class="crayon-v">$feed</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-85" class="crayon-line"><span class="crayon-c">//loop through each post</span></div>
<div id="crayon-54b70f9732798210978943-86" class="crayon-line crayon-striped-line"><span class="crayon-st">foreach</span> <span class="crayon-sy">(</span><span class="crayon-v">$xml</span><span class="crayon-o">-&gt;</span><span class="crayon-i">channel</span><span class="crayon-o">-&gt;</span><span class="crayon-i">itemas</span><span class="crayon-v">$item</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-87" class="crayon-line"><span class="crayon-v">$category</span><span class="crayon-o">=</span><span class="crayon-t">array</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-88" class="crayon-line crayon-striped-line"><span class="crayon-c">//add the data about that post to a variable with categories being an array of all the categories for the post</span></div>
<div id="crayon-54b70f9732798210978943-89" class="crayon-line"><span class="crayon-v">$article</span><span class="crayon-sy">[</span><span class="crayon-s">&#8216;title&#8217;</span><span class="crayon-sy">]</span> <span class="crayon-o">=</span><span class="crayon-e">htmlspecialchars</span><span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-t">string</span><span class="crayon-sy">)</span><span class="crayon-v">$item</span><span class="crayon-o">-&gt;</span><span class="crayon-i">title</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-90" class="crayon-line crayon-striped-line"><span class="crayon-v">$article</span><span class="crayon-sy">[</span><span class="crayon-s">&#8216;link&#8217;</span><span class="crayon-sy">]</span> <span class="crayon-o">=</span><span class="crayon-e">htmlspecialchars</span><span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-t">string</span><span class="crayon-sy">)</span><span class="crayon-v">$item</span><span class="crayon-o">-&gt;</span><span class="crayon-i">link</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-91" class="crayon-line"><span class="crayon-c">// get the creator element that is in the dc namespace</span></div>
<div id="crayon-54b70f9732798210978943-92" class="crayon-line crayon-striped-line"><span class="crayon-v">$namespaces</span><span class="crayon-o">=</span><span class="crayon-v">$item</span><span class="crayon-o">-&gt;</span><span class="crayon-e">getNameSpaces</span><span class="crayon-sy">(</span><span class="crayon-t">true</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-93" class="crayon-line"><span class="crayon-v">$dc_namespace</span><span class="crayon-o">=</span><span class="crayon-v">$item</span><span class="crayon-o">-&gt;</span><span class="crayon-e">children</span><span class="crayon-sy">(</span><span class="crayon-v">$namespaces</span><span class="crayon-sy">[</span><span class="crayon-s">&#8216;dc&#8217;</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-94" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-95" class="crayon-line"><span class="crayon-v">$article</span><span class="crayon-sy">[</span><span class="crayon-s">&#8216;author&#8217;</span><span class="crayon-sy">]</span> <span class="crayon-o">=</span><span class="crayon-e">htmlspecialchars</span><span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-t">string</span><span class="crayon-sy">)</span><span class="crayon-v">$dc_namespace</span><span class="crayon-o">-&gt;</span><span class="crayon-i">creator</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-96" class="crayon-line crayon-striped-line"><span class="crayon-st">foreach</span> <span class="crayon-sy">(</span><span class="crayon-v">$item</span><span class="crayon-o">-&gt;</span><span class="crayon-i">categoryas</span><span class="crayon-v">$single_category</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-54b70f9732798210978943-97" class="crayon-line"><span class="crayon-v">$category</span><span class="crayon-sy">[</span><span class="crayon-sy">]</span> <span class="crayon-o">=</span><span class="crayon-e">htmlspecialchars</span><span class="crayon-sy">(</span><span class="crayon-sy">(</span><span class="crayon-t">string</span><span class="crayon-sy">)</span><span class="crayon-v">$single_category</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-98" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-99" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732798210978943-100" class="crayon-line crayon-striped-line"><span class="crayon-v">$article</span><span class="crayon-sy">[</span><span class="crayon-s">&#8216;categories&#8217;</span><span class="crayon-sy">]</span> <span class="crayon-o">=</span><span class="crayon-v">$category</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-101" class="crayon-line"><span class="crayon-v">$article</span><span class="crayon-sy">[</span><span class="crayon-s">&#8216;description&#8217;</span><span class="crayon-sy">]</span> <span class="crayon-o">=</span> <span class="crayon-sy">(</span><span class="crayon-t">string</span><span class="crayon-sy">)</span> <span class="crayon-v">$item</span><span class="crayon-o">-&gt;</span><span class="crayon-i">description</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-102" class="crayon-line crayon-striped-line"><span class="crayon-c">//add the article to the array of articles</span></div>
<div id="crayon-54b70f9732798210978943-103" class="crayon-line"><span class="crayon-v">$allArticles</span><span class="crayon-sy">[</span><span class="crayon-sy">]</span> <span class="crayon-o">=</span><span class="crayon-v">$article</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-104" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-105" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732798210978943-106" class="crayon-line crayon-striped-line"></div>
<div id="crayon-54b70f9732798210978943-107" class="crayon-line"><span class="crayon-k ">return</span><span class="crayon-v">$allArticles</span><span class="crayon-sy">;</span></div>
<div id="crayon-54b70f9732798210978943-108" class="crayon-line crayon-striped-line"><span class="crayon-h">    </span><span class="crayon-sy">}</span></div>
<div id="crayon-54b70f9732798210978943-109" class="crayon-line"></div>
<div id="crayon-54b70f9732798210978943-110" class="crayon-line crayon-striped-line"><span class="crayon-sy">}</span></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>It is important to cast the XML elements as string otherwise they would be SimpleXML objects. Also, in XML elements entitled something like dc:creator mean that the creator element is in the dc namespace and that namespace has to be accessed and the creator element retrieved.</p>
<p>You cannot simply access it with something like:</p>
<div id="crayon-54b70f97327a8407390952" class="crayon-syntax crayon-theme-ssms2012 crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" data-flexie-id="6" data-flexie-parent="true">
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">
<table class="crayon-table">
<tbody>
<tr class="crayon-row">
<td class="crayon-nums " data-settings="show">
<div class="crayon-nums-content">
<div class="crayon-num" data-line="crayon-54b70f97327a8407390952-1">1</div>
</div>
</td>
<td class="crayon-code">
<div class="crayon-pre">
<div id="crayon-54b70f97327a8407390952-1" class="crayon-line"><span class="crayon-v">$item</span><span class="crayon-o">-&gt;</span><span class="crayon-sy">{</span><span class="crayon-s">&#8220;dc:creator&#8221;</span><span class="crayon-sy">}</span><span class="crayon-sy">;</span></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>When we give it a WordPress feed and call the showTabular() method on the class we get a page that resembles something like the graphic below:</p>
<p><img class="aligncenter size-full wp-image-825" src="http://images.phpgang.com/2014/12/Feed-for-Website-phpgang.jpg" alt="Feed for Website phpgang" width="631" height="276" /></p>
<p>&nbsp;</p>
<p>To get the code for this article, please visit: <a href="http://www.phpgang.com/using-php-and-simplexml-to-parse-wordpress-feeds_824.html" target="_blank">http://www.phpgang.com/using-php-and-simplexml-to-parse-wordpress-feeds_824.html</a></p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fusing-php-and-simplexml-to-parse-wordpress-feeds%2F&#038;t=Using%20PHP%20and%20SimpleXML%20to%20parse%20WordPress%20feeds&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fusing-php-and-simplexml-to-parse-wordpress-feeds%2F&#038;p&#091;images&#093;&#091;0&#093;=http%3A%2F%2Fimages.phpgang.com%2F2014%2F12%2FFeed-for-Website-phpgang.jpg&#038;p&#091;title&#093;=Using%20PHP%20and%20SimpleXML%20to%20parse%20WordPress%20feeds" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fusing-php-and-simplexml-to-parse-wordpress-feeds%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fusing-php-and-simplexml-to-parse-wordpress-feeds%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fusing-php-and-simplexml-to-parse-wordpress-feeds%2F&#038;title=Using%20PHP%20and%20SimpleXML%20to%20parse%20WordPress%20feeds" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fusing-php-and-simplexml-to-parse-wordpress-feeds%2F&#038;media=http%3A%2F%2Fimages.phpgang.com%2F2014%2F12%2FFeed-for-Website-phpgang.jpg&#038;description=Using%20PHP%20and%20SimpleXML%20to%20parse%20WordPress%20feeds" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fusing-php-and-simplexml-to-parse-wordpress-feeds%2F&#038;title=Using%20PHP%20and%20SimpleXML%20to%20parse%20WordPress%20feeds" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Using%20PHP%20and%20SimpleXML%20to%20parse%20WordPress%20feeds&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fusing-php-and-simplexml-to-parse-wordpress-feeds%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/using-php-and-simplexml-to-parse-wordpress-feeds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Website Hacking, Part IV</title>
		<link>https://www.dimoff.biz/blog/website-hacking-part-iv/</link>
		<comments>https://www.dimoff.biz/blog/website-hacking-part-iv/#comments</comments>
		<pubDate>Sat, 03 Jan 2015 10:23:23 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Information Security]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dimoff.biz/blog/?p=42</guid>
		<description><![CDATA[In Part IV of the Website Hacking series, we are going to look at: Storing your email address and telephone number in &#60;a href=mailto:*&#62; and &#60;a href=”tel:*&#62; and the inherent&#8230;]]></description>
				<content:encoded><![CDATA[<p>In Part IV of the Website Hacking series, we are going to look at:</p>
<ol>
<li>Storing your email address and telephone number in &lt;a href=mailto:*&gt; and &lt;a href=”tel:*&gt; and the inherent drawbacks of these methods</li>
<li>Shortcomings of disguising email in markup to avoid spam and other malicious requests (disguise such as mail [at] mail [dot] com)</li>
<li>Pitfalls of CDNs (Content Delivery Networks)</li>
<li>A widely used code snippet that is subject to XSS attacks</li>
<li>Relying on the HTML markup for important data for the application (such as product prices)</li>
<li>A loose security mechanism in an Australian governmental website.</li>
</ol>
<h2>Avoid giving sensitive information in a plainly visible way in the HTML markup</h2>
<p>We all know of the &lt;a href=<a href="mailto:sample@sample.com">mailto:sample@sample.com</a>&gt;Mail Me!&lt;/a&gt;. However, giving out your email like this makes it pretty easy for bots to filter out your email and place it in a database/file, whatever they desire, making you subject to spam and other malicious attacks.<br />
To illustrate, I have created a sample script that gets all results for “mailto:” and &lt;a href=”tel: xxx-xxx-xxxx”&gt; from https://meanpath.com/ and stores it in a file or displays it to the browser. This script is just a sample and assumes that all results are on a single page. Furthermore, MeanPath shows only 100 rows from the results unless you pay to get all of them.</p>
<p>Regular expressions are used to filter only those results that contain valid email addresses shown directly in the anchor tag. And for the telephone mining, it just gets the phones that are in the xxx-xxx-xxxx format. The code also ensures that no duplicate emails/telephones are entered into the list of the data.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack1.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack1.png" /></p>
<p><strong>Figure 1: A view of some of the collected emails and telephone numbers.</strong></p>
<p>A better way to mine such data would be through <a href="http://www.nerdydata.com/">http://www.nerdydata.com</a>.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack2.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack2.png" /></p>
<p><strong>Figure 2: First part of the code. It creates a MeanPath class with a function called mine_elements() which gets all results from MeanPath and stores it in an array. The other function filter_elements() filters only the elements that match the query that was given in the instantiation of the MeanPath object and also makes sure there are no duplicate entries.</strong></p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack3.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack3.png" /></p>
<p><strong>Figure 3: Second part of the code. The function display_data() shows the data in a ordered list in the browser. The function save_data_to_file() saves it on a random file, given when calling the function. Lastly, the MeanPath class is instantiated and data saved to a file and displayed in the browser.</strong></p>
<p>Thus, it should be evident by now that giving personal information should involve some safeguards. Of course, this is not always necessary.</p>
<p>I also have to say that “encoding” the email in a format such as “sample [at] sample [dot] com” or “sample [at] sample . com” does not make it any more secure.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack4.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack4.png" /></p>
<p>Here we have a short snippet of code. There is an HTML paragraph with an email given in it in that format and a PHP code that gets the file and extracts the email with a silly regular expression that extracts it and saves it into an array. The regular expression checks for any number of characters followed by [at] or @, followed by any number of characters after which there are some of the top-level domains.</p>
<p>Here is the browser result of the search:</p>
<div>
<div id="highlighter_26819" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php keyword">array</code><code class="php plain">(5) { [0]=&gt; string(44) "</code></div>
<div class="line number2 index1 alt1"><code class="php plain">Contact me at stereo [at] room [dot] com</code><code class="php string">" [1]=&gt; string(25) "</code></div>
<div class="line number3 index2 alt2"><code class="php plain">Contact me at stereo </code><code class="php string">" [2]=&gt; string(4) "</code><code class="php plain">[at]</code><code class="php string">" [3]=&gt; string(12) "</code> <code class="php plain">room [dot] </code><code class="php string">" [4]=&gt; string(3) "</code><code class="php plain">com" }</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>matches[0] is the full expression that matched our search, matches[1] is the first parenthesized part of it that matched, and matches[2] is the second parenthesized part of the regex, and so on.</p>
<h2>Use CDNs but be aware of security implications</h2>
<p>CDNs (Content Delivery Networks) are a great way to decrease page load time (both because they often provide the script in numerous countries and load the one that’s closer to the user) and because users may have already cached the script by visiting another site which uses that particular CDN. However, there are security risks in that you have no control over what is stored in the loaded file. If the CDN gets compromised, the code in the file you are loading may change, and that can lead to more than just cookies being stolen. Also, the script loaded from the CDN can become unavailable, temporarily or not, leading to a frustrating user experience. If the file was on your domain and your site went down, the users would know there is a problem with the site, however if a CDN file such as jQuery gets unavailable and you are relying on it heavily – they would not know what is happening – the site would be up but it would look completely out of whack.</p>
<p>First off, the attacker can change the source code of the delivered script, and:</p>
<ol>
<li>Replace your design with whatever he wants. Also, the attacker can execute JavaScript snippets if the user is using IE or Firefox (considering it is a .css file).</li>
<li>Replace the code to frustrate users, redirect your site to another one, steal users’ cookies and load any kind of exploit code he wants.</li>
<li>
<div>Gain server-side control if the delivered file is a JavaScript file.</div>
</li>
</ol>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack5.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack5.png" /></p>
<p><strong>Figure 4: Executing arbitrary JavaScript within CSS</strong></p>
<p>Expression() works for some modes of IE8 and the ones below IE8 (particularly IE7 and IE5) which are still used nowadays. We see a very simple HTML 4.01 page which sets a cookie on each visit to the page without checking if it exists when the page loads. After that we have a &lt;style&gt; tag in which we pick a selector and a property and add expression(code here) to it where arbitrary JavaScript code can go inside. For example, you are loading a .css file via CDN (let’s say a CSS reset) this could be a potential security risk.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack6.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack6.png" /></p>
<p><strong>Figure 5: Here is how the page looks in Internet Explorer 7.0 on Windows XP (Emulated from browserstack).</strong></p>
<p>As we can see, the expression ‘do’ gets evaluated and the cookie shown. We can do much more than this, but we will leave that to your imagination.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack7.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack7.png" /></p>
<p><strong>Figure 6: The same page in a contemporary browser (latest version of Chrome)</strong></p>
<p>Another thing that can be done with control over the CSS is the following:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack8.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack8.png" /></p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack9.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack9.png" /><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack10.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack10.png" /></p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack11.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack11.png" /></p>
<p><strong>Figure 8: Using a CSS CDN – possibilities</strong></p>
<p>Now, not only does a probable CDN have the ability to remove all of your content from being displayed to users, but it can add custom text to it, possibly acting as a fake redirect message and scaring/confusing your users enough to make them never come back to your site.</p>
<p>Only two CSS selectors are necessary to make such a change to your page, and the code does not assume any level of knowledge of the HTML markup in the page:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack12.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack12.png" /></p>
<p>As for using CDNs for JavaScript files, I hope you do not think that it is only possible to steal the JavaScript cookies and mess around with the DOM when the external file is compromised. It takes 2-3 lines of code to load any server-side file via AJAX.</p>
<p>To simulate a JavaScript file loaded from a CDN, we added a local .js file to the recipes page I’ve shown earlier.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack13.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack13.png" /></p>
<p>Then in the file we executed the following:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack14.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack14.png" /></p>
<p>And in the PHP script we had this:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack15.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack15.png" /></p>
<p>This worked as the picture below shows:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack16.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack16.png" /></p>
<p>Now the attacker has much more power over both the server and the website.</p>
<p>You can see one of the previous <a title="articles" href="http://resources.infosecinstitute.com/articles/">articles</a> of the series that concerned PHP Injection and learn some of the things that can be achieved from now on.</p>
<h2>XSS is everywhere</h2>
<p>Another thing to be on the lookout for is XSS. It comes in many forms, but here we will give one example that is sometimes unknown for developers. In PHP $_SERVER[&#8216;PHP_SELF&#8217;] is vulnerable to cross-site scripting. Often, it is used as part of a &lt;form&gt; action attribute. It should always be escaped with something like &lt;form method=”GET” action=”&lt;?php echo htmlspecialchars($_SERVER[&#8220;PHP_SELF&#8221;]); ?&gt;”&gt;.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack17.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack17.png" /></p>
<p><strong>Figure 9: An example of a page vulnerable to XSS. Do not pay attention to the poor nesting.</strong></p>
<p>What the &lt;form&gt; action does is just echo the URL that the user is currently in. This means that he can easily manipulate the URL to include code and the browser will execute it. He can also give links with manipulated URLs of the site to third-users and get something from them. All he has to do is close the action attribute and close the &lt;form&gt; tag. Thus, he has to type “&gt; and insert HTML code after that in the address bar.</p>
<p>&nbsp;</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack18.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack18.png" /></p>
<p><strong>Figure 10: XSS example</strong></p>
<p>Above we see a JavaScript snippet executed in the address bar which alerts “Hi” (localhost:8079/4/php_self.php/”&gt; &lt;script&gt; alert(“Hi”);&lt;/script&gt;). Google Chrome would break requests like this, but Firefox does not do it that well.</p>
<h2>Setting important data for an application in the markup</h2>
<p>To illustrate, I have taken some screenshots from a search in nerdydata.com for the keyword data-price=”</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack19.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack19.png" /></p>
<p><strong>Figure 11: data-price=”$269.99″. It could be generated dynamically just to show the price with JavaScript.</strong></p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack20.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack20.png" /></p>
<p><strong>Figure 12: data-price”75″ and a data-tid=”51127″ (the ID of the product is also set in the HTML markup)</strong></p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack21.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack21.png" /></p>
<p><strong>Figure 13: close to 14,000 websites with price set in the HTML markup. At least a couple of them will be vulnerable to user manipulation of the price.</strong></p>
<p>I do not think that I am wrong by saying that at least a couple of sites from those 14,000 will not have proper mechanism set up on the server side to check for the product’s price by checking the product ID or name and getting the prices from a database.</p>
<p>I also want to show this way of storing the price:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack22.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack22.png" /></p>
<p><strong>Figure 14: &lt;input type=”hidden” name=”price” value=”59″&gt;, &lt;input type=”hidden” name=”item_id” value=”…”&gt;</strong></p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack23.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack23.png" /></p>
<p><strong>Figure 15: 7087 websites with hidden input containing the price of the product.</strong></p>
<h2>Mo’ Captcha, Please</h2>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack24.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack24.png" /></p>
<p><strong>Figure 16: Site of Australian government…</strong></p>
<p>In the picture we see a page from the site of the Australian Customs and Border Protection Service which does not have any of the fancy CAPTCHAs (I agree that they may prevent also legitimate users from using the site, but it is more positive than negative).</p>
<p>The security mechanism in the abovementioned site seems static; the bold numbers do not change. In case they changed:</p>
<p>I have created a snippet which fetches the numbers, considering they weren’t static, and this was their security verification method – showing and making you type different numbers in bold text next to other characters, all of which are fully visible in the HTML markup.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack25.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack25.png" /></p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack26.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/102414_1134_WebsiteHack26.png" /></p>
<p><strong>Figure 17: This snippet fetches the page from their site and displays it in an unseen div so I can traverse the data with jQuery. The jQuery code then loops for each &lt;strong&gt; tag and adds its text to a variable, separating different strong tags by whitespace on both sides. Then a regex is called, which matches only numbers followed by whitespace on both sides and converts the resulting array to a string. Finally, all whitespace is removed from the mined numbers and their values are shown in the console and in an alert.</strong></p>
<h2>Conclusion</h2>
<p>I hope those tips have helped you, whether a lot or a little, to see common mistakes and possibilities for mistakes in web applications. If you want to have all the source code, you can download all the files with PHP, HTML, CSS, JavaScript and JQuery code used in the article from InfoSec Institute: <a href="http://resources.infosecinstitute.com/website-hacking-part-iv-tips-better-website-security/" target="_blank">http://resources.infosecinstitute.com/website-hacking-part-iv-tips-better-website-security/</a></p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-iv%2F&#038;t=Website%20Hacking%2C%20Part%20IV&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-iv%2F&#038;p&#091;images&#093;&#091;0&#093;=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F102414_1134_WebsiteHack1.png&#038;p&#091;title&#093;=Website%20Hacking%2C%20Part%20IV" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-iv%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-iv%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-iv%2F&#038;title=Website%20Hacking%2C%20Part%20IV" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-iv%2F&#038;media=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F102414_1134_WebsiteHack1.png&#038;description=Website%20Hacking%2C%20Part%20IV" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-iv%2F&#038;title=Website%20Hacking%2C%20Part%20IV" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Website%20Hacking%2C%20Part%20IV&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-iv%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/website-hacking-part-iv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Website Hacking, Part V</title>
		<link>https://www.dimoff.biz/blog/website-hacking-part-v/</link>
		<comments>https://www.dimoff.biz/blog/website-hacking-part-v/#comments</comments>
		<pubDate>Sat, 03 Jan 2015 10:07:35 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dimoff.biz/blog/?p=38</guid>
		<description><![CDATA[Introduction In this part of the Website Hacking series we are going to take a look at how to minimize damages from XSS attacks considering our web application can at&#8230;]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>In this part of the Website Hacking series we are going to take a look at how to minimize damages from XSS attacks considering our web application can at some point become vulnerable to this type of attacks (HttpOnly cookies are going to be discussed). We are going to look at a mechanism for bypassing the security that HttpOnly cookies provide known as <strong>Cross Site Tracing attack</strong>. We are also going to give a brief example of packet sniffing, followed by a more lengthy discussion on session hijacking and session fixation.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>Minimizing damages from XSS (HTTPOnly Cookies)</h2>
<p>If your site at some point becomes vulnerable to XSS, you probably know that your cookies would be exposed. The attacker can simply save document.cookie’s value somewhere and use it for malicious purposes – for example – he can try to steal your session by looking for a session identifier in your cookies. As you know, the main difference between cookies and sessions is that cookies are stored locally on the user’s machine, and sessions are stored in the server (usually the tmp folder, which has to be ensured is out of the public folder and out of reach for regular users). However, the way sessions work is that they save an ID which links the user with the particular session stored on the server, and this session ID (known as magic cookies or session keys) can be stored and passed to the server in a cookie.</p>
<p>Here is one way to make sure that if somebody could get the document.cookie from your users, he does not get all the cookies you have stored for that user.</p>
<p>Read the following setcookie description below (as taken from php.net)</p>
<div>
<div id="highlighter_601995" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php plain">bool setcookie ( string </code><code class="php variable">$name</code> <code class="php plain">[, string </code><code class="php variable">$value</code> <code class="php plain">[, int </code><code class="php variable">$expire</code> <code class="php plain">= 0 [, string </code><code class="php variable">$path</code> <code class="php plain">[, string </code><code class="php variable">$domain</code> <code class="php plain">[, bool </code><code class="php variable">$secure</code> <code class="php plain">= false [, bool </code><code class="php variable">$httponly</code> <code class="php plain">= false ]]]]]] )</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>A way to ensure that if an unauthorized person can execute JavaScript on clients he would not get the cookie is to set the last parameter ($httponly) to true. This will disallow JavaScript from reading the cookie that you are setting.</p>
<p>A sample cookie would look something like that:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack1.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack1.png" /></p>
<div>
<div id="highlighter_206126" class="syntaxhighlighter  plain">
<h2></h2>
<div class="highlight">
<h2></h2>
<div class="highlight">
<pre>$username = "Cartman";
$cookie /* true/false */ = setcookie("user2", $username, 
	0 /*expire at the end of the session */,  
	"/"  /* available within the whole domain */,
	 "", false, true /*HTTPonly; no JS access */);
</pre>
</div>
</div>
</div>
</div>
<p>Then you can easily access it with PHP with something like that:</p>
<div>
<div id="highlighter_520458" class="syntaxhighlighter  plain">if (isset($_COOKIE[&#8216;user2&#8242;])) {<br />
echo &#8220;OKAY&#8221;;<br />
}</div>
</div>
<p>Even better, there is a way to make sure all your cookies are HTTPOnly:</p>
<p>You can set in your php.ini file session.cookie_httponly to 1 or force HTTPOnly cookies only for particular pages/scripts on your website by adding:</p>
<div>
<div id="highlighter_181431" class="syntaxhighlighter  plain">
<h2></h2>
<div class="highlight">
<pre>// Make session cookies httponly
ini_set('session.cookie_httponly',1); 
</pre>
</div>
</div>
</div>
<p>in the page’s header.</p>
<p>Another way to add an HTTPOnly cookie (a more low-level one) is to set the header yourself:</p>
<div>
<div id="highlighter_360732" class="syntaxhighlighter  plain">
<h2></h2>
<div class="highlight">
<pre>// A more low-level way of setting a httponly cookie
header("Set-Cookie: user2=Kenny; httpOnly");
</pre>
</div>
</div>
</div>
<h2>HTTPOnly Cookies circumvention and prevention (XST)</h2>
<p>We have seen that HTTPOnly cookies disallow access to the cookies from JavaScript. However, a technique was crafted to circumvent this protection. It is known as XST or Cross Site Tracing Attack.</p>
<p>The attacker could target old browsers and type something similar to:</p>
<div>
<div id="highlighter_446472" class="syntaxhighlighter  plain">
<p>&lt;script&gt;</p>
<p>//Setting an AJAX TRACE request method to get cookies that cannot be seen with JavaScript<br />
try {<br />
var xhr = new XMLHttpRequest();</p>
<p>}</p>
<p>catch (e) {<br />
var xhr = new XDomainRequest();<br />
}</p>
<p>xhr.open(&#8216;trace&#8217;, &#8220;http://localhost:8079/WebsiteHacking5/code.php&#8221;, false);<br />
xhr.send();<br />
var traceData = xhr.responseText;<br />
&lt;/script&gt;</p>
<p><span style="line-height: 1.5;">After which the variable traceData would contain the cookies he desires as well as other data about the request. However, in contemporary browsers the TRACE request method is disabled for AJAX requests by default (throws a security error).</span></p>
</div>
</div>
<p>Similar AJAX XST attempts should not work on clients having Firefox 19.0.2+ or Chrome 25.0.1364.172+</p>
<p>Thus, it may still be useful to block Trace requests.</p>
<p>To disable it natively, Apache’s httpd.conf must be opened and the following line added to it:</p>
<p>TraceEnable off</p>
<p>This would work on versions of Apache &gt; 1.3.34 or 2.0.55+ for apache2.</p>
<p>Another way to disable TRACE methods without changing the htpd.conf file is changing the .htaccess file and adding:</p>
<div>
<div id="highlighter_207684" class="syntaxhighlighter  plain">RewriteEngine On<br />
RewriteCond %{REQUEST_METHOD} ^TRACE<br />
RewriteRule .* &#8211; [F]</div>
</div>
<p>Note: mod_rewrite has to be enabled for the snippet to work.</p>
<h2>Packet sniffing</h2>
<p>There are many sites out there whose client credentials are vulnerable when it comes to man-in-the-middle attacks. When connected to a public Wi-Fi, there could easily be someone at that restaurant sniffing the incoming traffic. Here is a sample using <a title="Wireshark" href="http://resources.infosecinstitute.com/wireshark/">Wireshark</a>:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack2.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack2.png" /></p>
<p>Firstly, we have applied an HTTP filter to only see HTTP requests.</p>
<p>Then we use Ctrl+F, search for a String and enter “POST” for value to only see the POST requests (in most cases, those contain user-entered data and frequently interesting ones).</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack3.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack3.png" /></p>
<p>Here we see a sample of unencrypted client/server communication after a registration form has been submitted.</p>
<p>A fix for that is using HTTPS (TLS/SSL).</p>
<h2>Session hijacking/Session fixation</h2>
<p>Assuming another vulnerability is present (such as XSS), an attacker can see users’ cookies, then he can see which one is the session ID (by default in Apache servers the session id is carried in a cookie called PHPSESSID and hijack an active session. Below we have provided an explanation of how that could happen. Assume we have this login script and login form on the same file (in a real application the accounts would be populated from a database).</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack4.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack4.png" /></p>
<p>We can see that in the upper left corner there is a paragraph with the text “User appears NOT logged in” before the user has been logged in.</p>
<p>Note: we have enhanced the form visually a bit with Bootstrap 3.</p>
<p>The code is as follows:</p>
<div>
<div id="highlighter_917126" class="syntaxhighlighter  plain">
<h2></h2>
<div class="highlight">
<pre>session_start();
if (isset($_SESSION['account'])) {
	echo "&lt;p&gt;User appears logged in.&lt;/p&gt;";
}
else {
	echo "&lt;p&gt;User appears NOT logged in.&lt;/p&gt;";
}



?&gt;

&lt;!DOCTYPE html&gt;



&lt;html lang="en"&gt;
&lt;head&gt;
	&lt;link rel="stylesheet" href="css/bootstrap.min.css"&gt;
	&lt;meta charset="UTF-8"&gt;
	&lt;title&gt;Login&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div class="container"&gt;
		&lt;div class="row"&gt;
			&lt;div class="col-lg-6 col-lg-offset-3"&gt;	
				&lt;form class="form form-vertical" action="&lt;?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?&gt;" method="POST"&gt;
				&lt;div class="input-group"&gt;
					&lt;span class="input-group-addon glyphicon glyphicon-user"&gt;&lt;/span&gt;
					&lt;label for="username"&gt;Username:&lt;/label&gt;
					&lt;input name="username" id="username" class="form-control input-lg" type="text"&gt;
				&lt;/div&gt;
				&lt;div class="input-group"&gt;
				&lt;span class="input-group-addon glyphicon glyphicon-lock"&gt;&lt;/span&gt;
				&lt;label for="password"&gt; Password &lt;/label&gt;
				&lt;input type="password" class="form-control input-lg" name="password" id="password"&gt;			
				&lt;/div&gt;
				&lt;div class="submit-container text-center"&gt;	
						&lt;input type="submit" style="margin-top: 15px;" class="btn btn-lg btn-success" value="Log in"&gt;
				&lt;/div&gt;
				
				&lt;/form&gt;

			&lt;/div&gt;
		&lt;/div&gt;

&lt;?php


$accounts = array("Cartman" =&gt; "overweight", 
	"Kenny" =&gt; "diesoften",
	 "Kyle" =&gt; "jew", 
	 "Chef" =&gt; "ladiesguy");


if ($_SERVER["REQUEST_METHOD"] === "POST") {
	$username = $_POST['username'];
	$pass = $_POST['password'];
	foreach ($accounts as $account =&gt; $accountPass) {
		if ($account == $username) {
			if ($accountPass == $pass) {
				echo "&lt;h1 class='text-center'&gt;Logged in as $account!. Redirecting... &lt;/h1&gt;";
				$_SESSION["account"] = $account;
				sleep(1);
				header("Location: memberzone.php");
			}
				
			}

		}

		 if (!isset($_SESSION["account"])) {
		 	echo "&lt;h1 class='text-center'&gt;Wrong credentials.&lt;/h1&gt;";
				
	}
		 }
?&gt;


		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
</div>
</div>
</div>
<p>We do not implement an actual attack but check our session ID cookie:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack5.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack5.png" /></p>
<p>Then we open the page in a browser that does not have any active sessions, start Tamper Data and submit the form with wrong credentials, but use the session ID of the actual logged in user.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack6.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack6.png" /></p>
<p>The result is: we have hijacked the session of a legitimate user and appear logged in. (We do not get redirected, but as you can see we are logged in and there’s no wrong credentials statement appearing).</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack7.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack7.png" /></p>
<p>Actually, as you can see we are not redirected to the memberzone, so the code inside the <strong>if</strong> ($accountPass<br />
==<br />
$pass) conditional does not execute, but if we have code executed in another block, which depends on the user being logged in or similar, then the attacker would have been able to run the code inside it and do whatever he wants with the user’s account.</p>
<p>The first and more ineffective way of protecting against such attacks is to set session.referer_check = yourDomainHere in php.ini or use ini_set to set it for a particular script. In that way, the session would be set only if the set domain/path is the referrer (equivalent to $_SERVER[&#8216;HTTP_REFERER&#8217;] regular expression search in a sense and not a very effective method). Plus, it does not accept multiple parameters. Multiple parameters are supported in some frameworks such as CakePHP.</p>
<p>What you should do is protect yourself against XSS attacks, as these can leak the session ID and lead to session hijacking. You should also use httpOnly cookies to revoke JavaScript access to your cookies, possibly minimizing damages from XSS and not leaking users’ cookies.</p>
<p>Another thing you can do is to check if the IP address of the person who first logged in and the IP address of the person using the session are the same (they are expected to be the same).</p>
<p>Firstly, we make the following changes (check if there was a session hijacking attempt, say to the user that he has been reported and send an email):</p>
<div>
<div id="highlighter_486733" class="syntaxhighlighter  plain">if (!isset($_SESSION[&#8220;account&#8221;])) {<br />
echo &#8220;&lt;h1 class=&#8217;text-center&#8217;&gt;Wrong credentials.&lt;/h1&gt;&#8221;;}<br />
else {if ($_SESSION[&#8216;IP&#8217;] !== $_SERVER[&#8220;REMOTE_ADDR&#8221;]) {<br />
echo &#8220;&lt;h1&gt; Security breach attempt. Contacting the administrator&#8221;;<br />
mail(&#8220;test@mail.bg&#8221;, &#8220;Security Breach&#8221;, $_SERVER[&#8220;REMOTE_ADDR&#8221;] . &#8221; attempted to hijack the session of a user!\r\n&#8221;);</p>
<p>}</p>
<p>}</p>
</div>
</div>
<p>In our loop that checks whether some of the credentials match, we have to also log the IP address of the person at the moment of logging him in.</p>
<div>
<div id="highlighter_178968" class="syntaxhighlighter  plain">
<h2></h2>
<div class="highlight">
<pre>$username = $_POST['username'];
	$pass = $_POST['password'];
	foreach ($accounts as $account =&gt; $accountPass) {
		if ($account == $username) {
			if ($accountPass == $pass) {
				echo "&lt;h1 class='text-center'&gt;Logged in as $account!&lt;/h1&gt;";
				$_SESSION["account"] = $account;
				$_SESSION['IP'] = $_SERVER["REMOTE_ADDR"];
	
			    }
				
			}

		}
</pre>
</div>
</div>
</div>
<p>And we modify the section when we show content only for logged in users to something like:</p>
<div>
<div id="highlighter_181863" class="syntaxhighlighter  plain">
<h2></h2>
<div class="highlight">
<pre>session_start();
if (isset($_SESSION['account']) &amp;&amp; $_SESSION['IP'] === $_SERVER["REMOTE_ADDR"]) {
	
	// Code to be executed for logged in users can be placed here
	echo "&lt;p&gt;User appears logged in.&lt;/p&gt;";
}
else {
	echo "&lt;p&gt;User appears NOT logged in.&lt;/p&gt;";
}
</pre>
</div>
</div>
</div>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack8.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack8.png" /></p>
<p>We have logged in with Chrome as Kenny.</p>
<p>We use a different IP address and try to steal the session with the session ID cookie credentials using Tamper Data.</p>
<p>We get the following page:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack9.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack9.png" /></p>
<p>User appears NOT logged in (we have not fooled the script) and a notification that the breach was found is displayed on the screen. And if we look at our email client, we will get a message resembling this but with the full IP address of the attacker written in the email’s contents:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack10.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/121014_1417_WebsiteHack10.png" /></p>
<p>Session fixation is different from session hijacking because in session hijacking the attacker could intercept or hijack the session’s cookie, but in session fixation the attacker supplies the user with a session ID that is already set, and when that user logs in the server would assign his data into the session ID that is set by the attacker, which would grant him access to the user’s session.</p>
<p>To prevent session fixation, one way is to check the referrer (we have mentioned that before). Another way is to reduce the duration for which sessions are active from another php.ini setting (session.cache_expire).</p>
<p>However, what we recommend is that you regenerate session IDs frequently.</p>
<p>Assigning a new session ID will ensure that the ID that a possible attacker has obtained would not be useful, because a new session ID would have been assigned to the user.</p>
<p>We can achieve that with the following snippet:</p>
<div></div>
<div>
<div id="highlighter_72578" class="syntaxhighlighter  plain">
<h2></h2>
<div class="highlight">
<pre>session_start();
if (!isset($_SESSION['check'])) {
 	session_destroy();
 	session_regenerate_id();
	$_SESSION['check'] = 1;
} 
</pre>
</div>
</div>
</div>
<h2>Conclusion</h2>
<p>In this article, we have examined some common exploits out there, and we hope that you will start implementing some of these security mechanisms in your future projects, if you are not implementing them already.</p>
<p>Sources:</p>
<ol>
<li>MDLog:/sysadmin, “Apache Tips: Disable the HTTP TRACE Method”, Accessed 3/12/2014, Available at:<a href="http://www.ducea.com/2007/10/22/apache-tips-disable-the-http-trace-method/">http://www.ducea.com/2007/10/22/apache-tips-disable-the-http-trace-method/</a></li>
<li>OWASP, “Cross Site Tracing”, Accessed 4/12/2014, Available at:<a href="https://www.owasp.org/index.php/Cross_Site_Tracing">https://www.owasp.org/index.php/Cross_Site_Tracing</a></li>
<li>
<div>Stack Overflow, ‘What is PHP’s session.referer_check protecting me from?’, Accessed 5/12/2014, Available at:<a href="http://stackoverflow.com/questions/4863946/what-is-phps-session-referer-check-protecting-me-from/">http://stackoverflow.com/questions/4863946/what-is-phps-session-referer-check-protecting-me-from/</a></div>
</li>
</ol>
<p><strong>This article was originally published on InfoSec Institute</strong></p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-v%2F&#038;t=Website%20Hacking%2C%20Part%20V&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-v%2F&#038;p&#091;images&#093;&#091;0&#093;=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F121014_1417_WebsiteHack1.png&#038;p&#091;title&#093;=Website%20Hacking%2C%20Part%20V" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-v%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-v%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-v%2F&#038;title=Website%20Hacking%2C%20Part%20V" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-v%2F&#038;media=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F121014_1417_WebsiteHack1.png&#038;description=Website%20Hacking%2C%20Part%20V" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-v%2F&#038;title=Website%20Hacking%2C%20Part%20V" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Website%20Hacking%2C%20Part%20V&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-v%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/website-hacking-part-v/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Website Hacking, Part II</title>
		<link>https://www.dimoff.biz/blog/website-hacking-part-ii/</link>
		<comments>https://www.dimoff.biz/blog/website-hacking-part-ii/#comments</comments>
		<pubDate>Fri, 26 Dec 2014 19:00:49 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Information Security]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://localhost:8079/current/Dimoff1/blog/?p=26</guid>
		<description><![CDATA[Introduction In this Part, we are going to briefly introduce Path Traversal, usage of Delimiters, and Information Disclosure attack. We are going to present simple solutions to simplified problems involving&#8230;]]></description>
				<content:encoded><![CDATA[<h1>Introduction</h1>
<p>In this Part, we are going to briefly introduce Path Traversal, usage of Delimiters, and Information Disclosure attack.</p>
<p>We are going to present simple solutions to simplified problems involving the attacks.</p>
<h1>Content</h1>
<h2>Exercise 8: Path Traversal</h2>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack1.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack1.png" /></p>
<p><strong>Figure : A simple webpage in which you choose an article and view it</strong></p>
<p>The website (index.php) in the PathTraversal folder contains a simple form which submits to the same page through the GET request method. Once a choice of article has been made and “View article” has been clicked, the following PHP code executes:</p>
<div>
<div id="highlighter_615086" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php plain">&lt;?php</code></div>
<div class="line number2 index1 alt1"><code class="php comments">//If the article GET parameter is set</code></div>
<div class="line number3 index2 alt2"><code class="php keyword">if</code> <code class="php plain">(isset(</code><code class="php variable">$_GET</code><code class="php plain">[</code><code class="php string">"article"</code><code class="php plain">])) {</code></div>
<div class="line number4 index3 alt1"><code class="php comments">// Create a div block and fill it with the contents from the file in the GET value.</code></div>
<div class="line number5 index4 alt2"><code class="php spaces">    </code><code class="php functions">echo</code> <code class="php string">"&lt;div id='article'&gt;"</code> <code class="php plain">. </code><code class="php functions">file_get_contents</code><code class="php plain">(</code><code class="php variable">$_GET</code><code class="php plain">[</code><code class="php string">"article"</code><code class="php plain">]) . </code><code class="php string">"&lt;/div&gt;"</code><code class="php plain">;</code></div>
<div class="line number6 index5 alt1"><code class="php plain">}</code></div>
<div class="line number7 index6 alt2"><code class="php plain">?&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The result is the following URL: http://localhost/2/PathTraversal/?article=1.htm</p>
<p>It loads the relevant article file placed in the GET method. The parameter article is formed via:</p>
<div>
<div id="highlighter_474492" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;</code><code class="xml keyword">select</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"article"</code> <code class="xml color1">required</code><code class="xml plain">=</code><code class="xml string">""</code><code class="xml plain">&gt;&lt;/</code><code class="xml keyword">select</code><code class="xml plain">&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>And the values are also directly given through the HTML code (the value attribute):</p>
<p>Domain Slamming</p>
<p>Now, legitimate users will use the interface provided in the website to browse it, but with the code as it is we can easily open myriad files they do not want you to open by directly tampering with the URL parameters. Many websites have config directories where they store important data – let’s see if you can do it.</p>
<h3>Tasks</h3>
<ol>
<li>Go back one directory and open openme.txt by changing the URL parameters.</li>
<li>
<div>We assume that we cannot open the folder config from our computer but only from the local server. Assume you do not know what files there are in the directory. First, you should check whether the directory exists.</div>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack2.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack2.png" /></li>
</ol>
<p>The directory exists and now we know that there is HTTPAuth in place. Your task is to somehow find out the username and the hashed password for the folder without using any brute-force or dictionary attacks on the username and password.</p>
<h3>Spoiler (Task 2)</h3>
<p>If we know that there is a HTTPAuth security mechanism in place, then we can automatically deduce there is an .htaccess file. Therefore, we can open the .htaccess file that we would not be able to open normally via the path traversal vulnerability of the article viewer page.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack3.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack3.png" /></p>
<p><strong>Figure: Viewing the .htaccess file from the article viewer page</strong></p>
<p>We type <a href="http://localhost/2/PathTraversal/?article=config/.htaccess">http://localhost/2/PathTraversal/?article=config/.htaccess</a> and now we know the path and the file in which accounts and passwords are stored as well as the user that is required to view the folder.</p>
<p>We type the path to the userlist.htpasswd file and get all usernames and passwords:</p>
<p>tomburrows:$apr1$ZF.78h2N$zhAaP2AY6VwxuELizJAwg.</p>
<p>Now, the username is known and we have incredibly reduced our cracking time. HTTPAuth is using UNIX’s “CRYPT” function to encrypt the passwords which is a “one way” encryption method.</p>
<p>Using path traversal, we can also go back several directories and browse to the <em>php.ini</em> and other important configuration files as well.</p>
<h3>A sample solution to our path traversal vulnerability</h3>
<div>
<div id="highlighter_478016" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
<div class="line number8 index7 alt1">8</div>
<div class="line number9 index8 alt2">9</div>
<div class="line number10 index9 alt1">10</div>
<div class="line number11 index10 alt2">11</div>
<div class="line number12 index11 alt1">12</div>
<div class="line number13 index12 alt2">13</div>
<div class="line number14 index13 alt1">14</div>
<div class="line number15 index14 alt2">15</div>
<div class="line number16 index15 alt1">16</div>
<div class="line number17 index16 alt2">17</div>
<div class="line number18 index17 alt1">18</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php plain">&lt;?php</code></div>
<div class="line number2 index1 alt1"><code class="php comments">//If the article GET parameter is set</code></div>
<div class="line number3 index2 alt2"></div>
<div class="line number4 index3 alt1"><code class="php keyword">if</code> <code class="php plain">(isset(</code><code class="php variable">$_GET</code><code class="php plain">[</code><code class="php string">"article"</code><code class="php plain">])) {</code></div>
<div class="line number5 index4 alt2"><code class="php comments">//Remove any “/” and “.” characters from the GET parameter’s value as this can be used for path traversal </code></div>
<div class="line number6 index5 alt1"><code class="php spaces">    </code><code class="php variable">$article</code> <code class="php plain">= </code><code class="php functions">str_replace</code><code class="php plain">(</code><code class="php keyword">array</code><code class="php plain">(</code><code class="php string">"/"</code><code class="php plain">, </code><code class="php string">"."</code><code class="php plain">), </code><code class="php string">""</code><code class="php plain">, </code><code class="php variable">$_GET</code><code class="php plain">[</code><code class="php string">"article"</code><code class="php plain">]);</code></div>
<div class="line number7 index6 alt2"><code class="php comments">// If the file does not exist, print a custom error.</code></div>
<div class="line number8 index7 alt1"><code class="php spaces">    </code><code class="php keyword">if</code> <code class="php plain">(!</code><code class="php functions">file_exists</code><code class="php plain">(</code><code class="php variable">$article</code> <code class="php plain">. </code><code class="php string">".htm"</code><code class="php plain">)) {</code></div>
<div class="line number9 index8 alt2"><code class="php spaces">    </code><code class="php functions">echo</code> <code class="php string">"&lt;h1&gt;The article does not exist!&lt;/h1&gt;"</code><code class="php plain">;</code></div>
<div class="line number10 index9 alt1"><code class="php spaces">    </code><code class="php plain">}</code></div>
<div class="line number11 index10 alt2"><code class="php spaces">    </code><code class="php keyword">else</code> <code class="php plain">{</code></div>
<div class="line number12 index11 alt1"><code class="php comments">//If and only if the file exists – echo out its contents</code></div>
<div class="line number13 index12 alt2"></div>
<div class="line number14 index13 alt1"><code class="php comments">// Create a div block and fill it with the contents from the file in the GET value.</code></div>
<div class="line number15 index14 alt2"><code class="php comments">//Add a mandatory file extension of .htm to the file</code></div>
<div class="line number16 index15 alt1"><code class="php spaces">    </code><code class="php functions">echo</code> <code class="php string">"&lt;div id='article'&gt;"</code> <code class="php plain">. </code><code class="php functions">file_get_contents</code><code class="php plain">(</code><code class="php variable">$article</code> <code class="php plain">. </code><code class="php string">".htm"</code><code class="php plain">) . </code><code class="php string">"&lt;/div&gt;"</code><code class="php plain">;</code></div>
<div class="line number17 index16 alt2"><code class="php spaces">    </code><code class="php plain">}</code></div>
<div class="line number18 index17 alt1"><code class="php plain">}</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The change in the HTML code is that we no longer use the full file name value in the options tags, we just use the name of the file (without its extension so only .htm files would be allowed)</p>
<div>
<div id="highlighter_847127" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;</code><code class="xml keyword">a</code> <code class="xml color1">title</code><code class="xml plain">=</code><code class="xml string">"Keyloggers: How They Work and More"</code> <code class="xml color1">href</code><code class="xml plain">=</code><code class="xml string">"<a href="http://resources.infosecinstitute.com/keyloggers-how-they-work-and-more/">http://resources.infosecinstitute.com/keyloggers-how-they-work-and-more/</a>"</code><code class="xml plain">&gt;Keyloggers: How They Work and More&lt;/</code><code class="xml keyword">a</code><code class="xml plain">&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Firstly, checking if the file exists and echoing it out only if it exists prevents another attack – that of information disclosure.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack4.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack4.png" /></p>
<p>There is a PHP warning thrown out if we type a non-existent file deliberately. Of course, another way to resolve such information disclosure issues is by turning off the display_errors In the php.ini file (this is most desirable if the site is live anyway).</p>
<p>With the above mentioned code we get a clean and neat error that the article does not exist, along with prevention of any path traversal attempts.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack5.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack5.png" /></p>
<p>&nbsp;</p>
<p><strong>Figure: We now receive an error when we try to go back one directory and open the openme.txt file</strong></p>
<p>Note: in old editions of PHP (older than 5.5.3) you could use the %00 marker to end the string abruptly and pass your own file extension in place of the “.htm” one in our solution code.</p>
<p>if (!file_exists($article . “.htm”)) could be exploited in older versions of PHP by typing:</p>
<p><a href="http://localhost/2/PathTraversal/?article=accounts.txt%20%00">http://localhost/2/PathTraversal/?article=accounts.txt %00</a></p>
<p>Which is equivalent to:</p>
<p>“accounts.txt.htm” forcing the server to ignore the .htm part of the string.</p>
<h2>Exercise 9: Information disclosure</h2>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack6.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack6.png" /></p>
<p><strong>Figure: Comment page</strong></p>
<p>For this exercise, I have created a working but problematic comments page which looks similar to a chat. You have to write a comment, and then you view all the comments up to now. The comments are stored in a .txt file rather than in a database and there is one PHP file that creates new comments and one that displays them on the screen.</p>
<div>
<div id="highlighter_597094" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
<div class="line number8 index7 alt1">8</div>
<div class="line number9 index8 alt2">9</div>
<div class="line number10 index9 alt1">10</div>
<div class="line number11 index10 alt2">11</div>
<div class="line number12 index11 alt1">12</div>
<div class="line number13 index12 alt2">13</div>
<div class="line number14 index13 alt1">14</div>
<div class="line number15 index14 alt2">15</div>
<div class="line number16 index15 alt1">16</div>
<div class="line number17 index16 alt2">17</div>
<div class="line number18 index17 alt1">18</div>
<div class="line number19 index18 alt2">19</div>
<div class="line number20 index19 alt1">20</div>
<div class="line number21 index20 alt2">21</div>
<div class="line number22 index21 alt1">22</div>
<div class="line number23 index22 alt2">23</div>
<div class="line number24 index23 alt1">24</div>
<div class="line number25 index24 alt2">25</div>
<div class="line number26 index25 alt1">26</div>
<div class="line number27 index26 alt2">27</div>
<div class="line number28 index27 alt1">28</div>
<div class="line number29 index28 alt2">29</div>
<div class="line number30 index29 alt1">30</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php comments">//Index.php server-side code</code></div>
<div class="line number2 index1 alt1"></div>
<div class="line number3 index2 alt2"><code class="php spaces">    </code><code class="php plain">&lt;?php</code></div>
<div class="line number4 index3 alt1"><code class="php spaces">        </code><code class="php variable">$path</code> <code class="php plain">= </code><code class="php string">"comments/"</code><code class="php plain">;</code></div>
<div class="line number5 index4 alt2"><code class="php spaces">        </code><code class="php plain">?&gt;</code></div>
<div class="line number6 index5 alt1"></div>
<div class="line number7 index6 alt2"><code class="php spaces">        </code><code class="php plain">&lt;?php </code></div>
<div class="line number8 index7 alt1"><code class="php spaces">            </code><code class="php keyword">if</code> <code class="php plain">(</code><code class="php variable">$_SERVER</code><code class="php plain">[</code><code class="php string">"REQUEST_METHOD"</code><code class="php plain">] === </code><code class="php string">"POST"</code><code class="php plain">) {</code></div>
<div class="line number9 index8 alt2"><code class="php spaces">                </code><code class="php keyword">include</code><code class="php plain">(</code><code class="php string">"add_comment.php"</code><code class="php plain">);         </code></div>
<div class="line number10 index9 alt1"></div>
<div class="line number11 index10 alt2"><code class="php spaces">            </code><code class="php plain">}</code></div>
<div class="line number12 index11 alt1"></div>
<div class="line number13 index12 alt2"><code class="php comments">//Add_comment.php</code></div>
<div class="line number14 index13 alt1"><code class="php plain">&lt;?php </code></div>
<div class="line number15 index14 alt2"><code class="php spaces">            </code><code class="php comments">//Open file and create an array with all comment information as indices</code></div>
<div class="line number16 index15 alt1"><code class="php spaces">            </code><code class="php variable">$comments</code> <code class="php plain">= </code><code class="php functions">file_get_contents</code><code class="php plain">(</code><code class="php variable">$path</code> <code class="php plain">. </code><code class="php string">"comments.txt"</code><code class="php plain">);</code></div>
<div class="line number17 index16 alt2"><code class="php spaces">            </code><code class="php variable">$newcomment</code> <code class="php plain">= [];</code></div>
<div class="line number18 index17 alt1"><code class="php spaces">            </code><code class="php variable">$newcomment</code><code class="php plain">[] = </code><code class="php variable">$_POST</code><code class="php plain">[</code><code class="php string">"name"</code><code class="php plain">];</code></div>
<div class="line number19 index18 alt2"><code class="php spaces">            </code><code class="php variable">$newcomment</code><code class="php plain">[] = </code><code class="php variable">$_POST</code><code class="php plain">[</code><code class="php string">"topic"</code><code class="php plain">];</code></div>
<div class="line number20 index19 alt1"><code class="php spaces">            </code><code class="php variable">$newcomment</code><code class="php plain">[] = </code><code class="php variable">$_POST</code><code class="php plain">[</code><code class="php string">"message"</code><code class="php plain">];</code></div>
<div class="line number21 index20 alt2"><code class="php spaces">            </code><code class="php comments">// Convert to string and add a delimiter to store in file</code></div>
<div class="line number22 index21 alt1"><code class="php spaces">            </code><code class="php variable">$newcomment</code> <code class="php plain">= implode(</code><code class="php string">":"</code><code class="php plain">, </code><code class="php variable">$newcomment</code><code class="php plain">);</code></div>
<div class="line number23 index22 alt2"><code class="php spaces">            </code><code class="php comments">// Write the string to the file</code></div>
<div class="line number24 index23 alt1"><code class="php spaces">            </code><code class="php variable">$comments_w</code> <code class="php plain">= </code><code class="php functions">fopen</code><code class="php plain">(</code><code class="php variable">$path</code> <code class="php plain">. </code><code class="php string">"comments.txt"</code><code class="php plain">, </code><code class="php string">'w'</code><code class="php plain">);</code></div>
<div class="line number25 index24 alt2"></div>
<div class="line number26 index25 alt1"><code class="php spaces">            </code><code class="php plain">fwrite(</code><code class="php variable">$comments_w</code><code class="php plain">, </code><code class="php variable">$comments</code> <code class="php plain">. </code><code class="php string">"n"</code> <code class="php plain">. </code><code class="php variable">$newcomment</code> <code class="php plain">. </code><code class="php string">":"</code> <code class="php plain">); </code></div>
<div class="line number27 index26 alt2"><code class="php spaces">            </code><code class="php comments">// Show all comments</code></div>
<div class="line number28 index27 alt1"><code class="php spaces">            </code><code class="php keyword">include</code><code class="php plain">(</code><code class="php variable">$path</code> <code class="php plain">. </code><code class="php string">"view_comments.php"</code><code class="php plain">);</code></div>
<div class="line number29 index28 alt2"></div>
<div class="line number30 index29 alt1"><code class="php spaces">            </code><code class="php plain">?&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack7.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack7.png" /></p>
<p><strong>Figure: How the comments file looks</strong></p>
<div>
<div id="highlighter_892377" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
<div class="line number8 index7 alt1">8</div>
<div class="line number9 index8 alt2">9</div>
<div class="line number10 index9 alt1">10</div>
<div class="line number11 index10 alt2">11</div>
<div class="line number12 index11 alt1">12</div>
<div class="line number13 index12 alt2">13</div>
<div class="line number14 index13 alt1">14</div>
<div class="line number15 index14 alt2">15</div>
<div class="line number16 index15 alt1">16</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php comments">// View_comments.php</code></div>
<div class="line number2 index1 alt1"></div>
<div class="line number3 index2 alt2"><code class="php spaces">    </code><code class="php plain">&lt;?php</code></div>
<div class="line number4 index3 alt1"><code class="php comments">//Convert to array and echo all out in a certain format within the comments div</code></div>
<div class="line number5 index4 alt2"><code class="php variable">$comments</code> <code class="php plain">= </code><code class="php functions">explode</code><code class="php plain">(</code><code class="php string">":"</code><code class="php plain">, </code><code class="php functions">file_get_contents</code><code class="php plain">(</code><code class="php variable">$path</code> <code class="php plain">. </code><code class="php string">"comments.txt"</code><code class="php plain">));</code></div>
<div class="line number6 index5 alt1"><code class="php functions">echo</code> <code class="php string">"&lt;div id='comments'&gt;"</code><code class="php plain">;</code></div>
<div class="line number7 index6 alt2"><code class="php keyword">for</code> <code class="php plain">(</code><code class="php variable">$i</code> <code class="php plain">= 0; </code><code class="php variable">$i</code> <code class="php plain">&lt; </code><code class="php functions">count</code><code class="php plain">(</code><code class="php variable">$comments</code><code class="php plain">) - 1; </code><code class="php variable">$i</code> <code class="php plain">+= 3) {</code></div>
<div class="line number8 index7 alt1"><code class="php spaces">    </code><code class="php functions">echo</code> <code class="php string">"&lt;p&gt;User: "</code> <code class="php plain">. </code><code class="php variable">$comments</code><code class="php plain">[</code><code class="php variable">$i</code><code class="php plain">] . </code><code class="php string">"&lt;br&gt; posted about: "</code><code class="php plain">.</code></div>
<div class="line number9 index8 alt2"><code class="php spaces">    </code><code class="php variable">$comments</code><code class="php plain">[</code><code class="php variable">$i</code> <code class="php plain">+ 1] . </code><code class="php string">"&lt;br&gt; and he wrote: "</code> <code class="php plain">. </code><code class="php variable">$comments</code><code class="php plain">[</code><code class="php variable">$i</code> <code class="php plain">+ 2];</code></div>
<div class="line number10 index9 alt1"><code class="php spaces">    </code><code class="php functions">echo</code> <code class="php string">" &lt;/p&gt;"</code><code class="php plain">; </code></div>
<div class="line number11 index10 alt2"></div>
<div class="line number12 index11 alt1"></div>
<div class="line number13 index12 alt2"><code class="php plain">}</code></div>
<div class="line number14 index13 alt1"><code class="php functions">echo</code> <code class="php string">"&lt;/div&gt;"</code><code class="php plain">;</code></div>
<div class="line number15 index14 alt2"></div>
<div class="line number16 index15 alt1"><code class="php plain">?&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>This application works just fine when viewed as is, but imagine if a user enters add_comment.php separately, without the file being included from the index.php. This can easily happen as the name of the service implies the file name, and this particular file name is frequently used, and the fact that add_comment.php is in the same directory facilitates the process.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack8.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/082814_1337_WebsiteHack8.png" /></p>
<p><strong>Figure: Viewing add_comment.php on its own</strong></p>
<p>Now, the attacker would know that we have a variable called $path and he can probably guess that we are setting the path to the comments file as there is a warning that file_get_contents(comments.txt) cannot be opened. Thus, he knows the name of the file that contains all our comments as well. Because the include is failing, he also knows the whole include_path which can also be dangerous. Also, the attacker knows another file in our directory tree (view_comments.php) so he can access it and look for some more errors. He also knows that in this file we are working with the POST values from the form, as he can view the HTML and see they are the same.</p>
<p>This comments form is also vulnerable to diferent code injection attacks. You can easily insert in one of the comment fields to test it out. In that way, the browsers of the users’ will execute any code that you like each time they visit the page.</p>
<p>A probable solution is easy: wrapping the post values in htmlspecialchars() function which converts &lt; and &gt; amongst others as special characters (&lt;, &gt;, etc.) preventing them from being interpreted as code.</p>
<p>$newcomment[] = htmlspecialchars($_POST[&#8220;name&#8221;]);</p>
<p>$newcomment[] = htmlspecialchars($_POST[&#8220;topic&#8221;]);</p>
<p>$newcomment[] = htmlspecialchars($_POST[&#8220;message&#8221;]);</p>
<p><strong>Solution</strong></p>
<p>A simple solution to get rid of all those errors in this example is to wrap the code in add_comment.php and view_comments.php inside the following if statement:</p>
<div>
<div id="highlighter_343023" class="syntaxhighlighter  php">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="php spaces">    </code><code class="php keyword">if</code> <code class="php plain">(isset(</code><code class="php variable">$path</code><code class="php plain">)) { </code></div>
<div class="line number2 index1 alt1"><code class="php comments">//code here</code></div>
<div class="line number3 index2 alt2"></div>
<div class="line number4 index3 alt1"><code class="php plain">}</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>To read the entire article, please go to: <a href="http://resources.infosecinstitute.com/website-hacking-101-part-ii/" target="_blank">the article in InfoSec Institute</a></p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-ii%2F&#038;t=Website%20Hacking%2C%20Part%20II&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-ii%2F&#038;p&#091;images&#093;&#091;0&#093;=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F082814_1337_WebsiteHack1.png&#038;p&#091;title&#093;=Website%20Hacking%2C%20Part%20II" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-ii%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-ii%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-ii%2F&#038;title=Website%20Hacking%2C%20Part%20II" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-ii%2F&#038;media=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F082814_1337_WebsiteHack1.png&#038;description=Website%20Hacking%2C%20Part%20II" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-ii%2F&#038;title=Website%20Hacking%2C%20Part%20II" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Website%20Hacking%2C%20Part%20II&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-ii%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/website-hacking-part-ii/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Website Hacking, Part I</title>
		<link>https://www.dimoff.biz/blog/website-hacking-part-i/</link>
		<comments>https://www.dimoff.biz/blog/website-hacking-part-i/#comments</comments>
		<pubDate>Fri, 26 Dec 2014 18:54:29 +0000</pubDate>
		<dc:creator><![CDATA[Dimoff]]></dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Information Security]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://localhost:8079/current/Dimoff1/blog/?p=24</guid>
		<description><![CDATA[&#160; Websites are used daily by a large part of the world’s population to carry sensitive data from a person to an entity with online-based presence. In websites containing materials&#8230;]]></description>
				<content:encoded><![CDATA[<p>&nbsp;</p>
<p>Websites are used daily by a large part of the world’s population to carry sensitive data from a person to an entity with online-based presence. In websites containing materials that are shown after authentication only, forms transfer data containing user credentials to server-side scripts. Users store their credit card details in their online accounts and use forms to buy items online, so it is crucial to keep the integrity, confidentiality and availability of this data intact.</p>
<p>This article is written merely with penetration testing and website security in mind. Any attempts to penetrate into live systems on your behalf and without consent may lead to criminal proceedings.</p>
<p>To try the training files that come along with this article, you would need a local server such as XAMPP or WAMPP with Apache and preferably MySQL turned on. If you are on Windows, to install Hydra you would need to install make, gcc and ssl libraries of Cygwin. Therafter, you would need to start it with the Cygwin Terminal. John the Ripper, on the other hand, can be started from the Command Prompt.</p>
<p><strong>Exercise 1: Deep Data Hiding</strong></p>
<p>In the past, and even today, some people have used security through obscurity. This means that they have unprotected directories and files with the sole protection being that they do not have any backlinks and no links to them in the main site. Thus, if one knew the URL of the directory or file – he could readily access it. A common way to reveal obscure directories is to check the publicly visible robots.txt and see what is disallowed to be indexed by search engines.</p>
<p>Now open the DeepDataHiding folder through your localhost and try to find the hidden directory where uploaded .doc files from “users” are stored, then access it. If you upload a .doc file to test this out, in the main page of the directory – it won’t leave your computer.</p>
<p><strong>Exercise 2: Populating a Dictionary</strong></p>
<p>To populate a dictionary, we will be using John the Ripper. Open the PopulatingDictionary folder.</p>
<p>You can populate a dictionary in John the Ripper and cut the output size by knowing the type of password (its maximum length, whether it should be only digits, contain special characters, etc.).</p>
<p>To create a simple dictionary and save it to a file, you can browse to the directory of the john the ripper installation in CMD and use: john-mmx –incremental=alpha –stdout &gt; filename whereas filename is the name and location of the file in which the words should be saved to.</p>
<p>There are various options in the incremental mode, such as Digit (only digits), Lanman (letters, numbers and some special characters), Alpha (only letters) and All (all characters). Thus, you can also use john-mmx –incremental=lanman –stdout &gt; wordlist.txt, etc.</p>
<p>Be aware that the size of the text file would probably get really big in just a couple of seconds, depending on your machine’s abilities.</p>
<p><strong>Exercise 3: Acquiring user and password list for dictionary attacks</strong></p>
<p>Querying Google for passwords and user lists is usually pretty straightforward.</p>
<p>You use something like filetype:lst password for passwords and filetype:lst user for username lists.</p>
<p>We have included a sample username list and a password list downloaded from the Internet along with the attachment files to this article.</p>
<p><strong>Exercise 4: Breaking HTTPAuth</strong></p>
<p>For this exercise, we will be using Hydra and the user/pass lists included in the attachment files.</p>
<p>When calling Hydra ($ hydra.exe) the parameter –L usrlistpath serves the purpose of supplying the program a path to a username list file whose usernames will be tested along with all the passwords until a match is found. –l username gives Hydra a single username, which option can be used if you know the username you are trying to break into but do not know the particular password.</p>
<p>-P loads a password list while –p loads a single password.</p>
<p>Next, you specify the host to attack (localhost or 127.0.0.1) followed by http-get (request a directory/page), followed by the path to the particular directory or file you are trying to access (path excluding the host which is already given). It will most likely look something like this:</p>
<p>hydra.exe -L HD:/WebsiteHacking/FormCracking/usrnames.txt -P HD:/WebsiteHacking/FormCracking/passwords.txt localhost http-get /HTTPSecurity/</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack1.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack1.png" /></p>
<p>Figure 1: the HTTPAuth seeking credentials. Get them!</p>
<p>To establish a simple HTTPAuth mechanism yourself, you need to create your password by browsing to htpasswd.exe in your Apache bin folder, starting it in Command Prompt, and creating it. You can move the user account list file to any directory you want and start the mechanism by editing your .htaccess file:</p>
<div>
<div id="highlighter_102282" class="syntaxhighlighter  plain">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="plain plain">AuthType Basic</code></div>
<div class="line number2 index1 alt1"></div>
<div class="line number3 index2 alt2"><code class="plain plain">AuthName "Admin Area"</code></div>
<div class="line number4 index3 alt1"></div>
<div class="line number5 index4 alt2"><code class="plain plain">AuthUserFile pathauthorized.htpasswd</code></div>
<div class="line number6 index5 alt1"></div>
<div class="line number7 index6 alt2"><code class="plain plain">Require user …</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>You can select only particular users to be able to access the page, and you can set different username lists for different parts of the website, but this mechanism for protection remains basic. To test cracking the example from the files, change the path of AuthUserFile to the current location of the HTTPSecurity directory.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack2.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack2.png" /></p>
<p><strong>Exercise 5: Breaking a POST login form</strong></p>
<p>The password and usernames list are in the FormCracking folder. They have not been changed, but the correct login credentials are easy enough.</p>
<p>The following statement might work:</p>
<p>hydra –L path/FormCracking/usrnames.txt -P path/FormCracking/passwords.txt 127.0.0.1 http-post-form</p>
<p>“/FormCracking/index.php:username=^USER^&amp;passwd=^PASS^:Oops”</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack3.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack3.png" /></p>
<p>The difference between this statement and when we cracked the HTTPAuth mechanism is that here we include the parameters that the form sends to the server-side script, in this case username and password. Those are the “name” attributes of the relevant input tags that we want to test.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack4.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack4.png" /></p>
<p>Figure 2: viewing the POST fields.</p>
<p>Another difference is that after the address that we want to crack we include separated by a colon ( : ) the text that shows when the login submission is incorrect. Basically, we are telling the program to repeat until it gets a different output. In our case, we have “Oops” as a part of the login error string we receive.</p>
<p>We also include ^USER^ and ^PASS^ after each POST field that must be filled with the data from the username and password lists by the program.</p>
<p>Then, we wait and the job is done.</p>
<p><strong>Exercise 6: Modifying Parameters</strong></p>
<p>The next exercise is in the folder ParameterTampering. Open ParameterTampering/login.php with your browser. Your task is to bypass authorization or login with wrong credentials without viewing the server-side code and accessing members.php message and members2.php without the “Error!”. You do not have to crack the user details. For one of the methods, you must see what logging in looks like – use john/123</p>
<p>The first manner in which you can do this is by modifying an element in the page, the second involves a change in the URL.</p>
<p>The other task is to enter in members2.php without the server echoing “Error”. To do this, you should tamper with the HTTP Headers and add a referrer. I would recommend a plugin such as Tamper Data for Firefox or Request Maker for Chrome.</p>
<p>Answers:</p>
<p>1st possibility:</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack5.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack5.png" /></p>
<p>Figure 3: modifying the values of hidden inputs.</p>
<p>It might seem weird at first, but many sites actually have hidden inputs in which they store important data. An example is PayPal shopping carts on third-party websites where you can change fields such as name of the product directly by changing the value of a hidden input. There are some outdated shopping carts which still use price as a hidden input which means that if you don’t use their API and verify the amount that was paid to you through a server-side script – the user can easily pay as much as he wants for the product!</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack6.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack6.png" /></p>
<p>Figure 4: an example of a shopping cart which sets the price of the item on the client-side.</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack7.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack7.png" /></p>
<p>Figure 5: changing the name of the product in stores using PayPal as a payment method can still do some harm.</p>
<p>2nd possibility:</p>
<p>Setting a loggedin GET request, that’s probably not something you would meet somewhere today though.</p>
<p>3rd possibility, members2.php:</p>
<p>Install and start Tamper Data with alt+T when the page is opened. Add a new Header…</p>
<p><img class="lazy " src="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack8.png" alt="" data-original="http://resources.infosecinstitute.com/wp-content/uploads/080814_1249_WebsiteHack8.png" /></p>
<p>Called Referer and with value the path to login.php, it would look like you were redirected from login.php. There are developers out there who think HTTP_REFERER proves that the user is legitimate despite that it’s just a header sent through HTTP requests, and this is a point of exploitation in some sites even today.</p>
<p><strong>Exercise 7: Exploiting Account Lockout</strong></p>
<p>&nbsp;</p>
<p>To read the entire article and download the practice files, please go to: <a target="_blank" href="http://resources.infosecinstitute.com/website-hacking-101/">the article in InfoSec Institute</a></p>
<div class="synved-social-container synved-social-container-share"><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox" data-provider="facebook" target="_blank" rel="nofollow" title="Share on Facebook" href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-i%2F&#038;t=Website%20Hacking%2C%20Part%20I&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-i%2F&#038;p&#091;images&#093;&#091;0&#093;=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F080814_1249_WebsiteHack1.png&#038;p&#091;title&#093;=Website%20Hacking%2C%20Part%20I" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="facebook" title="Share on Facebook" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox" data-provider="twitter" target="_blank" rel="nofollow" title="Share on Twitter" href="http://twitter.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-i%2F&#038;text=Hey%2C%20check%20this%20out" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="twitter" title="Share on Twitter" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/twitter.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-google_plus nolightbox" data-provider="google_plus" target="_blank" rel="nofollow" title="Share on Google+" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-i%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="google_plus" title="Share on Google+" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox" data-provider="reddit" target="_blank" rel="nofollow" title="Share on Reddit" href="http://www.reddit.com/submit?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-i%2F&#038;title=Website%20Hacking%2C%20Part%20I" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="reddit" title="Share on Reddit" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-pinterest nolightbox" data-provider="pinterest" target="_blank" rel="nofollow" title="Pin it with Pinterest" href="http://pinterest.com/pin/create/button/?url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-i%2F&#038;media=http%3A%2F%2Fresources.infosecinstitute.com%2Fwp-content%2Fuploads%2F080814_1249_WebsiteHack1.png&#038;description=Website%20Hacking%2C%20Part%20I" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="pinterest" title="Pin it with Pinterest" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/pinterest.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox" data-provider="linkedin" target="_blank" rel="nofollow" title="Share on Linkedin" href="http://www.linkedin.com/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-i%2F&#038;title=Website%20Hacking%2C%20Part%20I" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px;"><img alt="linkedin" title="Share on Linkedin" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png" /></a><a class="synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox" data-provider="mail" rel="nofollow" title="Share by email" href="mailto:?subject=Website%20Hacking%2C%20Part%20I&#038;body=Hey%2C%20check%20this%20out:%20https%3A%2F%2Fwww.dimoff.biz%2Fblog%2Fwebsite-hacking-part-i%2F" style="font-size: 0px; width:48px;height:48px;margin:0;margin-bottom:5px;"><img alt="mail" title="Share by email" class="synved-share-image synved-social-image synved-social-image-share" width="48" height="48" style="display: inline; width:48px;height:48px; margin: 0; padding: 0; border: none; box-shadow: none;" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png" /></a><a class="synved-social-credit" target="_blank" rel="nofollow" title="WordPress Social Media Feather" href="http://synved.com/wordpress-social-media-feather/" style="color:#444; text-decoration:none; font-size:8px; margin-left:5px;vertical-align:10px;white-space:nowrap;"><span>by </span><img style="display: inline;margin:0;padding:0;width:16px;height:16px;" width="16" height="16" alt="feather" src="https://www.dimoff.biz/blog/wp-content/plugins/social-media-feather/synved-social/image/icon.png" /></a></div>]]></content:encoded>
			<wfw:commentRss>https://www.dimoff.biz/blog/website-hacking-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
