Skip to content

Commit c682bb6

Browse files
author
HD Moore
committed
Try harder for non-exact matches
1 parent f5c7f4c commit c682bb6

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

tools/pattern_offset.rb

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,51 @@
2929
buffer = Rex::Text.pattern_create(len.to_i)
3030

3131
offset = Rex::Text.pattern_offset(buffer, value)
32+
33+
# Handle cases where there is no match by looking for "close" matches
34+
unless offset
35+
found = false
36+
$stderr.puts "[*] No exact matches, looking for likely candidates..."
37+
38+
# Look for shifts by a single byte
39+
0.upto(3) do |idx|
40+
0.upto(255) do |c|
41+
nvb = [value].pack("V")
42+
nvb[idx, 1] = [c].pack("C")
43+
nvi = nvb.unpack("V").first
44+
45+
off = Rex::Text.pattern_offset(buffer, nvi)
46+
if off
47+
mle = value - buffer[off,4].unpack("V").first
48+
mbe = value - buffer[off,4].unpack("N").first
49+
puts "[+] Possible match at offset #{off} (adjusted [ little-endian: #{mle} | big-endian: #{mbe} ] ) byte offset #{idx}"
50+
found = true
51+
end
52+
end
53+
end
54+
55+
exit if found
56+
57+
# Look for 16-bit offsets
58+
[0, 2].each do |idx|
59+
0.upto(65535) do |c|
60+
nvb = [value].pack("V")
61+
nvb[idx, 2] = [c].pack("v")
62+
nvi = nvb.unpack("V").first
63+
64+
off = Rex::Text.pattern_offset(buffer, nvi)
65+
if off
66+
mle = value - buffer[off,4].unpack("V").first
67+
mbe = value - buffer[off,4].unpack("N").first
68+
puts "[+] Possible match at offset #{off} (adjusted [ little-endian: #{mle} | big-endian: #{mbe} ] )"
69+
found = true
70+
end
71+
end
72+
end
73+
74+
end
75+
3276
while offset
33-
puts offset
77+
puts "[*] Exact match at offset #{offset}"
3478
offset = Rex::Text.pattern_offset(buffer, value, offset + 1)
3579
end

0 commit comments

Comments
 (0)