forked from cisagov/Malcolm
-
Notifications
You must be signed in to change notification settings - Fork 59
/
watch-pcap-uploads-folder.py
executable file
·274 lines (245 loc) · 8.74 KB
/
watch-pcap-uploads-folder.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2023 Battelle Energy Alliance, LLC. All rights reserved.
###################################################################################################
# Monitor a directory for PCAP files for processing (by publishing their filenames to a ZMQ socket)
#
# Run the script with --help for options
###################################################################################################
import argparse
import glob
import logging
import magic
import os
import pathlib
import re
import shutil
import signal
import sys
import time
import malcolm_utils
from malcolm_utils import eprint, str2bool, remove_suffix
import watch_common
###################################################################################################
scriptName = os.path.basename(__file__)
scriptPath = os.path.dirname(os.path.realpath(__file__))
origPath = os.getcwd()
shuttingDown = [False]
###################################################################################################
# handle sigint/sigterm and set a global shutdown variable
def shutdown_handler(signum, frame):
global shuttingDown
shuttingDown[0] = True
###################################################################################################
def file_processor(pathname, **kwargs):
uid = kwargs["uid"]
gid = kwargs["gid"]
pcapDir = kwargs["destination"]
zeekDir = kwargs["zeek"]
logger = kwargs["logger"] if "logger" in kwargs and kwargs["logger"] else logging
logger.info(f"{scriptName}:\t👓\t{pathname}")
if os.path.isfile(pathname):
time.sleep(0.1)
try:
os.chown(pathname, uid, gid)
# get the file magic mime type
fileMime = magic.from_file(pathname, mime=True)
fileType = magic.from_file(pathname)
if os.path.isdir(pcapDir) and (
(fileMime in ('application/vnd.tcpdump.pcap', 'application/x-pcapng'))
or re.search(r'pcap-?ng', fileType, re.IGNORECASE)
):
# a pcap file to be processed by dropping it into pcapDir
logger.info(f"{scriptName}:\t🖅\t{pathname} [{fileMime}][{fileType}] to {pcapDir}")
shutil.move(pathname, os.path.join(pcapDir, os.path.basename(pathname)))
elif os.path.isdir(zeekDir) and (
fileMime
in [
'application/gzip',
'application/x-gzip',
'application/x-7z-compressed',
'application/x-bzip2',
'application/x-cpio',
'application/x-lzip',
'application/x-lzma',
'application/x-rar-compressed',
'application/x-tar',
'application/x-xz',
'application/zip',
]
):
# looks like this is a compressed file, we're assuming it's a zeek log archive to be processed by filebeat
logger.info(f"{scriptName}:\t🖅\t{pathname} [{fileMime}][{fileType}] to {zeekDir}")
shutil.move(pathname, os.path.join(zeekDir, os.path.basename(pathname)))
else:
# unhandled file type uploaded, delete it
logger.warning(f"{scriptName}:\t🗑\t{pathname} ({fileMime}/{fileType})")
os.unlink(pathname)
except Exception as genericError:
logger.error(f"{scriptName}:\texception: {genericError}")
###################################################################################################
# main
def main():
global shuttingDown
parser = argparse.ArgumentParser(
description=scriptName,
add_help=False,
usage='{} <arguments>'.format(scriptName),
)
parser.add_argument('--verbose', '-v', action='count', default=1, help='Increase verbosity (e.g., -v, -vv, etc.)')
parser.add_argument(
'-r',
'--recursive-directory',
dest='recursiveDir',
help="If specified, monitor all directories with this name underneath --directory",
metavar='<name>',
type=str,
required=False,
)
parser.add_argument(
'--recursive',
dest='recursiveAll',
help="Monitor all directories underneath --directory",
metavar='true|false',
type=str2bool,
nargs='?',
const=True,
default=False,
required=False,
)
parser.add_argument(
'-p',
'--polling',
dest='polling',
help="Use polling (instead of inotify)",
metavar='true|false',
type=str2bool,
nargs='?',
const=True,
default=os.getenv('PCAP_PIPELINE_POLLING', False),
required=False,
)
parser.add_argument(
'-c',
'--closed-sec',
dest='assumeClosedSec',
help="When polling, assume a file is closed after this many seconds of inactivity",
metavar='<seconds>',
type=int,
default=int(os.getenv('PCAP_PIPELINE_POLLING_ASSUME_CLOSED_SEC', str(watch_common.ASSUME_CLOSED_SEC_DEFAULT))),
required=False,
)
parser.add_argument(
'-i',
'--in',
dest='srcDir',
help='Source directory to monitor',
metavar='<directory>',
type=str,
default=os.path.join(remove_suffix(os.getenv('PCAP_PATH', '/pcap'), '/'), 'upload'),
required=False,
)
parser.add_argument(
'-o',
'--out',
dest='dstDir',
help='Destination directory',
metavar='<directory>',
type=str,
default=os.path.join(remove_suffix(os.getenv('PCAP_PATH', '/pcap'), '/'), 'processed'),
required=False,
)
parser.add_argument(
'-z',
'--zeek',
dest='zeekDir',
help='Zeek upload directory',
metavar='<directory>',
type=str,
default=os.path.join(remove_suffix(os.getenv('ZEEK_PATH', '/zeek'), '/'), 'upload'),
required=False,
)
parser.add_argument(
'-u',
'--uid',
dest='chownUid',
help='UID to chown files',
metavar='<integer>',
type=int,
default=int(os.getenv('PUID', os.getenv('DEFAULT_UID', '1000'))),
required=False,
)
parser.add_argument(
'-g',
'--gid',
dest='chownGid',
help='UID to chown files',
metavar='<integer>',
type=int,
default=int(os.getenv('PGID', os.getenv('DEFAULT_GID', '1000'))),
required=False,
)
parser.add_argument(
'--start-sleep',
dest='startSleepSec',
help="Sleep for this many seconds before starting",
metavar='<seconds>',
type=int,
default=0,
required=False,
)
try:
parser.error = parser.exit
args = parser.parse_args()
except SystemExit:
parser.print_help()
exit(2)
args.verbose = logging.ERROR - (10 * args.verbose) if args.verbose > 0 else 0
logging.basicConfig(
level=args.verbose, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S'
)
logging.info(os.path.join(scriptPath, scriptName))
logging.info("Arguments: {}".format(sys.argv[1:]))
logging.info("Arguments: {}".format(args))
if args.verbose > logging.DEBUG:
sys.tracebacklimit = 0
# handle sigint and sigterm for graceful shutdown
signal.signal(signal.SIGINT, shutdown_handler)
signal.signal(signal.SIGTERM, shutdown_handler)
# sleep for a bit if requested
sleepCount = 0
while (not shuttingDown[0]) and (sleepCount < args.startSleepSec):
time.sleep(1)
sleepCount += 1
args.dstDir = remove_suffix(args.dstDir, '/')
args.srcDir = remove_suffix(args.srcDir, '/')
args.zeekDir = remove_suffix(args.zeekDir, '/')
# if directory to monitor doesn't exist, create it now
if not os.path.isdir(args.srcDir):
logging.info(f'{scriptName}:\tcreating "{args.srcDir}" to monitor')
pathlib.Path(args.srcDir).mkdir(parents=False, exist_ok=True)
# if recursion was requested, get list of directories to monitor
watchDirs = []
while len(watchDirs) == 0:
if args.recursiveDir is None:
watchDirs = [args.srcDir]
else:
watchDirs = glob.glob(f'{args.srcDir}/**/{args.recursiveDir}', recursive=True)
watch_common.WatchAndProcessDirectory(
watchDirs,
args.polling,
args.recursiveAll,
file_processor,
{
"logger": logging,
"destination": args.dstDir,
"zeek": args.zeekDir,
"uid": args.chownUid,
"gid": args.chownGid,
},
args.assumeClosedSec,
shuttingDown,
logging,
)
if __name__ == '__main__':
main()