-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathcommon.py
418 lines (318 loc) · 13.1 KB
/
common.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
407
408
409
410
411
412
413
414
415
416
417
418
# MIT License
# Copyright (c) 2017 Balazs Bucsay
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
if "common.py" in sys.argv[0]:
print("[-] Instead of poking around just try: python xfltreat.py --help")
sys.exit(-1)
import struct
import random
import socket
import os
import re
import platform
import pkgutil
DATA_CHANNEL_BYTE = "\x00"
CONTROL_CHANNEL_BYTE = "\x80"
CONTROL_CHECK = "XFLT>CHECK!"
CONTROL_CHECK_RESULT = "XFLT>CHECK_RESULT"
CONTROL_INIT = "XFLT>INIT!"
CONTROL_INIT_DONE = "XFLT>INIT_DONE"
CONTROL_LOGOFF = "XFLT>LOGOFF!"
CONTROL_DUMMY_PACKET = "XFLT>DUMMY_PACKET"
# Multi Operating System support values
OS_UNKNOWN = 0
OS_LINUX = 1
OS_MACOSX = 2
OS_WINDOWS = 4
OS_FREEBSD = 8
OS_OPENBSD = 16
OS_WHATNOT = 32
OS_SUPPORTED = OS_LINUX | OS_MACOSX | OS_WINDOWS | OS_FREEBSD
# print severity levels:
# 0 always
# 1 verbose (common.VERBOSE)
# 2 debug (common.DEBUG)
# print feedback levels:
# -1 negativ
# 0 neutral
# 1 positive
colour = True
VERBOSE = 1
DEBUG = 2
def internal_print(message, feedback = 0, verbosity = 0, severity = 0):
debug = ""
if severity == 2:
debug = "DEBUG: "
if verbosity >= severity:
if feedback == -1:
if colour:
prefix = "\033[91m[-]"
else:
prefix = "[-]"
if feedback == 0:
if colour:
prefix = "\033[39m[*]"
else:
prefix = "[*]"
if feedback == 1:
if colour:
prefix = "\033[92m[+]"
else:
prefix = "[+]"
if colour:
print("{0} {1}{2}\033[39m".format(prefix, debug, message))
else:
print("{0} {1}{2}".format(prefix, debug, message))
# check if the OS is supported
def os_support():
if OS_SUPPORTED & get_os_type():
return True
else:
internal_print("Sorry buddy, I am afraid that your OS is not yet supported", -1)
return False
# check if the requirements are met
def check_modules_installed():
reqs = []
os_type = get_os_type()
# reqs = [["module_name", "package_name"], [...]]
if os_type == OS_LINUX:
reqs = [["cryptography", "cryptography"], ["sctp","pysctp"]]
if os_type == OS_MACOSX:
reqs = [["cryptography", "cryptography"]]
if os_type == OS_WINDOWS:
reqs = [["cryptography", "cryptography"], ["win32file","pywin32"]]
if os_type == OS_FREEBSD:
reqs = [["cryptography", "cryptography"]]
allinstalled = True
for m in reqs:
if not pkgutil.find_loader(m[0]):
allinstalled = False
internal_print("The following python modules were not installed: {0}".format(m[1]), -1)
return allinstalled
# get os type. No need to import 'platform' in every module this way.
def get_os_type():
os_type = platform.system()
if os_type == "Linux":
return OS_LINUX
if os_type == "Darwin":
return OS_MACOSX
if os_type == "Windows":
return OS_WINDOWS
if os_type == "FreeBSD":
return OS_FREEBSD
if os_type == "OpenBSD":
return OS_OPENBSD
return OS_UNKNOWN
# get the release of the OS
def get_os_version():
return platform.version()
# get the release of the OS
def get_os_release():
return platform.release()
# get the privilege level, True if it is enough to run.
def get_privilege_level():
os_type = get_os_type()
if (os_type == OS_LINUX) or (os_type == OS_MACOSX) or (os_type == OS_FREEBSD):
if os.getuid() == 0:
return True
else:
return False
if os_type == OS_WINDOWS:
import ctypes
if ctypes.windll.shell32.IsUserAnAdmin():
return True
else:
return False
return False
# check if the forwarding was set properly.
def check_router_settings(config):
os_type = get_os_type()
if os_type == OS_LINUX:
if open('/proc/sys/net/ipv4/ip_forward','r').read()[0:1] == "0":
internal_print("The IP forwarding is not set.", -1)
internal_print("Please use the following two commands to set it properly (root needed):\n#\tsysctl -w net.ipv4.ip_forward=1\n#\tiptables -t nat -A POSTROUTING -s {0}/{1} -o [YOUR_INTERFACE/e.g./eth0] -j MASQUERADE\n".format(config.get("Global", "serverip"), config.get("Global", "servernetmask")))
return False
if os_type == OS_MACOSX:
import ctypes
import ctypes.util
# load libc
libc_name = ctypes.util.find_library('c')
libc = ctypes.CDLL(libc_name, use_errno=True)
# get value of forwarding with sysctl
fw_value = ctypes.c_int(-1)
err = libc.sysctlbyname("net.inet.ip.forwarding", ctypes.byref(fw_value), ctypes.byref(ctypes.c_uint(4)), None, 0)
if err < 0:
err = ctypes.get_errno()
internal_print("sysctl failed: {0} : {1}".format(err, os.strerror(err)), -1)
return False
if fw_value.value != 1:
internal_print("The IP forwarding is not set.", -1)
internal_print("Please use the following commands to set it properly (root needed):\n#\tsysctl -w net.inet.ip.forwarding=1\nPut the following line into the /etc/pf.conf after the \'nat-anchor \"com.apple/*\"' line:\n#\tnat on en0 from {0}/{1} to any -> (en0)\nThen load the config file with pfctl:\n#\tpfctl -f /etc/pf.conf -e -v".format(config.get("Global", "serverip"), config.get("Global", "servernetmask")))
return False
if os_type == OS_WINDOWS:
import _winreg as registry
#HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\IPEnableRouter - DWORD 1 - enable
#"Routing and Remote Access" service - Enable, start
PATH = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\"
try:
regkey = registry.OpenKey(registry.HKEY_LOCAL_MACHINE, PATH)
router_value = registry.QueryValueEx(regkey, "IPEnableRouter")[0]
except WindowsError as e:
internal_print("Cannot get IPEnableRouter value. Registry key cannot be found: {0}".format(e), -1)
return False
if router_value != 1:
internal_print("The IP forwarding is not set.", -1)
internal_print("Please set the IPEnableRouter value to 1 with the following command:\n\treg add HKLM\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters /t REG_DWORD /v IPEnableRouter /d 1 /f\n\tMoreover you need to enable and start the \"Routing and Remote Access\" service.\n")
return False
if os_type == OS_FREEBSD:
try:
f = open('/etc/rc.conf','r')
content = f.read()
f.close()
found = False
for line in content.split("\n"):
if line.find("gateway_enable") != -1:
if line.lower().find("yes") != -1:
found = True
break
if not found:
internal_print("The IP forwarding is not set.", -1)
internal_print("Please set the gateway_enable directive to yes in rc.conf.", -1)
return False
except IOError:
internal_print("Could not open rc.conf", -1)
return False
return True
# main config sanity check. If something missing from the Global section, or
# from the filesystem then it shouts.
def config_sanity_check(config, serverorclient):
dirlist = ["modules", "support", "encryption", "authentication", "misc"]
for d in dirlist:
if not os.path.isdir(d):
internal_print("The '{0}' directory is missing. Make sure you have it with the necessary files.".format(d), -1)
return False
if not config.has_section("Global"):
internal_print("config file missing 'Global' section", -1)
return False
if not config.has_option("Global", "remoteserverip"):
internal_print("'remoteserverip' option is missing from 'Global' section", -1)
return False
if not is_ipv4(config.get("Global", "remoteserverip")) and not is_ipv6(config.get("Global", "remoteserverip")) :
internal_print("'remoteserverip' should be ipv4 or ipv6 address in 'Global' section", -1)
return False
if not config.has_option("Global", "mtu"):
internal_print("'mtu' option is missing from 'Global' section", -1)
return False
if serverorclient:
if not config.has_option("Global", "serverif"):
internal_print("'serverif' option is missing from 'Global' section", -1)
return False
if not config.has_option("Global", "serverip"):
internal_print("'serverip' option is missing from 'Global' section", -1)
return False
if not config.has_option("Global", "servernetmask"):
internal_print("'servernetmask' option is missing from 'Global' section", -1)
return False
if not config.has_option("Global", "serverbind"):
internal_print("'serverbind' option is missing from 'Global' section", -1)
return False
else:
if not config.has_option("Global", "clientif"):
internal_print("'clientif' option is missing from 'Global' section", -1)
return False
if not config.has_option("Global", "clientip"):
internal_print("'clientip' option is missing from 'Global' section", -1)
return False
if not config.has_option("Global", "clientnetmask"):
internal_print("'clientnetmask' option is missing from 'Global' section", -1)
return False
if config.has_option("Global", "scope"):
scopefile = config.get("Global", "scope")
if not os.path.isfile(scopefile) and scopefile:
internal_print("File '{0}' does not exists. Delete 'scope' option from config or create scope file.".format(scopefile), -1)
return False
return True
# dummy function to check whether it is control channel or not.
def is_control_channel(control_character):
if control_character == None:
return False
if (ord(control_character) & 0x80) == ord(CONTROL_CHANNEL_BYTE):
return True
else:
return False
# Shamelessly stolen regular expressions, any of them could be wrong
def is_hostname(s):
return bool(re.match("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$", s))
def is_ipv4(s):
return bool(re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", s))
def is_ipv6(s):
too_long = "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"
return bool(re.match(too_long, s))
def check_line_type(line):
# check if it looks valid
if not bool(re.match("^([0-9\.\-/]*)$", line)):
return 0
# ip/mask format
if bool(re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(/[0-9]{1,2})$", line)):
return 1
# x.y.z.w-v format
if bool(re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\-[0-9]{1,3})$", line)):
return 2
# ip format
if bool(re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", line)):
return 3
return -1
def parse_scope_file(filename):
if not filename:
return []
f = open(filename, "r")
content = f.read()
f.close()
lines = content.split("\n")
scope = []
for line in lines:
if line:
if line[0:1] == "#":
# skip comments
continue
# get type of addressing, parse it accordingly
type_ = check_line_type(line)
if (type_ == 0) or (type_ == -1):
internal_print("Erroneous line in scope definition: {0}".format(line), -1)
if type_ == 1:
regexp = re.match("^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/([0-9]{1,3})$", line)
range_ip = struct.unpack(">I", socket.inet_aton(regexp.group(1)))[0]
network = socket.inet_ntoa(struct.pack(">I", range_ip & ((2**int(regexp.group(2)))-1)<<32-int(regexp.group(2))))
add = (network, socket.inet_ntoa(struct.pack(">I", ((2**int(regexp.group(2)))-1)<<32-int(regexp.group(2)))), regexp.group(2))
if add not in scope:
scope.append(add)
if type_ == 2:
regexp = re.match("^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\.([0-9]{1,3})\-([0-9]{1,3})$", line)
for i in xrange(int(regexp.group(2)), int(regexp.group(3))+1):
add = ("{0}.{1}".format(regexp.group(1), i), "255.255.255.255", "32")
if add not in scope:
scope.append(add)
if type_ == 3:
add = (line, "255.255.255.255", "32")
if add not in scope:
scope.append(add)
return scope
# ANSI escape codes are only supported from version 10
if get_os_type() == OS_WINDOWS:
if int(get_os_version()[0:get_os_version().find(".")]) < 10:
colour = False