iOS逆向之hook控制中心開關

控制中心中的快捷開關(iPad mini2 9.3.1)

iOS的控制中心(Control Center,底部上拉出現的快捷菜單)提供了幾個快捷開關方便操作。如中間的飛行模式、WIFI、藍牙、勿擾模式、旋轉鎖定,右下的Night Shift等。就是沒有GPS定位開關。

蘋果對開發者申請定位權限提供了兩個選項,一個是始終(Always),用于需要常駐后臺的導航之類的應用。還有一個是使用應用期間(While Using the App),就是給普通應用獲取個用戶所在城市之類用的。如果大家都遵守規則申請需要的權限也沒啥事,但是架不住國內流氓太多,不管什么應用都要始終權限,還不能選使用應用期間,一旦授權始終,就算不用這個應用,它在后臺也可以時不時的獲取定位。一直開著定位的話,一來費電,二來泄露隱私。所以,一般能不給權限的應用盡量不給,比如看天氣,團購類的應用,手動選擇不影響使用。不給定位沒法用的,比如導航,運動記錄類的應用,應用里授權,總的定位權限要用了在開,用完了就關。這就需要經常開關定位了。

只申請始終定位權的幾個例子

既然蘋果沒有提供,那就只能自己折騰了。比較簡單的方法是選個不常用的開關,讓他完成開關定位的功能。
比如Night Shift,因為Night Shift可以設置時間段自動啟用?;静恍枰诳刂浦行闹惺謩釉O置。


Night Shift可以設置時間段自動啟用

具體分析下完成這個功能需要兩個步驟:先要找到調用開關的方法。再hook這個方法完成修改定位設置。

首先要找到調用開關的方法:
因為要hook的快捷開關在控制中心里,所以可以先在iOS文件系統里搜索controlcenter這個關鍵字找下具體位置。

7de-iPad:~ root# grep -r controlcenter /
Binary file /Library/Application Support/AggregateDictionary/Whitelist.plist matches
Binary file /Library/Application Support/AggregateDictionary/keys.dat matches
Binary file /System/Library/Caches/com.apple.dyld/dyld_shared_cache_arm64 matches
Binary file /System/Library/Caches/com.apple.dyld/dyld_shared_cache_armv7s matches
Binary file /System/Library/CoreServices/SpringBoard.app/SpringBoard matches
grep: /System/Library/Frameworks/IOKit.framework/IOKit: No such file or directory
grep: /System/Library/Frameworks/System.framework/System: No such file or directory
grep: /System/Library/Frameworks/System.framework/Versions/Current: No such file or directory
grep: /System/Library/PrivateFrameworks/LocationBundles/MDM.framework: No such file or directory
...

其中第五條記錄在是SpringBoard中找到了controlcenter,確實控制中心也是從桌面劃出來的,可以以此為線索class-dump出SpringBoard頭文件看下。
把iPad中的SpringBoard.app拷出來,可以用iFunBox 或者scp命令
scp -r root@iPad的局域網IP:/System/Library/CoreServices/SpringBoard.app /Users/netx/Workspace
然后終端進入SpringBoard.app執行class-dump命令
class-dump -S -s -H SpringBoard -o /Users/netx/Workspace/SBHeaders
執行完成后可以在/Users/netx/Workspace/SBHeaders目錄下看到一堆的頭文件。
新建個Xcode項目把這些頭文件拖進去方便查找。

搜索controlcenter,看上去有用的有SBControlCenterButton,SBControlCenterButtonSettings,SBControlCenterControllerSBControlCenterViewController。

SpringBoard頭文件中的controlcenter搜索結果

再搜搜nightshift,只有一個SBCCNightShiftSetting

SpringBoard頭文件中的nightshift搜索結果

點進去看看。

// SBCCNightShiftSetting.h

#import "SBCCSettingModule.h"

@class CBBlueLightClient;

@interface SBCCNightShiftSetting : SBCCSettingModule
{
    CBBlueLightClient *_blueLightReductionClient;
    CDStruct_0b0a24e2 _currentStatus;
}

