-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter-reportlogger.py
executable file
·79 lines (68 loc) · 2.25 KB
/
filter-reportlogger.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
#!/usr/bin/env python3
#
# OpenSMTPD filter (smtpd-filters(7)) to log reporting information.
import logging
import sys
import tempfile
from argparse import ArgumentParser
logging.basicConfig(
level=logging.DEBUG,
format="%(levelname)s: %(message)s",
)
REGISTER_LINES = [
"register|report|smtp-in|link-connect",
"register|report|smtp-in|link-identify",
"register|report|smtp-in|link-disconnect",
"register|report|smtp-in|link-auth",
"register|report|smtp-in|timeout",
]
REGISTER_READY_LINE = "register|ready"
def process_stream_to_output(fp):
"""Process input event stream and write to specified output file
fp: writable output file object
"""
logging.info("writing events from stdin to %s", fp.name)
for in_line in sys.stdin:
fp.write(in_line.encode())
in_line = in_line.strip()
logging.info("received line >>%s<<", in_line)
if in_line == "config|ready":
logging.info(
"configuration received from smtpd (ready to process events)"
)
for out_line in REGISTER_LINES:
sys.stdout.write(f"{out_line}\n")
logging.debug("wrote the %s line to smtpd on stdout", out_line)
sys.stdout.write(f"{REGISTER_READY_LINE}")
logging.debug(
"wrote the %s line to smtpd on stdout", REGISTER_READY_LINE
)
sys.stdout.flush()
logging.debug("flushed stdout")
def main():
parser = ArgumentParser(description="log events from smtpd(8)")
parser.add_argument(
"-o",
"--outfile",
help="output file to which to write received events",
)
parser.add_argument(
"-t",
"--temp-outfile",
action="store_true",
help="write received events to an autogenerated temporary file",
)
args = parser.parse_args()
if args.temp_outfile:
with tempfile.NamedTemporaryFile(
delete=False, buffering=0, prefix="filter-bruteforce-"
) as fp:
process_stream_to_output(fp)
elif args.outfile:
with open(args.outfile, "ab", buffering=0) as fp:
process_stream_to_output(fp)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass