CSAW CTF Qualifications 2012 - Networking 300
01 Oct 2012The 2012 Qualification round for CSAW CTF was fun. I was playing with the Northeastern Seclab hacking group - PTHC. One of the more interesting challenges was networking 300.
As input we received a file called "dongle.pcap", no further description.
The first thing to do with pcaps is to load them in wireshark. The type for most packets is URB_Interrupt or URB_Control (URB is a USB request block). Most of the packets don't look interesting - by browsing we found packet 67 which contains the following string: "Teensy Keyboard/Mouse/Joystick", this made us assume that we have to deal with recovering key presses.
Some quick googling lead us to this website, we downloaded the source files and inspected the code to analyze the protocol. We figured out that the packets we are interested in are pretty specific, they should be:
- 72 bytes long
- the 10th byte is \x01
- the 12th one is \x1a
- the 66th byte should be non-zero
- also we only care about the last 8 bytes and can disregard the rest
This sounds like a perfect job for scapy:
import binascii from scapy.all import * dongle = rdpcap("dongle.pcap") for d in dongle: sd = str(d) if len(sd) == 0x48 and sd[9] == '\x01' and sd[11] == '\x1a': x = sd[len(sd) -8:] print binascii.hexlify(x)
This leaves us with 1338 packets, the last four bytes are all \x00. We inspect the file usb_keyboard_debug.h from the keyboard's source file and can see the key mapping. "A" = 4 etc. We created a mapping keycode-output and prepended it to our script so we would see the keyboards behavior. By inspecting the pcap we found that there were no ALT or STRG key presses, only shift and only for a couple of keys (3). For the sake of simplicity we decided just to check for these three cases in additional if clauses without making a generalized upper-case function. What we get from running the updated program:
XTERM -GEOMETRY 12X1+0+0 ECHO K RXTERM -GEOMETRY 12X1+75+0 ECHO E RXTERM -GEOMETRY 12X1+150+0 ECHO Y RXTERM -GEOMETRY 12X1+225+0 ECHO { RXTERM -GEOMETRY 12X1+300+0 ECHO C RXTERM -GEOMETRY 12X1+375+0 ECHO 4 RXTERM -GEOMETRY 12X1+450+0 ECHO 8 RXTERM -GEOMETRY 12X1+525+0 ECHO B RXTERM -GEOMETRY 12X1+600+0 ECHO A RXTERM -GEOMETRY 12X1+675+0 ECHO 9 RXTERM -GEOMETRY 12X1+0+40 ECHO 9 RXTERM -GEOMETRY 12X1+75+40 ECHO 3 RXTERM -GEOMETRY 12X1+150+40 ECHO D RXTERM -GEOMETRY 12X1+225+40 ECHO 3 RXTERM -GEOMETRY 12X1+300+40 ECHO 5 RXTERM -GEOMETRY 12X1+450+40 ECHO C RXTERM -GEOMETRY 12X1+375+40 ECHO 3 RXTERM -GEOMETRY 12X1+525+40 ECHO A RXTERM -GEOMETRY 12X1+600+40 ECHO }
The string "KEY{...}" made us assume we finished, but the scoreboard disagreed. It took us a while to figure out that we also have to take the RXTERM lines into account - the last two numbers (separated by "+" are the coordinates). That was the last part of the puzzle and we got the points.
The following program is the cleaned up, final version that will parse the pcap file, pull out the key presses, write the keys into a matrix with the corresponding coordinates and print them in order.
from scapy.all import * import binascii keys = {} keys[4]='A' keys[5]='B' keys[6]='C' keys[7]='D' keys[8]='E' keys[9]='F' keys[10]='G' keys[11]='H' keys[12]='I' keys[13]='J' keys[14]='K' keys[15]='L' keys[16]='M' keys[17]='N' keys[18]='O' keys[19]='P' keys[20]='Q' keys[21]='R' keys[22]='S' keys[23]='T' keys[24]='U' keys[25]='V' keys[26]='W' keys[27]='X' keys[28]='Y' keys[29]='Z' keys[30]='1' keys[31]='2' keys[32]='3' keys[33]='4' keys[34]='5' keys[35]='6' keys[36]='7' keys[37]='8' keys[38]='9' keys[39]='0' keys[41]='ESC' keys[43]='TAB' keys[45]='-' keys[46]='EQUAL' keys[47]='LEFT_BRACE' keys[48]='RIGHT_BRACE' keys[49]='BACKSLASH' keys[50]='NUMBER' keys[51]='SEMICOLON' keys[52]='QUOTE' keys[53]='TILDE' keys[54]='COMMA' keys[55]='PERIOD' keys[56]='SLASH' keys[57]='CAPS_LOCK' keys[58]='F1' keys[59]='F2' keys[60]='F3' keys[61]='F4' keys[62]='F5' keys[63]='F6' keys[64]='F7' keys[65]='F8' keys[66]='F9' keys[67]='F10' keys[68]='F11' keys[69]='F12' keys[72]='PAUSE' keys[74]='HOME' keys[75]='PAGE_UP' keys[76]='DELETE' keys[77]='END' keys[79]='RIGHT' keys[80]='LEFT' keys[81]='DOWN' keys[82]='UP' keys[83]='NUM_LOCK' keys[89]='KEYPAD_1' keys[90]='KEYPAD_2' keys[44]=' ' keys[40]='ENTER' dongle = rdpcap("dongle.pcap") buf = {} buf[1] = "" for d in dongle: sd = str(d) if len(sd) == 0x48 and sd[9] == '\x01' and sd[11] == '\x1a' and sd[0x42] != '\x00': x = sd[len(sd) -8:] # only these three keys get used with shift if x[0] == '\x02': if keys[ord(x[2])] == "EQUAL": key = "+" elif keys[ord(x[2])] == "RIGHT_BRACE": key = "}" elif keys[ord(x[2])] == "LEFT_BRACE": key = "{" else: key = keys[ord(x[2])] if key == "ENTER": print buf[len(buf)] buf[len(buf)+1]="" key = "\n" else: buf[len(buf)] += key # putting keys on their corresponding coordinates matrix = {} for i in range(1,len(buf),2): (a,b,c) = (buf[i]).split("+") if int(c) not in matrix.keys(): matrix[int(c)] = {} # look ahead for one line - the echo command matrix[int(c)][int(b)] = buf[i+1][-1:] # print pressed keys in order for x in sorted(matrix.keys()): for y in sorted(matrix[x].keys()): print "{a1e0ea66cab8e2163484f308b55c8b73124cdcf3d3411fefce873f0243ccaee6}03d-{a1e0ea66cab8e2163484f308b55c8b73124cdcf3d3411fefce873f0243ccaee6}03d-{a1e0ea66cab8e2163484f308b55c8b73124cdcf3d3411fefce873f0243ccaee6}s" {a1e0ea66cab8e2163484f308b55c8b73124cdcf3d3411fefce873f0243ccaee6} (x,y,matrix[x][y])