Desvío de paquetes VPN

Estoy trabajando en la creación de una VPN simulada (no crea realmente una conexión real con el servidor) para obtener todos los bytes de red entrantes y salientes (información).

Por ahora puedo obtener paquetes y analizarlos. Lo que obtienes por ejemplo:

IP Versión: 4 Longitud de la cabecera: 20 Longitud total: 60 Protocolo: 6 IP de origen: 10.0.2.0 IP de destino: 5.20.5.59 Nombre de host: clients4.google.com

Me gustaría saber qué y cómo debo hacer para hacer conexiones a sitios web / aplicaciones (Por ahora no se conecta).

En este sitio web: http://www.thegeekstuff.com/2014/06/android-vpn-service/ escribe que es necesario realizar estos pasos:

  1. Obtener paquete IP desde TUN. Igual que todo el servicio VPN.
  2. Extraiga la información de la capa 4. El tipo de protocolo (por ejemplo TCP / UDP) y su carga útil es una necesidad. Como hay un procedimiento de apretón de manos en TCP, antes de obtener realmente los datos de carga útil de él, necesitamos escribir de nuevo el paquete de apretón de manos primero.
  3. Elija el socket correspondiente para enviar la carga útil. Como este paso está trabajando en la capa 4, por lo que tenemos que guardar el socket y tratar de obtener datos de retorno más tarde. Si hay datos de devolución, necesitamos pasar estos paquetes a TUN.
  4. Obtenga el paquete del socket y construya un paquete de capa 3. Primero, necesitamos construir un paquete de capa 4 válido. UDP es un poco más fácil ya que el encabezado UDP de 4 bytes sólo contiene la dirección de origen, el puerto de origen, la dirección de destino y el puerto de destino. TCP es más complejo, ya que es un estado
  5. Conexión, el número de secuencia y el número de acuse de recibo deben ajustarse correctamente. A continuación, utilice el paquete de capa 4 como carga útil, necesitamos crear un paquete de capa 3 válido. Escribir el paquete IP de nuevo en TUN. Igual que todo el servicio VPN.

En 2 pasos obtengo información del paquete. Pero no deje de saber cómo se debe hacer más. Tal vez alguien me puede explicar en detalle. Además, tal vez pueda decirme cómo obtener los destinos de puerto cuando tengo su dirección IP. También hay el código:

