-
Notifications
You must be signed in to change notification settings - Fork 5
/
attack_A2C.py
73 lines (51 loc) · 1.74 KB
/
attack_A2C.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
import sys
import getopt
import gym
import gym_reflected_xss
# from baselines import deepq
# from baselines.logger import Logger, TensorBoardOutputFormat, HumanOutputFormat
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines.deepq.policies import MlpPolicy
from stable_baselines import DQN, A2C
# remove tensorflow warning messages
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
def callback(lcl, glb):
# stop training if reward exceeds 199
is_solved = lcl['t'] > 100 and sum(lcl['epsiode_rewards'][-101:-1] / 100 >= 100)
return is_solved
def main(argv):
start_url = ""
model_name = ""
option = 0
try:
opts, etc_args= getopt.getopt(argv[1:], "u:n")
except getopt.GetoptError:
print("Use option -o")
sys.exit(2)
for opt,arg in opts:
if opt in ("-u"):
option = arg
if opt in ("-n"):
model_name = arg
start_url = option
# create the environment
env = gym.make("reflected-xss-v0", start_url=start_url, mode=1, log_file_name="model_log.txt", block_obs=-1)
# create learning agent
print("[*] Loading A2Cmodel ...")
model = A2C.load("models/" + model_name)
print("[*] Start Agent working ...")
obs = env.reset()
numberOfTarget = 0
while True:
action , _states = model.predict(obs)
obs, rewards, done, info = env.step(action)
env.render()
if done:
numberOfTarget += 1
print("# of status: " + str(numberOfTarget))
env.reset()
if __name__ == '__main__':
main(sys.argv)