# Masking IP-Addresses on logging?

**URL:** <https://www.ruby-forum.com/t/masking-ip-addresses-on-logging/183663>\
**Category:** NGINX\
**Created:** [February 18, 2010, 1:40pm UTC](https://www.ruby-forum.com/t/masking-ip-addresses-on-logging/183663 "2010-02-18T13:40:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ts77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@ts77](https://www.ruby-forum.com/u/ts77)\
**Post date:** [February 18, 2010, 1:40pm UTC](https://www.ruby-forum.com/t/masking-ip-addresses-on-logging/183663/1 "2010-02-18T13:40:24Z")

</div>

Hey there,

some of you might know that storing full ip addresses in germany is  
getting more tricky every day and considered a legal problem.  
Is there any way to store masked ip addresses in access logs with nginx  
while retaining the possibility to use web analytics software?

thanks,

thomas

Posted at Nginx Forum:

> **[Masking IP-Addresses on logging?](https://forum.nginx.org/read.php?2,55369,55369#msg-55369)**
>
> Hey there, some of you might know that storing full ip addresses in germany is getting more tricky every day and considered a legal problem. Is there any way to store masked ip addresses in access logs with nginx while retaining the possibility to...

---

<div class="post-metadata">

**Author:** ![ts77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@ts77](https://www.ruby-forum.com/u/ts77)\
**Post date:** [February 18, 2010, 3:26pm UTC](https://www.ruby-forum.com/t/masking-ip-addresses-on-logging/183663/2 "2010-02-18T15:26:22Z")

</div>

It’s humorous when politicians try to regulate the openness of the  
Internet. Well, at least until it starts affecting you in ways like  
this.

You can change the log format with something like this:

server {  
…  
log\_format no\_ip '0.0.0.0 - no-user [$time\_local] ’  
'“$request” $status $body\_bytes\_sent ’  
‘“$http\_referer” “$http\_user\_agent”’;  
access\_log logs/access.log no\_ip;  
}

See also [Module ngx\_http\_log\_module](http://wiki.nginx.org/NginxHttpLogModule)

Note that in my example, I still write out a bogus IP (normally  
$remote\_addr) and “no-user” (normally $remote\_user, which I’m guessing  
might be problematic for you as well) so that existing log parsers  
won’t trip on your new format.

As for analytics software, you should still be able to use it without  
any problem – you’ll just lose the location info that’s normally  
determined based on the IP. If you want to keep that, you should be  
able to use the geo module and write the country code (or lat/long  
maybe?) to your log file as long as your analytics package can parse  
and properly handle the extra data.

---

<div class="post-metadata">

**Author:** ![ts77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@ts77](https://www.ruby-forum.com/u/ts77)\
**Post date:** [February 18, 2010, 11:27pm UTC](https://www.ruby-forum.com/t/masking-ip-addresses-on-logging/183663/3 "2010-02-18T23:27:35Z")

</div>

Nick P. wrote:

> It’s humorous when politicians try to regulate the openness of the Internet.

Humorous wouldn’t be my choice of words.

> You can change the log format with something like this:
> 
> log\_format no\_ip '0.0.0.0 - no-user [$time\_local] ’

No. This will break most log-based analytics software, which use the  
source address to tell between different visitors, in order to generate  
‘visit’, ‘page’, and ‘hit’ statistics.

I guess the only feasible way to have some kind of plausible denial in  
court _and_ to get your statistics is to use the result of a one-way  
hash function as a fake IP address.

MD4 and MD5 would do a fine job, but they are probably way too heavy to  
use on a web server. CRC-32 is lighter and outputs a 32bit code, which  
could be reformatted as a fake IP address.

I suggest writing a custom module that will compute the CRC-32 of  
“$remote\_address$http\_user\_agent” (just to get some variance over the IP  
address alone), then format it in the dotted-decimal form of IP  
addresses and make it available as $fake\_remote\_address.

This won’t keep you from reversing the code. No function will, in this  
case, not even MD5, because you could always brute-force your way  
through, as you only have 2^32 plain texts. If you used the User-agent  
as an input to the hash function and _not_ store it in the log, then it  
would get slightly better… But I guess CRC-32 will be enough for any  
lawyer 🙂

Tobia

---

<div class="post-metadata">

**Author:** ![ts77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@ts77](https://www.ruby-forum.com/u/ts77)\
**Post date:** [February 19, 2010, 1:01am UTC](https://www.ruby-forum.com/t/masking-ip-addresses-on-logging/183663/4 "2010-02-19T01:01:15Z")

</div>

On Thu, 2010-02-18 at 07:39 -0500, ts77 wrote:

> Hey there,
> 
> some of you might know that storing full ip addresses in germany is getting more tricky every day and considered a legal problem.  
> Is there any way to store masked ip addresses in access logs with nginx while retaining the possibility to use web analytics software?

Disclaimer: I’m not a lawyer and I’m not from Germany.

That being said, from my reading of the situation, I don’t think there’s  
an issue with storing the IP address so long as it’s only kept as long  
as actually _needed_ (i.e. for billing purposes or in your case, log  
analysis). Why not just do your analysis within some reasonable  
time-frame and then delete or scrub the raw data?

As to those who think that these laws are silly, I have three words for  
you: Senator Joseph McCarthy. It really ought not to be necessary to  
have such laws, but of course we in the information fields can’t be  
trusted to not hoard data that puts people in peril. I applaud Germany  
and the E.U. for putting their citizen’s privacy above corporate  
interests.

Regards,  
Cliff
