| 1 | ''' |
|---|
| 2 | Created on Jul 22, 2009 |
|---|
| 3 | |
|---|
| 4 | @author: qsy |
|---|
| 5 | ''' |
|---|
| 6 | |
|---|
| 7 | import btsniff |
|---|
| 8 | import sniffer |
|---|
| 9 | import crack |
|---|
| 10 | import btlayers |
|---|
| 11 | |
|---|
| 12 | from sniffcommon import * |
|---|
| 13 | from packet import BtMetaPacket |
|---|
| 14 | |
|---|
| 15 | class CollectHandler(btsniff.SniffHandler): |
|---|
| 16 | |
|---|
| 17 | def __init__(self): |
|---|
| 18 | self.data = [] |
|---|
| 19 | |
|---|
| 20 | def recvgenevt(self, unit): |
|---|
| 21 | self.data.append(BtMetaPacket(unit)) |
|---|
| 22 | |
|---|
| 23 | def recvlmp(self, unit): |
|---|
| 24 | self.recvgenevt(unit) |
|---|
| 25 | |
|---|
| 26 | def recvdv(self, unit): |
|---|
| 27 | self.recvgenevt(unit) |
|---|
| 28 | |
|---|
| 29 | def recvl2cap(self, unit): |
|---|
| 30 | self.recvgenevt(unit) |
|---|
| 31 | |
|---|
| 32 | def clear_data(self): |
|---|
| 33 | self.data = [] |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | class PinCrackCollectHandler(CollectHandler): |
|---|
| 37 | |
|---|
| 38 | def __init__(self, master_add, slave_add): |
|---|
| 39 | super(PinCrackCollectHandler, self).__init__() |
|---|
| 40 | self._capstate = btsniff.CaptureState() |
|---|
| 41 | self._pcr = crack.PinCrackRunner(master_add, slave_add, self._capstate) |
|---|
| 42 | self._pin = None |
|---|
| 43 | |
|---|
| 44 | def recvlmp(self, unit): |
|---|
| 45 | super(PinCrackCollectHandler, self).recvlmp(unit) |
|---|
| 46 | lmp = unit.payload |
|---|
| 47 | log.debug('PinCrackCollectHandler: op1 = %s' % str(lmp.header.op1)) |
|---|
| 48 | if lmp.header.op1 in crack.LMP_PINCRACK_OPCODES \ |
|---|
| 49 | and self._pcr.try_crack(lmp, unit.is_src_master): |
|---|
| 50 | self._pin = self._pcr.getpin() |
|---|
| 51 | |
|---|
| 52 | def is_done(self): |
|---|
| 53 | return self._pcr.is_done() |
|---|
| 54 | |
|---|
| 55 | def close(self): |
|---|
| 56 | self._pcr.terminate() |
|---|
| 57 | |
|---|
| 58 | def getpin(self): |
|---|
| 59 | """ |
|---|
| 60 | @return Pin as a string |
|---|
| 61 | """ |
|---|
| 62 | return self._pcr.getpin() |
|---|
| 63 | |
|---|
| 64 | class TextHandler(btsniff.SniffHandler): |
|---|
| 65 | ''' |
|---|
| 66 | This handler duplicates the functionality of Frontline. |
|---|
| 67 | Allows the calculation of a pin |
|---|
| 68 | ''' |
|---|
| 69 | def __init__(self, do_pin = False, |
|---|
| 70 | master_add = None, slave_add = None, writer = None): |
|---|
| 71 | |
|---|
| 72 | super(TextHandler, self).__init__() |
|---|
| 73 | self._state = btsniff.CaptureState() |
|---|
| 74 | if do_pin: |
|---|
| 75 | log.debug('do_pin') |
|---|
| 76 | self._state.pinstate = 1 |
|---|
| 77 | self._pcr = crack.PinCrackRunner(master_add, slave_add) |
|---|
| 78 | if master_add is None or slave_add is None: |
|---|
| 79 | raise Exception('Error: cannot do_pin without master/slave addresses') |
|---|
| 80 | else: |
|---|
| 81 | self._pcr = None |
|---|
| 82 | # if not writer: |
|---|
| 83 | # writer = sfio.HCIWriter() |
|---|
| 84 | self._writer = writer |
|---|
| 85 | # if session: |
|---|
| 86 | # self._session = session |
|---|
| 87 | # self._write_file = session.dump |
|---|
| 88 | # else: |
|---|
| 89 | # raise sniff.SniffError("FrontlineHandler: Session not given. session is %s" |
|---|
| 90 | # % session) |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | def _writetofile(self, type, packet): |
|---|
| 94 | pass |
|---|
| 95 | # self._writer.writetofile(hcipkttype = type, llid = self._session.state.llid, |
|---|
| 96 | # ismaster = self._session.state.master, packet = packet, filename = self._write_file) |
|---|
| 97 | |
|---|
| 98 | def _printpktdetails(self, packet): |
|---|
| 99 | """ |
|---|
| 100 | Parameters: |
|---|
| 101 | packet - sniff.SniffPacket |
|---|
| 102 | """ |
|---|
| 103 | master = not (packet.clock & FP_SLAVE_MASK) |
|---|
| 104 | header_len = packet.header_len |
|---|
| 105 | channel = packet.chan |
|---|
| 106 | clock = packet.clock |
|---|
| 107 | status = packet.status |
|---|
| 108 | hdr0 = packet.header_byte0 |
|---|
| 109 | type = packet.type |
|---|
| 110 | address = packet.address |
|---|
| 111 | llid = packet.llid |
|---|
| 112 | length = packet.payload_len |
|---|
| 113 | |
|---|
| 114 | log.debug('PL 0x%.2X Ch %.2d %c Clk 0x%.7X Status 0x%.1X Hdr0 0x%.2X [type: %d addr: %d] LLID %d Len %d' \ |
|---|
| 115 | % (header_len, |
|---|
| 116 | channel, |
|---|
| 117 | 'M' if master else 'S', |
|---|
| 118 | clock, |
|---|
| 119 | status, |
|---|
| 120 | hdr0, |
|---|
| 121 | type, |
|---|
| 122 | address, |
|---|
| 123 | llid, |
|---|
| 124 | length)) |
|---|
| 125 | |
|---|
| 126 | def _printpayload(self, payload): |
|---|
| 127 | log.debug(' '.join(['%.2x' % d for d in payload.rawdata])) |
|---|
| 128 | |
|---|
| 129 | def recvlmp(self, packet): |
|---|
| 130 | self._printpktdetails(packet) |
|---|
| 131 | lmp = packet.payload |
|---|
| 132 | if lmp: |
|---|
| 133 | log.debug('LMP Tid %d, Op1 %d' % (lmp.header.tid, lmp.header.op1)) |
|---|
| 134 | if lmp.header.op1 >= 124 and lmp.header.op1 <= 127: |
|---|
| 135 | log.debug(', Op2 %d' % (lmp.header.op2)) |
|---|
| 136 | log.debug(' '.join(['%.2x' % d for d in lmp.payload.rawdata])) |
|---|
| 137 | |
|---|
| 138 | if self._pcr and self._pcr.try_crack(lmp): |
|---|
| 139 | log.debug(19 * '=') |
|---|
| 140 | log.debug('Pin: ', self._pcr.getpin()) |
|---|
| 141 | log.debug(19 * '=') |
|---|
| 142 | |
|---|
| 143 | else: |
|---|
| 144 | log.debug('') |
|---|
| 145 | |
|---|
| 146 | def getpin(self, pincrackdata): |
|---|
| 147 | import tempfile |
|---|
| 148 | tmpfile = tempfile.TemporaryFile() |
|---|
| 149 | pcr = crack._pincrackrunner() # This is a thread. runcrack is actually thread.start() |
|---|
| 150 | evt = pcr.runcrack(pincrackdata, self._session.master, |
|---|
| 151 | self._session.slave, tmpfile) |
|---|
| 152 | evt.wait() |
|---|
| 153 | return pcr.getpin() |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | def recvl2cap(self, packet): |
|---|
| 157 | self._printpktdetails(packet) |
|---|
| 158 | log.debug("L2CAP:") |
|---|
| 159 | # self._printgenpkt(packet.payload) |
|---|
| 160 | self._printpayload(packet.payload) |
|---|
| 161 | |
|---|
| 162 | def recvdv(self, packet): |
|---|
| 163 | self._printpktdetails(packet) |
|---|
| 164 | log.debug('DV:') |
|---|
| 165 | self._printgenpkt(packet.payload) |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | def recvgenevt(self, packet): |
|---|
| 169 | self._printpktdetails(packet) |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|