5

I know how to develop in Android and use the Apache HTTP lib, but I want to go lower, get hold of the wireless interface adapter and be able to send & receive packets wirelessly. I know its possible since there are sniffer apps in the android market. I have looked around and googled a lot, but can't seem to get any ideas on how to even start. Surprisingly nobody seems to have asked this question on SO before too.

I suppose there is no android API that does it for you. Please suggest a way of sending/receiving packets in android or post references to any such resources.

Thanks!

Note: I know java.net api can be used in android (though I haven't used it before) but that doesn't give me the kind of access I want.

Rushil Paul
  • 2,010
  • 4
  • 26
  • 39
  • What's the actual purpose here? – user207421 Oct 24 '11 at 00:46
  • I wanna create an app which can connect to BSNL (ISP) server through an access point which is in Bridge mode. apart from this, many other interesting things can be done. – Rushil Paul Oct 24 '11 at 10:12
  • @Rushil, what does bridge mode have to do with anything? Usually when people talk about "bridge mode" it's in the context of having a device which is NOT acting as an access point, whose purpose is to provide access to a wireless LAN via Ethernet. Are you talking about ad-hoc wireless? If so, see [this question](http://stackoverflow.com/questions/1932150/can-android-do-peer-to-peer-ad-hoc-networking). – mpontillo Oct 24 '11 at 16:25
  • @Mike, Sorry if I wasn't clear, but by Bridge mode I meant, that my router+modem (Access Point too) displays 4 options by which I could connect to the internet. One of them is Bridge mode. And when the modem is in bridge mode, I cannot connect to my ISP servers using my Android device. – Rushil Paul Oct 25 '11 at 00:27
  • Basically, PPPoE protocol is used for communication with BSNL here, and android doesn't have any implementation for it. I could provide the implementation provided I get hold of the wifi adapter. – Rushil Paul Oct 25 '11 at 00:35
  • @Rushil, the only way I can think of that bridge mode could be useful would be if you were using a WiFi tethering feature on your phone in order to turn it into a 3G (or similar) access point. Then you could use your WiFi router in "bridge mode" (and turn off its NAT capability) in order to use your phone's internet connection from a LAN. This would not require any programming to accomplish, so I'm not sure why you would need to know how to do packet-level networking in Android in order to accomplish this. ;-) – mpontillo Oct 25 '11 at 00:36
  • @Mike, its not only about the internet connectivity, you could do a lot of interesting stuff by capturing/sending packets on the network. Like implementing a WEP cracker or something.! – Rushil Paul Oct 25 '11 at 14:02
  • It's more of a useful general question. I'd like to look for the mac/bssid address of my stolen laptop. without root – kirill_igum Jun 08 '13 at 19:49
  • @Rushil , did you get any good solution for that ? – Saki Make Jun 10 '13 at 08:49

3 Answers3

2

There is no way to do this with the standard SDK/NDK.

On a rooted device, you could possibly call into tcpdump, or another libpcap-based application. There is not a supported way to give Java applications the required privileges on a production Android device.

For example, this blog post describes how to use tcpdump to do a basic packet capture on Android.

mpontillo
  • 13,559
  • 7
  • 62
  • 90
  • Thanks! that's a good start! But that blog post said that you will have to have the android device connected to a pc. Isn't it possible to create an app which is able to capture and send packets using libpcap.? I mean, on a rooted phone..the blog post didn't seem to say much. – Rushil Paul Oct 24 '11 at 09:59
  • 1
    @Rushil, there are a few reasons you need a PC connected. (1) to root the device, if it's not already rooted. (2) to push the tcpdump binary onto the phone. (3) to run the `adb shell` to run tcpdump from. On a rooted phone, you could write Java code to call into a custom built `libpcap`-based binary which you would have to design in order to get data back to your GUI in a format you could display. The point is, if your only goal is to see the packets coming across, all you have to do is root the phone, enable debugging, push tcpdump, run `adb shell` as root and start using `tcpdump`. – mpontillo Oct 24 '11 at 16:10
  • @Rushil, also, as I mentioned, even on a rooted device giving a Java application root privileges is not supported. The only half-way supported thing (by the community, see [androidsu](http://androidsu.com/superuser/)) is to launch an arbitrary native binary as root. So you could cross-compile a binary with a statically linked `libpcap` inside (similar to `tcpdump`) which you could call to do the work you wanted to do. For example, you could design the program to output XML format results on `stdout` and give it commands on `stdin`. – mpontillo Oct 24 '11 at 16:17
  • @Mike - maybe this is a silly question but I just want to be clear. In order to "launch a native binary as root", do you need to have a rooted phone? – brianestey Jun 08 '12 at 02:18
1

Android OS 4.0 or later provides a VpnService through which you can monitor the network traffic. Application provided at http://www.taosoftware.co.jp/en/android/packetcapture/ uses VpnService that capture the data.

saini
  • 233
  • 3
  • 10
  • Thanks! Actually I need to be able to "make" such tools, not use third party softwares. Still I will try that out :-) – Rushil Paul Jun 02 '12 at 15:14
  • @sandy - I've been looking at the VpnService but haven't been able to monitor the network traffic. It seems to be used to connect to a VPN, not to act as one. Have you had any luck doing this? – brianestey Jun 08 '12 at 02:20
  • @brianestey -I trying but still can't find any solution. – saini Jun 08 '12 at 04:50
1

Did you have a look at the NDK? Maybe you can do what you want in C(++).

Peterdk
  • 15,625
  • 20
  • 101
  • 140