android安全——Activity劫持的防范程序

如果真的爆发了这种恶意程序,我们并不能在启动程序时每一次都那么小心去查看判断当前在运行的是哪一个程序。因此,前几个星期花了一点时间写了一个程序,叫反劫持助手。原理很简单,就是获取当前运行的是哪一个程序,并且显示在一个浮动窗口中,以帮忙用户判断当前运行的是哪一个程序,防范一些钓鱼程序的欺骗。 

在这一次,由于是“正当防卫”,就不再通过枚举来获取当前运行的程序了,在manifest文件中增加一个权限: 
Xml代码  收藏代码
  1. <uses-permission android:name="android.permission.GET_TASKS" />  

然后启动程序的时候,启动一个Service,在Service中启动一个浮动窗口,并周期性检测当前运行的是哪一个程序,然后显示在浮动窗口中。 
程序截图如下: 

 

其中Service代码如下: 
Java代码  收藏代码
  1. /* 
  2.  * @(#)AntiService.java            Project:ActivityHijackingDemo 
  3.  * Date:2012-9-13 
  4.  * 
  5.  * Copyright (c) 2011 CFuture09, Institute of Software,  
  6.  * Guangdong Ocean University, Zhanjiang, GuangDong, China. 
  7.  * All rights reserved. 
  8.  * 
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  10.  *  you may not use this file except in compliance with the License. 
  11.  * You may obtain a copy of the License at 
  12.  * 
  13.  *     http://www.apache.org/licenses/LICENSE-2.0 
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing, software 
  16.  * distributed under the License is distributed on an "AS IS" BASIS, 
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  18.  * See the License for the specific language governing permissions and 
  19.  * limitations under the License. 
  20.  */  
  21. package com.sinaapp.msdxblog.antihijacking.service;  
  22.   
  23. import android.app.ActivityManager;  
  24. import android.app.Notification;  
  25. import android.app.Service;  
  26. import android.content.Context;  
  27. import android.content.Intent;  
  28. import android.content.pm.PackageManager;  
  29. import android.content.pm.PackageManager.NameNotFoundException;  
  30. import android.os.Bundle;  
  31. import android.os.Handler;  
  32. import android.os.IBinder;  
  33. import android.os.Message;  
  34. import android.util.Log;  
  35.   
  36. import com.sinaapp.msdxblog.androidkit.thread.HandlerFactory;  
  37. import com.sinaapp.msdxblog.antihijacking.AntiConstants;  
  38. import com.sinaapp.msdxblog.antihijacking.view.AntiView;  
  39.   
  40. /** 
  41.  * @author Geek_Soledad (66704238@51uc.com) 
  42.  */  
  43. public class AntiService extends Service {  
  44.   
  45.     private boolean shouldLoop = false;  
  46.     private Handler handler;  
  47.     private ActivityManager am;  
  48.     private PackageManager pm;  
  49.     private Handler mainHandler;  
  50.     private AntiView mAntiView;  
  51.     private int circle = 2000;  
  52.   
  53.     @Override  
  54.     public IBinder onBind(Intent intent) {  
  55.         return null;  
  56.     }  
  57.   
  58.     @Override  
  59.     public void onStart(Intent intent, int startId) {  
  60.         super.onStart(intent, startId);  
  61.         startForeground(19901008new Notification());  
  62.         if (intent != null) {  
  63.              circle = intent.getIntExtra(AntiConstants.CIRCLE, 2000);  
  64.         }   
  65.         Log.i("circle", circle + "ms");  
  66.         if (true == shouldLoop) {  
  67.             return;  
  68.         }  
  69.         mAntiView = new AntiView(this);  
  70.         mainHandler = new Handler() {  
  71.             public void handleMessage(Message msg) {  
  72.                 String name = msg.getData().getString("name");  
  73.                 mAntiView.setText(name);  
  74.             };  
  75.         };  
  76.         pm = getPackageManager();  
  77.         shouldLoop = true;  
  78.         am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
  79.         handler = new Handler(  
  80.                 HandlerFactory.getHandlerLooperInOtherThread("anti")) {  
  81.             @Override  
  82.             public void handleMessage(Message msg) {  
  83.                 super.handleMessage(msg);  
  84.                 String packageName = am.getRunningTasks(1).get(0).topActivity  
  85.                         .getPackageName();  
  86.                 try {  
  87.                     String progressName = pm.getApplicationLabel(  
  88.                             pm.getApplicationInfo(packageName,  
  89.                                     PackageManager.GET_META_DATA)).toString();  
  90.                     updateText(progressName);  
  91.                 } catch (NameNotFoundException e) {  
  92.                     e.printStackTrace();  
  93.                 }  
  94.   
  95.                 if (shouldLoop) {  
  96.                     handler.sendEmptyMessageDelayed(0, circle);  
  97.                 }  
  98.             }  
  99.         };  
  100.         handler.sendEmptyMessage(0);  
  101.     }  
  102.   
  103.     private void updateText(String name) {  
  104.         Message message = new Message();  
  105.         Bundle data = new Bundle();  
  106.         data.putString("name", name);  
  107.         message.setData(data);  
  108.         mainHandler.sendMessage(message);  
  109.     }  
  110.   
  111.     @Override  
  112.     public void onDestroy() {  
  113.         shouldLoop = false;  
  114.         mAntiView.remove();  
  115.         super.onDestroy();  
  116.     }  
  117.   
  118. }  


浮动窗口仅为一个简单的textview,非此次的技术重点,在这里省略不讲。 
当然,从以上代码也可以看出本程序只能防范通过Activity作为钓鱼界面的程序,因为它是通过运行的顶层的Activity来获取程序名称的,对WooYun最近提到的另一个钓鱼方法它还是无能为力的,关于这一点将在下次谈。 
转自: http://maosidiaoxian.iteye.com/blog/1683990
原文地址:https://www.cnblogs.com/hzcya1995/p/13317935.html