# Run this script with the ENV var payload. # Payload will be executed as part of your request to visit a prisoner. # The goal is to free a prison in as few characters as possible. module PrisonBreak class Visit attr_accessor :free_prisoner attr_reader :prison, :payload DISABLED_SYMBOLS = %w{, ` ( ? ! + << %x @} DISABLED_WORDS = %w{send eval system exec popen rm puts require new load create file include free call push concat methods irb chr instance} GUARD_RE = Regexp.new((DISABLED_SYMBOLS + DISABLED_WORDS).map {|i| Regexp.escape(i) }.join('|')) def initialize(prison, payload) @prison = prison @payload = payload end def secure? if !GUARD_RE.match(payload).nil? raise "Unpermitted item: #{Regexp.last_match(0)}" end true end def perform puts "perform" puts payload instance_eval(payload) end end class Prison def initialize @cells = { 11 => ['Edmond Dantès'], 22 => ['Henri Charrière'], 33 => ['Michael Scofield'] }.freeze end def empty_cell? cells.values.any? &:empty? end private attr_reader :cells def unlock(cell, password, guest) puts "unlock called" if password == 'secret' puts "unlocking" guest.free_prisoner = cells[cell].shift else puts "nope" end end end end prison = PrisonBreak::Prison.new visit = PrisonBreak::Visit.new(prison, ENV.fetch("PAYLOAD")) # <= your payload goes here visit.perform if visit.secure? success = prison.empty_cell? && !visit.free_prisoner.nil? if success puts "Yes! You freed `#{visit.free_prisoner}`. Payload was #{ENV.fetch("PAYLOAD").length} chars" else puts "Nooooo! Your shenanigans were dedected." end