0

If it were possible to retrieve the remote IP from a packet received by my Apache2 server (through a custom plugin perhaps), would it always be guaranteed to be accurate? Or is this value as easy to spoof as the referrer header?

My intended use case is to rate-limit unauthenticated API calls.

Matt Parkins
  • 24,208
  • 8
  • 50
  • 59

3 Answers3

1

It is not reliable. Not only because it can be spoofed, but also because a network element can make your server see a different IP address.

For example, it is very typical in a company to access the Internet through a proxy. Depending on the configuration, from your server point of view, all the different users come from the same IP address.

In any case is a filter you can use in many scenarios. For example, show a captcha when you detect too many login requests from the same IP address.

Guido
  • 46,642
  • 28
  • 120
  • 174
1

If it's a TCP packet, then it'll be accurate as to the sending host. IPs in TCP packets cannot be spoofed unless you've got control of the routers involved. With spoofed source packets, only the initial SYN packet will come back, and then the SYN+ACK response from the server will go to the spoofed address, not wherever the forgery came from - e.g. you cannot do the full 3-way handshake unless you can control packet routing from the targetted machine.

UDP packets, on the other hand, can be trivially forged and you cannot trust anything about them.

As well, even simple things like proxy servers and NAT gateways can cloak the 'real' ip from where the packet originated. You'll get an IP, but it'll be the IP of the gateway/proxy, not the original machine.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

If your intention is to rate-limit invalid API calls you might want to consider using a service like spamhaus. They list IP's that are likely bots and probes. There are other companies and lists as well. But if your intention is to ever ID the 'bad guy' the source IP is very unlikely to be correct.

WildBill
  • 9,143
  • 15
  • 63
  • 87