davidB/json-simple-obfuscator

GitHub: davidB/json-simple-obfuscator

一个用于在 JSON 文件中部分隐藏敏感字段值的命令行工具,适合测试和演示场景下的数据脱敏。

Stars: 1 | Forks: 1

# json-simple-obfuscator [![CI](https://static.pigsec.cn/wp-content/uploads/repos/cas/99/993938d8ce5e902ccfb9d6747725c320d855dea3235ed9a304cedf0d94c9321f.svg)](https://github.com/davidB/json-simple-obfuscator/actions/workflows/ci.yml) [![Crates.io](https://img.shields.io/crates/v/json-simple-obfuscator.svg)](https://crates.io/crates/json-simple-obfuscator) [![Crates.io Downloads](https://img.shields.io/crates/d/json-simple-obfuscator.svg)](https://crates.io/crates/json-simple-obfuscator) [![GitHub Downloads](https://img.shields.io/github/downloads/davidB/json-simple-obfuscator/total.svg)](https://github.com/davidB/json-simple-obfuscator/releases) [![License: CC0-1.0](https://img.shields.io/badge/License-CC0_1.0-lightgrey.svg)](https://creativecommons.org/publicdomain/zero/1.0/) 一个用于部分隐藏 json 值的工具(使用不安全的伪匿名化 / 混淆算法)。 ## 目标 / 使用场景 - 隐藏用于测试、演示的示例 json 中的敏感值 - 当值作为其他字符串的一部分出现时,也将其隐藏 - 幂等且一致:`apply(a.json) == apply(apply(apply(.... (apply(a.json)))))`,因此它可以作为 pre-commit hook、构建阶段等的一部分使用 - 单射:不同的输入值生成不同的输出值,保留跨文件的引用和外键关系 - **请勿**用于隐藏或加密机密信息等 ## 使用方法 ``` ❯ json-simple-obfuscator file1.json file2.json ◇ Collected 2464 values from 102 files in 62.7ms │ ◇ Computed 267 replacements in 2.6ms │ ◇ Obfuscated 102 files in 103.3ms │ └ Done! ``` ``` ❯ json-simple-obfuscator -h A tool to partially hide json value (using unsecure pseudonimize / obfuscate algo). Usage: json-simple-obfuscator [OPTIONS] [FILE]... Arguments: [FILE]... path of files to obfuscate Options: -r, --replace additional values to obfuscate (can be repeated) -f, --field additional field names whose values are obfuscated (can be repeated, case-insensitive). Built-in sensitive fields: contains password/secret/token/phone/email; ends with name/_id/-id/Id; exact match user/login/address/id --no-default-fields disable built-in sensitive field detection (combine with --field to define an exact list) -s, --stem write output to a sibling file with this stem inserted before the extension (e.g. `-s pseudo` => foo.json -> foo.pseudo.json) instead of rewriting in place. Existing destination is overwritten -h, --help Print help -V, --version Print version ``` 默认情况下,每个输入文件都会被**就地**重写。传入 `--stem ` 以 保留原始文件不变,并将混淆后的输出写入同目录下的文件中, 文件名会在扩展名前插入指定的词干: ``` ❯ json-simple-obfuscator --stem pseudo foo.json # writes foo.pseudo.json ``` 请查看 [`examples/data-01/`](examples/data-01/) 获取一个可运行的示例。 ### 安装 从[发布页面](https://github.com/davidB/json-simple-obfuscator/releases)下载二进制文件。
通过 shell 脚本 ``` curl --proto '=https' --tlsv1.2 -LsSf https://github.com/davidB/json-simple-obfuscator/releases/latest/download/json-simple-obfuscator-installer.sh | sh ```
通过 power shell 脚本 ``` powershell -ExecutionPolicy Bypass -c "irm https://github.com/davidB/json-simple-obfuscator/releases/latest/download/json-simple-obfuscator-installer.ps1 | iex" ```
通过 homebrew ``` brew install davidB/tap/json-simple-obfuscator ```
通过 cargo ``` cargo install json-simple-obfuscator ```
通过 mise ``` [tools] "github:davidB/json-simple-obfuscator" = "latest" ```
## 一个简单的算法 ``` { "a": "Hello", "id": 123456, "details": { "user": "johnD", "name": "John Doe", "url": "http://example.com/item/123456" } } ``` 变为 ``` { "a": "Hello", "id": 111111, "details": { "user": "aaaaA", "name": "Aaaa Aaa", "url": "http://example.com/item/111111" } } ``` 1. 收集**所有输入文件**中“敏感”字段的值(字符串或数字)。 敏感字段是指名称(小写)为以下项的字段:`id`、`_id`、`*token`、`*password`、`*secret`、`user`、`*name`、`*email`、`*phone`、`login`、`address`、`*Id` 2. 将所有收集到的值按字母顺序排序,然后为每个值分配一个唯一的混淆替换值: - 基础替换:数字 → `1`,小写字母 → `a`,大写字母 → `A`(保留非字母数字字符) - 如果基础值已被占用,则在每个字符类别中从右向左递增(`1→2→…→9`、`a→b→…→z`、`A→B→…→Z`),溢出时向左进位;如果所有位置都已用尽,则在前面添加一个字符 - 排序确保对于相同的输入集合,映射是确定且可重现的 3. 将每个文件中收集到的值作为文本进行替换(保留 json5/jsonc 等的结构、顺序、注释等) ## 可能的功能(按需提供) 欢迎反馈、PR 和功能请求。例如: - ~~提供敏感字段列表的选项~~ - ~~从敏感模式中排除某些字段名称的选项~~ - 提供固定替换的选项(使用查找表) - 针对不同字母表、emoji 等计算替换值的选项 - 使用随机替换的选项(这会破坏幂等性)
标签:JSON, Rust, 可视化界面, 数据脱敏, 文档结构分析, 测试工具, 网络流量审计, 通知系统