Android运行cmd抓取tcpdump包并保存本地

  1 package com.alipay.net;
  2 
  3 /**
  4  * Created by xianyu.hxy on 2015/6/9.
  5  */
  6 
  7 import android.content.Context;
  8 import android.content.res.AssetManager;
  9 import android.os.Environment;
 10 import android.text.TextUtils;
 11 
 12 import java.io.*;
 13 
 14 public class CommandsHelper {
 15     private static final String NAME = "tcpdump";
 16     private static final String TAG = "CommandsHelper";
 17     public static final String DEST_FILE = Environment.getExternalStorageDirectory() + File.separator+"Alipay"+File.separator+"capture.pcap";
 18     public static final String DEST_FILE1="/sdcard/Alipay/capture.pcap";
 19     public static boolean startCapture(Context context) {
 20         InputStream is = null;
 21         OutputStream os = null;
 22         boolean retVal = false;
 23         try {
 24             AssetManager am = context.getAssets();
 25             is = am.open(NAME);
 26             File sdcardFile = Environment.getExternalStorageDirectory();
 27             File dstFile = new File(sdcardFile, NAME);
 28             os = new FileOutputStream(dstFile);
 29 
 30             copyStream(is, os);
 31 
 32             String[] commands = new String[7];
 33             commands[0] = "adb shell";
 34             commands[1] = "su";
 35             commands[2] = "cp -rf " + dstFile.toString() + " /data/local/tcpdump";
 36             commands[3] = "rm -r " + dstFile.toString();
 37             commands[4] = "chmod 777 /data/local/tcpdump";
 38             commands[5] ="cd /data/local";
 39             commands[6] = "./tcpdump -p -vv -s 0 -w " + DEST_FILE1;
 40 
 41             execCmd(commands);
 42         } catch (IOException e) {
 43             e.printStackTrace();
 44 
 45         } finally {
 46             closeSafely(is);
 47             closeSafely(os);
 48         }
 49 
 50         return retVal;
 51     }
 52 
 53     public static void stopCapture(Context context) {
 54         // 找出所有的带有tcpdump的进程
 55         String[] commands = new String[2];
 56         commands[0] = "adb shell";
 57         commands[1] = "ps|grep tcpdump|grep root|awk '{print $2}'";
 58         Process process = execCmd(commands);
 59         String result = parseInputStream(process.getInputStream());
 60         if (!TextUtils.isEmpty(result)) {
 61             String[] pids = result.split("
");
 62             if (null != pids) {
 63                 String[] killCmds = new String[pids.length];
 64                 for (int i = 0; i < pids.length; ++i) {
 65                     killCmds[i] = "kill -9 " + pids[i];
 66                 }
 67                 execCmd(killCmds);
 68             }
 69         }
 70     }
 71 
 72     public static Process execCmd(String command) {
 73         return execCmd(new String[] { command }, true);
 74     }
 75 
 76     public static Process execCmd(String[] commands) {
 77         return execCmd(commands, true);
 78     }
 79 
 80     public static Process execCmd(String[] commands, boolean waitFor) {
 81         Process suProcess = null;
 82         try {
 83             suProcess = Runtime.getRuntime().exec("su
");
 84 
 85             DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
 86             for (String cmd : commands) {
 87                 if (!TextUtils.isEmpty(cmd)) {
 88                     os.writeBytes(cmd + "
");
 89                 }
 90             }
 91             os.flush();
 92             os.writeBytes("exit
");
 93             os.flush();
 94         } catch (IOException e) {
 95             e.printStackTrace();
 96         }
 97 
 98         if (waitFor) {
 99             boolean retval = false;
100             try {
101                 int suProcessRetval = suProcess.waitFor();
102                 if (255 != suProcessRetval) {
103                     retval = true;
104                 } else {
105                     retval = false;
106                 }
107             } catch (Exception ex) {
108               //  Log.w("Error ejecutando el comando Root", ex);
109             }
110         }
111 
112         return suProcess;
113     }
114 
115     private static void copyStream(InputStream is, OutputStream os) {
116         final int BUFFER_SIZE = 1024;
117         try {
118             byte[] bytes = new byte[BUFFER_SIZE];
119             for (;;) {
120                 int count = is.read(bytes, 0, BUFFER_SIZE);
121                 if (count == -1) {
122                     break;
123                 }
124 
125                 os.write(bytes, 0, count);
126             }
127         } catch (IOException e) {
128             e.printStackTrace();
129         }
130     }
131 
132     private static void closeSafely(Closeable is) {
133         try {
134             if (null != is) {
135                 is.close();
136             }
137         } catch (IOException e) {
138             e.printStackTrace();
139         }
140     }
141 
142     private static String parseInputStream(InputStream is) {
143         InputStreamReader isr = new InputStreamReader(is);
144         BufferedReader br = new BufferedReader(isr);
145         String line = null;
146         StringBuilder sb = new StringBuilder();
147         try {
148             while ( (line = br.readLine()) != null) {
149                 sb.append(line).append("
");
150             }
151         } catch (IOException e) {
152             e.printStackTrace();
153         }
154 
155         return sb.toString();
156     }
157 }
Runtime.getRuntime().exec("su
");执行su的时候会弹出框;手机必须root;执行的tcpdump文件http://i.cnblogs.com/Files.aspx可下载。保存的.pcap文件可用wireshark分析。
非root情况下用fiddler只能获取http请求。
原文地址:https://www.cnblogs.com/hxy0107/p/4564103.html