我想做一个syslog解析器来转换我的syslog,它有一个key=value格式的JSON信息,输出文件是一个.txt文件,我可以导入到FortiSIEM中,这对于兼容的syslog来说是非常挑剔的,而且我无法开始解析"原始的"syslog,因此我的 idea 是在日志(log)到达SIEM之前简化它.
我用PyParsing做了一些测试,但我真的不知道如何使用它,我的输出文件正在创建中,但结果是空白的
我想我不能分享系统日志(log),所以这里有一个非常粗略的例子,说明系统日志(log)是什么样子的:
<;140>;1 2022-05-02T08:31:22.478Z platform数据导出-syslog_variation-{"key"=值,信息:{"key"=value,"key"=value},info2:{"key"=value,"key"=value},"key"=value}
我想出的 playbook 是:
from pyparsing import Word, Suppress, alphanums, CharsNotIn, ZeroOrMore, Dict
# Define header
priority = Suppress("<") + Word(alphanums) + Suppress(">")
version = Word(alphanums) + Suppress(" ")
timestamp = CharsNotIn(" ") + Suppress(" ")
hostname = CharsNotIn(" ") + Suppress(" ")
appname = CharsNotIn(" ") + Suppress(" ")
procid = CharsNotIn(" ") + Suppress(" ")
msgid = CharsNotIn("\n")
header = priority + version + timestamp + hostname + appname + procid + msgid
# Define key-value pairs
key = Word(alphanums + "_")
value = CharsNotIn("\n")
pair = key + Suppress("=") + value
kv_pairs = Dict(pair + ZeroOrMore(Suppress(",") + pair))
# Define message format
message = header + Suppress(" ") + kv_pairs
# Open input and output files
with open("syslog.txt") as input_file, open("syslog_output.txt", "w") as output_file:
for line in input_file:
try:
# Convert to key-value format
parsed_message = message.parseString(line.strip())
kv_message = " ".join([f"{key}={value}" for key, value in parsed_message.items()])
# Write the message to the output file
output_file.write(parsed_message + "\n")
except Exception as e:
print(f"Failed to parse line: {line} with error: {e}")
continue
当我运行脚本并打印header
和message
输出时,我得到两个异常:
Failed to parse line: "Whole Syslog Text"
with error: Expected ' ', found '2022' (at char 7), (line:1, col:8)
Failed to parse line:
with error: Expected '<' (at char 0), (line:1, col:1)
Header: {Suppress:('<') W:(0-9A-Za-z) Suppress:('>') W:(0-9A-Za-z) Suppress:(' ') !W:( ) Suppress:(' ') !W:( ) Suppress:(' ') !W:( ) Suppress:(' ') !W:( ) Suppress:(' ') !W:(
)}
Message: {Suppress:('<') W:(0-9A-Za-z) Suppress:('>') W:(0-9A-Za-z) Suppress:(' ') !W:( ) Suppress:(' ') !W:( ) Suppress:(' ') !W:( ) Suppress:(' ') !W:( ) Suppress:(' ') !W:(
) Suppress:(' ') Dict:({W:(0-9A-Z_a-z) Suppress:('=') !W:(
) [{Suppress:(',') W:(0-9A-Z_a-z) Suppress:('=') !W:(
)}]...})}
我希望我的输出文件如下所示:
<140>1 2022-05-02T08:31:22.478Z platform dataexport - syslog_variation -
key=value
key=value
key=value
...
我需要有标头来识别FortiSIEM上的日志(log)类型.