-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo2.py~
232 lines (166 loc) · 6.01 KB
/
demo2.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
#!/usr/bin/python
"""
This example shows how to create an empty Mininet object
(without a topology object) and add nodes to it manually.
"""
from mininet.net import Mininet
from mininet.node import Controller
from mininet.cli import CLI
from mininet.log import setLogLevel, info
import time
import json
import os
import socket
def ACL():
"Parse the JSON MUD file to extract Match rules"
with open('/usr/local/etc/controller/lighting-example.json') as data_file:
d = json.load(data_file)
#print(d)
acl = d["ietf-access-control-list:access-lists"]["acl"]
#print(acl)
#inbound rules#################################################################
idirection = str(acl[0]["acl-name"])
#print(idirection)
iace = acl[0]["access-list-entries"]["ace"][0]
#print(iace)
#input action
iact = str(iace["actions"]["permit"])
print(iact)
if iact == '[None]':
iact = 'ACCEPT'
print(iact)
# Matching rules
imatch = iace["matches"]
#print(imatch)
#inbound port
iport = str(imatch["destination-port-range"]["lower-port"])
#print(iport)
#Source IP
sip = imatch["ietf-acl-dnsname:source-hostname"]
#print(sip)
host = sip.split("//",1)[1]
host = host.split("/", 1)[0]
#print(host)
TranslatedIp = socket.gethostbyname(host)
#print(TranslatedIp)
#protocol
iproto = str(imatch["protocol"])
#print(iproto)
################################################################################
#print("Direction: "+ idirection)
#print("Drop Action: " + act)
#print("Port: " + iport)
#print("Source IP: " + TranslatedIp)
#print("Protocol:" + iproto)
#outbound rules#################################################################
odirection = str(acl[1]["acl-name"])
#print(odirection)
oace = acl[1]["access-list-entries"]["ace"][0]
#print(oace)
#action
oact = str(iace["actions"]["permit"])
#print(oact)
if oact == '[None]':
oact = 'ACCEPT'
#print(oact)
# Matching rules
omatch = oace["matches"]
#print(omatch)
#outbound port
oport = str(omatch["source-port-range"]["lower-port"])
#print(oport)
#Destination IP
dip = omatch["ietf-acl-dnsname:destination-hostname"]
#print(dip)
host = dip.split("//",1)[1]
host = host.split("/", 1)[0]
#print(host)
TranslatedIp = str(socket.gethostbyname(host))
#print(TranslatedIp)
#protocol
oproto = str(omatch["protocol"])
#print(oproto)
#################################################################################
#print("------------------------------------------------------------------------")
#print("Direction: "+ odirection)
#print("Drop Action: " + act)
#print("Port: " + oport)
#print("Destination IP: " + str(TranslatedIp))
#print("Protocol:" + oproto)
#return (TranslatedIp,iport,act,iproto)
inbound = []
inbound.append(TranslatedIp)
inbound.append(iport)
inbound.append(iact)
inbound.append(iproto)
#print(inbound)
outbound = []
outbound.append(TranslatedIp)
outbound.append(oport)
outbound.append(oact)
outbound.append(oproto)
#print(outbound)
return inbound,outbound
def MudNet():
"Create an empty network and add nodes to it."
net = Mininet( controller=Controller )
info( '*** Adding controller\n' )
net.addController( 'c0' )
info( '*** Adding MUD Architecture Blocks\n' )
hrs = net.addHost( 'RadServer', ip='10.0.0.1' )
hms = net.addHost( 'MUDServer', ip='10.0.0.2' )
hrc = net.addHost( 'RadClient', ip='10.0.0.3' )
info( '*** Adding switch\n' )
s3 = net.addSwitch( 's3' )
info( '*** Creating links\n' )
net.addLink( hrs, s3 )
net.addLink( hms, s3 )
net.addLink( hrc, s3 )
info( '*** Adding network hosts\n' )
h1 = net.addHost( 'h1') #Desktop connected to home network
h2 = net.addHost( 'h2') #IoT Device LED Light connected to home network
h3 = net.addHost( 'h3') ####### IP address allowed for communication
info( '*** Adding switch\n' )
s4 = net.addSwitch( 's4' )
info( '*** Creating links\n' )
net.addLink( s4, net.get('h1') )
net.addLink( s4, net.get('h2') )
net.addLink( s4, net.get('h3') )
s4.attach('s4-eth0')
s4.attach('s4-eth1')
s4.attach('s4.eth2')
info( '*** Starting network\n')
net.start()
hrs.cmdPrint('sudo radiusd -X > radbug.txt 2>&1 &')
hrs.cmd('sleep 15')
hms.cmdPrint('python -m SimpleHTTPServer 80 &')
hms.cmd('sleep 5')
hrc.cmdPrint('sudo echo "User-Name=Shashank, User-Password=IOTSECURITY, Columbia-MUD-URI=http://10.0.0.2/mud/lighting-example.json" | /usr/local/bin/radclient -x 10.0.0.1 auth testing123')
#s3.cmdPrint('ifconfig')
os.system("sleep 2")
#Create a mininet SDN with custom flow tables
#host,port,action,protocol = ACL()
x,y = ACL()
print('Inbound access entry: '+str(x))
print('////////////////////////')
print('Outbound access entry: '+str(y))
h1.cmd('ifconfig h1-eth0 hw ether 00:00:00:00:00:01 192.168.0.1')
h2.cmd('ifconfig h1-eth0 hw ether 00:00:00:00:00:02 192.168.0.2')
h3.cmd('ifconfig h1-eth0 hw ether 00:00:00:00:00:03 '+x[0])
##Add ARP
h3.cmdPrint('route add default gw 128.59.105.254 h3-eth0')
h3.cmdPrint('arp -s 128.59.105.254 00:00:00:00:33:33')
h1.cmdPrint('route add default gw 192.168.0.254 h1-eth0')
h1.cmdPrint('arp -s 192.168.0.254 00:00:00:00:00:11:11')
h2.cmdPrint('route add default gw 192.168.0.254 h2-eth0')
h2.cmdPrint('arp -s 192.168.0.254 00:00:00:00:00:11:11')
h3.cmdPrint('sudo python -m SimpleHTTPServer 80 &')
#os.system("more tables.txt")
os.system("ovs-ofctl add-flows s4 tables.txt")
info( '*** Running CLI\n' )
CLI( net )
info( '*** Stoping network' )
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
MudNet()