Coverage for /root/GitHubProjects/impacket/impacket/dot11.py: 88%

# Coverage for /root/GitHubProjects/impacket/impacket/dot11.py : 88%

## 1765 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.

2#

3# This software is provided under under a slightly modified version

4# of the Apache Software License. See the accompanying LICENSE file

5# for more information.

6#

7# Description:

8# IEEE 802.11 Network packet codecs.

9#

10# Author:

11# Gustavo Moreira

12

13 import struct

14 from binascii import crc32

15

16 from impacket.ImpactPacket import ProtocolPacket, array_tobytes

17 from impacket.Dot11Crypto import RC4

18 frequency = {

19 2412: 1, 2417: 2, 2422: 3, 2427: 4, 2432: 5, 2437: 6, 2442: 7, 2447: 8, 2452: 9,

20 2457: 10, 2462: 11, 2467: 12, 2472: 13, 2484: 14, 5170: 34, 5180: 36, 5190: 38, 5200: 40,

21 5210: 42, 5220: 44, 5230: 46, 5240: 48, 5260: 52, 5280: 56, 5300: 60, 5320: 64, 5500: 100,

22 5510: 102, 5520: 104, 5530: 106, 5540: 108, 5550: 110, 5560: 112, 5570: 114, 5580: 116, 5590: 118,

23 5600: 120, 5610: 122, 5620: 124, 5630: 126, 5640: 128, 5650: 130, 5660: 132, 5670: 134, 5680: 136,

24 5690: 138, 5700: 140, 5745: 149, 5765: 153, 5785: 157, 5805: 161, 5825: 165, 5855: 170, 5860: 172,

25 5865: 173, 5870: 174, 5875: 175, 5880: 176, 5885: 177, 5890: 178, 5895: 179, 5900: 180, 5905: 181,

26 5910: 182, 5915: 183, 5920: 184,

27}

28

29

30 class Dot11ManagementCapabilities():

31 #

32 # Capability Information

33 # 0 1 2 3 4 5 6 7 8 9 A B C D E F

34 # +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

35 # | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

36 # +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

37 # | | | | | | | | | | | | | | | |

38 # | | | | | | | | | | | | | | |---+-- Reserved

39 # | | | | | | | | | | | | | |

40 # | | | | | | | | | | | | | |---------- DSSS-OFDM

41 # | | | | | | | | | | | | |

42 # | | | | | | | | | | | |---+-------------- Reserved

43 # | | | | | | | | | | |

44 # | | | | | | | | | | |---------------------- Short slot time

45 # | | | | | | | | | |

46 # | | | | | | | | |---+-------------------------- Reserved

47 # | | | | | | | |

48 # | | | | | | | |---------------------------------- Channel agility (802.11b)

49 # | | | | | | |

50 # | | | | | | |-------------------------------------- PBCC (802.11b)

51 # | | | | | |

52 # | | | | | |------------------------------------------ Short preamble (802.11b)

53 # | | | | |

54 # | | | | |---------------------------------------------- Privacy

55 # | | | |

56 # | | | |-------------------------------------------------- CF-Poll request

57 # | | |

58 # | | |------------------------------------------------------ CF-Pollable

59 # | |

60 # | |---------------------------------------------------------- IBSS

61 # |

62 # |-------------------------------------------------------------- ESS

63 #

64 CAPABILITY_RESERVED_1 = int("1000000000000000", 2)

65 CAPABILITY_RESERVED_2 = int("0100000000000000", 2)

66 CAPABILITY_DSSS_OFDM = int("0010000000000000", 2)

67 CAPABILITY_RESERVED_3 = int("0001000000000000", 2)

68 CAPABILITY_RESERVED_4 = int("0000100000000000", 2)

69 CAPABILITY_SHORT_SLOT_TIME = int("0000010000000000", 2)

70 CAPABILITY_RESERVED_5 = int("0000001000000000", 2)

71 CAPABILITY_RESERVED_6 = int("0000000100000000", 2)

72 CAPABILITY_CH_AGILITY = int("0000000010000000", 2)

73 CAPABILITY_PBCC = int("0000000001000000", 2)

74 CAPABILITY_SHORT_PREAMBLE = int("0000000000100000", 2)

75 CAPABILITY_PRIVACY = int("0000000000010000", 2)

76 CAPABILITY_CF_POLL_REQ = int("0000000000001000", 2)

77 CAPABILITY_CF_POLLABLE = int("0000000000000100", 2)

78 CAPABILITY_IBSS = int("0000000000000010", 2)

79 CAPABILITY_ESS = int("0000000000000001", 2)

80

81 class Dot11Types():

82 # Management Types/SubTypes

83 DOT11_TYPE_MANAGEMENT = int("00",2)

84 DOT11_SUBTYPE_MANAGEMENT_ASSOCIATION_REQUEST = int("0000",2)

85 DOT11_SUBTYPE_MANAGEMENT_ASSOCIATION_RESPONSE = int("0001",2)

86 DOT11_SUBTYPE_MANAGEMENT_REASSOCIATION_REQUEST = int("0010",2)

87 DOT11_SUBTYPE_MANAGEMENT_REASSOCIATION_RESPONSE = int("0011",2)

88 DOT11_SUBTYPE_MANAGEMENT_PROBE_REQUEST = int("0100",2)

89 DOT11_SUBTYPE_MANAGEMENT_PROBE_RESPONSE = int("0101",2)

90 DOT11_SUBTYPE_MANAGEMENT_RESERVED1 = int("0110",2)

91 DOT11_SUBTYPE_MANAGEMENT_RESERVED2 = int("0111",2)

92 DOT11_SUBTYPE_MANAGEMENT_BEACON = int("1000",2)

93 DOT11_SUBTYPE_MANAGEMENT_ATIM = int("1001",2)

94 DOT11_SUBTYPE_MANAGEMENT_DISASSOCIATION = int("1010",2)

95 DOT11_SUBTYPE_MANAGEMENT_AUTHENTICATION = int("1011",2)

96 DOT11_SUBTYPE_MANAGEMENT_DEAUTHENTICATION = int("1100",2)

97 DOT11_SUBTYPE_MANAGEMENT_ACTION = int("1101",2)

98 DOT11_SUBTYPE_MANAGEMENT_RESERVED3 = int("1110",2)

99 DOT11_SUBTYPE_MANAGEMENT_RESERVED4 = int("1111",2)

100

101 DOT11_TYPE_MANAGEMENT_SUBTYPE_ASSOCIATION_REQUEST = \

102 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_ASSOCIATION_REQUEST<<2

103 DOT11_TYPE_MANAGEMENT_SUBTYPE_ASSOCIATION_RESPONSE = \

104 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_ASSOCIATION_RESPONSE<<2

105 DOT11_TYPE_MANAGEMENT_SUBTYPE_REASSOCIATION_REQUEST = \

106 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_REASSOCIATION_REQUEST<<2

107 DOT11_TYPE_MANAGEMENT_SUBTYPE_REASSOCIATION_RESPONSE = \

108 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_REASSOCIATION_RESPONSE<<2

109 DOT11_TYPE_MANAGEMENT_SUBTYPE_PROBE_REQUEST = \

110 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_PROBE_REQUEST<<2

111 DOT11_TYPE_MANAGEMENT_SUBTYPE_PROBE_RESPONSE = \

112 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_PROBE_RESPONSE<<2

113 DOT11_TYPE_MANAGEMENT_SUBTYPE_RESERVED1 = \

114 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_RESERVED1<<2

115 DOT11_TYPE_MANAGEMENT_SUBTYPE_RESERVED2 = \

116 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_RESERVED2<<2

117 DOT11_TYPE_MANAGEMENT_SUBTYPE_BEACON = \

118 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_BEACON<<2

119 DOT11_TYPE_MANAGEMENT_SUBTYPE_ATIM = \

120 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_ATIM<<2

121 DOT11_TYPE_MANAGEMENT_SUBTYPE_DISASSOCIATION = \

122 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_DISASSOCIATION<<2

123 DOT11_TYPE_MANAGEMENT_SUBTYPE_AUTHENTICATION = \

124 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_AUTHENTICATION<<2

125 DOT11_TYPE_MANAGEMENT_SUBTYPE_DEAUTHENTICATION = \

126 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_DEAUTHENTICATION<<2

127 DOT11_TYPE_MANAGEMENT_SUBTYPE_ACTION = \

128 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_ACTION<<2

129 DOT11_TYPE_MANAGEMENT_SUBTYPE_RESERVED3 = \

130 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_RESERVED3<<2

131 DOT11_TYPE_MANAGEMENT_SUBTYPE_RESERVED4 = \

132 DOT11_TYPE_MANAGEMENT|DOT11_SUBTYPE_MANAGEMENT_RESERVED4<<2

133

134 # Control Types/SubTypes

135 DOT11_TYPE_CONTROL = int("01",2)

136 DOT11_SUBTYPE_CONTROL_RESERVED1 = int("0000",2)

137 DOT11_SUBTYPE_CONTROL_RESERVED2 = int("0001",2)

138 DOT11_SUBTYPE_CONTROL_RESERVED3 = int("0010",2)

139 DOT11_SUBTYPE_CONTROL_RESERVED4 = int("0011",2)

140 DOT11_SUBTYPE_CONTROL_RESERVED5 = int("0100",2)

141 DOT11_SUBTYPE_CONTROL_RESERVED6 = int("0101",2)

142 DOT11_SUBTYPE_CONTROL_RESERVED7 = int("0110",2)

143 DOT11_SUBTYPE_CONTROL_RESERVED8 = int("0111",2)

144 DOT11_SUBTYPE_CONTROL_BLOCK_ACK_REQUEST = int("1000",2)

145 DOT11_SUBTYPE_CONTROL_BLOCK_ACK = int("1001",2)

146 DOT11_SUBTYPE_CONTROL_POWERSAVE_POLL = int("1010",2)

147 DOT11_SUBTYPE_CONTROL_REQUEST_TO_SEND = int("1011",2)

148 DOT11_SUBTYPE_CONTROL_CLEAR_TO_SEND = int("1100",2)

149 DOT11_SUBTYPE_CONTROL_ACKNOWLEDGMENT = int("1101",2)

150 DOT11_SUBTYPE_CONTROL_CF_END = int("1110",2)

151 DOT11_SUBTYPE_CONTROL_CF_END_CF_ACK = int("1111",2)

152

153 DOT11_TYPE_CONTROL_SUBTYPE_RESERVED1 = \

154 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_RESERVED1<<2

155 DOT11_TYPE_CONTROL_SUBTYPE_RESERVED2 = \

156 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_RESERVED2<<2

157 DOT11_TYPE_CONTROL_SUBTYPE_RESERVED3 = \

158 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_RESERVED3<<2

159 DOT11_TYPE_CONTROL_SUBTYPE_RESERVED4 = \

160 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_RESERVED4<<2

161 DOT11_TYPE_CONTROL_SUBTYPE_RESERVED5 = \

162 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_RESERVED5<<2

163 DOT11_TYPE_CONTROL_SUBTYPE_RESERVED6 = \

164 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_RESERVED6<<2

165 DOT11_TYPE_CONTROL_SUBTYPE_RESERVED7 = \

166 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_RESERVED7<<2

167 DOT11_TYPE_CONTROL_SUBTYPE_BLOCK_ACK_REQUEST = \

168 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_BLOCK_ACK_REQUEST<<2

169 DOT11_TYPE_CONTROL_SUBTYPE_BLOCK_ACK = \

170 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_BLOCK_ACK<<2

171 DOT11_TYPE_CONTROL_SUBTYPE_POWERSAVE_POLL = \

172 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_POWERSAVE_POLL<<2

173 DOT11_TYPE_CONTROL_SUBTYPE_REQUEST_TO_SEND = \

174 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_REQUEST_TO_SEND<<2

175 DOT11_TYPE_CONTROL_SUBTYPE_CLEAR_TO_SEND = \

176 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_CLEAR_TO_SEND<<2

177 DOT11_TYPE_CONTROL_SUBTYPE_ACKNOWLEDGMENT = \

178 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_ACKNOWLEDGMENT<<2

179 DOT11_TYPE_CONTROL_SUBTYPE_CF_END = \

180 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_CF_END<<2

181 DOT11_TYPE_CONTROL_SUBTYPE_CF_END_CF_ACK = \

182 DOT11_TYPE_CONTROL|DOT11_SUBTYPE_CONTROL_CF_END_CF_ACK<<2

183

184 # Data Types/SubTypes

185 DOT11_TYPE_DATA = int("10",2)

186 DOT11_SUBTYPE_DATA = int("0000",2)

187 DOT11_SUBTYPE_DATA_CF_ACK = int("0001",2)

188 DOT11_SUBTYPE_DATA_CF_POLL = int("0010",2)

189 DOT11_SUBTYPE_DATA_CF_ACK_CF_POLL = int("0011",2)

190 DOT11_SUBTYPE_DATA_NULL_NO_DATA = int("0100",2)

191 DOT11_SUBTYPE_DATA_CF_ACK_NO_DATA = int("0101",2)

192 DOT11_SUBTYPE_DATA_CF_POLL_NO_DATA = int("0110",2)

193 DOT11_SUBTYPE_DATA_CF_ACK_CF_POLL_NO_DATA = int("0111",2)

194 DOT11_SUBTYPE_DATA_QOS_DATA = int("1000",2)

195 DOT11_SUBTYPE_DATA_QOS_DATA_CF_ACK = int("1001",2)

196 DOT11_SUBTYPE_DATA_QOS_DATA_CF_POLL = int("1010",2)

197 DOT11_SUBTYPE_DATA_QOS_DATA_CF_ACK_CF_POLL = int("1011",2)

198 DOT11_SUBTYPE_DATA_QOS_NULL_NO_DATA = int("1100",2)

199 DOT11_SUBTYPE_DATA_RESERVED1 = int("1101",2)

200 DOT11_SUBTYPE_DATA_QOS_CF_POLL_NO_DATA = int("1110",2)

201 DOT11_SUBTYPE_DATA_QOS_CF_ACK_CF_POLL_NO_DATA = int("1111",2)

202

203 DOT11_TYPE_DATA_SUBTYPE_DATA = \

204 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA<<2

205 DOT11_TYPE_DATA_SUBTYPE_CF_ACK = \

206 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_CF_ACK<<2

207 DOT11_TYPE_DATA_SUBTYPE_CF_POLL = \

208 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_CF_POLL<<2

209 DOT11_TYPE_DATA_SUBTYPE_CF_ACK_CF_POLL = \

210 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_CF_ACK_CF_POLL<<2

211 DOT11_TYPE_DATA_SUBTYPE_NULL_NO_DATA = \

212 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_NULL_NO_DATA<<2

213 DOT11_TYPE_DATA_SUBTYPE_CF_ACK_NO_DATA = \

214 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_CF_POLL_NO_DATA<<2

215 DOT11_TYPE_DATA_SUBTYPE_CF_ACK_CF_POLL_NO_DATA = \

216 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_CF_ACK_CF_POLL_NO_DATA<<2

217 DOT11_TYPE_DATA_SUBTYPE_QOS_DATA = \

218 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_QOS_DATA<<2

219 DOT11_TYPE_DATA_SUBTYPE_QOS_DATA_CF_ACK = \

220 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_QOS_DATA_CF_ACK<<2

221 DOT11_TYPE_DATA_SUBTYPE_QOS_DATA_CF_POLL = \

222 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_QOS_DATA_CF_POLL<<2

223 DOT11_TYPE_DATA_SUBTYPE_QOS_DATA_CF_ACK_CF_POLL = \

224 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_QOS_DATA_CF_ACK_CF_POLL<<2

225 DOT11_TYPE_DATA_SUBTYPE_QOS_NULL_NO_DATA = \

226 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_QOS_NULL_NO_DATA<<2

227 DOT11_TYPE_DATA_SUBTYPE_RESERVED1 = \

228 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_RESERVED1<<2

229 DOT11_TYPE_DATA_SUBTYPE_QOS_CF_POLL_NO_DATA = \

230 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_QOS_CF_POLL_NO_DATA<<2

231 DOT11_TYPE_DATA_SUBTYPE_QOS_CF_ACK_CF_POLL_NO_DATA = \

232 DOT11_TYPE_DATA|DOT11_SUBTYPE_DATA_QOS_CF_ACK_CF_POLL_NO_DATA<<2

233

