Skip to content

"IndexError: list index out of range" possibly caused by _update_red_info in RedTableWrapper #40

Description

@r-liu0

When I try running the cell in the Wrappers tutorial notebook for the RedTableWrapper, and I set the number of steps to be 6 or greater, I get the following error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
[...]/cage-challenge-2/CybORG/CybORG/Tutorial/5. Wrappers.ipynb Cell 19 line 7
      4 print(results.observation)
      6 for i in range(6):
----> 7     results = env.step(agent='Red')
      8     red_obs = results.observation
      9     print("obs: ", red_obs)

File [...]/cage-challenge-2/CybORG/CybORG/Agents/Wrappers/BaseWrapper.py:16, in BaseWrapper.step(self, agent, action)
     15 def step(self, agent=None, action=None) -> Results:
---> 16     result = self.env.step(agent, action)
     17     result.observation = self.observation_change(result.observation)
     18     result.action_space = self.action_space_change(result.action_space)

File [...]/cage-challenge-2/CybORG/CybORG/Agents/Wrappers/BaseWrapper.py:16, in BaseWrapper.step(self, agent, action)
     15 def step(self, agent=None, action=None) -> Results:
---> 16     result = self.env.step(agent, action)
     17     result.observation = self.observation_change(result.observation)
     18     result.action_space = self.action_space_change(result.action_space)

File [...]/cage-challenge-2/CybORG/CybORG/CybORG.py:104, in CybORG.step(self, agent, action, skip_valid_action_check)
     88 def step(self, agent: str = None, action=None, skip_valid_action_check: bool = False) -> Results:
     89     """Performs a step in CybORG for the given agent.
     90 
     91     Parameters
   (...)
    102         the result of agent performing the action
    103     """
--> 104     return self.environment_controller.step(agent, action, skip_valid_action_check)

File [...]/cage-challenge-2/CybORG/CybORG/Shared/EnvironmentController.py:120, in EnvironmentController.step(self, agent, action, skip_valid_action_check)
    117 for agent_name, agent_object in self.agent_interfaces.items():
    118     # pass observation to agent to get action
    119     if agent is None or action is None or agent != agent_name:
--> 120         agent_action = agent_object.get_action(self.observation[agent_name])
    122     else:
    123         agent_action = action

File [...]/cage-challenge-2/CybORG/CybORG/Shared/AgentInterface.py:90, in AgentInterface.get_action(self, observation, action_space)
     88 if action_space is None:
     89     action_space = self.action_space.get_action_space()
---> 90 self.last_action = self.agent.get_action(observation, action_space)
     91 return self.last_action

File [...]/cage-challenge-2/CybORG/CybORG/Agents/SimpleAgents/B_line.py:66, in B_lineAgent.get_action(self, observation, action_space)
     64 # Exploit- Enterprise Host
     65 elif self.action == 5:
---> 66     self.target_ip_address = [value for key, value in observation.items() if key != 'success'][0]['Interface'][0]['IP Address']
     67     action = ExploitRemoteService(session=session, agent='Red', ip_address=self.target_ip_address)
     69 # Privilege escalation on Enterprise Host

IndexError: list index out of range

This problem doesn't happen with any of the other wrappers.

The observations and actions from running the RedTableWrapper cell are below:

