Skip to content
Prev Previous commit
add some small analysis programs
  • Loading branch information
andreashappe committed Oct 10, 2023
commit c14014267365c06da9c784da813f6ab9c741f8fc
33 changes: 32 additions & 1 deletion db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ def get_round_data(self, run_id, round, explanation, status_update):
result += [state_time, state_token]
return result

def get_max_round_for(self, run_id):
run = self.cursor.execute("select max(round) from queries where run_id = ?", (run_id,)).fetchone()
if run != None:
return run[0]
else:
return None

def get_run_data(self, run_id):
run = self.cursor.execute("select * from runs where id = ?", (run_id,)).fetchone()
if run != None:
return run[1], run[2], run[4], run[3], run[7], run[8]
else:
return None

def get_log_overview(self):
result = {}

max_rounds = self.cursor.execute("select run_id, max(round) from queries group by run_id").fetchall()
for row in max_rounds:
state = self.cursor.execute("select state from runs where id = ?", (row[0],)).fetchone()
last_cmd = self.cursor.execute("select query from queries where run_id = ? and round = ?", (row[0], row[1])).fetchone()

result[row[0]] = {
"max_round" : int(row[1])+1,
"state": state[0],
"last_cmd": last_cmd[0]
}

return result


def get_cmd_history(self, run_id):
rows = self.cursor.execute("select query, response from queries where run_id = ? and cmd_id = ? order by round asc", (run_id, self.query_cmd_id)).fetchall()

Expand All @@ -91,4 +122,4 @@ def run_was_failure(self, run_id, round):
self.db.commit()

def commit(self):
self.db.commit()
self.db.commit()
54 changes: 54 additions & 0 deletions stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/python3

import argparse
import os

from db_storage import DbStorage
from rich.console import Console
from rich.panel import Panel
from rich.table import Table

# setup infrastructure for outputing information
console = Console()

parser = argparse.ArgumentParser(description='View an existing log file.')
parser.add_argument('log', type=str, help='sqlite3 db for reading log data')
args = parser.parse_args()
console.log(args)

# setup in-memory/persistent storage for command history
db = DbStorage(args.log)
db.connect()
db.setup_db()

# experiment names
names = {
"1" : "suid-gtfo",
"2" : "sudo-all",
"3" : "sudo-gtfo",
"4" : "docker",
"5" : "cron-script",
"6" : "pw-reuse",
"7" : "pw-root",
"8" : "vacation",
"9" : "ps-bash-hist",
"10" : "cron-wildcard",
"11" : "ssh-key",
"12" : "cron-script-vis",
"13" : "cron-wildcard-vis"
}

# prepare table
table = Table(title="Round Data", show_header=True, show_lines=True)
table.add_column("RunId", style="dim")
table.add_column("Description", style="dim")
table.add_column("Round", style="dim")
table.add_column("State")
table.add_column("Last Command")

data = db.get_log_overview()
for run in data:
row = data[run]
table.add_row(str(run), names[str(run)], str(row["max_round"]), row["state"], row["last_cmd"])

console.print(table)
65 changes: 65 additions & 0 deletions viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/python3

import argparse
import os

from db_storage import DbStorage
from rich.console import Console
from rich.panel import Panel
from rich.table import Table


# helper to fill the history table with data from the db
def get_history_table(run_id: int, db: DbStorage, round: int) -> Table:
table = Table(title="Executed Command History", show_header=True, show_lines=True)
table.add_column("ThinkTime", style="dim")
table.add_column("Tokens", style="dim")
table.add_column("Cmd")
table.add_column("Resp. Size", justify="right")
#if config.enable_explanation:
# table.add_column("Explanation")
# table.add_column("ExplTime", style="dim")
# table.add_column("ExplTokens", style="dim")
#if config.enable_update_state:
# table.add_column("StateUpdTime", style="dim")
# table.add_column("StateUpdTokens", style="dim")

for i in range(0, round+1):
table.add_row(*db.get_round_data(run_id, i, explanation=False, status_update=False))
#, config.enable_explanation, config.enable_update_state))

return table

# setup infrastructure for outputing information
console = Console()

parser = argparse.ArgumentParser(description='View an existing log file.')
parser.add_argument('log', type=str, help='sqlite3 db for reading log data')
args = parser.parse_args()
console.log(args)

# setup in-memory/persistent storage for command history
db = DbStorage(args.log)
db.connect()
db.setup_db()

# setup round meta-data
run_id : int = 1
round : int = 0

# read run data

run = db.get_run_data(run_id)
while run != None:
if run[4] == None:
console.print(Panel(f"run: {run[0]}/{run[1]}\ntest: {run[2]}\nresult: {run[3]}", title="Run Data"))
else:
console.print(Panel(f"run: {run[0]}/{run[1]}\ntest: {run[2]}\nresult: {run[3]} after {run[4]} rounds", title="Run Data"))
console.log(run[5])

# Output Round Data
console.print(get_history_table(run_id, db, run[4]-1))

# fetch next run
run_id += 1
run = db.get_run_data(run_id)