234 # Reserved Types/SubTypes

235 DOT11_TYPE_RESERVED = int("11",2)

236 DOT11_SUBTYPE_RESERVED_RESERVED1 = int("0000",2)

237 DOT11_SUBTYPE_RESERVED_RESERVED2 = int("0001",2)

238 DOT11_SUBTYPE_RESERVED_RESERVED3 = int("0010",2)

239 DOT11_SUBTYPE_RESERVED_RESERVED4 = int("0011",2)

240 DOT11_SUBTYPE_RESERVED_RESERVED5 = int("0100",2)

241 DOT11_SUBTYPE_RESERVED_RESERVED6 = int("0101",2)

242 DOT11_SUBTYPE_RESERVED_RESERVED7 = int("0110",2)

243 DOT11_SUBTYPE_RESERVED_RESERVED8 = int("0111",2)

244 DOT11_SUBTYPE_RESERVED_RESERVED9 = int("1000",2)

245 DOT11_SUBTYPE_RESERVED_RESERVED10 = int("1001",2)

246 DOT11_SUBTYPE_RESERVED_RESERVED11 = int("1010",2)

247 DOT11_SUBTYPE_RESERVED_RESERVED12 = int("1011",2)

248 DOT11_SUBTYPE_RESERVED_RESERVED13 = int("1100",2)

249 DOT11_SUBTYPE_RESERVED_RESERVED14 = int("1101",2)

250 DOT11_SUBTYPE_RESERVED_RESERVED15 = int("1110",2)

251 DOT11_SUBTYPE_RESERVED_RESERVED16 = int("1111",2)

252

253 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED1 = \

254 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED1<<2

255 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED2 = \

256 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED2<<2

257 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED3 = \

258 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED3<<2

259 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED4 = \

260 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED4<<2

261 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED5 = \

262 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED5<<2

263 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED6 = \

264 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED6<<2

265 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED7 = \

266 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED7<<2

267 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED8 = \

268 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED8<<2

269 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED9 = \

270 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED9<<2

271 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED10 = \

272 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED10<<2

273 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED11 = \

274 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED11<<2

275 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED12 = \

276 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED12<<2

277 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED13 = \

278 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED13<<2

279 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED14 = \

280 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED14<<2

281 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED15 = \

282 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED15<<2

283 DOT11_TYPE_RESERVED_SUBTYPE_RESERVED16 = \

284 DOT11_TYPE_RESERVED|DOT11_SUBTYPE_RESERVED_RESERVED16<<2

285

286 class Dot11(ProtocolPacket):

287 def __init__(self, aBuffer = None, FCS_at_end = True):

288 header_size = 2

289 self.__FCS_at_end=not not FCS_at_end # Is Boolean

290 if self.__FCS_at_end:

291 tail_size = 4

292 else:

293 tail_size = 0

294

295 ProtocolPacket.__init__(self, header_size,tail_size)

296 if(aBuffer):

297 self.load_packet(aBuffer)

298

299 def get_order(self):

300 "Return 802.11 frame 'Order' field"

301 b = self.header.get_byte(1)

302 return ((b >> 7) & 0x01)

303

304 def set_order(self, value):

305 "Set 802.11 frame 'Order' field"

306 # clear the bits

307 mask = (~0x80) & 0xFF

308 masked = self.header.get_byte(1) & mask

309 # set the bits

310 nb = masked | ((value & 0x01) << 7)

311 self.header.set_byte(1, nb)

312

313 def get_protectedFrame(self):

314 "Return 802.11 frame 'Protected' field"

315 b = self.header.get_byte(1)

316 return ((b >> 6) & 0x01)

317

318 def set_protectedFrame(self, value):

319 "Set 802.11 frame 'Protected Frame' field"

320 # clear the bits

321 mask = (~0x40) & 0xFF

322 masked = self.header.get_byte(1) & mask

323 # set the bits

324 nb = masked | ((value & 0x01) << 6)

325 self.header.set_byte(1, nb)

326

327 def get_moreData(self):

328 "Return 802.11 frame 'More Data' field"

329 b = self.header.get_byte(1)

330 return ((b >> 5) & 0x01)

331

332 def set_moreData(self, value):

333 "Set 802.11 frame 'More Data' field"

334 # clear the bits

335 mask = (~0x20) & 0xFF

336 masked = self.header.get_byte(1) & mask

337 # set the bits

338 nb = masked | ((value & 0x01) << 5)

339 self.header.set_byte(1, nb)

340

341 def get_powerManagement(self):

342 "Return 802.11 frame 'Power Management' field"

343 b = self.header.get_byte(1)

344 return ((b >> 4) & 0x01)

345

346 def set_powerManagement(self, value):

347 "Set 802.11 frame 'Power Management' field"

348 # clear the bits

349 mask = (~0x10) & 0xFF

350 masked = self.header.get_byte(1) & mask

351 # set the bits

352 nb = masked | ((value & 0x01) << 4)

353 self.header.set_byte(1, nb)

354

355 def get_retry(self):

356 "Return 802.11 frame 'Retry' field"

357 b = self.header.get_byte(1)

358 return ((b >> 3) & 0x01)

359

360 def set_retry(self, value):

361 "Set 802.11 frame 'Retry' field"

362 # clear the bits

363 mask = (~0x08) & 0xFF

364 masked = self.header.get_byte(1) & mask

365 # set the bits

366 nb = masked | ((value & 0x01) << 3)

367 self.header.set_byte(1, nb)

368

369 def get_moreFrag(self):

370 "Return 802.11 frame 'More Fragments' field"

371 b = self.header.get_byte(1)

372 return ((b >> 2) & 0x01)

373

374 def set_moreFrag(self, value):

375 "Set 802.11 frame 'More Fragments' field"

376 # clear the bits

377 mask = (~0x04) & 0xFF

378 masked = self.header.get_byte(1) & mask

379 # set the bits

380 nb = masked | ((value & 0x01) << 2)

381 self.header.set_byte(1, nb)

382

383 def get_fromDS(self):

384 "Return 802.11 frame 'from DS' field"

385 b = self.header.get_byte(1)

386 return ((b >> 1) & 0x01)

387

388 def set_fromDS(self, value):

389 "Set 802.11 frame 'from DS' field"

390 # clear the bits

391 mask = (~0x02) & 0xFF

392 masked = self.header.get_byte(1) & mask

393 # set the bits

394 nb = masked | ((value & 0x01) << 1)

395 self.header.set_byte(1, nb)

396

397 def get_toDS(self):

398 "Return 802.11 frame 'to DS' field"

399 b = self.header.get_byte(1)

400 return (b & 0x01)

401

402 def set_toDS(self, value):

403 "Set 802.11 frame 'to DS' field"

404 # clear the bits

405 mask = (~0x01) & 0xFF

406 masked = self.header.get_byte(1) & mask

407 # set the bits

408 nb = masked | (value & 0x01)

409 self.header.set_byte(1, nb)

410

411 def get_subtype(self):

412 "Return 802.11 frame 'subtype' field"

413 b = self.header.get_byte(0)

414 return ((b >> 4) & 0x0F)

415

416 def set_subtype(self, value):

417 "Set 802.11 frame 'subtype' field"

418 # clear the bits

419 mask = (~0xF0)&0xFF

420 masked = self.header.get_byte(0) & mask

421 # set the bits

422 nb = masked | ((value << 4) & 0xF0)

423 self.header.set_byte(0, nb)

424

425 def get_type(self):

426 "Return 802.11 frame 'type' field"

427 b = self.header.get_byte(0)

428 return ((b >> 2) & 0x03)

429

430 def set_type(self, value):

431 "Set 802.11 frame 'type' field"

432 # clear the bits

433 mask = (~0x0C)&0xFF

434 masked = self.header.get_byte(0) & mask

435 # set the bits

436 nb = masked | ((value << 2) & 0x0C)

437 self.header.set_byte(0, nb)

438

439 def get_type_n_subtype(self):

440 "Return 802.11 frame 'Type and Subtype' field"

441 b = self.header.get_byte(0)

442 return ((b >> 2) & 0x3F)

443

444 def set_type_n_subtype(self, value):

445 "Set 802.11 frame 'Type and Subtype' field"

446 # clear the bits

447 mask = (~0xFC)&0xFF

448 masked = self.header.get_byte(0) & mask

449 # set the bits

450 nb = masked | ((value << 2) & 0xFC)

451 self.header.set_byte(0, nb)

452

453 def get_version(self):

454 "Return 802.11 frame control 'Protocol version' field"

455 b = self.header.get_byte(0)

456 return (b & 0x03)

457

458 def set_version(self, value):

459 "Set the 802.11 frame control 'Protocol version' field"

460 # clear the bits

461 mask = (~0x03)&0xFF

462 masked = self.header.get_byte(0) & mask

463 # set the bits

464 nb = masked | (value & 0x03)

465 self.header.set_byte(0, nb)

466

467 def compute_checksum(self,bytes):

468 crcle=crc32(bytes)&0xffffffff

469 # ggrr this crc32 is in little endian, convert it to big endian

470 crc=struct.pack('<L', crcle)

471 # Convert to long

472 (crc_long,) = struct.unpack('!L', crc)

473 return crc_long

474

475 def is_QoS_frame(self):

476 "Return 'True' if is an QoS data frame type"

477

478 b = self.header.get_byte(0)

479 return (b & 0x80) and True

480

481 def is_no_framebody_frame(self):

482 "Return 'True' if it frame contain no Frame Body"

483

484 b = self.header.get_byte(0)

485 return (b & 0x40) and True

486

487 def is_cf_poll_frame(self):

488 "Return 'True' if it frame is a CF_POLL frame"

489

490 b = self.header.get_byte(0)

491 return (b & 0x20) and True

492

493 def is_cf_ack_frame(self):

494 "Return 'True' if it frame is a CF_ACK frame"

495

496 b = self.header.get_byte(0)

497 return (b & 0x10) and True

498

499 def get_fcs(self):

500 "Return 802.11 'FCS' field"

501

502 if not self.__FCS_at_end:

503 return None

504

505 b = self.tail.get_long(-4, ">")

506 return b

507

508 def set_fcs(self, value = None):

509 "Set the 802.11 CTS control frame 'FCS' field. If value is None, is auto_checksum"

510

511 if not self.__FCS_at_end:

512 return

513

514 # calculate the FCS

515 if value is None:

516 payload = self.get_body_as_string()

517 crc32=self.compute_checksum(payload)

518 value=crc32

519

520 # set the bits

521 nb = value & 0xFFFFFFFF

522 self.tail.set_long(-4, nb)

523

524 class Dot11ControlFrameCTS(ProtocolPacket):

525 "802.11 Clear-To-Send Control Frame"

526

527 def __init__(self, aBuffer = None):

528 header_size = 8

529 tail_size = 0

530

531 ProtocolPacket.__init__(self, header_size, tail_size)

532 if(aBuffer): 532 ↛ exitline 532 didn't return from function '__init__', because the condition on line 532 was never false

533 self.load_packet(aBuffer)

534

535 def get_duration(self):

536 "Return 802.11 CTS control frame 'Duration' field"

537 b = self.header.get_word(0, "<")

538 return b

539

540 def set_duration(self, value):

541 "Set the 802.11 CTS control frame 'Duration' field"

542 # set the bits

543 nb = value & 0xFFFF

544 self.header.set_word(0, nb, "<")

545

546 def get_ra(self):

547 "Return 802.11 CTS control frame 48 bit 'Receiver Address' field as a 6 bytes array"

548 return self.header.get_bytes()[2:8]

549

550 def set_ra(self, value):

551 "Set 802.11 CTS control frame 48 bit 'Receiver Address' field as a 6 bytes array"

552 for i in range(0, 6):

553 self.header.set_byte(2+i, value[i])

554

555 class Dot11ControlFrameACK(ProtocolPacket):

556 "802.11 Acknowledgement Control Frame"

557

558 def __init__(self, aBuffer = None):

559 header_size = 8

560 tail_size = 0

561

562 ProtocolPacket.__init__(self, header_size, tail_size)

563 if(aBuffer): 563 ↛ exitline 563 didn't return from function '__init__', because the condition on line 563 was never false

564 self.load_packet(aBuffer)

565

566 def get_duration(self):

567 "Return 802.11 ACK control frame 'Duration' field"

568 b = self.header.get_word(0, "<")

569 return b

570

571 def set_duration(self, value):

572 "Set the 802.11 ACK control frame 'Duration' field"

573 # set the bits

574 nb = value & 0xFFFF

575 self.header.set_word(0, nb, "<")

576

577 def get_ra(self):

578 "Return 802.11 ACK control frame 48 bit 'Receiver Address' field as a 6 bytes array"

579 return self.header.get_bytes()[2:8]

580

581 def set_ra(self, value):

582 "Set 802.11 ACK control frame 48 bit 'Receiver Address' field as a 6 bytes array"

583 for i in range(0, 6):

584 self.header.set_byte(2+i, value[i])

585

586 class Dot11ControlFrameRTS(ProtocolPacket):

587 "802.11 Request-To-Send Control Frame"

588

589 def __init__(self, aBuffer = None):

590 header_size = 14

591 tail_size = 0

592

593 ProtocolPacket.__init__(self, header_size, tail_size)

594 if(aBuffer): 594 ↛ exitline 594 didn't return from function '__init__', because the condition on line 594 was never false

595 self.load_packet(aBuffer)

596

597 def get_duration(self):

598 "Return 802.11 RTS control frame 'Duration' field"

599 b = self.header.get_word(0, "<")

600 return b

601

602 def set_duration(self, value):

603 "Set the 802.11 RTS control frame 'Duration' field"

604 # set the bits

605 nb = value & 0xFFFF

606 self.header.set_word(0, nb, "<")

607

608 def get_ra(self):

609 "Return 802.11 RTS control frame 48 bit 'Receiver Address' field as a 6 bytes array"

610 return self.header.get_bytes()[2:8]

611

612 def set_ra(self, value):

613 "Set 802.11 RTS control frame 48 bit 'Receiver Address' field as a 6 bytes array"

614 for i in range(0, 6):

615 self.header.set_byte(2+i, value[i])

616

617 def get_ta(self):

618 "Return 802.11 RTS control frame 48 bit 'Transmitter Address' field as a 6 bytes array"

619 return self.header.get_bytes()[8:14]

620

621 def set_ta(self, value):

622 "Set 802.11 RTS control frame 48 bit 'Transmitter Address' field as a 6 bytes array"

623 for i in range(0, 6):

624 self.header.set_byte(8+i, value[i])

625

626 class Dot11ControlFramePSPoll(ProtocolPacket):

627 "802.11 Power-Save Poll Control Frame"

628

629 def __init__(self, aBuffer = None):

630 header_size = 14

631 tail_size = 0

632

633 ProtocolPacket.__init__(self, header_size, tail_size)

634 if(aBuffer): 634 ↛ exitline 634 didn't return from function '__init__', because the condition on line 634 was never false

635 self.load_packet(aBuffer)

636

637 def get_aid(self):

638 "Return 802.11 PSPoll control frame 'AID' field"

639 # the spec says "The AID value always has its two MSBs each set to 1."

640 # TODO: Should we do check/modify it? Wireshark shows the only MSB to 0

641 b = self.header.get_word(0, "<")

642 return b

643

644 def set_aid(self, value):

645 "Set the 802.11 PSPoll control frame 'AID' field"

646 # set the bits

647 nb = value & 0xFFFF

648 # the spec says "The AID value always has its two MSBs each set to 1."

649 # TODO: Should we do check/modify it? Wireshark shows the only MSB to 0

650 self.header.set_word(0, nb, "<")

651

652 def get_bssid(self):

653 "Return 802.11 PSPoll control frame 48 bit 'BSS ID' field as a 6 bytes array"

654 return self.header.get_bytes()[2:8]

655

656 def set_bssid(self, value):

657 "Set 802.11 PSPoll control frame 48 bit 'BSS ID' field as a 6 bytes array"

658 for i in range(0, 6):

659 self.header.set_byte(2+i, value[i])

660

661 def get_ta(self):

662 "Return 802.11 PSPoll control frame 48 bit 'Transmitter Address' field as a 6 bytes array"

663 return self.header.get_bytes()[8:14]

664

665 def set_ta(self, value):

666 "Set 802.11 PSPoll control frame 48 bit 'Transmitter Address' field as a 6 bytes array"

