顯示具有 DIVA 標籤的文章。 顯示所有文章
顯示具有 DIVA 標籤的文章。 顯示所有文章

2016年8月30日 星期二

DIVA Android - 13.Input Validation Issues – Part 3

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
DIVA Android - 6.Insecure Data Storage – Part 4
DIVA Android - 7.Input Validation Issues – Part 1
DIVA Android - 8.Input Validation Issues – Part 2
DIVA Android - 9.Access Control Issues – Part 1
DIVA Android - 10.Access Control Issues – Part 2
DIVA Android - 11.Access Control Issues – Part 3
DIVA Android - 12.Hardcoding Issues – Part 2
DIVA Android - 13.Input Validation Issues – Part 3

「13.Input Validation Issues – Part 3」對應到的 Activity 是 InputValidation3Activity,看一下 code 長這樣:



一樣追 DivaJni:



JNI 機制上一篇有稍微提到就不多說了,這次追 native 變數 initiateLaunchSequence。

接著看 divajni.c:



弱點在於 strcpy 不會做字串長度的檢查,而前面的變數 CODESIZEMAX 值為 20,一但使用者輸入值超過 20,便可能造成 buffer overflow。

防範方法:

(1)限制使用者輸入的字串長度。

(2)使用安全的函數,參考 停止使用 strncpy 函数!

DIVA Android - 12.Hardcoding Issues – Part 2

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
DIVA Android - 6.Insecure Data Storage – Part 4
DIVA Android - 7.Input Validation Issues – Part 1
DIVA Android - 8.Input Validation Issues – Part 2
DIVA Android - 9.Access Control Issues – Part 1
DIVA Android - 10.Access Control Issues – Part 2
DIVA Android - 11.Access Control Issues – Part 3
DIVA Android - 12.Hardcoding Issues – Part 2
DIVA Android - 13.Input Validation Issues – Part 3

「12.Hardcoding Issues – Part 2」對應到的 Activity 是 Hardcode2Activity,看一下 code 長這樣:



先追 DivaJni:



前面是會去 load divajni 這個 library,再來下面可看到它宣告變數時使用了 native。native 的作用大致上就是 java 可以藉由 c 去傳遞、處理資料,而透過的這個機制叫 JNI。這邊我們不用知道太複雜,先知道在使用這機制之前,必須先在 src 資料夾內建立 jni 的資料夾:



再來看 Android.mk,我們要找出 LOCAL_SRC_FILES 對應的檔案,這是指要被 compile 的 c/c++ source code:



接著看 divajni.c:



便可發現 key 為「olsdfgad;lh」。

弱點在於一般使用 JNI 時與 native 對應的 JNI function 很容易遭人反追到敏感資訊,如上面可看到jakhar_aseem_diva_DivaJni_access;其次是未加密 so、混淆 so 的 JNI function,只要逆向一下 so 檔便能查到被寫死的敏感資訊:



防範方法:

(1)加密 so 檔。

(2)混淆 JNI function。

2016年8月29日 星期一

DIVA Android - 11.Access Control Issues – Part 3

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
DIVA Android - 6.Insecure Data Storage – Part 4
DIVA Android - 7.Input Validation Issues – Part 1
DIVA Android - 8.Input Validation Issues – Part 2
DIVA Android - 9.Access Control Issues – Part 1
DIVA Android - 10.Access Control Issues – Part 2
DIVA Android - 11.Access Control Issues – Part 3
DIVA Android - 12.Hardcoding Issues – Part 2
DIVA Android - 13.Input Validation Issues – Part 3

「11.Access Control Issues – Part 3」對應到的 Activity 是 AccessControl3Activity,看一下 code 長這樣:


首先,如同「DIVA Android - 3.Insecure Data Storage – Part 1」中,先透過 getDefaultSharedPreferences 取得 preferences 的路徑(/data/data/<package_name>/shared_prefs/<package_name>_preferences.xml),接著將使用者輸入的密碼明文儲存至該檔中,這是第一個弱點。

再來 goToNotes 會去啟動 AccessControl3NotesActivity ,再來追這隻 Activity。



getContentResolver 會根據 query 取得指定的資料,所以我們再來看一下 NotesProvider 內的 CONTENT_URI:



稍微拼一下可得知我們要找的 URI 就是 content://jakhar.aseem.diva.provider.notesprovider/notes。

最後只要確定 AndroidManifest.xml 中 NotesProvider 的 exported 狀態是 true 就可以了。



直接撈資料吧:adb shell content query --uri content://jakhar.aseem.diva.provider.notesprovider/notes



