Android运行cmd抓取tcpdump包并保存本地
2015-06-09 17:26
393 查看
package com.alipay.net; /** * Created by xianyu.hxy on 2015/6/9. */ import android.content.Context; import android.content.res.AssetManager; import android.os.Environment; import android.text.TextUtils; import java.io.*; public class CommandsHelper { private static final String NAME = "tcpdump"; private static final String TAG = "CommandsHelper"; public static final String DEST_FILE = Environment.getExternalStorageDirectory() + File.separator+"Alipay"+File.separator+"capture.pcap"; public static final String DEST_FILE1="/sdcard/Alipay/capture.pcap"; public static boolean startCapture(Context context) { InputStream is = null; OutputStream os = null; boolean retVal = false; try { AssetManager am = context.getAssets(); is = am.open(NAME); File sdcardFile = Environment.getExternalStorageDirectory(); File dstFile = new File(sdcardFile, NAME); os = new FileOutputStream(dstFile); copyStream(is, os); String[] commands = new String[7]; commands[0] = "adb shell"; commands[1] = "su"; commands[2] = "cp -rf " + dstFile.toString() + " /data/local/tcpdump"; commands[3] = "rm -r " + dstFile.toString(); commands[4] = "chmod 777 /data/local/tcpdump"; commands[5] ="cd /data/local"; commands[6] = "./tcpdump -p -vv -s 0 -w " + DEST_FILE1; execCmd(commands); } catch (IOException e) { e.printStackTrace(); } finally { closeSafely(is); closeSafely(os); } return retVal; } public static void stopCapture(Context context) { // 找出所有的带有tcpdump的进程 String[] commands = new String[2]; commands[0] = "adb shell"; commands[1] = "ps|grep tcpdump|grep root|awk '{print $2}'"; Process process = execCmd(commands); String result = parseInputStream(process.getInputStream()); if (!TextUtils.isEmpty(result)) { String[] pids = result.split("\n"); if (null != pids) { String[] killCmds = new String[pids.length]; for (int i = 0; i < pids.length; ++i) { killCmds[i] = "kill -9 " + pids[i]; } execCmd(killCmds); } } } 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("su\n"); 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; } private static void copyStream(InputStream is, OutputStream os) { final int BUFFER_SIZE = 1024; try { byte[] bytes = new byte[BUFFER_SIZE]; for (;;) { int count = is.read(bytes, 0, BUFFER_SIZE); if (count == -1) { break; } os.write(bytes, 0, count); } } catch (IOException e) { e.printStackTrace(); } } private static void closeSafely(Closeable is) { try { if (null != is) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } private static String parseInputStream(InputStream is) { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; StringBuilder sb = new StringBuilder(); try { while ( (line = br.readLine()) != null) { sb.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } }
Runtime.getRuntime().exec("su\n");执行su的时候会弹出框;手机必须root;执行的tcpdump文件http://i.cnblogs.com/Files.aspx可下载。保存的.pcap文件可用wireshark分析。 非root情况下用fiddler只能获取http请求。
相关文章推荐
- android 异步加载网络图片
- android-async-http框架之与服务器进行数据交互
- Spring HTTP Invoker 学习小记
- Spring HTTP invoker简介
- Android 关于异步Http请求,以及编码问题
- wireshark如何过滤 http数据包
- Java Socket发送与接收HTTP消息简单实现
- nginx 同一个IP上配置多个HTTPS主机
- Linux(Ubuntu)的网络配置
- [Unity3D ARPG网络游戏编程实践]1——初识unity网络编程
- nginx+tcp
- 构建网络爬虫?so easy
- Linux常用网络工具:批量主机服务扫描之netcat
- 《 Web应用测试 》笔记(第4 章-网络基础)
- 【转】浅谈HTTP中Get与Post的区别
- Http协议
- 网络编程释疑之:TCP半开连接的处理(好)
- HttpURLConnection(一)
- http状态码
- 那些年我们踩到过的坑(二):3.1 版 MultiThreadedHttpConnectionManager 未releaseConnection导致应用服务器宕机