667 for i in range(0, 6):

668 self.header.set_byte(8+i, value[i])

669

670 class Dot11ControlFrameCFEnd(ProtocolPacket):

671 "802.11 'Contention Free End' Control Frame"

672

673 def __init__(self, aBuffer = None):

674 header_size = 14

675 tail_size = 0

676

677 ProtocolPacket.__init__(self, header_size, tail_size)

678 if(aBuffer): 678 ↛ exitline 678 didn't return from function '__init__', because the condition on line 678 was never false

679 self.load_packet(aBuffer)

680

681 def get_duration(self):

682 "Return 802.11 CF-End control frame 'Duration' field"

683 b = self.header.get_word(0, "<")

684 return b

685

686 def set_duration(self, value):

687 "Set the 802.11 CF-End control frame 'Duration' field"

688 # set the bits

689 nb = value & 0xFFFF

690 self.header.set_word(0, nb, "<")

691

692 def get_ra(self):

693 "Return 802.11 CF-End control frame 48 bit 'Receiver Address' field as a 6 bytes array"

694 return self.header.get_bytes()[2:8]

695

696 def set_ra(self, value):

697 "Set 802.11 CF-End control frame 48 bit 'Receiver Address' field as a 6 bytes array"

698 for i in range(0, 6):

699 self.header.set_byte(2+i, value[i])

700

701 def get_bssid(self):

702 "Return 802.11 CF-End control frame 48 bit 'BSS ID' field as a 6 bytes array"

703 return self.header.get_bytes()[8:14]

704

705 def set_bssid(self, value):

706 "Set 802.11 CF-End control frame 48 bit 'BSS ID' field as a 6 bytes array"

707 for i in range(0, 6):

708 self.header.set_byte(8+i, value[i])

709

710 class Dot11ControlFrameCFEndCFACK(ProtocolPacket):

711 '802.11 \'CF-End + CF-ACK\' Control Frame'

712

713 def __init__(self, aBuffer = None):

714 header_size = 14

715 tail_size = 0

716

717 ProtocolPacket.__init__(self, header_size, tail_size)

718 if(aBuffer): 718 ↛ exitline 718 didn't return from function '__init__', because the condition on line 718 was never false

719 self.load_packet(aBuffer)

720

721 def get_duration(self):

722 'Return 802.11 \'CF-End+CF-ACK\' control frame \'Duration\' field'

723 b = self.header.get_word(0, "<")

724 return b

725

726 def set_duration(self, value):

727 'Set the 802.11 \'CF-End+CF-ACK\' control frame \'Duration\' field'

728 # set the bits

729 nb = value & 0xFFFF

730 self.header.set_word(0, nb, "<")

731

732 def get_ra(self):

733 'Return 802.11 \'CF-End+CF-ACK\' control frame 48 bit \'Receiver Address\' field as a 6 bytes array'

734 return self.header.get_bytes()[2:8]

735

736 def set_ra(self, value):

737 'Set 802.11 \'CF-End+CF-ACK\' control frame 48 bit \'Receiver Address\' field as a 6 bytes array'

738 for i in range(0, 6):

739 self.header.set_byte(2+i, value[i])

740

741 def get_bssid(self):

742 'Return 802.11 \'CF-End+CF-ACK\' control frame 48 bit \'BSS ID\' field as a 6 bytes array'

743 return self.header.get_bytes()[8:16]

744

745 def set_bssid(self, value):

746 'Set 802.11 \'CF-End+CF-ACK\' control frame 48 bit \'BSS ID\' field as a 6 bytes array'

747 for i in range(0, 6):

748 self.header.set_byte(8+i, value[i])

749

750 class Dot11DataFrame(ProtocolPacket):

751 '802.11 Data Frame'

752

753 def __init__(self, aBuffer = None):

754 header_size = 22

755 tail_size = 0

756

757 ProtocolPacket.__init__(self, header_size, tail_size)

758 if(aBuffer):

759 self.load_packet(aBuffer)

760

761 def get_duration(self):

762 'Return 802.11 \'Data\' data frame \'Duration\' field'

763 b = self.header.get_word(0, "<")

764 return b

765

766 def set_duration(self, value):

767 'Set the 802.11 \'Data\' data frame \'Duration\' field'

768 # set the bits

769 nb = value & 0xFFFF

770 self.header.set_word(0, nb, "<")

771

772 def get_address1(self):

773 'Return 802.11 \'Data\' data frame 48 bit \'Address1\' field as a 6 bytes array'

774 return self.header.get_bytes()[2:8]

775

776 def set_address1(self, value):

777 'Set 802.11 \'Data\' data frame 48 bit \'Address1\' field as a 6 bytes array'

778 for i in range(0, 6):

779 self.header.set_byte(2+i, value[i])

780

781 def get_address2(self):

782 'Return 802.11 \'Data\' data frame 48 bit \'Address2\' field as a 6 bytes array'

783 return self.header.get_bytes()[8:14]

784

785 def set_address2(self, value):

786 'Set 802.11 \'Data\' data frame 48 bit \'Address2\' field as a 6 bytes array'

787 for i in range(0, 6):

788 self.header.set_byte(8+i, value[i])

789

790 def get_address3(self):

791 'Return 802.11 \'Data\' data frame 48 bit \'Address3\' field as a 6 bytes array'

792 return self.header.get_bytes()[14: 20]

793

794 def set_address3(self, value):

795 'Set 802.11 \'Data\' data frame 48 bit \'Address3\' field as a 6 bytes array'

796 for i in range(0, 6):

797 self.header.set_byte(14+i, value[i])

798

799 def get_sequence_control(self):

800 'Return 802.11 \'Data\' data frame \'Sequence Control\' field'

801 b = self.header.get_word(20, "<")

802 return b

803

804 def set_sequence_control(self, value):

805 'Set the 802.11 \'Data\' data frame \'Sequence Control\' field'

806 # set the bits

807 nb = value & 0xFFFF

808 self.header.set_word(20, nb, "<")

809

810 def get_fragment_number(self):

811 'Return 802.11 \'Data\' data frame \'Fragment Number\' subfield'

812

813 b = self.header.get_word(20, "<")

814 return (b&0x000F)

815

816 def set_fragment_number(self, value):

817 'Set the 802.11 \'Data\' data frame \'Fragment Number\' subfield'

818 # clear the bits

819 mask = (~0x000F) & 0xFFFF

820 masked = self.header.get_word(20, "<") & mask

821 # set the bits

822 nb = masked | (value & 0x000F)

823 self.header.set_word(20, nb, "<")

824

825 def get_sequence_number(self):

826 'Return 802.11 \'Data\' data frame \'Sequence Number\' subfield'

827

828 b = self.header.get_word(20, "<")

829 return ((b>>4) & 0xFFF)

830

831 def set_sequence_number(self, value):

832 'Set the 802.11 \'Data\' data frame \'Sequence Number\' subfield'

833 # clear the bits

834 mask = (~0xFFF0) & 0xFFFF

835 masked = self.header.get_word(20, "<") & mask

836 # set the bits

837 nb = masked | ((value & 0x0FFF ) << 4 )

838 self.header.set_word(20, nb, "<")

839

840 def get_frame_body(self):

841 'Return 802.11 \'Data\' data frame \'Frame Body\' field'

842

843 return self.get_body_as_string()

844

845 def set_frame_body(self, data):

846 'Set 802.11 \'Data\' data frame \'Frame Body\' field'

847

848 self.load_body(data)

849

850 class Dot11DataQoSFrame(Dot11DataFrame):

851 '802.11 Data QoS Frame'

852

853 def __init__(self, aBuffer = None):

854 header_size = 24

855 tail_size = 0

856

857 ProtocolPacket.__init__(self, header_size, tail_size)

858 if(aBuffer):

859 self.load_packet(aBuffer)

860

861 def get_QoS(self):

862 'Return 802.11 \'Data\' data frame \'QoS\' field'

863 b = self.header.get_word(22, "<")

864 return b

865

866 def set_QoS(self, value):

867 'Set the 802.11 \'Data\' data frame \'QoS\' field'

868 # set the bits

869 nb = value & 0xFFFF

870 self.header.set_word(22, nb, "<")

871

872 class Dot11DataAddr4Frame(Dot11DataFrame):

873 '802.11 Data With ToDS From DS Flags (With Addr 4) Frame'

874

875 def __init__(self, aBuffer = None):

876 header_size = 28

877 tail_size = 0

878

879 ProtocolPacket.__init__(self, header_size, tail_size)

880 if(aBuffer):

881 self.load_packet(aBuffer)

882

883 def get_address4(self):

884 'Return 802.11 \'Data\' data frame 48 bit \'Address4\' field as a 6 bytes array'

885 return self.header.get_bytes()[22:28]

886

887 def set_address4(self, value):

888 'Set 802.11 \'Data\' data frame 48 bit \'Address4\' field as a 6 bytes array'

889 for i in range(0, 6):

890 self.header.set_byte(22+i, value[i])

891

892 class Dot11DataAddr4QoSFrame(Dot11DataAddr4Frame):

893 '802.11 Data With ToDS From DS Flags (With Addr 4) and QoS Frame'

894

895 def __init__(self, aBuffer = None):

896 header_size = 30

897 tail_size = 0

898

899 ProtocolPacket.__init__(self, header_size, tail_size)

900 if(aBuffer):

901 self.load_packet(aBuffer)

902

903 def get_QoS(self):

904 'Return 802.11 \'Data\' data frame \'QoS\' field'

905 b = self.header.get_word(28, "<")

906 return b

907

908 def set_QoS(self, value):

909 'Set the 802.11 \'Data\' data frame \'QoS\' field'

910 # set the bits

911 nb = value & 0xFFFF

912 self.header.set_word(28, nb, "<")

913

914 class SAPTypes():

915 NULL = 0x00

916 LLC_SLMGMT = 0x02

917 SNA_PATHCTRL = 0x04

918 IP = 0x06

919 SNA1 = 0x08

920 SNA2 = 0x0C

921 PROWAY_NM_INIT = 0x0E

922 NETWARE1 = 0x10

923 OSINL1 = 0x14

924 TI = 0x18

925 OSINL2 = 0x20

926 OSINL3 = 0x34

927 SNA3 = 0x40

928 BPDU = 0x42

929 RS511 = 0x4E

930 OSINL4 = 0x54

931 X25 = 0x7E

932 XNS = 0x80

933 BACNET = 0x82

934 NESTAR = 0x86

935 PROWAY_ASLM = 0x8E

936 ARP = 0x98

937 SNAP = 0xAA

938 HPJD = 0xB4

939 VINES1 = 0xBA

940 VINES2 = 0xBC

941 NETWARE2 = 0xE0

942 NETBIOS = 0xF0

943 IBMNM = 0xF4

944 HPEXT = 0xF8

945 UB = 0xFA

946 RPL = 0xFC

947 OSINL5 = 0xFE

948 GLOBAL = 0xFF

949

950 class LLC(ProtocolPacket):

951 '802.2 Logical Link Control (LLC) Frame'

952

953 DLC_UNNUMBERED_FRAMES = 0x03

954

955 def __init__(self, aBuffer = None):

956 header_size = 3

957 tail_size = 0

958

959 ProtocolPacket.__init__(self, header_size, tail_size)

960 if(aBuffer):

961 self.load_packet(aBuffer)

962

963 def get_DSAP(self):

964 "Get the Destination Service Access Point (SAP) from LLC frame"

965 return self.header.get_byte(0)

966

967 def set_DSAP(self, value):

968 "Set the Destination Service Access Point (SAP) of LLC frame"

969 self.header.set_byte(0, value)

970

971 def get_SSAP(self):

972 "Get the Source Service Access Point (SAP) from LLC frame"

973 return self.header.get_byte(1)

974

975 def set_SSAP(self, value):

976 "Set the Source Service Access Point (SAP) of LLC frame"

977 self.header.set_byte(1, value)

978

979 def get_control(self):

980 "Get the Control field from LLC frame"

981 return self.header.get_byte(2)

982

983 def set_control(self, value):

984 "Set the Control field of LLC frame"

985 self.header.set_byte(2, value)

986

987 class SNAP(ProtocolPacket):

988 '802.2 SubNetwork Access Protocol (SNAP) Frame'

989

990 def __init__(self, aBuffer = None):

991 header_size = 5

992 tail_size = 0

993

994 ProtocolPacket.__init__(self, header_size, tail_size)

995 if(aBuffer):

996 self.load_packet(aBuffer)

997

998 def get_OUI(self):

999 "Get the three-octet Organizationally Unique Identifier (OUI) SNAP frame"

1000 b = array_tobytes(self.header.get_bytes()[0:3])

1001 #unpack requires a string argument of length 4 and b is 3 bytes long

1002 (oui,) = struct.unpack('!L', b'\x00'+b)

1003 return oui

1004

1005 def set_OUI(self, value):

1006 "Set the three-octet Organizationally Unique Identifier (OUI) SNAP frame"

1007 # clear the bits

1008 mask = ((~0xFFFFFF00) & 0xFF)

1009 masked = self.header.get_long(0, ">") & mask

1010 # set the bits

1011 nb = masked | ((value & 0x00FFFFFF) << 8)

1012 self.header.set_long(0, nb)

1013

1014 def get_protoID(self):

1015 "Get the two-octet Protocol Identifier (PID) SNAP field"

1016 return self.header.get_word(3, ">")

1017

1018 def set_protoID(self, value):

1019 "Set the two-octet Protocol Identifier (PID) SNAP field"

1020 self.header.set_word(3, value, ">")

1021

1022 class Dot11WEP(ProtocolPacket):

1023 '802.11 WEP'

1024

1025 def __init__(self, aBuffer = None):

1026 header_size = 4

1027 tail_size = 0

1028

1029 ProtocolPacket.__init__(self, header_size, tail_size)

1030 if(aBuffer):

1031 self.load_packet(aBuffer)

1032

1033 def is_WEP(self):

1034 'Return True if it\'s a WEP'

1035 # We already know that it's private.

1036 # Now we must differentiate between WEP and WPA/WPA2

1037 # WPA/WPA2 have the ExtIV (Bit 5) enaled and WEP disabled

1038 b = self.header.get_byte(3)

1039 return not (b & 0x20)

1040

1041 def get_iv(self):

1042 'Return the \'WEP IV\' field'

1043 b = array_tobytes(self.header.get_bytes()[0:3])

1044 #unpack requires a string argument of length 4 and b is 3 bytes long

1045 (iv,) = struct.unpack('!L', b'\x00'+b)

1046 return iv

1047

1048 def set_iv(self, value):

1049 'Set the \'WEP IV\' field.'

1050 # clear the bits

1051 mask = ((~0xFFFFFF00) & 0xFF)

1052 masked = self.header.get_long(0, ">") & mask

1053 # set the bits

1054 nb = masked | ((value & 0x00FFFFFF) << 8)

1055 self.header.set_long(0, nb)

1056

1057 def get_keyid(self):

1058 'Return the \'WEP KEY ID\' field'

1059 b = self.header.get_byte(3)

1060 return ((b>>6) & 0x03)

1061

1062 def set_keyid(self, value):

1063 'Set the \'WEP KEY ID\' field'

1064 # clear the bits

1065 mask = (~0xC0) & 0xFF

1066 masked = self.header.get_byte(3) & mask

1067 # set the bits

1068 nb = masked | ((value & 0x03) << 6)

1069 self.header.set_byte(3, nb)

1070

1071 def get_decrypted_data(self, key_string):

1072 'Return \'WEP Data\' field decrypted'

1073

1074 # Needs to be at least 8 bytes of payload

1075 if len(self.body_string)<8: 1075 ↛ 1076line 1075 didn't jump to line 1076, because the condition on line 1075 was never true

1076 return self.body_string

1077

1078 # initialize the first bytes of the key from the IV

1079 # and copy rest of the WEP key (the secret part)

1080

1081 # Convert IV to 3 bytes long string

1082 iv=struct.pack('>L',self.get_iv())[-3:]

1083 key=iv+key_string

1084 rc4=RC4(key)

1085 decrypted_data=rc4.decrypt(self.body_string)

1086

1087 return decrypted_data

1088

1089 def get_encrypted_data(self, key_string):

1090 # RC4 is symmetric

1091 return self.get_decrypted_data(key_string)

1092

1093 def encrypt_frame(self, key_string):

