For2
===
## 問題概要
### ジャンル
Forensics
### 点数
200
### フラグ
CTF{tHE_cAT_iS_the_cULpRIT}
### 問題文
Find the flag.
[capture.pcapng](https://capturetheflag.withgoogle.com/attachment/ba5dc6e166469130afe6416e84386e11bce4f14793e5fa2f5d6f412ebad4b9d9)
### 挑戦者
mzyy94
@K_atc
tkmru
## 解法
pcap整形して
これやるだけ
コードは適当に拾ってきて書き換えた。
```python=
#!/usr/bin/python
import struct
from PIL import Image
import dpkt
INIT_X, INIT_Y = 2000, 2000
picture = Image.new("RGB", (5000, 5000), "white")
pixels = picture.load() 
f = open("capture.pcapng","rb")
x, y = INIT_X, INIT_Y
D = f.read(64)
while D:
    D = f.read(64)
    try:data = struct.unpack("bbbb", D[27:31])
    except:pass
    status = data[0]
    x = x + data[1]
    y = y + data[2]
    if (status == 1):
        for i in range(-5, 5):
            for j in range(-5, 5):
                 try:pixels[x + i , y + j] = (0, 0, 0, 0)
                 except:pass
    else:
        try:pixels[x, y] = (255, 0, 0, 0)
        except:pass
    D = f.read(64)
picture.save("flag.png", "PNG")
```
## 議論
USB接続のマウスでフラグの文字を書いてるっぽい
マウス(idVendor: Logitech, Inc. (0x046d), idProduct: M90/M100 Optical Mouse (0xc05a))のうごきを復元すればよさそう
←キーボード入力はやったことあるんだけどなぁ〜><
デバイスデスクリプタ中心 → http://www.cqpub.co.jp/hanbai/books/33/33381/33381.pdf
Device Class Definition for Human Interface Devices (HID) 
http://www.usb.org/developers/hidpage/HID1_11.pdf
このUSBマウスはインタラプト転送で位置情報を送っているみたい。ペイロード4バイトの内訳はこんな感じで良さそう。
Definition の B.2 Protocol 2 (Mouse) より
Bytes | Bits | Description
:-:|:-:|-
0 | 0 | Button 1
0 | 1 | Button 2
0 | 2 | Button 3
0 | 4 to 7 | Device-specific 
1 | 0 to 7 | X displacement (Signed integer)
2 | 0 to 7 | Y displacement (Signed integer)
3 to n | 0 to 7 | Device specific (optional)
https://wiki.wireshark.org/USB
Each event contains a header, described by the following structure:
```
typedef struct _usb_header {
   u_int64_t id;
   u_int8_t event_type;
   u_int8_t transfer_type;
   u_int8_t endpoint_number;
   u_int8_t device_address;
   u_int16_t bus_id;
   char setup_flag;
   char data_flag;
   int64_t ts_sec;
   int32_t ts_usec;
   int32_t status;
   u_int32_t urb_len;
   u_int32_t data_len;
   pcap_usb_setup setup;
} pcap_usb_header;
```
takaki:payloadだけ抜いた https://gist.github.com/tikuwachan/7740a94a05b77ba49431d84a07775a93
tkmru: for2に似た問題情報。https://github.com/ctfs/write-ups-2015/tree/master/boston-key-party-2015/school-bus/riverside
plotしてみる
