deepspider 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/.claude/agents/check.md +122 -0
  2. package/.claude/agents/debug.md +106 -0
  3. package/.claude/agents/dispatch.md +214 -0
  4. package/.claude/agents/implement.md +96 -0
  5. package/.claude/agents/plan.md +396 -0
  6. package/.claude/agents/research.md +120 -0
  7. package/.claude/commands/evolve/merge.md +80 -0
  8. package/.claude/commands/trellis/before-backend-dev.md +13 -0
  9. package/.claude/commands/trellis/before-frontend-dev.md +13 -0
  10. package/.claude/commands/trellis/break-loop.md +107 -0
  11. package/.claude/commands/trellis/check-backend.md +13 -0
  12. package/.claude/commands/trellis/check-cross-layer.md +153 -0
  13. package/.claude/commands/trellis/check-frontend.md +13 -0
  14. package/.claude/commands/trellis/create-command.md +154 -0
  15. package/.claude/commands/trellis/finish-work.md +129 -0
  16. package/.claude/commands/trellis/integrate-skill.md +219 -0
  17. package/.claude/commands/trellis/onboard.md +358 -0
  18. package/.claude/commands/trellis/parallel.md +193 -0
  19. package/.claude/commands/trellis/record-session.md +62 -0
  20. package/.claude/commands/trellis/start.md +280 -0
  21. package/.claude/commands/trellis/update-spec.md +213 -0
  22. package/.claude/hooks/inject-subagent-context.py +758 -0
  23. package/.claude/hooks/ralph-loop.py +374 -0
  24. package/.claude/hooks/session-start.py +126 -0
  25. package/.claude/settings.json +41 -0
  26. package/.claude/skills/deepagents-guide/SKILL.md +428 -0
  27. package/.cursor/commands/trellis-before-backend-dev.md +13 -0
  28. package/.cursor/commands/trellis-before-frontend-dev.md +13 -0
  29. package/.cursor/commands/trellis-break-loop.md +107 -0
  30. package/.cursor/commands/trellis-check-backend.md +13 -0
  31. package/.cursor/commands/trellis-check-cross-layer.md +153 -0
  32. package/.cursor/commands/trellis-check-frontend.md +13 -0
  33. package/.cursor/commands/trellis-create-command.md +154 -0
  34. package/.cursor/commands/trellis-finish-work.md +129 -0
  35. package/.cursor/commands/trellis-integrate-skill.md +219 -0
  36. package/.cursor/commands/trellis-onboard.md +358 -0
  37. package/.cursor/commands/trellis-record-session.md +62 -0
  38. package/.cursor/commands/trellis-start.md +156 -0
  39. package/.cursor/commands/trellis-update-spec.md +213 -0
  40. package/.env.example +11 -0
  41. package/.husky/pre-commit +1 -0
  42. package/.mcp.json +8 -0
  43. package/.trellis/.template-hashes.json +65 -0
  44. package/.trellis/.version +1 -0
  45. package/.trellis/scripts/add-session.sh +384 -0
  46. package/.trellis/scripts/common/developer.sh +129 -0
  47. package/.trellis/scripts/common/git-context.sh +263 -0
  48. package/.trellis/scripts/common/paths.sh +208 -0
  49. package/.trellis/scripts/common/phase.sh +150 -0
  50. package/.trellis/scripts/common/registry.sh +247 -0
  51. package/.trellis/scripts/common/task-queue.sh +142 -0
  52. package/.trellis/scripts/common/task-utils.sh +151 -0
  53. package/.trellis/scripts/common/worktree.sh +128 -0
  54. package/.trellis/scripts/create-bootstrap.sh +299 -0
  55. package/.trellis/scripts/get-context.sh +7 -0
  56. package/.trellis/scripts/get-developer.sh +15 -0
  57. package/.trellis/scripts/init-developer.sh +34 -0
  58. package/.trellis/scripts/multi-agent/cleanup.sh +396 -0
  59. package/.trellis/scripts/multi-agent/create-pr.sh +241 -0
  60. package/.trellis/scripts/multi-agent/plan.sh +207 -0
  61. package/.trellis/scripts/multi-agent/start.sh +310 -0
  62. package/.trellis/scripts/multi-agent/status.sh +828 -0
  63. package/.trellis/scripts/task.sh +1118 -0
  64. package/.trellis/spec/backend/deepagents-guide.md +337 -0
  65. package/.trellis/spec/backend/directory-structure.md +126 -0
  66. package/.trellis/spec/backend/examples/skills/deepagents-guide/README.md +11 -0
  67. package/.trellis/spec/backend/examples/skills/deepagents-guide/agent.js.template +20 -0
  68. package/.trellis/spec/backend/examples/skills/deepagents-guide/skills-config.js.template +13 -0
  69. package/.trellis/spec/backend/examples/skills/deepagents-guide/subagent.js.template +19 -0
  70. package/.trellis/spec/backend/hook-guidelines.md +178 -0
  71. package/.trellis/spec/backend/index.md +36 -0
  72. package/.trellis/spec/backend/quality-guidelines.md +201 -0
  73. package/.trellis/spec/backend/state-management.md +76 -0
  74. package/.trellis/spec/backend/tool-guidelines.md +144 -0
  75. package/.trellis/spec/backend/type-safety.md +71 -0
  76. package/.trellis/spec/guides/code-reuse-thinking-guide.md +92 -0
  77. package/.trellis/spec/guides/cross-layer-thinking-guide.md +94 -0
  78. package/.trellis/spec/guides/index.md +79 -0
  79. package/.trellis/tasks/archive/02-02-evolving-skills/prd.md +61 -0
  80. package/.trellis/tasks/archive/02-02-evolving-skills/task.json +29 -0
  81. package/.trellis/tasks/archive/2026-02/00-bootstrap-guidelines/prd.md +86 -0
  82. package/.trellis/tasks/archive/2026-02/00-bootstrap-guidelines/task.json +27 -0
  83. package/.trellis/tasks/archive/2026-02/02-02-skills-system/check.jsonl +3 -0
  84. package/.trellis/tasks/archive/2026-02/02-02-skills-system/debug.jsonl +2 -0
  85. package/.trellis/tasks/archive/2026-02/02-02-skills-system/implement.jsonl +5 -0
  86. package/.trellis/tasks/archive/2026-02/02-02-skills-system/prd.md +33 -0
  87. package/.trellis/tasks/archive/2026-02/02-02-skills-system/task.json +41 -0
  88. package/.trellis/workflow.md +407 -0
  89. package/.trellis/workspace/index.md +123 -0
  90. package/.trellis/workspace/pony/index.md +40 -0
  91. package/.trellis/workspace/pony/journal-1.md +7 -0
  92. package/.trellis/worktree.yaml +47 -0
  93. package/AGENTS.md +18 -0
  94. package/CLAUDE.md +292 -0
  95. package/README.md +134 -0
  96. package/agents/deepspider.md +142 -0
  97. package/docs/DEBUG.md +42 -0
  98. package/docs/GUIDE.md +334 -0
  99. package/docs/PROMPT.md +60 -0
  100. package/docs/USAGE.md +226 -0
  101. package/eslint.config.js +51 -0
  102. package/package.json +78 -0
  103. package/requirements-crypto.txt +14 -0
  104. package/src/agent/index.js +97 -0
  105. package/src/agent/logger.js +164 -0
  106. package/src/agent/middleware/filterTools.js +64 -0
  107. package/src/agent/middleware/report.js +79 -0
  108. package/src/agent/prompts/system.js +315 -0
  109. package/src/agent/run.js +575 -0
  110. package/src/agent/skills/anti-detect/SKILL.md +28 -0
  111. package/src/agent/skills/anti-detect/evolved.md +12 -0
  112. package/src/agent/skills/captcha/SKILL.md +37 -0
  113. package/src/agent/skills/captcha/evolved.md +12 -0
  114. package/src/agent/skills/config.js +30 -0
  115. package/src/agent/skills/crawler/SKILL.md +9 -0
  116. package/src/agent/skills/crawler/evolved.md +16 -0
  117. package/src/agent/skills/dynamic-analysis/SKILL.md +91 -0
  118. package/src/agent/skills/dynamic-analysis/evolved.md +12 -0
  119. package/src/agent/skills/env/SKILL.md +72 -0
  120. package/src/agent/skills/env/evolved.md +12 -0
  121. package/src/agent/skills/evolve.js +79 -0
  122. package/src/agent/skills/general/SKILL.md +12 -0
  123. package/src/agent/skills/general/evolved.md +12 -0
  124. package/src/agent/skills/js2python/SKILL.md +30 -0
  125. package/src/agent/skills/js2python/evolved.md +13 -0
  126. package/src/agent/skills/report/SKILL.md +21 -0
  127. package/src/agent/skills/report/evolved.md +12 -0
  128. package/src/agent/skills/sandbox/SKILL.md +22 -0
  129. package/src/agent/skills/sandbox/evolved.md +16 -0
  130. package/src/agent/skills/static-analysis/SKILL.md +93 -0
  131. package/src/agent/skills/static-analysis/evolved.md +12 -0
  132. package/src/agent/skills/xpath/SKILL.md +119 -0
  133. package/src/agent/subagents/anti-detect.js +45 -0
  134. package/src/agent/subagents/captcha.js +51 -0
  135. package/src/agent/subagents/crawler.js +138 -0
  136. package/src/agent/subagents/dynamic.js +64 -0
  137. package/src/agent/subagents/env-agent.js +82 -0
  138. package/src/agent/subagents/index.js +37 -0
  139. package/src/agent/subagents/js2python.js +72 -0
  140. package/src/agent/subagents/sandbox.js +55 -0
  141. package/src/agent/subagents/static.js +66 -0
  142. package/src/agent/tools/analysis.js +135 -0
  143. package/src/agent/tools/analyzer.js +85 -0
  144. package/src/agent/tools/anti-detect.js +89 -0
  145. package/src/agent/tools/antidebug.js +64 -0
  146. package/src/agent/tools/async.js +43 -0
  147. package/src/agent/tools/browser.js +324 -0
  148. package/src/agent/tools/captcha.js +223 -0
  149. package/src/agent/tools/capture.js +179 -0
  150. package/src/agent/tools/correlate.js +303 -0
  151. package/src/agent/tools/crawler.js +116 -0
  152. package/src/agent/tools/cryptohook.js +80 -0
  153. package/src/agent/tools/debug.js +246 -0
  154. package/src/agent/tools/deobfuscator.js +90 -0
  155. package/src/agent/tools/env.js +83 -0
  156. package/src/agent/tools/envdump.js +92 -0
  157. package/src/agent/tools/evolve.js +164 -0
  158. package/src/agent/tools/extract.js +114 -0
  159. package/src/agent/tools/extractor.js +54 -0
  160. package/src/agent/tools/file.js +224 -0
  161. package/src/agent/tools/hook.js +84 -0
  162. package/src/agent/tools/hookManager.js +178 -0
  163. package/src/agent/tools/index.js +137 -0
  164. package/src/agent/tools/nodejs.js +101 -0
  165. package/src/agent/tools/patch.js +46 -0
  166. package/src/agent/tools/preprocess.js +71 -0
  167. package/src/agent/tools/profile.js +122 -0
  168. package/src/agent/tools/python.js +627 -0
  169. package/src/agent/tools/report.js +124 -0
  170. package/src/agent/tools/runtime.js +132 -0
  171. package/src/agent/tools/sandbox.js +79 -0
  172. package/src/agent/tools/store.js +73 -0
  173. package/src/agent/tools/trace.js +74 -0
  174. package/src/agent/tools/tracing.js +201 -0
  175. package/src/agent/tools/utils.js +51 -0
  176. package/src/agent/tools/verify.js +184 -0
  177. package/src/agent/tools/webcrack.js +109 -0
  178. package/src/analyzer/ASTAnalyzer.js +387 -0
  179. package/src/analyzer/CallStackAnalyzer.js +379 -0
  180. package/src/analyzer/Deobfuscator.js +289 -0
  181. package/src/analyzer/EncryptionAnalyzer.js +99 -0
  182. package/src/analyzer/index.js +22 -0
  183. package/src/browser/EnvBridge.js +186 -0
  184. package/src/browser/cdp.js +168 -0
  185. package/src/browser/client.js +197 -0
  186. package/src/browser/collector.js +444 -0
  187. package/src/browser/collectors/RequestCryptoLinker.js +109 -0
  188. package/src/browser/collectors/ResponseSearcher.js +107 -0
  189. package/src/browser/collectors/ScriptCollector.js +158 -0
  190. package/src/browser/collectors/index.js +26 -0
  191. package/src/browser/defaultHooks.js +932 -0
  192. package/src/browser/hooks/crypto.js +55 -0
  193. package/src/browser/hooks/index.js +64 -0
  194. package/src/browser/hooks/native.js +9 -0
  195. package/src/browser/hooks/network.js +33 -0
  196. package/src/browser/index.js +42 -0
  197. package/src/browser/interceptors/NetworkInterceptor.js +116 -0
  198. package/src/browser/interceptors/ScriptInterceptor.js +76 -0
  199. package/src/browser/interceptors/index.js +6 -0
  200. package/src/browser/ui/analysisPanel.js +1782 -0
  201. package/src/browser/ui/confirmDialog.js +158 -0
  202. package/src/browser/ui/panel.html +152 -0
  203. package/src/browser/ui/selector.js +170 -0
  204. package/src/config/index.js +5 -0
  205. package/src/config/paths.js +71 -0
  206. package/src/config/patterns/crypto.js +36 -0
  207. package/src/config/profiles/chrome.json +71 -0
  208. package/src/config/profiles/firefox.json +44 -0
  209. package/src/config/profiles/safari.json +38 -0
  210. package/src/core/EnvMonitor.js +200 -0
  211. package/src/core/PatchGenerator.js +278 -0
  212. package/src/core/Sandbox.js +181 -0
  213. package/src/env/AntiAntiDebug.js +111 -0
  214. package/src/env/AsyncHook.js +68 -0
  215. package/src/env/BrowserAPIList.js +265 -0
  216. package/src/env/CookieHook.js +48 -0
  217. package/src/env/CryptoHook.js +205 -0
  218. package/src/env/EnvCodeGenerator.js +157 -0
  219. package/src/env/EnvDumper.js +356 -0
  220. package/src/env/EnvExtractor.js +220 -0
  221. package/src/env/HookBase.js +618 -0
  222. package/src/env/NetworkHook.js +159 -0
  223. package/src/env/modules/bom/history.js +29 -0
  224. package/src/env/modules/bom/location.js +26 -0
  225. package/src/env/modules/bom/navigator.js +70 -0
  226. package/src/env/modules/bom/screen.js +26 -0
  227. package/src/env/modules/bom/storage.js +23 -0
  228. package/src/env/modules/dom/document.js +110 -0
  229. package/src/env/modules/dom/event.js +51 -0
  230. package/src/env/modules/index.js +34 -0
  231. package/src/env/modules/webapi/fetch.js +46 -0
  232. package/src/env/modules/webapi/url.js +47 -0
  233. package/src/env/modules/webapi/xhr.js +48 -0
  234. package/src/index.js +27 -0
  235. package/src/mcp/server.js +89 -0
  236. package/src/store/DataStore.js +708 -0
  237. package/src/store/Store.js +158 -0
  238. package/src/store/Validator.js +24 -0
  239. package/test/analyze.test.js +90 -0
  240. package/test/envdump.test.js +74 -0
  241. package/test/flow.test.js +90 -0
  242. package/test/hooks.test.js +138 -0
  243. package/test/plugin.test.js +35 -0
  244. package/test/refactor-full.test.js +30 -0
  245. package/test/refactor.test.js +21 -0
  246. package/test/samples/obfuscated.js +61 -0
  247. package/test/samples/original.js +66 -0
  248. package/test/samples/v10_eval_chain.js +52 -0
  249. package/test/samples/v11_bytecode_vm.js +81 -0
  250. package/test/samples/v12_polymorphic.js +69 -0
  251. package/test/samples/v1_ob_basic.js +98 -0
  252. package/test/samples/v2_ob_advanced.js +99 -0
  253. package/test/samples/v3_jjencode.js +77 -0
  254. package/test/samples/v4_aaencode.js +73 -0
  255. package/test/samples/v5_control_flow.js +86 -0
  256. package/test/samples/v6_string_encryption.js +71 -0
  257. package/test/samples/v7_jsvmp.js +83 -0
  258. package/test/samples/v8_anti_debug.js +79 -0
  259. package/test/samples/v9_proxy_trap.js +49 -0
  260. package/test/samples.test.js +96 -0
  261. package/test/webcrack.test.js +55 -0