1094 enc = self.get_encrypted_data(key_string)

1095 self.load_body(enc)

1096

1097 class Dot11WEPData(ProtocolPacket):

1098 '802.11 WEP Data Part'

1099

1100 def __init__(self, aBuffer = None):

1101 header_size = 0

1102 tail_size = 4

1103

1104 ProtocolPacket.__init__(self, header_size, tail_size)

1105 if(aBuffer):

1106 self.load_packet(aBuffer)

1107

1108 def get_icv(self):

1109 "Return 'WEP ICV' field"

1110

1111 b = self.tail.get_long(-4, ">")

1112 return b

1113

1114 def set_icv(self, value = None):

1115 "Set 'WEP ICV' field"

1116

1117 # Compute the WEP ICV

1118 if value is None: 1118 ↛ 1119line 1118 didn't jump to line 1119, because the condition on line 1118 was never true

1119 value=self.get_computed_icv()

1120

1121 # set the bits

1122 nb = value & 0xFFFFFFFF

1123 self.tail.set_long(-4, nb)

1124

1125 def get_computed_icv(self):

1126 crcle=crc32(self.body_string)&0xffffffff

1127 # This crc32 is in little endian, convert it to big endian

1128 crc=struct.pack('<L', crcle)

1129 # Convert to long

1130 (crc_long,) = struct.unpack('!L', crc)

1131 return crc_long

1132

1133 def check_icv(self):

1134 computed_icv=self.get_computed_icv()

1135 current_icv=self.get_icv()

1136 if computed_icv==current_icv: 1136 ↛ 1139line 1136 didn't jump to line 1139, because the condition on line 1136 was never false

1137 return True

1138 else:

1139 return False

1140

1141 class Dot11WPA(ProtocolPacket):

1142 '802.11 WPA'

1143

1144 def __init__(self, aBuffer = None):

1145 header_size = 8

1146 tail_size = 0

1147

1148 ProtocolPacket.__init__(self, header_size, tail_size)

1149 if(aBuffer): 1149 ↛ exitline 1149 didn't return from function '__init__', because the condition on line 1149 was never false

1150 self.load_packet(aBuffer)

1151

1152 def is_WPA(self):

1153 'Return True if it\'s a WPA'

1154 # Now we must differentiate between WPA and WPA2

1155 # In WPA WEPSeed is set to (TSC1 | 0x20) & 0x7f.

1156 b = self.get_WEPSeed() == ((self.get_TSC1() | 0x20 ) & 0x7f)

1157 return (b and self.get_extIV())

1158

1159 def get_keyid(self):

1160 'Return the \'WPA KEY ID\' field'

1161 b = self.header.get_byte(3)

1162 return ((b>>6) & 0x03)

1163

1164 def set_keyid(self, value):

1165 'Set the \'WPA KEY ID\' field'

1166 # clear the bits

1167 mask = (~0xC0) & 0xFF

1168 masked = self.header.get_byte(3) & mask

1169 # set the bits

1170 nb = masked | ((value & 0x03) << 6)

1171 self.header.set_byte(3, nb)

1172

1173 def get_decrypted_data(self):

1174 'Return \'WPA Data\' field decrypted'

1175 # TODO: Replace it with the decoded string

1176 return self.body_string

1177

1178 def get_TSC1(self):

1179 'Return the \'WPA TSC1\' field'

1180 b = self.header.get_byte(0)

1181 return (b & 0xFF)

1182

1183 def set_TSC1(self, value):

1184 'Set the \'WPA TSC1\' field'

1185 # set the bits

1186 nb = (value & 0xFF)

1187 self.header.set_byte(0, nb)

1188

1189 def get_WEPSeed(self):

1190 'Return the \'WPA WEPSeed\' field'

1191 b = self.header.get_byte(1)

1192 return (b & 0xFF)

1193

1194 def set_WEPSeed(self, value):

1195 'Set the \'WPA WEPSeed\' field'

1196 # set the bits

1197 nb = (value & 0xFF)

1198 self.header.set_byte(1, nb)

1199

1200 def get_TSC0(self):

1201 'Return the \'WPA TSC0\' field'

1202 b = self.header.get_byte(2)

1203 return (b & 0xFF)

1204

1205 def set_TSC0(self, value):

1206 'Set the \'WPA TSC0\' field'

1207 # set the bits

1208 nb = (value & 0xFF)

1209 self.header.set_byte(2, nb)

1210

1211 def get_extIV(self):

1212 'Return the \'WPA extID\' field'

1213 b = self.header.get_byte(3)

1214 return ((b>>5) & 0x1)

1215

1216 def set_extIV(self, value):

1217 'Set the \'WPA extID\' field'

1218 # clear the bits

1219 mask = (~0x20) & 0xFF

1220 masked = self.header.get_byte(3) & mask

1221 # set the bits

1222 nb = masked | ((value & 0x01) << 5)

1223 self.header.set_byte(3, nb)

1224

1225 def get_TSC2(self):

1226 'Return the \'WPA TSC2\' field'

1227 b = self.header.get_byte(4)

1228 return (b & 0xFF)

1229

1230 def set_TSC2(self, value):

1231 'Set the \'WPA TSC2\' field'

1232 # set the bits

1233 nb = (value & 0xFF)

1234 self.header.set_byte(4, nb)

1235

1236 def get_TSC3(self):

1237 'Return the \'WPA TSC3\' field'

1238 b = self.header.get_byte(5)

1239 return (b & 0xFF)

1240

1241 def set_TSC3(self, value):

1242 'Set the \'WPA TSC3\' field'

1243 # set the bits

1244 nb = (value & 0xFF)

1245 self.header.set_byte(5, nb)

1246

1247 def get_TSC4(self):

1248 'Return the \'WPA TSC4\' field'

1249 b = self.header.get_byte(6)

1250 return (b & 0xFF)

1251

1252 def set_TSC4(self, value):

1253 'Set the \'WPA TSC4\' field'

1254 # set the bits

1255 nb = (value & 0xFF)

1256 self.header.set_byte(6, nb)

1257

1258 def get_TSC5(self):

1259 'Return the \'WPA TSC5\' field'

1260 b = self.header.get_byte(7)

1261 return (b & 0xFF)

1262

1263 def set_TSC5(self, value):

1264 'Set the \'WPA TSC5\' field'

1265 # set the bits

1266 nb = (value & 0xFF)

1267 self.header.set_byte(7, nb)

1268

1269 class Dot11WPAData(ProtocolPacket):

1270 '802.11 WPA Data Part'

1271

1272 def __init__(self, aBuffer = None):

1273 header_size = 0

1274 tail_size = 12

1275

1276 ProtocolPacket.__init__(self, header_size, tail_size)

1277 if(aBuffer): 1277 ↛ exitline 1277 didn't return from function '__init__', because the condition on line 1277 was never false

1278 self.load_packet(aBuffer)

1279

1280 def get_icv(self):

1281 "Return 'WPA ICV' field"

1282

1283 b = self.tail.get_long(-4, ">")

1284 return b

1285

1286 def set_icv(self, value = None):

1287 "Set 'WPA ICV' field"

1288

1289 # calculate the FCS

1290 if value is None:

1291 value=self.compute_checksum(self.body_string)

1292

1293 # set the bits

1294 nb = value & 0xFFFFFFFF

1295 self.tail.set_long(-4, nb)

1296

1297 def get_MIC(self):

1298 'Return the \'WPA2Data MIC\' field'

1299 return self.get_tail_as_string()[:8]

1300

1301 def set_MIC(self, value):

1302 'Set the \'WPA2Data MIC\' field'

1303 #Padding to 8 bytes with 0x00's

1304 value.ljust(8,b'\x00')

1305 #Stripping to 8 bytes

1306 value=value[:8]

1307 icv=self.tail.get_buffer_as_string()[-4:]

1308 self.tail.set_bytes_from_string(value+icv)

1309

1310 class Dot11WPA2(ProtocolPacket):

1311 '802.11 WPA2'

1312

1313 def __init__(self, aBuffer = None):

1314 header_size = 8

1315 tail_size = 0

1316

1317 ProtocolPacket.__init__(self, header_size, tail_size)

1318 if(aBuffer): 1318 ↛ exitline 1318 didn't return from function '__init__', because the condition on line 1318 was never false

1319 self.load_packet(aBuffer)

1320

1321 def is_WPA2(self):

1322 'Return True if it\'s a WPA2'

1323 # Now we must differentiate between WPA and WPA2

1324 # In WPA WEPSeed is set to (TSC1 | 0x20) & 0x7f.

1325 # In WPA2 WEPSeed=PN1 and TSC1=PN0

1326 b = self.get_PN1() == ((self.get_PN0() | 0x20 ) & 0x7f)

1327 return (not b and self.get_extIV())

1328

1329 def get_extIV(self):

1330 'Return the \'WPA2 extID\' field'

1331 b = self.header.get_byte(3)

1332 return ((b>>5) & 0x1)

1333

1334 def set_extIV(self, value):

1335 'Set the \'WPA2 extID\' field'

1336 # clear the bits

1337 mask = (~0x20) & 0xFF

1338 masked = self.header.get_byte(3) & mask

1339 # set the bits

1340 nb = masked | ((value & 0x01) << 5)

1341 self.header.set_byte(3, nb)

1342

1343 def get_keyid(self):

1344 'Return the \'WPA2 KEY ID\' field'

1345 b = self.header.get_byte(3)

1346 return ((b>>6) & 0x03)

1347

1348 def set_keyid(self, value):

1349 'Set the \'WPA2 KEY ID\' field'

1350 # clear the bits

1351 mask = (~0xC0) & 0xFF

1352 masked = self.header.get_byte(3) & mask

1353 # set the bits

1354 nb = masked | ((value & 0x03) << 6)

1355 self.header.set_byte(3, nb)

1356

1357 def get_decrypted_data(self):

1358 'Return \'WPA2 Data\' field decrypted'

1359 # TODO: Replace it with the decoded string

1360 return self.body_string

1361

1362 def get_PN0(self):

1363 'Return the \'WPA2 PN0\' field'

1364 b = self.header.get_byte(0)

1365 return (b & 0xFF)

1366

1367 def set_PN0(self, value):

1368 'Set the \'WPA2 PN0\' field'

1369 # set the bits

1370 nb = (value & 0xFF)

1371 self.header.set_byte(0, nb)

1372

1373 def get_PN1(self):

1374 'Return the \'WPA2 PN1\' field'

1375 b = self.header.get_byte(1)

1376 return (b & 0xFF)

1377

1378 def set_PN1(self, value):

1379 'Set the \'WPA2 PN1\' field'

1380 # set the bits

1381 nb = (value & 0xFF)

1382 self.header.set_byte(1, nb)

1383

1384 def get_PN2(self):

1385 'Return the \'WPA2 PN2\' field'

1386 b = self.header.get_byte(4)

1387 return (b & 0xFF)

1388

1389 def set_PN2(self, value):

1390 'Set the \'WPA2 PN2\' field'

1391 # set the bits

1392 nb = (value & 0xFF)

1393 self.header.set_byte(4, nb)

1394

1395 def get_PN3(self):

1396 'Return the \'WPA2 PN3\' field'

1397 b = self.header.get_byte(5)

1398 return (b & 0xFF)

1399

1400 def set_PN3(self, value):

1401 'Set the \'WPA2 PN3\' field'

1402 # set the bits

1403 nb = (value & 0xFF)

1404 self.header.set_byte(5, nb)

1405

1406 def get_PN4(self):

1407 'Return the \'WPA2 PN4\' field'

1408 b = self.header.get_byte(6)

1409 return (b & 0xFF)

1410

1411 def set_PN4(self, value):

1412 'Set the \'WPA2 PN4\' field'

1413 # set the bits

1414 nb = (value & 0xFF)

1415 self.header.set_byte(6, nb)

1416

1417 def get_PN5(self):

1418 'Return the \'WPA2 PN5\' field'

1419 b = self.header.get_byte(7)

1420 return (b & 0xFF)

1421

1422 def set_PN5(self, value):

1423 'Set the \'WPA2 PN5\' field'

1424 # set the bits

1425 nb = (value & 0xFF)

1426 self.header.set_byte(7, nb)

1427

1428 class Dot11WPA2Data(ProtocolPacket):

1429 '802.11 WPA2 Data Part'

1430

1431 def __init__(self, aBuffer = None):

1432 header_size = 0

1433 tail_size = 8

1434

1435 ProtocolPacket.__init__(self, header_size, tail_size)

1436 if(aBuffer): 1436 ↛ exitline 1436 didn't return from function '__init__', because the condition on line 1436 was never false

1437 self.load_packet(aBuffer)

1438

1439 def get_MIC(self):

1440 'Return the \'WPA2Data MIC\' field'

1441 return self.get_tail_as_string()

1442

1443 def set_MIC(self, value):

1444 'Set the \'WPA2Data MIC\' field'

1445 #Padding to 8 bytes with 0x00's

1446 value.ljust(8,b'\x00')

1447 #Stripping to 8 bytes

1448 value=value[:8]

1449 self.tail.set_bytes_from_string(value)

1450

1451 class RadioTap(ProtocolPacket):

1452 __HEADER_BASE_SIZE = 8 # minimal header size

1453 _PRESENT_FLAGS_SIZE = 4

1454 _BASE_PRESENT_FLAGS_OFFSET = 4

1455

1456 class __RadioTapField(object):

1457 ALIGNMENT = 1

1458

1459 def __str__( self ):

1460 return str( self.__class__.__name__ )

1461

1462 class RTF_TSFT(__RadioTapField):

1463 BIT_NUMBER = 0

1464 STRUCTURE = "<Q"

1465 ALIGNMENT = 8

1466

1467 class RTF_FLAGS(__RadioTapField):

1468 BIT_NUMBER = 1

1469 STRUCTURE = "<B"

1470

1471 # https://web.archive.org/web/20160423125307/www.radiotap.org/defined-fields/Flags

1472 PROPERTY_CFP = 0x01 #sent/received during CFP

1473 PROPERTY_SHORTPREAMBLE = 0x02 #sent/received with short preamble

1474 PROPERTY_WEP = 0x04 #sent/received with WEP encryption

1475 PROPERTY_FRAGMENTATION = 0x08 #sent/received with fragmentation

1476 PROPERTY_FCS_AT_END = 0x10 #frame includes FCS

1477 PROPERTY_PAYLOAD_PADDING= 0x20 #frame has padding between 802.11 header and payload (to 32-bit boundary)

1478 PROPERTY_BAD_FCS = 0x40 #does not pass FCS check

1479 PROPERTY_SHORT_GI = 0x80 #frame used short guard interval (HT). Unspecified but used:

1480

1481 class RTF_RATE(__RadioTapField):

1482 BIT_NUMBER = 2

1483 STRUCTURE = "<B"

1484

1485 class RTF_CHANNEL(__RadioTapField):

1486 BIT_NUMBER = 3

1487 STRUCTURE = "<HH"

1488 ALIGNMENT = 2

1489

1490 class RTF_FHSS(__RadioTapField):

1491 BIT_NUMBER = 4

1492 STRUCTURE = "<BB"

1493

1494 class RTF_DBM_ANTSIGNAL(__RadioTapField):

1495 BIT_NUMBER = 5

1496 STRUCTURE = "<B"

1497

1498 class RTF_DBM_ANTNOISE(__RadioTapField):

1499 BIT_NUMBER = 6

1500 STRUCTURE = "<B"

1501

1502 class RTF_LOCK_QUALITY(__RadioTapField):

1503 BIT_NUMBER = 7

1504 STRUCTURE = "<H"

1505 ALIGNMENT = 2

1506

1507 class RTF_TX_ATTENUATION(__RadioTapField):

1508 BIT_NUMBER = 8

1509 STRUCTURE = "<H"

1510 ALIGNMENT = 2

1511

1512 class RTF_DB_TX_ATTENUATION(__RadioTapField):

1513 BIT_NUMBER = 9

1514 STRUCTURE = "<H"

1515 ALIGNMENT = 2

1516

1517 class RTF_DBM_TX_POWER(__RadioTapField):

1518 BIT_NUMBER = 10

1519 STRUCTURE = "<b"

1520 ALIGNMENT = 2

1521

1522 class RTF_ANTENNA(__RadioTapField):

1523 BIT_NUMBER = 11

1524 STRUCTURE = "<B"

1525

1526 class RTF_DB_ANTSIGNAL(__RadioTapField):

1527 BIT_NUMBER = 12

1528 STRUCTURE = "<B"

1529

1530 class RTF_DB_ANTNOISE(__RadioTapField):

1531 BIT_NUMBER = 13

1532 STRUCTURE = "<B"

1533

1534## # official assignment, clashes with RTF_FCS_IN_HEADER

1535## class RTF_RX_FLAGS(__RadioTapField):

1536## BIT_NUMBER = 14

1537## STRUCTURE = "<H"

1538## ALIGNMENT = 2

1539

1540 # clashes with RTF_RX_FLAGS

1541 class RTF_FCS_IN_HEADER(__RadioTapField):

1542 BIT_NUMBER = 14

1543 STRUCTURE = "<L"

1544 ALIGNMENT = 4

1545

1546 # clashes with HARDWARE_QUEUE

1547 class RTF_TX_FLAGS(__RadioTapField):

1548 BIT_NUMBER = 15

1549 STRUCTURE = "<H"

1550 ALIGNMENT = 2

1551

1552## # clashes with TX_FLAGS

1553## class RTF_HARDWARE_QUEUE(__RadioTapField):

1554## BIT_NUMBER = 15

1555## STRUCTURE = "<B"

1556## ALIGNMENT = 1

1557

1558 # clashes with RSSI

1559 class RTF_RTS_RETRIES(__RadioTapField):

1560 BIT_NUMBER = 16

1561 STRUCTURE = "<B"

1562

1563## # clashes with RTS_RETRIES

1564## class RTF_RSSI(__RadioTapField):

1565## BIT_NUMBER = 16

1566## STRUCTURE = "<H"

1567## ALIGNMENT = 1

1568

1569 class RTF_DATA_RETRIES(__RadioTapField):

1570 BIT_NUMBER = 17

1571 STRUCTURE = "<B"

1572

1573 class RTF_XCHANNEL(__RadioTapField):

1574 BIT_NUMBER = 18

1575 STRUCTURE = "<LHBB"

1576 ALIGNMENT = 4

1577

1578 class RTF_EXT(__RadioTapField):

1579 BIT_NUMBER = 31

1580 STRUCTURE = []

1581

1582 # Sort the list so the 'for' statement walk the list in the right order

1583 radiotap_fields = __RadioTapField.__subclasses__()

1584 radiotap_fields.sort(key= lambda x: x.BIT_NUMBER)

1585

1586 def __init__(self, aBuffer = None):

1587 header_size = self.__HEADER_BASE_SIZE

1588 tail_size = 0

1589

1590 if aBuffer:

1591 length = struct.unpack('<H', aBuffer[2:4])[0]

1592 header_size=length

1593

1594 ProtocolPacket.__init__(self, header_size, tail_size)

1595 self.load_packet(aBuffer)

1596 else:

1597 ProtocolPacket.__init__(self, header_size, tail_size)

1598 self.set_version(0)

1599 self.__set_present(0x00000000)

1600

1601 def get_header_length(self):

1602 'Return the RadioTap header \'length\' field'

1603 self.__update_header_length()

1604 return self.header.get_word(2, "<")

1605

1606 def get_version(self):

1607 'Return the \'version\' field'

1608 b = self.header.get_byte(0)

1609 return b

1610

1611 def set_version(self, value):

1612 'Set the \'version\' field'

1613 nb = (value & 0xFF)

1614 self.header.set_byte(0, nb)

1615

1616 nb = (value & 0xFF)

1617

1618 def get_present(self, offset=_BASE_PRESENT_FLAGS_OFFSET):

1619 "Return RadioTap present bitmap field"

1620 present = self.header.get_long(offset, "<")

1621 return present

1622

1623 def __set_present(self, value):

1624 "Set RadioTap present field bit"

1625 self.header.set_long(4, value)

1626

1627 def get_present_bit(self, field, offset=4):

1628 'Get a \'present\' field bit'

1629 present=self.get_present(offset)

1630 return not not (2**field.BIT_NUMBER & present)

1631

1632 def __set_present_bit(self, field):

1633 'Set a \'present\' field bit'

1634 npresent=2**field.BIT_NUMBER | self.get_present()

1635 self.header.set_long(4, npresent,'<')

1636

1637 def __unset_present_bit(self, field):

1638 'Unset a \'present\' field bit'

1639 npresent=~(2**field.BIT_NUMBER) & self.get_present()

1640 self.header.set_long(4, npresent,'<')

1641

1642 def __align(self, val, align):

1643 return ( (((val) + ((align) - 1)) & ~((align) - 1)) - val )

1644

1645 def __get_field_position(self, field):

1646

1647 offset = RadioTap._BASE_PRESENT_FLAGS_OFFSET

1648 extra_present_flags_count = 0

1649 while self.get_present_bit(RadioTap.RTF_EXT, offset):

1650 offset += RadioTap._PRESENT_FLAGS_SIZE

1651 extra_present_flags_count += 1

1652

1653 field_position = self.__HEADER_BASE_SIZE + (RadioTap._BASE_PRESENT_FLAGS_OFFSET * extra_present_flags_count)

1654

1655 for f in self.radiotap_fields: 1655 ↛ 1664line 1655 didn't jump to line 1664, because the loop on line 1655 didn't complete

1656 field_position += self.__align(field_position, f.ALIGNMENT)

1657 if f == field:

1658 return field_position

1659

1660 if self.get_present_bit(f):

1661 total_length = struct.calcsize(f.STRUCTURE)

1662 field_position += total_length

1663

1664 return None

1665

1666 def unset_field( self, field):

1667 is_present=self.get_present_bit(field)

1668 if is_present is False: 1668 ↛ 1669line 1668 didn't jump to line 1669, because the condition on line 1668 was never true

1669 return False

1670

1671 byte_pos=self.__get_field_position(field)

1672 if not byte_pos: 1672 ↛ 1673line 1672 didn't jump to line 1673, because the condition on line 1672 was never true

1673 return False

1674

1675 self.__unset_present_bit(field)

1676

1677 header=self.get_header_as_string()

1678 total_length = struct.calcsize(field.STRUCTURE)

1679 header=header[:byte_pos]+header[byte_pos+total_length:]

1680

1681 self.load_header(header)

1682

1683 def __get_field_values( self, field ):

1684 is_present=self.get_present_bit(field)

1685 if is_present is False:

1686 return None

1687

1688 byte_pos=self.__get_field_position(field)

1689 header=self.get_header_as_string()

1690 total_length=struct.calcsize(field.STRUCTURE)

1691 v=header[ byte_pos: byte_pos+total_length ]

1692

1693 field_values = struct.unpack(field.STRUCTURE, v)

1694

1695 return field_values

1696

1697 def __set_field_values( self, field, values ):

1698 if not hasattr(values,'__iter__'): 1698 ↛ 1699line 1698 didn't jump to line 1699, because the condition on line 1698 was never true

1699 raise Exception("arg 'values' is not iterable")

1700

1701 # It's for to known the qty of argument of a structure

1702 num_fields=len(''.join(c for c in field.STRUCTURE if c not in '=@!<>'))

1703

1704 if len(values)!=num_fields: 1704 ↛ 1705line 1704 didn't jump to line 1705, because the condition on line 1704 was never true

1705 raise Exception("Field %s has exactly %d items"%(str(field),struct.calcsize(field.STRUCTURE)))

1706

1707 is_present=self.get_present_bit(field)

1708 if is_present is False:

1709 self.__set_present_bit(field)

1710

1711 byte_pos=self.__get_field_position(field)

1712 header=self.get_header_as_string()

1713 total_length=struct.calcsize(field.STRUCTURE)

1714

1715 new_str = struct.pack(field.STRUCTURE, *values)

1716

1717 if is_present is True:

1718 header=header[:byte_pos]+new_str+header[byte_pos+total_length:]

1719 else:

1720 header=header[:byte_pos]+new_str+header[byte_pos:]

1721 self.load_header(header)

1722

1723

1724 def set_tsft( self, nvalue ):

1725 "Set the Value in microseconds of the MAC's 64-bit 802.11 "\

1726 "Time Synchronization Function timer when the first bit of "\

1727 "the MPDU arrived at the MAC"

1728 self.__set_field_values(RadioTap.RTF_TSFT, [nvalue])

1729

1730 def get_tsft( self ):

1731 "Get the Value in microseconds of the MAC's 64-bit 802.11 "\

1732 "Time Synchronization Function timer when the first bit of "\

1733 "the MPDU arrived at the MAC"

1734

1735 values=self.__get_field_values(RadioTap.RTF_TSFT)

1736 if not values:

1737 return None

1738 return values[0]

1739

1740 def set_flags( self, nvalue ):

1741 "Set the properties of transmitted and received frames."

1742 self.__set_field_values(self.RTF_FLAGS, [nvalue])

1743

1744 def get_flags( self ):

1745 "Get the properties of transmitted and received frames."

1746 values=self.__get_field_values(self.RTF_FLAGS)

1747 if not values: 1747 ↛ 1748line 1747 didn't jump to line 1748, because the condition on line 1747 was never true

1748 return None

1749 return values[0]

1750

1751 def set_rate( self, nvalue ):

1752 "Set the TX/RX data rate in 500 Kbps units"

1753

1754 self.__set_field_values(self.RTF_RATE, [nvalue])

1755

1756 def get_rate( self ):

1757 "Get the TX/RX data rate in 500 Kbps units"

1758

1759 values=self.__get_field_values(self.RTF_RATE)

1760 if not values: 1760 ↛ 1761line 1760 didn't jump to line 1761, because the condition on line 1760 was never true

1761 return None

1762 return values[0]

1763

1764 def set_channel( self, freq, flags ):

1765 "Set the channel Tx/Rx frequency in MHz and the channel flags"

1766

1767 self.__set_field_values(self.RTF_CHANNEL, [freq, flags])

1768

1769 def get_channel( self ):

1770 "Get the TX/RX data rate in 500 Kbps units"

1771

1772 values=self.__get_field_values(self.RTF_CHANNEL)

1773

1774 return values

1775

1776 def set_FHSS( self, hop_set, hop_pattern ):

1777 "Set the hop set and pattern for frequency-hopping radios"

1778

1779 self.__set_field_values(self.RTF_FHSS, [hop_set, hop_pattern])

1780

1781 def get_FHSS( self ):

1782 "Get the hop set and pattern for frequency-hopping radios"

1783

1784 values=self.__get_field_values(self.RTF_FHSS)

1785

1786 return values

1787

1788 def set_dBm_ant_signal( self, signal ):

1789 "Set the RF signal power at the antenna, decibel difference from an "\

1790 "arbitrary, fixed reference."

1791

1792 self.__set_field_values(self.RTF_DBM_ANTSIGNAL, [signal])

1793

1794 def get_dBm_ant_signal( self ):

1795 "Get the RF signal power at the antenna, decibel difference from an "\

1796 "arbitrary, fixed reference."

1797

1798 values=self.__get_field_values(self.RTF_DBM_ANTSIGNAL)

1799 if not values:

1800 return None

1801 return values[0]

1802

1803 def set_dBm_ant_noise( self, signal ):

1804 "Set the RF noise power at the antenna, decibel difference from an "\

1805 "arbitrary, fixed reference."

1806

1807 self.__set_field_values(self.RTF_DBM_ANTNOISE, [signal])

1808

1809 def get_dBm_ant_noise( self ):

1810 "Get the RF noise power at the antenna, decibel difference from an "\

1811 "arbitrary, fixed reference."

1812

1813 values=self.__get_field_values(self.RTF_DBM_ANTNOISE)

1814 if not values:

1815 return None

1816 return values[0]

1817

1818 def set_lock_quality( self, quality ):

1819 "Set the quality of Barker code lock. "\

1820 "Called 'Signal Quality' in datasheets. "

1821

1822 self.__set_field_values(self.RTF_LOCK_QUALITY, [quality])

1823

1824 def get_lock_quality( self ):

1825 "Get the quality of Barker code lock. "\

1826 "Called 'Signal Quality' in datasheets. "

1827

1828 values=self.__get_field_values(self.RTF_LOCK_QUALITY)

1829 if not values:

1830 return None

1831 return values[0]

1832

1833 def set_tx_attenuation( self, power ):

1834 "Set the transmit power expressed as unitless distance from max power "\

1835 "set at factory calibration. 0 is max power."

1836

1837 self.__set_field_values(self.RTF_TX_ATTENUATION, [power])

1838

1839 def get_tx_attenuation( self ):

1840 "Set the transmit power expressed as unitless distance from max power "\

1841 "set at factory calibration. 0 is max power."

1842

1843 values=self.__get_field_values(self.RTF_TX_ATTENUATION)

1844 if not values:

1845 return None

1846 return values[0]

1847

1848 def set_dB_tx_attenuation( self, power ):

1849 "Set the transmit power expressed as decibel distance from max power "\

1850 "set at factory calibration. 0 is max power. "

1851

1852 self.__set_field_values(self.RTF_DB_TX_ATTENUATION, [power])

1853

1854 def get_dB_tx_attenuation( self ):

1855 "Set the transmit power expressed as decibel distance from max power "\

1856 "set at factory calibration. 0 is max power. "

1857

1858 values=self.__get_field_values(self.RTF_DB_TX_ATTENUATION)

1859 if not values:

1860 return None

1861 return values[0]

1862

1863 def set_dBm_tx_power( self, power ):

1864 "Set the transmit power expressed as dBm (decibels from a 1 milliwatt"\

1865 " reference). This is the absolute power level measured at the "\

1866 "antenna port."

1867

1868 self.__set_field_values(self.RTF_DBM_TX_POWER, [power])

1869

1870 def get_dBm_tx_power( self ):

1871 "Get the transmit power expressed as dBm (decibels from a 1 milliwatt"\

1872 " reference). This is the absolute power level measured at the "\

1873 "antenna port."

1874

1875 values=self.__get_field_values(self.RTF_DBM_TX_POWER)

1876 if not values:

1877 return None

1878 return values[0]

1879

1880 def set_antenna( self, antenna_index ):

1881 "Set Rx/Tx antenna index for this packet. "\

1882 "The first antenna is antenna 0. "\

1883

1884 self.__set_field_values(self.RTF_ANTENNA, [antenna_index])

1885

1886 def get_antenna( self ):

1887 "Set Rx/Tx antenna index for this packet. "\

1888 "The first antenna is antenna 0. "\

1889

1890 values=self.__get_field_values(self.RTF_ANTENNA)

1891 if not values: 1891 ↛ 1892line 1891 didn't jump to line 1892, because the condition on line 1891 was never true

1892 return None

1893 return values[0]

1894

1895 def set_dB_ant_signal( self, signal ):

1896 "Set the RF signal power at the antenna, decibel difference from an "\

1897 "arbitrary, fixed reference."

1898

1899 self.__set_field_values(self.RTF_DB_ANTSIGNAL, [signal])

1900

1901 def get_dB_ant_signal( self ):

1902 "Get the RF signal power at the antenna, decibel difference from an "\

1903 "arbitrary, fixed reference."

1904

1905 values=self.__get_field_values(self.RTF_DB_ANTSIGNAL)

1906 if not values:

1907 return None

1908 return values[0]

1909

1910 def set_dB_ant_noise( self, signal ):

1911 "Set the RF noise power at the antenna, decibel difference from an "\

1912 "arbitrary, fixed reference."

1913

1914 self.__set_field_values(self.RTF_DB_ANTNOISE, [signal])

1915

1916 def get_dB_ant_noise( self ):

1917 "Get the RF noise power at the antenna, decibel difference from an "\

1918 "arbitrary, fixed reference."

1919

1920 values=self.__get_field_values(self.RTF_DB_ANTNOISE)

1921 if not values:

1922 return None

1923 return values[0]

1924

1925## def set_rx_flags( self, flags ):

1926## "Set the properties of received frames."

1927##

1928## self.__set_field_values(self.RTF_RX_FLAGS, [flags])

1929##

1930## def get_rx_flags( self ):

1931## "Get the properties of received frames."

1932##

1933## values=self.__get_field_values(self.RTF_RX_FLAGS)

1934## if not values:

1935## return None

1936## return values[0]

1937

1938 def set_FCS_in_header( self, fcs ):

1939 "Set the Field containing the FCS of the frame (instead of it being "\

1940 "appended to the frame as it would appear on the air.) "

1941