content provider 的 exported 狀態為 true 為第二個弱點。

防範方法:

(1)敏感資料應加密後再儲存,且應使用安全的加密方式,詳情參考 NIST

(2)不該 exported 的 component 應設為 false。

2016年8月26日 星期五

DIVA Android - 10.Access Control Issues – Part 2

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
DIVA Android - 6.Insecure Data Storage – Part 4
DIVA Android - 7.Input Validation Issues – Part 1
DIVA Android - 8.Input Validation Issues – Part 2
DIVA Android - 9.Access Control Issues – Part 1
DIVA Android - 10.Access Control Issues – Part 2
DIVA Android - 11.Access Control Issues – Part 3
DIVA Android - 12.Hardcoding Issues – Part 2
DIVA Android - 13.Input Validation Issues – Part 3

「10.Access Control Issues – Part 2」對應到的 Activity 是 AccessControl2Activity,看一下 code 長這樣:



首先建立一個隱式的 Intent,並透過 setAction 定義 Action 的名稱為「jakhar.aseem.diva.action.VIEW_CREDS2」,然後利用 RadioButton 去判斷使用者是否已選擇 id 為「aci2rbregnow」的選項,並把該狀態賦予 chk_pin。

接著看一下AndroidManifest.xml中的「jakhar.aseem.diva.action.VIEW_CREDS2」,會發現它所對應的 Activity 為「APICreds2Activity」,所以可得知當程式 startActivity 時會去呼叫這隻 APICredsActivity。


不過在呼叫之前,剛剛的 chk_pin 會透過 putExtra 將狀態賦予給 R.String.chk_pin。





接著看這隻 APICreds2Activity,可以看到 bcheck 的值若為 false,則會吐出我們要的資訊。往回追一下,bcheck 被透過 getBooleanExtra,將 R.String.chk_pin 的值設為 true。

所以很明顯的,我們需要去改變 bcheck 的輸出,而既然必須不透過此 App 去啟動指定的 component,那下手的點,就是 R.String.chk_pin 的預設值。



翻一下 strings.xml,可看到 R.String.chk_pin 所對應的值為 check_pin。



那就開始了:adb shell am start -n jakhar.aseem.diva/.APICreds2Activity –-ez check_pin false



理論上 ez 參數沒下錯,不過不知為什麼我的 ez 參數會被當成 pkg(若有大大知道原因還請指教)。

後來索性直接用 drozer 跑就行了:run app.activity.start --component jakhar.aseem.diva jakhar.aseem.diva.APICreds2Activity --extra boolean check_pin false



弱點在於使用隱式 Intent 這種方式由於未指定 component 的名稱,並且須在AndroidManifest.xml 設定 intent-filter,便可讓其他應用程式使用它。

防範方法:

(1)使用顯式 Intent,確保不會遭其他 App 存取。

(2)不該 exported 的 component 應設為 false。

DIVA Android - 9.Access Control Issues – Part 1

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
「9.Access Control Issues – Part 1」對應到的 Activity 是 AccessControl1Activity,看一下 code 長這樣:



首先它建立一個隱式的 Intent,並透過 setAction 定義 Action 的名稱為「jakhar.aseem.diva.action.VIEW_CREDS」,接著看一下 AndroidManifest.xml 中的「jakhar.aseem.diva.action.VIEW_CREDS」,會發現它所對應的 Activity 為「APICredsActivity」,所以可得知當程式 startActivity 時會去呼叫這隻 APICredsActivity。



再來我們嘗試不藉由 DIVA 這隻 App 直接去啟動 APICredsActivity:am start jakhar.aseem.diva/jakhar.aseem.diva.APICredsActivity



執行成功:



弱點在於使用隱式 Intent 這種方式由於未指定 component 的名稱,並且須在AndroidManifest.xml 中,針對需藉此方式存取的那個 component 設定 intent-filter,這會使得該 component 為 exported,如此便可讓其他應用程式使用它。

防範方法:

(1)使用顯式 Intent,確保不會遭其他 App 存取。
(2)不該 exported 的 component 應設為 false。

DIVA Android - 8.Input Validation Issues – Part 2

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
「8.Input Validation Issues – Part 2」對應到的 Activity 是 InputValidation2URISchemeActivity,看一下 code 長這樣:





基本就是透過 webview 去讀 url,不過 url 是可以存取本機檔案的,前提是有設定讀取檔案的權限:




