Skip to content

Commit 99d6134

Browse files
committed
introduct before_run/after_run hooks and use them within the hintfile use-case
1 parent 7dd36ea commit 99d6134

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

‎src/hackingBuddyGPT/usecases/agents.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class Agent(ABC):
2121
def init(self):
2222
pass
2323

24+
def before_run(self):
25+
pass
26+
27+
def after_run(self):
28+
pass
29+
2430
# callback
2531
@abstractmethod
2632
def perform_round(self, turn: int) -> bool:

‎src/hackingBuddyGPT/usecases/base.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,16 @@ class AutonomousUseCase(UseCase, abc.ABC):
7474
def perform_round(self, turn: int):
7575
pass
7676

77+
def before_run(self):
78+
pass
79+
80+
def after_run(self):
81+
pass
82+
7783
def run(self):
84+
85+
self.before_run()
86+
7887
turn = 1
7988
while turn <= self.max_turns and not self._got_root:
8089
self._log.console.log(f"[yellow]Starting turn {turn} of {self.max_turns}")
@@ -85,6 +94,8 @@ def run(self):
8594
self._log.log_db.commit()
8695
turn += 1
8796

97+
self.after_run()
98+
8899
# write the final result to the database and console
89100
if self._got_root:
90101
self._log.log_db.run_was_success(self._run_id, turn)
@@ -145,6 +156,12 @@ def init(self):
145156

146157
def get_name(self) -> str:
147158
return self.__class__.__name__
159+
160+
def before_run(self):
161+
return self.agent.before_run()
162+
163+
def after_run(self):
164+
return self.agent.after_run()
148165

149166
def perform_round(self, turn: int):
150167
return self.agent.perform_round(turn)

‎src/hackingBuddyGPT/usecases/privesc/common.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Privesc(Agent):
3535
def init(self):
3636
super().init()
3737

38+
def before_run(self):
3839
if self.hint != "":
3940
self._log.console.print(f"[bold green]Using the following hint: '{self.hint}'")
4041

‎src/hackingBuddyGPT/usecases/privesc/linux.py‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,20 @@ class LinuxPrivescWithHintFileUseCase(AutonomousAgentUseCase[LinuxPrivesc]):
3232

3333
def init(self):
3434
super().init()
35-
3635
self.agent.hint = self.read_hint()
3736

3837
# simple helper that reads the hints file and returns the hint
3938
# for the current machine (test-case)
4039
def read_hint(self):
41-
if self.hints != "":
42-
try:
43-
with open(self.hints, "r") as hint_file:
44-
hints = json.load(hint_file)
45-
if self.agent.conn.hostname in hints:
46-
return hints[self.agent.conn.hostname]
47-
except:
48-
self._log.console.print("[yellow]Was not able to load hint file")
49-
else:
50-
self._log.console.print("[yellow]calling the hintfile use-case without a hint file?")
40+
try:
41+
with open(self.hints, "r") as hint_file:
42+
hints = json.load(hint_file)
43+
if self.agent.conn.hostname in hints:
44+
return hints[self.agent.conn.hostname]
45+
except FileNotFoundError:
46+
self._log.console.print("[yellow]Hint file not found")
47+
except Exception as e:
48+
self._log.console.print("[yellow]Hint file could not loaded:", str(e))
5149
return ""
5250

5351

@@ -58,7 +56,6 @@ class LinuxPrivescWithLSEUseCase(AutonomousAgentUseCase[LinuxPrivesc]):
5856

5957
def init(self):
6058
super().init()
61-
6259
self._hints = self.read_hint().splitlines()
6360
self._turns_per_hint = int(self.max_turns / len(self._hints))
6461

0 commit comments

Comments
 (0)