From 01930a9fdbf46b77c6789f1cb81e6ef4d0ada74f Mon Sep 17 00:00:00 2001 From: Grey_D Date: Tue, 28 Feb 2023 00:36:43 +0800 Subject: [PATCH] first testcase --- .gitignore | 160 +++++++++++++++++++++++++++++++++++++++++ README.md | 8 +++ example_chatgpt_api.py | 34 ++++++++- llm_handle/README.md | 12 ++++ llm_handle/__init__.py | 0 llm_handle/parser.py | 37 ++++++++++ 6 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 llm_handle/README.md create mode 100644 llm_handle/__init__.py create mode 100644 llm_handle/parser.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6769e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 3071ed9..571c899 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ # GPT-Terminal-Bridge + +## General +The goal is to build a user-friendly bridge to automate ChatGPT and other GPT modules to automatically control terminal-based applications. This allows an automation of many human-intensive works, such as penetration testing with tools. + +## Installation +1. Install `requirements.txt` with `pip install -r requirements.txt` +2. Install `chatgpt-wrapper`: `pip install git+https://github.com/mmabrouk/chatgpt-wrapper`. More details at: https://github.com/mmabrouk/chatgpt-wrapper. +3. \ No newline at end of file diff --git a/example_chatgpt_api.py b/example_chatgpt_api.py index cb30dd8..6f97410 100644 --- a/example_chatgpt_api.py +++ b/example_chatgpt_api.py @@ -1,5 +1,35 @@ from chatgpt_wrapper import ChatGPT +from llm_handle.parser import extract_cmd +import os bot = ChatGPT() -response = bot.ask("Hello, world!") -print(response) # prints the response from chatGPT \ No newline at end of file +response = bot.ask("Can you give me a sample command in Mac terminal for checking the user names? Please give me the code directly.") +sample_response = """ +Certainly! To list all user names on a Mac using the terminal, you can use the `dscl` command with the `list` option for the `/Users` node. Here's the command: +``` +dscl . list /Users | grep -v '^_' +``` + +This will output a list of all user accounts on the system, excluding any system accounts that start with an underscore. +""" +# print("The response is:", response) +command = extract_cmd(str(response)) +print("The command is:", command) + +# execute the command in the mac terminal. +# Alert!! The execution should be really careful, because it can be dangerous. +# It is recommended to use this in a sandbox environment. +output = os.popen(command).read() +print("The output is:\n", output) +# Ideally, the output should be: +""" +daemon +uname +nobody +root""" + +# delete the session in the end +bot.delete_conversation() + + + diff --git a/llm_handle/README.md b/llm_handle/README.md new file mode 100644 index 0000000..40c58e3 --- /dev/null +++ b/llm_handle/README.md @@ -0,0 +1,12 @@ +# LLM-Handle + + +## General +The LLM-Handle is the tool to automate the process of communicating with LLM models such as ChatGPT. It mainly performs the following tasks: + - Initialize the task with some pre-set prompts + - Extract the useful information from the model's response + - Pass the outside information to the model as accurate command. + + +## Example usage +TODO \ No newline at end of file diff --git a/llm_handle/__init__.py b/llm_handle/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/llm_handle/parser.py b/llm_handle/parser.py new file mode 100644 index 0000000..4e17844 --- /dev/null +++ b/llm_handle/parser.py @@ -0,0 +1,37 @@ +import re +def extract_cmd(response:str) -> str: + """ + Process the response from chatgpt_wrapper, and extract the command for the bot. + + Parameters + ---------- + response: str + The response from chatgpt_wrapper. + + Returns + ---------- + command: str + The command for the bot. + """ + # The response from chatgpt_wrapper is a string. + # The command is wrapped in ``````, and our goal is to extract the command. + try: + code_count = response.count("```") + if code_count == 0: + return False + elif code_count % 2 == 1: + raise ValueError("The number of ``` is not even.") + # Extract the command from the response. + result_list = re.findall(r"```(.+?)```", response, re.DOTALL) + if len(result_list) > 1: + raise ValueError("More than one command is found.") + except AttributeError: # Nonetype, nothing found + return False + + result = result_list[0] + if result[0] == "\n": # If the command starts with a newline, remove it. + result = result[1:] + + return result + +