How can I change IP in HttpServletRequest from client side?

1.8k Views Asked by At

server code:

  String  ip =  request.getRemoteAddr()
     if(ip='127.0.0.1')
        System.out.print("hello");

Now I am accessing that remote site from my machine, so obvious my IP address should be like 192.*.*.*.

How can I cheat the server(IP spoofing) so server always prints "hello" for my request?

1

There are 1 best solutions below

5
On

New answer to edited question: You can't in Java. If need to pretend that the request is coming from 127.0.0.1 (the server itself), so you'll need to hack into the network stack of your operating system.

Old answer: The IP Address your client uses to connect to the server depends on the network interface it uses and the kind of network attached to this network interface. Example: If your client is a laptop it most likely has only one network interface. This network interface uses 192.168.1.10 as its IP address (e.g., assigned from the DHCP on your router) as its internal IP address. Your router might also be connected to the internet, with an IP, say 20.20.20.20, which it shares with connected devices via NAT. If you use this to connect to your server which is on your local network, the client's IP address that the server sees will be 192.168.1.10; if you connect to your server which is not on your local network but somewhere on the internet, your client's IP (that the server sees) will be 20.20.20.20

So you cannot make your client pretend to use 127.0.0.1 (if server and client are running on the same machine, your client will most likely have 127.0.0.1). Of course there are techniques like IP spoofing where you pretend to have a different IP than you actually have, but that's totally different issue.