public class VPN extends VpnService implements Handler.Callback, Runnable { private static final String TAG = "VpnService"; private String mServerAddress = "127.0.0.1"; private int mServerPort = 55555; private Handler mHandler; private Thread mThread; private ParcelFileDescriptor mInterface; @Override public int onStartCommand(Intent intent, int flags, int startId) { if (mHandler == null) { mHandler = new Handler(this); } if (mThread != null) { mThread.interrupt(); } mThread = new Thread(this, "VpnThread"); mThread.start(); return START_STICKY; } @Override public void onDestroy() { if (mThread != null) { mThread.interrupt(); } super.onDestroy(); } @Override public boolean handleMessage(Message message) { if (message != null) { Toast.makeText(this, (String) message.obj, Toast.LENGTH_SHORT).show(); } return true; } @Override public synchronized void run() { try { Log.i(TAG, "Starting"); InetSocketAddress server = new InetSocketAddress(mServerAddress, mServerPort); run(server); } catch (Exception e) { Log.e(TAG, "Got " + e.toString()); try { mInterface.close(); } catch (Exception e2) { } Message msgObj = mHandler.obtainMessage(); msgObj.obj = "Disconnected"; mHandler.sendMessage(msgObj); } finally { } } DatagramChannel mTunnel = null; protected boolean run(InetSocketAddress server) throws Exception { boolean connected = false; mTunnel = DatagramChannel.open(); if (!protect(mTunnel.socket())) { throw new IllegalStateException("Cannot protect the tunnel"); } mTunnel.connect(server); mTunnel.configureBlocking(false); handshake(); connected = true; Message msgObj = mHandler.obtainMessage(); msgObj.obj = "Connected"; mHandler.sendMessage(msgObj); new Thread () { public void run () { FileInputStream in = new FileInputStream(mInterface.getFileDescriptor()); ByteBuffer packet = ByteBuffer.allocate(32767); DatagramChannel tunnel = mTunnel; FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor()); int length; String destIP; try { while (true) { while ((length = in.read(packet.array())) > 0) { packet.limit(length); Log.d(TAG, "Total Length:" + mTunnel.socket().getInetAddress()); mTunnel.write(packet); packet.flip(); TCP_IP TCP_debug = new TCP_IP(packet); TCP_debug.debug(); destIP = TCP_debug.getDestination(); // InetAddress address = InetAddress.getByName(destIP); // System.out.println(address.getHostAddress()); // Gaunamas IP (185.11.24.36) // System.out.println(address.getHostName()); // www.15min.lt out.write(packet.array(), 0, length); packet.clear(); Thread.sleep(100); } } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }.start(); return connected; } private void makeConnection(String destination, int port) { try { run(new InetSocketAddress(destination, port)); } catch (Exception e) { Log.d(TAG, "klaida jungiantis"); } } private void handshake() throws Exception { if (mInterface == null) { Builder builder = new Builder(); //builder.setMtu(1500); //builder.addAddress("10.0.2.0", 32); // builder.addRoute("0.0.0.0", 0); builder.addAddress("192.168.0.1", 24); builder.addDnsServer("8.8.8.8"); builder.addRoute("0.0.0.0", 0); try { mInterface.close(); } catch (Exception e) { // ignore } mInterface = builder.setSession("VPN'as").establish(); } } } public class TCP_IP extends VPN { private ByteBuffer packet; private String hostname; private String destIP; private String sourceIP; private int version; private int protocol; private int port; public TCP_IP(ByteBuffer pack) { this.packet = pack; } public void debug() { int buffer = packet.get(); int headerlength; int temp; version = buffer >> 4; headerlength = buffer & 0x0F; headerlength *= 4; System.out.println("IP Version:"+version); System.out.println("Header Length:"+headerlength); String status = ""; status += "Header Length:"+headerlength; buffer = packet.get(); //DSCP + EN buffer = packet.getChar(); //Total Length System.out.println( "Total Length:"+buffer); buffer = packet.getChar(); //Identification buffer = packet.getChar(); //Flags + Fragment Offset buffer = packet.get(); //Time to Live buffer = packet.get(); //Protocol protocol = buffer; System.out.println( "Protocol:"+buffer); status += " Protocol:"+buffer; buffer = packet.getChar(); //Header checksum byte buff = (byte)buffer; sourceIP = ""; buff = packet.get(); //Source IP 1st Octet temp = ((int) buff) & 0xFF; sourceIP += temp; sourceIP += "."; buff = packet.get(); //Source IP 2nd Octet temp = ((int) buff) & 0xFF; sourceIP += temp; sourceIP += "."; buff = packet.get(); //Source IP 3rd Octet temp = ((int) buff) & 0xFF; sourceIP += temp; sourceIP += "."; buff = packet.get(); //Source IP 4th Octet temp = ((int) buff) & 0xFF; sourceIP += temp; System.out.println( "Source IP:"+sourceIP); status += " Source IP:"+sourceIP; destIP = ""; buff = packet.get(); //Destination IP 1st Octet temp = ((int) buff) & 0xFF; destIP += temp; destIP += "."; buff = packet.get(); //Destination IP 2nd Octet temp = ((int) buff) & 0xFF; destIP += temp; destIP += "."; buff = packet.get(); //Destination IP 3rd Octet temp = ((int) buff) & 0xFF; destIP += temp; destIP += "."; buff = packet.get(); //Destination IP 4th Octet temp = ((int) buff) & 0xFF; destIP += temp; System.out.println( "Destination IP:" + destIP); status += " Destination IP:"+destIP; } public String getDestination() { return destIP; } public int getProtocol() { return protocol; } public int getPort() { return port; } public String getHostname() { return hostname; } public int getIPversion() { return version; } } 

Cambio

 private String mServerAddress = "127.0.0.1"; 

a

 private String mServerAddress = "10.0.0.1"; 

No sé la respuesta y explicar lo que pasó todavía, pero vi en Android Monitor que no podemos usar localhost (127.0.0.1) en la dirección del servidor.

Hay otro problema, con SSL sobre HTTP (HTTPS). Si se conecta en un sitio como http://www.gorio.com.br , funcionará, pero si intenta https://www.google.com no funcionará.

  • Proteger un socket en VpnService
  • Android: No se puede conectar al servidor VPN a través de la aplicación ToyVpn; Obtener una excepción de puerto inalcanzable
  • Cómo fusionar la aplicación de cliente OpenVPN con otra aplicación para Android?
  • ¿Cómo utilizar VPN en Android?
  • Obtener el estado de la conexión VPN en Android
  • ¿Cómo configurar VPN de forma programática?
  • ¿Cómo encontrar el estado de la conexión VPN a través de API de marco o cualquier otro método eficiente?
  • Método para pasar nombre de usuario y contraseña en VpnService.Builder
  • Cómo conectarse a una VPN, que ya está configurada en un teléfono Android, mediante programación
  • Clase Android VpnService y su clase de constructor
  • VpnService Android 4.0
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.