# Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 2.65 KB  |  hits: 14  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #!/usr/bin/env ruby
2.
3.
4. def pattern_generator(length, set = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a)
5.         parts = []
6.
7.         signal_set = set.slice!(0, set.length / 4)
8.
9.         set.each {|a| set.each {|b| set.each {|c| signal_set.each {|d|
10.                 return parts[0, length].join if parts.length > length
11.                 parts << a << b << c << d
12.         }}}}
13.
14.         return "Out of combinations"
15. end
16.
17. def pattern_gen(set = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a)
18.         signal_set = set.slice!(0, set.length / 4)
19.
20.         set.each {|a| set.each {|b| set.each {|c| signal_set.each {|d|
21.                 yield a
22.                 yield b
23.                 yield c
24.                 yield d
25.         }}}}
26. end
27.
28. def digitize_sets(length, sets)
29.         state = Array.new(sets.length) {0}
30.         rev_sets = sets.reverse
31.
32.         result = []
33.         while result.length < length do
34.
35.                 carry = false
36.                 state.length.times {|i|
37.                         state[i] += 1 and carry = false if carry
38.
39.                         if state[i] >= rev_sets[i].length then
40.                                 state[i] = 0
41.                                 carry = true
42.                         end
43.                 }
44.
45.                 raise "Exhausted sets" if carry
46.
47.                 (state.length-1).downto(0) {|i|
48.                         result << rev_sets[i][state[i]]
49.                 }
50.
51.                 state[0] += 1
52.         end
53.
54.         return result[0, length].join
55. end
56.
57. def digitize_sets2(length, sets)
58.         parts = []
59.         rev_sets = sets.reverse
60.
61.         combinator_output = Proc.new {|pre|
62.                 parts.concat(pre)
63.                 return parts[0, length].join if parts.length > length
64.         }
65.
66.         combinators = [combinator_output]
67.
68.         sets.length.times {|i|
69.                 combinators.push(Proc.new {|pre|
70.                         rev_sets[i].each {|p|
71.                                 combinators[i].call(pre + [p])
72.                         }
73.                 })
74.         }
75.
76.         combinators[-1].call([])
77.
78.         return "Out of combinations"
79. end
80.
81. def pattern_generator2(length, set = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a)
82.         signal_set = set.slice!(0, set.length / 4)
83.         sets = [set, set, set, signal_set]
84.
85.         return digitize_sets(length, sets)
86. end
87.
88. def pattern_generator3(length, set = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a)
89.         signal_set = set.slice!(0, set.length / 4)
90.         sets = [set, set, set, signal_set]
91.
92.         return digitize_sets2(length, sets)
93. end
94.
95. require '/opt/metasploit3/msf3/lib/rex/text.rb'
96. def pattern_generator4(length, set = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a)
97.         signal_set = set.slice!(0, set.length / 4).join
98.         set = set.join
99.
100.         sets = [set, set, set, signal_set]
101.
102.         return Rex::Text::pattern_create(length, sets)
103. end
104.
105. require 'benchmark'
106.
107. def benchmark(length = 1024*1024)
108.         template = pattern_generator(length)
109.         Benchmark.bm do |x|
110.                 x.report { puts template == pattern_generator(length) }
111.                 x.report { puts template == pattern_generator2(length) }
112.                 x.report { puts template == pattern_generator3(length) }
113.                 x.report { puts template == pattern_generator4(length) }
114.         end
115. end