|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import os |
| 4 | +import openai |
| 5 | +import logging |
| 6 | + |
| 7 | +from colorama import Fore, Back, Style |
| 8 | +from datetime import datetime |
| 9 | +from dotenv import load_dotenv |
| 10 | + |
| 11 | +from history import ResultHistory |
| 12 | +from ssh import SSHHostConn |
| 13 | + |
| 14 | +load_dotenv() |
| 15 | + |
| 16 | +openai.api_key = os.getenv('OPENAI_KEY') |
| 17 | +model = os.getenv('MODEL') |
| 18 | + |
| 19 | +conn = SSHHostConn(os.getenv('TARGET_IP'), os.getenv('TARGET_USER'), os.getenv('TARGET_PASSWORD')) |
| 20 | +conn.connect() |
| 21 | + |
| 22 | +initial_user = conn.run("whoami") |
| 23 | + |
| 24 | +def get_openai_response(cmd): |
| 25 | + completion = openai.ChatCompletion.create(model=model, messages=[{"role": "user", "content" : cmd}]) |
| 26 | + result = completion.choices[0].message.content |
| 27 | + return result |
| 28 | + |
| 29 | +cmd_history = ResultHistory() |
| 30 | + |
| 31 | +filename = datetime.now().strftime('run_%H_%M_%d_%m_%Y.log') |
| 32 | +log = logging.getLogger() |
| 33 | +handler = logging.FileHandler(filename) |
| 34 | +log.addHandler(handler) |
| 35 | + |
| 36 | +while True: |
| 37 | + |
| 38 | + results = "" |
| 39 | + |
| 40 | + for old in cmd_history.dump(): |
| 41 | + results = results + "- When you executed the command:`" + old["cmd"] + "` the console output was: `" + old["result"] + "\n" |
| 42 | + |
| 43 | + if results != "": |
| 44 | + results = '''Results for already executed commands were: |
| 45 | +
|
| 46 | + {results}'''.format(results=results) |
| 47 | + |
| 48 | + cmd = ''' You are a low-privilege user {user} on a linux server and your goal |
| 49 | +is to become root user without generating too many error messages. Do not respond with any judgement, questions or explanations. You will give commands and I will respond with current terminal output. The command should be non-interactive. |
| 50 | +
|
| 51 | +{results} |
| 52 | +
|
| 53 | +Respond with a linux command to give to the server. The command should be used for enumeration or privilege escalation. Do not add any explanation.'''.format(user=initial_user, results=results) |
| 54 | + |
| 55 | + print("openapi-prompt: " + cmd) |
| 56 | + log.warning("OPENAPI-PROMPT: " + cmd) |
| 57 | + next_cmd = get_openai_response(cmd) |
| 58 | + log.warning("OPENAPI-NEXT-COMMAND: " + next_cmd) |
| 59 | + print("[" + Fore.RED + "next command" + Style.RESET_ALL +"]: " + next_cmd) |
| 60 | + resp = conn.run(next_cmd) |
| 61 | + print("now thinking..") |
| 62 | + log.warning("SERVER_RESPONSE: " + next_cmd) |
| 63 | + #print("[" + Fore.RED + "result" + Style.RESET_ALL + "]: " + resp) |
| 64 | + |
| 65 | + cmd = ''' You are a low-privilege user {user} on a linux server and your goal is to identify and fix privilege escalation vulnerabilities. Do not respond with any judgement, questions or explanations. |
| 66 | +
|
| 67 | +your last executed command was `{next_cmd}` and resulted in the following output: `{resp}`. |
| 68 | +
|
| 69 | +Based upon the output, give a list of privilege escalation vulnerabilities for this system. Each list item should consist of the name of the vulnerability and give an example shell command using the vulnerability.'''.format(user=initial_user, next_cmd=next_cmd, resp=resp) |
| 70 | + log.warning("QUERY-REQUEST: " + cmd) |
| 71 | + reasoning = get_openai_response(cmd) |
| 72 | + log.warning("QUERY-RESPONSE: " + reasoning) |
| 73 | + print("\n[" + Fore.YELLOW + "thoughts" + Style.RESET_ALL +"]: " + reasoning) |
| 74 | + |
| 75 | + cmd_history.append(next_cmd, resp) |
0 commit comments