【HarmonyOS 5】鴻蒙檢測系統完整性
一、前言
從現實安全威脅來看,設備系統完整性風險已影響至移動應用的各個場景。不少用戶因使用越獄設備(Jailbreak)或非真實設備(Emulator),導致應用安全防護機制失效——某金融類APP曾發現用戶在越獄設備上繞過安全校驗,非法獲取賬戶資金操作權限,直接引發數千萬資金損失風險,此類事件不僅導致用戶資產暴露于黑客攻擊之下,更嚴重損害企業金融風控體系的公信力。
與此同時,非真實設備(Emulator)常被黑產用于模擬用戶環境實施批量攻擊:惡意團伙通過模擬器批量注冊虛假賬號,利用自動化工具繞過設備指紋校驗,在電商平臺薅取數百萬補貼資金;或通過篡改模擬器參數,偽造地理位置信息實施精準詐騙,導致企業反欺詐系統形同虛設,直接引發業務信任危機。
而設備被攻擊(Attack)的場景更凸顯系統完整性防護的緊迫性:黑客通過植入root提權工具突破設備底層防護,在用戶無感知的情況下竊取支付憑證、生物特征等敏感數據——某社交APP用戶因設備被植入惡意程序,通訊錄信息及聊天記錄遭批量竊取并販賣,不僅導致個人隱私泄露,更引發連鎖的電信詐騙案件;部分企業因未有效攔截被攻擊設備的接入,導致內部測試環境遭滲透,核心代碼與業務邏輯被竊取,直接面臨高達數億的經濟損失與品牌聲譽的斷崖式崩塌。
應用通過華為 HarmonyOS 的Device Security Kit提供的safetyDetect.checkSysIntegrity 接口,可檢測 系統環境是否完整,是否為模擬器,被破解設備,越獄設備,并根據檢測結果提示或攔截用戶訪問。
二、業務流程與使用
1.首先需要在AGC平臺給對應項目應用,進行安全檢測服務的開通:
登錄AppGallery Connect( https://developer.huawei.com/consumer/cn/service/josp/agc/index.html#/ )網站,選擇“我的項目”。在項目列表中找到需要開通Device Security服務的項目。
之后重新生成調試Profile,在項目中手動進行簽名證書的配置,就可使用該接口了。否則會try catch提示Permission Denied。
2.創建safetyDetect.SysIntegrityRequest配置需要檢測的Url數組,調用safetyDetect.checkSysIntegrity接口異步請求檢測:
import { safetyDetect } from '@kit.DeviceSecurityKit';
import { BusinessError } from '@ohos.base';
import { hilog } from '@kit.PerformanceAnalysisKit';const req = { nonce: '服務器生成的隨機值' };
try {const data = await safetyDetect.checkSysIntegrity(req);console.log('檢測結果:', data.result); // true/false
} catch (err) {console.error('錯誤碼:', err.code, '信息:', err.message);
}
3.注意事項:
(1)每日每設備調用次數,最多1 萬次。
(2)每分鐘調用次數,最多5 次。
(3)并發調用數 ,最多5 個。
三、源碼示例:
// 導入所需模塊
import { safetyDetect } from '@kit.DeviceSecurityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { promptAction } from '@kit.ArkUI';
struct deviceCheckTestPage {private TAG: string = "DeviceCheckTestPage";// 存儲檢測結果 resultMsg: string = '未檢測'; detailMsg: string = '';// 生成隨機nonce(示例方法,實際需從服務器獲取)private generateNonce(): string {// 示例中使用本地隨機生成(僅用于演示)// 實際開發必須從應用服務器動態獲取,確保每次請求唯一性return 'imEe1PCRcjGkBCAhOCh6ImADztOZ8ygxlWRs' // 從服務器生成的隨機的nonce值}// 系統完整性檢測函數private async checkSystemIntegrity() {try {console.info(this.TAG, '開始檢測系統完整性')// 獲取nonce(示例:本地生成,實際需從服務器獲取)const nonce = this.generateNonce();// 構建檢測請求const req: safetyDetect.SysIntegrityRequest = {nonce: nonce};// 調用檢測接口const response = await safetyDetect.checkSysIntegrity(req);console.info(this.TAG, '檢測結果:%{public}s', response.result)// 處理結果this.processResult(response);} catch (error) {const err = error as BusinessError;console.info(this.TAG, '檢測結果:%{public}s', '檢測失敗:%{public}d %{public}s', err.code, err.message)this.showPrompt('檢測失敗' + `錯誤碼:${err.code}\n${err.message}`);}}// 結果處理函數private processResult(response: safetyDetect.SysIntegrityResponse) {// SysIntegrityResponse - result// nonce:調用checkSysIntegrity接口時傳入的nonce字符串。//// timestamp:服務器生成的時間戳。//// hapBundleName:您應用的包名。//// hapCertificateSha256:您應用的簽名證書SHA256摘要。//// basicIntegrity:系統完整性檢測的結果,true表示檢測結果完整,false表示存在風險。//// appId:您應用的appid。//// detail:可選字段,當basicIntegrity結果為false時,該字段將提供存在風險的原因,// JWS格式的系統完整性檢測結果。JWS內容詳見《Device Security Kit開發指南》中的系統完整性檢測開發步驟。let result = response.result;// jailbreak:設備被越獄。// emulator:非真實設備。// attack:設備被攻擊。this.resultMsg = `系統完整性:${response ? '安全' : '風險'}`;if (!result) {// this.detailMsg = `風險原因:${detail.join('、')}`;} else {this.detailMsg = '無具體風險信息';}this.showPrompt('檢測完成' + `${this.resultMsg}\n${this.detailMsg}`);}// 提示框函數private showPrompt(message: string) {promptAction.showToast({message: message})}build() {Column() {Text('系統完整性檢測Demo').fontSize(20).fontWeight(500).margin(10);Button('開始檢測').width('90%').height(48).backgroundColor('#007DFF').fontColor('white').onClick(() => this.checkSystemIntegrity()).margin(20);Text(this.resultMsg).fontSize(16).fontWeight(400).margin({ bottom: 5 });Text(this.detailMsg).fontSize(14).fontColor('#666')}.padding(20).width('100%').height('100%').justifyContent(FlexAlign.Center).backgroundColor('#F5F5F5');}
}
系統完整性檢測結果簽名驗證的java示例代碼,僅供應用服務器參考。(https://gitee.com/harmonyos_samples/device-security-kit-samplecode-safetydetect-serverdemo-java)