Skip to content

Commit 7cfc4d4

Browse files
author
Brent Cook
committed
fix odd indentation and style issues
1 parent 901b793 commit 7cfc4d4

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

tools/exploit/pattern_offset.rb

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
66
end
77

8-
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
9-
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
8+
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
9+
$LOAD_PATH.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
1010

1111
require 'msfenv'
1212
require 'msf/core'
@@ -22,16 +22,17 @@ def self.parse(args)
2222
opt.banner = "Usage: #{__FILE__} [options]\nExample: #{__FILE__} -q Aa3A|0x39634138|0xFFFF4138\n[*] Exact match at offset 9"
2323
opt.separator ''
2424
opt.separator 'Options:'
25-
opt.on('-q', '--query Aa0A', String, "Query to Locate") do |query|
26-
options[:query] = query
25+
26+
opt.on('-q', '--query Aa0A', String, "Query to Locate") do |query|
27+
options[:query] = query
2728
end
2829

2930
opt.on('-l', '--length <length>', Integer, "The length of the pattern") do |len|
3031
options[:length] = len
31-
end
32+
end
3233

3334
opt.on('-s', '--sets <ABC,def,123>', Array, "Custom Pattern Sets") do |sets|
34-
options[:sets] = sets
35+
options[:sets] = sets
3536
end
3637

3738
opt.on_tail('-h', '--help', 'Show this message') do
@@ -57,7 +58,7 @@ def self.parse(args)
5758
end
5859
end
5960

60-
class Driver
61+
class Driver
6162
def initialize
6263
begin
6364
@opts = OptsConsole.parse(ARGV)
@@ -69,69 +70,66 @@ def initialize
6970

7071
def run
7172
query = (@opts[:query])
72-
pattern = Rex::Text.pattern_create(@opts[:length], @opts[:sets])
7373

74-
75-
if (query.length >= 8 and query.hex > 0)
74+
if query.length >= 8 && query.hex > 0
7675
query = query.hex
7776
# However, you can also specify a four-byte string
78-
elsif (query.length == 4)
77+
elsif query.length == 4
7978
query = query.unpack("V").first
8079
else
81-
# Or even a hex query that isn't 8 bytes long
80+
# Or even a hex query that isn't 8 bytes long
8281
query = query.to_i(16)
8382
end
8483

8584
buffer = Rex::Text.pattern_create(@opts[:length], @opts[:sets])
8685
offset = Rex::Text.pattern_offset(buffer, query)
8786

88-
# Handle cases where there is no match by looking for "close" matches
89-
unless offset
90-
found = false
91-
$stderr.puts "[*] No exact matches, looking for likely candidates..."
92-
93-
# Look for shifts by a single byte
94-
0.upto(3) do |idx|
95-
0.upto(255) do |c|
96-
nvb = [query].pack("V")
97-
nvb[idx, 1] = [c].pack("C")
98-
nvi = nvb.unpack("V").first
99-
100-
off = Rex::Text.pattern_offset(buffer, nvi)
101-
if off
102-
mle = query - buffer[off,4].unpack("V").first
103-
mbe = query - buffer[off,4].unpack("N").first
104-
puts "[+] Possible match at offset #{off} (adjusted [ little-endian: #{mle} | big-endian: #{mbe} ] ) byte offset #{idx}"
105-
found = true
106-
end
107-
end
108-
end
109-
110-
exit! if found
111-
112-
# Look for 16-bit offsets
113-
[0, 2].each do |idx|
114-
0.upto(65535) do |c|
115-
nvb = [query].pack("V")
116-
nvb[idx, 2] = [c].pack("v")
117-
nvi = nvb.unpack("V").first
118-
119-
off = Rex::Text.pattern_offset(buffer, nvi)
120-
if off
121-
mle = query - buffer[off,4].unpack("V").first
122-
mbe = query - buffer[off,4].unpack("N").first
123-
puts "[+] Possible match at offset #{off} (adjusted [ little-endian: #{mle} | big-endian: #{mbe} ] )"
124-
found = true
125-
end
126-
end
127-
end
128-
end
87+
# Handle cases where there is no match by looking for "close" matches
88+
unless offset
89+
found = false
90+
$stderr.puts "[*] No exact matches, looking for likely candidates..."
91+
92+
# Look for shifts by a single byte
93+
0.upto(3) do |idx|
94+
0.upto(255) do |c|
95+
nvb = [query].pack("V")
96+
nvb[idx, 1] = [c].pack("C")
97+
nvi = nvb.unpack("V").first
98+
99+
off = Rex::Text.pattern_offset(buffer, nvi)
100+
if off
101+
mle = query - buffer[off, 4].unpack("V").first
102+
mbe = query - buffer[off, 4].unpack("N").first
103+
puts "[+] Possible match at offset #{off} (adjusted [ little-endian: #{mle} | big-endian: #{mbe} ] ) byte offset #{idx}"
104+
found = true
105+
end
106+
end
107+
end
108+
109+
exit! if found
110+
111+
# Look for 16-bit offsets
112+
[0, 2].each do |idx|
113+
0.upto(65535) do |c|
114+
nvb = [query].pack("V")
115+
nvb[idx, 2] = [c].pack("v")
116+
nvi = nvb.unpack("V").first
117+
118+
off = Rex::Text.pattern_offset(buffer, nvi)
119+
if off
120+
mle = query - buffer[off, 4].unpack("V").first
121+
mbe = query - buffer[off, 4].unpack("N").first
122+
puts "[+] Possible match at offset #{off} (adjusted [ little-endian: #{mle} | big-endian: #{mbe} ] )"
123+
found = true
124+
end
125+
end
126+
end
127+
end
129128

130129
while offset
131-
puts "[*] Exact match at offset #{offset}"
132-
offset = Rex::Text.pattern_offset(buffer, query, offset + 1)
130+
puts "[*] Exact match at offset #{offset}"
131+
offset = Rex::Text.pattern_offset(buffer, query, offset + 1)
133132
end
134-
135133
end
136134
end
137135
end
@@ -140,7 +138,7 @@ def run
140138
driver = PatternOffset::Driver.new
141139
begin
142140
driver.run
143-
rescue ::Exception => e
141+
rescue ::StandardError => e
144142
elog("#{e.class}: #{e.message}\n#{e.backtrace * "\n"}")
145143
$stderr.puts "[x] #{e.class}: #{e.message}"
146144
$stderr.puts "[*] If necessary, please refer to framework.log for more details."

0 commit comments

Comments
 (0)