@@ -0,0 +1,387 @@
1
+ /**
2
+ * DeepSpider - AST 分析器
3
+ * 函数依赖图、变量作用域、调用链追踪、代码切片
4
+ */
5
+
6
+ import { parse } from '@babel/parser';
7
+ import traverse from '@babel/traverse';
8
+ import generate from '@babel/generator';
9
+
10
+ export class ASTAnalyzer {
11
+ constructor() {
12
+ this.ast = null;
13
+ this.code = '';
14
+ }
15
+
16
+ parse(code) {
17
+ this.code = code;
18
+ this.ast = parse(code, {
19
+ sourceType: 'unambiguous',
20
+ plugins: ['jsx', 'typescript', 'decorators-legacy'],
21
+ errorRecovery: true,
22
+ });
23
+ return this.ast;
24
+ }
25
+
26
+ _getMemberPath(node) {
27
+ if (!node) return '';
28
+ if (node.type === 'Identifier') return node.name;
29
+ if (node.type === 'ThisExpression') return 'this';
30
+ if (node.type === 'MemberExpression') {
31
+ const obj = this._getMemberPath(node.object);
32
+ const prop = node.computed
33
+ ? `[${node.property.name || node.property.value || '?'}]`
34
+ : node.property.name || node.property.value;
35
+ return obj ? `${obj}.${prop}` : prop;
36
+ }
37
+ return '';
38
+ }
39
+
40
+ extractFunctions(code) {
41
+ const ast = this.parse(code);
42
+ const functions = [];
43
+
44
+ traverse.default(ast, {
45
+ FunctionDeclaration: (path) => {
46
+ const node = path.node;
47
+ functions.push({
48
+ type: 'declaration',
49
+ name: node.id?.name || 'anonymous',
50
+ params: node.params.map(p => this._getParamName(p)),
51
+ start: node.start,
52
+ end: node.end,
53
+ loc: node.loc,
54
+ });
55
+ },
56
+ FunctionExpression: (path) => {
57
+ const node = path.node;
58
+ functions.push({
59
+ type: 'expression',
60
+ name: node.id?.name || 'anonymous',
61
+ params: node.params.map(p => this._getParamName(p)),
62
+ start: node.start,
63
+ end: node.end,
64
+ loc: node.loc,
65
+ });
66
+ },
67
+ ArrowFunctionExpression: (path) => {
68
+ const node = path.node;
69
+ functions.push({
70
+ type: 'arrow',
71
+ name: 'arrow',
72
+ params: node.params.map(p => this._getParamName(p)),
73
+ start: node.start,
74
+ end: node.end,
75
+ loc: node.loc,
76
+ });
77
+ }
78
+ });
79
+
80
+ return functions;
81
+ }
82
+
83
+ _getParamName(param) {
84
+ if (param.type === 'Identifier') return param.name;
85
+ if (param.type === 'RestElement') return `...${param.argument.name}`;
86
+ if (param.type === 'AssignmentPattern') return param.left.name;
87
+ if (param.type === 'ObjectPattern') return '{...}';
88
+ if (param.type === 'ArrayPattern') return '[...]';
89
+ return 'param';
90
+ }
91
+
92
+ extractCalls(code) {
93
+ const ast = this.parse(code);
94
+ const calls = [];
95
+
96
+ traverse.default(ast, {
97
+ CallExpression: (path) => {
98
+ const node = path.node;
99
+ let name = '';
100
+ if (node.callee.type === 'Identifier') {
101
+ name = node.callee.name;
102
+ } else if (node.callee.type === 'MemberExpression') {
103
+ name = this._getMemberPath(node.callee);
104
+ }
105
+ calls.push({
106
+ name,
107
+ args: node.arguments.length,
108
+ start: node.start,
109
+ loc: node.loc
110
+ });
111
+ }
112
+ });
113
+
114
+ return calls;
115
+ }
116
+
117
+ // 构建函数依赖图
118
+ buildDependencyGraph(code) {
119
+ const ast = this.parse(code);
120
+ const graph = new Map();
121
+ const funcNames = new Set();
122
+ const funcCalls = new Map();
123
+
124
+ // 一次遍历收集函数定义和调用
125
+ traverse.default(ast, {
126
+ FunctionDeclaration: (path) => {
127
+ const name = path.node.id?.name;
128
+ if (name) {
129
+ funcNames.add(name);
130
+ const calls = new Set();
131
+ path.traverse({
132
+ CallExpression(innerPath) {
133
+ if (innerPath.node.callee.type === 'Identifier') {
134
+ calls.add(innerPath.node.callee.name);
135
+ }
136
+ }
137
+ });
138
+ funcCalls.set(name, calls);
139
+ }
140
+ },
141
+ VariableDeclarator: (path) => {
142
+ const node = path.node;
143
+ if ((node.init?.type === 'FunctionExpression' ||
144
+ node.init?.type === 'ArrowFunctionExpression') &&
145
+ node.id.type === 'Identifier') {
146
+ const name = node.id.name;
147
+ funcNames.add(name);
148
+ const calls = new Set();
149
+ path.traverse({
150
+ CallExpression(innerPath) {
151
+ if (innerPath.node.callee.type === 'Identifier') {
152
+ calls.add(innerPath.node.callee.name);
153
+ }
154
+ }
155
+ });
156
+ funcCalls.set(name, calls);
157
+ }
158
+ }
159
+ });
160
+
161
+ // 过滤只保留内部函数调用
162
+ for (const [name, calls] of funcCalls) {
163
+ const filtered = Array.from(calls).filter(c => funcNames.has(c));
164
+ graph.set(name, filtered);
165
+ }
166
+
167
+ return graph;
168
+ }
169
+
170
+ // 变量作用域分析
171
+ analyzeScope(code) {
172
+ const ast = this.parse(code);
173
+ const scopes = [];
174
+
175
+ traverse.default(ast, {
176
+ FunctionDeclaration: (path) => {
177
+ const node = path.node;
178
+ scopes.push({
179
+ type: 'function',
180
+ name: node.id?.name,
181
+ params: node.params.map(p => this._getParamName(p)),
182
+ });
183
+ },
184
+ FunctionExpression: (path) => {
185
+ const node = path.node;
186
+ scopes.push({
187
+ type: 'function',
188
+ name: node.id?.name || 'anonymous',
189
+ params: node.params.map(p => this._getParamName(p)),
190
+ });
191
+ },
192
+ });
193
+
194
+ return scopes;
195
+ }
196
+
197
+ // 代码切片提取(增强版 - 包含变量依赖)
198
+ extractSlice(code, funcName) {
199
+ const ast = this.parse(code);
200
+ const graph = this.buildDependencyGraph(code);
201
+ const needed = new Set([funcName]);
202
+ const globalVars = new Set();
203
+
204
+ // 递归收集函数依赖
205
+ const collectFuncs = (name) => {
206
+ const deps = graph.get(name) || [];
207
+ for (const dep of deps) {
208
+ if (!needed.has(dep)) {
209
+ needed.add(dep);
210
+ collectFuncs(dep);
211
+ }
212
+ }
213
+ };
214
+ collectFuncs(funcName);
215
+
216
+ // 收集全局变量依赖
217
+ traverse.default(ast, {
218
+ FunctionDeclaration: (path) => {
219
+ const node = path.node;
220
+ if (node.id && needed.has(node.id.name)) {
221
+ path.traverse({
222
+ Identifier: (innerPath) => {
223
+ if (!needed.has(innerPath.node.name)) {
224
+ globalVars.add(innerPath.node.name);
225
+ }
226
+ }
227
+ });
228
+ }
229
+ }
230
+ });
231
+
232
+ // 提取代码
233
+ const slices = [];
234
+
235
+ // 先提取全局变量
236
+ traverse.default(ast, {
237
+ VariableDeclaration: (path) => {
238
+ const node = path.node;
239
+ for (const decl of node.declarations) {
240
+ if (decl.id.type === 'Identifier' && globalVars.has(decl.id.name)) {
241
+ slices.push(this.code.slice(node.start, node.end));
242
+ break;
243
+ }
244
+ }
245
+ }
246
+ });
247
+
248
+ // 再提取函数
249
+ traverse.default(ast, {
250
+ FunctionDeclaration: (path) => {
251
+ const node = path.node;
252
+ if (node.id && needed.has(node.id.name)) {
253
+ slices.push(this.code.slice(node.start, node.end));
254
+ }
255
+ },
256
+ VariableDeclarator: (path) => {
257
+ const node = path.node;
258
+ if (node.id.type === 'Identifier' && needed.has(node.id.name)) {
259
+ if (node.init?.type === 'FunctionExpression' ||
260
+ node.init?.type === 'ArrowFunctionExpression') {
261
+ const start = node.start;
262
+ const end = node.end;
263
+ slices.push(`var ${this.code.slice(start, end)};`);
264
+ }
265
+ }
266
+ }
267
+ });
268
+
269
+ return slices.join('\n\n');
270
+ }
271
+
272
+ // 查找特定模式的调用(逆向分析常用)
273
+ findCallPattern(code, pattern) {
274
+ const ast = this.parse(code);
275
+ const matches = [];
276
+ const regex = new RegExp(pattern, 'i');
277
+
278
+ traverse.default(ast, {
279
+ CallExpression: (path) => {
280
+ const node = path.node;
281
+ const name = node.callee.type === 'Identifier'
282
+ ? node.callee.name
283
+ : this._getMemberPath(node.callee);
284
+
285
+ if (regex.test(name)) {
286
+ matches.push({
287
+ name,
288
+ args: node.arguments.map(a => this.code.slice(a.start, a.end)),
289
+ start: node.start,
290
+ end: node.end,
291
+ loc: node.loc,
292
+ code: this.code.slice(node.start, node.end)
293
+ });
294
+ }
295
+ }
296
+ });
297
+
298
+ return matches;
299
+ }
300
+
301
+ // 查找赋值语句(追踪变量来源)
302
+ findAssignments(code, varName) {
303
+ const ast = this.parse(code);
304
+ const assignments = [];
305
+
306
+ traverse.default(ast, {
307
+ AssignmentExpression: (path) => {
308
+ const node = path.node;
309
+ const left = node.left.type === 'Identifier'
310
+ ? node.left.name
311
+ : this._getMemberPath(node.left);
312
+
313
+ if (left === varName || left.startsWith(varName + '.')) {
314
+ assignments.push({
315
+ target: left,
316
+ operator: node.operator,
317
+ value: this.code.slice(node.right.start, node.right.end),
318
+ valueType: node.right.type,
319
+ start: node.start,
320
+ loc: node.loc
321
+ });
322
+ }
323
+ },
324
+ VariableDeclarator: (path) => {
325
+ const node = path.node;
326
+ if (node.id.type === 'Identifier' && node.id.name === varName && node.init) {
327
+ assignments.push({
328
+ target: varName,
329
+ operator: '=',
330
+ value: this.code.slice(node.init.start, node.init.end),
331
+ valueType: node.init.type,
332
+ start: node.start,
333
+ loc: node.loc,
334
+ isDeclaration: true
335
+ });
336
+ }
337
+ }
338
+ });
339
+
340
+ return assignments;
341
+ }
342
+
343
+ // 提取字符串字面量(用于分析混淆代码)
344
+ extractStrings(code) {
345
+ const ast = this.parse(code);
346
+ const strings = [];
347
+
348
+ traverse.default(ast, {
349
+ StringLiteral: (path) => {
350
+ const node = path.node;
351
+ if (node.value.length > 0) {
352
+ strings.push({
353
+ value: node.value,
354
+ raw: node.extra?.raw || `"${node.value}"`,
355
+ start: node.start,
356
+ loc: node.loc
357
+ });
358
+ }
359
+ },
360
+ TemplateLiteral: (path) => {
361
+ const node = path.node;
362
+ node.quasis.forEach(q => {
363
+ if (q.value.cooked) {
364
+ strings.push({
365
+ value: q.value.cooked,
366
+ raw: q.value.raw,
367
+ start: q.start,
368
+ loc: q.loc,
369
+ isTemplate: true
370
+ });
371
+ }
372
+ });
373
+ }
374
+ });
375
+
376
+ return strings;
377
+ }
378
+
379
+ // 生成代码(从 AST 还原)
380
+ generate(ast) {
381
+ return generate(ast || this.ast, {
382
+ format: { indent: { style: ' ' }, quotes: 'single' }
383
+ });
384
+ }
385
+ }
386
+
387
+ export default ASTAnalyzer;