弱點在於未指定讀取哪個本機檔案的狀態下,AndroidManifest.xml 有設定讀取本機檔案,這樣就能存取任何檔案。另一弱點在於開啟讀取 JavaScript,在 Android OS 4.1以下是會有 XSS 的風險。

防範方法:

(1)若需讀取特定本機檔案就應直接寫死。
(2)若不需讀取 JavaScript,setJavaScriptEnabled 就設為 false,避免使用者使用舊版 Android OS 時受 XSS 風險影響。

DIVA Android - 7.Input Validation Issues – Part 1

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
「7.Input Validation Issues – Part 1」對應到的 Activity 是 SQLInjectionActivity,看一下 code 長這樣:







首先新建「sqli」DB,再建立「sqliuser」Table、「user」、「password」、「credit_card」欄位,並新增 3 筆資料。

看到拼接又沒過濾特殊字串,基本就可以判斷存在 SQL Injection了。

先來撇一下:



再來看 log 的輸出:



可以上了。



弱點在於前面提到的使用字串拼接,且也未過濾單引號雙引號等特殊字串。

防範方法:

(1)使用SQLite API
(2)若要使用一般 SQL 語法,就不要拼接字串,同時應過濾特殊字串

2016年8月25日 星期四

DIVA Android - 6.Insecure Data Storage – Part 4

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
「6.Insecure Data Storage – Part 4」對應到的 Activity 是 InsecureDataStorage4Activity,看一下 code 長這樣:





首先 getExternalStorageDirectory 會取得外部儲存空間的路徑,並在底下新增一個 uinfo.txt 的隱藏檔案,接著根據使用者輸入的字串將值寫入該檔中。



Save 後在 /mnt/sdcard/ 中就能發現多了一個 uinfo.txt 的隱藏檔案,cat 後明文顯示。





弱點在於敏感資訊沒有加密。

防範方法:敏感資料應加密後再儲存,且應使用安全的加密方式,詳情參考 NIST

DIVA Android - 5.Insecure Data Storage – Part 3

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
「5.Insecure Data Storage – Part 3」對應到的 Activity 是 InsecureDataStorage3Activity,看一下 code 長這樣:





createTempFile 會在 /data/data/<package_name>/ 資料夾內新增一個以 uinfo 開頭的暫存檔,然後把使用者輸入的字串寫入該檔中。



Save 後在 /data/data/jakhar.aseem.diva/ 中就能發現多了一個以 uinfo 字串開頭的暫存檔,cat 後明文顯示。



弱點在於敏感資訊沒有加密。

防範方法:敏感資料應加密後再儲存,且應使用安全的加密方式,詳情參考 NIST

DIVA Android - 4.Insecure Data Storage – Part 2

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
「4.Insecure Data Storage – Part 2」對應到的 Activity 是 InsecureDataStorage2Activity,看一下 code 長這樣:







首先檢查是否已存在「ids2」這個名稱的 DB,沒有就建立一個。

然後檢查有沒有存在「myuser」這個名稱的 Table,沒有就建立一個,同時新增「user」、「password」這兩個欄位。接著把使用者在 ids2Usr、ids2Pwd 欄位輸入的值,新增到 myuser 這張表中。



Save 後我們看一下手機上儲存這支 app 相關資料的路徑,而我們在 databases 中就能看到ids2這個db。





adb pull 把 ids2 拉出來,開啟來看就能發現資料被以明文方式儲存在裡面。



弱點在於敏感資訊沒有加密。

防範方法:敏感資料應加密後再儲存,且應使用安全的加密方式,詳情參考 NIST

DIVA Android - 3.Insecure Data Storage – Part 1

相關文章:

DIVA Android - 1.Insecure Logging
DIVA Android - 2.Hardcoding Issues – Part 1
DIVA Android - 3.Insecure Data Storage – Part 1
DIVA Android - 4.Insecure Data Storage – Part 2
DIVA Android - 5.Insecure Data Storage – Part 3
「3.Insecure Data Storage – Part 1」對應到的 Activity 是 InsecureDataStorage1Activity,看一下 code 長這樣:





getDefaultSharedPreferences 這段會取得 preferences 的路徑(/data/data/<package_name>/shared_prefs/<package_name>_preferences.xml),再透過 commit 將值寫入該檔。



Save 後我們看一下剛提到的路徑:/data/data/jakhar.aseem.diva/shared_prefs/,而我們在 jakhar.aseem.diva_preferences.xml 中就能看到剛輸入的帳號密碼。



弱點在於敏感資訊沒有加密。

防範方法:敏感資料應加密後再儲存,且應使用安全的加密方式,詳情參考 NIST