Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update all the params #251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pentestgpt/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '"0.14.0"'
__version__ = '""'
56 changes: 32 additions & 24 deletions pentestgpt/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from dataclasses import dataclass
import argparse
import sys
from dataclasses import dataclass
from typing import Optional
import argparse

from loguru import logger
from pentestgpt.test_connection import main as test_connection

from pentestgpt.test_connection import test_connection
from pentestgpt.utils.pentest_gpt import pentestGPT


@dataclass
class PentestConfig:
log_dir: str
Expand All @@ -14,62 +17,63 @@ class PentestConfig:
use_logging: bool
use_api: bool


class PentestGPTCLI:

DEFAULT_CONFIG = {
"log_dir": "logs",
"reasoning_model": "gpt-4-o",
"parsing_model": "gpt-4-o",
"reasoning_model": "gpt-4o",
"parsing_model": "gpt-4o",
}

VALID_MODELS = {
"reasoning": ["gpt-4", "gpt-4-turbo"],
"parsing": ["gpt-4-turbo", "gpt-3.5-turbo-16k"]
"reasoning": ["gpt-4", "gpt-4-turbo", "gpt-4o", "gpt-3.5-turbo-16k"],
"parsing": ["gpt-4-turbo", "gpt-3.5-turbo-16k", "gpt-4o"],
}

def __init__(self):
self.parser = self._create_parser()

def _create_parser(self) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="PentestGPT")

parser.add_argument(
"--log_dir",
type=str,
default=self.DEFAULT_CONFIG["log_dir"],
help="Path to the log directory for storing conversations"
help="Path to the log directory for storing conversations",
)

parser.add_argument(
"--reasoning_model",
type=str,
default=self.DEFAULT_CONFIG["reasoning_model"],
choices=self.VALID_MODELS["reasoning"],
help="Model for higher-level cognitive tasks"
help="Model for higher-level cognitive tasks",
)

parser.add_argument(
"--parsing_model",
type=str,
default=self.DEFAULT_CONFIG["parsing_model"],
choices=self.VALID_MODELS["parsing"],
help="Model for structural and grammatical language processing"
help="Model for structural and grammatical language processing",
)

parser.add_argument(
"--logging",
action="store_true",
default=False,
help="Enable data collection through langfuse logging"
help="Enable data collection through langfuse logging",
)

parser.add_argument(
"--useAPI",
action="store_true",
default=True,
help="Deprecated: Set to False only for testing with cookie"
help="Deprecated: Set to False only for testing with cookie",
)

return parser

def parse_args(self) -> PentestConfig:
Expand All @@ -79,39 +83,43 @@ def parse_args(self) -> PentestConfig:
reasoning_model=args.reasoning_model,
parsing_model=args.parsing_model,
use_logging=args.logging,
use_api=args.useAPI
use_api=args.useAPI,
)


def check_connection() -> bool:
try:
return test_connection()
except Exception as e:
logger.error(f"Connection test failed: {e}")
return False


def run_pentest(config: PentestConfig) -> None:
try:
pentest_handler = pentestGPT(
reasoning_model=config.reasoning_model,
parsing_model=config.parsing_model,
useAPI=config.use_api,
log_dir=config.log_dir,
use_langfuse_logging=config.use_logging
use_langfuse_logging=config.use_logging,
)
pentest_handler.main()
except Exception as e:
logger.error(f"PentestGPT execution failed: {e}")
sys.exit(1)


def main():
cli = PentestGPTCLI()
config = cli.parse_args()

if not check_connection():
logger.error("Connection test failed. Exiting...")
sys.exit(1)

run_pentest(config)


if __name__ == "__main__":
main()
114 changes: 51 additions & 63 deletions pentestgpt/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
from pathlib import Path

import loguru
import openai
Expand All @@ -8,7 +7,6 @@
from pentestgpt._version import __version__
from pentestgpt.config.chat_config import ChatGPTConfig
from pentestgpt.utils.APIs.chatgpt_api import ChatGPTAPI
from pentestgpt.utils.chatgpt import ChatGPT

logger = loguru.logger

Expand All @@ -18,78 +16,68 @@ def get_project_version():
return __version__


def test_connection(log_dir="logs", base_url=ChatGPTConfig().api_base):
"""
Test connection function, can be called directly or from pentestgpt.

Args:
log_dir (str): Directory for logging connection tests.
base_url (str): Base URL for the OpenAI API.
"""
# Set up logging
logger.add(f"{log_dir}/chatgpt_connection_test.log", level="ERROR")
chatgpt_config = ChatGPTConfig(api_base=base_url)
console = Console()

