-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconverter.py
261 lines (178 loc) · 6.87 KB
/
converter.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
#!/usr/bin/env python3
# Title: DirectFire Converter
# Description: DirectFire is a firewall configuration conversion tool written in Python
# Author: Glenn Akester (@glennake)
# Version: 0.0.1
#
# DirectFire Converter is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DirectFire Converter is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# If you don't have a copy of the GNU General Public License,
# it is available here <http://www.gnu.org/licenses/>.
# Imports
try:
import argparse
except:
raise ImportError("Could not import module: argparse")
try:
from colorama import Fore, Back, Style
except:
raise ImportError("Could not import module: colorama")
from datetime import datetime
import logging
from traceback_with_variables import prints_exc, LoggerAsFile
# Import common and settings
import DirectFire.Converter.common as common
import DirectFire.Converter.settings as settings
# Get arguments
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("-c", "--config", help="/full/path/to/config", required=True)
arg_parser.add_argument(
"-s",
"--source",
choices=[
"ciscoasa",
"ciscoasa_pre83",
"fortigate",
"junipersrx",
"netscreen",
"watchguard",
],
help="source format",
required=True,
)
arg_parser.add_argument(
"-d",
"--destination",
choices=["ciscoasa", "data", "fortigate"],
help="destination format",
required=True,
)
arg_parser.add_argument(
"-r", "--routing", help="path to supplemental routing information csv file"
)
args = arg_parser.parse_args()
# Initiate logging
now = str(datetime.now().strftime("%Y%m%d_%H%M%S"))
logging.basicConfig(
filename="logs/"
+ now
+ "_"
+ str(args.source)
+ "_"
+ str(args.destination)
+ ".log",
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG,
)
logger = logging.getLogger(__name__)
logger.info("DirectFire.Converter.main: converter starting")
logger.info("DirectFire.Converter.main: source format is " + args.source)
def parse(src_format, src_config, routing_info=""):
logger.info("DirectFire.Converter.parse: loading parser module for " + src_format)
if src_format == "ciscoasa": ## Cisco ASA
from DirectFire.Converter.parsers.ciscoasa import parse
elif src_format == "ciscoasa_pre83": ## Cisco ASA pre 8.3
from DirectFire.Converter.parsers.ciscoasa_pre83 import parse
elif src_format == "fortigate": ## Fortinet FortiGate
from DirectFire.Converter.parsers.fortigate import parse
elif src_format == "junipersrx": ## Juniper SRX (JunOS)
from DirectFire.Converter.parsers.junipersrx import parse
elif src_format == "netscreen": ## Juniper Netscreen (ScreenOS)
from DirectFire.Converter.parsers.netscreen import parse
elif src_format == "watchguard": ## WatchGuard
from DirectFire.Converter.parsers.watchguard import parse
else:
logger.info(
"DirectFire.Converter.parse: failed to load parser module for " + src_format
)
print(f"{Fore.RED}Error: failed to load parser module.{Style.RESET_ALL}")
exit()
logger.info("DirectFire.Converter.parse: loaded parser module for " + src_format)
logger.info("DirectFire.Converter.parse: starting parse of source configuration")
parsed_data = parse(src_config, routing_info)
logger.info("DirectFire.Converter.parse: completed parse of source configuration")
return parsed_data
def generate(dst_format, parsed_data):
logger.info(
"DirectFire.Converter.generate: loading generator module for " + dst_format
)
if dst_format == "ciscoasa": ## Cisco ASA post 8.3
from DirectFire.Converter.generators.ciscoasa import generate
elif dst_format == "data": ## JSON Data
from DirectFire.Converter.generators.data import generate
elif dst_format == "fortigate": ## Fortinet FortiGate
from DirectFire.Converter.generators.fortigate import generate
else:
logger.info(
"DirectFire.Converter.parse: failed to load generator module for "
+ dst_format
)
print(f"{Fore.RED}Error: failed to load generator module.{Style.RESET_ALL}")
exit()
logger.info(
"DirectFire.Converter.generate: loaded generator module for " + dst_format
)
logger.info(
"DirectFire.Converter.generate: starting generation of destination output"
)
dst_config = generate(parsed_data)
logger.info(
"DirectFire.Converter.generate: completed generation of destination output"
)
return dst_config
@prints_exc(file_=LoggerAsFile(logger), fmt=settings.TBWV_FMT)
def main(src_format, dst_format, routing_info=""):
# Load source configuration file
logger.info(
"DirectFire.Converter.main: loading source configuration from " + args.config
)
try:
with open(args.config) as config_file:
src_config = config_file.read()
except:
logger.error(
"DirectFire.Converter.main: source file either not found or not readable "
+ args.config
)
print(
f"{Fore.RED}Error: source file either not found or not readable.{Style.RESET_ALL}"
)
exit()
if routing_info:
try:
with open(args.routing) as routing_file:
routing_info = routing_file.read()
except:
logger.error(
"DirectFire.Converter.main: routing file either not found or not readable "
+ args.config
)
print(
f"{Fore.RED}Error: routing file either not found or not readable.{Style.RESET_ALL}"
)
exit()
# Run configuration parser
logger.info("DirectFire.Converter.main: running configuration parser")
parsed_data = parse(
src_format=src_format, src_config=src_config, routing_info=routing_info
)
logger.info("DirectFire.Converter.main: configuration parser finished")
# Output
logger.info("DirectFire.Converter.main: running configuration generator")
dst_config = generate(dst_format=dst_format, parsed_data=parsed_data)
for line in dst_config:
print(line)
### add support for output to file
logger.info("DirectFire.Converter.main: configuration generator finished")
logger.info("DirectFire.Converter.main: converter exiting")
if __name__ == "__main__":
main(src_format=args.source, dst_format=args.destination, routing_info=args.routing)