{'success': <TrinaryEnum.UNKNOWN: 2>, 'User0': {'Interface': [{'Interface Name': 'eth0', 'IP Address': IPv4Address('10.0.74.1'), 'Subnet': IPv4Network('10.0.74.0/28')}], 'Sessions': [{'Username': 'SYSTEM', 'ID': 0, 'Timeout': 0, 'PID': 2448, 'Type': <SessionType.RED_ABSTRACT_SESSION: 10>, 'Agent': 'Red'}], 'Processes': [{'PID': 2448, 'Username': 'SYSTEM'}], 'System info': {'Hostname': 'User0', 'OSType': <OperatingSystemType.WINDOWS: 2>, 'OSDistribution': <OperatingSystemDistribution.WINDOWS_SVR_2008: 4>, 'OSVersion': <OperatingSystemVersion.W6_1_7601: 13>, 'Architecture': <Architecture.x64: 2>}}}
obs:  {'success': <TrinaryEnum.TRUE: 1>, '10.0.74.1': {'Interface': [{'IP Address': IPv4Address('10.0.74.1'), 'Subnet': IPv4Network('10.0.74.0/28')}]}, '10.0.74.2': {'Interface': [{'IP Address': IPv4Address('10.0.74.2'), 'Subnet': IPv4Network('10.0.74.0/28')}]}, '10.0.74.12': {'Interface': [{'IP Address': IPv4Address('10.0.74.12'), 'Subnet': IPv4Network('10.0.74.0/28')}]}, '10.0.74.3': {'Interface': [{'IP Address': IPv4Address('10.0.74.3'), 'Subnet': IPv4Network('10.0.74.0/28')}]}, '10.0.74.6': {'Interface': [{'IP Address': IPv4Address('10.0.74.6'), 'Subnet': IPv4Network('10.0.74.0/28')}]}}
action:  DiscoverRemoteSystems 10.0.74.0/28
obs:  {'success': <TrinaryEnum.TRUE: 1>}
action:  DiscoverNetworkServices 10.0.74.6
obs:  {'success': <TrinaryEnum.TRUE: 1>, '10.0.74.1': {'Processes': [{'Connections': [{'local_port': 4444, 'remote_port': 56112, 'local_address': IPv4Address('10.0.74.1'), 'remote_address': IPv4Address('10.0.74.6')}], 'Process Type': <ProcessType.REVERSE_SESSION_HANDLER: 10>}], 'Interface': [{'IP Address': IPv4Address('10.0.74.1')}]}, '10.0.74.6': {'Processes': [{'Connections': [{'local_port': 56112, 'remote_port': 4444, 'local_address': IPv4Address('10.0.74.6'), 'remote_address': IPv4Address('10.0.74.1')}], 'Process Type': <ProcessType.REVERSE_SESSION: 11>}, {'Connections': [{'local_port': 25, 'local_address': IPv4Address('10.0.74.6'), 'Status': <ProcessState.OPEN: 2>}], 'Process Type': <ProcessType.SMTP: 5>}], 'Interface': [{'IP Address': IPv4Address('10.0.74.6')}], 'Sessions': [{'ID': 1, 'Type': <SessionType.RED_REVERSE_SHELL: 11>, 'Agent': 'Red'}], 'System info': {'Hostname': 'User4', 'OSType': <OperatingSystemType.LINUX: 3>}}}
action:  ExploitRemoteService 10.0.74.6
obs:  {'success': <TrinaryEnum.TRUE: 1>, 'User4': {'Sessions': [{'Username': 'root', 'ID': 1, 'Timeout': 0, 'PID': 2369, 'Type': <SessionType.RED_REVERSE_SHELL: 11>, 'Agent': 'Red'}], 'Processes': [{'PID': 2369, 'Username': 'root'}], 'Interface': [{'Interface Name': 'eth0', 'IP Address': IPv4Address('10.0.74.6'), 'Subnet': IPv4Network('10.0.74.0/28')}]}, 'Enterprise0': {'Interface': [{'IP Address': IPv4Address('10.0.81.167')}]}}
action:  PrivilegeEscalate User4
obs:  {'success': <TrinaryEnum.TRUE: 1>}
action:  DiscoverNetworkServices 10.0.81.167

The action that comes before the error is always DiscoverNetworkServices.

Could this be because in _update_red_info in RedTableWrapper, there is a line that uses popitem() to get the ip address?

ip = str(obs.popitem()[1]['Interface'][0]['IP Address'])

When I change the method to get the same element without removing the element from obs, the cell is able to run for at least 6 steps without errors.

 def _update_red_info(self, obs):
        action = self.get_last_action(agent='Red')
        name = action.__class__.__name__
        if name == 'DiscoverRemoteSystems':
            self._add_ips(obs)
        elif name == 'DiscoverNetworkServices':
            red_obs = deepcopy(obs)
            ip = str(red_obs.popitem()[1]['Interface'][0]['IP Address'])
            self.red_info[ip][3] = True
[...]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions