-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp_gen_policy.py
258 lines (221 loc) · 8.9 KB
/
cp_gen_policy.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
import csv
import re
import socket
import struct
import cpapi
import CP
tufin="y:/checkpoint/checkpoint/Last_policy.csv"
Policy_Package="ISFW_new"
def cidr_to_netmask(cidr):
network, net_bits = cidr.split('/')
host_bits = 32 - int(net_bits)
netmask = socket.inet_ntoa(struct.pack('!I', (1 << 32) - (1 << host_bits)))
return network, netmask
def netmask_to_cidr(netmask):
return (sum([bin(int(x)).count('1') for x in netmask.split('.')]))
list_of_hosts=[]
list_of_networks=[]
list_of_services=[]
# Open Secure Track csv and parse lines for src, dst, services
with open(tufin, newline='', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
#print(row[0],row[2])
if (row):
if re.match("Rule",(row[0])):
#print (row)
#print (row [1], row[2], row [3], row[4])
# Generate SRC Addresslist
if (row[1]!='Any'):
ip,nm=cidr_to_netmask(row[1])
#print ("ip: {} netmask: {}".format(ip,nm))
# Falls Host
if (nm=="255.255.255.255"):
name="h_"+ip
mylist=[name,ip]
if mylist not in list_of_hosts:
list_of_hosts.append(mylist)
else:
name="n_"+ip
mylist=[name,ip,nm]
if mylist not in list_of_networks:
list_of_networks.append(mylist)
# Generate DST Addresslist
if (row[2]!='Any'):
ip,nm=cidr_to_netmask(row[2])
#print ("ip: {} netmask: {}".format(ip,nm))
# Falls Host
if (nm=="255.255.255.255"):
name="h_"+ip
mylist=[name,ip]
if mylist not in list_of_hosts:
list_of_hosts.append(mylist)
else:
name="n_"+ip
mylist=[name,ip,nm]
if mylist not in list_of_networks:
list_of_networks.append(mylist)
# Generate Services List
if (row[4]!='Any'):
#print ("ip: {} netmask: {}".format(ip,nm))
# Falls Host
name=row[4]+"_"+row[3]
mylist=[name,row[3],row[4]]
if mylist not in list_of_services:
list_of_services.append(mylist)
#"api-key" : "rSAotPs5GYdduCluv5JbyQ=="
# Instance Class Object and login
cp=CP.CP("192.168.173.86","andy","admin123","tch01")
# Check for duplicate IP Object
obj_dictionary=cp.get_host_dict() # dict of hosts, key is IP
for item in list_of_hosts:
#print (item[1])
if item[1] in obj_dictionary:
print ("IP: {} already exist as {}".format(item[1], obj_dictionary[item[1]]))
else:
cp.add_host(item[0],item[1])
# Check for duplicate Network Object
obj_dictionary=cp.get_network_dict()
for item in list_of_networks:
if item[1] in obj_dictionary:
# print (obj_dictionary[item[1]][0]['subnet-mask'])
# print (item[2])
if ( item[2] == obj_dictionary[item[1]][0]["subnet-mask"] ):
print ("Subnet: {} already exists as {}".format(item, obj_dictionary[item[1]]))
else:
cp.add_network(item[0],item[1],item[2])
# generate tcp and udp list from services list
list_of_tcp_services=[]
list_of_udp_services=[]
for item in list_of_services:
print (item[2])
if item[2]=='TCP':
list_of_tcp_services.append(item)
if item[2]=='UDP':
list_of_udp_services.append(item)
# check if tcp service exists, if not add ist to database
obj_dictionary=cp.get_tcp_services_dict()
# d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}
# 'one' in d.values()
for item in list_of_tcp_services:
#print (item[1])
for objects in obj_dictionary:
if item[1]==obj_dictionary[objects][0]['port']:
found=True
#print (item,obj_dictionary[objects][0]['port'])
break
if found:
print (item,obj_dictionary[objects][0]['name'],obj_dictionary[objects][0]['port'])
found=False
else:
print ("Add service {}".format(item[1]))
cp.add_service_tcp(item[0],item[1])
obj_dictionary=cp.get_udp_services_dict()
for item in list_of_udp_services:
#print (item[1])
for objects in obj_dictionary:
if item[1]==obj_dictionary[objects][0]['port']:
found=True
#print (item,obj_dictionary[objects][0]['port'])
break
if found:
print (item,obj_dictionary[objects][0]['name'],obj_dictionary[objects][0]['port'])
found=False
else:
print ("Add service {}".format(item[1]))
cp.add_service_udp(item[0],item[1])
# Check for duplicate UDP port
# Todo
# End of object generation
# Generate Policy Package
mydict= { "name" : Policy_Package,
"comments" : "Tchibo Test",
"color" : "green",
"threat-prevention" : False,
"access" : True
}
response=cp.call_api("add-package",mydict)
if response.success:
print ( "New_Standard_Package_1 added")
# Generate Rules
net_dictionary=cp.get_network_dict()
host_dictionary=cp.get_host_dict()
tcp_dictionary=cp.get_tcp_services_dict()
udp_dictionary=cp.get_udp_services_dict()
comment=""
# Open csv and generate rules
print ("rulebase generation")
with open(tufin, newline='', encoding='utf-8') as f:
reader = csv.reader(f)
position=1
for row in reader:
#print(row[0],row[2])
if (row):
if re.match("Rule",(row[0])):
#print (row)
#print (row [1], row[2], row [3], row[4])
# Generate SRC Addresslist
if (row[1]!='Any'):
ip,nm=cidr_to_netmask(row[1])
#print ("ip: {} netmask: {}".format(ip,nm))
# Falls Host
if (nm=="255.255.255.255"):
src=host_dictionary[ip][0]['uid']
else:
src=net_dictionary[ip][0]['uid']
else:
src="97aeb369-9aea-11d5-bd16-0090272ccb30"
# Generate DST Addresslist
if (row[2]!='Any'):
ip,nm=cidr_to_netmask(row[2])
#print ("ip: {} netmask: {}".format(ip,nm))
# Falls Host
if (nm=="255.255.255.255"):
dst=host_dictionary[ip][0]['uid']
else:
dst=net_dictionary[ip][0]['uid']
else:
dst="97aeb369-9aea-11d5-bd16-0090272ccb30"
# Generate Services List
if (row[4]=="TCP"):
for k in tcp_dictionary:
if (tcp_dictionary[k][0]['port']==row[3]):
#print (k,tcp_dictionary[k][0]['port'])
service=tcp_dictionary[k][0]['uid']
comment=row[4]+"_"+row[3]
break
else:
service="Any"
comment=row[4]+"_"+row[3]
elif (row[4]=="UDP"):
for k in udp_dictionary:
if (udp_dictionary[k][0]['port']==row[3]):
#print (k,udp_dictionary[k][0]['port'])
service=udp_dictionary[k][0]['uid']
comment=row[4]+"_"+row[3]
break
else:
service="Any"
comment=row[4]+"_"+row[3]
#print ("ip: {} netmask: {}".format(ip,nm))
# Falls Host
elif (row[4]=="Any"):
service="Any"
comment="Any"
else:
service="Any"
comment=row[4]
PP=Policy_Package+" Network"
mydict={"name": row[0], "layer": PP , "position": position, "action": "accept","track" : {
"type" : "Log" }, "source": [src], "destination":[ dst ], "service": service, "comments" : comment }
response = cp.call_api("add-access-rule",mydict)
if response.success:
print("The rule: '{}' has been added successfully".format(mydict['name']))
#print ("name: {} src: {} dst: {} srv: {} comment: {}".format(row[0],src,dst,service,comment))
position+=1
else:
print ("Error adding rule {}".format(row[0]))
# publish changes
cp.commit()
# logout
cp.logout()