LoginSignup
0
1

More than 3 years have passed since last update.

picoCTF2018 writeup (what base is this ?)

Posted at

問題

サーバに接続すると,問題が3問与えられます.
1問目は,2進数のリストが表示されます.
2問目は,16進数が与えられます.
3問目は,8進数のリストが表示されます.
それぞれ単語に変換して入力するですが,制限時間があるので,Pythonで自動化処理を施しました.

解法

import re
from telnetlib import Telnet


def q1(bin_list):
    '''
    1問目
    2進数のリストを受け取って,単語にして返す
    '''
    dec_list = [int(x, 2) for x in bin_list]
    ans = ''.join([chr(x) for x in dec_list])
    return ans


def string_split2char(string, div_num):
    '''
    文字列をdiv_num文字ずつ区切る
    '''
    dec_list = re.split(f'(.{{{div_num}}})', string)[1::2]
    return dec_list


def q2(word):
    '''
    2問目
    16進数の文字列(区切り文字なし)を受け取って,単語にして返す
    '''
    dec_list = string_split2char(word, 2)
    ans = ''.join(list(map(chr, [int(x, 16) for x in dec_list])))
    return ans


def q3(oct_list):
    '''
    3問目
    8進数のリストを受け取って,単語にして返す
    '''
    dec_list = [int(x, 8) for x in oct_list]
    ans = ''.join([chr(x) for x in dec_list])
    return ans


def my_index_multi(l, x):
    '''
    リストの中で,xにマッチするインデックスをリストで返す
    '''
    return [i for i, _x in enumerate(l) if _x == x]


def get_given_word(text):
    '''
    設問の中からキーワードを抽出する
    '''
    word_list = text.split()
    left_index = my_index_multi(word_list, 'the').pop()
    right_index = my_index_multi(word_list, 'as').pop()
    return word_list[left_index+1:right_index]


if __name__ == "__main__":
    with Telnet('2018shell.picoctf.com', 1225) as tn:
        # q1
        # 問題文読み込み
        q1_text = tn.read_until(b'Input:').decode()
        # キーワードを抽出
        word1 = get_given_word(q1_text)
        # 変換して解答
        ans1 = q1(word1)
        tn.write(ans1.encode() + b'\n')

        # q2
        q2_text = tn.read_until(b'Input:').decode()
        word2 = get_given_word(q2_text)
        ans2 = q2(word2[0])
        tn.write(ans2.encode() + b'\n')

        # q3
        q3_text = tn.read_until(b'Input:').decode()
        word3 = get_given_word(q3_text)
        ans3 = q3(word3)
        tn.write(ans3.encode() + b'\n')

        # flag
        ret = tn.read_all()
        print(ret.decode())
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1