Files
rea/src/cli.ts
T
Ysopwnableandgensokyo-dev e5004c11ea feat(managed): recover protobuf descriptors from .NET assemblies (#17287)
* feat(managed): describe the protobuf descriptor recovery report and its budget

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* feat(managed): recover protobuf descriptors from generated Reflection classes

Google.Protobuf's generated <File>Reflection classes build each
FileDescriptorProto in a static constructor that concatenates string
literals and passes them to System.Convert.FromBase64String. Read those
literals from the CIL in order, decode each site and keep the results that
parse as FileDescriptorProtos, byte for byte. A FileDescriptorSet lists
them dependencies first, with the well-known protobuf files they import;
other imports are named as missing. Literal characters count against
max_descriptor_bytes before they are joined. Nothing is loaded or run.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* fix(managed): report descriptor recovery refusals as parse failures

A metadata representation budget stops the whole call as a named limit,
including inside one static constructor; other managed reader refusals
become malformed or unsupported input.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* feat(managed): recover protobuf descriptors through an identity-checked provider

The provider reads one bounded managed assembly. A refusal at
max_descriptor_bytes, max_metadata_representation_bytes or
max_snapshot_bytes names the control to raise.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* feat(analysis): summarize retained protobuf descriptor recovery

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* feat(managed): add the recover_protobuf_descriptors tool

CLI recover-protobuf-descriptors and MCP recover_protobuf_descriptors
return each embedded .proto file's package, imports, messages, enums and
services, and a FileDescriptorSet for decoding captured payloads. Large
reports return the retained per-file summary within the agent budget.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* test(managed): cover protobuf descriptor recovery through the service, MCP and CLI

The protoc-style assembly fixture moves to tests/fixtures so the unit,
MCP and CLI tests share it.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* docs(managed): describe protobuf descriptor recovery

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* docs(skill): route Google.Protobuf assemblies to recover_protobuf_descriptors

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* ci: scope protobuf descriptor recovery with the managed formats

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* chore(schemas): record the advertised recover_protobuf_descriptors catalog summary

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* fix(managed): attribute, bound and account for every descriptor search

- Static constructors are found among physical MethodDef rows and named by
  the shared ownership lookup, so a MethodPtr table cannot attribute a
  descriptor to the wrong type.
- A FileDescriptorProto's wire form is walked along its schema first, and
  every nested message it would create is charged to the representation
  budget before protobuf-es decodes it.
- A body whose CIL was not fully decoded, and malformed member lists, make
  coverage partial instead of looking searched.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* test(managed): build descriptor fixtures in protoc's String.Concat shapes

The fixture's static constructor now joins its literals through
String.Concat, as arguments or a string[], before calling
FromBase64String with its real static byte[](string) signature; the
managed PE fixture can name each MemberRef row. Tests cover both forms
and the descriptor tree bound.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* fix(analysis): sample missing imports in the protobuf descriptor summary

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* docs(managed): save the descriptor set from complete delivery and fail on null

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* fix(managed): name the descriptor nesting limit instead of rejecting deep files

The nesting cutoff is now the max_descriptor_depth control (100 by
default, up to 1,000). A deeper descriptor stops the call with a named
limit rather than being listed as not a descriptor, and protobuf-es
decodes up to the same depth so it cannot refuse what the walk admitted.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* docs(managed): list the descriptor depth budget and record its schema

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

---------

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-12-06 08:42:26 +08:00

191 lines
8.4 KiB
TypeScript

import type { FridaInstrumentationPort } from "./ports/frida/FridaInstrumentationPort.js";
import {
CURRENT_ENTRYPOINT_PATH,
isDirectEntrypoint,
} from "./process/CurrentEntrypointPath.js";
import {
CLI_COMMAND_REGISTRATIONS,
CLI_REGISTRATION_ORDER,
type CliCommandModules,
} from "./cli/cliCommandRegistrations.js";
import type { CliResultOutput } from "./cli/cliResultOutput.js";
import type { CliJavaScriptAnalysisHost } from "./cli/javascriptApplicationAnalysis.js";
import { createCliShell } from "./cli/cliShell.js";
import type { CliInstance } from "./cli/types.js";
import { createDirectAnalysis } from "./composition/directAnalysis.js";
import * as setupModule from "./cli/setupCommands.js";
import * as coreAnalysisModule from "./cli/coreAnalysisCommands.js";
import * as reverseEngineeringModule from "./cli/reverseEngineeringCommands.js";
import * as cutterModule from "./cli/cutterCommands.js";
import * as userConfigModule from "./cli/userConfigCommands.js";
import * as utilityModule from "./cli/utilityCommands.js";
import * as binaryDiagnosticsModule from "./cli/binaryDiagnosticsCommands.js";
import * as wasmModule from "./cli/wasmCommands.js";
import * as apkSignatureModule from "./cli/apkSignatureCommands.js";
import * as captureModule from "./cli/captureCommands.js";
import * as signalModule from "./cli/signalCommands.js";
import * as evmModule from "./cli/evmCommands.js";
import * as desktopApplicationModule from "./cli/desktopApplicationCommands.js";
import * as releaseExposureModule from "./cli/releaseExposureCommands.js";
import * as goModule from "./cli/goCommands.js";
import * as il2cppModule from "./cli/il2cppCommands.js";
import * as labviewModule from "./cli/labviewCommands.js";
import * as modelModule from "./cli/modelCommands.js";
import * as gpuModule from "./cli/gpuCommands.js";
import * as rustModule from "./cli/rustCommands.js";
import * as pythonPycModule from "./cli/pythonPycCommands.js";
import * as bamlModule from "./cli/bamlCommands.js";
import * as protobufDescriptorsModule from "./cli/protobufDescriptorCommands.js";
import * as debPackageModule from "./cli/debPackageCommands.js";
import * as nugetNupkgModule from "./cli/nugetNupkgCommands.js";
import * as vsixPackageModule from "./cli/vsixPackageCommands.js";
import * as ociImageLayoutModule from "./cli/ociImageLayoutCommands.js";
import * as yaraModule from "./cli/yaraCommands.js";
import * as erlangModule from "./cli/erlangCommands.js";
import * as v8CodeCacheModule from "./cli/v8CodeCacheCommands.js";
import * as mavenGradleMetadataModule from "./cli/mavenGradleMetadataCommands.js";
import * as sqliteModule from "./cli/sqliteCommands.js";
import * as artifactModule from "./cli/artifactCommands.js";
import * as managedModule from "./cli/managedCommands.js";
import * as firmwareModule from "./cli/firmwareCommands.js";
import * as uefiModule from "./cli/uefiCommands.js";
import * as androidModule from "./cli/androidCommands.js";
import * as pcapModule from "./cli/pcapCommands.js";
import * as flutterModule from "./cli/flutterCommands.js";
import * as fridaModule from "./cli/fridaCommands.js";
import * as apktoolModule from "./cli/apktoolCommands.js";
import * as jebModule from "./cli/jebCommands.js";
import * as adbModule from "./cli/adbCommands.js";
import * as browserModule from "./cli/browserCommands.js";
import * as webRuntimeModule from "./cli/webRuntimeCommands.js";
import * as webNetworkCaptureModule from "./cli/webNetworkCaptureCommands.js";
import * as advancedBrowserModule from "./cli/browserAdvancedCommands.js";
import * as browserScenarioModule from "./cli/browserScenarioCommands.js";
import * as electronModule from "./cli/electronCommands.js";
import * as javaScriptApplicationModule from "./cli/javascriptApplicationAnalysis.js";
import * as javaScriptRuntimeObservationModule from "./cli/javascriptRuntimeCommands.js";
import * as guidanceModule from "./cli/guidanceCommands.js";
import * as guidedWorkflowModule from "./cli/guidedWorkflowCommands.js";
import * as sourceToBundleModule from "./cli/sourceToBundleCommand.js";
import * as staticObservedEndpointsModule from "./cli/staticObservedEndpointsCommand.js";
import * as analysisViewModule from "./cli/analysisViewCommands.js";
import * as evidenceModule from "./cli/evidenceCommands.js";
import * as environmentModule from "./cli/environmentCommands.js";
import * as processModule from "./cli/processCommands.js";
import * as desktopCaptureModule from "./cli/desktopCaptureCommands.js";
import * as javaScriptRecoveryModule from "./cli/javascriptRecoveryCommands.js";
import * as webScriptModule from "./cli/webScriptCommands.js";
import * as webModuleModule from "./cli/webModuleCommands.js";
import * as webSourceLocationModule from "./cli/webSourceLocationCommands.js";
import * as applicationModule from "./cli/applicationCommands.js";
import * as nativeFunctionMatchingModule from "./cli/nativeFunctionMatchingCommands.js";
export { closeCliResources } from "./cli/cliResources.js";
const CLI_COMMAND_MODULES: CliCommandModules = {
setup: setupModule,
coreAnalysis: coreAnalysisModule,
reverseEngineering: reverseEngineeringModule,
cutter: cutterModule,
utility: utilityModule,
userConfig: userConfigModule,
binaryDiagnostics: binaryDiagnosticsModule,
wasm: wasmModule,
apkSignature: apkSignatureModule,
capture: captureModule,
signal: signalModule,
evm: evmModule,
desktopApplication: desktopApplicationModule,
releaseExposure: releaseExposureModule,
go: goModule,
il2cpp: il2cppModule,
labview: labviewModule,
model: modelModule,
gpu: gpuModule,
rust: rustModule,
pythonPyc: pythonPycModule,
baml: bamlModule,
protobufDescriptors: protobufDescriptorsModule,
debPackage: debPackageModule,
nugetNupkg: nugetNupkgModule,
vsixPackage: vsixPackageModule,
ociImageLayout: ociImageLayoutModule,
yara: yaraModule,
erlang: erlangModule,
v8CodeCache: v8CodeCacheModule,
mavenGradleMetadata: mavenGradleMetadataModule,
sqlite: sqliteModule,
artifact: artifactModule,
managed: managedModule,
firmware: firmwareModule,
uefi: uefiModule,
android: androidModule,
pcap: pcapModule,
flutter: flutterModule,
frida: fridaModule,
apktool: apktoolModule,
jeb: jebModule,
adb: adbModule,
browser: browserModule,
webRuntime: webRuntimeModule,
webNetworkCapture: webNetworkCaptureModule,
advancedBrowser: advancedBrowserModule,
browserScenario: browserScenarioModule,
electron: electronModule,
javaScriptApplication: javaScriptApplicationModule,
javaScriptRuntimeReconciliation: electronModule,
javaScriptRuntimeObservation: javaScriptRuntimeObservationModule,
guidance: guidanceModule,
guidedWorkflow: guidedWorkflowModule,
sourceToBundle: sourceToBundleModule,
staticObservedEndpoints: staticObservedEndpointsModule,
analysisView: analysisViewModule,
evidence: evidenceModule,
environment: environmentModule,
process: processModule,
desktopCapture: desktopCaptureModule,
javaScriptRecovery: javaScriptRecoveryModule,
webScript: webScriptModule,
webModule: webModuleModule,
webSourceLocation: webSourceLocationModule,
application: applicationModule,
nativeFunctionMatching: nativeFunctionMatchingModule,
};
/**
* Build the complete one-shot Incur CLI without starting Hopper at import time.
* Analysis commands acquire and close their own sessions; bare `mcp` and
* `--mcp` are intercepted by the executable dispatcher before this module loads.
* One-shot commands load only their own registrars; see `cliLoader.ts`.
*/
export const createCli = (
selectedEnvironment: Readonly<
Record<string, string | undefined>
> = process.env,
resultOutput?: CliResultOutput,
fridaInstrumentation?: FridaInstrumentationPort,
savedDefaults?: ReadonlySet<string>,
javascriptAnalysis?: CliJavaScriptAnalysisHost,
): CliInstance =>
createCliShell({
selectedEnvironment,
resultOutput,
fridaInstrumentation,
savedDefaults,
javascriptAnalysis,
createAnalysis: createDirectAnalysis,
registrations: CLI_REGISTRATION_ORDER.map((id) => ({
registration: CLI_COMMAND_REGISTRATIONS[id],
module: CLI_COMMAND_MODULES[id],
})),
});
// Direct execution needs a visible failure pointing to the dispatcher. Imports
// only expose the registry. A normal exit lets piped stderr drain completely.
if (isDirectEntrypoint(CURRENT_ENTRYPOINT_PATH, import.meta.url)) {
process.stderr.write(
"dist/cli.js is the command registry, not the CLI entry point. Run `rea` (scripts/rea.mjs) instead.\n",
);
process.exitCode = 1;
}