-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog_parser.rb
More file actions
280 lines (227 loc) · 6.48 KB
/
log_parser.rb
File metadata and controls
280 lines (227 loc) · 6.48 KB
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
#!/usr/bin/env ruby
require './config'
class AppImage
attr_accessor :name, :path, :base, :size, :oep
def initialize(path, base, size, oep)
@path, @base, @size, @oep = path, base, size, oep
@name = @path[@path.rindex('\\')+1..-1].upcase
end
def add_method(method)
@methods ||= {}
@methods[method.name] = method
end
def to_stdout
str = "Image: #{@name} [Base Address: #{@base}, MappeSize: #{@size}, Entry Point: #{@oep}]\n"
str += "\tPath: #{@path}\n"
if @methods
str += "\tMethods: \n"
@methods.each_pair { |name,method| str += "\t\t#{method.address} => #{name} \n"}
end
str += "\n"
end
def to_xml( less = [] )
str = "<image name=\"#{@name}\">\n"
str += "<path>#{@path}</path>\n"
str += "<base>#{@base}</base>\n"
str += "<mapped-size>#{@size}</mapped-size>\n"
str += "<entry-point>#{@oep}</entry-point>\n"
if @methods
str += "<methods>"
@methods.each_pair { |name,method| str += "<method address=\"#{method.address}\">#{name}</method>\n"}
str += "</methods>"
end
str += "</image>"
end
end
class AppMethod
attr_accessor :name, :address, :count, :calls
def initialize( name, address )
@name, @address = name, address
@count = 0
@calls = []
end
def called( call )
@count += 1
@calls<< call
end
def to_stdout
str = "#{@name} #{@count}\n"
str += "\t Calls: \n"
@calls.each { |call| str += call.to_stdout}
str += "\n"
end
def to_xml( less = [] )
"<method call-count=\"#{@count}\">#{@name}</method>"
end
end
class AppThread
attr_reader :calls, :id
def initialize( id )
@id = id
@calls = []
end
def called( call )
@calls << call
end
def to_stdout
str = "Thread: 0x#{@id.to_s(16)}\n"
str += "\tCalled:\n"
@calls.each { |call| str += call.to_stdout}
str += "\n"
end
def to_xml( less = [] )
xml = "<thread id=\"#{@id.to_s(16)}\" calls=\"#{@calls.size}\">"
@calls.each_index { |i| xml += @calls[i].to_xml}
xml += "</thread>"
end
end
class AppCall
attr_accessor :name, :arguments, :thread, :ret
def initialize( name, arguments, thread)
@name, @arguments, @thread = name, arguments, thread
@ret = nil
end
def returned( values)
@ret = values
end
def to_stdout
str = "#{@name}\n"
str += "\tThread: 0x#{@thread.id.to_s(16)}\n"
if @arguments && @arguments.size > 0
str += "\tInput Parameters: #{@arguments.to_s}\n"
end
if @ret && @ret.size>0
str += "\t Returned: #{@ret.to_s}\n"
end
str += "\n"
end
def to_xml( less = [] )
str = "<call thread=\"#{@thread.id.to_s(16)}\">"
str += "<name>#{@name}</name>"
if @arguments && @arguments.size > 0
str += "<call-params>"
@arguments.each_pair {|name, value| str += "<param name=\"#{name}\" value=\"#{value}\"/>"}
str += "</call-params>"
else
str += "<call-params/>"
end
if @ret && @ret.size > 0
str += "<return-params>"
@ret.each_pair {|name,value| str += "<param name=\"#{name}\" value=\"#{value}\"/>"}
str += "</return-params>"
else
str += "<return-params/>"
end
str += "</call>"
end
end
class Parser
DELIM = '|'
def initialize( source)
@src = source
@threads = {}
@images = {}
@methods = {}
@calls = []
@last_call = {}
end
def onImage( data )
parts = data.split(DELIM)
path = parts[1]
base = parts[2].split('@')[1]
size = parts[3].split('@')[1]
oep = parts[4].split('@')[1]
img = AppImage.new(path, base, size, oep)
# puts "[?] added image #{img.name}"
@images[img.name] = img
end
def onThread( data )
thr, fn_name, event, *params = data.split(DELIM)
th_id = thr.split('=')[1].to_i(16)
@threads[th_id] ||= AppThread.new(th_id)
case event
when 'CALL'
call = AppCall.new(fn_name, __params_hash(params), @threads[th_id])
@last_call[th_id] = call
@calls<< call
@methods[call.name].called( call ) if @methods[call.name]
when 'RETN'
if @last_call[th_id].ret != nil
puts "[!] Unexpected #{data}"
else
@last_call[th_id].returned( __params_hash(params) )
@threads[th_id].called( @last_call[th_id] )
end
end
end
def __params_hash( params )
params = [params] if params.is_a?(String)
return nil if not params.is_a?(Array)
# puts params.inspect if DEBUG == 1
args = {}
params.each do |param|
next unless param.include?('=')
name,value = param.split('=')
args[name] = value
end
return args
end
def onSymbol( data )
# puts data.inspect
cmd, img, fn_name, addr = data.split(DELIM)
method = AppMethod.new(fn_name,addr.split('@')[1])
if @images.key?(img)
@images[img].add_method(method)
else
puts "[!] Missing Image #{img} with Method #{fn_name}"
end
@methods[method.name] = method
end
def parse
File.open(@src,"r") do |file|
while line = file.gets
next if line.strip.size == 0 or line.start_with?( "[*]Done" )
# puts line if DEBUG == 1
if line.match(/^Image*/)
onImage( line.strip )
elsif line.match(/^Thread*/)
onThread( line.strip )
elsif line.match(/^SYMBOL*/)
onSymbol( line.strip ) #unless line.match ( )
end
end
end
return self
end
def result
# puts "[*] #{@images.size} Loaded Images...\n"
# @images.each_value {|img| puts img.to_stdout}
puts "[*] Threads #{@threads.size}"
@threads.each_value {|th| puts th.to_stdout}
#puts "[*] Calls #{@calls.size}"
# puts "[*] Methods #{@methods.size}"
# @methods.each_value { |method| puts method.to_stdout}
end
def xmlize ( io )
io.write( "<puncture>" )
io.write( "<images count=\"#{@images.size}\">" )
@images.each_value {|image| io.write( image.to_xml )}
io.write( "</images>" )
io.write( "<methods count=\"#{@methods.size}\">" )
@methods.each_value { |method| io.write( method.to_xml )}
io.write( "</methods>" )
io.write( "<calls count=\"#{@calls.size}\">")
@calls.each { |call| io.write( call.to_xml )}
io.write( "</calls>" )
io.write( "<threads count=\"#{@threads.size}\">" )
@threads.each_value {|thrd| io.write( thrd.to_xml )}
io.write( "</threads>" )
io.write( "</puncture>" )
io.flush
end
end
if $0 == __FILE__
parser = Parser.new($config['LOG_FILE']).parse
parser.result
File.open("puncture.xml","w") { |file| parser.xmlize( file )}
end