Skip to content

Instantly share code, notes, and snippets.

@harunpehlivan
Forked from Ninjex/main.rb
Created January 10, 2018 10:24
Show Gist options
  • Save harunpehlivan/e91017b503d4d8f741cfe40e3476559e to your computer and use it in GitHub Desktop.
Save harunpehlivan/e91017b503d4d8f741cfe40e3476559e to your computer and use it in GitHub Desktop.
Hackthisite Programming 2 (Requires Mechanize and RMagick gems) -- Autocompletion
#!/usr/bin/ruby
# ███▄ █ ██▓ ███▄ █ ▄▄▄██▀▀▀▓█████ ▒██ ██▒
# ██ ▀█ █ ▓██▒ ██ ▀█ █ ▒██ ▓█ ▀ ▒▒ █ █ ▒░
# ▓██ ▀█ ██▒▒██▒▓██ ▀█ ██▒ ░██ ▒███ ░░ █ ░
# ▓██▒ ▐▌██▒░██░▓██▒ ▐▌██▒▓██▄██▓ ▒▓█ ▄ ░ █ █ ▒
# ▒██░ ▓██░░██░▒██░ ▓██░ ▓███▒ ░▒████▒▒██▒ ▒██▒
# ░ ▒░ ▒ ▒ ░▓ ░ ▒░ ▒ ▒ ▒▓▒▒░ ░░ ▒░ ░▒▒ ░ ░▓ ░
# ░ ░░ ░ ▒░ ▒ ░░ ░░ ░ ▒░ ▒ ░▒░ ░ ░ ░░░ ░▒ ░
# ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
# ░ ░ ░ ░ ░ ░ ░ ░ ░
#
require_relative 'morse.rb'
require 'mechanize'
require 'RMagick'
require 'io/console'
print 'Username: '
name = gets.chomp
print 'Password: '
pass = STDIN.noecho(&:gets).chomp
client = Mechanize.new { |agent| agent.user_agent = 'Mechanize -- Ninjex' }
client.get('https://www.hackthissite.org/') do |page|
login_form = client.click(page.link_with(:text => /Login/))
login_page = login_form.form_with(:action => '/user/login') do |f|
f.username = name
f.password = pass
end.click_button
end
client.get('https://www.hackthissite.org/missions/prog/2/') do |page|
client.get('https://www.hackthissite.org/missions/prog/2/PNG').save('encrypted.png')
img = Magick::Image::read("encrypted.png")[0]
offset, count = [], 0
img.each_pixel do |pixel|
if pixel.to_s.include?('red=65535, green=65535, blue=65535')
offset << count
count = 0
end
count += 1
end
answer = decrypt_morse(offset.map!(&:chr).join)
submit_form = page.form_with(:action => '/missions/prog/2/index.php') { |f| f.solution = answer }.click_button
puts "\r\nSubmited Answer: #{answer}"
puts "Verify points @ https://www.hackthissite.org/user/view/#{name}"
end
File.delete('encrypted.png')
def decrypt_morse(cipher)
morse = {
".-" => 'A', "-..." => 'B', "-.-." => 'C', "-.." => 'D',
"." => 'E', "..-." => 'F', "--." => 'G', "...." => 'H',
".." => 'I', ".---" => 'J', "-.-" => 'K', ".-.." => 'L',
"--" => 'M', "-." => 'N', "---" => 'O', ".--." => 'P',
"--.-" => 'Q', ".-." => 'R', "..." => 'S', "-" => 'T',
"..-" => 'U', "...-" => 'V', ".--" => 'W', "-..-" => 'X',
"-.--" => 'Y', "--.." => 'Z', ".----" => '1', "..---" => '2',
"...--" => '3', "....-" => '4', "....." => '5', "-...." => '6',
"--..." => '7', "---.." => '8', "----." => '9', "-----" => '0',
'/' => ' ',
}
plain = []
cipher.split(' ').map{ |crypt| plain << morse[crypt] }
return plain.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment