3种常见的数据脱敏方案

VSole2023-03-03 15:45:32

目录

  1. SQL数据脱敏实现
  2. JAVA数据脱敏实现
  3. mybatis-mate-sensitive-jackson

1、SQL数据脱敏实现

MYSQL(电话号码,身份证)数据脱敏的实现

-- CONCAT()、LEFT()和RIGHT()字符串函数组合使用,请看下面具体实现
 
-- CONCAT(str1,str2,…):返回结果为连接参数产生的字符串
-- LEFT(str,len):返回从字符串str 开始的len 最左字符
-- RIGHT(str,len):从字符串str 开始,返回最右len 字符
 
-- 电话号码脱敏sql:
 
SELECT mobilePhone AS 脱敏前电话号码,CONCAT(LEFT(mobilePhone,3), '********' ) AS 脱敏后电话号码 FROM t_s_user
 
-- 身份证号码脱敏sql:
 
SELECT idcard AS 未脱敏身份证, CONCAT(LEFT(idcard,3), '****' ,RIGHT(idcard,4)) AS 脱敏后身份证号 FROM t_s_user

2、JAVA数据脱敏实现

可参考:海强 / sensitive-plus

https://gitee.com/strong_sea/sensitive-plus

数据脱敏插件,目前支持地址脱敏、银行卡号脱敏、中文姓名脱敏、固话脱敏、身份证号脱敏、手机号脱敏、密码脱敏 一个是正则脱敏、另外一个根据显示长度脱敏,默认是正则脱敏,可以根据自己的需要配置自己的规则。

3、mybatis-mate-sensitive-jackson

mybatisplus 的新作,可以测试使用,生产需要收费。

根据定义的策略类型,对数据进行脱敏,当然策略可以自定义。

# 目前已有
package mybatis.mate.strategy;
 
public interface SensitiveType {
    String chineseName = "chineseName";
    String idCard = "idCard";
    String phone = "phone";
    String mobile = "mobile";
    String address = "address";
    String email = "email";
    String bankCard = "bankCard";
    String password = "password";
    String carNumber = "carNumber";
}

Demo 代码目录

1、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-mate-examplesartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <artifactId>mybatis-mate-sensitive-jacksonartifactId>
    <dependencies>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
    dependencies>
 
project>

2、appliation.yml

# DataSource Config
spring:
  datasource:
#    driver-class-name: org.h2.Driver
#    schema: classpath:db/schema-h2.sql
#    data: classpath:db/data-h2.sql
#    url: jdbc:h2:mem:test
#    username: root
#    password: test
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_mate?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
# Mybatis Mate 配置
mybatis-mate:
  cert:
    # 请添加微信wx153666购买授权,不白嫖从我做起! 测试证书会失效,请勿正式环境使用
    grant: thisIsTestLicense
    license: as/bsBaSVrsA9FfjC/N77ruEt2/QZDrW+MHETNuEuZBra5mlaXZU+DE1ZvF8UjzlLCpH3TFVH3WPV+Ya7Ugiz1Rx4wSh/FK6Ug9lhos7rnsNaRB/+mR30aXqtlLt4dAmLAOCT56r9mikW+t1DDJY8TVhERWMjEipbqGO9oe1fqYCegCEX8tVCpToKr5J1g1V86mNsNnEGXujnLlEw9jBTrGxAyQroD7Ns1Dhwz1K4Y188mvmRQp9t7OYrpgsC7N9CXq1s1c2GtvfItHArkqHE4oDrhaPjpbMjFWLI5/XqZDtW3D+AVcH7pTcYZn6vzFfDZEmfDFV5fQlT3Rc+GENEg==
 
# Logger Config
logging:
  level:
    mybatis.mate: debug

3、Appliation启动类

package mybatis.mate.sensitive.jackson;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SensitiveJacksonApplication {
 
    // 测试访问 http://localhost:8080/info ,http://localhost:8080/list
    public static void main(String[] args) {
        SpringApplication.run(SensitiveJacksonApplication.class, args);
    }
}

4、配置类,自定义脱敏策略

package mybatis.mate.sensitive.jackson.config;
 
import mybatis.mate.databind.ISensitiveStrategy;
import mybatis.mate.strategy.SensitiveStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class SensitiveStrategyConfig {
 
    /**
     * 注入脱敏策略
     */
    @Bean
    public ISensitiveStrategy sensitiveStrategy() {
        // 自定义 testStrategy 类型脱敏处理
        return new SensitiveStrategy().addStrategy("testStrategy", t -> t + "***test***");
    }
}

5、业务类

User,注解标识脱敏字段,及选用脱敏策略

package mybatis.mate.sensitive.jackson.entity;
 
import lombok.Getter;
import lombok.Setter;
import mybatis.mate.annotation.FieldSensitive;
import mybatis.mate.sensitive.jackson.config.SensitiveStrategyConfig;
import mybatis.mate.strategy.SensitiveType;
 
@Getter
@Setter
public class User {
    private Long id;
    /**
     * 这里是一个自定义的策略 {@link SensitiveStrategyConfig} 初始化注入
     */
    @FieldSensitive("testStrategy")
    private String username;
    /**
     * 默认支持策略 {@link SensitiveType }
     */
    @FieldSensitive(SensitiveType.mobile)
    private String mobile;
    @FieldSensitive(SensitiveType.email)
    private String email;
 
}

UserController

package mybatis.mate.sensitive.jackson.controller;
 
import mybatis.mate.databind.ISensitiveStrategy;
import mybatis.mate.databind.RequestDataTransfer;
import mybatis.mate.sensitive.jackson.entity.User;
import mybatis.mate.sensitive.jackson.mapper.UserMapper;
import mybatis.mate.strategy.SensitiveType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ISensitiveStrategy sensitiveStrategy;
 
    // 测试访问 http://localhost:8080/info
    @GetMapping("/info")
    public User info() {
        return userMapper.selectById(1L);
    }
 
    // 测试返回 map 访问 http://localhost:8080/map
    @GetMapping("/map")
    public Map map() {
        // 测试嵌套对象脱敏
        Map userMap = new HashMap<>();
        userMap.put("user", userMapper.selectById(1L));
        userMap.put("test", 123);
        userMap.put("userMap", new HashMap() {{
            put("user2", userMapper.selectById(2L));
            put("test2", "hi china");
        }});
        // 手动调用策略脱敏
        userMap.put("mobile", sensitiveStrategy.getStrategyFunctionMap()
                .get(SensitiveType.mobile).apply("15315388888"));
        return userMap;
    }
 
    // 测试访问 http://localhost:8080/list
    // 不脱敏 http://localhost:8080/list?skip=1
    @GetMapping("/list")
    public List list(HttpServletRequest request) {
        if ("1".equals(request.getParameter("skip"))) {
            // 跳过脱密处理
            RequestDataTransfer.skipSensitive();
        }
        return userMapper.selectList(null);
    }
}

UserMapper

package mybatis.mate.sensitive.jackson.mapper;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import mybatis.mate.sensitive.jackson.entity.User;
import org.apache.ibatis.annotations.Mapper;
 
@Mapper
public interface UserMapper extends BaseMapper<User> {
 
}

6、测试

GET http://localhost:8080/list

[
  {
    "id": 1,
    "username": "Jone***test***",
    "mobile": "153******81",
    "email": "t****@baomidou.com"
  },
  {
    "id": 2,
    "username": "Jack***test***",
    "mobile": "153******82",
    "email": "t****@baomidou.com"
  },
  {
    "id": 3,
    "username": "Tom***test***",
    "mobile": "153******83",
    "email": "t****@baomidou.com"
  }
]

GET http://localhost:8080/list?skip=1

