The world's first agentic reverse engineer.
add basic t"ui" w click until later
Amruth0b725a3bc329
diff --git a/kong/__main__.py b/kong/__main__.pynew file mode 100644index 0000000..294e41f--- /dev/null+++ b/kong/__main__.py@@ -0,0 +1,164 @@+"""Kong CLI — autonomous binary analysis engine."""++from __future__ import annotations++from pathlib import Path++import click+from rich.console import Console+from rich.markup import escape++from kong import __version__+from kong.config import GhidraConfig, KongConfig, OutputConfig++console = Console()++[email protected]_option(version=__version__, prog_name="kong")[email protected]("-v", "--verbose", is_flag=True, help="Enable verbose output.")[email protected]_context+def cli(ctx: click.Context, verbose: bool) -> None:+ """Kong — autonomous binary analysis engine.++ Point it at a stripped binary, get back clean decompiled source,+ annotated Ghidra project, and structured JSON.+ """+ ctx.ensure_object(dict)+ ctx.obj["verbose"] = verbose++[email protected]("binary", type=click.Path(exists=True, dir_okay=False))[email protected]("--headless", is_flag=True, help="Run without TUI (for CI/Docker).")+ "--output", "-o",+ type=click.Path(),+ default="./kong_output",+ help="Output directory.",+)+ "--format", "-f",+ "formats",+ type=click.Choice(["source", "json", "ghidra"], case_sensitive=False),+ multiple=True,+ default=["source", "json", "ghidra"],+ help="Output formats.",+)[email protected]("--ghidra-dir", default=None, help="Ghidra installation directory.")[email protected]_context+def analyze(+ ctx: click.Context,+ binary: str,+ headless: bool,+ output: str,+ formats: tuple[str, ...],+ ghidra_dir: str | None,+) -> None:+ """Analyze a binary with Kong's autonomous agent."""+ config = KongConfig(+ ghidra=GhidraConfig(install_dir=ghidra_dir),+ output=OutputConfig(directory=Path(output), formats=list(formats)),+ headless=headless,+ verbose=ctx.obj["verbose"],+ )++ binary_path = Path(binary).resolve()+ console.print(f"[bold]Kong v{__version__}[/bold]")+ console.print(f"Binary: {binary_path}")+ console.print(f"Output: {config.output.directory}")+ console.print(f"Formats: {', '.join(config.output.formats)}")+ console.print()++ if not config.ghidra.install_dir:+ console.print("[red]Ghidra is not installed or not found.[/red]")+ console.print(+ "\nInstall Ghidra and try again:\n"+ " [bold]brew install ghidra[/bold]\n"+ "\nOr set [bold]GHIDRA_INSTALL_DIR[/bold] to your Ghidra installation path."+ )+ raise SystemExit(1)++ from kong.ghidra.client import GhidraClient, GhidraClientError++ try:+ with console.status(+ "[bold green]Opening binary in Ghidra (this may take 30-60s on first run) ...",+ ):+ client = GhidraClient(+ binary_path=str(binary_path),+ install_dir=config.ghidra.install_dir,+ )+ client.open()+ except GhidraClientError as e:+ console.print(f"[red]Failed to open binary:[/red] {escape(str(e))}")+ raise SystemExit(1)++ # Get binary info+ info = client.get_binary_info()+ console.print(f"[green]Loaded.[/green] {info.arch} {info.format} ({info.compiler})")++ # Enumerate functions+ functions = client.list_functions()+ thunks = sum(1 for f in functions if f.is_thunk)+ console.print(+ f"Functions: {len(functions)} total, {thunks} thunks, "+ f"{len(functions) - thunks} to analyze"+ )++ # TODO: Phase 2-4 — Agent loop (Session 2+)+ console.print("\n[yellow]Agent loop not yet implemented.[/yellow]")+ console.print("[dim]Session 1 complete — Ghidra integration verified.[/dim]")++ client.close()++[email protected]("binary", type=click.Path(exists=True, dir_okay=False))[email protected]("--ghidra-dir", default=None, help="Ghidra installation directory.")+def info(binary: str, ghidra_dir: str | None) -> None:+ """Show info about a binary."""+ from kong.ghidra.client import GhidraClient, GhidraClientError++ ghidra_config = GhidraConfig(install_dir=ghidra_dir)+ if not ghidra_config.install_dir:+ console.print("[red]Ghidra is not installed or not found.[/red]")+ raise SystemExit(1)++ try:+ client = GhidraClient(+ binary_path=str(Path(binary).resolve()),+ install_dir=ghidra_config.install_dir,+ )+ client.open()+ except GhidraClientError as e:+ console.print(f"[red]Failed to open binary:[/red] {e}")+ raise SystemExit(1)++ bi = client.get_binary_info()+ functions = client.list_functions()++ console.print(f"[bold]Binary:[/bold] {bi.name}")+ console.print(f"[bold]Path:[/bold] {bi.path}")+ console.print(f"[bold]Arch:[/bold] {bi.arch}")+ console.print(f"[bold]Format:[/bold] {bi.format}")+ console.print(f"[bold]Endianness:[/bold] {bi.endianness}")+ console.print(f"[bold]Word Size:[/bold] {bi.word_size * 8}-bit")+ console.print(f"[bold]Compiler:[/bold] {bi.compiler}")+ console.print(f"[bold]Functions:[/bold] {len(functions)}")++ # Classification breakdown+ from collections import Counter+ counts = Counter(f.classification.value for f in functions if f.classification)+ for cls, count in sorted(counts.items()):+ console.print(f" {cls}: {count}")++ client.close()+++def main() -> None:+ cli()+++if __name__ == "__main__":+ main()