shinobi CTF

HTBとCTF頑張る

HackIT CTF 2017 Forensic 100

Challenge

This file was captured from one of the computers at the Internet cafe. We think that the hacker was using this computer at that time. Try to get his secret documents. ( flag format is flag{…} )

Solution

あたえられるのはpcapファイルです。中を開くとUSBのパケットが並びます。interrupt転送やらハブのディスクリプタが流れた後、怪しげなディスクリプタが流れていました。アップルのキーボードに関するパケットのようです。 f:id:grapeBiscuit:20170829114902j:plain

その後のパケットもinterrupt転送で8byteのパケットを転送しているので、これはUSB keyboardのパケットだと推定。これを読み解けばflagを取れそうです。
keyboardのデータに関しては、usb.orgがこちらで資料を公開しています。53ページからがキーボードのレイアウトで、これをもとに8byteのデータをを文字に変換するスクリプトを書きます。
f:id:grapeBiscuit:20170906095110j:plain
7月に発売されたセキュリティコンテストのためのCTF問題集にとても参考となるスクリプトが紹介されていたので、それをベースに書きました。

セキュリティコンテストのためのCTF問題集

セキュリティコンテストのためのCTF問題集

#! /usr/bin/env python
#! -*- cording: utf-8 -*-

from scapy.all import *

keymap = {  0x04: ('a','A'), 0x05: ('b','B'),0x06: ('c','C'),
            0x07: ('d','D'), 0x08: ('e','E'),0x09: ('f','F'),
            0x0a: ('g','G'), 0x0b: ('h','H'),0x0c: ('i','I'),
            0x0d: ('j','J'), 0x0e: ('k','K'),0x0f: ('l','L'),
            0x10: ('m','M'), 0x11: ('n','N'),0x12: ('o','O'),
            0x13: ('p','P'), 0x14: ('q','Q'),0x15: ('r','R'),
            0x16: ('s','S'), 0x17: ('t','T'),0x18: ('u','U'),
            0x19: ('v','V'), 0x1a: ('w','W'),0x1b: ('x','X'),
            0x1c: ('y','Y'), 0x1d: ('z','Z'),0x1e: ('1','!'),
            0x1f: ('2','@'), 0x20: ('3','#'),0x21: ('4','$'),
            0x22: ('5','%'), 0x23: ('6','^'),0x24: ('7','&'),
            0x25: ('8','*'), 0x26: ('9','('),0x27: ('0',')'),
            0x28: (' [Enter] ',' [Enter] '), 0x29: ('\x1b','\x1b'),
            0x2a: (' [del] ',' [del] '), 0x2b: ('\x09','\x09'),
            0x2c: ('\x20','\x20'), 0x2d: ('-','_'),
            0x2e: ('=','+'), 0x2f: ('[','{'),0x30: (']','}'),
            0x31: ('\\','|'), 0x33: (';',':'),0x34: ('\'','\"'),
            0x35: ('`','~'), 0x36: (',','<'),0x37: ('.','>'),
            0x38: ('/','?'),
            0x51:(' [downArrow] ',' [downArrow] '), 0x52: (' [upArrow] ',' [upArrow] '),0x32: ('\\','|')
            }

def read_usbdata_from_pcap():
    pcap = rdpcap("task.pcap")
    usb_data = []
    for pkt in pcap:
        buf = pkt['Raw'].load
        if buf[22] == '\x01':
            if len(buf[27:]) == 8:
                usb_data.append(buf[27:])
    return usb_data


def analyze_usb_data(usb_data):
    flag = ""
    for d in usb_data:
        if d[2] == '\x00' or not('\00' in d[3:8]):
            #No Event
            continue
        if d[0] == '\x02' or d[0] == '\x20':
            #press shift
            #binary -> int
            c = keymap[ord(d[2])][1]
            flag += c
        else:
            #binary -> int
            c = keymap[ord(d[2])][0]
            flag += c
    print flag

def main():
    data = read_usbdata_from_pcap()
    analyze_usb_data(data)

if __name__ == '__main__':
    main()

すると、次のキーが押されていた事がわかります。


w [Enter] k [Enter] f [Enter] b [Enter] 3' [upArrow] [ [upArrow] l [upArrow] # [upArrow] {w$ [downArrow] >b [downArrow] ag [downArrow] [e [downArrow] ci.[ [upArrow] [f [upArrow] {k [upArrow] n$ [upArrow] ju} [downArrow] : [downArrow] 3 [downArrow] u [downArrow] %= [upArrow] | [upArrow] y [upArrow] 6 [upArrow] ,‘ [downArrow] p [downArrow] b [downArrow] 7 [downArrow] %& [upArrow] d [upArrow] 0 [upArrow] j [upArrow] pt [downArrow] i [downArrow] a [downArrow] [ [downArrow] k( [upArrow] = [upArrow] r [upArrow] m [upArrow] ]= [downArrow] 0 [downArrow] d [downArrow] > [downArrow] lc [upArrow] * [upArrow] _ [upArrow] { [upArrow] j% [downArrow] u [downArrow] s [downArrow] ( [downArrow] *2 [upArrow] 0 [upArrow] n [upArrow] ’ [upArrow] ;9 [downArrow] h [downArrow] 4 [downArrow] ] [downArrow] y4 [upArrow] ‘ [upArrow] k [upArrow] ; [upArrow] +p [downArrow] f [downArrow] e [downArrow] $ [downArrow] !} [upArrow] 1 [upArrow] _ [upArrow] k [upArrow] s& [downArrow] s [downArrow] 2 [downArrow] c [downArrow] %q [upArrow] $ [upArrow] . [upArrow] ! [upArrow] #, [downArrow] s [downArrow] 0 [downArrow] c [downArrow] z3 [upArrow] e [upArrow] } [upArrow] - [upArrow] i


これをひとつづつテキストエディタ上で入力します。
特殊文字"[“や”{“を自動保管してくれる機能に注意しましょう。notepad.exeが一番いいかもしれません。
するとflagが出現します。


w{w$ju},’pt]=j%;9+ps&#,i
k#>bn$:6pijm0u{h’;fks!s-
flag{k3yb0ard_sn4ke_2.0}
b[[e[fu|7d[=>((0]’$1c$ce
3’ci.[%=%&k(lc
2y4!}%qz3


sweets

flag{k3yb0ard_sn4ke_2.0}
USBのプロトコルをかじっていれば解ける問題でした。セキュリティコンテストのためのCTF問題集がとても参考になりました。ビギナーは必携です。