+ (_Bool)_isSupported;
+ (id)displayName;
+ (id)identifier;
+ (_Bool)isSupported:(int)arg1;
- (void).cxx_destruct;
- (id)_alertControllerForDisablingAccessibilityScreenFilter:(CDUnknownBlockType)arg1 cancelBlock:(CDUnknownBlockType)arg2;
- (id)_formatCardinalityForTransitionTime:(CDStruct_bdf7039f)arg1 forLocale:(id)arg2;
- (void)_getBlueLightStatus:(CDUnknownBlockType)arg1;
- (void)_setNightShiftEnabled:(_Bool)arg1;
- (id)_statusUpdateForBlueLightStatus:(CDStruct_0b0a24e2)arg1;
- (id)_statusUpdateForBlueLightStatus:(CDStruct_0b0a24e2)arg1 forLocale:(id)arg2;
- (id)_statusUpdateFormatStringForBlueLightEnabled:(_Bool)arg1 transitionTime:(CDStruct_bdf7039f)arg2;
- (id)_statusUpdateFormatStringForBlueLightEnabled:(_Bool)arg1 transitionTime:(CDStruct_bdf7039f)arg2 forLocale:(id)arg3;
- (id)_timeFormatterForLocale:(id)arg1;
- (id)_timeStringForBlueLightTransitionTime:(CDStruct_bdf7039f)arg1;
- (id)_timeStringForBlueLightTransitionTime:(CDStruct_bdf7039f)arg1 forLocale:(id)arg2;
- (_Bool)_toggleState;
- (void)_updateState;
- (_Bool)_uses24HourTimeForLocale:(id)arg1;
- (void)activate;
- (id)aggdKey;
- (void)deactivate;
- (id)glyphImageForState:(int)arg1;
- (_Bool)isRestricted;
- (id)statusUpdate;

@end

有個_toggleState方法,用cycript測試下調這個方法會不會觸發屏幕色溫變化。
ssh到iPad執行cycript -p SpringBoard
choose(SBCCNightShiftSetting)找下有沒有SBCCNightShiftSetting實例

7de-iPad:~ root# cycript -p SpringBoard
cy# choose(SBCCNightShiftSetting)
[#"<SBCCNightShiftSetting: 0x15210f3a0; id = nightShift; name = Night Shift>"]
cy# 

找到之后用#實例的內存地址調用下_toggleState,輸入[#0x15210f3a0 _toggleState] ,可以看到night shift被開啟了,再輸入一次[#0x15210f3a0_toggleState]命令,nightshift被關閉了,就是它了。(屏幕色溫的變換無法在錄制中看到,不過可以看到按鈕狀態的變化,實際屏幕是有變黃屏,再變回正常色溫這個過程。)

測試_toggleState

現在完成了第一步,接下來就是怎么修改定位設置。
定位首先想到的就是CoreLocation,在開發過程中要查詢定位是否可用也都用[CLLocationManager locationServicesEnabled],查看CLLocationManager的API沒發現有設置定位相關的方法。求助萬能的Google,搜到一條相關的討論,提到了+(void)setLocationServicesEnabled:(BOOL)enabled;這個私有方法,我們再用cycrpit測試下。
先在隱私(Privacy)中把定位關閉。運行[CLLocationManager setLocationServicesEnabled:YES],好像設置沒改,切換其他的選項卡再回來看看,定位被打開了,看來是界面沒有刷新。在試試[CLLocationManager setLocationServicesEnabled:NO],同樣先切換其他的選項卡在切回來,定位被關閉了。說明這個方法確實可以操作定位設置。

測試setLocationServicesEnabled:

接下來只需要hookSBCCNightShiftSetting_toggleState方法,在內部調用下[CLLocationManager setLocationServicesEnabled:]就可以了。
安裝Theos,在Mac終端中輸入nic.pl,選擇[11.] iphone/tweak(序號可能不同)新建Tweak,輸入項目名稱NightShift2Location,包名,作者,需要tweak的對象包名(默認springboard),tweak安裝完成后需要重啟的應用名(默認springboard),這個tweak工程就創建完成了。
需要在Tweak.xm中寫hook代碼。

%hook SBCCNightShiftSetting
-(void)_toggleState
{
%orig; // 調用原方法
[CLLocationManager setLocationServicesEnabled:self.state]; // 用nightshift的狀態設置定位
}
%end

修改control 和Makefile

// control
Package: com.netx.nightshift2location
Name: NightShift2Location
Depends: mobilesubstrate, firmware (>=8.0)
Version: 0.0.1
Architecture: iphoneos-arm
Description: Make Night Shift Setting of ControlCenter toggle Location Setting.
Maintainer: netx
Author: netx
Section: Tweaks
// Makefile
THEOS_DEVICE_IP = 192.168.1.106 // iPad的局域網IP
ARCHS= armv7 arm64
TARGET = iphone:latest:8.0
include $(THEOS)/makefiles/common.mk
TWEAK_NAME = NightShift2Location
NightShift2Location_FILES = Tweak.xm
NightShift2Location_FRAMEWORKS = UIKit
include $(THEOS_MAKE_PATH)/tweak.mk
after-install::
install.exec "killall -9 SpringBoard"

還需要寫兩個頭文件騙過編譯器,并在tweak.xm中導入。

// CLLocationManager.h
#import <CoreLocation/CoreLocation.h>

@interface CLLocationManager()
+ (void)setLocationServicesEnabled:(BOOL)enabled;
@end
// SBCCNightShiftSetting.h
@interface SBCCNightShiftSetting : NSObject
- (int)state;
@end

在終端輸入 make package install 安裝到iPad,測試可用。這個tweak就算完成了。^ ^

測試效果

參考:
iOS應用逆向工程 第2版
Get iPhone location in iOS without preference Location Services set to ON

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容