于是采用linux的tcpdump来实现了抓包的功能
用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具。
作为互联网上经典的的系统管理员必备工具,tcpdump以其强大的功能,灵活的截取策略,成为每个高级的系统管理员分析网络,排查问题等所必备的东东之一。
对于tcpdump详细的可以参考http://baike.baidu.com/view/76504.htm?fr=ala0_1_1百度百科的介绍
我这里主要介绍一下如何在Android上使用tcpdump
我所采用的测试机是破解版G2和模拟器
具体的就是在APK程序里执行linux命令
引用
tcpdump -p -vv -s 0 -w /sdcard/capture.pcap
这样就能够将抓到的包在sdcard上写一个capture.pcap文件
而这个文件可以用wireshark在PC上打开进行查看 非常方便
具体的源代码如下
MainHook.java
import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class MainHook extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { //Runtime.getRuntime().exec("su"); //Runtime.getRuntime().exec("dd if=/sdcard/tcpdump of=/data/local/tcpdump"); //Runtime.getRuntime().exec("chmod 6755 /data/local/tcpdump"); Runtime.getRuntime().exec("tcpdump -p -vv -s 0 -w /sdcard/capture.pcap"); Log.i("run","success!!!!!!!!!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("run",e.toString()); } } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zy.hook" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainHook" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest>
尤其不要忘记了权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
的声明
运行程序我们就可以抓包了
会在sdcard上生成capture.pcap文件
抓一段时间之后,将文件从sdcard上导出
然后我们用wireshark打开
可以非常方便的查看我们抓到的包
希望能够对大家有用