DeepakPal25/flutter_security_suite
GitHub: DeepakPal25/flutter_security_suite
一个企业级 Flutter 移动安全插件,提供 Root/越狱检测、证书锁定、应用完整性验证、截图防护和加密安全存储等跨平台防护能力。
Stars: 0 | Forks: 0
# flutter_security_suite
一个用于移动应用安全的 Flutter 插件,提供 Root/越狱检测、Certificate Pinning、应用完整性验证、截图保护和加密的安全存储——在 Android (Kotlin) 和 iOS (Swift) 上均有原生实现。
[](https://pub.dev/packages/flutter_security_suite)
[](LICENSE)
[]()
## 功能
| Feature | Android | iOS |
|---------|---------|-----|
| Root/Jailbreak Detection | su binary & app detection | Cydia, dylib scanning, file checks |
| Certificate Pinning | SHA-256 SPKI fingerprint | SHA-256 SPKI fingerprint |
| App Integrity | Debug flag & installer validation | Debugger detection via sysctl |
| Screenshot Protection | `FLAG_SECURE` window flag | Secure UITextField overlay |
| Secure Storage | EncryptedSharedPreferences (AES-256) | iOS Keychain (SecItem API) |
## 替代方案
在选择此包之前,请考虑哪种工具适合您的需求:
| Package | Maintained by | Best for |
|---------|--------------|----------|
| **flutter_security_suite** (this) | Individual (open-source) | Learning, prototypes, open auditing |
| [freerasp](https://pub.dev/packages/freerasp) | Talsec (company) | Production apps requiring active threat intel |
| [flutter_ios_security_suite](https://pub.dev/packages/flutter_ios_security_suite) | Individual (open-source) | iOS-only jailbreak checks |
## 限制与安全注意事项
- **Root/越狱检测是基于启发式的。** 拥有高级工具(例如带有 Zygisk 模块的 Magisk)的坚定攻击者可以绕过基于文件和基于包的检查。本包提供了一个合理的基准,而非保证。
- **Certificate Pinning 在原始 `SecureSocket` 上以纯 Dart 实现。** 它不会拦截来自原生 SDK、WebView 或建立自己连接的第三方库的流量。
- **iOS 上的截图保护** 使用 `UITextField` 覆盖层,这会阻止标准的 iOS 截图 API。它无法防止通过 QuickTime 或 AirPlay 镜像进行的屏幕录制。
- **本包与任何金融机构或支付网络无关。** 内部的 `SecureBankKit` 命名是一个遗留的实现细节,并非认证。
- **没有主动的威胁情报源。** 新的绕过技术不会自动解决;您必须手动更新包。
## 入门指南
### 安装
```
dependencies:
flutter_security_suite: ^1.0.3
```
### 平台设置
**Android** — 无需额外设置。Min SDK: 23。
**iOS** — 最低部署目标: iOS 12.0。如果使用带有 Cydia URL scheme 检查的越狱检测,请添加到您的 `Info.plist`:
```
LSApplicationQueriesSchemes
cydia
```
## 用法
### 初始化
```
import 'package:flutter_security_suite/flutter_security_suite.dart';
final kit = SecureBankKit.initialize(
enableRootDetection: true,
enableAppIntegrity: true,
enablePinning: false,
enableLogging: false,
certificatePins: {},
);
```
### 运行安全检查
```
final status = await kit.runSecurityCheck();
if (status.isSecure) {
// Device is clean - proceed normally
} else {
if (status.isRooted) print('Device is rooted/jailbroken');
if (!status.isAppIntegrityValid) print('App integrity compromised');
if (!status.isCertificatePinningValid) print('Certificate pinning failed');
}
```
### Certificate Pinning
锁定 **Subject Public Key Info (SPKI)** SHA-256 哈希值。只要密钥对保持不变,这就能在证书续订后依然有效。
```
final kit = SecureBankKit.initialize(
enablePinning: true,
certificatePins: {
'api.example.com': ['sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='],
},
);
```
要从实时主机提取 SPKI pin:
```
openssl s_client -connect api.example.com:443 -servername api.example.com 2>/dev/null \
| openssl x509 -pubkey -noout \
| openssl pkey -pubin -outform DER \
| openssl dgst -sha256 -binary \
| base64
```
### 截图保护
```
await kit.screenshotProtection.enable();
await kit.screenshotProtection.disable();
```
### 安全存储
```
await kit.secureStorage.write(key: 'auth_token', value: 'jwt_abc123');
final token = await kit.secureStorage.read(key: 'auth_token');
await kit.secureStorage.delete(key: 'auth_token');
await kit.secureStorage.deleteAll();
```
### 错误处理
```
final result = await someSecurityOperation();
result.fold(
onSuccess: (data) => print('Result: $data'),
onFailure: (error) => print('Error: ${error.message}'),
);
if (result.isSuccess) {
final value = result.dataOrNull;
}
```
## 架构
```
┌─────────────────────────────────────────────┐
│ PUBLIC API (SecureBankKit) │ Consumer-facing facade
├─────────────────────────────────────────────┤
│ DOMAIN (Entities, UseCases, Repositories) │ Business logic & contracts
├─────────────────────────────────────────────┤
│ DATA (Datasources, Repository Impls) │ Implementation layer
├─────────────────────────────────────────────┤
│ PLATFORM (MethodChannel Bridge) │ Flutter ↔ Native bridge
├─────────────────────────────────────────────┤
│ CORE (Result types, Exceptions, Logger) │ Shared utilities
└─────────────────────────────────────────────┘
```
## 工作原理
### Root/Jailbreak 检测
**Android:**
- 扫描系统路径以查找 `su` 二进制文件(`/sbin/su`, `/system/bin/su` 等)
- 通过 `PackageManager` 检测 Root 应用(SuperSU, Magisk 等)
- 检查构建标签是否为 `test-keys`
**iOS:**
- 检查已知的越狱文件(Cydia, MobileSubstrate, bash, ssh, apt)
- 扫描已加载的 dylibs 以查找可疑模块(FridaGadget, SubstrateLoader 等)
- 测试 Cydia URL scheme 可用性
- 尝试写入受限的 `/private/` 路径
### App Integrity
**Android:**
- 验证应用未被标记为可调试
- 验证安装程序来源(Google Play, Amazon, Huawei)
**iOS:**
- 通过 `sysctl` 检测调试器附加(P_TRACED 标志)
- `isAppStoreBuild()` 检查是否缺少 `embedded.mobileprovision`(区分 App Store 与 TestFlight/开发构建)
### Secure Storage
**Android:** `EncryptedSharedPreferences` — 密钥加密: AES-256-SIV, 值加密: AES-256-GCM
**iOS:** Keychain via `SecItem` API — accessibility: `kSecAttrAccessibleWhenUnlockedThisDeviceOnly`
## 项目结构
```
flutter_security_suite/
├── lib/
│ ├── flutter_security_suite.dart # Main export
│ ├── secure_bank_kit.dart # Public API facade
│ ├── core/ # SecurityResult, exceptions, logger
│ ├── domain/ # Entities, use cases, repository contracts
│ ├── data/ # Datasource & repository implementations
│ └── platform/
│ └── method_channel_security.dart # MethodChannel bridge
├── android/src/main/kotlin/ # Kotlin native handlers
├── ios/Classes/ # Swift native handlers
├── example/ # Demo application
└── test/ # 11 test files, 47 unit tests
```
## 测试
```
flutter test
```
覆盖范围包括平台(MethodChannel mock)、领域(带有 mocked repository 的用例)和数据(带有 mocked datasource 的 repository 实现)层——包括成功和失败路径。
## 环境要求
| | Minimum |
|---|---|
| Flutter | >= 3.10.0 |
| Dart SDK | >= 3.0.0 |
| Android | API 23 (Marshmallow) |
| iOS | 12.0 |
## 贡献
欢迎贡献和错误报告。在提交大型 pull request 之前,请先开启一个 issue。
## 许可证
MIT — 见 [LICENSE](LICENSE)。
标签:AES-256, Android开发, Clean Architecture, DOM解析, EncryptedSharedPreferences, Flutter, Flutter插件, iOS开发, Keychain, Kotlin, Root检测, SSL Pinning, Swift, 企业级安全, 反调试, 安全存储, 应用完整性, 操作系统检测, 漏洞评估, 目录枚举, 私有化部署, 移动安全, 网络安全, 网络安全, 证书锁定, 越狱检测, 防御规避, 防截屏, 隐私保护, 隐私保护