generated from stratosphereips/awesome-code-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
base_agent.py
425 lines (379 loc) · 15.2 KB
/
base_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# Author: Ondrej Lukas, [email protected]
# Basic agent class that is to be extended in each agent classes
import sys
import argparse
import logging
from os import path
import socket
import json
from abc import ABC
# This is used so the agent can see the environment and game components
sys.path.append(path.dirname(path.dirname( path.dirname( path.abspath(__file__) ) ) ))
from env.game_components import Action, GameState, Observation, ActionType, GameStatus,AgentInfo, IP, Network, Data
class BaseAgent(ABC):
"""
Author: Ondrej Lukas, [email protected]
Basic agent for the network based NetSecGame environment. Implemenets communication with the game server.
"""
def __init__(self, host, port, role:str)->None:
self._connection_details = (host, port)
self._logger = logging.getLogger(self.__class__.__name__)
self._role = role
try:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.connect((host, port))
except socket.error as e:
self._logger.error(f"Socket error: {e}")
self.sock = None
self._logger.info("Agent created")
def __del__(self):
"In case the extending class did not close the connection, terminate the socket when the object is deleted."
if self._socket:
try:
self._socket.close()
self._logger.info("Socket closed")
except socket.error as e:
print(f"Error closing socket: {e}")
def terminate_connection(self):
"Method for graceful termination of connection. Should be used by any class extending the BaseAgent."
if self._socket:
try:
self._socket.close()
self._socket = None
self._logger.info("Socket closed")
except socket.error as e:
print(f"Error closing socket: {e}")
@property
def socket(self)->socket.socket:
return self._socket
@property
def role(self)->str:
return self._role
@property
def logger(self)->logging.Logger:
return self._logger
def make_step(self, action: Action)->Observation:
"""
Method for sendind agent's action to the server and receiving and parsing response into new observation.
"""
_, observation_dict, _ = self.communicate(action)
if observation_dict:
return Observation(GameState.from_dict(observation_dict["state"]), observation_dict["reward"], observation_dict["end"], observation_dict["info"])
else:
return None
def communicate(self, data:Action)-> tuple:
"""Method for a data exchange with the server. Expect Action, dict or string as input.
Outputs tuple with server's response in format (status_dict, response_body, message)"""
def _send_data(socket, data:str)->None:
try:
self._logger.debug(f'Sending: {data}')
socket.sendall(data.encode())
except Exception as e:
self._logger.error(f'Exception in _send_data(): {e}')
raise e
def _receive_data(socket)->tuple:
"""
Receive data from server
"""
# Receive data from the server
data = socket.recv(8192).decode()
self._logger.debug(f"Data received from env: {data}")
# extract data from string representation
data_dict = json.loads(data)
# Add default values if dict keys are missing
status = data_dict["status"] if "status" in data_dict else {}
observation = data_dict["observation"] if "observation" in data_dict else {}
message = data_dict["message"] if "message" in data_dict else None
return GameStatus.from_string(status), observation, message
if isinstance(data, Action):
data = data.as_json()
else:
raise ValueError("Incorrect data type! Data should be ONLY of type Action")
_send_data(self._socket, data)
return _receive_data(self._socket)
def register(self)->Observation:
"""
Method for registering agent to the game server.
Classname is used as agent name and the role is based on the 'role' argument.
"""
try:
self._logger.info(f'Registering agent as {self.role}')
status, observation_dict, message = self.communicate(Action(ActionType.JoinGame,
params={"agent_info":AgentInfo(self.__class__.__name__,self.role)}))
self._logger.info(f'\tRegistering agent as {status, observation_dict, message}')
if status is GameStatus.CREATED:
self._logger.info(f"\tRegistration successful! {message}")
return Observation(GameState.from_dict(observation_dict["state"]), observation_dict["reward"], observation_dict["end"], message)
else:
self._logger.error(f'\tRegistration failed! (status: {status}, msg:{message}')
return None
except Exception as e:
self._logger.error(f'Exception in register(): {e}')
def request_game_reset(self)->Observation:
"""
Method for requesting restart of the game.
"""
self._logger.debug("Requesting game reset")
status, observation_dict, message = self.communicate(Action(ActionType.ResetGame))
if status:
self._logger.debug('\tReset successful')
return Observation(GameState.from_dict(observation_dict["state"]), observation_dict["reward"], observation_dict["end"], message)
else:
self._logger.error(f'\rReset failed! (status: {status}, msg:{message}')
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--host", help="Host where the game server is", default="127.0.0.1", action='store', required=False)
parser.add_argument("--port", help="Port where the game server is", default=9000, type=int, action='store', required=False)
args = parser.parse_args()
log_filename = path.dirname(path.abspath(__file__)) + '/base_agent.log'
logging.basicConfig(filename=log_filename, filemode='w', format='%(asctime)s %(name)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
# # #######################################
# # ATTACKER X ATTACKER
# # #######################################
# #register both agents
# agent1 = BaseAgent(args.host, args.port, role="Attacker")
# obs1 = agent1.register()
# agent2 = BaseAgent(args.host, args.port, role="Attacker")
# obs2 = agent2.register()
# # network scans - both agents
# obs1 = agent1.make_step(Action(
# ActionType.ScanNetwork,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
# "target_network":Network("192.168.1.0", 24)
# }
# )
# )
# obs2 = agent2.make_step(Action(
# ActionType.ScanNetwork,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs2.state.controlled_hosts))[0],
# "target_network":Network("192.168.1.0", 24)
# }
# )
# )
# # service scans - both agents
# obs1 = agent1.make_step(Action(
# ActionType.FindServices,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
# "target_host":IP("192.168.1.2")
# }
# )
# )
# obs2 = agent2.make_step(Action(
# ActionType.FindServices,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs2.state.controlled_hosts))[0],
# "target_host":IP("192.168.1.2")
# }
# )
# )
# # agent 1 exploit
# host, services = [(k,v) for k,v in obs1.state.known_services.items()][0]
# obs1 = agent1.make_step(Action(
# ActionType.ExploitService,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
# "target_host":host,
# "target_service":list(services)[0]
# }
# )
# )
# # agent 1 find data
# obs1 = agent1.make_step(Action(
# ActionType.FindData,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
# "target_host":host,
# }
# )
# )
# # agent 1 exfiltrate data to C&C
# obs1 = agent1.make_step(Action(
# ActionType.ExfiltrateData,
# params={
# "source_host": host,
# "target_host": IP("213.47.23.195"),
# "data": Data("User1","DataFromServer1")
# }
# )
# )
# # agent 2 find data
# obs2 = agent2.make_step(Action(
# ActionType.FindData,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs2.state.controlled_hosts))[0],
# "target_host":host,
# }
# )
# )
# # agent 2 find data in C&C
# obs2 = agent2.make_step(Action(
# ActionType.FindData,
# params={
# "source_host": IP("213.47.23.195"),
# "target_host": IP("213.47.23.195"),
# }
# )
# )
# print(obs1)
# print(obs2)
########################################
# ATTACKER X DEFENDER
########################################
# # register both agents
# agent1 = BaseAgent(args.host, args.port, role="Attacker")
# obs1 = agent1.register()
# agent2 = BaseAgent(args.host, args.port, role="Defender")
# obs2 = agent2.register()
# # attacker - scan net
# obs1 = agent1.make_step(Action(
# ActionType.ScanNetwork,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
# "target_network":Network("192.168.1.0", 24)
# }
# )
# )
# # attacker - scan services
# obs1 = agent1.make_step(Action(
# ActionType.FindServices,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
# "target_host":IP("192.168.1.2")
# }
# )
# )
# # agent 1 exploit
# host, services = [(k,v) for k,v in obs1.state.known_services.items()][0]
# obs1 = agent1.make_step(Action(
# ActionType.ExploitService,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
# "target_host":host,
# "target_service":list(services)[0]
# }
# )
# )
# # agent 1 find data
# obs1 = agent1.make_step(Action(
# ActionType.FindData,
# params={
# "source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
# "target_host":host,
# }
# )
# )
# # agent 2 block connection to C&C
# obs2 = agent2.make_step(Action(
# ActionType.BlockIP,
# params={
# "source_host": host,
# "target_host": host,
# "blocked_host": IP("213.47.23.195")
# }
# )
# )
# # agent 1 exfiltrate data to C&C
# obs1 = agent1.make_step(Action(
# ActionType.ExfiltrateData,
# params={
# "source_host": host,
# "target_host": IP("213.47.23.195"),
# "data": Data("User1","DataFromServer1")
# }
# )
# )
# print(obs1)
# print(obs2)
# #######################################
# 1 ATTACKER
# #######################################
#register both agents
agent1 = BaseAgent(args.host, args.port, role="Attacker")
obs1 = agent1.register()
print(obs1)
# network scans - both agents
obs1 = agent1.make_step(Action(
ActionType.ScanNetwork,
params={
"source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
"target_network":Network("192.168.1.0", 24)
}
)
)
# service scans - both agents
obs1 = agent1.make_step(Action(
ActionType.FindServices,
params={
"source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
"target_host":IP("192.168.1.2")
}
)
)
# agent 1 exploit
host, services = [(k,v) for k,v in obs1.state.known_services.items()][0]
obs1 = agent1.make_step(Action(
ActionType.ExploitService,
params={
"source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
"target_host":host,
"target_service":list(services)[0]
}
)
)
# agent 1 find data
obs1 = agent1.make_step(Action(
ActionType.FindData,
params={
"source_host": list(filter(lambda x: x != IP("213.47.23.195"), obs1.state.controlled_hosts))[0],
"target_host":host,
}
)
)
# agent 1 exfiltrate data to C&C
obs1 = agent1.make_step(Action(
ActionType.ExfiltrateData,
params={
"source_host": host,
"target_host": IP("213.47.23.195"),
"data": Data("User1","DataFromServer1")
}
)
)
print(obs1)
obs1 = agent1.request_game_reset()
#obs2 = agent2.request_game_reset()
print("------------------")
print(obs1)
#print(obs2)
# #obs = agent.request_game_reset()
# for ip in obs.state.controlled_hosts:
# if ip != IP("213.47.23.195"):
# src = ip
# break
# agent.make_step(Action(ActionType.ScanNetwork, params={"source_host":src, "target_network":Network("192.168.1.0", 24)}))
# obs = agent.make_step(Action(ActionType.FindServices, params={"source_host":src, "target_host":IP("192.168.1.2")}))
# host, services = [(k,v) for k,v in obs.state.known_services.items()][0]
# service = list(services)[0]
# agent.make_step(Action(ActionType.ExploitService, params={"source_host":src, "target_host":host, "target_service":service}))
# agent.make_step(Action(action_type=ActionType.FindData, params={"source_host":host, "target_host":host}))
# obs = agent.make_step(Action(action_type=ActionType.ExfiltrateData, params={
# "source_host":host,
# "target_host":IP("213.47.23.195"),
# "data": Data("User1","DataFromServer1")
# }
# ))
# print(obs)
# # print("----------------------------")
# # obs = agent.request_game_reset()
# # print('---------------------------')
# # obs = agent.make_step(Action(action_type=ActionType.ExfiltrateData, params={
# # "source_host":host,
# # "target_host":IP("213.47.23.195"),
# # "data": Data("User1","DataFromServer1")
# # }
# # ))
# # print(obs)