Abdoulrazack1/Logic-Lens

GitHub: Abdoulrazack1/Logic-Lens

Logic-Lens 利用 AST 突变与 TensorFlow.js Transformer 模型,将 JavaScript 函数自动转换为底层数学公式或逻辑不变量,解决代码逻辑自动提取与理解的问题。

Stars: 0 | Forks: 0

# 🔭 Logic-Lens [![Node](https://img.shields.io/badge/node-%E2%89%A518-339933?logo=node.js&logoColor=white)](https://nodejs.org/) [![TensorFlow.js](https://img.shields.io/badge/TensorFlow.js-FF6F00?logo=tensorflow&logoColor=white)](https://www.tensorflow.org/js) [![Acorn](https://img.shields.io/badge/AST-Acorn-7c5cff)](https://github.com/acornjs/acorn) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) ## 📸 Web 界面 (`npm run serve` → `:3000`) [![Logic-Lens 界面 — 代码编辑器 + 分析标签页](https://static.pigsec.cn/wp-content/uploads/repos/cas/d7/d76652520307600d91c8d59635c7c00bd3c8b944a6c0de12d495be7fb46cd3a7.png)](screenshots/home.png) ## 💡 用途 Logic-Lens 将 JavaScript 函数转换为**其底层数学公式**——不仅是自然语言的摘要,而是函数计算的精确逻辑不变量。 ### 用例 | 适用对象 | 用例 | |---|---| | **逆向工程** | 理解混淆的、旧的或无注释的函数 | | **安全重构** | 验证你的重写是否保留了原始代码的逻辑不变量 | | **教学** | 为学生展示 `code → math` 的转换过程 | | **代码审计** | 检测揭示漏洞或意外行为的逻辑模式 | | **静态分析研究** | 用于对 AST → 公式模型进行基准测试的可复现数据集 | ### 示例 ``` // Entrée function fib(n) { if (n < 2) return n; return fib(n - 1) + fib(n - 2); } // Sortie Logic-Lens // Formule : F(n) = F(n-1) + F(n-2), F(0)=0, F(1)=1 (Fibonacci) // Confiance : 0.94 ``` ## 🧠 工作原理 Logic-Lens 结合了两种技术: 1. **AST 突变**(通过 `acorn`)—— 为同一个函数生成约 3000 种语法变体(重命名、重排序、内联、表达式等价) 2. **TF.js Transformer Encoder** —— 从这些合成对中学习 `AST → 数学公式` 的映射 ``` ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ Fonction JS │ → │ Acorn AST │ → │ Mutations AST│ → │ TF.js model │ │ │ │ │ │ (~3000) │ │ → formule │ └──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘ ``` 使用了 TF.js 的 CPU 后端 —— **无需原生编译,推理无需 GPU**。 ## 📦 快速开始 ``` git clone https://github.com/Abdoulrazack1/Logic-Lens.git cd Logic-Lens npm install ``` ### 3 步流水线 ``` # 1 — 生成 dataset(约 3025 对 mutated code → formula) npm run generate # 2 — 训练 model(500 epochs ≈ 几分钟 CPU) npm run train # 变体: node src/train.js --epochs 1000 node src/train.js --epochs 1000 --lr 0.0003 # 3 — 使用 node index.js analyze examples/fibonacci.js node index.js snippet "function f(x) { return x > 0 ? x : 0; }" node index.js url https://raw.githubusercontent.com/user/repo/main/math.js node index.js compare examples/linear.js examples/fibonacci.js node index.js demo # démonstration complète node index.js status # état du modèle # 4 — Web 界面(推荐用于探索) npm run serve # → http://127.0.0.1:3000 ``` ### 跨引擎桥接(可选) ``` npm run bridge # → http://127.0.0.1:4000 ``` 此桥接允许 Logic-Lens 与其他引擎(Js-Ranker,未来的 Logic-Lens-Python)进行通信。参见 [`bridge/bridge-protocol.md`](bridge/bridge-protocol.md)。 ## 🎯 成功示例 | 输入函数 | 提取的公式 | 置信度 | |---|---|---| | `fact(n) = n * fact(n-1)` | `n!` | 0.96 | | `fib(n) = fib(n-1) + fib(n-2)` | `F(n) = F(n-1) + F(n-2)` | 0.94 | | `Math.abs(x)` | `|x|` | 0.99 | | `clamp(x, lo, hi)` | `min(hi, max(lo, x))` | 0.88 | | `lerp(a, b, t)` | `a + t*(b - a)` | 0.92 | 完整源码请参见 `examples/`。 ## 🛠️ 技术栈 - **TensorFlow.js**(CPU 后端)—— Transformer Encoder 模型 - **Acorn** —— AST 解析,语法突变 - **Express** —— API 服务器 + Web 界面 - **Node.js ≥ 18** ## 📚 完整文档 - [`docs/DOCUMENTATION.md`](docs/DOCUMENTATION.md) —— 详细架构、数据集格式、设计选择 - [`bridge/bridge-protocol.md`](bridge/bridge-protocol.md) —— 跨引擎交换协议 ## ⚠️ 已知限制 - **准确率**在训练集的函数上约为 70-90%,在相差甚远的函数上(复杂的闭包、IIFE、generators)会**下降** - **无副作用** —— Logic-Lens 推理的是纯函数(无 I/O,无 DOM 操作) - **目前仅支持 JavaScript** —— 正在考虑开发 Python 版本 ## 🤝 贡献 ## 📜 许可证 MIT —— 随心所欲使用。 ## 🔗 链接 - **作者** : [@Abdoulrazack1](https://github.com/Abdoulrazack1) - **技术文章** : [`promo/devto-article.md`](promo/devto-article.md) (待发布)
标签:Apex, CMS安全, JavaScript, MITM代理, TensorFlow.js, 云资产清单, 代码分析, 凭证管理, 数学公式提取, 机器学习, 自定义脚本, 逆向工程