Debian Codegraph @

bin/grokevt-findlogs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/python
2
3 # This script attempts to find log file fragments in raw binary
4 # files, such as memory dumps and disk images.
5 #
6 # Copyright (C) 2006-2007 Timothy D. Morgan
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation version 3 of the
11 # License.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # vi:set tabsize=4:
19 # $Id: grokevt-findlogs 120 2011-06-20 18:12:16Z tim $
20
21
22 import sys
23 import os
24 import types
25 import time
26 import struct
27 import grokevt
28
29
30 class fileSearcher:
31 f = None
32 largest = 0
33 bs = 512*1024
34 strings = None
35 sz = -1
36
37 def __init__(self, file_name, strings):
38 self.f = open(BIN_FILE, 'rb')
39
40 self.largest = max([len(s) for s in strings])
41 self.bs = max(self.largest,self.bs)
42 self.strings = strings
43
44
45 def findOne(self, start=0):
46 self.f.seek(start)
47 offset = start
48 buf = self.f.read(self.bs)
49 while buf != '':
50 hits = []
51 for s in self.strings:
52 h = buf.find(s)
53 if h != -1:
54 hits.append(h)
55
56 if len(hits) > 0:
57 return offset+min(hits)
58
59 buf = buf[(self.bs - self.largest):] + self.f.read(self.bs)
60 offset += (self.bs - self.largest)
61
62 return -1
63
64
65 def read(self, offset, count):
66 self.f.seek(offset)
67 return self.f.read(count)
68
69
70 def size(self):
71 if self.sz > 0:
72 return self.sz
73
74 o = self.f.tell()
75 self.f.seek(0, 2)
76 self.sz = self.f.tell()
77 self.f.seek(o)
78 return self.sz
79
80
81 def close(self):
82 self.f.close()
83
84
85 def usage():
86 sys.stderr.write("USAGE:\n")
87 sys.stderr.write(" %s -?\n" % os.path.basename(sys.argv[0]))
88 sys.stderr.write(" %s [-v] [-h] [-H] [-o <OFFSET>] <RAW_FILE>\n\n"
89 % os.path.basename(sys.argv[0]))
90 sys.stderr.write("grokevt-findlogs attempts to find log file fragments in raw\n")
91 sys.stderr.write("binary files, such as memory dumps and disk images.\n")
92 sys.stderr.write("Please see the man page for more information.\n")
93
94
95
96 # Globals influenced by command line options
97 print_header = 1
98 # XXX: not currently used
99 print_verbose = 0
100 start_offset = 0
101
102 # Parse command line
103 argv_len = len(sys.argv)
104 if (argv_len < 2) or (sys.argv[1] == '-?') or (sys.argv[1] == '--help'):
105 usage()
106 sys.exit(os.EX_USAGE)
107 else:
108 BIN_FILE=sys.argv[argv_len-1]
109
110 skip_one = 0
111 for i in range(1,len(sys.argv)-1):
112 if skip_one:
113 skip_one = 0
114 else:
115 if sys.argv[i] == '-H':
116 print_header = 0
117 elif sys.argv[i] == '-h':
118 print_header = 1
119 elif sys.argv[i] == '-v':
120 print_verbose = 1
121 elif sys.argv[i] == '-o':
122 if i+1 < len(sys.argv):
123 # XXX: might be nice to allow hexadecimal in addition to decimal
124 start_offset = int(sys.argv[i+1])
125 skip_one = 1
126 else:
127 usage()
128 sys.stderr.write("ERROR: -o option requires parameter.\n")
129 sys.exit(os.EX_USAGE)
130 else:
131 usage()
132 sys.stderr.write("ERROR: Unrecognized option '%s'.\n" % option)
133 sys.exit(os.EX_USAGE)
134
135
136 if not os.access(BIN_FILE, os.R_OK):
137 sys.stderr.write("ERROR: Input file could not be read.\n")
138 sys.exit(os.EX_OSFILE)
139
140
141 searcher = fileSearcher(BIN_FILE,
142 (grokevt.header_log_magic, grokevt.cursor_magic))
143
144 if print_header:
145 print("OFFSET,LENGTH,TYPE,MESSAGE_NUM,CREATED,CONTIGUOUS")
146
147 contig_next = -1
148 i = searcher.findOne(start_offset)
149 while i != -1:
150 found_one = False
151 if (i + grokevt.min_record_size < searcher.size()):
152 offset = i-4
153 size1 = struct.unpack("<I", searcher.read(offset,4))[0]
154 end = offset+size1
155 if (size1 >= grokevt.min_record_size) and (end < searcher.size()):
156 size2 = struct.unpack("<I", searcher.read(end-4,4))[0]
157 if size1 == size2:
158 record = searcher.read(offset,size1)
159 guess = grokevt.guessRecordType(record)
160
161 contig = ''
162 if contig_next == offset:
163 contig = '*'
164
165 if guess == 'log':
166 (mnum,dc) = struct.unpack("<II", record[8:16])
167 print("%d,%d,%s,%s,%s,%s"\
168 % (offset,size1,guess,"%d" % mnum,
169 time.strftime("%Y-%m-%d %H:%M:%S",
170 time.gmtime(dc)),
171 contig))
172 found_one = True
173
174 elif guess != 'unknown':
175 print("%d,%d,%s,,,%s" % (offset,size1,guess,contig))
176 found_one = True
177
178 if found_one:
179 contig_next = end
180 i = searcher.findOne(end)
181 else:
182 i = searcher.findOne(i+1)
183
184 searcher.close()