forked from nahamsec/lazys3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazys3.rb
101 lines (82 loc) · 2.23 KB
/
lazys3.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby
require 'net/http'
require 'timeout'
class String
def red; "\e[31m#{self}\e[0m" end
end
class S3
attr_reader :bucket, :domain, :code
def initialize(bucket)
@bucket = bucket
@domain = format('http://%s.s3.amazonaws.com', bucket)
end
def exists?
code != 404
end
def code
http && http.code.to_i
end
private
def http
Timeout::timeout(5) do
@http ||= Net::HTTP.get_response(URI.parse(@domain))
end
rescue
end
end
class Scanner
def initialize(list)
@list = list
end
def scan
@list.each do |word|
bucket = S3.new word
if bucket.exists?
puts "Found bucket: #{bucket.bucket} (#{bucket.code})".red
end
end
end
end
class Wordlist
ENVIRONMENTS = %w(dev development stage s3 staging prod production test)
PERMUTATIONS = %i(permutation_raw permutation_envs permutation_host)
class << self
def generate(common_prefix, prefix_wordlist)
[].tap do |list|
PERMUTATIONS.each do |permutation|
list << send(permutation, common_prefix, prefix_wordlist)
end
end.flatten.uniq
end
def from_file(prefix, file)
generate(prefix, IO.read(file).split("\n"))
end
def permutation_raw(common_prefix, _prefix_wordlist)
common_prefix
end
def permutation_envs(common_prefix, prefix_wordlist)
[].tap do |permutations|
prefix_wordlist.each do |word|
ENVIRONMENTS.each do |environment|
['%s-%s-%s', '%s-%s.%s', '%s-%s%s', '%s.%s-%s', '%s.%s.%s'].each do |bucket_format|
permutations << format(bucket_format, common_prefix, word, environment)
end
end
end
end
end
def permutation_host(common_prefix, prefix_wordlist)
[].tap do |permutations|
prefix_wordlist.each do |word|
['%s.%s', '%s-%s', '%s%s'].each do |bucket_format|
permutations << format(bucket_format, common_prefix, word)
permutations << format(bucket_format, word, common_prefix)
end
end
end
end
end
end
wordlist = Wordlist.from_file(ARGV[0], '~/common_bucket_prefixes.txt')
puts "Generated wordlist from file, #{wordlist.length} items..."
Scanner.new(wordlist).scan