-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcap_time_shift.py
executable file
·323 lines (276 loc) · 9.84 KB
/
pcap_time_shift.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import dateparser
import os
import platform
import shutil
import sys
from datetime import datetime
from subprocess import PIPE, Popen
###################################################################################################
args = None
debug = False
editcapBin = None
capinfosBin = None
script_name = os.path.basename(__file__)
script_path = os.path.dirname(os.path.realpath(__file__))
orig_path = os.getcwd()
pyPlatform = platform.system()
###################################################################################################
PLATFORM_WINDOWS = "Windows"
PLATFORM_MAC = "Darwin"
PLATFORM_LINUX = "Linux"
###################################################################################################
# print to stderr
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
sys.stderr.flush()
###################################################################################################
# convenient boolean argument parsing
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
###################################################################################################
# determine if a program/script exists and is executable in the system path
def Which(cmd, debug=False):
result = any(os.access(os.path.join(path, cmd), os.X_OK) for path in os.environ["PATH"].split(os.pathsep))
if debug:
eprint(f"Which {cmd} returned {result}")
return result
###################################################################################################
# run command with arguments and return its exit code, stdout, and stderr
def check_output_input(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden')
if 'stderr' in kwargs:
raise ValueError('stderr argument not allowed, it will be overridden')
if 'input' in kwargs and kwargs['input']:
if 'stdin' in kwargs:
raise ValueError('stdin and input arguments may not both be used')
input_data = kwargs['input']
kwargs['stdin'] = PIPE
else:
input_data = None
kwargs.pop('input', None)
process = Popen(*popenargs, stdout=PIPE, stderr=PIPE, **kwargs)
try:
output, errput = process.communicate(input_data)
except:
process.kill()
process.wait()
raise
retcode = process.poll()
return retcode, output, errput
###################################################################################################
# run command with arguments and return its exit code and output
def run_process(command, stdout=True, stderr=True, stdin=None, cwd=None, env=None, debug=False):
retcode = -1
output = []
try:
# run the command
retcode, cmdout, cmderr = check_output_input(command, input=stdin.encode() if stdin else None, cwd=cwd, env=env)
# split the output on newlines to return a list
if stderr and (len(cmderr) > 0):
output.extend(cmderr.decode(sys.getdefaultencoding()).split('\n'))
if stdout and (len(cmdout) > 0):
output.extend(cmdout.decode(sys.getdefaultencoding()).split('\n'))
except (FileNotFoundError, OSError, IOError) as e:
if stderr:
output.append("Command {} not found or unable to execute".format(command))
if debug:
eprint(
"{}{} returned {}: {}".format(
command, "({})".format(stdin[:80] + bool(stdin[80:]) * '...' if stdin else ""), retcode, output
)
)
return retcode, output
###################################################################################################
def get_pcap_first_time(pcapFile, continueOnError=False, debug=False):
global capinfosBin
try:
err, out = run_process(
[capinfosBin, '-a', '-C', '-K', '-M', '-m', '-r', '-S', '-T', pcapFile], stderr=False, debug=debug
)
if (err not in (0, 1)) or (out is None) or (len(out) <= 0):
raise Exception(f'{capinfosBin}(pcapFile) returned {err}: {out}')
try:
return datetime.fromtimestamp(float(out[0].split(',')[-1]))
except ValueError as e:
return datetime.fromtimestamp(float(out[0].split(',')[-1].split('.')[0]))
except Exception as e:
if continueOnError:
return None
else:
raise
###################################################################################################
def shift_pcap(pcapFile, baseTime, earliestRelativeTime, fileFormat='pcap', inPlace=False, debug=False):
global editcapBin
if os.path.isfile(pcapFile) and (baseTime is not None):
inFileParts = os.path.splitext(os.path.basename(pcapFile))
outFile = os.path.join(os.path.dirname(pcapFile), inFileParts[0] + "_shift" + inFileParts[1])
pcapTime = get_pcap_first_time(pcapFile)
relativeDiff = pcapTime - (earliestRelativeTime if earliestRelativeTime is not None else pcapTime)
err, out = run_process(
[
editcapBin,
'-F',
fileFormat,
'-t',
str(round((baseTime - pcapTime + relativeDiff).total_seconds())),
pcapFile,
outFile,
],
debug=debug,
)
if err != 0:
raise Exception(f'{editcapBin}(pcapFile) failed')
if inPlace:
os.remove(pcapFile)
shutil.move(outFile, pcapFile)
outFile = pcapFile
return outFile
else:
return None
###################################################################################################
# main
def main():
global args
global debug
global editcapBin
global capinfosBin
parser = argparse.ArgumentParser(
description=script_name, add_help=False, usage='{} <arguments>'.format(script_name)
)
parser.add_argument(
'-d',
'--defaults',
dest='accept_defaults',
type=str2bool,
nargs='?',
const=True,
default=False,
metavar='true|false',
help="Accept defaults to prompts without user interaction",
)
parser.add_argument(
'-v',
'--verbose',
dest='debug',
type=str2bool,
nargs='?',
const=True,
default=False,
metavar='true|false',
help="Verbose/debug output",
)
parser.add_argument(
'-t',
'--time',
dest='startTime',
type=str,
default=None,
required=False,
metavar='<string>',
help="Start time basis",
)
parser.add_argument(
'-f',
'--format',
dest='fileFormat',
type=str,
default='pcap',
required=False,
metavar='<string>',
help="File format",
)
parser.add_argument(
'-r',
'--relative',
dest='relative',
type=str2bool,
nargs='?',
const=True,
default=False,
metavar='true|false',
help="Maintain PCAP files' offsets relative to each other",
)
parser.add_argument(
'-p',
'--pcap',
dest='pcaps',
nargs='*',
type=str,
default=None,
required=True,
metavar='<PCAP file(s)>',
help="PCAP(s) to shift",
)
parser.add_argument(
'-i',
'--in-place',
dest='inPlace',
type=str2bool,
nargs='?',
const=True,
default=False,
metavar='true|false',
help="Adjust the PCAP files in-place",
)
try:
parser.error = parser.exit
args = parser.parse_args()
except SystemExit:
parser.print_help()
exit(2)
debug = args.debug
args.pcaps = [os.path.realpath(x) for x in args.pcaps if os.path.isfile(x)]
if (args.pcaps is None) or (len(args.pcaps) <= 0):
raise Exception('PCAP file(s) not specified or do not exist')
editcapBin = 'editcap.exe' if ((pyPlatform == PLATFORM_WINDOWS) and Which('editcap.exe')) else 'editcap'
capinfosBin = 'capinfos.exe' if ((pyPlatform == PLATFORM_WINDOWS) and Which('capinfos.exe')) else 'capinfos'
err, out = run_process([capinfosBin, '--version'], debug=args.debug)
if err != 0:
raise Exception(f'{script_name} requires capinfos')
err, out = run_process([editcapBin, '--version'], debug=args.debug)
if err != 0:
raise Exception(f'{script_name} requires editcap')
earliestTime = min(
[
x
for x in [get_pcap_first_time(pcap, continueOnError=True, debug=args.debug) for pcap in args.pcaps]
if x is not None
]
+ [datetime.now()]
)
if args.startTime is None:
# if they didn't sepecify a time, default to the earliest packet time
args.startTime = earliestTime
else:
# otherwise use whatever time they specified
args.startTime = dateparser.parse(args.startTime)
if debug:
eprint(os.path.join(script_path, script_name))
eprint("Arguments: {}".format(sys.argv[1:]))
eprint("Arguments: {}".format(args))
else:
sys.tracebacklimit = 0
for pcap in args.pcaps:
try:
shift_pcap(
pcap,
args.startTime,
earliestTime if args.relative else None,
fileFormat=args.fileFormat,
inPlace=args.inPlace,
debug=args.debug,
)
except Exception as e:
eprint(f'Exception "{e}" processing {pcap}, skipping')
###################################################################################################
if __name__ == '__main__':
main()