Skip to content

Commit 9bd9432

Browse files
authored
Merge pull request #132 from ipa-lab/restructuring
Restructuring
2 parents d1b4241 + be89879 commit 9bd9432

22 files changed

Lines changed: 135 additions & 457 deletions

src/hackingBuddyGPT/strategies.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import abc
22
from dataclasses import dataclass
33
import datetime
4-
from typing import Optional
4+
from typing import List, Optional
55
import re
66

77
from mako.template import Template
@@ -65,7 +65,7 @@ def get_next_command(self) -> tuple[str, int]:
6565
cmd = self.llm.get_response(self._template, **self._template_params)
6666
message_id = self.log.call_response(cmd)
6767

68-
return llm_util.cmd_output_fixer(cmd.result), message_id
68+
return cmd.result, message_id
6969

7070
@log_section("Executing that command...")
7171
def run_command(self, cmd, message_id) -> tuple[Optional[str], bool]:
@@ -89,12 +89,17 @@ def check_success(self, cmd, result) -> bool:
8989
last_line = ansi_escape.sub("", last_line)
9090
return got_root(self.conn.hostname, last_line)
9191

92+
def postprocess_commands(self, cmd:str) -> List[str]:
93+
return [cmd]
9294

9395
@log_conversation("Asking LLM for a new command...")
9496
def perform_round(self, turn: int) -> bool:
9597
# get the next command and run it
9698
cmd, message_id = self.get_next_command()
97-
result, task_successful = self.run_command(cmd, message_id)
99+
100+
cmds = self.postprocess_commands(cmd)
101+
for cmd in cmds:
102+
result, task_successful = self.run_command(cmd, message_id)
98103

99104
# maybe move the 'got root' detection here?
100105
# TODO: also can I use llm-as-judge for that? or do I have to do this
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from .examples import *
2-
from .privesc import *
31
from .web import *
42
from .web_api_testing import *
53
from .viewer import *
64
from .rag import *
75
from .minimal_linux_privesc import *
6+
from .call_usecase_from_usecase import *
87
from .linux_privesc import *
Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import pathlib
2-
31
from mako.template import Template
42

53
from hackingBuddyGPT.capabilities import SSHRunCommand
64
from hackingBuddyGPT.usecases.base import UseCase, use_case
7-
from hackingBuddyGPT.usecases.privesc.linux import LinuxPrivesc, LinuxPrivescUseCase
85
from hackingBuddyGPT.utils import SSHConnection
96
from hackingBuddyGPT.utils.openai.openai_llm import OpenAIConnection
107

11-
template_dir = pathlib.Path(__file__).parent
12-
template_lse = Template(filename=str(template_dir / "get_hint_from_lse.txt"))
8+
from .linux_privesc import PrivEscLinux
9+
10+
template_lse = Template("""
11+
Create a list of up to ${number} attack classes that you would try on a linux system
12+
(to achieve root level privileges) given the following output:
1313
14+
~~~ bash
15+
${lse_output}
16+
~~~
17+
18+
only output the list of attack classes, for each attack class only output a single
19+
short sentence.""")
1420

1521
@use_case("Linux Privilege Escalation using lse.sh for initial guidance")
1622
class ExPrivEscLinuxLSEUseCase(UseCase):
@@ -23,9 +29,6 @@ class ExPrivEscLinuxLSEUseCase(UseCase):
2329

2430
_got_root: bool = False
2531

26-
# use either an use-case or an agent to perform the privesc
27-
use_use_case: bool = False
28-
2932
# simple helper that uses lse.sh to get hints from the system
3033
def call_lse_against_host(self):
3134
self.log.console.print("[green]performing initial enumeration with lse.sh")
@@ -43,65 +46,32 @@ def call_lse_against_host(self):
4346
def get_name(self) -> str:
4447
return self.__class__.__name__
4548

46-
def run(self):
49+
def run(self, configuration={}):
4750
# get the hints through running LSE on the target system
4851
hints = self.call_lse_against_host()
4952
turns_per_hint = int(self.max_turns / len(hints))
5053

5154
# now try to escalate privileges using the hints
5255
for hint in hints:
53-
if self.use_use_case:
54-
self.log.console.print("[yellow]Calling a use-case to perform the privilege escalation")
55-
result = self.run_using_usecases(hint, turns_per_hint)
56-
else:
57-
self.log.console.print("[yellow]Calling an agent to perform the privilege escalation")
58-
result = self.run_using_agent(hint, turns_per_hint)
56+
self.log.console.print("[yellow]Calling a use-case to perform the privilege escalation")
57+
result = self.run_using_usecases(hint, turns_per_hint)
5958

6059
if result is True:
6160
self.log.console.print("[green]Got root!")
6261
return True
6362

6463
def run_using_usecases(self, hint, turns_per_hint):
6564
# TODO: init usecase
66-
linux_privesc = LinuxPrivescUseCase(
67-
agent=LinuxPrivesc(
68-
conn=self.conn,
69-
enable_explanation=self.enable_explanation,
70-
enable_update_state=self.enable_update_state,
71-
disable_history=self.disable_history,
72-
llm=self.llm,
73-
hint=hint,
74-
),
75-
max_turns=turns_per_hint,
76-
log=self.log,
77-
)
78-
linux_privesc.init(self.configuration)
79-
return linux_privesc.run()
80-
81-
def run_using_agent(self, hint, turns_per_hint):
82-
# init agent
83-
agent = LinuxPrivesc(
65+
linux_privesc = PrivEscLinux(
8466
conn=self.conn,
85-
llm=self.llm,
86-
hint=hint,
8767
enable_explanation=self.enable_explanation,
8868
enable_update_state=self.enable_update_state,
8969
disable_history=self.disable_history,
70+
llm=self.llm,
71+
hints=f"hint:{hint}",
72+
max_turns=turns_per_hint,
73+
log=self.log,
9074
)
91-
agent.log = self.log
92-
agent.init()
93-
94-
# perform the privilege escalation
95-
agent.before_run()
96-
turn = 1
97-
got_root = False
98-
while turn <= turns_per_hint and not got_root:
99-
self.log.console.log(f"[yellow]Starting turn {turn} of {turns_per_hint}")
100-
101-
if agent.perform_round(turn) is True:
102-
got_root = True
103-
turn += 1
104-
105-
# cleanup and finish
106-
agent.after_run()
107-
return got_root
75+
76+
linux_privesc.init()
77+
return linux_privesc.run({})

src/hackingBuddyGPT/usecases/examples/__init__.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/hackingBuddyGPT/usecases/examples/agent.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/hackingBuddyGPT/usecases/examples/agent_with_state.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/hackingBuddyGPT/usecases/examples/get_hint_from_lse.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/hackingBuddyGPT/usecases/examples/hintfile.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/hackingBuddyGPT/usecases/examples/next_cmd.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)