1942 self.__set_field_values(self.RTF_FCS_IN_HEADER, [fcs])

1943

1944 def get_FCS_in_header( self ):

1945 "Get the Field containing the FCS of the frame (instead of it being "\

1946 "appended to the frame as it would appear on the air.) "

1947

1948 values=self.__get_field_values(self.RTF_FCS_IN_HEADER)

1949 if not values:

1950 return None

1951 return values[0]

1952

1953## def set_RSSI( self, rssi, max_rssi ):

1954## "Set the received signal strength and the maximum for the hardware."

1955##

1956## self.__set_field_values(self.RTF_RSSI, [rssi, max_rssi])

1957##

1958## def get_RSSI( self ):

1959## "Get the received signal strength and the maximum for the hardware."

1960##

1961## values=self.__get_field_values(self.RTF_RSSI)

1962##

1963## return values

1964

1965 def set_RTS_retries( self, retries):

1966 "Set the number of RTS retries a transmitted frame used."

1967

1968 self.__set_field_values(self.RTF_RTS_RETRIES, [retries])

1969

1970 def get_RTS_retries( self ):

1971 "Get the number of RTS retries a transmitted frame used."

1972

1973 values=self.__get_field_values(self.RTF_RTS_RETRIES)

1974 if not values:

1975 return None

1976 return values[0]

1977

1978 def set_tx_flags( self, flags ):

1979 "Set the properties of transmitted frames."

1980

1981 self.__set_field_values(self.RTF_TX_FLAGS, [flags])

1982

1983 def get_tx_flags( self ):

1984 "Get the properties of transmitted frames."

1985

1986 values=self.__get_field_values(self.RTF_TX_FLAGS)

1987 if not values:

1988 return None

1989 return values[0]

1990

1991 def set_xchannel( self, flags, freq, channel, maxpower ):

1992 "Set extended channel information: flags, freq, channel and maxpower"

1993

1994 self.__set_field_values(self.RTF_XCHANNEL, [flags, freq, channel, maxpower] )

1995

1996 def get_xchannel( self ):

1997 "Get extended channel information: flags, freq, channel and maxpower"

1998

1999 values=self.__get_field_values(field=self.RTF_XCHANNEL)

2000

2001 return values

2002

2003 def set_data_retries( self, retries ):

2004 "Set the number of data retries a transmitted frame used."

2005

2006 self.__set_field_values(self.RTF_DATA_RETRIES, [retries])

2007

2008 def get_data_retries( self ):

2009 "Get the number of data retries a transmitted frame used."

2010

2011 values=self.__get_field_values(self.RTF_DATA_RETRIES)

2012 if not values:

2013 return None

2014 return values[0]

2015

2016 def set_hardware_queue( self, queue ):

2017 "Set the hardware queue to send the frame on."

2018

2019 self.__set_field_values(self.RTF_HARDWARE_QUEUE, [queue])

2020

2021## def get_hardware_queue( self ):

2022## "Get the hardware queue to send the frame on."

2023##

2024## values=self.__get_field_values(self.RTF_HARDWARE_QUEUE)

2025## if not values:

2026## return None

2027## return values[0]

2028

2029 def __update_header_length(self):

2030 'Update the RadioTap header length field with the real size'

2031 self.header.set_word(2, self.get_header_size(), "<")

2032

2033 def get_packet(self):

2034 self.__update_header_length()

2035 return ProtocolPacket.get_packet(self)

2036

2037 class Dot11ManagementFrame(ProtocolPacket):

2038 '802.11 Management Frame'

2039

2040 def __init__(self, aBuffer = None):

2041 header_size = 22

2042 tail_size = 0

2043

2044 ProtocolPacket.__init__(self, header_size, tail_size)

2045 if(aBuffer): 2045 ↛ exitline 2045 didn't return from function '__init__', because the condition on line 2045 was never false

2046 self.load_packet(aBuffer)

2047

2048 def get_duration(self):

2049 'Return 802.11 Management frame \'Duration\' field'

2050 b = self.header.get_word(0, "<")

2051 return b

2052

2053 def set_duration(self, value):

2054 'Set the 802.11 Management frame \'Duration\' field'

2055 # set the bits

2056 nb = value & 0xFFFF

2057 self.header.set_word(0, nb, "<")

2058

2059 def get_destination_address(self):

2060 'Return 802.11 Management frame \'Destination Address\' field as a 6 bytes array'

2061 return self.header.get_bytes()[2:8]

2062

2063 def set_destination_address(self, value):

2064 'Set 802.11 Management frame \'Destination Address\' field as a 6 bytes array'

2065 for i in range(0, 6):

2066 self.header.set_byte(2+i, value[i])

2067

2068 def get_source_address(self):

2069 'Return 802.11 Management frame \'Source Address\' field as a 6 bytes array'

2070 return self.header.get_bytes()[8:14]

2071

2072 def set_source_address(self, value):

2073 'Set 802.11 Management frame \'Source Address\' field as a 6 bytes array'

2074 for i in range(0, 6):

2075 self.header.set_byte(8+i, value[i])

2076

2077 def get_bssid(self):

2078 'Return 802.11 Management frame \'BSSID\' field as a 6 bytes array'

2079 return self.header.get_bytes()[14: 20]

2080

2081 def set_bssid(self, value):

2082 'Set 802.11 Management frame \'BSSID\' field as a 6 bytes array'

2083 for i in range(0, 6):

2084 self.header.set_byte(14+i, value[i])

2085

2086 def get_sequence_control(self):

2087 'Return 802.11 Management frame \'Sequence Control\' field'

2088 b = self.header.get_word(20, "<")

2089 return b

2090

2091 def set_sequence_control(self, value):

2092 'Set the 802.11 Management frame \'Sequence Control\' field'

2093 # set the bits

2094 nb = value & 0xFFFF

2095 self.header.set_word(20, nb, "<")

2096

2097 def get_fragment_number(self):

2098 'Return 802.11 Management frame \'Fragment Number\' subfield'

2099

2100 b = self.get_sequence_control()

2101 return (b&0x000F)

2102

2103 def set_fragment_number(self, value):

2104 'Set the 802.11 Management frame \'Fragment Number\' subfield'

2105 # clear the bits

2106 mask = (~0x000F) & 0xFFFF

2107 masked = self.header.get_word(20, "<") & mask

2108 # set the bits

2109 nb = masked | (value & 0x000F)

2110 self.header.set_word(20, nb, "<")

2111

2112 def get_sequence_number(self):

2113 'Return 802.11 Management frame \'Sequence Number\' subfield'

2114

2115 b = self.get_sequence_control()

2116 return ((b>>4) & 0xFFF)

2117

2118 def set_sequence_number(self, value):

2119 'Set the 802.11 Management frame \'Sequence Number\' subfield'

2120 # clear the bits

2121 mask = (~0xFFF0) & 0xFFFF

2122 masked = self.header.get_word(20, "<") & mask

2123 # set the bits

2124 nb = masked | ((value & 0x0FFF ) << 4 )

2125 self.header.set_word(20, nb, "<")

2126

2127 def get_frame_body(self):

2128 'Return 802.11 Management frame \'Frame Body\' field'

2129

2130 return self.get_body_as_string()

2131

2132 def set_frame_body(self, data):

2133 'Set 802.11 Management frame \'Frame Body\' field'

2134

2135 self.load_body(data)

2136

2137 class DOT11_MANAGEMENT_ELEMENTS():

2138 SSID = 0

2139 SUPPORTED_RATES = 1

2140 FH_PARAMETER_SET = 2

2141 DS_PARAMETER_SET = 3

2142 CF_PARAMETER_SET = 4

2143 TIM = 5

2144 IBSS_PARAMETER_SET = 6

2145 COUNTRY = 7

2146 HOPPING_PARAMETER = 8

2147 HOPPING_TABLE = 9

2148 REQUEST = 10

2149 BSS_LOAD = 11

2150 EDCA_PARAMETER_SET = 12

2151 TSPEC = 13

2152 TCLAS = 14

2153 SCHEDULE = 15

2154 CHALLENGE_TEXT = 16

2155 # RESERVED 17-31

2156 POWER_CONSTRAINT = 32

2157 POWER_CAPABILITY = 33

2158 TPC_REQUEST = 34

2159 TPC_REPORT = 35

2160 SUPPORTED_CHANNELS = 36

2161 CHANNEL_SWITCH_ANN = 37

2162 MEASURE_REQ = 38

2163 MEASURE_REP = 39

2164 QUIET = 40

2165 IBSS_DFS = 41

2166 ERP_INFO = 42

2167 TS_DELAY = 43

2168 TCLAS_PROCESSING = 44

2169 #RESERVED 45 # See: IEEE 802.11n

2170 QOS_CAPABILITY = 46

2171 #RESERVED 47 # See: IEEE 802.11g

2172 RSN = 48

2173 #RESERVED 49

2174 EXT_SUPPORTED_RATES = 50

2175 #RESERVED 51-126

2176 EXTENDED_CAPABILITIES = 127

2177 #RESERVED 128-220

2178 VENDOR_SPECIFIC = 221

2179 #RESERVED 222-255

2180

2181 class Dot11ManagementHelper(ProtocolPacket):

2182

2183 def __init__(self, header_size, tail_size, aBuffer = None):

2184 self.__HEADER_BASE_SIZE=header_size

2185

2186 if aBuffer: 2186 ↛ 2193line 2186 didn't jump to line 2193, because the condition on line 2186 was never false

2187 elements_length=self.__calculate_elements_length(aBuffer[self.__HEADER_BASE_SIZE:])

2188 header_size+=elements_length

2189

2190 ProtocolPacket.__init__(self, header_size, tail_size)

2191 self.load_packet(aBuffer)

2192 else:

2193 ProtocolPacket.__init__(self, header_size, tail_size)

2194

2195 def _find_element(self, elements, element_id ):

2196 remaining=len(elements)

2197

2198 offset=0

2199 while remaining > 0:

2200 (id,length)=struct.unpack("!BB",elements[offset: offset+2])

2201 if element_id is None:

2202 pass # through the whole list returning the length

2203 elif id==element_id:

2204 yield (0,offset,length+2) # ==

2205 length+=2 #id+length

2206 offset+=length

2207 if length>remaining: 2207 ↛ 2209line 2207 didn't jump to line 2209, because the condition on line 2207 was never true

2208 # Error!!

2209 length = remaining

2210 remaining-=length

2211 # < Not found

2212 yield (-1, offset, None)

2213

2214 def __calculate_elements_length(self, elements):

2215 gen_tp=self._find_element(elements, None )

2216 (match,offset,length)=next(gen_tp)

2217 if match != -1: 2217 ↛ 2219line 2217 didn't jump to line 2219, because the condition on line 2217 was never true

2218 # element_id is None, then __find_tagged_parameter must return -1

2219 raise Exception("Internal Error %s"%match)

2220 return offset

2221

2222 def _get_elements_generator(self, element_id):

2223 elements=self.get_header_as_string()[self.__HEADER_BASE_SIZE:]

2224 gen_tp=self._find_element(elements, element_id )

2225 while True:

2226 (match,offset,length)=next(gen_tp)

2227 if match != 0:

2228 return

2229 value_offset=offset+2

2230 value_end=offset+length

2231 value=elements[value_offset: value_end]

2232 yield value

2233

2234 def _get_element(self, element_id):

2235 gen_get_element=self._get_elements_generator(element_id)

2236 try:

2237 s=next(gen_get_element)

2238

2239 if s is None: 2239 ↛ 2240line 2239 didn't jump to line 2240, because the condition on line 2239 was never true

2240 raise Exception("gen_get_element salio con None in _get_element!!!")

2241

2242 return s

2243 except StopIteration:

2244 pass

2245

2246 return None

2247

2248 def delete_element(self, element_id, multiple = False):

2249 header=self.get_header_as_string()

2250 elements=header[self.__HEADER_BASE_SIZE:]

2251 gen_tp=self._find_element(elements, element_id )

2252 found=False

2253 while True:

2254 (match,offset,length)=next(gen_tp)

2255 if match != 0:

2256 break

2257 start=self.__HEADER_BASE_SIZE+offset

2258 header=header[:start]+header[start+length:]

2259 found=True

2260 if multiple is False:

2261 break

2262

2263 if not found:

2264 return False

2265

2266 self.load_header(header)

2267 return True

2268

2269 def _set_element(self, element_id, value, replace = True):

2270 parameter=struct.pack('BB%ds'%len(value),element_id,len(value),value)

2271

2272 header=self.get_header_as_string()

2273 elements=header[self.__HEADER_BASE_SIZE:]

2274 gen_tp=self._find_element(elements, element_id )

2275 found=False

2276 while True:

2277 (match,offset,length)=next(gen_tp)

2278 start=self.__HEADER_BASE_SIZE+offset

2279 if match == 0 and replace:

2280 # Replace

2281 header=header[:start]+parameter+header[start+length:]

2282 found=True

2283 break

2284 elif match > 0: 2284 ↛ 2286, 2284 ↛ 22902 missed branches: 1) line 2284 didn't jump to line 2286, because the condition on line 2284 was never true, 2) line 2284 didn't jump to line 2290, because the condition on line 2284 was never false

2285 # Add

2286 header=header[:start]+parameter+header[start:]

2287 found=True

2288 break

2289 else:

2290 break

2291 if not found:

2292 # Append (found<0 Not found)

2293 header=header+parameter

2294 self.load_header(header)

2295

2296 class Dot11ManagementBeacon(Dot11ManagementHelper):

2297 '802.11 Management Beacon Frame'

2298

2299 __HEADER_BASE_SIZE = 12 # minimal header size

2300

2301 def __init__(self, aBuffer = None):

2302 header_size = self.__HEADER_BASE_SIZE

2303 tail_size = 0

2304 Dot11ManagementHelper.__init__(self, header_size, tail_size, aBuffer)

2305

2306 def get_timestamp(self):

2307 'Return the 802.11 Management Beacon frame \'Timestamp\' field'

2308 b = self.header.get_long_long(0, "<")

2309 return b

2310

2311 def set_timestamp(self, value):

2312 'Set the 802.11 Management Beacon frame \'Timestamp\' field'

2313 # set the bits

2314 nb = value & 0xFFFFFFFFFFFFFFFF

2315 self.header.set_long_long(0, nb, "<")

2316

2317 def get_beacon_interval(self):

2318 'Return the 802.11 Management Beacon frame \'Beacon Interval\' field' \

2319 'To convert it to seconds => secs = Beacon_Interval*1024/1000000'

2320

2321 b = self.header.get_word(8, "<")

2322 return b

2323

2324 def set_beacon_interval(self, value):

2325 'Set the 802.11 Management Beacon frame \'Beacon Interval\' field'

2326 # set the bits

2327 nb = value & 0xFFFF

2328 self.header.set_word(8, nb, "<")

2329

2330 def get_capabilities(self):

2331 'Return the 802.11 Management Beacon frame \'Capability information\' field. '

2332

2333 b = self.header.get_word(10, "<")

2334 return b

2335

2336 def set_capabilities(self, value):

2337 'Set the 802.11 Management Beacon frame \'Capability Information\' field'

2338 # set the bits

2339 nb = value & 0xFFFF

2340 self.header.set_word(10, nb, "<")

2341

2342 def get_ssid(self):

2343 "Get the 802.11 Management SSID element. "\

2344 "The SSID element indicates the identity of an ESS or IBSS."

2345 return self._get_element(DOT11_MANAGEMENT_ELEMENTS.SSID)

2346

2347 def set_ssid(self, ssid):

2348 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SSID,ssid)

2349

2350 def get_supported_rates(self, human_readable=False):

2351 "Get the 802.11 Management Supported Rates element. "\

2352 "Specifies up to eight rates, then an Extended Supported Rate element "\

2353 "shall be generated to specify the remaining supported rates."\

2354 "If human_readable is True, the rates are returned in Mbit/sec"

2355 s=self._get_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES)

2356 if s is None: 2356 ↛ 2357line 2356 didn't jump to line 2357, because the condition on line 2356 was never true

2357 return None

2358

2359 rates=struct.unpack('%dB'%len(s),s)

2360 if not human_readable:

2361 return rates

2362

2363 rates_Mbs=tuple([(x&0x7F)*0.5 for x in rates])

2364 return rates_Mbs

2365

2366 def set_supported_rates(self, rates):

2367 "Set the 802.11 Management Supported Rates element. "\

