-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig2.py
More file actions
99 lines (85 loc) · 3.22 KB
/
Config2.py
File metadata and controls
99 lines (85 loc) · 3.22 KB
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
import re
'''
'rst_attack'
'echo_attack'
'smuf_attack'
'land_attack'
'red'
'''
class ConfigWriter(object):
def __init__(self,ControlPort,IpDst,IpBanList:list,IpBrodCast):
#basic
self.Control = 'CONTROL :: ControlSocket(tcp,'+str(ControlPort)+')\n'
self.Out_default = 'out :: Queue(1024) -> ToDevice(ens33)\n'
self.Out_red = 'out :: RED(768,1024,0.02)->Queue(1024) -> ToDevice('+IpDst+')\n'
self.Is_ip ='FromDevice(ens37)-> is_ip :: Classifier(12/0800, -)\n'
self.Not_ip ='is_ip[1]->out\n'
self.Set_IPAddr ='SetIPAddress('+IpDst+')'
self.Ip_strip = 'is_ip[0]->Strip(14)-> CheckIPHeader(CHECKSUM false) -> CheckLength(65535) -> IPReassembler() \n->\n'
self.red_flag =0
#strategy
self.rst_attack = 'rst,'
self.echo_attack ='dst udp port 7 or 19,'
self.smuf_attack ='dst '+IpBrodCast+' and icmp,'
self.land_attack = 'dst '+IpDst+' and src '+IpDst+','
if len(IpBanList)
for i in IpBanList
self.IpBaned +='src '+i+','
else
self.IpBaned =''
def ChangePort(self,newPort):
self.Control = 'CONTROL :: ControlSocket(tcp,'+newPort+')\n'
def strategy_init(self,Strategy):
self.Strategy_build=''
self.length =len(Strategy)+len(IpBanList)
for i in Strategy:
if i == 'rst_attack':
self.Strategy_build+= self.rst_attack
elif i =='echo_attack':
self.Strategy_build += self.echo_attack
elif i =='smuf_attack':
self.Strategy_build += self.smuf_attack
elif i =='land_attack':
self.Strategy_build += self.land_attack
elif i =='red':
self.red_flag = 1
self.length--
else:
print('STRATEGY ERROR')
Strategy_build += self.IpBaned
#IpClassfier
self.Ip_Classfier = 'ic :: IPClassifier( '+self.Strategy_build+ '-)'
port = ''
for i in range(self.length):
port +='ic['+str(i)+']->discard\n'
port +='ic['+str(self.length)+']->'+self.Set_IPAddr+'->out\n'
if self.red_flag == 0:
basic =self.Control + self.Out_default + self.Is_ip + self.Not_ip + self.Ip_strip
self.basic = basic
else:
basic = self.Control + self.Out_red + self.Is_ip + self.Not_ip + self.Ip_strip
self.basic =basic
self.port = port
def NewConfig(self,Strategy,id):
self.strategy_init(Strategy)
config =self.basic+self.Ip_Classfier+self.port
try:
file = open('config/test_'+id+'.click', 'w')
file.write(config)
except IOError:
print('FILE WRITE ERROR')
else:
print('FILE WRITE SUCCESS')
file.close()
def ConfigDefine(self,conf,id):
try:
file = open('config/test'+id+'.click','w')
file.write(conf)
except IOError:
print('FILE WRITE ERROR')
else:
print('FILE WRITE SUCCESS')
file.close()
if __name__ == '__main__':
witer = ConfigWriter(22222,'192.168.3.133',[],'192.168.3.255')
witer.NewConfig(('smuf_attack','land_attack','red'))