Hi i would know an information, I must develop an Android application, I 
need to sniff the packages that come from a certain connection.


I know that for this is it possible use tcpdump, but I can't use it because 
I need root permission from my device, I can't give to my device these 
permission. Exist some alternative that I can considered ?


I got to find out Vpn Service, but i don't understand if I can use it like 
tcpdump, for example if I can pass the same parameter that I use in a 
tcpdump command.

I need to obtain a .pcap or similar file with the result of the "sniffed" 
packed.


someone can help me ?


Actually i have seen different post on internet but nothing help me.





import android.content.Intent;
import android.net.VpnService;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

public class MyVpnService extends VpnService {

 private Thread mThread;
 private ParcelFileDescriptor mInterface;
 FileInputStream in;

 //a. Configure a builder for the interface.
 Builder builder = new Builder();

 // Services interface
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
 // Start a new session by creating a new thread.
 mThread = new Thread(new Runnable() {
 @Override
 public void run() {
 try {

 //a. Configure the TUN and get the interface.
 mInterface = builder.setSession("MyVPNService")
 .addAddress("216.58.210.195", 24)
 .addDnsServer("8.8.8.8")
 .addRoute("0.0.0.0", 0).establish();

 //b. Packets to be sent are queued in this input stream.
 in = new FileInputStream(mInterface.getFileDescriptor());

 //b. Packets received need to be written to this output stream.
 FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());

 //c. The UDP channel can be used to pass/get ip package to/from server
 DatagramChannel tunnel = DatagramChannel.open();

 // Connect to the remote server
 tunnel.connect(new InetSocketAddress("127.0.0.1", 8087));

 //d. Protect this socket, so package send by it will not be feedback to the 
vpn service.
 protect(tunnel.socket());


 //e. Use a loop to pass packets.
 ByteBuffer packet = ByteBuffer.allocate(32767);

 // We use a timer to determine the status of the tunnel. It
 // works on both sides. A positive value means sending, and
 // any other means receiving. We start with receiving.
 int timer = 0;

 File sdCard = Environment.getExternalStorageDirectory();
 File dir = new File (sdCard.getAbsolutePath() + "/MYDIR/");
 dir.mkdirs();
 File file = new File(dir, "report.txt");

 // We keep forwarding packets till something goes wrong.
 while (true) {
 // Assume that we did not make any progress in this iteration.
 boolean idle = true;
 // Read the outgoing packet from the input stream.
 int length = in.read(packet.array());
 if (length > 0) {
 // Write the outgoing packet to the tunnel.
 packet.limit(length);
 tunnel.write(packet);
 packet.clear();
 // There might be more outgoing packets.
 idle = false;
 // If we were receiving, switch to sending.
 if (timer < 1) {
 timer = 1;
 }
 }
 // Read the incoming packet from the tunnel.
 length = tunnel.read(packet);
 if (length > 0) {
 // Ignore control messages, which start with zero.
 if (packet.get(0) != 0) {
 // Write the incoming packet to the output stream.
 out.write(packet.array(), 0, length);
 // out = new FileOutputStream(file);

 System.out.println("TEST"+out.toString());
 }
 packet.clear();
 // There might be more incoming packets.
 idle = false;
 // If we were sending, switch to receiving.
 if (timer > 0) {
 timer = 0;
 }
 }
 // If we are idle or waiting for the network, sleep for a
 // fraction of time to avoid busy looping.
 if (idle) {
 Thread.sleep(100);
 // Increase the timer. This is inaccurate but good enough,
 // since everything is operated in non-blocking mode.
 timer += (timer > 0) ? 100 : -100;
 // We are receiving for a long time but not sending.
 if (timer < -15000) {
 // Send empty control messages.
 packet.put((byte) 0).limit(1);
 for (int i = 0; i < 3; ++i) {
 packet.position(0);
 tunnel.write(packet);
 }
 packet.clear();
 // Switch to sending.
 timer = 1;
 }
 // We are sending for a long time but not receiving.
 if (timer > 20000) {
 throw new IllegalStateException("Timed out");
 }
 }
 }
 } catch (Exception e) {
 // Catch any exception
 e.printStackTrace();
 } finally {
 try {
 if (mInterface != null) {
 mInterface.close();
 mInterface = null;
 }
 } catch (Exception e) {

 }
 }
 }

 }, "MyVpnRunnable");

 //start the service
 mThread.start();
 return START_STICKY;
 }

 @Override
 public void onDestroy() {
 // TODO Auto-generated method stub
 if (mThread != null) {
 mThread.interrupt();
 }
 super.onDestroy();
 }
}


This is one example of code, i dont understand: 1) How to get packet with 
VpnService and write a pcap file...

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/16ebea12-43ac-4305-a16b-ef25f5a4b1e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to