windows-exe-decompiler-mcp-server 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 (190) hide show
  1. package/CODEX_INSTALLATION.md +69 -0
  2. package/COPILOT_INSTALLATION.md +77 -0
  3. package/LICENSE +21 -0
  4. package/README.md +314 -0
  5. package/bin/windows-exe-decompiler-mcp-server.js +3 -0
  6. package/dist/analysis-provenance.d.ts +184 -0
  7. package/dist/analysis-provenance.js +74 -0
  8. package/dist/analysis-task-runner.d.ts +31 -0
  9. package/dist/analysis-task-runner.js +160 -0
  10. package/dist/artifact-inventory.d.ts +23 -0
  11. package/dist/artifact-inventory.js +175 -0
  12. package/dist/cache-manager.d.ts +128 -0
  13. package/dist/cache-manager.js +454 -0
  14. package/dist/confidence-semantics.d.ts +66 -0
  15. package/dist/confidence-semantics.js +122 -0
  16. package/dist/config.d.ts +335 -0
  17. package/dist/config.js +193 -0
  18. package/dist/database.d.ts +227 -0
  19. package/dist/database.js +601 -0
  20. package/dist/decompiler-worker.d.ts +441 -0
  21. package/dist/decompiler-worker.js +1962 -0
  22. package/dist/dynamic-trace.d.ts +95 -0
  23. package/dist/dynamic-trace.js +629 -0
  24. package/dist/env-validator.d.ts +15 -0
  25. package/dist/env-validator.js +249 -0
  26. package/dist/error-handler.d.ts +28 -0
  27. package/dist/error-handler.example.d.ts +22 -0
  28. package/dist/error-handler.example.js +141 -0
  29. package/dist/error-handler.js +139 -0
  30. package/dist/ghidra-analysis-status.d.ts +49 -0
  31. package/dist/ghidra-analysis-status.js +178 -0
  32. package/dist/ghidra-config.d.ts +134 -0
  33. package/dist/ghidra-config.js +464 -0
  34. package/dist/index.d.ts +9 -0
  35. package/dist/index.js +200 -0
  36. package/dist/job-queue.d.ts +169 -0
  37. package/dist/job-queue.js +407 -0
  38. package/dist/logger.d.ts +106 -0
  39. package/dist/logger.js +176 -0
  40. package/dist/policy-guard.d.ts +115 -0
  41. package/dist/policy-guard.js +243 -0
  42. package/dist/process-output.d.ts +15 -0
  43. package/dist/process-output.js +90 -0
  44. package/dist/prompts/function-explanation-review.d.ts +5 -0
  45. package/dist/prompts/function-explanation-review.js +64 -0
  46. package/dist/prompts/semantic-name-review.d.ts +5 -0
  47. package/dist/prompts/semantic-name-review.js +63 -0
  48. package/dist/runtime-correlation.d.ts +34 -0
  49. package/dist/runtime-correlation.js +279 -0
  50. package/dist/runtime-paths.d.ts +3 -0
  51. package/dist/runtime-paths.js +11 -0
  52. package/dist/selection-diff.d.ts +667 -0
  53. package/dist/selection-diff.js +53 -0
  54. package/dist/semantic-name-suggestion-artifacts.d.ts +116 -0
  55. package/dist/semantic-name-suggestion-artifacts.js +314 -0
  56. package/dist/server.d.ts +129 -0
  57. package/dist/server.js +578 -0
  58. package/dist/tools/artifact-read.d.ts +235 -0
  59. package/dist/tools/artifact-read.js +317 -0
  60. package/dist/tools/artifacts-diff.d.ts +728 -0
  61. package/dist/tools/artifacts-diff.js +304 -0
  62. package/dist/tools/artifacts-list.d.ts +515 -0
  63. package/dist/tools/artifacts-list.js +389 -0
  64. package/dist/tools/attack-map.d.ts +290 -0
  65. package/dist/tools/attack-map.js +519 -0
  66. package/dist/tools/cache-observability.d.ts +4 -0
  67. package/dist/tools/cache-observability.js +36 -0
  68. package/dist/tools/code-function-cfg.d.ts +50 -0
  69. package/dist/tools/code-function-cfg.js +102 -0
  70. package/dist/tools/code-function-decompile.d.ts +55 -0
  71. package/dist/tools/code-function-decompile.js +103 -0
  72. package/dist/tools/code-function-disassemble.d.ts +43 -0
  73. package/dist/tools/code-function-disassemble.js +185 -0
  74. package/dist/tools/code-function-explain-apply.d.ts +255 -0
  75. package/dist/tools/code-function-explain-apply.js +225 -0
  76. package/dist/tools/code-function-explain-prepare.d.ts +535 -0
  77. package/dist/tools/code-function-explain-prepare.js +276 -0
  78. package/dist/tools/code-function-explain-review.d.ts +397 -0
  79. package/dist/tools/code-function-explain-review.js +589 -0
  80. package/dist/tools/code-function-rename-apply.d.ts +248 -0
  81. package/dist/tools/code-function-rename-apply.js +220 -0
  82. package/dist/tools/code-function-rename-prepare.d.ts +506 -0
  83. package/dist/tools/code-function-rename-prepare.js +279 -0
  84. package/dist/tools/code-function-rename-review.d.ts +574 -0
  85. package/dist/tools/code-function-rename-review.js +761 -0
  86. package/dist/tools/code-functions-list.d.ts +37 -0
  87. package/dist/tools/code-functions-list.js +91 -0
  88. package/dist/tools/code-functions-rank.d.ts +34 -0
  89. package/dist/tools/code-functions-rank.js +90 -0
  90. package/dist/tools/code-functions-reconstruct.d.ts +2725 -0
  91. package/dist/tools/code-functions-reconstruct.js +2807 -0
  92. package/dist/tools/code-functions-search.d.ts +39 -0
  93. package/dist/tools/code-functions-search.js +90 -0
  94. package/dist/tools/code-reconstruct-export.d.ts +1212 -0
  95. package/dist/tools/code-reconstruct-export.js +4002 -0
  96. package/dist/tools/code-reconstruct-plan.d.ts +274 -0
  97. package/dist/tools/code-reconstruct-plan.js +342 -0
  98. package/dist/tools/dotnet-metadata-extract.d.ts +541 -0
  99. package/dist/tools/dotnet-metadata-extract.js +355 -0
  100. package/dist/tools/dotnet-reconstruct-export.d.ts +567 -0
  101. package/dist/tools/dotnet-reconstruct-export.js +1151 -0
  102. package/dist/tools/dotnet-types-list.d.ts +325 -0
  103. package/dist/tools/dotnet-types-list.js +201 -0
  104. package/dist/tools/dynamic-dependencies.d.ts +115 -0
  105. package/dist/tools/dynamic-dependencies.js +213 -0
  106. package/dist/tools/dynamic-memory-import.d.ts +10 -0
  107. package/dist/tools/dynamic-memory-import.js +567 -0
  108. package/dist/tools/dynamic-trace-import.d.ts +10 -0
  109. package/dist/tools/dynamic-trace-import.js +235 -0
  110. package/dist/tools/entrypoint-fallback-disasm.d.ts +30 -0
  111. package/dist/tools/entrypoint-fallback-disasm.js +89 -0
  112. package/dist/tools/ghidra-analyze.d.ts +88 -0
  113. package/dist/tools/ghidra-analyze.js +208 -0
  114. package/dist/tools/ghidra-health.d.ts +37 -0
  115. package/dist/tools/ghidra-health.js +212 -0
  116. package/dist/tools/ioc-export.d.ts +209 -0
  117. package/dist/tools/ioc-export.js +542 -0
  118. package/dist/tools/packer-detect.d.ts +165 -0
  119. package/dist/tools/packer-detect.js +284 -0
  120. package/dist/tools/pe-exports-extract.d.ts +175 -0
  121. package/dist/tools/pe-exports-extract.js +253 -0
  122. package/dist/tools/pe-fingerprint.d.ts +234 -0
  123. package/dist/tools/pe-fingerprint.js +269 -0
  124. package/dist/tools/pe-imports-extract.d.ts +105 -0
  125. package/dist/tools/pe-imports-extract.js +245 -0
  126. package/dist/tools/report-generate.d.ts +157 -0
  127. package/dist/tools/report-generate.js +457 -0
  128. package/dist/tools/report-summarize.d.ts +2131 -0
  129. package/dist/tools/report-summarize.js +596 -0
  130. package/dist/tools/runtime-detect.d.ts +135 -0
  131. package/dist/tools/runtime-detect.js +247 -0
  132. package/dist/tools/sample-ingest.d.ts +94 -0
  133. package/dist/tools/sample-ingest.js +327 -0
  134. package/dist/tools/sample-profile-get.d.ts +183 -0
  135. package/dist/tools/sample-profile-get.js +121 -0
  136. package/dist/tools/sandbox-execute.d.ts +441 -0
  137. package/dist/tools/sandbox-execute.js +392 -0
  138. package/dist/tools/strings-extract.d.ts +375 -0
  139. package/dist/tools/strings-extract.js +314 -0
  140. package/dist/tools/strings-floss-decode.d.ts +143 -0
  141. package/dist/tools/strings-floss-decode.js +259 -0
  142. package/dist/tools/system-health.d.ts +434 -0
  143. package/dist/tools/system-health.js +446 -0
  144. package/dist/tools/task-cancel.d.ts +21 -0
  145. package/dist/tools/task-cancel.js +70 -0
  146. package/dist/tools/task-status.d.ts +27 -0
  147. package/dist/tools/task-status.js +106 -0
  148. package/dist/tools/task-sweep.d.ts +22 -0
  149. package/dist/tools/task-sweep.js +77 -0
  150. package/dist/tools/tool-help.d.ts +340 -0
  151. package/dist/tools/tool-help.js +261 -0
  152. package/dist/tools/yara-scan.d.ts +554 -0
  153. package/dist/tools/yara-scan.js +313 -0
  154. package/dist/types.d.ts +266 -0
  155. package/dist/types.js +41 -0
  156. package/dist/worker-pool.d.ts +204 -0
  157. package/dist/worker-pool.js +650 -0
  158. package/dist/workflows/deep-static.d.ts +104 -0
  159. package/dist/workflows/deep-static.js +276 -0
  160. package/dist/workflows/function-explanation-review.d.ts +655 -0
  161. package/dist/workflows/function-explanation-review.js +440 -0
  162. package/dist/workflows/reconstruct.d.ts +2053 -0
  163. package/dist/workflows/reconstruct.js +666 -0
  164. package/dist/workflows/semantic-name-review.d.ts +2418 -0
  165. package/dist/workflows/semantic-name-review.js +521 -0
  166. package/dist/workflows/triage.d.ts +659 -0
  167. package/dist/workflows/triage.js +1374 -0
  168. package/dist/workspace-manager.d.ts +150 -0
  169. package/dist/workspace-manager.js +411 -0
  170. package/ghidra_scripts/DecompileFunction.java +487 -0
  171. package/ghidra_scripts/DecompileFunction.py +150 -0
  172. package/ghidra_scripts/ExtractCFG.java +256 -0
  173. package/ghidra_scripts/ExtractCFG.py +233 -0
  174. package/ghidra_scripts/ExtractFunctions.java +442 -0
  175. package/ghidra_scripts/ExtractFunctions.py +101 -0
  176. package/ghidra_scripts/README.md +125 -0
  177. package/ghidra_scripts/SearchFunctionReferences.java +380 -0
  178. package/helpers/DotNetMetadataProbe/DotNetMetadataProbe.csproj +9 -0
  179. package/helpers/DotNetMetadataProbe/Program.cs +566 -0
  180. package/install-to-codex.ps1 +178 -0
  181. package/install-to-copilot.ps1 +303 -0
  182. package/package.json +101 -0
  183. package/requirements.txt +9 -0
  184. package/workers/requirements-dynamic.txt +11 -0
  185. package/workers/requirements.txt +8 -0
  186. package/workers/speakeasy_compat.py +175 -0
  187. package/workers/static_worker.py +5183 -0
  188. package/workers/yara_rules/default.yar +33 -0
  189. package/workers/yara_rules/malware_families.yar +93 -0
  190. package/workers/yara_rules/packers.yar +80 -0
