Legacy CLI and Interactive Pentest Orchestration
Overview
The legacy PentestGPT entry point is pentestgpt_legacy/main.py. It builds the pentestgpt-legacy CLI around three modes: interactive operation, --list-models, and --smoke-test. --version reads pentestgpt_legacy/_version.py, while pentestgpt_legacy/__init__.py exposes the same package version. The supported-model listing is rendered from pentestgpt_legacy.llm.registry.models_by_provider() and marks providers configured according to pentestgpt_legacy.config.configured_providers().
Configuration for the CLI is split intentionally. pentestgpt_legacy/config.py is the app-level bridge used by the CLI: it imports provider settings from pentestgpt_legacy/…/config.py and selects default reasoning and parsing models by scanning DEFAULT_REASONING_PREFERENCE and DEFAULT_PARSING_PREFERENCE from pentestgpt_legacy/…/registry.py for the first model whose provider is configured. The underlying settings object in pentestgpt_legacy/…/config.py reads .env and environment variables for provider keys such as OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY/GOOGLE_API_KEY, DEEPSEEK_API_KEY, GROK_API_KEY/XAI_API_KEY, QWEN_API_KEY/DASHSCOPE_API_KEY, and KIMI_API_KEY/MOONSHOT_API_KEY. main.py also supports --base-url, which writes a provider-specific *_BASE_URL environment override for the selected reasoning, parsing, and generation models before constructing any LLM clients.
Interactive mode in pentestgpt_legacy/main.py calls run_interactive(), resolves the reasoning and parsing models from CLI flags or defaults, lazily imports pentestgpt_legacy/…/pentest_gpt.py, constructs pentestGPT, and calls session.main(). The orchestrator in utils/pentest_gpt.py is the classic human-in-the-loop flow: three independent LLMClient sessions cooperate as reasoning, generation, and parsing agents. pentestGPT.__init__() creates log output under the CLI --log-dir, constructs clients via pentestgpt_legacy.llm.factory.get_client(), defaults the generation model to the reasoning model, computes a parsing chunk window from the reasoning model context window, and initializes in-memory transcript state.
The three LLM sessions are initialized in pentestGPT.initialize() using prompt strings from pentestgpt_legacy/…/prompt_class.py: generation_session_init, reasoning_session_init, and input_parsing_init. LLMClient in pentestgpt_legacy/…/client.py gives the legacy core the expected synchronous send_new_message() and send_message() API while preserving per-conversation history internally and bridging to async provider calls. After session initialization, _feed_init_prompts() asks the user for the one-line test description, sends it to the reasoning agent with PentestGPTPrompt.task_description to construct the initial Penetration Testing Tree (PTT), then sends that reasoning output to the generation agent with todo_to_command to expand the initial task guidance.
The main REPL is pentestGPT.input_handler() in pentestgpt_legacy/…/pentest_gpt.py. User command completion and validation come from pentestgpt_legacy/…/task_handler.py: main_task_entry() accepts only next, more, todo, discuss, google, help, or quit, and local_task_entry() accepts only discuss, brainstorm, help, google, or continue. Multiline free-text input and radio selection are handled by pentestgpt_legacy/…/prompt_select.py, where Shift+Right accepts multiline prompts.
The main commands drive different parts of the orchestration flow:
nextasks the user to classify pasted input astool,user-comments,web, ordefault, summarizes it throughinput_parsing_handler(), then sends the parsed summary toreasoning_handler(). The reasoning handler updates the PTT withprocess_resultsand immediately asks the reasoning session to select the next favorable task withprocess_results_task_selection.morerequires a previous reasoning response. It sends that response to the generation session, then sendslocal_task_initto switch into a local sub-task loop wherelocal_input_handler()can discuss or brainstorm details with the generation agent untilcontinueis entered.todoasks the reasoning agent to revise/select tasks usingask_todo, then sends the resulting reasoning response to the generation agent withtodo_to_commandfor concrete instructions.discusscollects user text and sends it throughreasoning_handler()with thediscussionprompt prefix so the PTT can be updated as part of the response.googleis a placeholder in both main and local loops; utils/pentest_gpt.py logs and prints a fixed “still under development” message rather than calling an external search service.quitreturnsFalseto break the main loop.
Input summarization is centralized in pentestGPT.input_parsing_handler(). It prepends source-specific instructions from postfix_options, normalizes pasted content onto one line, wraps it at 8,000 characters, and sends each chunk to the parsing LLM session. The summarized output is logged in the input_parsing transcript and is what the reasoning agent sees for long tool/web/user input. reasoning_handler() also re-routes oversized text through input_parsing_handler() when it exceeds parsing_char_window, so callers that bypass next still get summarization before updating the PTT.
State is mostly in memory during the run. pentestGPT.history records timestamped user, PentestGPT, reasoning, parsing, generation, and exception entries. On exit from pentestGPT.main(), the history is written as JSON to <log_dir>/pentestGPT_log_<timestamp>.txt; then save_session() asks for a session name and writes {"task_log": ..., "history": ...} under test_history/ in the current working directory. There is no server-side conversation resume: utils/pentest_gpt.py explicitly relies on the LLMClient objects to hold conversation state for the current process.
Connectivity tooling is also rooted in this area. pentestgpt_legacy/smoke_test.py powers pentestgpt-legacy --smoke-test by selecting registry models, building a client with get_client(), making one real provider call asking for OK, and returning a non-zero exit code if any attempted model fails. Models whose providers lack credentials are skipped, not failed. pentestgpt_legacy/test_connection.py is a lighter wrapper that probes the first non-legacy model for each configured provider via the same _probe() function.
Coupling
- If command names in pentestgpt_legacy/…/task_handler.py change, update every matching branch in pentestgpt_legacy/…/pentest_gpt.py. The prompt loops validate against the completer task lists, but the orchestrator dispatch is string-based and silently falls to “Please key in the correct options” for names it does not recognize.
- If prompt fields in pentestgpt_legacy/…/prompt_class.py are renamed or their expected output format changes, verify pentestgpt_legacy/…/pentest_gpt.py call sites. The reasoning/generation handoff assumes
reasoning_session_init,process_results,process_results_task_selection,todo_to_command, and local-task prompts preserve the PTT-oriented contract. - If model IDs, provider keys, or default preference lists in pentestgpt_legacy/…/registry.py change, verify pentestgpt_legacy/config.py and pentestgpt_legacy/main.py. Default model selection depends on registry IDs resolving to providers returned by
configured_providers(), and--base-urlconstructs environment variable names from the registry provider key. - If pentestgpt_legacy/…/client.py changes its synchronous
send_new_message()/send_message()signatures or conversation-id behavior, update pentestgpt_legacy/…/pentest_gpt.py. The orchestrator stores three session IDs and passes them back on every follow-up call. - If provider credential handling changes in pentestgpt_legacy/…/config.py or pentestgpt_legacy/…/factory.py, verify both pentestgpt_legacy/main.py and pentestgpt_legacy/smoke_test.py. Interactive startup,
--list-models, full smoke tests, and test_connection.py all use the same configured-provider and client-construction path.
Risks
- pentestgpt_legacy/…/pentest_gpt.py writes session files with
open(..., "w")insave_session()and in the final log write without specifying an encoding or using atomic writes. If the process is interrupted during exit, the JSON transcript inlogs/ortest_history/can be partially written and unusable. - pentestgpt_legacy/…/pentest_gpt.py treats the fixed Google placeholder as a successful command response. If callers or tests later expect real web-search results from the
googlecommand in either the main or local loop, the orchestration will continue with “still under development” text rather than retrieved content.