android下的抓包+输入密码提醒

这个Android应用示例展示了如何使用TCPDump进行网络抓包,并在输出中搜索包含'password'的相关行,以提醒用户可能存在的密码输入情况。代码通过Runtime.exec()执行shell命令获取root权限并运行tcpdump。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.hackthon;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;



public class MainActivity extends Activity {

	 @Override   
	    public void onCreate(Bundle savedInstanceState) {   
	        super.onCreate(savedInstanceState);   
	        setContentView(R.layout.activity_main);
	        Log.i("run","success!"); 
	        //if (haveRoot())
	        //Log.i("run", "have root"); 
	        
	        try {   
	        	Log.i("run","success!!"); 
				//Runtime.getRuntime().exec("su");   
				//Runtime.getRuntime().exec("echo 'ssss'");
				//Runtime.getRuntime().exec("dd if=/sdcard/tcpdump of=/data/local/tcpdump");   
	            //Runtime.getRuntime().exec("chmod 6755 /data/local/tcpdump");   
				String output;
	            Process proc = Runtime.getRuntime().exec("su");
                DataOutputStream dataOutputStream = new DataOutputStream(proc.getOutputStream());
                dataOutputStream.writeBytes("tcpdump -s 0 -X -tt tcp port 80" + "\n");
                dataOutputStream.flush();
                //dataOutputStream.writeBytes("exit" + "\n");
                //dataOutputStream.flush();
                //proc.waitFor();
	        	//Log.i("run",getString(proc.exitValue())); 
                //dataOutputStream.close();
	            BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(proc.getInputStream()));
	            String out = "";
	            //String test=bufferedReader1.readLine();
	            //Log.i("run",test);
	        	Log.i("run","success!!!"); 
	            while (((output = bufferedReader1.readLine()) != null)) {
		        	//Log.i("run",out); 
	                if ((out.indexOf("password") != -1) || (out.indexOf("pwd") != -1) || (out.indexOf("PASSWORD") != -1) || (out.indexOf("Password") != -1) || (out.indexOf("PWD") != -1)){
	                	System.out.println("warning!");
	    	        	Log.i("run","success!!!!"); 
	                	Toast.makeText(this, "warning!", Toast.LENGTH_SHORT).show();
	                }
	      
	                if (output.startsWith("\t0x0000")) {
	                    out = output.substring(51);
	                }
	                else if (output.startsWith("\t"))
	                    out = out + output.substring(51);
	            }
	            Log.i("run","success!!!!!!!!!");  
	            Toast.makeText(this, "success!!!!!!!!!", Toast.LENGTH_SHORT).show();
	        } catch (IOException e) {   
	            // TODO Auto-generated catch block   
	            e.printStackTrace();   
	           Log.i("run",e.toString());  
	           
	        }   
	    }   
	 private static boolean mHaveRoot = false;

	    public static boolean haveRoot() {
	        if (!mHaveRoot) {
	            int ret = execRootCmdSilent("echo test");
	            if (ret != 1) {
	            	Log.i("run","have root!");
	                mHaveRoot = true;
	            } else {
	            	Log.i("run","not root!");
	            }
	        } else {
	        	Log.i("run","mHaveRoot = true, have root!");
	        }
	        return mHaveRoot;
	    }

	    @SuppressWarnings("deprecation")
	    public static String execRootCmd(String cmd) {
	        String result = "";
	        DataOutputStream dos = null;
	        DataInputStream dis = null;

	        try {
	            Process p = Runtime.getRuntime().exec("su");
	            dos = new DataOutputStream(p.getOutputStream());
	            dis = new DataInputStream(p.getInputStream());
	            Log.i("run",cmd);

	            dos.writeBytes(cmd + "\n");
	            dos.flush();
	            dos.writeBytes("exit\n");
	            dos.flush();

	            String line = null;
	            while ((line = dis.readLine()) != null) {
	                result += line;
	            }
	            p.waitFor();
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            if (dos != null) {
	                try {
	                    dos.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if (dis != null) {
	                try {
	                    dis.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	        return result;

	    }

	    public static int execRootCmdSilent(String cmd) {
	        int result = -1;
	        DataOutputStream dos = null;

	        try {
	            Process p = Runtime.getRuntime().exec("su");
	            Log.i("run", "root!!!!!!");
	            dos = new DataOutputStream(p.getOutputStream());
	            Log.i("run",cmd);

	            dos.writeBytes(cmd + "\n");
	            dos.flush();
	            dos.writeBytes("exit\n");
	            dos.flush();
	            p.waitFor();
	            result = p.exitValue();
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            if (dos != null) {
	                try {
	                    dos.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	        return result;
	    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值