[flutter] 如何防止GPS偽定位

在 Flutter 開發中,要防止 GPS 偽定位並確保應用程式獲取的地理位置是可信的,可以採取以下方法來符合 Android 和 iOS 的需求:


1. 啟用 Google Play Services 的「位置驗證」

適用於:Android

Google 提供了「位置驗證 (Location Verification)」功能,可以偵測裝置的 GPS 是否受到軟體偽造攻擊。
你可以透過 FusedLocationProvidersetMockMode(false) 來防止應用程式接受模擬位置。

步驟

  1. AndroidManifest.xml 加入權限:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  2. 使用 locationSettingsRequest 來確保 GPS 設定正確:

    import 'package:geolocator/geolocator.dart';
    Future<Position> getLocation() async { bool serviceEnabled; LocationPermission permission; // 檢查 GPS 是否開啟 serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { return Future.error('位置服務未啟用'); } // 檢查權限 permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { return Future.error('位置權限被拒絕'); } } if (permission == LocationPermission.deniedForever) { return Future.error('位置權限永久拒絕,請到設定中開啟'); } // 取得目前位置 return await Geolocator.getCurrentPosition(); }

2. 使用 Google SafetyNet API (Android)

適用於:Android

Google 的 SafetyNet Attestation API 可檢查裝置是否被 Root、是否在模擬器上運行,進一步確保 GPS 資料的真實性。

  1. pubspec.yaml 加入:

    dependencies:
    device_info_plus: ^9.0.3
  2. main.dart 使用:

    import 'package:device_info_plus/device_info_plus.dart';
    Future<bool> isRealDevice() async { DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo; return !androidInfo.isPhysicalDevice; }

如果 isPhysicalDevicefalse,代表這是模擬器或非真實裝置,可拒絕服務。


3. iOS 端防止 GPS 偽定位

適用於:iOS

iOS 的 CoreLocation 框架可以檢測 GPS 是否來自模擬器或虛假信號。

步驟

  1. Info.plist 加入:

    <key>NSLocationWhenInUseUsageDescription</key>
    <string>我們需要您的位置來提供更好的服務。</string> <key>NSLocationAlwaysUsageDescription</key> <string>我們需要您的位置來提供更好的服務。</string>
  2. main.dart 確保應用只在真機執行:

    import 'package:device_info_plus/device_info_plus.dart';
    Future<bool> isiOSRealDevice() async { DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); IosDeviceInfo iosInfo = await deviceInfo.iosInfo; return iosInfo.isPhysicalDevice; }

如果 isPhysicalDevicefalse,代表這是模擬器,應考慮阻擋功能或顯示警告。


4. 偵測異常 GPS 資料

適用於:Android & iOS

即使防範模擬器與 root 之後,仍可能有 GPS Spoofing (例如 Fake GPS App)。
可以透過以下方式偵測異常:

  • 檢測速度:GPS 速度異常高,例如超過 300 km/h 可能是偽造。

  • 檢測座標變化:短時間內位置變化過快或不合理。

  • 使用多重來源驗證:比較 GPS、WiFi 和行動網路定位是否一致。

Future<bool> isGPSDataValid(Position position) async {
if (position.speed > 100) { return false; // 速度異常高 } if (position.accuracy > 50) { return false; // 位置精度異常 } return true; }

5. 防止 Mock Location App (Android)

適用於:Android

在 Android 8 以上,可透過 Settings.Secure 檢查裝置是否允許模擬位置:

import 'package:platform_device_id/platform_device_id.dart';
Future<bool> isMockLocationEnabled() async { bool isMock = await Geolocator.isLocationServiceEnabled(); return isMock; }

如果 isMocktrue,可顯示警告或封鎖應用。


總結

方法適用平台功能
FusedLocationProviderAndroid檢查 GPS 是否為模擬
SafetyNet AttestationAndroid檢測 Root、模擬器、系統完整性
CoreLocationiOS確保裝置為真機
檢查速度與座標變化Android & iOS偵測異常 GPS
防止 Mock Location AppAndroid阻擋 Fake GPS App

這些措施可以幫助你的 Flutter 應用防止 GPS 偽定位,確保獲取的地理位置數據是可信的!

留言

這個網誌中的熱門文章

flutter 使用 ToastDialog 範例

[flutter]flutter如何防止GPS偽定位

ScaffoldMessenger 範例