Created
August 7, 2019 22:32
-
-
Save pocc/08f43a0bf7b50a7f4c9dff93de60bc0e to your computer and use it in GitHub Desktop.
Check the validity of your Wireshark config files after editing them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Check the validity of your Wireshark config files after editing them. | |
Part of https://tshark.dev/packetcraft/config_files. Ross Jacobs, 2019-08-07. | |
""" | |
import re | |
import os | |
def gen_regexes(): | |
"""Generate the regex dictionary.""" | |
ws = r'\s+' # whitespace | |
name = r'(?:[^\s]*)' | |
# Comment is always optional | |
comment = r'(?: *# ?.*)?' | |
yaml_key = r'(?:[\s\S]+?)(?=>\n\S)' | |
# Capture/Display Filter | |
ws_filter = r'(?:[^@\n]+?)' | |
# Colorfilter color consisting of 3 comma-separated values, 0-65535 | |
color = r'\[(?:\d{1,5}),(?:\d{1,5}),(?:\d{1,5})]' | |
mac_addr = r'(?:[a-fA-F0-9]{2}[.:-]){5}[a-fA-F0-9]{2}' | |
oui = r'(?:[a-fA-F0-9]{2}[.:-]){2}[a-fA-F0-9]{2}' | |
ip_addr = r'(?:[\d\.]*|[\d:]*)' | |
cidr = r'\/\d{1,3}' | |
domain = r'[0-9A-Za-z.]+' | |
service = r'\d+\/\S+' | |
ipx = r'(?:[0-9A-Fa-f]{2}[:.-]){3}[a-fA-F0-9]{2}|[0-9A-Fa-f]{4}' | |
vlan = r'(?:\d{1,4})' | |
ss7 = r'(?:[0-9-]+)' | |
# Don't start regex with newlines as Python's readline drops it. | |
line_regexes = { | |
"preferences": name + ': ?' + yaml_key, | |
"recent": name + ws + ws_filter + comment, | |
"cfilters": '"' + name + '"' + ws + ws_filter + comment, | |
"dfilters": '"' + name + '"' + ws + ws_filter + comment, | |
"colorfilters": '@' + name + '@' + ws_filter + '@' + color + color, | |
"disabled_protos": name, | |
"ethers": mac_addr + ws + name + comment, | |
"manuf": oui + ws + name + comment, | |
"hosts": ip_addr + ws + domain + comment, | |
"services": domain + ws + service + comment, | |
"subnets": ip_addr + cidr + ws + name + comment, | |
"ipxnets": ipx + ws + name + comment, | |
"vlans": vlan + ws + name + comment, | |
"ss7pcs": ss7 + ws + name + comment | |
} | |
return line_regexes | |
def readfile(filename, f, line_regexes): | |
"""Read the file and output data based on it.""" | |
message = "" | |
line_comment = r'(?:^|\n)#.*' | |
line = f.readline() | |
invalid_syntax = False | |
count = 1 | |
while line: | |
regex = '(' + line_comment + '|' + line_regexes[filename] + '|\n)' | |
if not re.findall(regex, line): | |
message += filename + ": Invalid syntax found on line: `" \ | |
+ line + "` \nAt line num " + count + \ | |
" using regex `" + regex + "`" | |
invalid_syntax = True | |
count += 1 | |
line = f.readline() | |
if not invalid_syntax: | |
message += filename + ": ✓" | |
return message | |
def main(): | |
"""Main func.""" | |
line_regexes = gen_regexes() | |
files_not_found = [] | |
found_configs = [] | |
dir_files = os.listdir(".") | |
if os.path.basename(os.getcwd()).lower() != "wireshark": | |
print("Dir is not `wireshark`.\n" | |
"Are you sure this is the wireshark config folder?") | |
for filename in line_regexes.keys(): | |
if os.path.exists(filename): | |
with open(filename) as f: | |
file_message = readfile(filename, f, line_regexes) | |
found_configs.append(file_message) | |
else: | |
files_not_found.append(filename) | |
if filename in dir_files: | |
dir_files.remove(filename) | |
print("Found files:\n\t" + "\n\t".join(found_configs)) | |
print("\nExtra config files:\n\t" + "\n\t".join(dir_files)) | |
print("\nConfig files not found:\n\t" + "\n\t".join(files_not_found)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment