-
Notifications
You must be signed in to change notification settings - Fork 10
/
attack_broker.py
112 lines (88 loc) · 3.81 KB
/
attack_broker.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
import json
import shutil
import os
import sys
import fileinput
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import subprocess
import sys
from subprocess import check_output
import time
filter = "JSON file (*.json)|*.json|All Files (*.*)|*.*||"
with open('attack_library.json', 'r') as f:
distros_dict = json.load(f)
path = "Database/"
def print_function():
print("------------------------------------------")
print("Welcome to Smart Grid Simulation System!")
print("------------------------------------------")
print("Please select Attack ID from: ")
for x in range(len(distros_dict['object'])):
print("------------------------------------------")
print('Attack ID: ' + str(distros_dict['object'][x]["attack_id"]))
print('Category Name: ' + str(distros_dict['object'][x]["category_name"]))
print('Attack Type: ' + str(distros_dict['object'][x]["name"]))
print("------------------------------------------")
def readfile(index):
for x in range(len(distros_dict['object'])):
if distros_dict['object'][x]["attack_id"]==str(index):
print("\n \n------------------------------------------")
print("You selected Attack ID: " + str(distros_dict['object'][x]["attack_id"]))
print('- Category Name: ' + str(distros_dict['object'][x]["category_name"]))
print('- Attack Type: ' + str(distros_dict['object'][x]["name"]))
print('- Description: ' + str(distros_dict['object'][x]["attack_type"][0]["description"]))
print('- Attack Component: ' + str(distros_dict['object'][x]["attack_component"][0]["component_name"]))
print('- Start Time: ' + str(distros_dict['object'][x]["attack_schedule"][0]["start_time"]))
print('- End Time: ' + str(distros_dict['object'][x]["attack_schedule"][0]["end_time"]))
filepath = str(distros_dict['object'][x]["attack_component"][0]["file"])
file_out = str("run_" + filepath)
affected_value = distros_dict['object'][x]["attack_type"][0]["affected_value"][0]
print('- Affected Value: ')
for key, value in affected_value.items():
print('\t- ' + str(key) + " = " + str(value))
config(filepath, file_out, key, value)
def config(filepath, file_out, key, value):
f = open(file_out,'r')
filedata = f.read()
f.close()
if filepath == "ns-3.cc":
newdata = filedata.replace("//Flag", "//Flag"+ "\n \t" + key + " = " + str(value) + ";//")
else:
newdata = filedata.replace(key,key + " " + str(value) + ";//")
f = open(file_out,'w')
f.write(newdata)
f.close()
#Check Process is running or not
def is_running(pid):
try:
os.kill(pid, 0)
except OSError:
return False
return True
def main():
os.system('clear')
os.chdir(sys.argv[1])
os.remove("run_ns-3.cc")
os.remove("run_GridLab-D.glm")
time.sleep(1)
shutil.copyfile("ns-3.cc", "run_ns-3.cc")
shutil.copyfile("GridLab-D.glm", "run_GridLab-D.glm")
print_function()
readfile(sys.argv[2])
p1 = subprocess.Popen('./compile-ns3.sh run_ns-3.cc', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, executable='/bin/bash')
p1.wait()
time.sleep(2)
p2 = subprocess.Popen('./run.sh', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, executable='/bin/bash')
time.sleep(2)
child = subprocess.Popen('pgrep xterm', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, executable='/bin/bash')
pid = int(child.communicate()[0].split('\n')[0])
print("The Simulation is running!")
print("Process PID: "+str(pid))
while is_running(pid):
time.sleep(1)
print("Finished!")
if __name__ == '__main__':
main()