{"id":2716,"date":"2017-02-05T13:20:35","date_gmt":"2017-02-05T12:20:35","guid":{"rendered":"https:\/\/kudzia.eu\/b\/?p=2716"},"modified":"2017-02-05T13:21:13","modified_gmt":"2017-02-05T12:21:13","slug":"simplistic-gatekeeper-limiting-access-to-apache2-based-proxy","status":"publish","type":"post","link":"https:\/\/kudzia.eu\/b\/2017\/02\/simplistic-gatekeeper-limiting-access-to-apache2-based-proxy\/","title":{"rendered":"simplistic gatekeeper limiting access to apache2-based proxy"},"content":{"rendered":"<p>i had to expose some web-based application hosted on a windows server to the internet. i don&#8217;t put too much trust in the developers of that particular application so i did not want to make it reachable from the public internet. while i could not use ip address based whitelist i could count on the end-user of the application to do a simple routine at the beginning of her\/his work: visit another page and only then try to access the actual web-app. security through obscurity? perhaps, but for me it that&#8217;s one more layer of protection.<br \/>\n<!--more--><\/p>\n<p>i&#8217;ve found out that i don&#8217;t need to dynamically rewrite apache2 config and add\/remove ip addresses that are allowed to access the protected resource. <a href=\"http:\/\/stackoverflow.com\/a\/19937874\/104398\">this<\/a> stackoverflow answer suggested that mod rewrite&#8217;s <a href=\"https:\/\/httpd.apache.org\/docs\/2.4\/rewrite\/rewritemap.html\">RewriteMap<\/a> can be a solution. so here&#8217;s my solution:<\/p>\n<p>apache2 vhost:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;VirtualHost *:443&gt;\r\n DocumentRoot \/var\/www\/\r\n ServerName some.host.com\r\n ProxyPass \/ProtectedResource http:\/\/10.0.0.5\/ProtectedResource\r\n ProxyPassReverse \/ProtectedResource http:\/\/10.0.0.5\/ProtectedResource\r\n \r\n RewriteEngine On\r\n RewriteMap ipslist txt:\/var\/www\/gatekeeper\/list.txt\r\n &lt;Location \/ProtectedResource&gt;\r\n  AddOutputFilterByType INFLATE;SUBSTITUTE;DEFLATE text\/html text\/plain text\/xml\r\n  Substitute &quot;s|http:\/\/10.0.0.5|https:\/\/some.host.com|i&quot;\r\n  \r\n  RewriteCond %{REMOTE_ADDR} ^(.*)$\r\n  RewriteCond ${ipslist:%1|black} ^black$ &#x5B;NC]\r\n  RewriteRule ^ - &#x5B;F]\r\n &lt;\/Location&gt;\r\n \r\n # ssl related stuff + logging  \r\n&lt;\/Virtualhost&gt;\r\n<\/pre>\n<p>did you see what i did there? not just allowed reverse-proxying of request arriving to \/ProtectedResource to an internal Windows-based 10.0.0.5 but also protecting access to it via https and re-writing on the fly references in html\/xml to http:\/\/10.0.0.5 into https:\/\/ssl.host.com ; access to the \/ProtectedResource is only possible for hosts that are listed in the text file \/var\/www\/gatekeeper\/list.txt<\/p>\n<p>that file is updated by two scripts:<\/p>\n<p>\/var\/www\/gatekeeper\/index.php &#8211; invoked by the user at the beginning of the working day by visiting https:\/\/some.host.com\/gatekeeper\/:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n $ip = $_SERVER&#x5B;'REMOTE_ADDR'];\r\n $x=file_get_contents('list.txt');\r\n file_put_contents('list.txt',$x.&quot;\\n&quot;.$ip.' '.time());\r\n echo &quot;hi $ip&quot;;\r\n<\/pre>\n<p>and \/var\/www\/gatekeeper\/cron.php &#8211; invoked from cron by visiting https:\/\/some.host.com\/gatekeeper\/cron.php &#8211; it&#8217;s responsible for clean-up of the stale entries older than few hours:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n $res = &quot;&quot;;\r\n foreach(explode(&quot;\\n&quot;,file_get_contents('list.txt')) as $row){\r\n  $row = explode(' ',$row);\r\n   if (sizeof($row)!=2)continue;\r\n   if ($row&#x5B;1]&lt;time()-3600*8)continue;\r\n   $res.=$row&#x5B;0].&quot; &quot;.$row&#x5B;1].&quot;\\n&quot;;\r\n }\r\n file_put_contents('list.txt',$res);\r\n<\/pre>\n<p>there&#8217;s a risk of race conditions when during the execution of cron someone visits the gatekeeper page. in my case &#8211; where number of users is very small, i&#8217;m comfortable with it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>i had to expose some web-based application hosted on a windows server to the internet. i don&#8217;t put too much trust in the developers of that particular application so i did not want to make it reachable from the public internet. while i could not use ip address based whitelist i could count on the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[98],"class_list":["post-2716","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-poor-mans-security"],"_links":{"self":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/2716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/comments?post=2716"}],"version-history":[{"count":2,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/2716\/revisions"}],"predecessor-version":[{"id":2718,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/2716\/revisions\/2718"}],"wp:attachment":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/media?parent=2716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/categories?post=2716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/tags?post=2716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}