3 Retrieve a packet from a ethereal/tethereal core file
4 and save it in a packet-capture file.
20 re_frame = re.compile(r"^#(?P<num>\d+) ")
21 re_func1 = re.compile(r"^#\d+\s+(?P<func>\w+) \(")
22 re_func2 = re.compile(r"^#\d+\s+0x[A-Fa-f\d]+ in (?P<func>\w+) \(")
24 def __init__(self, lines):
26 # In order; each item is the function name.
28 found_non_bt_frame = 0
32 m = self.re_frame.search(line)
34 # Skip the first frame that gdb shows,
35 # which is not part of the backtrace.
36 if not found_non_bt_frame:
37 found_non_bt_frame = 1
40 # Get the frame number and make sure it's
41 # what we expect it should be.
42 frame_num = int(m.group("num"))
43 if frame_num != frame_will_be:
44 sys.exit("Found frame %d instead of %d" % \
45 (frame_num, frame_will_be))
47 # Find the function name. XXX - need to handle '???'
48 n = self.re_func1.search(line)
50 n = self.re_func2.search(line)
53 func = n.group("func")
55 sys.exit("Function name not found in %s" % (line,))
58 self.frames.append(func)
65 def HasFunction(self, func):
66 return func in self.frames
68 def Frame(self, func):
69 return self.frames.index(func)
72 # Some values from wiretap; wiretap should be a shared
73 # libray and a Python module should be created for it so
74 # this program could just write a libpcap file directly.
75 WTAP_ENCAP_PER_PACKET = -1
76 WTAP_ENCAP_UNKNOWN = 0
77 WTAP_ENCAP_ETHERNET = 1
78 WTAP_ENCAP_TOKEN_RING = 2
82 WTAP_ENCAP_FDDI_BITSWAPPED = 6
85 WTAP_ENCAP_ATM_RFC1483 = 9
86 WTAP_ENCAP_LINUX_ATM_CLIP = 10
88 WTAP_ENCAP_ATM_SNIFFER = 12
90 WTAP_ENCAP_ASCEND = 14
93 WTAP_ENCAP_PPP_WITH_PHDR = 17
94 WTAP_ENCAP_IEEE_802_11 = 18
96 WTAP_ENCAP_FRELAY = 20
98 WTAP_ENCAP_CISCO_IOS = 22
99 WTAP_ENCAP_LOCALTALK = 23
100 WTAP_ENCAP_PRISM_HEADER = 24
101 WTAP_ENCAP_PFLOG = 25
102 WTAP_ENCAP_AIROPEEK = 26
103 WTAP_ENCAP_HHDLC = 27
104 # last WTAP_ENCAP_ value + 1
105 WTAP_NUM_ENCAP_TYPES = 28
109 WTAP_ENCAP_ETHERNET : 1,
110 WTAP_ENCAP_TOKEN_RING : 6,
111 WTAP_ENCAP_ARCNET : 7,
114 WTAP_ENCAP_FDDI_BITSWAPPED : 10,
115 WTAP_ENCAP_FDDI : 10,
116 WTAP_ENCAP_ATM_RFC1483 : 11,
117 WTAP_ENCAP_RAW_IP : 12,
118 WTAP_ENCAP_LINUX_ATM_CLIP : 16, # or 18, or 19...
119 WTAP_ENCAP_CHDLC : 104,
120 WTAP_ENCAP_IEEE_802_11 : 105,
121 WTAP_ENCAP_SLL : 113,
122 WTAP_ENCAP_LOCALTALK : 114,
123 WTAP_ENCAP_PFLOG : 117,
124 WTAP_ENCAP_CISCO_IOS : 118,
125 WTAP_ENCAP_PRISM_HEADER : 119,
126 WTAP_ENCAP_HHDLC : 121,
131 WTAP_ENCAP_UNKNOWN : "Unknown",
132 WTAP_ENCAP_ETHERNET : "Ethernet",
133 WTAP_ENCAP_TOKEN_RING : "Token-Ring",
134 WTAP_ENCAP_SLIP : "SLIP",
135 WTAP_ENCAP_PPP : "PPP",
136 WTAP_ENCAP_FDDI : "FDDI",
137 WTAP_ENCAP_FDDI_BITSWAPPED : "FDDI (Bitswapped)",
138 WTAP_ENCAP_RAW_IP : "Raw IP",
139 WTAP_ENCAP_ARCNET : "ARCNET",
140 WTAP_ENCAP_ATM_RFC1483 : "ATM RFC1483",
141 WTAP_ENCAP_LINUX_ATM_CLIP : "Linux ATM CLIP",
142 WTAP_ENCAP_LAPB : "LAPB",
143 WTAP_ENCAP_ATM_SNIFFER : "ATM Sniffer",
144 WTAP_ENCAP_NULL : "Null",
145 WTAP_ENCAP_ASCEND : "Ascend",
146 WTAP_ENCAP_LAPD : "LAPD",
147 WTAP_ENCAP_V120 : "V.120",
148 WTAP_ENCAP_PPP_WITH_PHDR : "PPP (with PHDR)",
149 WTAP_ENCAP_IEEE_802_11 : "IEEE 802.11",
150 WTAP_ENCAP_SLL : "SLL",
151 WTAP_ENCAP_FRELAY : "Frame Relay",
152 WTAP_ENCAP_CHDLC : "Cisco HDLC",
153 WTAP_ENCAP_CISCO_IOS : "Cisco IOS",
154 WTAP_ENCAP_LOCALTALK : "LocalTalk",
155 WTAP_ENCAP_PRISM_HEADER : "Prism Header",
156 WTAP_ENCAP_PFLOG : "PFLog",
157 WTAP_ENCAP_AIROPEEK : "AiroPeek",
158 WTAP_ENCAP_HHDLC : "HHDLC",
161 def wtap_to_pcap(wtap):
162 if not wtap_to_pcap_map.has_key(wtap):
163 sys.exit("Don't know how to convert wiretap encoding %d to libpcap." % \
166 return wtap_to_pcap_map[wtap]
169 def run_gdb(*commands):
170 if len(commands) == 0:
173 # Create a temporary file
174 fname = tempfile.mktemp()
176 fh = open(fname, "w")
178 sys.exit("Cannot open %s for writing: %s" % (fname, err))
180 # Put the commands in it
193 sys.exit("Cannot close %s: %s" % (fname, err))
197 cmd = "gdb --nw --quiet --command=%s %s %s" % (fname, exec_file, core_file)
199 print "Invoking %s" % (cmd,)
207 sys.exit("Cannot run gdb: %s" % (err,))
210 result = pipe.readlines()
217 sys.exit("gdb returned an exit value of %s" % (error,))
220 # Remove the temp file and return the results
227 def get_value_from_frame(frame_num, variable, fmt=""):
230 cmds.append("up %d" % (frame_num,))
232 cmds.append("print %s %s" % (fmt, variable))
233 lines = apply(run_gdb, cmds)
235 LOOKING_FOR_START = 0
237 state = LOOKING_FOR_START
245 if state == LOOKING_FOR_START:
249 if line[0:4] == "$1 =":
251 state = READING_VALUE
253 elif state == READING_VALUE:
258 def get_int_from_frame(frame_num, variable):
259 text = get_value_from_frame(frame_num, variable)
263 sys.exit("Could not convert '%s' to integer." % (text,))
267 def get_byte_array_from_frame(frame_num, variable, length):
268 var = "{char}%s@%d" % (variable, length)
269 text = get_value_from_frame(frame_num, var, "/u")
271 # Remove curly braces
274 sys.exit("Left curly brace not found in '%s'" % (text,))
279 sys.exit("Right curly brace not found in '%s'" % (text,))
282 # Split the string until many little strings, each representing a byte
284 values_as_text = text.split(',')
286 # And get the numeric values
287 return map(int, values_as_text)
289 def make_cap_file(pkt_data, lnk_t):
291 pcap_lnk_t = wtap_to_pcap(lnk_t)
293 # Create a temporary file
294 fname = tempfile.mktemp()
296 fh = open(fname, "w")
298 sys.exit("Cannot open %s for writing: %s" % (fname, err))
302 # Put the hex dump in it
305 for byte in pkt_data:
306 if (offset % BYTES_IN_ROW) == 0:
307 print >> fh, "\n%08X " % (offset,),
308 print "\n%08X " % (offset,),
310 print >> fh, "%02X " % (byte,),
311 print "%02X " % (byte,),
324 sys.exit("Cannot close %s: %s" % (fname, err))
328 cmd = "text2pcap -q -l %s %s %s" % (pcap_lnk_t, fname, output_file)
329 # print "Command is %s" % (cmd,)
331 retval = os.system(cmd)
337 sys.exit("Cannot run text2pcap: %s" % (err,))
339 # Remove the temp file
346 print "%s created with %d bytes in packet, and %s encoding." % \
347 (output_file, len(pkt_data), wtap_name[lnk_t])
349 sys.exit("text2pcap did not run succesfully.")
354 def try_frame(func_text, cap_len_text, lnk_t_text, data_text):
357 bt_text = run_gdb("bt")
358 bt = BackTrace(bt_text)
359 if not bt.HasFunction(func_text):
360 print "%s() not found in backtrace." % (func_text,)
363 print "%s() found in backtrace." % (func_text,)
365 # Figure out where the call to epan_dissect_run is.
366 frame_num = bt.Frame(func_text)
368 # Get the capture length
369 cap_len = get_int_from_frame(frame_num, cap_len_text)
371 # Get the encoding type
372 lnk_t = get_int_from_frame(frame_num, lnk_t_text)
374 # Get the packet data
375 pkt_data = get_byte_array_from_frame(frame_num, data_text, cap_len)
378 print "Length=%d" % (cap_len,)
379 print "Encoding=%d" % (lnk_t,)
380 print "Data (%d bytes) = %s" % (len(pkt_data), pkt_data)
381 make_cap_file(pkt_data, lnk_t)
385 if try_frame("epan_dissect_run",
386 "fd->cap_len", "fd->lnk_t", "data"):
388 elif try_frame("add_packet_to_packet_list",
389 "fdata->cap_len", "fdata->lnk_t", "buf"):
392 sys.exit("A packet cannot be pulled from this core.")
396 print "pkt-from-core.py [-v] -w capture_file executable-file (core-file or process-id)"
398 print "\tGiven an executable file and a core file, this tool"
399 print "\tuses gdb to retrieve the packet that was being dissected"
400 print "\tat the time ethereal/tethereal stopped running. The packet"
401 print "\tis saved in the capture_file specified by the -w option."
403 print "\t-v : verbose"
414 opts, args = getopt.getopt(sys.argv[1:], optstring)
418 for opt, arg in opts:
426 if output_file == None:
437 if __name__ == '__main__':