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
|
#!/usr/bin/env python
"""
Converts netscreen snoop hex-dumps to a hex-dump that text2pcap can read.
Copyright (c) 2004 by Gilbert Ramirez <gram@alumni.rice.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import sys
import re
import os
import stat
import time
class OutputFile:
TIMER_MAX = 99999.9
def __init__(self, name, base_time):
try:
self.fh = open(name, "w")
except IOError, err:
sys.exit(err)
self.base_time = base_time
self.prev_timestamp = 0.0
def PrintPacket(self, timestamp, datalines):
# What do to with the timestamp? I need more data about what
# the netscreen timestamp is, then I can generate one for the text file.
# print "TS:", timestamp.group("time")
try:
timestamp = float(timestamp.group("time"))
except ValueError:
sys.exit("Unable to convert '%s' to floating point." % \
(timestamp,))
# Did we wrap around the timeer max?
if timestamp < self.prev_timestamp:
self.base_time += self.TIMER_MAX
self.prev_timestamp = timestamp
packet_timestamp = self.base_time + timestamp
# Determine the time string to print
gmtime = time.gmtime(packet_timestamp)
subsecs = packet_timestamp - int(packet_timestamp)
assert subsecs <= 0
subsecs = int(subsecs * 10)
print >> self.fh, "%s.%d" % (time.strftime("%Y-%m-%d %H:%M:%S", gmtime), \
subsecs)
# Print the packet data
offset = 0
for lineno, hexgroup in datalines:
hexline = hexgroup.group("hex")
hexpairs = hexline.split()
print >> self.fh, "%08x %s" % (offset, hexline)
offset += len(hexpairs)
# Blank line
print >> self.fh
re_timestamp = re.compile(r"^(?P<time>\d+\.\d): \d+\((?P<io>.)\):")
re_hex_line = re.compile(r"(?P<hex>([0-9a-f]{2} ){1,16})\s+(?P<ascii>.){1,16}")
def run(input_filename, output_filename):
try:
ifh = open(input_filename, "r")
except IOError, err:
sys.exit(err)
try:
ctime = os.stat(input_filename)[stat.ST_CTIME]
except OSError, err:
sys.exit(err)
output_file = OutputFile(output_filename, ctime)
timestamp = None
datalines = []
lineno = 0
for line in ifh.xreadlines():
lineno += 1
if not timestamp:
m = re_timestamp.search(line)
if m:
timestamp = m
else:
m = re_hex_line.search(line)
if m:
datalines.append((lineno, m))
else:
if datalines:
output_file.PrintPacket(timestamp, datalines)
timestamp = None
datalines = []
if datalines:
output_file.PrintPacket(timestamp, datalines)
timestamp = None
datalines = []
def usage():
print >> sys.stderr, "Usage: netscreen2dump.py netscreen-dump-file new-dump-file"
sys.exit(1)
def main():
if len(sys.argv) != 3:
usage()
run(sys.argv[1], sys.argv[2])
if __name__ == "__main__":
main()
|