The Neverending Crypto Level 2

10

Hope you remember 1984, because you are
about to go on a journey.

How many levels can you get to?

Good luck.

nc 146.148.102.236 24069

Level 2は、a~yの入力に対して、以下の通り、n~z{|}~と半角スペースに戻って'(シングルクォーテーション)に暗号化されます。zは'(シングルクォーテーション)の次の(に暗号化されると思われます。したがって、-(ハイフン)だけが以上のルールから外れますので半角スペースに対応する暗号化と思われます。
"------------------------------------------\n"
"You take the book\n"
"This is level 2 the attic\n"
"Round 1. Give me some text:abcdefghijklmnopqrstuvwxy encrypted is nopqrstuvwxyz{|}~ !\"\#$%&'\n"
["n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " ", "!", "\"", "#", "$", "%", "&", "'"]
"What is z||{-puvyq decrypted?\n"

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
n o p q r s t u v w x y z { | } ~   ! " # $ % & ' (

このルールをLevel 1で書いたRubyプログラムに追加します。
require "socket"

begin
  sock = TCPSocket.open("146.148.102.236", 24069)
rescue
  puts "TCPSocket.open failed : #$!\n"
else
  level = 1
    while sock.gets
      buf = $_
      p buf
      if buf.start_with?("This is level 1") or buf.include?("Correct!") then
        str = "abcdefghijklmnopqrstuvwxy"
        printf(str + "\n")
        sock.write(str + "\n")
        STDOUT.flush
      elsif buf.include?(" encrypted is ") then
        if level == 1 then
          ary = buf.split(" ")
        elsif level == 2 then
          buf = buf[buf.index("encrypted is ") + "encrypted is ".length, str.length]
          ary = buf.split("")
          p ary
        end
        #p ary
      elsif buf.start_with?("What is") then
        ans = ""
        if level == 1 then
          enc = buf.gsub(/\s\s\s/, " _ ").split(" ")
          #p enc
          for e in enc do
            if e == "What" or e == "is" or e == "decrypted?" then
              next
            elsif e == "_" then
              ans = ans + " "
            elsif ary.index(e) then
              ans = ans + str[ary.index(e) - 8].chr
            else
              ans = ans + "z"
            end
          end
        elsif level == 2 then
          buf.slice!("What is ")
          buf.slice!(" decrypted?\n")
          enc = buf.split("")
          p enc
          for e in enc do
            if ary.index(e) then
              ans = ans + str[ary.index(e)].chr
            elsif e == "-" then
              ans = ans + " "
            else
              ans = ans + "z"
            end
          end
        end
        printf(ans + "\n")
        sock.write(ans + "\n")
        STDOUT.flush
      elsif buf.start_with?("Round complete!") then
        level += 1
      else
      end
    end
  sock.close
end
実行すると、Level 2を50問解いた後にフラグが表示されます。
(略)
"------------------------------------------\n"
"You take the book\n"
"This is level 2 the attic\n"
"Round 1. Give me some text:abcdefghijklmnopqrstuvwxy encrypted is nopqrstuvwxyz{|}~ !\"\#$%&'\n"
["n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " ", "!", "\"", "#", "$", "%", "&", "'"]
"What is z||{-puvyq decrypted?\n"
["z", "|", "|", "{", "-", "p", "u", "v", "y", "q"]
moon child
":Correct!\n"
(略)
"Round 50. Give me some text:abcdefghijklmnopqrstuvwxy encrypted is nopqrstuvwxyz{|}~ !\"\#$%&'\n"
["n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " ", "!", "\"", "#", "$", "%", "&", "'"]
"What is %ryp|zr-n\" r'# decrypted?\n"
["%", "r", "y", "p", "|", "z", "r", "-", "n", "\"", " ", "r", "'", "#"]
welcome atreyu
":Correct!\n"
abcdefghijklmnopqrstuvwxy
"Round complete!\n"
"TUCTF{c4n_s0me0ne_turn_a_1ight_0n}\n"
フラグは、 
TUCTF{c4n_s0me0ne_turn_a_1ight_0n}