-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdasse.rb
93 lines (80 loc) · 1.83 KB
/
dasse.rb
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
# coding: utf-8
##
## DAsse - Disassembler for *.exe file
## (https://github.com/hisui/dasse)
##
require "optparse"
require "pp"
$:.unshift "#{File.dirname(__FILE__)}/lib"
$:.unshift "#{File.dirname(__FILE__)}/ext"
require "exefile"
require "x86"
$dasse_config = {
cpu_arch: "x86",
file_format: "exe",
show_offset: true,
show_hex_dump: true,
}
OptionParser.new {|opt|
opt.on("-f [VAL]") {|v| $dasse_config[:file_format] = v }
opt.on("-a [VAL]") {|v| $dasse_config[:cpu_arch] = v }
opt.parse! ARGV
}
if ARGV.empty?
$stderr.puts "You must specify a file."
exit -1
end
data_offset = 0
data = File.open(ARGV[0], "rb:ASCII-8BIT") {|io|
case $dasse_config[:file_format]
when "raw" then io.read
when "exe" then
exe = ExeImage.new io
unless section = exe.get_section_header(".text")
raise RuntimeError.new("`.text' section is not found!")
end
# 多分、コレでいいはず・・・
data_offset = section.virtualAddress + exe.optional_header.imageBase
exe.get_section_contents ".text"
else
$stderr.puts "Unknown type of file_format=`#{$dasse_config[:file_format]}'."
exit -1
end
}
decoder = case $dasse_config[:cpu_arch]
when "x86" then DASM_x86.new data
when "raw" then
class DefaultDecoder
attr_reader :pos
def initialize(src)
@src = src
@pos = 0
end
def more?
@pos < @src.size
end
def walk
slice = @src[@pos, 8]
slice.bytes.map {|b|
33 <= b && b <= 126 ? b.chr: "."
}.join " "
ensure
@pos += 8
end
end
DefaultDecoder.new data
else
$stderr.puts "Unknown type of cpu_arch=`#{$dasse_config[:cpu_arch]}'."
exit -1
end
while decoder.more?
pos = decoder.pos
col = decoder.walk
if $dasse_config[:show_offset]
printf "%08x: ", pos + data_offset
end
if $dasse_config[:show_hex_dump]
printf "% 30s: ", data[pos...decoder.pos].bytes.map{|b| "%02x" % b}.join(" ")
end
puts col.to_s
end