-
Notifications
You must be signed in to change notification settings - Fork 12
/
apc-convert.py
406 lines (312 loc) · 11.3 KB
/
apc-convert.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import json
import sys
import argparse
import os.path
from Crypto.PublicKey import RSA
CERT_START = "-----BEGIN CERTIFICATE-----"
CERT_END = "-----END CERTIFICATE-----"
RSA_START = "-----BEGIN RSA PRIVATE KEY-----"
RSA_END = "-----END RSA PRIVATE KEY-----"
# Python 2.x Workaround
if hasattr(__builtins__, 'raw_input'):
input=raw_input
class OVPNFile(object):
def __init__(self):
self.content = {"client": [], "dev": ["tun"], "resolv-retry": ["infinite"], "auth": ["SHA256"]}
self.login = None
@property
def cert(self):
cert = self.content.get("cert", [""])[0]
if "\n" in cert:
return cert
if os.path.exists(cert):
cert_file = open(cert, "r")
head_found = False
for line in cert_file: #Seek Cert file until relevant content
if line.find(CERT_START) >= 0:
cert = line[line.find(CERT_START):]
head_found = True
elif head_found:
cert += line
cert_file.close()
cert = cert.strip()
return cert
@cert.setter
def cert(self, value):
value = value.replace("\n", "").strip()
if not value.startswith(CERT_START): raise ValueError()
self.content['cert'] = [value]
@property
def key(self):
cert = self.content.get("key", [""])[0]
if "\n" in cert:
return cert
if os.path.exists(cert):
return open(cert, "r").read()
return cert
@key.setter
def key(self, value):
value = value.replace("\n", "").strip()
if not value.startswith(RSA_START): raise ValueError()
self.content['key'] = [value]
@property
def ca(self):
cert = self.content.get("ca", [""])[0]
if "\n" in cert:
return cert
if os.path.exists(cert):
return open(cert, "r").read()
return cert
@ca.setter
def ca(self, value):
value = value.replace("\n", "").strip()
if not value.startswith(CERT_START): raise ValueError()
self.content['ca'] = [value]
@property
def proto(self):
return self.content.get("proto", [""])[0]
@proto.setter
def proto(self, value):
if value.lower() not in ("tcp", "udp", "tcp6", "udp6"): raise ValueError()
self.content["proto"] = [value.lower()]
@property
def remote(self): return self.content.get("remote", [""])
@remote.setter
def remote(self, value): self.content["remote"] = value
@property
def remote_cert_tls(self): return self.content.get("remote-cert-tls", [""])[0]
@remote_cert_tls.setter
def remote_cert_tls(self, value): self.content["remote-cert-tls"] = [value]
@property
def cipher(self): return self.content.get("cipher", [""])[0]
@cipher.setter
def cipher(self, value): self.content["cipher"] = [value]
@property
def auth(self): return self.content.get("auth", [""])[0]
auth.setter
def auth(self, value): self.content["auth"] = [value]
@property
def comp_lzo(self):
return "comp-lzo" in self.content
@comp_lzo.setter
def comp_lzo(self, value):
if value:
self.content["comp-lzo"] = []
def getLogin(self):
try:
self.content["auth-user-pass"].remove("")
userPass = open(self.content["auth-user-pass"][0])
return userPass.readline().strip(), userPass.readline().strip()
except:
return None, None
def addLogin(self, username, password):
self.content["auth-user-pass"] = ["login.txt"]
self.login = username + "\n" + password
def _parseLine(self, line):
line = line.strip()
if len(line)==0 or line[0] in ("#", ";"): return
line = line.split(" ")
key = line[0]
val = " ".join(line[1:])
values = self.content.get(key, [])
values.append(val)
self.content[key] = list(set(values))
def load(self, filestream):
self.__init__()
try:
for line in filestream:
self._parseLine(line)
except StopIteration:
pass
def save(self, filestream):
txtCont = ""
for key in self.content:
val = self.content[key]
if len(val) == 0: val=[""]
for v in val:
if isinstance(v, list):
v = " ".join(v)
if "\n" in v or len(v)>300:
txtCont += "\n<"+ key + ">\n" + v + "\n</" + key + ">\n\n"
else:
txtCont += key + " " + v + "\n"
filestream.write(txtCont)
if "auth-user-pass" in self.content:
open(self.content["auth-user-pass"][0], "w").write(self.login)
class APCFile(object):
def __init__(self):
self.content = {
"key":"",
"protocol":"tcp",
"server_port":"",
"certificate":"",
"ca_cert":"",
"server_dn":"",
"server_address":[""],
"authentication_algorithm":"SHA256",
"username":"",
"encryption_algorithm":"BF-CBC",
"compression":"",
"password":""
}
@property
def cert(self):
return self.content.get("certificate")
@cert.setter
def cert(self, value):
value = value.replace("\n", "").strip()
if not value.startswith(CERT_START): raise ValueError()
if not value.startswith(CERT_START + "\n"):
value = value.replace(CERT_START, CERT_START + "\n")
if not value.endswith("\n" + CERT_END):
value = value.replace(CERT_END, "\n" + CERT_END)
self.content['certificate'] = value
@property
def key(self):
return self.content.get("key")
@key.setter
def key(self, value):
value = value.replace("\n", "").strip()
if not value.startswith(RSA_START): raise ValueError()
if not value.startswith(RSA_START + "\n"):
value = value.replace(RSA_START, RSA_START + "\n")
if not value.endswith("\n" + RSA_END):
value = value.replace(RSA_END, "\n" + RSA_END)
self.content['key'] = value
@property
def ca(self):
return self.content.get("ca_cert")
@ca.setter
def ca(self, value):
value = value.replace("\n", "").strip()
if not value.startswith(CERT_START): raise ValueError()
if not value.startswith(CERT_START + "\n"):
value = value.replace(CERT_START, CERT_START + "\n")
if not value.endswith("\n" + CERT_END):
value = value.replace(CERT_END, "\n" + CERT_END)
self.content['ca_cert'] = value
@property
def proto(self):
return self.content.get("protocol")
@proto.setter
def proto(self, value):
if value.lower() not in ("tcp", "udp", "tcp6", "udp6"): raise ValueError()
self.content["protocol"] = value.lower()
@property
def username(self): return self.content.get("username")
@username.setter
def username(self, value): self.content["username"] = value
@property
def password(self): return self.content.get("password")
@password.setter
def password(self, value): self.content["password"] = value
@property
def remote_cert_tls(self): return self.content.get("server_dn")
@remote_cert_tls.setter
def remote_cert_tls(self, value): self.content["server_dn"] = value
@property
def cipher(self): return self.content.get("encryption_algorithm")
@cipher.setter
def cipher(self, value): self.content["encryption_algorithm"] = value
@property
def auth(self): return self.content.get("authentication_algorithm")
auth.setter
def auth(self, value): self.content["authentication_algorithm"] = value
@property
def remote(self):
outp = []
for r in self.content.get("server_address"):
outp.append(r + " " + self.content.get("server_port"))
return outp
@remote.setter
def remote(self, value):
try:
self.content["server_address"] = []
for v in value:
server, port = v.split(" ")
if self.content.get("server_port") == "":
self.content["server_port"] = port
if self.content["server_port"] != port:
raise ValueError("Different ports per remote isn't supported with apc")
self.content["server_address"].append(server)
except:
raise ValueError()
@property
def comp_lzo(self):
return self.content.get("compression") == ""
@comp_lzo.setter
def comp_lzo(self, value):
try:
self.content["compression"] = str(int(value))
except:
raise ValueError()
def save(self, filestream):
jcontent = json.dumps(self.content, separators=(',',':')).replace("\n", "")
filestream.write(jcontent)
def load(self, filestream):
jcontent = ""
try:
for line in filestream:
jcontent+=line
except StopIteration:
pass
self.content = json.loads(jcontent)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='converts ovpn and cert files to sophos apc and reverse')
parser.add_argument('action', metavar='action', type=str, help='ovpn2apc or apc2ovpn')
parser.add_argument('src', metavar='src', type=argparse.FileType('r'), help='input apc/ovpn file "-" for stdin')
parser.add_argument('dst', metavar='dst', type=argparse.FileType('w'), help='output apc/ovpn file "-" for stout')
parser.add_argument('--username', metavar='user', type=str, nargs='?', help='vpn username, only if action is ovpn2apc')
parser.add_argument('--password', metavar='pwd', type=str, nargs='?', help='vpn password, only if action is ovpn2apc')
args = parser.parse_args()
apc = APCFile()
ovpn = OVPNFile()
if args.action == "ovpn2apc":
ovpn.load(args.src)
username = args.username
password = args.password
if username is None and password is None:
username, password = ovpn.getLogin()
if username is None:
username = input("Please enter VPN User: ")
if password is None:
import getpass
password = getpass.getpass("Please enter VPN Password: ")
apc.ca = ovpn.ca
apc.username = username
apc.password = password
apc.cipher = ovpn.cipher
apc.auth = ovpn.auth
apc.comp_lzo = ovpn.comp_lzo
apc.proto = ovpn.proto
apc.remote = ovpn.remote
apc.remote_cert_tls = ovpn.remote_cert_tls
try:
apc.cert = ovpn.cert
except ValueError:
# Convert key to RSA
key = RSA.importKey(ovpn.cert)
apc.cert = key.exportKey().decode()
try:
apc.key = ovpn.key
except ValueError:
# Convert key to RSA
key = RSA.importKey(ovpn.key)
apc.key = key.exportKey().decode()
apc.save(args.dst)
elif args.action == "apc2ovpn":
apc.load(args.src)
ovpn.cert = apc.cert
ovpn.ca = apc.ca
ovpn.key = apc.key
ovpn.cipher = apc.cipher
ovpn.auth = apc.auth
ovpn.comp_lzo = apc.comp_lzo
ovpn.proto = apc.proto
ovpn.remote = apc.remote
ovpn.remote_cert_tls = apc.remote_cert_tls
ovpn.addLogin(apc.username, apc.password)
ovpn.save(args.dst)
else:
print("unknown action '"+args.action+"'\n")
parser.print_help()