I am working on sniffing packages from an android android application. I want to use the data in my research on user behavior on social networks.
What I learned in the last two months is that I need to create a vpn service and use it to sniff data. There are many useful codes on the Internet that I tried to understand and run. And I managed to sniff the data of some applications using these codes that use the same concept.
Using each of the above code and with minor changes, I managed to capture packages for all applications, but with applications such as facebook, vpnService made these applications stop working; when I create a vpn service, and I try, for example, to comment on a message or to take any action that requires sending / receiving data between the Facebook application and the Facebook server, this action will not happen; it seems like somehow the Facebook application knows that someone is sniffing it there.
Over the past two months, I have been trying to find a way to capture Facebook application packages without breaking the functionality of the Facebook application.
I tried installing the certificate following the KeyChain demo in android samples, but this did not work with me.
/ , - ; , , , , facebook. LostNetNoRootFirewall google play.
LostNetNoRootFirewall vpn sniff, facebook !
, vpnService , facebook, , ?
, . [ ]
package com.git.firewall;
public class GITVpnService extends VpnService implements Handler.Callback, Runnable {
private static final String TAG = "GITVpnService";
private String mServerAddress = "127.0.0.1";
private int mServerPort = 55555;
private PendingIntent mConfigureIntent;
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();
}
}
@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;
private boolean run(InetSocketAddress server) throws Exception {
boolean connected = false;
android.os.Debug.waitForDebugger();
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);
int length;
try
{
while (true)
{
while ((length = in.read(packet.array())) > 0) {
packet.limit(length);
debugPacket(packet);
mTunnel.write(packet);
packet.clear();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start();
new Thread ()
{
public void run ()
{
DatagramChannel tunnel = mTunnel;
ByteBuffer packet = ByteBuffer.allocate(8096);
FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());
while (true)
{
try
{
int length;
while ((length = tunnel.read(packet)) > 0)
{
out.write(packet.array(), 0, length);
packet.clear();
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
}.start();
return connected;
}
private void handshake() throws Exception {
if (mInterface == null)
{
Builder builder = new Builder();
builder.setMtu(1500);
builder.addAddress("10.0.0.2",32);
builder.addRoute("0.0.0.0", 0);
try {
mInterface.close();
} catch (Exception e) {
}
mInterface = builder.setSession("GIT VPN")
.setConfigureIntent(mConfigureIntent)
.establish();
}
}
private void debugPacket(ByteBuffer packet)
{
int buffer = packet.get();
int version;
int headerlength;
version = buffer >> 4;
headerlength = buffer & 0x0F;
headerlength *= 4;
Log.d(TAG, "IP Version:"+version);
Log.d(TAG, "Header Length:"+headerlength);
String status = "";
status += "Header Length:"+headerlength;
buffer = packet.get();
buffer = packet.getChar();
Log.d(TAG, "Total Length:"+buffer);
buffer = packet.getChar();
buffer = packet.getChar();
buffer = packet.get();
buffer = packet.get();
Log.d(TAG, "Protocol:"+buffer);
status += " Protocol:"+buffer;
buffer = packet.getChar();
String sourceIP = "";
buffer = packet.get();
sourceIP += buffer;
sourceIP += ".";
buffer = packet.get();
sourceIP += buffer;
sourceIP += ".";
buffer = packet.get();
sourceIP += buffer;
sourceIP += ".";
buffer = packet.get();
sourceIP += buffer;
Log.d(TAG, "Source IP:"+sourceIP);
status += " Source IP:"+sourceIP;
String destIP = "";
buffer = packet.get();
destIP += buffer;
destIP += ".";
buffer = packet.get();
destIP += buffer;
destIP += ".";
buffer = packet.get();
destIP += buffer;
destIP += ".";
buffer = packet.get();
destIP += buffer;
Log.d(TAG, "Destination IP:"+destIP);
status += " Destination IP:"+destIP;
}
}