forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_badchars.rb
executable file
·164 lines (135 loc) · 3.91 KB
/
find_badchars.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
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
#!/usr/bin/env ruby
#
# $Id$
#
# This script is intended to assist an exploit developer in deducing what
# "bad characters" exist for a given input path to a program.
#
# $Revision$
#
msfbase = __FILE__
while File.symlink?(msfbase)
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end
gem 'rex-text'
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
require 'msfenv'
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
require 'rex'
OutStatus = "[*] "
OutError = "[-] "
$args = Rex::Parser::Arguments.new(
"-b" => [ true, "The list of characters to avoid: '\\x00\\xff'" ],
"-h" => [ false, "Help banner" ],
"-i" => [ true, "Read memory contents from the supplied file path" ],
"-t" => [ true, "The format that the memory contents are in (empty to list)" ])
def usage
$stderr.puts("\n" + " Usage: #{File.basename($0)} <options>\n" + $args.usage)
exit
end
def show_format_list
$stderr.puts("Supported formats:\n")
$stderr.puts(" raw raw binary data\n")
$stderr.puts(" windbg output from windbg's \"db\" command\n")
$stderr.puts(" gdb output from gdb's \"x/bx\" command\n")
$stderr.puts(" hex hex bytes like \"\\xFF\\x41\" or \"eb fe\"\n")
end
def debug_buffer(name, buf)
str = "\n#{buf.length} bytes of "
str << name
str += ":" if buf.length > 0
str += "\n\n"
$stderr.puts str
if buf.length > 0
$stderr.puts Rex::Text.to_hex_dump(buf)
end
end
# Input defaults
badchars = ''
fmt = 'raw'
input = $stdin
# Output
new_badchars = ''
# Parse the argument and rock it
$args.parse(ARGV) { |opt, idx, val|
case opt
when "-i"
begin
input = File.new(val)
rescue
$stderr.puts(OutError + "Failed to open file #{val}: #{$!}")
exit
end
when "-b"
badchars = Rex::Text.hex_to_raw(val)
when "-t"
if (val =~ /^(raw|windbg|gdb|hex)$/)
fmt = val
else
if val.nil? or val.length < 1
show_format_list
else
$stderr.puts(OutError + "Invalid format: #{val}")
end
exit
end
when "-h"
usage
end
}
if input == $stdin
$stderr.puts(OutStatus + "Please paste the memory contents in \"" + fmt + "\" format below (end with EOF):\n")
end
# Working data set
from_msf = Rex::Text.charset_exclude(badchars)
from_dbg = ''
# Process the input
from_dbg = input.read
case fmt
when "raw"
# this should already be in the correct format :)
when "windbg"
translated = ''
from_dbg.each_line do |ln|
translated << ln.chomp[10,47].gsub!(/(-| )/, '')
end
from_dbg = Rex::Text.hex_to_raw(translated)
when "gdb"
translated = ''
from_dbg.each_line do |ln|
translated << ln.chomp.split(':')[1].gsub!(/0x/, '\x').gsub!(/ /, '')
end
from_dbg = Rex::Text.hex_to_raw(translated)
when "hex"
translated = ''
from_dbg.each_line do |ln|
translated << ln.chomp.gsub!(/ /,'')
end
from_dbg = Rex::Text.hex_to_raw(translated)
end
=begin
# Uncomment these to debug stuff ..
debug_buffer("BadChars", badchars)
debug_buffer("memory contents", from_dbg)
debug_buffer("Rex::Text.charset_exclude() output", from_msf)
=end
# Find differences between the two data sets
from_msf = from_msf.unpack('C*')
from_dbg = from_dbg.unpack('C*')
minlen = from_msf.length
minlen = from_dbg.length if from_dbg.length < minlen
(0..(minlen-1)).each do |idx|
ch1 = from_msf[idx]
ch2 = from_dbg[idx]
if ch1 != ch2
str = "Byte at index 0x%04x differs (0x%02x became 0x%02x)" % [idx, ch1, ch2]
$stderr.puts OutStatus + str
new_badchars << ch1
end
end
# show the results
if new_badchars.length < 1
$stderr.puts(OutStatus + "All characters matched, no new bad characters discovered.")
else
$stderr.puts(OutStatus + "Proposed BadChars: \"" + Rex::Text.to_hex(new_badchars) + "\"")
end