-
Notifications
You must be signed in to change notification settings - Fork 2
/
firewall_v3.py
executable file
·310 lines (260 loc) · 12.4 KB
/
firewall_v3.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
# -*- coding: utf-8 -*
"""
this version support delete a flow after a specific time
and support port match rules
"""
import sys
import os
sys.path.append("/home/bsg/FireWall")
from ryu.base import app_manager
from ryu import cfg
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_4
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
from ryu.lib.packet import arp
from ryu.lib.packet import ipv4
from ryu.lib.packet import tcp
from ryu.lib.packet import udp
import ipruleTest
import readRules as rl
import time
import signal
from os.path import expanduser
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_PSS
APP_COOKIE = 0
LIFETIME_ROUTE = 60 #time to delete entries when there are no pkt_in
HARD_LIFETIME = 600 #time to delete entries
PROTOCOLS = {1: 'icmp', 2: 'igmp', 6: 'tcp', 17: 'udp',89:'ospf'}
CONF = cfg.CONF
CONF.register_cli_opts([cfg.StrOpt('Groups',default='Group.conf',help='Group config file location'),cfg.StrOpt('SigFile',default='Group.sig',help='Group signature file location'),cfg.StrOpt('Key',default=expanduser("~")+"/.ssh/id_rsa.pub",help='public key for verifying the config file'),cfg.StrOpt('Rules',default='Rules.txt',help='iptables rules for the controller')])
class MyFireWall(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION,ofproto_v1_4.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(MyFireWall, self).__init__(*args, **kwargs)
self.mac_to_port = {}
#self.table = rl.setUp('simpleRulesTest.txt')
self.table = rl.setUp(self.CONF.Rules)
self.groupData = ipruleTest.GroupData(self.CONF.Groups)
if self.verifyConf(self.CONF.SigFile,self.CONF.Groups,self.CONF.Key):
print "Group File Signature Verified"
else:
print "Group File Signature Verification Failed!"
noVerify = raw_input("Do you want to continue running the controller?(y/n) ")
if noVerify != 'y':
sys.exit()
self.comparison = ipruleTest.Comparison(self.table,self.groupData)
signal.signal(signal.SIGHUP,self.signalHandler)
def signalHandler(self,sigID,frame):
if sigID == signal.SIGHUP:
print "Reloading Config File"
if self.verifyConf(self.CONF.SigFile,self.CONF.Groups,self.CONF.Key):
print "Group File Signature Verified"
self.groupData.reload(self.CONF.Groups)
app_manager.RyuApp.send_event_to_observers(self,ofp_event.EventOFPSwitchFeatures)
else:
print "Group File Signature Verification Failed!"
noVerify = raw_input("Do you want to continue running the controller?(y/n) ")
if noVerify != 'y':
sys.exit()
else:
self.groupData.reload(self.CONF.Groups)
def verifyConf(self,sigFile,confFile,keyFile):
pubKey = RSA.importKey(open(keyFile,'r').read())
hash = SHA256.new(open(confFile,'r').read())
if(os.path.isfile(sigFile) == False):
return False
signature = open(sigFile).read()
print "Verifying signature using",sigFile
verifier = PKCS1_PSS.new(pubKey)
if verifier.verify(hash,signature):
return True
else:
return False
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
#clean the switch when controller restart
self.del_flow(datapath)
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
print "Cleared Flow Table"
def add_flow(self, datapath, priority, match, actions, lifetime=0, hard_time=0):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
actions)]
mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
match=match, instructions=inst, idle_timeout=lifetime,
hard_timeout=hard_time)
datapath.send_msg(mod)
def del_flow(self, datapath, match=None, priority=0):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
mod = parser.OFPFlowMod(
datapath=datapath, command=ofproto.OFPFC_DELETE,
cookie=APP_COOKIE, cookie_mask=APP_COOKIE,
out_port=ofproto.OFPP_ANY, out_group=ofproto.OFPG_ANY,
match=match, priority=priority,
)
datapath.send_msg(mod)
def send_flow_stats_request(self, datapath):
ofp = datapath.ofproto
ofp_parser = datapath.ofproto_parser
cookie = cookie_mask = 0
match = ofp_parser.OFPMatch(in_port=1)
req = ofp_parser.OFPFlowStatsRequest(datapath, 0,ofp.OFPTT_ALL,ofp.OFPP_ANY, ofp.OFPG_ANY,cookie, cookie_mask,match)
datapath.send_msg(req)
@set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def flow_stats_reply_handler(self, ev):
flows = []
for stat in ev.msg.body:
flows.append('table_id=%s '
'duration_sec=%d duration_nsec=%d '
'priority=%d '
'idle_timeout=%d hard_timeout=%d flags=0x%04x '
'importance=%d cookie=%d packet_count=%d '
'byte_count=%d match=%s instructions=%s' %
(stat.table_id,
stat.duration_sec, stat.duration_nsec,
stat.priority,
stat.idle_timeout, stat.hard_timeout,
stat.flags, stat.importance,
stat.cookie, stat.packet_count, stat.byte_count,
stat.match, stat.instructions))
self.logger.debug('FlowStats: %s', flows)
def send_aggregate_stats_request(self, datapath):
ofp = datapath.ofproto
ofp_parser = datapath.ofproto_parser
cookie = cookie_mask = 0
match = ofp_parser.OFPMatch(in_port=1)
req = ofp_parser.OFPAggregateStatsRequest(datapath, 0,ofp.OFPTT_ALL,ofp.OFPP_ANY,ofp.OFPG_ANY,cookie, cookie_mask,match)
datapath.send_msg(req)
@set_ev_cls(ofp_event.EventOFPAggregateStatsReply, MAIN_DISPATCHER)
def aggregate_stats_reply_handler(self, ev):
body = ev.msg.body
self.logger.debug('AggregateStats: packet_count=%d byte_count=%d '
'flow_count=%d',
body.packet_count, body.byte_count,
body.flow_count)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
dst = eth.dst
src = eth.src
pkt_arp = pkt.get_protocols(arp.arp)
pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
print "pkt_ip####", pkt_ipv4
ipProto = None
dstIp = None
srcIp = None
if pkt_ipv4:
# ip_str = pkt_ipv4[0]
ipProto = pkt_ipv4.proto
dstIp = pkt_ipv4.dst
srcIp = pkt_ipv4.src
self.logger.info("packet IP info %s %s %s", dstIp, srcIp, ipProto)
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
# learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = in_port
if dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
actions_drop = []
actions_forward = [parser.OFPActionOutput(out_port)]
if pkt_ipv4:
#matched_rule = self.comparison.compare(PROTOCOLS[ipProto], srcIp, dstIp)
#print "matched_rule: ", matched_rule
if ipProto == 2:
# for igmp message
pass
elif ipProto == 6:
#tcp pkt
pkt_tcp = pkt.get_protocol(tcp.tcp)
print "####pkt_tcp", pkt_tcp
tcp_sport = pkt_tcp.src_port
tcp_dport = pkt_tcp.dst_port
matched_rule = self.comparison.compare(PROTOCOLS[ipProto], srcIp, dstIp, tcp_sport, tcp_dport)
print "matched_rule: ", matched_rule
if not matched_rule:
return
if matched_rule['target'] == 'ACCEPT':
match = parser.OFPMatch(ipv4_src=srcIp, ipv4_dst=dstIp, ip_proto=ipProto,
tcp_src=tcp_sport, tcp_dst=tcp_dport, eth_type=0x800)
time.sleep(0.5)
self.add_flow(datapath, 1, match, actions_forward, LIFETIME_ROUTE)
# accept, forward action
elif matched_rule['target'] == 'DROP':
match = parser.OFPMatch(ipv4_src=srcIp, ipv4_dst=dstIp, ip_proto=ipProto,
tcp_src=tcp_sport, tcp_dst=tcp_dport, eth_type=0x800)
self.add_flow(datapath, 1, match, actions_drop, LIFETIME_ROUTE)
else:
# for pkts without a matched rule
return
elif ipProto == 17:
#udp pkt
pkt_udp = pkt.get_protocol(udp.udp)
print "####pkt_udp", pkt_udp
udp_sport = pkt_udp.src_port
udp_dport = pkt_udp.dst_port
matched_rule = self.comparison.compare(PROTOCOLS[ipProto], srcIp, dstIp, udp_sport, tcp_dport)
print "matched_rule: ", matched_rule
if not matched_rule:
return
if matched_rule['target'] == 'ACCEPT':
match = parser.OFPMatch(ipv4_src=srcIp, ipv4_dst=dstIp, ip_proto=ipProto,
udp_src=udp_sport, udp_dst=tcp_dport, eth_type=0x800)
self.add_flow(datapath, 1, match, actions_forward, LIFETIME_ROUTE)
# accept, forward action
elif matched_rule['target'] == 'DROP':
match = parser.OFPMatch(ipv4_src=srcIp, ipv4_dst=dstIp, ip_proto=ipProto,
udp_src=udp_sport, udp_dst=udp_dport, eth_type=0x800)
self.add_flow(datapath, 1, match, actions_drop, LIFETIME_ROUTE)
else:
# for pkts without a matched rule
return
else:
matched_rule = self.comparison.compare(PROTOCOLS[ipProto], srcIp, dstIp)
print "matched_rule: ", matched_rule
print "Rule Target %s", matched_rule['target']
if matched_rule['target'] == 'ACCEPT':
match = parser.OFPMatch(ipv4_src=srcIp, ipv4_dst=dstIp, ip_proto=ipProto, eth_type=0x800)
self.add_flow(datapath, 1, match, actions_forward, LIFETIME_ROUTE)
# accept, forward action
elif matched_rule['target'] == 'DROP':
match = parser.OFPMatch(ipv4_src=srcIp, ipv4_dst=dstIp, ip_proto=ipProto, eth_type=0x800)
self.add_flow(datapath, 1, match, actions_drop, LIFETIME_ROUTE)
else:
# for pkts without a matched rule
return
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
in_port=in_port, actions=actions, data=data)
datapath.send_msg(out)
#self.send_flow_stats_request(datapath)
self.send_aggregate_stats_request(datapath)