Flutter安全工具插件flutter_security_toolkit的使用
Flutter安全工具插件flutter_security_toolkit的使用
在当今世界,移动设备上存储了大量的个人信息和业务关键数据,因此安全性不再是可选项,而是必不可少的。
Mobile Security Toolkit 是一个开源项目,旨在通过提供一个开发者友好的、一站式仓库来帮助开发者和安全专家处理移动设备的安全问题。
此项目使用了以下组件:
功能
已经实现的功能包括:
- ✅ 检测越狱或Root权限
- ✅ 检测钩子
- ✅ 检测模拟器
你可以通过以下示例应用程序查看这些功能的实际应用: 示例应用程序
安装
你可以在你的项目中使用Mobile Security Toolkit,通过Swift包管理器导入它。
pub.dev
在pubspec.yaml
文件中添加以下内容:
dependencies:
flutter_security_toolkit: ^1.0.1
使用
变量API
使用可获取变量来获取设备当前状态:
Future<bool?> areRootPrivilegesDetected()
Future<bool?> areHooksDetected()
Future<bool?> isSimulatorDetected()
异步流API
此功能将在未来实现。
路线图
接下来要实现的功能包括:
- ❌ 应用签名检查
- ❌ 调试器检测
- ❌ 设备密码检查
- ❌ 完整性检查
- ❌ 硬件安全检查
贡献
查看贡献指南
作者和致谢
作者
特别感谢
- Sabrina Geiger
- Dennis Gill
- Jonas Rottmann
许可证
查看LICENSE
示例代码
以下是使用flutter_security_toolkit
插件的一个完整示例:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_security_toolkit/flutter_security_toolkit.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _jailbroken = false;
bool _hooks = false;
bool _simulator = false;
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 平台消息是异步的,因此我们在异步方法中初始化。
Future<void> initPlatformState() async {
// 平台消息可能会失败,所以我们使用try/catch来捕获PlatformException。
// 我们还处理了消息可能返回null的情况。
try {
final (jailbroken, hooks, simulator) = await (
ThreatDetectionCenter.areRootPrivilegesDetected(),
ThreatDetectionCenter.areHooksDetected(),
ThreatDetectionCenter.isSimulatorDetected(),
).wait;
if (!mounted) return;
setState(() {
_jailbroken = jailbroken ?? _jailbroken;
_hooks = hooks ?? _hooks;
_simulator = simulator ?? _simulator;
});
} on PlatformException {
// 不执行任何操作
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: SafeArea(
child: Center(
child: Builder(builder: (context) {
final textTheme = Theme.of(context).textTheme;
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
_jailbroken || _hooks ? Icons.lock_open : Icons.lock,
size: 80,
).padding(bottom: 24),
Text(
'保护',
style: textTheme.headlineLarge,
).padding(bottom: 8),
Text(
'以下是一些可能使你面临风险的威胁',
style:
textTheme.titleMedium?.copyWith(color: Colors.grey),
textAlign: TextAlign.center,
).padding(bottom: 16),
ThreatCard(
title: '越狱 / Root',
description:
'是一种获取操作系统特权控制的方法。工具如Magisk或Shadow可以隐藏特权访问',
status: _jailbroken,
),
ThreatCard(
title: '钩子',
description:
'拦截系统或应用程序调用并修改它们(例如修改函数调用的返回值)',
status: _hooks,
),
ThreatCard(
title: '模拟器',
description: '在模拟器中运行应用程序',
status: _simulator,
),
],
),
);
}),
).padding(left: 20, right: 20),
),
),
);
}
}
class ThreatCard extends StatelessWidget {
final String title;
final String description;
final bool status;
const ThreatCard({
required this.title,
required this.description,
required this.status,
super.key,
});
[@override](/user/override)
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: textTheme.titleLarge?.copyWith(color: Colors.black),
),
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
color: !status ? Colors.green : Colors.red,
child: Column(
children: [
Text(
!status ? '安全' : '检测到',
style: textTheme.bodySmall?.copyWith(color: Colors.white),
),
],
).paddingAll(8),
)
],
).padding(bottom: 8),
Text(
description,
style: textTheme.titleMedium?.copyWith(color: Colors.grey),
textAlign: TextAlign.start,
),
],
).paddingAll(16),
).paddingAll(8);
}
}
extension PaddedWidget on Widget {
Widget padding({
double left = 0.0,
double top = 0.0,
double right = 0.0,
double bottom = 0.0,
}) =>
Padding(
padding: EdgeInsets.only(
left: left,
top: top,
right: right,
bottom: bottom,
),
child: this,
);
Widget paddingAll(double all) =>
padding(left: all, top: all, right: all, bottom: all);
}
更多关于Flutter安全工具插件flutter_security_toolkit的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复