first testcase

This commit is contained in:
Grey_D 2023-02-28 00:36:43 +08:00
parent c09f6bf016
commit 01930a9fdb
6 changed files with 249 additions and 2 deletions

160
.gitignore vendored Normal file
View File

@ -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/

View File

@ -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.

View File

@ -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
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()

12
llm_handle/README.md Normal file
View File

@ -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

0
llm_handle/__init__.py Normal file
View File

37
llm_handle/parser.py Normal file
View File

@ -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 ```<command>```, 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