-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move python code from conf to files
Improvement for dev experience move python to .py files outside of .conf
- Loading branch information
Ryan Faircloth
committed
Mar 15, 2022
1 parent
088e9cf
commit d75ec38
Showing
8 changed files
with
183 additions
and
122 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
simple syslog-ng Python parser example | ||
resolves IP to hostname | ||
value pair names are hard-coded | ||
""" | ||
import re | ||
import socket | ||
|
||
|
||
class FixHostResolver(object): | ||
def parse(self, log_message): | ||
""" | ||
Resolves IP to hostname | ||
""" | ||
|
||
# try to resolve the IP address | ||
try: | ||
ipaddr = log_message["SOURCEIP"].decode("utf-8") | ||
|
||
hostname, aliaslist, ipaddrlist = socket.gethostbyaddr(ipaddr) | ||
# print(ipaddr) | ||
# print(hostname) | ||
parts = str(hostname).split(".") | ||
name = parts[0] | ||
# print(name) | ||
if len(parts) > 1: | ||
log_message["HOST"] = name | ||
except: | ||
pass | ||
|
||
# return True, other way message is dropped | ||
return True |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import re | ||
import binascii | ||
|
||
|
||
class leef_kv(object): | ||
def init(self, options): | ||
self.regex = r"( ?(?:[A-Z]{2,4}T|HAEC|IDLW|MSK|NT|UTC|THA))" | ||
return True | ||
|
||
def parse(self, log_message): | ||
|
||
try: | ||
msg = log_message["MESSAGE"].decode("utf-8") | ||
# All LEEF message are | separated super structures | ||
structure = msg.split("|") | ||
# Indexed fields for Splunk | ||
|
||
log_message["fields.leef_version"] = structure[0][5:] | ||
log_message["fields.leef_vendor"] = structure[1] | ||
log_message["fields.leef_product"] = structure[2] | ||
log_message["fields.leef_product_version"] = structure[3] | ||
log_message["fields.leef_EventID"] = structure[4] | ||
# We just want the event field | ||
event = structure[len(structure) - 1] | ||
log_message[".leef.event"] = event | ||
# V1 will always use tab | ||
if structure[0][5:].startswith("1"): | ||
separator = "\t" | ||
lv = "1" | ||
pairs = event.split(separator) | ||
if len(pairs) < 4: | ||
separator = "|" | ||
pairs = structure[5:] | ||
event = "\t".join(pairs) | ||
log_message[".leef.event"] = event | ||
else: | ||
lv = "2" | ||
# V2 messages should always provide the sep but some fail do comply | ||
# with the format spec if they don't assume tab | ||
if len(structure) == 6 or not structure[5]: | ||
separator = "\t" | ||
pairs = event.split(separator) | ||
else: | ||
separator = structure[5] | ||
if separator.startswith("0"): | ||
separator = separator[1:] | ||
pairs = event.split(separator) | ||
|
||
if separator.startswith("x"): | ||
hex_sep = f"0{separator.lower()}" | ||
else: | ||
hex_sep = f'0x{binascii.b2a_hex(separator.encode("utf-8")).decode("utf-8").lower()}' | ||
if structure[0][5:].startswith("1"): | ||
log_message[".splunk.sourcetype"] = f"LEEF:{lv}" | ||
else: | ||
log_message[".splunk.sourcetype"] = f"LEEF:{lv}:{hex_sep}" | ||
log_message[".splunk.source"] = f"{structure[1]}:{structure[2]}" | ||
log_message["fields.sc4s_vendor_product"] = f"{structure[1]}_{structure[2]}" | ||
|
||
for p in pairs: | ||
f, v = p.split("=", 1) | ||
if f == "devTime": | ||
log_message[".leef." + f] = re.sub( | ||
self.regex, "", v, 0, re.MULTILINE | ||
) | ||
else: | ||
log_message[".leef." + f] = v | ||
except Exception as e: | ||
log_message["fields.leef_exception"] = str(e) | ||
pass | ||
|
||
# return True, other way message is dropped | ||
return True |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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