android應用安全——元件通訊安全(Intent)

技術小胖子發表於2017-11-01

    這裡主要涉及到了Activity、Content Provider、Service、Broadcast Receiver等。這些如果在Androidmanifest.xml配置不當,會被其他應用呼叫,引起風險。android應用內部的Activity、Service、Broadcast Receiver等,他們通過Intent通訊,元件間需要通訊就需要在Androidmanifest.xml檔案中暴露元件,前面提到的風險就有可能是不恰當的元件暴露引起的。

     一、Intent基礎知識

Intent啟動不同元件的方法如下:         

元件名稱

方法名稱

Activity                                                                         

startActivity()

startActivityForResult()                                                                                                                                               

Service

startService()

bindService()

Broadcasts

sendBroadcast()

sendOrderedBroadcast()

sendStickyBroadcast()

 

     Intent的兩種基本用法:一種是顯式的Intent,即在構造Intent物件時就指定接收者;


另一種是隱式的Intent,即Intent的傳送者在構造Intent物件時,並不知道也不關心接收


者是誰,有利於降低傳送者和接收者之間的耦合。


     顯示呼叫例子:


Intent intent =  new  Intent(); intent.setClassName( "com.samples.intent.simple" ,                       "com.samples.intent.simple.TestActivity" ); startActivity(intent);  Intent intent =  new  Intent(A.activity,B.class); startActivity(intent);




     隱式呼叫例子


Intent intent =  new  Intent(Intent. ACTION_DIAL ); startActivity(intent); Intent intent =  new  Intent("com.test.broadcast"); intent.putString("PASSWORD","123456");  sendBroadcast(intent); Intent intent =  new  Intent("com.test.service"); intent.putString("USERNAME","test");  startService(intent);



  顯示呼叫和隱式呼叫都能過在不同應用間傳遞資料。


二、可能產生的風險:

       

        1、惡意呼叫


          2、惡意接受資料


          3、仿冒應用,例如(惡意釣魚,啟動登入介面)


          4、惡意傳送廣播、啟動應用服務。


          5、呼叫元件,接受元件返回的資料


       6、攔截有序廣播


         上面也是想到了一部分,應用中應該會有更多的例子。


三、怎樣避歸風險:

       

       1、最小化元件暴露

       不參與跨應用呼叫的元件新增android:exported=”false”屬性,這個屬性說明它是私有的,只有同一個應用程式的元件或帶有相同使用者ID的應用程式才能啟動或繫結該服務。

      

  <activity             android:name=".LoginActivity"             android:label="@string/app_name"             android:screenOrientation="portrait"              android:exported="false">




       2、設定元件訪問許可權


        參與跨應用呼叫的元件或者公開的廣播、服務設定許可權。設定許可權如下:


    (1)元件新增android:permission屬性。


<activity android:name=".Another" android:label="@string/app_name"            android:permission="com.test.custempermission">   </activity>



       (2)宣告< permission>屬性

<permission android:description="test"           android:label="test"           android:name="com.test.custempermission"           android:protectionLevel="normal">       </permission>


     protectionLevel有四種級別normal、dangerous、signature、signatureOrSystem。signature、signatureOrSystem時,只有相同簽名時才能呼叫。


       (3)呼叫元件者宣告<uses-permission>


<uses-permission android:name="com.test.custempermission" />




       3、暴露元件的程式碼檢查


        Android 提供各種 API 來在執行時檢查、執行、授予和撤銷許可權。這些 API


 是 android.content.Context 類的一部分,這個類提供有關應用程式環境的全域性資訊。

if (context.checkCallingOrSelfPermission("com.test.custempermission")         != PackageManager.PERMISSION_GRANTED) {             // The Application requires permission to access the               // Internet"); } else {     // OK to access the Internet }



/**
* @author 張興業
*  http://blog.csdn.net/xyz_lmn
*  android開發進階群:241395671
*/



        

參考:android安全-intent

               Android 安全架構及許可權控制機制剖析

http://www.ibm.com/developerworks/cn/opensource/os-cn-android-sec/




     本文轉自xyz_lmn51CTO部落格,原文連結:http://blog.51cto.com/xyzlmn/1230744,如需轉載請自行聯絡原作者



相關文章