Created
March 8, 2016 03:15
-
-
Save crowell/40040dd86f416b1cb533 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pty' | |
require 'fileutils' | |
def execmd cmd | |
begin | |
PTY.spawn(cmd) do |r, w, pid| | |
begin | |
r.each { |line| print line;} | |
rescue Errno::EIO | |
end | |
end | |
rescue PTY::ChildExited => e | |
puts "bootstrapped!" | |
end | |
end | |
def debootstrap ubuntu_version, challname | |
cmd = "debootstrap #{ubuntu_version} #{challname}_chroot" | |
execmd cmd | |
end | |
def add_user challname | |
cmd = "chroot #{challname}_chroot /bin/bash -c \"useradd -m -s /bin/bash #{challname}\"" | |
execmd cmd | |
end | |
def copy_task challname, flag | |
FileUtils.copy challname, "./#{challname}_chroot/home/#{challname}/#{challname}" | |
FileUtils.copy flag, "./#{challname}_chroot/home/#{challname}/#{flag}" | |
end | |
def write_shell_script challname, port | |
script = [] | |
script << "#!/bin/bash" | |
script << "socat TCP-LISTEN:#{port},fork,reuseaddr,chroot=./#{challname}_chroot,su=nobody EXEC:\"/home/#{challname}/#{challname}\"" | |
File.open("#{challname}_runner.sh", 'w') {|f| f.write script.join("\n")} | |
FileUtils.chmod "+x", "#{challname}_runner.sh" | |
end | |
if ARGV.length != 4 | |
puts "#{$0} ubuntu_version task_bin flag port" | |
end | |
ubuntu_version = ARGV[0] | |
challname = ARGV[1] | |
flag = ARGV[2] | |
port = ARGV[3] | |
debootstrap ubuntu_version, challname | |
add_user challname | |
copy_task challname, flag | |
write_shell_script challname, port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment