2

I need to set source IP address (for something like IP Spoofing) before sending out an http request. Class used for setting up http connection is HTTPURLConnection. I found below link on stackoverflow which is really useful.

Registering and using a custom java.net.URL protocol

As in the post, I have already created 3 classes extending URLConnection , URLStreamHandler and implementing URLStreamHandlerFactory. This looks to be working fine; however I am getting exception which I think is because I have not implemented getInputStream for URLConnection as was mentioned in above post.

I have couple of questions
1> I am extending custom URLConnection class from HTTPURLConnection, so what's the need of implementing getInputStream as anyway it's a virtual method
2> If I have to do it, can someone provide sample implementation of this method?

Community
  • 1
  • 1
theHeman
  • 505
  • 1
  • 6
  • 26
  • I don't think this approach is going to work. The source IP address that you are trying to spoof is actually the source IP address from the TCP/IP layer. To spoof that, you are going to have to do >>real<< IP spoofing, and you can't do that with a regular Java Socket, or anything build ontop of one. – Stephen C May 30 '16 at 14:36
  • @StephenC well you can have interfaces with multiple ips .. so you don't have to spoof ;) or have multiple interfaces with different ips – Zarathustra May 30 '16 at 14:42
  • @Zarathustra We need this in the context of JMeter. It does support IP spoofing, but only for HTTP Requests with HTTPClient implementation; (http://jmeter.apache.org/usermanual/component_reference.html) however we need to support it for HTTP Requests with Java implementation; as it is not available OOTB, we are trying to modify its source code. – theHeman May 30 '16 at 15:07
  • @ashhem you should note that in your question. I am pretty sure that you can do some proxy magic as well in jmeter – Zarathustra May 30 '16 at 15:34

3 Answers3

2

JMeter already provides the IP Spoofing feature.

In Http Request Defaults, select (in version 3.0 of JMeter) advanced tab :

enter image description here

See http://jmeter.apache.org/usermanual/component_reference.html#HTTP_Request_parms1:

Source address field [Only for HTTP Request with HTTPClient implementation] This property is used to enable IP Spoofing. It overrides the default local IP address for this sample. The JMeter host must have multiple IP addresses (i.e. IP aliases, network interfaces, devices). The value can be a host name, IP address, or a network interface device such as "eth0" or "lo" or "wlan0". If the property httpclient.localaddress is defined, that is used for all HttpClient requests.

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
  • As I had mentioned in my previous comment, It does support IP spoofing, but only for HTTP Requests with HTTPClient implementation; however we need to support it for HTTP Requests with Java implementation. – theHeman May 31 '16 at 06:01
0

Based on UBIK LOAD PACK's answer. Here is the same code using Aapche http client 3.x. (The variable for setting source IP is args[1])

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.protocol.Protocol;

public class HC3Test {  
  public static void main(String[] args) throws Exception {
    String url = args[0];
    java.net.URL uri = new java.net.URL(url);
    HostConfiguration hc = new HostConfiguration();
    hc.setHost(uri.getHost(), uri.getPort(), Protocol.getProtocol(uri.getProtocol()));
    hc.setLocalAddress(java.net.InetAddress.getByName(args[1]));//for pseudo 'ip spoofing'

    HttpClient client = new HttpClient(new SimpleHttpConnectionManager());
    client.setHostConfiguration(hc);
    GetMethod method = new GetMethod(url);
    client.executeMethod(method);
    method.releaseConnection();
  }
}

Sample code for http client 4.x:

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;

public class HC4Test {
  public static void main(String[] args) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    org.apache.http.params.HttpParams params = httpClient.getParams();
    params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, 
       java.net.InetAddress.getByName(args[1]));//for pseudo 'ip spoofing'
    httpClient.execute(new HttpGet(args[0]));
  }
}
Beck Yang
  • 3,004
  • 2
  • 21
  • 26
-1

To accomplish to have a different source address via an http request you could use a local proxy.

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;

public class Huhu {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://google.com");
Proxy proxy = new Proxy(Proxy.Type.DIRECT,
    new InetSocketAddress(
        InetAddress.getByAddress(
                new byte[]{your, ip, interface, here}), yourTcpPortHere));
URLConnection conn = url.openConnection(proxy);
    }
}

You do not have to override anything this way.

Zarathustra
  • 2,853
  • 4
  • 33
  • 62
  • When I try this code, I get below error java.lang.IllegalArgumentException: type DIRECT is not compatible with address /10.41.16.41:8888. I used 10.41.16.41 as ip address (wireless NIC) and 8888 as. – theHeman May 31 '16 at 06:21