2368 "Specifies a tuple or list with up to eight rates, then an "\

2369 "Extended Supported Rate element shall be generated to specify "\

2370 "the remaining supported rates."

2371 qty_rates=len(rates)

2372 if qty_rates>8: 2372 ↛ 2373line 2372 didn't jump to line 2373, because the condition on line 2372 was never true

2373 raise Exception("requires up to eight rates")

2374 rates_string=struct.pack('B'*qty_rates,*rates)

2375 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES,rates_string)

2376

2377 def get_ds_parameter_set(self):

2378 "Get the 802.11 Management DS Parameter set element. "\

2379 "Contains information to allow channel number identification for "\

2380 "STAs using a DSSS PHY."

2381 s=self._get_element(DOT11_MANAGEMENT_ELEMENTS.DS_PARAMETER_SET)

2382 if s is None: 2382 ↛ 2383line 2382 didn't jump to line 2383, because the condition on line 2382 was never true

2383 return None

2384

2385 (ch,)=struct.unpack('B',s)

2386

2387 return ch

2388

2389 def set_ds_parameter_set(self, channel):

2390 "Set the 802.11 Management DS Parameter set element. "\

2391 "Contains information to allow channel number identification for "\

2392 "STAs using a DSSS PHY."

2393 channel_string=struct.pack('B',channel)

2394 self._set_element(DOT11_MANAGEMENT_ELEMENTS.DS_PARAMETER_SET,channel_string)

2395

2396 def get_rsn(self):

2397 "Get the 802.11 Management Robust Security Network element."

2398 s = self._get_element(DOT11_MANAGEMENT_ELEMENTS.RSN)

2399 if s is None:

2400 return None

2401 return s

2402

2403 def set_rsn(self, data):

2404 "Set the 802.11 Management Robust Security Network element."

2405 self._set_element(DOT11_MANAGEMENT_ELEMENTS.RSN, data)

2406

2407 def get_erp(self):

2408 "Get the 802.11 Management ERP (extended rate PHY) Information element."

2409 s = self._get_element(DOT11_MANAGEMENT_ELEMENTS.ERP_INFO)

2410 if s is None:

2411 return None

2412

2413 (erp,) = struct.unpack('B',s)

2414

2415 return erp

2416

2417 def set_erp(self, erp):

2418 "Set the 802.11 Management ERP (extended rate PHY) Inforamation "\

2419 "element."

2420 erp_string = struct.pack('B',erp)

2421 self._set_element(DOT11_MANAGEMENT_ELEMENTS.ERP_INFO, erp_string)

2422

2423 def get_country(self):

2424 "Get the 802.11 Management Country element." \

2425 "Returns a tuple containing Country code, first channel number, "\

2426 "number of channels and maximum transmit power level"

2427 s = self._get_element(DOT11_MANAGEMENT_ELEMENTS.COUNTRY)

2428 if s is None:

2429 return None

2430

2431 code, first, num, max = struct.unpack('3sBBB',s)

2432 code = code.strip(' ')

2433 return code, first, num, max

2434

2435 def set_country(self, code, first_channel, number_of_channels, max_power):

2436 "Set the 802.11 Management Country element."

2437 if len(code) > 3:

2438 raise Exception("Country code must be up to 3 bytes long")

2439

2440 #Padding the country code

2441 code += ' ' * (3-len(code))

2442

2443 country_string = struct.pack('3sBBB', code, first_channel,

2444 number_of_channels, max_power)

2445 self._set_element(DOT11_MANAGEMENT_ELEMENTS.COUNTRY, country_string)

2446

2447 def get_vendor_specific(self):

2448 "Get the 802.11 Management Vendor Specific elements "\

2449 "as a list of tuples."

2450 "The Vendor Specific information element is used to carry "\

2451 "information not defined in the standard within a single "\

2452 "defined format"

2453

2454 vs=[]

2455 gen_get_element=self._get_elements_generator(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC)

2456 try:

2457 while 1:

2458 s=next(gen_get_element)

2459

2460 if s is None: 2460 ↛ 2461line 2460 didn't jump to line 2461, because the condition on line 2460 was never true

2461 raise Exception("gen_get_element salio con None!!!")

2462

2463 # OUI is 3 bytes

2464 oui=s[:3]

2465 data=s[3:]

2466 vs.append((oui,data))

2467 except StopIteration:

2468 pass

2469

2470 return vs

2471

2472 def add_vendor_specific(self, oui, data):

2473 "Set the 802.11 Management Vendor Specific element. "\

2474 "The Vendor Specific information element is used to carry "\

2475 "information not defined in the standard within a single "\

2476 "defined format"

2477

2478 # 3 is the OUI length

2479 max_data_len=255-3

2480 data_len=len(data)

2481

2482 if data_len>max_data_len: 2482 ↛ 2483line 2482 didn't jump to line 2483, because the condition on line 2482 was never true

2483 raise Exception("data allow up to %d bytes long" % max_data_len)

2484 if len(oui) > 3: 2484 ↛ 2485line 2484 didn't jump to line 2485, because the condition on line 2484 was never true

2485 raise Exception("oui is three bytes long")

2486

2487 self._set_element(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC,oui+data, replace=False)

2488

2489 class Dot11ManagementProbeRequest(Dot11ManagementHelper):

2490 '802.11 Management Probe Request Frame'

2491

2492 def __init__(self, aBuffer = None):

2493 header_size = 0

2494 tail_size = 0

2495 Dot11ManagementHelper.__init__(self, header_size, tail_size, aBuffer)

2496

2497 def get_ssid(self):

2498 "Get the 802.11 Management SSID element. "\

2499 "The SSID element indicates the identity of an ESS or IBSS."

2500 return self._get_element(DOT11_MANAGEMENT_ELEMENTS.SSID)

2501

2502 def set_ssid(self, ssid):

2503 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SSID,ssid)

2504

2505 def get_supported_rates(self, human_readable=False):

2506 "Get the 802.11 Management Supported Rates element. "\

2507 "Specifies up to eight rates, then an Extended Supported Rate element "\

2508 "shall be generated to specify the remaining supported rates."\

2509 "If human_readable is True, the rates are returned in Mbit/sec"

2510 s=self._get_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES)

2511 if s is None: 2511 ↛ 2512line 2511 didn't jump to line 2512, because the condition on line 2511 was never true

2512 return None

2513

2514 rates=struct.unpack('%dB'%len(s),s)

2515 if not human_readable:

2516 return rates

2517

2518 rates_Mbs=tuple([(x&0x7F)*0.5 for x in rates])

2519 return rates_Mbs

2520

2521 def set_supported_rates(self, rates):

2522 "Set the 802.11 Management Supported Rates element. "\

2523 "Specifies a tuple or list with up to eight rates, then an "\

2524 "Extended Supported Rate element shall be generated to specify "\

2525 "the remaining supported rates."

2526 qty_rates=len(rates)

2527 if qty_rates>8: 2527 ↛ 2528line 2527 didn't jump to line 2528, because the condition on line 2527 was never true

2528 raise Exception("requires up to eight rates")

2529 rates_string=struct.pack('B'*qty_rates,*rates)

2530 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES,rates_string)

2531

2532 class Dot11ManagementProbeResponse(Dot11ManagementBeacon):

2533 '802.11 Management Probe Response Frame'

2534

2535 def __init__(self, aBuffer = None):

2536 Dot11ManagementBeacon.__init__(self, aBuffer)

2537

2538 class DOT11_REASON_CODES():

2539 # RESERVED = 0

2540 UNSPECIFIED_REASON = 1

2541 PREV_AUTH_NO_LONGER_VALID = 2

2542 DEAUTH_STA_IS_LEAVING = 3

2543 DISASS_DUE_TO_INACTIVITY = 4

2544 DISASS_AP_UNABLE_HANDLE_ALL_STA = 5

2545 C2_FRAME_FROM_NONAUTHENTICATED_STA = 6

2546 C3_FRAME_FROM_NONASSOCIATED_STA = 7

2547 DISSASS_STA_IS_LEAVING = 8

2548 STA_REQ_NOT_AUTH_STA = 9

2549 DISASS_POWER_CAP_IE_UNNACCEPTABLE = 10

2550 DISASS_SUP_CH_IE_UNNACCEPTABLE = 11

2551 # RESERVED = 12

2552 INVALID_IE = 13

2553 MIC_FAILURE = 14

2554 FOUR_WAY_HANDSHAKE_TIMEOUT = 15

2555 GROUP_KEY_HANDSHAKE_TIMEOUT = 16

2556 IE_FOUR_WAY_HANDSHAKE_DIFFERENT = 17

2557 INVALID_GROUP_CIPHER = 18

2558 INVALID_PAIRWISE_CIPHER = 19

2559 INVALID_AKMP = 20

2560 UNSUPPORTED_RSN_IE_VERSION = 21

2561 INVALID_RSN_IE_CAP = 22

2562 X_AUTH_FAILED = 23

2563 CIPHER_SUITE_REJECTED_SECURITY_POLICY = 24

2564 # RESERVED = 25 - 31

2565 DISASS_QOS_RELATED_REASON = 32

2566 DISASS_QOS_UNSUFFICIENT_BANDWIDTH = 33

2567 DISASS_EXCESSIVE_FRAMES_WITHOUT_ACK = 34

2568 DISASS_STA_TX_OUTSIDE_TXOPS = 35

2569 REQ_STA_LEAVING = 36

2570 REQ_STA_NOT_WANT_MECHANISM = 37

2571 REQ_STA_RECV_FRAMES_WHICH_SETUP_REQ = 38

2572 REQ_STA_DUE_TIMEOUT = 39

2573 STA_NOT_SUPPORT_CIPHER_SUITE = 45

2574 # RESERVED = 46 - 65 535

2575

2576 class Dot11ManagementDeauthentication(ProtocolPacket):

2577 '802.11 Management Deauthentication Frame'

2578

2579 def __init__(self, aBuffer = None):

2580 header_size = 2

2581 tail_size = 0

2582 if aBuffer: 2582 ↛ 2586line 2582 didn't jump to line 2586, because the condition on line 2582 was never false

2583 ProtocolPacket.__init__(self, header_size, tail_size)

2584 self.load_packet(aBuffer)

2585 else:

2586 ProtocolPacket.__init__(self, header_size, tail_size)

2587

2588 def get_reason_code(self):

2589 "Get the 802.11 Management Deauthentication or Disassociation Code."

2590 return self.header.get_word(0, "<")

2591

2592 def set_reason_code(self, rc):

2593 self.header.set_word(0, rc, "<")

2594

2595 class DOT11_AUTH_ALGORITHMS():

2596 OPEN = 0

2597 SHARED_KEY = 1

2598

2599 class DOT11_AUTH_STATUS_CODES():

2600 SUCCESSFUL = 0

2601 UNSPECIFIED_FAILURE = 1

2602 # RESERVED = 2 - 9

2603 CAP_REQ_UNSUPPORTED = 10

2604 REASS_DENIED_CANNOT_CONFIRM_ASS_EXISTS = 11

2605 ASS_DENIED_REASON_OUTSIDE_SCOPE_STANDARD = 12

2606 STA_NOT_SUPPORT_AUTH_ALGORITHM = 13

2607 AUTH_SEQ_OUT_OF_EXPECTED = 14

2608 AUTH_REJECTED_CHALLENGE_FAILURE = 15

2609 AUTH_REJECTED_TIMEOUT = 16

2610 ASS_DENIED_AP_UNABLE_HANDLE_MORE_STA = 17

2611 ASS_DENIED_STA_NOT_SUPPORTING_DATA_RATES = 18

2612 ASS_DENIED_STA_NOT_SUPPORTING_SHORT_PREAMBLE = 19

2613 ASS_DENIED_STA_NOT_SUPPORTING_PBCC_MODULATION = 20

2614 ASS_DENIED_STA_NOT_SUPPORTING_CHANNEL_AGILITY = 21

2615 ASS_REQUEST_REJECTED_SPACTRUM_MGT_CAP = 22

2616 ASS_REQUEST_REJECTED_POWER_CAP_IE_UNNACCEPTABLE = 23

2617 ASS_REQUEST_REJECTED_SUP_CH_IE_UNNACCEPTABLE = 24

2618 ASS_DENIED_STA_NOT_SUPPORTING_SHORT_SLOT_TIME = 25

2619 ASS_DENIED_STA_NOT_SUPPORTING_DSSS_OFDM = 26

2620 # RESERVED = 27 - 31

2621 UNSPECIFIED_QOS = 32

2622 ASS_DENIED_QOS_UNSUFFICIENT_BANDWIDTH = 33

2623 ASS_DENIED_EXCESSIVE_FRAME_LOST = 34

2624 ASS_DENIED_STA_NOT_SUPPORT_QOS = 35

2625 # RESERVED = 36

2626 REQ_HAS_BEEN_DECLINED = 37

2627 REQ_NOT_SUCCESSFUL_PARAM_INVALID_VALUE = 38

2628 TSPEC = 39

2629 INVALID_IE = 40

2630 INVALID_GROUP_CIPHER = 41

2631 INVALID_PAIRWISE_CIPHER = 42

2632 INVALID_AKMP = 43

2633 UNSUPPORTED_RSN_IE_VERSION = 44

2634 INVALID_RSN_IE_CAP = 45

2635 CIPHER_SUITE_REJECTED_SECURITY_POLICY = 46

2636 TS_NOT_CREATED = 47

2637 DIRECT_LINK_NOT_ALLOWED_BSS_POLICY = 48

2638 DST_STA_NOT_PRESENT_IN_BSS = 49

2639 DST_STA_NOT_QOS_STA = 50

2640 ASS_DENIED_LISTEN_INTERVAL_TOO_LARGE = 51

2641 # RESERVED = 52 - 65 535

2642

2643 class Dot11ManagementAuthentication(Dot11ManagementHelper):

2644 '802.11 Management Authentication Frame'

2645

2646 __HEADER_BASE_SIZE = 6 # minimal header size

2647

2648 def __init__(self, aBuffer = None):

2649 header_size = self.__HEADER_BASE_SIZE

2650 tail_size = 0

2651 Dot11ManagementHelper.__init__(self, header_size, tail_size, aBuffer)

2652

2653 def get_authentication_algorithm(self):

2654 "Get the 802.11 Management Authentication Algorithm."

2655 return self.header.get_word(0, "<")

2656

2657 def set_authentication_algorithm(self, algorithm):

2658 "Set the 802.11 Management Authentication Algorithm."

2659 self.header.set_word(0, algorithm, "<")

2660

2661 def get_authentication_sequence(self):

2662 "Get the 802.11 Management Authentication Sequence."

2663 return self.header.get_word(2, "<")

2664

2665 def set_authentication_sequence(self, seq):

2666 "Set the 802.11 Management Authentication Sequence."

2667 self.header.set_word(2, seq, "<")

2668

2669 def get_authentication_status(self):

2670 "Get the 802.11 Management Authentication Status."

2671 return self.header.get_word(4, "<")

2672

2673 def set_authentication_status(self, status):

2674 "Set the 802.11 Management Authentication Status."

2675 self.header.set_word(4, status, "<")

2676

2677 def get_challenge_text(self):

2678 return self._get_element(DOT11_MANAGEMENT_ELEMENTS.CHALLENGE_TEXT)

2679

2680 def set_challenge_text(self, challenge):

2681 self._set_element(DOT11_MANAGEMENT_ELEMENTS.CHALLENGE_TEXT, challenge)

2682

2683 def get_vendor_specific(self):

2684 "Get the 802.11 Management Vendor Specific elements "\

2685 "as a list of tuples."

2686 "The Vendor Specific information element is used to carry "\

2687 "information not defined in the standard within a single "\

2688 "defined format"

2689

2690 vs=[]

2691 gen_get_element=self._get_elements_generator(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC)

2692 try:

2693 while 1:

2694 s=next(gen_get_element)

2695

2696 if s is None: 2696 ↛ 2697line 2696 didn't jump to line 2697, because the condition on line 2696 was never true

2697 raise Exception("gen_get_element salio con None!!!")

2698

2699 # OUI is 3 bytes

2700 oui=s[:3]

2701 data=s[3:]

2702 vs.append((oui,data))

2703 except StopIteration:

2704 pass

2705

2706 return vs

2707

2708 def add_vendor_specific(self, oui, data):

2709 "Set the 802.11 Management Vendor Specific element. "\

2710 "The Vendor Specific information element is used to carry "\

2711 "information not defined in the standard within a single "\

2712 "defined format"

2713

2714 # 3 is the OUI length

2715 max_data_len=255-3

2716 data_len=len(data)

2717

2718 if data_len>max_data_len: 2718 ↛ 2719line 2718 didn't jump to line 2719, because the condition on line 2718 was never true

2719 raise Exception("data allow up to %d bytes long" % max_data_len)

2720 if len(oui) > 3: 2720 ↛ 2721line 2720 didn't jump to line 2721, because the condition on line 2720 was never true

2721 raise Exception("oui is three bytes long")

2722

2723 self._set_element(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC,oui+data, replace=False)

2724

2725 class Dot11ManagementDisassociation(Dot11ManagementDeauthentication):

2726 '802.11 Management Disassociation Frame'

2727

2728 def __init__(self, aBuffer = None):

2729 Dot11ManagementDeauthentication.__init__(self, aBuffer)

2730

2731 class Dot11ManagementAssociationRequest(Dot11ManagementHelper):

2732 '802.11 Management Association Request Frame'

2733

2734 __HEADER_BASE_SIZE = 4 # minimal header size

2735

2736 def __init__(self, aBuffer = None):

2737 header_size = self.__HEADER_BASE_SIZE

2738 tail_size = 0

2739 Dot11ManagementHelper.__init__(self, header_size, tail_size, aBuffer)

2740

2741 def get_capabilities(self):

2742 'Return the 802.11 Management Association Request Frame \'Capability information\' field. '

2743 b = self.header.get_word(0, "<")

2744 return b

2745

2746 def set_capabilities(self, value):

2747 'Set the 802.11 Management Association Request Frame \'Capability Information\' field'

2748 # set the bits

2749 nb = value & 0xFFFF

2750 self.header.set_word(0, nb, "<")

2751

2752 def get_listen_interval(self):

2753 'Return the 802.11 Management Association Request Frame \'Listen Interval\' field. '

2754 b = self.header.get_word(2, "<")

2755 return b

2756

2757 def set_listen_interval(self, value):

2758 'Set the 802.11 Management Association Request Frame \'Listen Interval\' field'

2759 self.header.set_word(2, value, "<")

2760

2761 def get_ssid(self):

2762 "Get the 802.11 Management SSID element. "\

2763 "The SSID element indicates the identity of an ESS or IBSS."

2764 return self._get_element(DOT11_MANAGEMENT_ELEMENTS.SSID)

2765

2766 def set_ssid(self, ssid):

2767 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SSID,ssid)

2768

2769 def get_supported_rates(self, human_readable=False):

2770 "Get the 802.11 Management Supported Rates element. "\

2771 "Specifies up to eight rates, then an Extended Supported Rate element "\

2772 "shall be generated to specify the remaining supported rates."\

2773 "If human_readable is True, the rates are returned in Mbit/sec"

2774 s=self._get_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES)

2775 if s is None: 2775 ↛ 2776line 2775 didn't jump to line 2776, because the condition on line 2775 was never true

2776 return None

2777

2778 rates=struct.unpack('%dB'%len(s),s)

2779 if not human_readable:

2780 return rates

2781

2782 rates_Mbs=tuple([(x&0x7F)*0.5 for x in rates])

2783 return rates_Mbs

2784

2785 def set_supported_rates(self, rates):

2786 "Set the 802.11 Management Supported Rates element. "\

2787 "Specifies a tuple or list with up to eight rates, then an "\

2788 "Extended Supported Rate element shall be generated to specify "\

2789 "the remaining supported rates."

2790 qty_rates=len(rates)

2791 if qty_rates>8: 2791 ↛ 2792line 2791 didn't jump to line 2792, because the condition on line 2791 was never true

2792 raise Exception("requires up to eight rates")

2793 rates_string=struct.pack('B'*qty_rates,*rates)

2794 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES,rates_string)

2795

2796 def get_rsn(self):

2797 "Get the 802.11 Management Robust Security Network element."

2798 s = self._get_element(DOT11_MANAGEMENT_ELEMENTS.RSN)

2799 if s is None: 2799 ↛ 2800line 2799 didn't jump to line 2800, because the condition on line 2799 was never true

2800 return None

2801 return s

2802

2803 def set_rsn(self, data):

2804 "Set the 802.11 Management Robust Security Network element."

2805 self._set_element(DOT11_MANAGEMENT_ELEMENTS.RSN, data)

2806

2807 def get_vendor_specific(self):

2808 "Get the 802.11 Management Vendor Specific elements "\

2809 "as a list of tuples."

2810 "The Vendor Specific information element is used to carry "\

2811 "information not defined in the standard within a single "\

2812 "defined format"

2813

2814 vs=[]

2815 gen_get_element=self._get_elements_generator(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC)

2816 try:

2817 while 1:

2818 s=next(gen_get_element)

2819

2820 if s is None: 2820 ↛ 2821line 2820 didn't jump to line 2821, because the condition on line 2820 was never true

2821 raise Exception("gen_get_element salio con None!!!")

2822

2823 # OUI is 3 bytes

2824 oui=s[:3]

2825 data=s[3:]

2826 vs.append((oui,data))

2827 except StopIteration:

2828 pass

2829

2830 return vs

2831

2832 def add_vendor_specific(self, oui, data):

2833 "Set the 802.11 Management Vendor Specific element. "\

2834 "The Vendor Specific information element is used to carry "\

2835 "information not defined in the standard within a single "\

2836 "defined format"

2837

2838 # 3 is the OUI length

2839 max_data_len=255-3

2840 data_len=len(data)

2841

2842 if data_len>max_data_len: 2842 ↛ 2843line 2842 didn't jump to line 2843, because the condition on line 2842 was never true

2843 raise Exception("data allow up to %d bytes long" % max_data_len)

2844 if len(oui) > 3: 2844 ↛ 2845line 2844 didn't jump to line 2845, because the condition on line 2844 was never true

2845 raise Exception("oui is three bytes long")

2846

2847 self._set_element(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC,oui+data, replace=False)

2848

2849 class Dot11ManagementAssociationResponse(Dot11ManagementHelper):

2850 '802.11 Management Association Response Frame'

2851

2852 __HEADER_BASE_SIZE = 6 # minimal header size

2853

2854 def __init__(self, aBuffer = None):

2855 header_size = self.__HEADER_BASE_SIZE

2856 tail_size = 0

2857 Dot11ManagementHelper.__init__(self, header_size, tail_size, aBuffer)

2858

2859 def get_capabilities(self):

2860 'Return the 802.11 Management Association Response Frame \'Capability information\' field. '

2861 b = self.header.get_word(0, "<")

2862 return b

2863

2864 def set_capabilities(self, value):

2865 'Set the 802.11 Management Association Response Frame \'Capability Information\' field'

2866 # set the bits

2867 nb = value & 0xFFFF

2868 self.header.set_word(0, nb, "<")

2869

2870 def get_status_code(self):

2871 'Return the 802.11 Management Association Response Frame \'Status Code\' field. '

2872 b = self.header.get_word(2, "<")

2873 return b

2874

2875 def set_status_code(self, value):

2876 'Set the 802.11 Management Association Response Frame \'Status Code\' field'

2877 self.header.set_word(2, value, "<")

2878

2879 def get_association_id(self):

2880 'Return the 802.11 Management Association Response Frame \'Association Id\' field. '

2881 b = self.header.get_word(4, "<")

2882 return b

2883

2884 def set_association_id(self, value):

2885 'Set the 802.11 Management Association Response Frame \'Association Id\' field'

2886 self.header.set_word(4, value, "<")

2887

2888 def get_supported_rates(self, human_readable=False):

2889 "Get the 802.11 Management Supported Rates element. "\

2890 "Specifies up to eight rates, then an Extended Supported Rate element "\

2891 "shall be generated to specify the remaining supported rates."\

2892 "If human_readable is True, the rates are returned in Mbit/sec"

2893 s=self._get_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES)

2894 if s is None: 2894 ↛ 2895line 2894 didn't jump to line 2895, because the condition on line 2894 was never true

2895 return None

2896

2897 rates=struct.unpack('%dB'%len(s),s)

2898 if not human_readable:

2899 return rates

2900

2901 rates_Mbs=tuple([(x&0x7F)*0.5 for x in rates])

2902 return rates_Mbs

2903

2904 def set_supported_rates(self, rates):

2905 "Set the 802.11 Management Supported Rates element. "\

2906 "Specifies a tuple or list with up to eight rates, then an "\

2907 "Extended Supported Rate element shall be generated to specify "\

2908 "the remaining supported rates."

2909 qty_rates=len(rates)

2910 if qty_rates>8: 2910 ↛ 2911line 2910 didn't jump to line 2911, because the condition on line 2910 was never true

2911 raise Exception("requires up to eight rates")

2912 rates_string=struct.pack('B'*qty_rates,*rates)

2913 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES,rates_string)

2914

2915 def get_vendor_specific(self):

2916 "Get the 802.11 Management Vendor Specific elements "\

2917 "as a list of tuples."

2918 "The Vendor Specific information element is used to carry "\

2919 "information not defined in the standard within a single "\

2920 "defined format"

2921

2922 vs=[]

2923 gen_get_element=self._get_elements_generator(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC)

2924 try:

2925 while 1:

2926 s=next(gen_get_element)

2927

2928 if s is None: 2928 ↛ 2929line 2928 didn't jump to line 2929, because the condition on line 2928 was never true

2929 raise Exception("gen_get_element salio con None!!!")

2930

2931 # OUI is 3 bytes

2932 oui=s[:3]

2933 data=s[3:]

2934 vs.append((oui,data))

2935 except StopIteration:

2936 pass

2937

2938 return vs

2939

2940 def add_vendor_specific(self, oui, data):

2941 "Set the 802.11 Management Vendor Specific element. "\

2942 "The Vendor Specific information element is used to carry "\

2943 "information not defined in the standard within a single "\

2944 "defined format"

2945

2946 # 3 is the OUI length

2947 max_data_len=255-3

2948 data_len=len(data)

2949 if data_len>max_data_len: 2949 ↛ 2950line 2949 didn't jump to line 2950, because the condition on line 2949 was never true

2950 raise Exception("data allow up to %d bytes long" % max_data_len)

2951 if len(oui) > 3: 2951 ↛ 2952line 2951 didn't jump to line 2952, because the condition on line 2951 was never true

2952 raise Exception("oui is three bytes long")

2953

2954 self._set_element(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC,oui+data, replace=False)

2955

2956 class Dot11ManagementReassociationRequest(Dot11ManagementHelper):

2957 '802.11 Management Reassociation Request Frame'

2958

2959 __HEADER_BASE_SIZE = 10 # minimal header size

2960

2961 def __init__(self, aBuffer = None):

2962 header_size = self.__HEADER_BASE_SIZE

2963 tail_size = 0

2964 Dot11ManagementHelper.__init__(self, header_size, tail_size, aBuffer)

2965

2966 def get_capabilities(self):

2967 'Return the 802.11 Management Reassociation Request Frame \'Capability information\' field. '

2968 b = self.header.get_word(0, "<")

2969 return b

2970

2971 def set_capabilities(self, value):

2972 'Set the 802.11 Management Reassociation Request Frame \'Capability Information\' field'

2973 # set the bits

2974 nb = value & 0xFFFF

2975 self.header.set_word(0, nb, "<")

2976

2977 def get_listen_interval(self):

2978 'Return the 802.11 Management Reassociation Request Frame \'Listen Interval\' field. '

2979 b = self.header.get_word(2, "<")

2980 return b

2981

2982 def set_listen_interval(self, value):

2983 'Set the 802.11 Management Reassociation Request Frame \'Listen Interval\' field'

2984 self.header.set_word(2, value, "<")

2985

2986 def get_current_ap(self):

2987 'Return the 802.11 Management Reassociation Request Frame \'Current AP\' field.'

2988 return self.header.get_bytes()[4:10]

2989

2990 def set_current_ap(self, value):

2991 'Set the 802.11 Management Reassociation Request Frame \'Current AP\' field'

2992 for i in range(0, 6):

2993 self.header.set_byte(4+i, value[i])

2994

2995 def get_ssid(self):

2996 "Get the 802.11 Management SSID element. "\

2997 "The SSID element indicates the identity of an ESS or IBSS."

2998 return self._get_element(DOT11_MANAGEMENT_ELEMENTS.SSID)

2999

3000 def set_ssid(self, ssid):

3001 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SSID,ssid)

3002

3003 def get_supported_rates(self, human_readable=False):

3004 "Get the 802.11 Management Supported Rates element. "\

3005 "Specifies up to eight rates, then an Extended Supported Rate element "\

3006 "shall be generated to specify the remaining supported rates."\

3007 "If human_readable is True, the rates are returned in Mbit/sec"

3008 s=self._get_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES)

3009 if s is None: 3009 ↛ 3010line 3009 didn't jump to line 3010, because the condition on line 3009 was never true

3010 return None

3011

3012 rates=struct.unpack('%dB'%len(s),s)

3013 if not human_readable:

3014 return rates

3015

3016 rates_Mbs=tuple([(x&0x7F)*0.5 for x in rates])

3017 return rates_Mbs

3018

3019 def set_supported_rates(self, rates):

3020 "Set the 802.11 Management Supported Rates element. "\

3021 "Specifies a tuple or list with up to eight rates, then an "\

3022 "Extended Supported Rate element shall be generated to specify "\

3023 "the remaining supported rates."

3024 qty_rates=len(rates)

3025 if qty_rates>8: 3025 ↛ 3026line 3025 didn't jump to line 3026, because the condition on line 3025 was never true

3026 raise Exception("requires up to eight rates")

3027 rates_string=struct.pack('B'*qty_rates,*rates)

3028 self._set_element(DOT11_MANAGEMENT_ELEMENTS.SUPPORTED_RATES,rates_string)

3029

3030 def get_rsn(self):

3031 "Get the 802.11 Management Robust Security Network element."

3032 s = self._get_element(DOT11_MANAGEMENT_ELEMENTS.RSN)

3033 if s is None: 3033 ↛ 3034line 3033 didn't jump to line 3034, because the condition on line 3033 was never true

3034 return None

3035 return s

3036

3037 def set_rsn(self, data):

3038 "Set the 802.11 Management Robust Security Network element."

3039 self._set_element(DOT11_MANAGEMENT_ELEMENTS.RSN, data)

3040

3041 def get_vendor_specific(self):

3042 "Get the 802.11 Management Vendor Specific elements "\

3043 "as a list of tuples."

3044 "The Vendor Specific information element is used to carry "\

3045 "information not defined in the standard within a single "\

3046 "defined format"

3047

3048 vs=[]

3049 gen_get_element=self._get_elements_generator(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC)

3050 try:

3051 while 1:

3052 s=next(gen_get_element)

3053

3054 if s is None: 3054 ↛ 3055line 3054 didn't jump to line 3055, because the condition on line 3054 was never true

3055 raise Exception("gen_get_element salio con None!!!")

3056

3057 # OUI is 3 bytes

3058 oui=s[:3]

3059 data=s[3:]

3060 vs.append((oui,data))

3061 except StopIteration:

3062 pass

3063

3064 return vs

3065

3066 def add_vendor_specific(self, oui, data):

3067 "Set the 802.11 Management Vendor Specific element. "\

3068 "The Vendor Specific information element is used to carry "\

3069 "information not defined in the standard within a single "\

3070 "defined format"

3071

3072 # 3 is the OUI length

3073 max_data_len=255-3

3074 data_len=len(data)

3075

3076 if data_len>max_data_len: 3076 ↛ 3077line 3076 didn't jump to line 3077, because the condition on line 3076 was never true

3077 raise Exception("data allow up to %d bytes long" % max_data_len)

3078 if len(oui) > 3: 3078 ↛ 3079line 3078 didn't jump to line 3079, because the condition on line 3078 was never true

3079 raise Exception("oui is three bytes long")

3080

3081 self._set_element(DOT11_MANAGEMENT_ELEMENTS.VENDOR_SPECIFIC,oui+data, replace=False)

3082

3083 class Dot11ManagementReassociationResponse(Dot11ManagementAssociationResponse):

3084 '802.11 Management Reassociation Response Frame'

3085

3086 def __init__(self, aBuffer = None):

3087 Dot11ManagementAssociationResponse.__init__(self, aBuffer)