[
  {
    "id": 1,
    "username": "Jone",
    "mobile": "15315388881",
    "email": "test1@baomidou.com"
  },
  {
    "id": 2,
    "username": "Jack",
    "mobile": "15315388882",
    "email": "test2@baomidou.com"
  },
  {
    "id": 3,
    "username": "Tom",
    "mobile": "15315388883",
    "email": "test3@baomidou.com"
  }
]
string数据脱敏
本作品采用《CC 协议》,转载必须注明作者和本文链接
脱敏前电话号码,CONCAT(LEFT(mobilePhone,3),?身份证号码脱敏sql:. 根据定义的策略类型,对数据进行脱敏,当然策略可以自定义。请添加微信wx153666购买授权,不白嫖从我做起!?测试证书会失效,请勿正式环境使用
可以认为IAM分成两类,一个是AWS提供的IAM,这是一个完整的身份管理系统,但AWS只提供了系统,基于该系统的配置及信息维护,由客户完全负责。AWS 提供了虚拟网络及其之上的VPC,子网,ACL,安全组等,客户需要准确设计配置自己的网络,以确保正确的隔离和防护。用户控制权限的修改通常由特权用户或者管理员组实现。
2020年3月9日,国家市场监督管理总局、国家标准化管理委员会发布中华人民共和国国家标准公告(2020年第1号),全国信息安全标准化技术委员会归口的国家标准GB/T 35273-2020《信息安全技术 个人信息安全规范》完成修...
经常会遇到这样一种情况:项目的配置文件中总有一些敏感信息,比如数据源的url、用户名、密码....这些信息一旦被暴露那么整个数据库都将会被泄漏,那么如何将这些配置隐藏呢?今天介绍一种方案,让你在无感知的情况下实现配置文件的加密、解密。
Android 应用gl,使用了加固i,老版本的应用gl是js源码,新版本更新后,刚开始以为是加密了源码,分析后才知道是使用了Hermes优化引擎。Hermes优化的优化效果如下:优化前:优化后:二、前期准备2.1 绕过root检测应用gl使用了加固i,在被root 的手机上运行,闪退。绕过方法也很简单,在magisk的设置中,开启遵守排除列表,然后在配置排除列表中选择应用gl。
文章主要内容记录对某次应急事件中获取到的特殊钓鱼样本的分析,该样本通过sapien powershell studio将powershell代码封装成可执行文件来绕过一些查杀和限制;0x01 背景某次应急事件中拿到一个攻击者使用的钓鱼样本,这个样本比较有意思和之前的分析有些不同,第一次分析也算曲折,此文记录下对该样本的分析过程。光从行为侧去下结论是不太行的。
文章主要内容记录对某次应急事件中获取到的特殊钓鱼样本的分析,该样本通过sapien powershell studio将powershell代码封装成可执行文件来绕过一些查杀和限制;0x01 背景某次应急事件中拿到一个攻击者使用的钓鱼样本,这个样本比较有意思和之前的分析有些不同,第一次分析也算曲折,此文记录下对该样本的分析过程。光从行为侧去下结论是不太行的。
根据厂商的要求,在修补后的固件未发布前,我对该漏洞细节进行了保密。若读者将本文内容用作其他用途,由读者承担全部法律及连带责任,文章作者不承担任何法律及连带责任。此时,我们惊喜地发现xxx系列产品的xxx型号固件并没有被加密,可以成功解开。漏洞分析此部分以xxx固件为例进行分析,该固件是aarch64架构的。其他固件也许架构或部分字段的偏移不同,但均存在该漏洞。找到无鉴权的API接口显然,此类固件的cgi部分是用Lua所写的。
C:\Users\bk\Desktop\天府科技云APP\天府科技云服务平台\天府科技云服务平台.apkC:\Program Files\Java\jdk1.8.0_111\bin\jarsigner.exe?文件将解压出来的classes.dex文件拷贝到dex2jar工具文件夹中执行命令:d2j-dex2jar classes.dex执行完毕后,得到反编译而来的classes-dex2jar.jar文件使用jd-gui.exe或者luyten-0.5.4打开 classes-dex2jar.jar文件,得到360安全加固混淆加密的源代码。应同时使用V1+V2签名)6.应用完整性校检将反编译出来源码中修改图片文件名为test.png进行重新生成apk包,命令如下:java -jar apktool.jar b -f?
VSole
网络安全专家