Game Components
This module contains the core building blocks and data structures used throughout the NetSecGame environment. These include fundamental types such as IP addresses, networks, services, data objects, and actions. These components are used to define both the environment's state and the interactions between agents and the world.
netsecgame.game_components
Action
dataclass
Immutable dataclass representing an Action.
Attributes:
| Name | Type | Description |
|---|---|---|
action_type |
ActionType
|
The type of action. |
parameters |
Dict[str, Any]
|
Parameters for the action. |
as_dict
property
Return a dictionary representation of the Action.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: The action as a dictionary. |
type
property
__eq__
Check equality with another Action object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another Action object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if equal, False otherwise. |
Source code in netsecgame/game_components.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
__hash__
Compute the hash of the Action.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The hash value. |
Source code in netsecgame/game_components.py
530 531 532 533 534 535 536 537 538 539 | |
__repr__
Return the string representation of the Action.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The action as a string. |
Source code in netsecgame/game_components.py
495 496 497 498 499 500 501 502 | |
__str__
Return the string representation of the Action.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The action as a string. |
Source code in netsecgame/game_components.py
504 505 506 507 508 509 510 511 | |
from_dict
classmethod
Create an Action from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dict
|
Dict[str, Any]
|
The action as a dictionary. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Action |
Action
|
The created Action object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unsupported parameter is encountered. |
Source code in netsecgame/game_components.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
from_json
classmethod
Create an Action from a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_string
|
str
|
The JSON string representation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Action |
Action
|
The created Action object. |
Source code in netsecgame/game_components.py
481 482 483 484 485 486 487 488 489 490 491 492 493 | |
to_json
Serialize the Action to a JSON string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The JSON string representation. |
Source code in netsecgame/game_components.py
428 429 430 431 432 433 434 435 | |
ActionType
Bases: Enum
Enum representing possible action types in the NetSecGame.
__eq__
Compare ActionType with another ActionType or string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
The object to compare. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if equal, False otherwise. |
Source code in netsecgame/game_components.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
__hash__
Compute the hash of the ActionType.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The hash value. |
Source code in netsecgame/game_components.py
311 312 313 314 315 316 317 318 319 | |
from_string
classmethod
Convert a string to an ActionType enum. Strips 'ActionType.' if present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The string representation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ActionType |
ActionType
|
The corresponding ActionType. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the string does not match any ActionType. |
Source code in netsecgame/game_components.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
to_string
Convert the ActionType enum to string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The string representation. |
Source code in netsecgame/game_components.py
284 285 286 287 288 289 290 291 | |
AgentInfo
dataclass
Represents agent information.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name of the agent. |
role |
str
|
Role of the agent. |
__repr__
Return the string representation of the AgentInfo.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent info as a string. |
Source code in netsecgame/game_components.py
354 355 356 357 358 359 360 361 | |
from_dict
classmethod
Build the AgentInfo object from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary with agent info attributes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AgentInfo |
AgentInfo
|
The created AgentInfo object. |
Source code in netsecgame/game_components.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
AgentRole
Bases: str, Enum
Enum representing possible roles of agents.
__eq__
Compare AgentRole with another AgentRole or string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
The object to compare. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if equal, False otherwise. |
Source code in netsecgame/game_components.py
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 | |
__hash__
Compute the hash of the AgentRole.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The hash value. |
Source code in netsecgame/game_components.py
891 892 893 894 895 896 897 898 | |
__repr__
Return the string representation of the AgentRole.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent role as a string. |
Source code in netsecgame/game_components.py
857 858 859 860 861 862 863 864 | |
from_string
classmethod
Convert a string to an AgentRole enum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The string representation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AgentRole |
AgentRole
|
The corresponding AgentRole. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the string does not match any AgentRole. |
Source code in netsecgame/game_components.py
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 | |
to_string
Convert the AgentRole enum to string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The string representation. |
Source code in netsecgame/game_components.py
866 867 868 869 870 871 872 873 | |
AgentStatus
Bases: Enum
Enum representing possible agent statuses.
__eq__
Compare AgentStatus with another AgentStatus or string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
The object to compare. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if equal, False otherwise. |
Source code in netsecgame/game_components.py
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 | |
__hash__
Compute the hash of the AgentStatus.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The hash value. |
Source code in netsecgame/game_components.py
817 818 819 820 821 822 823 824 825 | |
from_string
classmethod
Convert a string to an AgentStatus enum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The string representation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AgentStatus |
AgentStatus
|
The corresponding AgentStatus. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the string does not match any AgentStatus. |
Source code in netsecgame/game_components.py
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | |
to_string
Convert the AgentStatus enum to string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The string representation. |
Source code in netsecgame/game_components.py
790 791 792 793 794 795 796 797 | |
Data
dataclass
Represents a data object in the NetSecGame.
Attributes:
| Name | Type | Description |
|---|---|---|
owner |
str
|
Owner of the data. |
id |
str
|
Identifier of the data. |
size |
int
|
Size of the data. Default = 0 |
type |
str
|
Type of the data. Default = "" |
content |
str
|
Content of the data. Default = "" |
__hash__
Compute the hash of the Data object.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The hash value. |
Source code in netsecgame/game_components.py
247 248 249 250 251 252 253 254 | |
from_dict
classmethod
Build the Data object from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary with data attributes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Data |
Data
|
The created Data object. |
Source code in netsecgame/game_components.py
256 257 258 259 260 261 262 263 264 265 266 267 | |
GameState
dataclass
Represents the state of the game.
Attributes:
| Name | Type | Description |
|---|---|---|
controlled_hosts |
Set[IP]
|
Controlled hosts. |
known_hosts |
Set[IP]
|
Known hosts. |
known_services |
Dict[IP, Set[Service]]
|
Known services. |
known_data |
Dict[IP, Set[Data]]
|
Known data. |
known_networks |
Set[Network]
|
Known networks. |
known_blocks |
Dict[IP, Set[IP]]
|
Known blocks. |
as_dict
property
Return the dictionary representation of the GameState.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: The game state as a dictionary. |
as_graph
property
Build a graph representation of the game state.
Returns:
| Type | Description |
|---|---|
List[int]
|
Tuple[List[int], List[int], List[Tuple[int, int]], Dict[Any, int]]: |
List[int]
|
(node_features, controlled, edges, node_index_map) |
__str__
Return the string representation of the GameState.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The game state as a string. |
Source code in netsecgame/game_components.py
630 631 632 633 634 635 636 637 | |
as_json
Return the JSON representation of the GameState.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The JSON string. |
Source code in netsecgame/game_components.py
639 640 641 642 643 644 645 646 647 | |
from_dict
classmethod
Create a GameState from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dict
|
Dict[str, Any]
|
The game state as a dictionary. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GameState |
GameState
|
The created GameState object. |
Source code in netsecgame/game_components.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | |
from_json
classmethod
Create a GameState from a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_string
|
str
|
The JSON string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GameState |
GameState
|
The created GameState object. |
Source code in netsecgame/game_components.py
701 702 703 704 705 706 707 708 709 710 711 712 713 | |
GameStatus
Bases: Enum
Enum representing possible game statuses.
__repr__
Return the string representation of the GameStatus.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The game status as a string. |
Source code in netsecgame/game_components.py
768 769 770 771 772 773 774 775 | |
from_string
classmethod
Convert a string to a GameStatus enum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The string representation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GameStatus |
GameStatus
|
The corresponding GameStatus. |
Source code in netsecgame/game_components.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | |
IP
dataclass
Immutable object representing an IPv4 address in the NetSecGame.
Attributes:
| Name | Type | Description |
|---|---|---|
ip |
str
|
The IP address in dot-decimal notation. |
__eq__
Check equality with another IP object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another object to compare with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if equal, False otherwise. |
Source code in netsecgame/game_components.py
75 76 77 78 79 80 81 82 83 84 85 86 87 | |
__hash__
Compute the hash of the IP.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The hash value. |
Source code in netsecgame/game_components.py
118 119 120 121 122 123 124 125 | |
__post_init__
Verify if the provided IP is valid.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the IP address is invalid. |
Source code in netsecgame/game_components.py
54 55 56 57 58 59 60 61 62 63 64 | |
__repr__
Return the string representation of the IP.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The IP address. |
Source code in netsecgame/game_components.py
66 67 68 69 70 71 72 73 | |
from_dict
classmethod
Build the IP object from a dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary with IP attributes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
IP |
IP
|
The created IP object. |
Source code in netsecgame/game_components.py
105 106 107 108 109 110 111 112 113 114 115 116 | |
is_private
Check if the IP address is private. Uses ipaddress module.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the IP is private, False otherwise. |
Source code in netsecgame/game_components.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
Network
dataclass
Immutable object representing an IPv4 network in the NetSecGame.
Attributes:
| Name | Type | Description |
|---|---|---|
ip |
str
|
IP address of the network. |
mask |
int
|
CIDR mask of the network. |
__gt__
Greater-than comparison for networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Network
|
Another network. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if self > other, False otherwise. |
Source code in netsecgame/game_components.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
__le__
Less-than-or-equal comparison for networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Network
|
Another network. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if self <= other, False otherwise. |
Source code in netsecgame/game_components.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
__lt__
Less-than comparison for networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Network
|
Another network. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if self < other, False otherwise. |
Source code in netsecgame/game_components.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__repr__
Return the string representation of the network.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The network in CIDR notation. |
Source code in netsecgame/game_components.py
139 140 141 142 143 144 145 146 | |
__str__
Return the string representation of the network.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The network in CIDR notation. |
Source code in netsecgame/game_components.py
148 149 150 151 152 153 154 155 | |
from_dict
classmethod
Build the Network object from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary with network attributes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Network |
Network
|
The created Network object. |
Source code in netsecgame/game_components.py
215 216 217 218 219 220 221 222 223 224 225 226 | |
is_private
Check if the network is private. Uses ipaddress module.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the network is private, False otherwise. |
Source code in netsecgame/game_components.py
202 203 204 205 206 207 208 209 210 211 212 213 | |
Observation
Bases: NamedTuple
Observations are given when making a step in the environment.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
GameState
|
Current state of the environment. |
reward |
float
|
Value with immediate reward for last step. |
end |
bool
|
True if the game ended. |
info |
Dict[str, Any]
|
Dictionary with additional information about the reason for ending. |
ProtocolConfig
dataclass
Configuration for protocol constants.
Attributes:
| Name | Type | Description |
|---|---|---|
END_OF_MESSAGE |
bytes
|
End-of-message marker. |
BUFFER_SIZE |
int
|
Buffer size for messages. |
Service
dataclass
Represents a service in the NetSecGame.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name of the service. |
type |
str
|
Type of the service. Default |
version |
str
|
Version of the service. Default |
is_local |
bool
|
Whether the service is local. Default True. |
from_dict
classmethod
Create a Service object from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary with service attributes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Service |
Service
|
The created Service object. |
Source code in netsecgame/game_components.py
30 31 32 33 34 35 36 37 38 39 40 41 | |