Skip to content

Commit 3281781

Browse files
committed
Addressed r7 comments, fixed bug in results loop
1 parent 0a27a18 commit 3281781

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

modules/auxiliary/gather/shodan_search.rb

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ def initialize(info = {})
4646
OptString.new('OUTFILE', [false, 'A filename to store the list of IPs']),
4747
OptBool.new('DATABASE', [false, 'Add search results to the database', false]),
4848
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+
5051
], self.class)
5152
end
5253

5354
# create our Shodan query function that performs the actual web request
5455
def shodan_query(query, apikey, page)
5556
# send our query to Shodan
56-
5757
uri = URI.parse('https://api.shodan.io/shodan/host/search?query=' +
5858
Rex::Text.uri_encode(query) + '&key=' + apikey + '&page=' + page.to_s)
5959
http = Net::HTTP.new(uri.host, uri.port)
@@ -106,7 +106,6 @@ def run
106106

107107
if results[page]['total'] == 0
108108
print_error('No Results Found!')
109-
return
110109
end
111110

112111
# Determine page count based on total results
@@ -118,14 +117,14 @@ def run
118117
end
119118

120119
# start printing out our query statistics
121-
print_status("Total: #{results[page]['total']} on #{tpages} "\
120+
print_status("Total: #{results[page]['total']} on #{tpages} " +
122121
"pages. Showing: #{maxpage} page(s)")
123122

124123
# If search results greater than 100, loop & get all results
125124
print_status('Collecting data, please wait...')
126125
if results[page]['total'] > 100
127126
page += 1
128-
while page <= tpages
127+
while page <= maxpage
129128
break if page > datastore['MAXPAGE']
130129
results[page] = shodan_query(query, apikey, page)
131130
page += 1
@@ -140,50 +139,45 @@ def run
140139
)
141140

142141
# 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|
154147
city = host['location']['city'] || 'N/A'
155148
ip = host['ip_str'] || 'N/A'
156149
port = host['port'] || ''
157150
country = host['location']['country_name'] || 'N/A'
158151
hostname = host['hostnames'][0]
159152
data = host['data']
160153

161-
report_host(:host => ip,
162-
:name => hostname,
154+
report_host(:host => ip,
155+
:name => hostname,
163156
:comments => 'Added from Shodan',
164-
:info => host['info']
157+
:info => host['info']
165158
) if datastore['DATABASE']
166159

167-
report_service(:host => ip,
160+
report_service(:host => ip,
168161
:port => port,
169162
:info => 'Added from Shodan'
170163
) if datastore['DATABASE']
171164

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
177170
# Unfortunately we cannot display the banner properly,
178171
# because it messes with our output format
179172
tbl << ["#{ip}:#{port}", city, country, hostname]
180173
end
181174
end
175+
p += 1
182176
end
183177

184178
# Show data and maybe save it if needed
185179
print_line
186180
print_line("#{tbl}")
187-
save_output(tbl) if not datastore['OUTFILE'].nil?
181+
save_output(tbl) if datastore['OUTFILE']
188182
end
189183
end

0 commit comments

Comments
 (0)