Skip to content

Instantly share code, notes, and snippets.

@epitron
Last active August 29, 2015 14:05
Show Gist options
  • Save epitron/bf844793bd7cc21c9212 to your computer and use it in GitHub Desktop.
Save epitron/bf844793bd7cc21c9212 to your computer and use it in GitHub Desktop.
Figlet-based CAPTCHA breaker (for IRC bots) -- accelerated!
require 'epitools'
figlet_left = File.readlines('./figlet.input')
def get_first_char(i, arr)
arr.map { |x| x[0..i] }
end
def remove_first_char(i, arr)
arr.map { |x| x.slice(i..-1) }
end
def find_figlet(arr)
'A'.upto('Z').detect do |l|
`figlet -f big -w 11 #{l}`.split("\n").reject {|x| x.strip.empty?} == arr
end
end
time {
res = ''
while !figlet_left.map(&:strip).reject(&:empty?).empty? do
i = 5
while !(x = find_figlet(get_first_char(i, figlet_left))) do
i += 1
abort("Partial result:#{res}") if i > 9
end
figlet_left = remove_first_char(i + 1, figlet_left)
res << x
end
puts res
}
require 'epitools'
require 'digest/sha1'
def hash_fig(arr)
Digest::SHA1.hexdigest(arr.join("\n"))
end
def columnize(arr)
arr.map(&:chars).transpose
end
ALPHABET = [*'A'..'Z']
FIGS = `figlet -f big -w 11 "#{ALPHABET.join("\n")}"`.split("\n").each_slice(8).map { |fig| columnize(fig[0..5]) }
LUT = Hash[ FIGS.map {|fig| hash_fig(fig) }.zip(ALPHABET) ]
figlet = columnize(File.readlines('figlet.input'))
time {
result = ""
pos = 5
until figlet.empty? or pos > figlet.size
hash = hash_fig(figlet[0...pos])
if x = LUT[hash]
result << x
figlet = figlet[pos..-1] # drop the letter
pos = 5
else
pos += 1
end
end
puts result
}
@epitron
Copy link
Author

epitron commented Aug 30, 2014

$ ruby unfiglet-dorei-slow.rb 
LVUIJQ
elapsed time: 1.11940s
$ ruby unfiglet-epi-fast.rb 
LVUIJQ
elapsed time: 0.00059s

:D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment