Mod_rpaf source download

The home of mod_rpaf @ http://stderr.net/apache/rpaf/ seems to be offline. In the meantime, its clear people are searching for this code so find it here.

Note – this is not my code. All development (apart from my patch in my previous post) is attributed to Thomas Eibner (thomas…@…stderr.net).

Advertisements

Mod_rpaf: Extract real-IP from behind reverse proxy/load balancer

There are many reasons why you’d want to front your Apache-based site behind a reverse proxy but one of the sacrifices you make in speed and security, you would by default lose a sensible way to extract the real IP (or at least have a very good chance to) of the client making the request. This is because your reverse proxy or load balancer in many situations becomes the client and to Apache this means you will only ever see the IP (the internal IP if its local to your Apache server farm) which become useless if you are doing analytics on the Apache logs or making decisions for the request based on the IP (for example Geo decisions, or ModSecurity).
To circumvent this, you’d want to use mod_rpaf (http://stderr.net/apache/rpaf/). This module is easily integrated into your Apache environment using the following method (Apache 2.x):

sudo apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

And then configuring Apache to pick up this module:

# if DSO load module first:
LoadModule rpaf_module modules/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname Off
RPAFproxy_ips 10.10.10.1
RPAFheader X-Forwarded-For

This says look in the X-Forwarded-For header for the existence of the IPs listed in the RPAFproxy_ips line, and if found, pick up the external IP.

Unfortunately in practice this doesn’t work so well for a couple of reasons:

1. When auto-scaling an environment such as in Amazon, and you’re using something like ELB, you won’t know what internal IP the ELB will talk to Apache on – causing you to not be able to auto-configure mod_rpaf at install/run-time.
2. It makes an assumption on the external IP: it just takes the last IP seen on the X-Forwarded-For line.

Number 2 caused me issues because it is possible to have an internal IP address as the last entry on the X-Forwarded-For line – and if inspected properly, the external IP would be somewhere along the X-Forwarded-For line. To get around this and the ability to have more flexibility in the mod_rpaf config so that the code invoked to expose the real IP when an internal address was seen I created the following patch:

--- mod_rpaf-2.0.c 2011-06-23 13:51:53.000000000 +0100
 +++ mod_rpaf-2.0.c.new 2011-06-24 16:08:18.000000000 +0100
 @@ -71,6 +71,7 @@
 #include "http_protocol.h"
 #include "http_vhost.h"
 #include "apr_strings.h"
 +#include "string.h"
module AP_MODULE_DECLARE_DATA rpaf_module;
@@ -136,10 +137,14 @@
 }
static int is_in_array(const char *remote_ip, apr_array_header_t *proxy_ips) {
 - int i;
 + int i,len;
 + char tmp[16];
 char **list = (char**)proxy_ips->elts;
 for (i = 0; i nelts; i++) {
 - if (strcmp(remote_ip, list[i]) == 0)
 + len=strlen(list[i]);
 + strncpy(tmp,remote_ip,len);
 + tmp[len]='';
 + if (strcmp(tmp, list[i]) == 0)
 return 1;
 }
 return 0;
 @@ -155,6 +160,7 @@
 static int change_remote_ip(request_rec *r) {
 const char *fwdvalue;
 char *val;
 + int i;
 rpaf_server_cfg *cfg = (rpaf_server_cfg *)ap_get_module_config(r->server->module_config,
 &rpaf_module);
@@ -183,7 +189,10 @@
 rcr->old_ip = apr_pstrdup(r->connection->pool, r->connection->remote_ip);
 rcr->r = r;
 apr_pool_cleanup_register(r->pool, (void *)rcr, rpaf_cleanup, apr_pool_cleanup_null);
 - r->connection->remote_ip = apr_pstrdup(r->connection->pool, ((char **)arr->elts)[((arr->nelts)-1)]);
 + for(i=arr->nelts-1; i >= 0; i--) {
 + if (is_in_array( apr_pstrdup(r->connection->pool, ((char **)arr->elts)[i]), cfg->proxy_ips) == 0 )
 + r->connection->remote_ip = apr_pstrdup(r->connection->pool, ((char **)arr->elts)[i]);
 + }
 r->connection->remote_addr->sa.sin.sin_addr.s_addr = apr_inet_addr(r->connection->remote_ip);
 if (cfg->sethostname) {
 const char *hostvalue;

Patch and compile/install as follows:

patch -p1 < patch_mod_rpaf
sudo apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

This allows you to use a modified config file which allows you to run Apache behind, say, ELB and it extracts the last seen external IP (i.e. not 10. 172. or 192.168.). Of course, edit to suit your particular environment:

# if DSO load module first:
LoadModule rpaf_module modules/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname Off
RPAFproxy_ips 10. 172. 192.168.
RPAFheader X-Forwarded-For

When it sees a line like

X-Forwarded-For: 192.168.100.227, 209.88.21.195, 10.58.59.219, 192.168.123.123

It will pick out 209.88.21.195 as the real IP.