{"id":395,"date":"2017-05-21T01:04:51","date_gmt":"2017-05-21T00:04:51","guid":{"rendered":"http:\/\/simkin.org\/wordpress\/?p=395"},"modified":"2017-05-21T01:04:51","modified_gmt":"2017-05-21T00:04:51","slug":"apache-security","status":"publish","type":"post","link":"http:\/\/simkin.org\/wordpress\/?p=395","title":{"rendered":"Apache Security"},"content":{"rendered":"<p>I have recently been the unfortunate recipient of some website hacking leading to a minor bit of spam injection into my mail server. Nothing too serious and due mainly to an old version of Centos and some WordPress code that could have been constructed a little bit better, but anyway. So what do you look for when checking your webserver and how do you keep it a little bit more secure?<br \/>\nIn my case, I had a few random .php pages inserted and my investigation spurned me on to take the basic steps I should have done in the first place. Some of these were already correct but no harm in covering them:<\/p>\n<ol>\n<li>Set your apache instance to NOT run as root and set your file permissions to read-only to all places that need access by the server. Things to look for:\n<ul>\n<li>in \/etc\/https\/conf\/httpd.conf, make sure the user and group lines are a user like &#8220;www-data&#8221; or &#8220;apache&#8221;<\/li>\n<\/ul>\n<\/li>\n<li>Keep an eye on \/var\/log\/&lt;websitename&gt;-access_log for odd stuff going on\n<ul>\n<li>I have a php page on my webserver that looks for post messages for example. Any that I don&#8217;t like, I block the client site. To block people from your website, include this in your vhost:<\/li>\n<li>Order deny,allow<\/li>\n<li>deny from 1.2.3.4\/24<\/li>\n<li>allow from all<\/li>\n<\/ul>\n<\/li>\n<li>If you run phpMyAdmin, ensure you ONLY allow hosts you recognise<\/li>\n<li>Install mod_security (read:\u00a0https:\/\/www.cyberciti.biz\/faq\/rhel-fedora-centos-httpd-mod_security-configuration\/)<\/li>\n<li>Edit your .htaccess file to prevent http1.0 POSTing:\n<ul>\n<li>\n<pre><code># require HTTP 1.1 for POST\n&lt;IfModule mod_rewrite.c&gt;\n\tRewriteCond %{THE_REQUEST} ^POST(.*)HTTP\/(0\\.9|1\\.0)$ [NC]\n\tRewriteRule .* - [F,L]\n&lt;\/IfModule&gt;<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Setup a PHP file on your website to allow you to quickly review what is going on from anywhere. Mine does this for example:<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<pre> &lt;?php\n$badtext = array(\"&lt;\",\"&gt;\");\n$goodtext = array (\"{\",\"}\");\n$output = shell_exec('ls -la webcam.jpg');\necho \"&lt;pre&gt;$output&lt;\/pre&gt;\";\n$output = shell_exec('date');\necho \"Page Refreshed:&lt;pre&gt;$output&lt;\/pre&gt;\";\necho \"Who has been trying to hack simkin.org...&lt;br&gt;\";\n$simkinhackers = shell_exec('tail -n60 \/var\/log\/simkin.org-access_log | grep \"POST\"\n');\nforeach(preg_split(\"\/((\\r?\\n)|(\\r\\n?))\/\", $simkinhackers) as $line){\n \/\/output the first bit of each line with the ip address\n $pos = strpos($line, \"-\")-1;\n \/\/now the rest of the line\n $ip = substr($line, 0,$pos);\n if (strlen($ip)&gt;1) {\n echo \"&lt;br \/&gt;&lt;a href='chkIP.php?item=\".$ip.\"'&gt;\".$ip.\"&lt;\/a&gt;\";\n echo \"&lt;a href='https:\/\/www.abuseipdb.com\/check\/\".$ip.\"'&gt;.&lt;\/a&gt;\";\n echo substr($line,$pos,strlen($line)-$pos);\n }\n}\n$simkinhackers2 = shell_exec('tail -n60 \/var\/log\/simkin.org-access_log | grep \"USER\"');\nforeach(preg_split(\"\/((\\r?\\n)|(\\r\\n?))\/\", $simkinhackers2) as $line){\n \/\/output the first bit of each line with the ip address\n $pos = strpos($line, \"-\")-1;\n \/\/now the rest of the line\n $ip = substr($line, 0,$pos);\n if (strlen($ip)&gt;1) {\n echo \"&lt;br \/&gt;&lt;a href='chkIP.php?item=\".$ip.\"'&gt;\".$ip.\"&lt;\/a&gt;\";\n echo \"&lt;a href='https:\/\/www.abuseipdb.com\/check\/\".$ip.\"'&gt;.&lt;\/a&gt;\";\n echo substr($line,$pos,strlen($line)-$pos);\n }\n}\n$simkinhacker3 = shell_exec('tail -n60 \/var\/log\/simkin.org-access_log | grep \"action=upload\"');\nforeach(preg_split(\"\/((\\r?\\n)|(\\r\\n?))\/\", $simkinhackers3) as $line){\n \/\/output the first bit of each line with the ip address\n $pos = strpos($line, \"-\")-1;\n \/\/now the rest of the line\n $ip = substr($line, 0,$pos);\n if (strlen($ip)&gt;1) {\n echo \"&lt;br \/&gt;&lt;a href='chkIP.php?item=\".$ip.\"'&gt;\".$ip.\"&lt;\/a&gt;\";\n echo \"&lt;a href='https:\/\/www.abuseipdb.com\/check\/\".$ip.\"'&gt;.&lt;\/a&gt;\";\n echo substr($line,$pos,strlen($line)-$pos);\n }\n}\n$simkinhackers4 = shell_exec('tail -n200 \/var\/log\/simkin.org-access_log | grep \"lostpassword\"');\nforeach(preg_split(\"\/((\\r?\\n)|(\\r\\n?))\/\", $simkinhackers4) as $line){\n \/\/output the first bit of each line with the ip address\n $pos = strpos($line, \"-\")-1;\n \/\/now the rest of the line\n $ip = substr($line, 0,$pos);\n if (strlen($ip)&gt;1) {\n echo \"&lt;br \/&gt;&lt;a href='chkIP.php?item=\".$ip.\"'&gt;\".$ip.\"&lt;\/a&gt;\";\n echo \"&lt;a href='https:\/\/www.abuseipdb.com\/check\/\".$ip.\"'&gt;.&lt;\/a&gt;\";\n echo substr($line,$pos,strlen($line)-$pos);\n }\n}\n# Grab the IP addresses to a logfile\n $ip1 = shell_exec('tail -n60 \/var\/log\/simkin.org-access_log | grep \"POST\" | egrep -o \"([0-9]{1,3}\\.){3}[0-9]{1,3}\"');\n $ip2 = shell_exec('tail -n60 \/var\/log\/simkin.org-access_log | grep \"USER\" | egrep -o \"([0-9]{1,3}\\.){3}[0-9]{1,3}\"');\n $ip3 = shell_exec('tail -n200 \/var\/log\/simkin.org-access_log | grep \"lostpassword\" | egrep -o \"([0-9]{1,3}\\.){3}[0-9]{1,3}\"');\n $textstring = $ip1.$ip2.$ip3;\n file_put_contents('~\/badpeople.log',$textstring , LOCK_EX);\n $nul = shell_exec(\"cat ~\/badpeople.log | awk '!seen[$0]++' &gt; ~\/badpeople.txt\");\n $handle = fopen(\"~\/badpeople.txt\", \"r\");\n if ($handle) {\n while (($line = fgets($handle)) !== false) {\n \/\/ process the line read.\n echo \"&lt;br \/&gt;&lt;a href='https:\/\/www.whois.com\/whois\/\".$line.\"' target='new'&gt;\".$line.\"&lt;\/a&gt;\";\n }\n fclose($handle);\n } else {\n \/\/ error opening the file.\n }\n ?&gt;<\/pre>\n<ol>\n<li>Whitelist your website to only allow POSTing to pages you expect to see POSTS (see:\u00a0https:\/\/perishablepress.com\/protect-post-requests\/)<\/li>\n<li>Keep a backup<\/li>\n<li>Update everything regularly with sudo apt-get update or yum update<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I have recently been the unfortunate recipient of some website hacking leading to a minor bit of spam injection into &hellip; <a class=\"more-link\" href=\"http:\/\/simkin.org\/wordpress\/?p=395\">More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[287],"tags":[144,120],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/395"}],"collection":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=395"}],"version-history":[{"count":0,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/395\/revisions"}],"wp:attachment":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}