最近做一个Android联网抓包的工具
自己在网上搜索了好久
发现还是没有头绪
于是考虑在linux层上下功夫
于是采用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命令
view plaincopy to clipboardprint?
01.tcpdump -p -vv -s 0 -w /sdcard/capture.pcap
tcpdump -p -vv -s 0 -w /sdcard/capture.pcap
这样就能够将抓到的包在sdcard上写一个capture.pcap文件
而这个文件可以用wireshark在PC上打开进行查看 非常方便
具体的源代码如下
MainHook.java
view plaincopy to clipboardprint?
01.package zy.hook;
02.import java.io.IOException;
03.import android.app.Activity;
04.import android.os.Bundle;
05.import android.util.Log;
06.public class MainHook extends Activity {
07. /** Called when the activity is first created. */
08. @Override
09. public void onCreate(Bundle savedInstanceState) {
10. super.onCreate(savedInstanceState);
11. setContentView(R.layout.main);
12. try {
13. //Runtime.getRuntime().exec("su");
14. //Runtime.getRuntime().exec("dd if=/sdcard/tcpdump of=/data/local/tcpdump");
15. //Runtime.getRuntime().exec("chmod 6755 /data/local/tcpdump");
16. Runtime.getRuntime().exec("tcpdump -p -vv -s 0 -w /sdcard/capture.pcap");
17. Log.i("run","success!!!!!!!!!");
18. } catch (IOException e) {
19. // TODO Auto-generated catch block
20. e.printStackTrace();
21. Log.i("run",e.toString());
22. }
23. }
24.}
package zy.hook;
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
view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03. package="zy.hook"
04. android:versionCode="1"
05. android:versionName="1.0">
06. <application android:icon="@drawable/icon" android:label="@string/app_name">
07. <activity android:name=".MainHook"
08. android:label="@string/app_name">
09. <intent-filter>
10. <action android:name="android.intent.action.MAIN" />
11. <category android:name="android.intent.category.LAUNCHER" />
12. </intent-filter>
13. </activity>
14. </application>
15. <uses-sdk android:minSdkVersion="7" />
16. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
17.</manifest>
<?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打开
可以非常方便的查看我们抓到的包
希望能够对大家有用
本文版权归csdn博主曾阳所有
http://blog.csdn.net/Zengyangtech/archive/2010/08/31/5853366.aspx