Skip to content

Commit cf52dd8

Browse files
committed
Refactor search
1 parent 2fa5223 commit cf52dd8

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

modules/post/windows/gather/enum_ad_users_to_wordlist.rb

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,54 +67,42 @@ def run
6767

6868
return if q.nil? || q[:results].empty?
6969

70-
wordlist = {}
70+
@words_dict = {}
7171
q[:results].each do |result|
7272
result.each do |field|
73-
next unless field.present?
74-
next if field =~ /^\s*$/ or field == '-' or field == '' or field.length < 3
75-
76-
field.gsub!(/[\(\)\"]/, '') # clear up common punctuation in descriptions
77-
field.downcase! # clear up case
78-
79-
tmp = []
80-
parts = field.split(/\s+/)
81-
tmp = tmp + parts + [ parts.join ] unless parts.empty?
82-
parts = field.split('-')
83-
tmp = tmp + parts + [ parts.join ] unless parts.empty?
84-
parts = field.split(',')
85-
tmp = tmp + parts + [ parts.join ] unless parts.empty?
86-
parts = field.split('+')
87-
tmp = tmp + parts + [ parts.join ] unless parts.empty?
88-
89-
# add the entire field if its not too long
90-
wordlist[field] += 1 if field.length < 24
91-
92-
if tmp.length > 0
93-
tmp = tmp.flatten
94-
tmp.each do |r|
95-
next if r.length < 3 or r.length > 24
96-
# sub fields can still have unwanted characters due to not chained if (ie, it has dashes and commas)
97-
r.gsub!(/[\s\,\-\+]/, '')
98-
wordlist[r] += 1 if r.length < 24
99-
end
100-
end
73+
search_words(field)
10174
end # result.each
10275
end # q.each
10376

10477
# build array of words to output sorted on frequency
105-
out = []
106-
s = wordlist.sort_by &:last
107-
s.each do |k, v|
108-
if(k.length > 3)
109-
out.push(k)
110-
# print_status("#{k} ==> #{v}")
111-
end
112-
end
78+
output = []
79+
ordered_dict = @words_dict.sort_by { |k,v| v }.reverse
80+
ordered_dict.collect! { |k, v| k }
81+
11382
wordlist_file = Rex::Quickfile.new("wordlist")
114-
wordlist_file.write( out.flatten.uniq.join("\n") + "\n" )
115-
print_status("Seeded the password database with #{out.length} words into #{wordlist_file.path}...")
83+
wordlist_file.write(ordered_dict.join("\n") + "\n")
84+
print_status("Seeded the password database with #{output.length} words into #{wordlist_file.path}...")
11685
wordlist_file.close
86+
end
87+
88+
def search_words(field)
89+
return if field.blank?
90+
return if field =~ /^\s*$/ || field.length < 3
11791

92+
field.gsub!(/[\(\)\"]/, '') # clear up common punctuation in descriptions
93+
field.downcase! # clear up case
94+
95+
words = field.split(/\s+|=|\/|,|\+/)
96+
return if words.empty?
97+
98+
words.each do |word|
99+
next if word.length < 3 || word.length > 24
100+
if @words_dict[word]
101+
@words_dict[word] += 1
102+
else
103+
@words_dict[word] = 1
104+
end
105+
end
118106
end
119107
end
120108

0 commit comments

Comments
 (0)