tcpdump工具可以在手机上进行抓包,具体步骤可以参考链接:tcpdump抓包。
本文章通过代码结合adb命令在Android手机端进行抓包,具体步骤如下:
1、新建Android工程,将tcpdump工具copy到assets文件中
2、将tcpdump写入到/mnt目录中
3、修改/mnt中tcpdump的权限
4、代码执行adb 命令,操作/mnt中tcpdump工具进行抓包
代码如下:
package com.demo.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
public class CommandsHelper {
private static final String NAME = "tcpdump";
private static final String TAG = "CommandsHelper";
public static final String DEST_FILE = "/mnt" + "/capture.pcap";
/**
* 开始抓包
* @param context
* @param tcpDumpFileName
*/
public static void startCapture(final Context context,final String tcpDumpFileName) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
boolean isCopySus = copyFileFromAssets(context, NAME, "/mnt/tcpdump");
if(isCopySus){
String[] commands = new String[5];
commands[0] = "adb shell";
commands[1] = "su";
commands[2] = "chmod 777 /mnt/tcpdump";
commands[3] = "cd /mnt";
commands[4] = "tcpdump -p -vv -s 0 -w " + "/mnt" + "/"+tcpDumpFileName+".pcap";
execCmd(commands);
}
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, " error: " + e.getMessage());
}
}
}).start();
}
public static void stopCapture() {
// 找出所有的带有tcpdump的进程
new Thread(new Runnable() {
@Override
public void run() {
String pid = getPidByPidName("tcpdump");
if(pid != null){
execCmd(new String[]{"adb shell","su","kill -9 "+pid}, true);
}
}
}).start();
}
public static String getPidByPidName(String PidName){
BufferedReader reader =null;
try{
Process process = Runtime.getRuntime().exec("ps | grep "+PidName);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = reader.readLine())!=null){
if(line.contains(PidName)){
String[] strs = line.split("\\s+");
return strs[1];
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
}
}
}
return null;
}
public static Process execCmd(String command) {
return execCmd(new String[] { command }, true);
}
public static Process execCmd(String[] commands) {
return execCmd(commands, true);
}
public static Process execCmd(String[] commands, boolean waitFor) {
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("sh");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
for (String cmd : commands) {
if (!TextUtils.isEmpty(cmd)) {
os.writeBytes(cmd + "\n");
}
}
os.flush();
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
if (waitFor) {
boolean retval = false;
try {
int suProcessRetval = suProcess.waitFor();
if (255 != suProcessRetval) {
retval = true;
} else {
retval = false;
}
} catch (Exception ex) {
Log.w("Error ejecutando el comando Root", ex);
}
}
return suProcess;
}
/**
* 从Assets中拷贝文件
* @param context
* @param fileName
* @param path
* @return
*/
public static boolean copyFileFromAssets(Context context, String fileName, String path) throws IOException{
boolean copyIsFinish = false;
InputStream is = context.getAssets().open(fileName);
File file = new File(path);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024];
int i = 0;
while ((i = is.read(temp)) > 0) {
fos.write(temp, 0, i);
}
fos.close();
is.close();
copyIsFinish = true;
return copyIsFinish;
}
}
执行代码中startCapture方法进行抓包,stopCapture方法停止抓包。
备注:在具备root权限下抓包可行。
运行有其他问题欢迎留言。
本文参考:http://blog.csdn.net/leehong2005/article/details/20538127