|
29 | 29 | buffer = Rex::Text.pattern_create(len.to_i)
|
30 | 30 |
|
31 | 31 | 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 | + |
32 | 76 | while offset
|
33 |
| - puts offset |
| 77 | + puts "[*] Exact match at offset #{offset}" |
34 | 78 | offset = Rex::Text.pattern_offset(buffer, value, offset + 1)
|
35 | 79 | end
|
0 commit comments