tcpdump 抓包

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值