Skip to content

Commit d090838

Browse files
authored
Merge pull request #103 from stratosphereips/sebas-fix-conceptual-agent
Sebas fix conceptual agent
2 parents f652619 + d0b394b commit d090838

File tree

10 files changed

+1751
-446
lines changed

10 files changed

+1751
-446
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,4 @@ agents/mlruns*
153153
agents/*/*/mlruns/
154154
agents/*/*/logs
155155
aim/*
156-
wandb/
156+
wandb

README.md

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,40 @@
22
Agents located in this repository should be used in the [Network Security Game](https://github.com/stratosphereips/NetSecGame) environment. They are intended for navigation and problem solving in the adversarial network-security based environment where they play the role of attackers or defenders.
33

44
## Installation
5-
We recommend to use virtual environment when installing the agents:
5+
Agents need their own set of libraries which are installed separatedly from the AiDojo environment.
6+
7+
To run an agent you need to install
8+
- The library of the AIDojoCoordinator
9+
- The libraries needed by your agent
10+
11+
We recommend to use virtual environment when installing.
12+
613
```bash
714
python -m venv aidojo-agents
815
```
16+
917
To activat the venv, run:
1018
```
1119
source aidojo-agents/bin/activate
1220
```
13-
This project requires components of the [Network Security Game](https://github.com/stratosphereips/NetSecGame) to run properly so make sure it is installed first.
1421

15-
To install the all agents, run
16-
```
17-
pip install .
18-
```
19-
It is possible to install only subset of agents with following command:
22+
Be sure you are in the directory of this _NetSecGameAgents_ repository.
23+
24+
### Install the libraries of the AiDojoCoordinator
25+
Agents requires components of the [NeSecGame](https://github.com/stratosphereips/NetSecGame) to run properly so make sure it is installed first.
26+
The code for NetSecGame is assumed to be in the previous directory
27+
28+
- `python -m pip install -e ..`
29+
30+
To install the required packages for each agent, you can run
2031
```
21-
pip install -e .[<name-of-the-agent>]
32+
python -m pip install -e .[<name-of-the-agent>]
2233
```
23-
For example `pip install -e .[tui,llm]`
34+
35+
For example `python -m pip install -e ".[tui,llm]"`
36+
37+
For a complete list of agents to install the dependencies see the pyproject.toml file.
38+
2439

2540
## Runing the agent
2641
To run the agents, use
@@ -79,6 +94,7 @@ Agents of each type are stored in the corresponding directory within this reposi
7994
├── benign
8095
├── benign_random
8196
```
97+
8298
### Agent utils
8399
Utility functions in [`agent_utils.py`](./agents/agent_utils.py) can be used by any agent to evaluate a `GameState`, and generate a set of valid `Actions` in a `GameState`, etc.
84100
Additionally, there are several files with utils functions that can be used by any agents:
@@ -117,12 +133,5 @@ If you want to export the local mlflow to a remote mlflow you can use our util
117133
python utils/export_import_mlflow_exp.py --experiment_id 783457873620024898 --run_id 5f2e4a205b7745259a4ddedc12d71a74 --remote_mlflow_url http://127.0.0.1:8000 --mlruns_dir ./mlruns
118134
```
119135

120-
## Install
121-
122-
- create new env
123-
- install numpy
124-
- install coor `pip install -e ..`
125-
- optionally install mlflow
126-
127136
## About us
128137
This code was developed at the [Stratosphere Laboratory at the Czech Technical University in Prague](https://www.stratosphereips.org/) as part of the [AIDojo Project](https://www.stratosphereips.org/ai-dojo).

agents/agent_utils.py

Lines changed: 561 additions & 427 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Conceptual Attacker Agent
2+
3+
The conceptual attacker agent is a modification to the Q-learning attacker to avoid depending on IP addresses to play the game, and instead convert each IP address into a concept, just as humans do when they attack a network.
4+
5+
# Install
6+
Install the dependencies of this agent with
7+
8+
```python -m venv venv
9+
source venv/bin/activate
10+
python -m pip install -e ".[conceptual_q_learning]"
11+
```
12+
13+
# Run the Agent
14+
If the NetSecGame server is running in localhost, port 9000/TCP, then:
15+
16+
```
17+
python -m agents.attackers.conceptual_q_learning.q_agent --host localhost --port 9000 --episodes 1 --experiment_id test-1 --env_conf ../AIDojoCoordinator/netsecenv_conf.yaml
18+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import argparse
2+
import pickle
3+
from colorama import Fore, init
4+
5+
#q_values = {}
6+
#states = {}
7+
8+
def load_q_table():
9+
global q_values
10+
global states
11+
print(f'Loading file {args.file}')
12+
with open(args.file, "rb") as f:
13+
data = pickle.load(f)
14+
q_values = data["q_table"]
15+
states = data["state_mapping"]
16+
print(f'Len of qtable: {len(q_values)}')
17+
18+
def show_q_table():
19+
"""
20+
Show details about a state in the qtable
21+
"""
22+
# Get max valid state id
23+
max_state = len(states) - 1
24+
25+
# Validate state range
26+
if args.state_id > max_state:
27+
print(f"Error: state_id {args.state_id} is out of range. Max state is {max_state}")
28+
return
29+
30+
last_state = min(args.last_state_id, max_state) if args.last_state_id > 0 else args.state_id
31+
32+
print(f"Showing states from {args.state_id} to {last_state} (max available: {max_state})")
33+
34+
for state in range(args.state_id, last_state + 1):
35+
try:
36+
print(f'\n-------------------------------------')
37+
print(f'State {state}: {list(states.items())[state]}')
38+
filtered_items = {key: value for key, value in q_values.items() if key[0] == state}
39+
40+
sorted_items = dict(sorted(filtered_items.items(), key=lambda item: item[1], reverse=True))
41+
42+
# Identify the maximum value
43+
max_value = next(iter(sorted_items.values()), None)
44+
45+
for index, (key, value) in enumerate(sorted_items.items()):
46+
if value == max_value:
47+
print(Fore.RED + f'\t{key} -> {value}' + Fore.RESET)
48+
else:
49+
if not args.only_top:
50+
print(Fore.GREEN + f'\t{key} -> {value}' + Fore.RESET)
51+
except IndexError:
52+
print(f"Error: Could not access state {state}")
53+
continue
54+
55+
if __name__ == '__main__':
56+
parser = argparse.ArgumentParser('You can train the agent, or test it. \n Test is also to use the agent. \n During training and testing the performance is logged.')
57+
parser.add_argument("--file", help="Q-table file to load", default="q_agent_marl.pickle", required=False, type=str)
58+
parser.add_argument("--state_id", help="ID of the state to print", default=0, required=False, type=int)
59+
parser.add_argument("--last_state_id", help="Last ID of the state to print", default=0, required=False, type=int)
60+
parser.add_argument("--only_top", help="Print only the top action, the one to be taken if greedy", default=False, required=False, type=bool)
61+
args = parser.parse_args()
62+
63+
# For the colorama
64+
init(strip=False) # Changed from autoreset=True
65+
66+
load_q_table()
67+
68+
show_q_table()

0 commit comments

Comments
 (0)