@@ -0,0 +1,235 @@
1
+ /**
2
+ * artifact.read tool implementation
3
+ * Read artifact metadata and optional file content directly via MCP.
4
+ */
5
+ import { z } from 'zod';
6
+ import type { ToolDefinition, ToolArgs, WorkerResult } from '../types.js';
7
+ import type { WorkspaceManager } from '../workspace-manager.js';
8
+ import type { DatabaseManager } from '../database.js';
9
+ export declare const ArtifactReadInputSchema: z.ZodObject<{
10
+ sample_id: z.ZodString;
11
+ artifact_id: z.ZodOptional<z.ZodString>;
12
+ artifact_type: z.ZodOptional<z.ZodString>;
13
+ path: z.ZodOptional<z.ZodString>;
14
+ include_untracked_files: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
15
+ recursive: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
16
+ scan_roots: z.ZodDefault<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>;
17
+ select_latest: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
18
+ include_content: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
19
+ max_bytes: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
20
+ encoding: z.ZodDefault<z.ZodOptional<z.ZodEnum<["auto", "utf8", "base64"]>>>;
21
+ parse_json: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
22
+ ioc_highlights: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
23
+ }, "strip", z.ZodTypeAny, {
24
+ sample_id: string;
25
+ recursive: boolean;
26
+ encoding: "utf8" | "base64" | "auto";
27
+ include_untracked_files: boolean;
28
+ scan_roots: string[];
29
+ select_latest: boolean;
30
+ include_content: boolean;
31
+ max_bytes: number;
32
+ parse_json: boolean;
33
+ ioc_highlights: boolean;
34
+ path?: string | undefined;
35
+ artifact_id?: string | undefined;
36
+ artifact_type?: string | undefined;
37
+ }, {
38
+ sample_id: string;
39
+ path?: string | undefined;
40
+ recursive?: boolean | undefined;
41
+ encoding?: "utf8" | "base64" | "auto" | undefined;
42
+ artifact_id?: string | undefined;
43
+ artifact_type?: string | undefined;
44
+ include_untracked_files?: boolean | undefined;
45
+ scan_roots?: string[] | undefined;
46
+ select_latest?: boolean | undefined;
47
+ include_content?: boolean | undefined;
48
+ max_bytes?: number | undefined;
49
+ parse_json?: boolean | undefined;
50
+ ioc_highlights?: boolean | undefined;
51
+ }>;
52
+ export type ArtifactReadInput = z.infer<typeof ArtifactReadInputSchema>;
53
+ export declare const ArtifactReadOutputSchema: z.ZodObject<{
54
+ ok: z.ZodBoolean;
55
+ data: z.ZodOptional<z.ZodObject<{
56
+ sample_id: z.ZodString;
57
+ tool_version: z.ZodString;
58
+ artifact: z.ZodObject<{
59
+ id: z.ZodString;
60
+ type: z.ZodString;
61
+ path: z.ZodString;
62
+ sha256: z.ZodString;
63
+ mime: z.ZodNullable<z.ZodString>;
64
+ created_at: z.ZodString;
65
+ }, "strip", z.ZodTypeAny, {
66
+ path: string;
67
+ type: string;
68
+ id: string;
69
+ sha256: string;
70
+ mime: string | null;
71
+ created_at: string;
72
+ }, {
73
+ path: string;
74
+ type: string;
75
+ id: string;
76
+ sha256: string;
77
+ mime: string | null;
78
+ created_at: string;
79
+ }>;
80
+ content: z.ZodOptional<z.ZodString>;
81
+ content_encoding: z.ZodOptional<z.ZodEnum<["utf8", "base64"]>>;
82
+ parsed_json: z.ZodOptional<z.ZodAny>;
83
+ highlights: z.ZodOptional<z.ZodObject<{
84
+ urls: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
85
+ ip_addresses: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
86
+ commands: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
87
+ registry_keys: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
88
+ pipes: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
89
+ }, "strip", z.ZodTypeAny, {
90
+ urls?: string[] | undefined;
91
+ ip_addresses?: string[] | undefined;
92
+ registry_keys?: string[] | undefined;
93
+ commands?: string[] | undefined;
94
+ pipes?: string[] | undefined;
95
+ }, {
96
+ urls?: string[] | undefined;
97
+ ip_addresses?: string[] | undefined;
98
+ registry_keys?: string[] | undefined;
99
+ commands?: string[] | undefined;
100
+ pipes?: string[] | undefined;
101
+ }>>;
102
+ bytes_read: z.ZodNumber;
103
+ total_size: z.ZodNumber;
104
+ truncated: z.ZodBoolean;
105
+ }, "strip", z.ZodTypeAny, {
106
+ sample_id: string;
107
+ artifact: {
108
+ path: string;
109
+ type: string;
110
+ id: string;
111
+ sha256: string;
112
+ mime: string | null;
113
+ created_at: string;
114
+ };
115
+ tool_version: string;
116
+ truncated: boolean;
117
+ bytes_read: number;
118
+ total_size: number;
119
+ content?: string | undefined;
120
+ content_encoding?: "utf8" | "base64" | undefined;
121
+ parsed_json?: any;
122
+ highlights?: {
123
+ urls?: string[] | undefined;
124
+ ip_addresses?: string[] | undefined;
125
+ registry_keys?: string[] | undefined;
126
+ commands?: string[] | undefined;
127
+ pipes?: string[] | undefined;
128
+ } | undefined;
129
+ }, {
130
+ sample_id: string;
131
+ artifact: {
132
+ path: string;
133
+ type: string;
134
+ id: string;
135
+ sha256: string;
136
+ mime: string | null;
137
+ created_at: string;
138
+ };
139
+ tool_version: string;
140
+ truncated: boolean;
141
+ bytes_read: number;
142
+ total_size: number;
143
+ content?: string | undefined;
144
+ content_encoding?: "utf8" | "base64" | undefined;
145
+ parsed_json?: any;
146
+ highlights?: {
147
+ urls?: string[] | undefined;
148
+ ip_addresses?: string[] | undefined;
149
+ registry_keys?: string[] | undefined;
150
+ commands?: string[] | undefined;
151
+ pipes?: string[] | undefined;
152
+ } | undefined;
153
+ }>>;
154
+ warnings: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
155
+ errors: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
156
+ metrics: z.ZodOptional<z.ZodObject<{
157
+ elapsed_ms: z.ZodNumber;
158
+ tool: z.ZodString;
159
+ }, "strip", z.ZodTypeAny, {
160
+ elapsed_ms: number;
161
+ tool: string;
162
+ }, {
163
+ elapsed_ms: number;
164
+ tool: string;
165
+ }>>;
166
+ }, "strip", z.ZodTypeAny, {
167
+ ok: boolean;
168
+ metrics?: {
169
+ elapsed_ms: number;
170
+ tool: string;
171
+ } | undefined;
172
+ data?: {
173
+ sample_id: string;
174
+ artifact: {
175
+ path: string;
176
+ type: string;
177
+ id: string;
178
+ sha256: string;
179
+ mime: string | null;
180
+ created_at: string;
181
+ };
182
+ tool_version: string;
183
+ truncated: boolean;
184
+ bytes_read: number;
185
+ total_size: number;
186
+ content?: string | undefined;
187
+ content_encoding?: "utf8" | "base64" | undefined;
188
+ parsed_json?: any;
189
+ highlights?: {
190
+ urls?: string[] | undefined;
191
+ ip_addresses?: string[] | undefined;
192
+ registry_keys?: string[] | undefined;
193
+ commands?: string[] | undefined;
194
+ pipes?: string[] | undefined;
195
+ } | undefined;
196
+ } | undefined;
197
+ warnings?: string[] | undefined;
198
+ errors?: string[] | undefined;
199
+ }, {
200
+ ok: boolean;
201
+ metrics?: {
202
+ elapsed_ms: number;
203
+ tool: string;
204
+ } | undefined;
205
+ data?: {
206
+ sample_id: string;
207
+ artifact: {
208
+ path: string;
209
+ type: string;
210
+ id: string;
211
+ sha256: string;
212
+ mime: string | null;
213
+ created_at: string;
214
+ };
215
+ tool_version: string;
216
+ truncated: boolean;
217
+ bytes_read: number;
218
+ total_size: number;
219
+ content?: string | undefined;
220
+ content_encoding?: "utf8" | "base64" | undefined;
221
+ parsed_json?: any;
222
+ highlights?: {
223
+ urls?: string[] | undefined;
224
+ ip_addresses?: string[] | undefined;
225
+ registry_keys?: string[] | undefined;
226
+ commands?: string[] | undefined;
227
+ pipes?: string[] | undefined;
228
+ } | undefined;
229
+ } | undefined;
230
+ warnings?: string[] | undefined;
231
+ errors?: string[] | undefined;
232
+ }>;
233
+ export declare const artifactReadToolDefinition: ToolDefinition;
234
+ export declare function createArtifactReadHandler(workspaceManager: WorkspaceManager, database: DatabaseManager): (args: ToolArgs) => Promise<WorkerResult>;
235
+ //# sourceMappingURL=artifact-read.d.ts.map
@@ -0,0 +1,317 @@
1
+ /**
2
+ * artifact.read tool implementation
3
+ * Read artifact metadata and optional file content directly via MCP.
4
+ */
5
+ import fs from 'fs/promises';
6
+ import path from 'path';
7
+ import { z } from 'zod';
8
+ import { listArtifactInventory, normalizeRelativeArtifactPath, } from '../artifact-inventory.js';
9
+ const TOOL_NAME = 'artifact.read';
10
+ const TOOL_VERSION = '0.1.0';
11
+ const TEXT_EXTENSIONS = new Set([
12
+ '.txt',
13
+ '.md',
14
+ '.json',
15
+ '.log',
16
+ '.yaml',
17
+ '.yml',
18
+ '.xml',
19
+ '.ini',
20
+ '.cfg',
21
+ '.c',
22
+ '.h',
23
+ '.cpp',
24
+ '.hpp',
25
+ '.cs',
26
+ '.py',
27
+ '.js',
28
+ '.ts',
29
+ ]);
30
+ export const ArtifactReadInputSchema = z.object({
31
+ sample_id: z.string().describe('Sample ID (format: sha256:<hex>)'),
32
+ artifact_id: z.string().optional().describe('Specific artifact UUID to fetch'),
33
+ artifact_type: z.string().optional().describe('Artifact type to fetch latest match'),
34
+ path: z.string().optional().describe('Artifact relative path to fetch'),
35
+ include_untracked_files: z
36
+ .boolean()
37
+ .optional()
38
+ .default(true)
39
+ .describe('Allow synthetic inventory entries for untracked files under scan roots'),
40
+ recursive: z
41
+ .boolean()
42
+ .optional()
43
+ .default(true)
44
+ .describe('Recursively scan export roots when include_untracked_files=true'),
45
+ scan_roots: z
46
+ .array(z.string())
47
+ .optional()
48
+ .default(['reports', 'ghidra', 'dotnet'])
49
+ .describe('Workspace subdirectories to scan for untracked artifact files'),
50
+ select_latest: z
51
+ .boolean()
52
+ .optional()
53
+ .default(true)
54
+ .describe('When selector matches multiple artifacts, choose latest (true) or oldest (false)'),
55
+ include_content: z
56
+ .boolean()
57
+ .optional()
58
+ .default(true)
59
+ .describe('Return file content in response payload'),
60
+ max_bytes: z
61
+ .number()
62
+ .int()
63
+ .min(256)
64
+ .max(2 * 1024 * 1024)
65
+ .optional()
66
+ .default(256 * 1024)
67
+ .describe('Maximum bytes to read from artifact file'),
68
+ encoding: z
69
+ .enum(['auto', 'utf8', 'base64'])
70
+ .optional()
71
+ .default('auto')
72
+ .describe('Content encoding mode when include_content=true'),
73
+ parse_json: z
74
+ .boolean()
75
+ .optional()
76
+ .default(false)
77
+ .describe('Parse JSON artifacts into structured object when content is UTF-8'),
78
+ ioc_highlights: z
79
+ .boolean()
80
+ .optional()
81
+ .default(true)
82
+ .describe('Extract IOC highlights from UTF-8 text content'),
83
+ });
84
+ export const ArtifactReadOutputSchema = z.object({
85
+ ok: z.boolean(),
86
+ data: z
87
+ .object({
88
+ sample_id: z.string(),
89
+ tool_version: z.string(),
90
+ artifact: z.object({
91
+ id: z.string(),
92
+ type: z.string(),
93
+ path: z.string(),
94
+ sha256: z.string(),
95
+ mime: z.string().nullable(),
96
+ created_at: z.string(),
97
+ }),
98
+ content: z.string().optional(),
99
+ content_encoding: z.enum(['utf8', 'base64']).optional(),
100
+ parsed_json: z.any().optional(),
101
+ highlights: z
102
+ .object({
103
+ urls: z.array(z.string()).optional(),
104
+ ip_addresses: z.array(z.string()).optional(),
105
+ commands: z.array(z.string()).optional(),
106
+ registry_keys: z.array(z.string()).optional(),
107
+ pipes: z.array(z.string()).optional(),
108
+ })
109
+ .optional(),
110
+ bytes_read: z.number(),
111
+ total_size: z.number(),
112
+ truncated: z.boolean(),
113
+ })
114
+ .optional(),
115
+ warnings: z.array(z.string()).optional(),
116
+ errors: z.array(z.string()).optional(),
117
+ metrics: z
118
+ .object({
119
+ elapsed_ms: z.number(),
120
+ tool: z.string(),
121
+ })
122
+ .optional(),
123
+ });
124
+ function looksLikeText(buffer) {
125
+ if (buffer.length === 0) {
126
+ return true;
127
+ }
128
+ let suspicious = 0;
129
+ for (const byte of buffer) {
130
+ if (byte === 0) {
131
+ suspicious += 1;
132
+ continue;
133
+ }
134
+ if (byte < 0x09 || (byte > 0x0d && byte < 0x20)) {
135
+ suspicious += 1;
136
+ }
137
+ }
138
+ return suspicious / buffer.length < 0.08;
139
+ }
140
+ function isTextArtifact(artifact, sample) {
141
+ const extension = path.extname(artifact.path).toLowerCase();
142
+ if (TEXT_EXTENSIONS.has(extension)) {
143
+ return true;
144
+ }
145
+ if (artifact.mime && artifact.mime.startsWith('text/')) {
146
+ return true;
147
+ }
148
+ if (artifact.mime === 'application/json') {
149
+ return true;
150
+ }
151
+ return looksLikeText(sample);
152
+ }
153
+ function selectArtifact(input, artifacts) {
154
+ const ordered = input.select_latest ? artifacts : [...artifacts].reverse();
155
+ if (input.artifact_id) {
156
+ return ordered.find((item) => item.id === input.artifact_id) || null;
157
+ }
158
+ if (input.path) {
159
+ const normalizedPath = normalizeRelativeArtifactPath(input.path);
160
+ return (ordered.find((item) => normalizeRelativeArtifactPath(item.path) === normalizedPath) || null);
161
+ }
162
+ if (input.artifact_type) {
163
+ return ordered.find((item) => item.type === input.artifact_type) || null;
164
+ }
165
+ return ordered[0] || null;
166
+ }
167
+ function dedupe(values) {
168
+ return Array.from(new Set(values));
169
+ }
170
+ function extractIOCTextHighlights(content) {
171
+ const urls = dedupe(content.match(/https?:\/\/[^\s"'<>]+/gi) || []).slice(0, 30);
172
+ const ips = dedupe(content.match(/\b(?:\d{1,3}\.){3}\d{1,3}\b/g) || []).slice(0, 30);
173
+ const registry = dedupe(content.match(/HKEY_[A-Z_]+\\[^\s]+/gi) || []).slice(0, 30);
174
+ const pipes = dedupe(content.match(/\\\\\.\\pipe\\[^\s]+|\\\\pipe\\[^\s]+/gi) || []).slice(0, 30);
175
+ const commandMatches = content.match(/(?:^|\s)(?:cmd\.exe|powershell\.exe|wscript\.exe|cscript\.exe|mshta\.exe)[^\r\n]*/gim) ||
176
+ [];
177
+ const commands = dedupe(commandMatches.map((item) => item.trim())).slice(0, 30);
178
+ return {
179
+ urls: urls.length > 0 ? urls : undefined,
180
+ ip_addresses: ips.length > 0 ? ips : undefined,
181
+ commands: commands.length > 0 ? commands : undefined,
182
+ registry_keys: registry.length > 0 ? registry : undefined,
183
+ pipes: pipes.length > 0 ? pipes : undefined,
184
+ };
185
+ }
186
+ export const artifactReadToolDefinition = {
187
+ name: TOOL_NAME,
188
+ description: 'Read artifact metadata/content by sample_id and artifact selector (artifact_id, artifact_type, or path).',
189
+ inputSchema: ArtifactReadInputSchema,
190
+ outputSchema: ArtifactReadOutputSchema,
191
+ };
192
+ export function createArtifactReadHandler(workspaceManager, database) {
193
+ return async (args) => {
194
+ const startTime = Date.now();
195
+ try {
196
+ const input = ArtifactReadInputSchema.parse(args);
197
+ const sample = database.findSample(input.sample_id);
198
+ if (!sample) {
199
+ return {
200
+ ok: false,
201
+ errors: [`Sample not found: ${input.sample_id}`],
202
+ metrics: {
203
+ elapsed_ms: Date.now() - startTime,
204
+ tool: TOOL_NAME,
205
+ },
206
+ };
207
+ }
208
+ const artifacts = await listArtifactInventory(workspaceManager, database, input.sample_id, {
209
+ includeMissing: true,
210
+ includeUntrackedFiles: input.include_untracked_files,
211
+ recursive: input.recursive,
212
+ scanRoots: input.scan_roots,
213
+ });
214
+ if (artifacts.length === 0) {
215
+ return {
216
+ ok: false,
217
+ errors: [`No artifacts found for sample: ${input.sample_id}`],
218
+ metrics: {
219
+ elapsed_ms: Date.now() - startTime,
220
+ tool: TOOL_NAME,
221
+ },
222
+ };
223
+ }
224
+ const selected = selectArtifact(input, artifacts);
225
+ if (!selected) {
226
+ return {
227
+ ok: false,
228
+ errors: [
229
+ `Artifact not found for selectors: artifact_id=${input.artifact_id || 'n/a'}, artifact_type=${input.artifact_type || 'n/a'}, path=${input.path || 'n/a'}. Try artifacts.list first to enumerate available records.`,
230
+ ],
231
+ metrics: {
232
+ elapsed_ms: Date.now() - startTime,
233
+ tool: TOOL_NAME,
234
+ },
235
+ };
236
+ }
237
+ const workspace = await workspaceManager.getWorkspace(input.sample_id);
238
+ const artifactAbsPath = workspaceManager.normalizePath(workspace.root, selected.path);
239
+ const stat = await fs.stat(artifactAbsPath);
240
+ const responseData = {
241
+ sample_id: input.sample_id,
242
+ tool_version: TOOL_VERSION,
243
+ artifact: {
244
+ id: selected.id,
245
+ sample_id: input.sample_id,
246
+ type: selected.type,
247
+ path: selected.path,
248
+ sha256: selected.sha256,
249
+ mime: selected.mime,
250
+ created_at: selected.created_at,
251
+ },
252
+ bytes_read: 0,
253
+ total_size: stat.size,
254
+ truncated: false,
255
+ };
256
+ const warnings = [];
257
+ const selectorProvided = Boolean(input.artifact_id || input.artifact_type || input.path);
258
+ if (!selectorProvided && artifacts.length > 1) {
259
+ warnings.push(`No selector provided; resolved to ${selected.id} (${selected.type}). Use artifact_id/artifact_type/path for deterministic selection.`);
260
+ }
261
+ if ('tracked' in selected && selected.tracked === false) {
262
+ warnings.push('Resolved to untracked filesystem artifact; consider registering export artifacts for stable ids.');
263
+ }
264
+ if (input.include_content) {
265
+ const fileBuffer = await fs.readFile(artifactAbsPath);
266
+ const truncated = fileBuffer.length > input.max_bytes;
267
+ const outputBuffer = truncated ? fileBuffer.subarray(0, input.max_bytes) : fileBuffer;
268
+ responseData.bytes_read = outputBuffer.length;
269
+ responseData.truncated = truncated;
270
+ if (truncated) {
271
+ warnings.push(`Artifact content truncated to ${input.max_bytes} bytes (total ${fileBuffer.length} bytes).`);
272
+ }
273
+ let encoding = 'utf8';
274
+ if (input.encoding === 'base64') {
275
+ encoding = 'base64';
276
+ }
277
+ else if (input.encoding === 'auto') {
278
+ encoding = isTextArtifact(selected, outputBuffer) ? 'utf8' : 'base64';
279
+ }
280
+ responseData.content_encoding = encoding;
281
+ responseData.content =
282
+ encoding === 'utf8' ? outputBuffer.toString('utf-8') : outputBuffer.toString('base64');
283
+ if (encoding === 'utf8' && input.parse_json) {
284
+ try {
285
+ responseData.parsed_json = JSON.parse(responseData.content);
286
+ }
287
+ catch (error) {
288
+ warnings.push(`parse_json enabled but JSON parsing failed: ${error.message}`);
289
+ }
290
+ }
291
+ if (encoding === 'utf8' && input.ioc_highlights) {
292
+ responseData.highlights = extractIOCTextHighlights(responseData.content);
293
+ }
294
+ }
295
+ return {
296
+ ok: true,
297
+ data: responseData,
298
+ warnings: warnings.length > 0 ? warnings : undefined,
299
+ metrics: {
300
+ elapsed_ms: Date.now() - startTime,
301
+ tool: TOOL_NAME,
302
+ },
303
+ };
304
+ }
305
+ catch (error) {
306
+ return {
307
+ ok: false,
308
+ errors: [error.message],
309
+ metrics: {
310
+ elapsed_ms: Date.now() - startTime,
311
+ tool: TOOL_NAME,
312
+ },
313
+ };
314
+ }
315
+ };
316
+ }
317
+ //# sourceMappingURL=artifact-read.js.map