大談android安全2——Activity劫持的防范程序
上篇在里面介紹了由于Android設(shè)計(jì)上的缺陷而導(dǎo)致的釣魚漏洞,并且也在文末介紹了用戶防范的方法。
然而,如果真的爆發(fā)了這種惡意程序,我們并不能在啟動(dòng)程序時(shí)每一次都那么小心去查看判斷當(dāng)前在運(yùn)行的是哪一個(gè)程序。因此,前幾個(gè)星期花了一點(diǎn)時(shí)間寫了一個(gè)程序,叫反劫持助手。原理很簡(jiǎn)單,就是獲取當(dāng)前運(yùn)行的是哪一個(gè)程序,并且顯示在一個(gè)浮動(dòng)窗口中,以幫忙用戶判斷當(dāng)前運(yùn)行的是哪一個(gè)程序,防范一些釣魚程序的欺騙。
在這一次,由于是“正當(dāng)防衛(wèi)”,就不再通過枚舉來獲取當(dāng)前運(yùn)行的程序了,在manifest文件中增加一個(gè)權(quán)限:
- <uses-permission android:name="android.permission.GET_TASKS" />
然后啟動(dòng)程序的時(shí)候,啟動(dòng)一個(gè)Service,在Service中啟動(dòng)一個(gè)浮動(dòng)窗口,并周期性檢測(cè)當(dāng)前運(yùn)行的是哪一個(gè)程序,然后顯示在浮動(dòng)窗口中。
程序截圖如下:
其中Service代碼如下:
- /*
- * @(#)AntiService.java Project:ActivityHijackingDemo
- * Date:2012-9-13
- *
- * Copyright (c) 2011 CFuture09, Institute of Software,
- * Guangdong Ocean University, Zhanjiang, GuangDong, China.
- * All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.sinaapp.msdxblog.antihijacking.service;
- import android.app.ActivityManager;
- import android.app.Notification;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.util.Log;
- import com.sinaapp.msdxblog.androidkit.thread.HandlerFactory;
- import com.sinaapp.msdxblog.antihijacking.AntiConstants;
- import com.sinaapp.msdxblog.antihijacking.view.AntiView;
- /**
- * @author Geek_Soledad (66704238@51uc.com)
- */
- public class AntiService extends Service {
- private boolean shouldLoop = false;
- private Handler handler;
- private ActivityManager am;
- private PackageManager pm;
- private Handler mainHandler;
- private AntiView mAntiView;
- private int circle = 2000;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onStart(Intent intent, int startId) {
- super.onStart(intent, startId);
- startForeground(19901008, new Notification());
- if (intent != null) {
- circle = intent.getIntExtra(AntiConstants.CIRCLE, 2000);
- }
- Log.i("circle", circle + "ms");
- if (true == shouldLoop) {
- return;
- }
- mAntiView = new AntiView(this);
- mainHandler = new Handler() {
- public void handleMessage(Message msg) {
- String name = msg.getData().getString("name");
- mAntiView.setText(name);
- };
- };
- pm = getPackageManager();
- shouldLoop = true;
- am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
- handler = new Handler(
- HandlerFactory.getHandlerLooperInOtherThread("anti")) {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- String packageName = am.getRunningTasks(1).get(0).topActivity
- .getPackageName();
- try {
- String progressName = pm.getApplicationLabel(
- pm.getApplicationInfo(packageName,
- PackageManager.GET_META_DATA)).toString();
- updateText(progressName);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- if (shouldLoop) {
- handler.sendEmptyMessageDelayed(0, circle);
- }
- }
- };
- handler.sendEmptyMessage(0);
- }
- private void updateText(String name) {
- Message message = new Message();
- Bundle data = new Bundle();
- data.putString("name", name);
- message.setData(data);
- mainHandler.sendMessage(message);
- }
- @Override
- public void onDestroy() {
- shouldLoop = false;
- mAntiView.remove();
- super.onDestroy();
- }
- }
浮動(dòng)窗口僅為一個(gè)簡(jiǎn)單的textview,非此次的技術(shù)重點(diǎn),在這里省略不講。
當(dāng)然,從以上代碼也可以看出本程序只能防范通過Activity作為釣魚界面的程序,因?yàn)樗峭ㄟ^運(yùn)行的頂層的Activity來獲取程序名稱的,對(duì)WooYun最近提到的另一個(gè)釣魚方法它還是無能為力的,關(guān)于這一點(diǎn)將在下次談。