# Print version
version = get_project_version()
console.print(
f"Testing the connection for PentestGPT v{version}", style="bold green"
)

# Test connection with GPT-3.5 and GPT-4o
models_to_test = ["gpt-3.5-turbo-16k", "gpt-4o"]
for model in models_to_test:
print(f"#### Testing connection for OpenAI API with model {model}")
try:
chatgpt_config.model = model
chatgpt = ChatGPTAPI(chatgpt_config)
openai.api_key = chatgpt_config.openai_key
chatgpt.send_new_message("Hi, how are you?")
console.print(
f"Connection successful with model {model}. Use <pentestgpt --reasoning_model={model}> to start.",
style="bold green",
)
except Exception as e:
logger.error(e)
console.print(
f"Failed to connect with model {model}. Check API key and configuration.",
style="bold red",
)
print("Error:", e)
return False
return True


def main():
parser = argparse.ArgumentParser(description="PentestGPTTestConnection")
# Parse arguments only if running this script directly
parser = argparse.ArgumentParser(description="PentestGPT Test Connection")
parser.add_argument(
"--logDir",
"--log_dir",
type=str,
default="logs",
help="Log file directory for PentestGPTTestConnection",
help="Directory for logging connection tests",
)

parser.add_argument(
"--baseUrl",
"--base_url",
type=str,
default=ChatGPTConfig().api_base,
help="Base URL for OpenAI API,default: https://api.openai.com/v1",
help="Base URL for OpenAI API, default: https://api.openai.com/v1",
)

args = parser.parse_args()
logger.add(args.logDir + "/chatgpt_connection_test.log", level="ERROR")

chatgpt_config = ChatGPTConfig(api_base=args.baseUrl)
console = Console()

# print version
version = get_project_version()
console.print(
f"You're testing the connection for PentestGPT v{version}", style="bold green"
)

# successful connection bool
can_connect = False

# 1. test the connection for chatgpt api with GPT-3.5
print("#### Test connection for OpenAI api (GPT-3.5)")
try:
chatgpt_config.model = "gpt-3.5-turbo-16k"
chatgpt = ChatGPTAPI(chatgpt_config)
openai.api_key = chatgpt_config.openai_key
result, conversation_id = chatgpt.send_new_message("Hi how are you?")
console.print(
"1. You're connected with OpenAI API. You have GPT-3.5 access. To start PentestGPT, please use <pentestgpt --reasoning_model=gpt-3.5-turbo-16k>",
style="bold green",
)
can_connect = True
except Exception as e: # use a general exception first. Update later for debug
logger.error(e)
console.print(
"1. The OpenAI API key is not properly configured. The likely reason is that you do not link a payment method to OpenAI so your key is not active. \nPlease follow README to update OpenAI API key through `export OPENAI_API_KEY=<>`",
style="bold red",
)
print("The error is below:", e)

# 2. test the connection for chatgpt api with GPT-4
print("#### Test connection for OpenAI api (GPT-4)")
try:
chatgpt_config.model = "gpt-4o"
chatgpt = ChatGPTAPI(chatgpt_config)
openai.api_key = chatgpt_config.openai_key
result, conversation_id = chatgpt.send_new_message("Hi how are you?")
console.print(
"1. You're connected with OpenAI API. You have GPT-4(o) access. To start PentestGPT, please use <pentestgpt --reasoning_model=gpt-4o>",
style="bold green",
)
can_connect = True
except Exception as e: # use a general exception first. Update later for debug
logger.error(e)
console.print(
"2. The OpenAI API key is not properly configured. Please check the error below:",
style="bold red",
)
print("The error is below:", e)

return can_connect
# Call test_connection with arguments from CLI
test_connection(log_dir=args.log_dir, base_url=args.base_url)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion pentestgpt/utils/APIs/module_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"module_name": "chatgpt_api",
"class_name": "ChatGPTAPI",
},
"gpt-4-o": {
"gpt-4o": {
"config_name": "GPT4O",
"module_name": "chatgpt_api",
"class_name": "ChatGPTAPI",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pentestgpt"
version = "0.14.0"
version = "0.14.1"
description = "PentestGPT is an LLM-powered penetration testing tool."
authors = ["Gelei Deng <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="pentestgpt",
version="0.14.0",
version="",
description="PentestGPT, a GPT-empowered penetration testing tool",
long_description="""
PentestGPT is a penetration testing tool empowered by ChatGPT.
Expand Down