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
|
"""
Baseclass for reading PDML produced from Tethereal.
Copyright (c) 2003 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
from xml.sax import saxlib
from xml.sax import saxexts
from xml.sax import saxutils
class CaptureFile:
pass
class FoundItException(Exception):
pass
class PacketList:
"""Holds Packet objects, and has methods for finding
items within it."""
def __init__(self, children=None):
if children == None:
self.children = []
else:
self.children = children
def __getitem__(self, index):
"""We act like a list."""
return self.children[index]
def item_exists(self, name):
"""Does an item with name 'name' exist in this
PacketList?"""
for child in self.children:
if child.name == name:
return 1
try:
for child in self.children:
child._item_exists(name)
except FoundItException:
return 1
return 0
def _item_exists(self, name):
for child in self.children:
if child.name == name:
raise FoundItException
child._item_exists(name)
def get_items(self, name, items=None):
"""Return all items that match the name 'name'.
They are returned in order of a depth-first-search."""
if items == None:
top_level = 1
items = []
else:
top_level = 0
for child in self.children:
if child.name == name:
items.append(child)
child.get_items(name, items)
if top_level:
return PacketList(items)
def get_items_before(self, name, before_item, items=None):
"""Return all items that match the name 'name' that
exist before the before_item. The before_item is an object.
They results are returned in order of a depth-first-search.
This function allows you to find fields from protocols that occur
before other protocols. For example, if you have an HTTP
protocol, you can find all tcp.dstport fields *before* that HTTP
protocol. This helps analyze in the presence of tunneled protocols."""
if items == None:
top_level = 1
items = []
else:
top_level = 0
for child in self.children:
if top_level == 1 and child == before_item:
break
if child.name == name:
items.append(child)
# Call get_items because the 'before_item' applies
# only to the top level search.
child.get_items(name, items)
if top_level:
return PacketList(items)
class ProtoTreeItem(PacketList):
def __init__(self, xmlattrs):
PacketList.__init__(self)
self.name = xmlattrs.get("name", "")
self.showname = xmlattrs.get("showname", "")
self.pos = xmlattrs.get("pos", "")
self.size = xmlattrs.get("size", "")
self.value = xmlattrs.get("value", "")
self.show = xmlattrs.get("show", "")
self.hide = xmlattrs.get("hide", "")
def add_child(self, child):
self.children.append(child)
def get_name(self):
return self.name
def get_showname(self):
return self.showname
def get_pos(self):
return self.pos
def get_size(self):
return self.size
def get_value(self):
return self.value
def get_show(self):
return self.show
def get_hide(self):
return self.hide
def dump(self, fh):
if self.name:
print >> fh, " name=%s" % (saxutils.quoteattr(self.name),),
if self.showname:
print >> fh, "showname=%s" % (saxutils.quoteattr(self.showname),),
if self.pos:
print >> fh, "pos=%s" % (saxutils.quoteattr(self.pos),),
if self.size:
print >> fh, "size=%s" % (saxutils.quoteattr(self.size),),
if self.value:
print >> fh, "value=%s" % (saxutils.quoteattr(self.value),),
if self.show:
print >> fh, "show=%s" % (saxutils.quoteattr(self.show),),
if self.hide:
print >> fh, "hide=%s" % (saxutils.quoteattr(self.hide),),
class Packet(ProtoTreeItem, PacketList):
def dump(self, fh, indent=0):
print >> fh, " " * indent, "<packet>"
indent += 1
for child in self.children:
child.dump(fh, indent)
print >> fh, " " * indent, "</packet>"
class Protocol(ProtoTreeItem):
def dump(self, fh, indent=0):
print >> fh, "%s<proto " % (" " * indent,),
ProtoTreeItem.dump(self, fh)
print >> fh, '>'
indent += 1
for child in self.children:
child.dump(fh, indent)
print >> fh, " " * indent, "</proto>"
class Field(ProtoTreeItem):
def dump(self, fh, indent=0):
print >> fh, "%s<field " % (" " * indent,),
ProtoTreeItem.dump(self, fh)
if self.label:
print >> fh, "label=%s" % (saxutils.quoteattr(self.label),),
if self.children:
print >> fh, ">"
indent += 1
for child in self.children:
child.dump(fh, indent)
print >> fh, " " * indent, "</field>"
else:
print >> fh, "/>"
class ParseXML(saxlib.HandlerBase):
ELEMENT_FILE = "pdml"
ELEMENT_FRAME = "packet"
ELEMENT_PROTOCOL = "proto"
ELEMENT_FIELD = "field"
def __init__(self, cb):
self.cb = cb
self.chars = ""
self.element_stack = []
def startElement(self, name, xmlattrs):
self.chars = ""
if name == self.ELEMENT_FILE:
# Eventually, we should check version number of pdml here
elem = CaptureFile()
elif name == self.ELEMENT_FRAME:
elem = Packet(xmlattrs)
elif name == self.ELEMENT_PROTOCOL:
elem = Protocol(xmlattrs)
elif name == self.ELEMENT_FIELD:
elem = Field(xmlattrs)
else:
sys.exit("Unknown element: %s" % (name,))
self.element_stack.append(elem)
def endElement(self, name):
elem = self.element_stack.pop()
# if isinstance(elem, Field):
# if elem.get_name() == "frame.number":
# print >> sys.stderr, "Packet:", elem.get_show()
# Add element as child to previous element as long
# as there is more than 1 element in the stack. Only
# one element in the stack means that the the element in
# the stack is the single CaptureFile element, and we don't
# want to add this element to that, as we only want one
# Packet element in memory at a time.
if len(self.element_stack) > 1:
parent_elem = self.element_stack[-1]
parent_elem.add_child(elem)
self.chars = ""
# If we just finished a Packet element, hand it to the
# user's callback.
if isinstance(elem, Packet):
self.cb(elem)
def characters(self, chars, start, length):
self.chars = self.chars + chars[start:start+length]
def parse_fh(fh, cb):
# Create a parser
parser = saxexts.make_parser()
# Create the handler
ch = ParseXML(cb)
# Tell the parser to use our handler
parser.setDocumentHandler(ch)
# Parse the file
parser.parseFile(fh)
# Close the parser
parser.close()
def _test():
import sys
def test_cb(obj):
pass
filename = sys.argv[1]
fh = open(filename, "r")
parse_fh(fh, test_cb)
if __name__ == '__main__':
_test()
|