Skip to content

Instantly share code, notes, and snippets.

@bestie
Last active August 3, 2018 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bestie/9cd1efc5b4e00990aee3dc5006536cda to your computer and use it in GitHub Desktop.
Save bestie/9cd1efc5b4e00990aee3dc5006536cda to your computer and use it in GitHub Desktop.
Prison Break
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment