5
5
msfbase = File . expand_path ( File . readlink ( msfbase ) , File . dirname ( msfbase ) )
6
6
end
7
7
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' ]
10
10
11
11
require 'msfenv'
12
12
require 'msf/core'
@@ -22,16 +22,17 @@ def self.parse(args)
22
22
opt . banner = "Usage: #{ __FILE__ } [options]\n Example: #{ __FILE__ } -q Aa3A|0x39634138|0xFFFF4138\n [*] Exact match at offset 9"
23
23
opt . separator ''
24
24
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
27
28
end
28
29
29
30
opt . on ( '-l' , '--length <length>' , Integer , "The length of the pattern" ) do |len |
30
31
options [ :length ] = len
31
- end
32
+ end
32
33
33
34
opt . on ( '-s' , '--sets <ABC,def,123>' , Array , "Custom Pattern Sets" ) do |sets |
34
- options [ :sets ] = sets
35
+ options [ :sets ] = sets
35
36
end
36
37
37
38
opt . on_tail ( '-h' , '--help' , 'Show this message' ) do
@@ -57,7 +58,7 @@ def self.parse(args)
57
58
end
58
59
end
59
60
60
- class Driver
61
+ class Driver
61
62
def initialize
62
63
begin
63
64
@opts = OptsConsole . parse ( ARGV )
@@ -69,69 +70,66 @@ def initialize
69
70
70
71
def run
71
72
query = ( @opts [ :query ] )
72
- pattern = Rex ::Text . pattern_create ( @opts [ :length ] , @opts [ :sets ] )
73
73
74
-
75
- if ( query . length >= 8 and query . hex > 0 )
74
+ if query . length >= 8 && query . hex > 0
76
75
query = query . hex
77
76
# However, you can also specify a four-byte string
78
- elsif ( query . length == 4 )
77
+ elsif query . length == 4
79
78
query = query . unpack ( "V" ) . first
80
79
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
82
81
query = query . to_i ( 16 )
83
82
end
84
83
85
84
buffer = Rex ::Text . pattern_create ( @opts [ :length ] , @opts [ :sets ] )
86
85
offset = Rex ::Text . pattern_offset ( buffer , query )
87
86
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
129
128
130
129
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 )
133
132
end
134
-
135
133
end
136
134
end
137
135
end
@@ -140,7 +138,7 @@ def run
140
138
driver = PatternOffset ::Driver . new
141
139
begin
142
140
driver . run
143
- rescue ::Exception => e
141
+ rescue ::StandardError => e
144
142
elog ( "#{ e . class } : #{ e . message } \n #{ e . backtrace * "\n " } " )
145
143
$stderr. puts "[x] #{ e . class } : #{ e . message } "
146
144
$stderr. puts "[*] If necessary, please refer to framework.log for more details."
0 commit comments