@@ -46,14 +46,14 @@ def initialize(info = {})
46
46
OptString . new ( 'OUTFILE' , [ false , 'A filename to store the list of IPs' ] ) ,
47
47
OptBool . new ( 'DATABASE' , [ false , 'Add search results to the database' , false ] ) ,
48
48
OptInt . new ( 'MAXPAGE' , [ true , 'Max amount of pages to collect' , 1 ] ) ,
49
- OptString . new ( 'FILTER' , [ false , 'Search for a specific IP/City/Country/Hostname' ] )
49
+ OptRegexp . new ( 'REGEX' , [ true , 'Regex search for a specific IP/City/Country/Hostname' , '.*' ] )
50
+
50
51
] , self . class )
51
52
end
52
53
53
54
# create our Shodan query function that performs the actual web request
54
55
def shodan_query ( query , apikey , page )
55
56
# send our query to Shodan
56
-
57
57
uri = URI . parse ( 'https://api.shodan.io/shodan/host/search?query=' +
58
58
Rex ::Text . uri_encode ( query ) + '&key=' + apikey + '&page=' + page . to_s )
59
59
http = Net ::HTTP . new ( uri . host , uri . port )
@@ -106,7 +106,6 @@ def run
106
106
107
107
if results [ page ] [ 'total' ] == 0
108
108
print_error ( 'No Results Found!' )
109
- return
110
109
end
111
110
112
111
# Determine page count based on total results
@@ -118,14 +117,14 @@ def run
118
117
end
119
118
120
119
# start printing out our query statistics
121
- print_status ( "Total: #{ results [ page ] [ 'total' ] } on #{ tpages } " \
120
+ print_status ( "Total: #{ results [ page ] [ 'total' ] } on #{ tpages } " +
122
121
"pages. Showing: #{ maxpage } page(s)" )
123
122
124
123
# If search results greater than 100, loop & get all results
125
124
print_status ( 'Collecting data, please wait...' )
126
125
if results [ page ] [ 'total' ] > 100
127
126
page += 1
128
- while page <= tpages
127
+ while page <= maxpage
129
128
break if page > datastore [ 'MAXPAGE' ]
130
129
results [ page ] = shodan_query ( query , apikey , page )
131
130
page += 1
@@ -140,50 +139,45 @@ def run
140
139
)
141
140
142
141
# Organize results and put them into the table and database
143
- page = 1
144
- #my_filter = Regexp.new(datastore['FILTER'], true) if datastore['FILTER']
145
- my_filter = datastore [ 'FILTER' ]
146
- print_status ( "page: #{ page } " )
147
- print_status ( "tpages: #{ tpages } " )
148
- pages = page ..tpages
149
- pages . each do |i |
150
- next if results [ i ] . nil? or results [ i ] [ 'matches' ] . nil?
151
- print_status ( "i is: #{ i } " )
152
- results [ i ] [ 'matches' ] . each do |host |
153
-
142
+ p = 1
143
+ regex = datastore [ 'REGEX' ] if datastore [ 'REGEX' ]
144
+ while p <= maxpage
145
+ break if p > maxpage
146
+ results [ p ] [ 'matches' ] . each do |host |
154
147
city = host [ 'location' ] [ 'city' ] || 'N/A'
155
148
ip = host [ 'ip_str' ] || 'N/A'
156
149
port = host [ 'port' ] || ''
157
150
country = host [ 'location' ] [ 'country_name' ] || 'N/A'
158
151
hostname = host [ 'hostnames' ] [ 0 ]
159
152
data = host [ 'data' ]
160
153
161
- report_host ( :host => ip ,
162
- :name => hostname ,
154
+ report_host ( :host => ip ,
155
+ :name => hostname ,
163
156
:comments => 'Added from Shodan' ,
164
- :info => host [ 'info' ]
157
+ :info => host [ 'info' ]
165
158
) if datastore [ 'DATABASE' ]
166
159
167
- report_service ( :host => ip ,
160
+ report_service ( :host => ip ,
168
161
:port => port ,
169
162
:info => 'Added from Shodan'
170
163
) if datastore [ 'DATABASE' ]
171
164
172
- if ip =~ / #{ my_filter } / or
173
- city =~ / #{ my_filter } /i or
174
- country =~ / #{ my_filter } /i or
175
- hostname =~ / #{ my_filter } /i or
176
- data =~ / #{ my_filter } /i
165
+ if ip =~ regex ||
166
+ city =~ regex ||
167
+ country =~ regex ||
168
+ hostname =~ regex ||
169
+ data =~ regex
177
170
# Unfortunately we cannot display the banner properly,
178
171
# because it messes with our output format
179
172
tbl << [ "#{ ip } :#{ port } " , city , country , hostname ]
180
173
end
181
174
end
175
+ p += 1
182
176
end
183
177
184
178
# Show data and maybe save it if needed
185
179
print_line
186
180
print_line ( "#{ tbl } " )
187
- save_output ( tbl ) if not datastore [ 'OUTFILE' ] . nil?
181
+ save_output ( tbl ) if datastore [ 'OUTFILE' ]
188
182
end
189
183
end
0 commit comments