Created
June 15, 2014 15:20
-
-
Save sota1235/3243ac7739b5f95a187d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/ruby | |
| require 'socket' | |
| class Mysocket | |
| def initialize(host, port) | |
| @host = host | |
| @port = port | |
| end | |
| def connect | |
| @sock = TCPSocket.open(@host, @port) | |
| end | |
| def send(msg) | |
| @sock.puts(msg) | |
| end | |
| def recv | |
| return @sock.gets | |
| end | |
| end | |
| # ここから問題処理 | |
| # 接続先ホスト(今回は変える必要はありません) | |
| host = 'rg-ctf.yagihashoo.com' | |
| # 接続先ポート(問題によって変える必要があります) | |
| port = 10000 | |
| sock = Mysocket.new(host, port) | |
| sock.connect | |
| # 説明文の読み込みと表示 ここから | |
| # 説明文のない問題もあるので、その場合は | |
| # このwhileブロックをコメントアウトするか | |
| # 削除して下さい | |
| while true | |
| line = sock.recv | |
| if line == "####################\n" then | |
| print sock.recv | |
| break | |
| end | |
| line = sock.recv | |
| end | |
| # 説明文の読み込みと表示 ここまで | |
| # 問題を読み込んで答えを返す ここから | |
| # このサンプルでは全部で10個の問題が出題されます | |
| # 他の多くの問題もそうですが、10個でない場合もあるので、 | |
| # その場合は適宜このスクリプトを書き換えて下さい | |
| for num in 1..10 do | |
| # 問題そのものを取得 | |
| line = sock.recv | |
| # 読み込んだ問題を表示 (この時点では未送信) | |
| print line | |
| print sock.recv | |
| # 読み込んだ時点では末尾に改行が付いているのでrstrip()で改行を取り除く | |
| # このサンプルでは与えられた文字列をそのまま返せばいいだけなので、 | |
| # これが答えとなりますが、他の問題では与えられた問題に対して | |
| # 適切に処理をして、正解を返してください | |
| ans = line.rstrip | |
| # 答えの送信(末尾には改行が必要です)とローカルへの表示 | |
| sock.send(ans + "\n") | |
| print ans + "\n" | |
| print sock.recv | |
| print sock.recv | |
| end | |
| # 最後にFLAGが表示されるのでここでFLAGを取得・表示します | |
| print sock.recv | |
| print sock.recv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment