| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net" |
| 6 | |
| 7 | "github.com/bettercap/bettercap/network" |
| 8 | |
| 9 | "github.com/google/gopacket" |
| 10 | "github.com/google/gopacket/layers" |
| 11 | ) |
| 12 | |
| 13 | var ( |
| 14 | openFlags = 1057 |
| 15 | wpaFlags = 1041 |
| 16 | specManFlag = 1<<8 |
| 17 | durationID = uint16(0x013a) |
| 18 | capabilityInfo = uint16(0x0411) |
| 19 | listenInterval = uint16(3) |
| 20 | //1-54 Mbit |
| 21 | fakeApRates = []byte{0x82, 0x84, 0x8b, 0x96, 0x24, 0x30, 0x48, 0x6c, 0x03, 0x01} |
| 22 | fakeApWpaRSN = []byte{ |
| 23 | 0x01, 0x00, // RSN Version 1 |
| 24 | 0x00, 0x0f, 0xac, 0x02, // Group Cipher Suite : 00-0f-ac TKIP |
| 25 | 0x02, 0x00, // 2 Pairwise Cipher Suites (next two lines) |
| 26 | 0x00, 0x0f, 0xac, 0x04, // AES Cipher / CCMP |
| 27 | 0x00, 0x0f, 0xac, 0x02, // TKIP Cipher |
| 28 | 0x01, 0x00, // 1 Authentication Key Management Suite (line below) |
| 29 | 0x00, 0x0f, 0xac, 0x02, // Pre-Shared Key |
| 30 | 0x00, 0x00, |
| 31 | } |
| 32 | wpaSignatureBytes = []byte{0, 0x50, 0xf2, 1} |
| 33 | |
| 34 | assocRates = []byte{0x82, 0x84, 0x8b, 0x96, 0x24, 0x30, 0x48, 0x6c} |
| 35 | assocESRates = []byte{0x0C, 0x12, 0x18, 0x60} |
| 36 | assocRSNInfo = []byte{0x01, 0x00, 0x00, 0x0F, 0xAC, 0x04, 0x01, 0x00, 0x00, 0x0F, 0xAC, 0x04, 0x01, 0x00, 0x00, 0x0F, 0xAC, 0x02, 0x8C, 0x00} |
| 37 | assocCapabilities = []byte{0x2C, 0x01, 0x03, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
| 38 | ) |
| 39 | |
| 40 | type Dot11ApConfig struct { |
| 41 | SSID string |
| 42 | BSSID net.HardwareAddr |
| 43 | Channel int |
| 44 | Encryption bool |
| 45 | SpectrumManagement bool |
| 46 | } |
| 47 | |
| 48 | func Dot11Info(id layers.Dot11InformationElementID, info []byte) *layers.Dot11InformationElement { |
| 49 | return &layers.Dot11InformationElement{ |
| 50 | ID: id, |
| 51 | Length: uint8(len(info) & 0xff), |
| 52 | Info: info, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func NewDot11Beacon(conf Dot11ApConfig, seq uint16, extendDot11Info ...*layers.Dot11InformationElement) (error, []byte) { |
| 57 | flags := openFlags |
| 58 | if conf.Encryption { |
| 59 | flags = wpaFlags |
| 60 | } |
| 61 | if conf.SpectrumManagement { |
| 62 | flags |= specManFlag |
| 63 | } |
| 64 | stack := []gopacket.SerializableLayer{ |
| 65 | &layers.RadioTap{ |
| 66 | DBMAntennaSignal: int8(-10), |
| 67 | ChannelFrequency: layers.RadioTapChannelFrequency(network.Dot11Chan2Freq(conf.Channel)), |
| 68 | }, |
| 69 | &layers.Dot11{ |
| 70 | Address1: network.BroadcastHw, |
| 71 | Address2: conf.BSSID, |
| 72 | Address3: conf.BSSID, |
| 73 | Type: layers.Dot11TypeMgmtBeacon, |
| 74 | SequenceNumber: seq, |
| 75 | }, |
| 76 | &layers.Dot11MgmtBeacon{ |
| 77 | Flags: uint16(flags), |
| 78 | Interval: 100, |
| 79 | }, |
| 80 | Dot11Info(layers.Dot11InformationElementIDSSID, []byte(conf.SSID)), |
| 81 | Dot11Info(layers.Dot11InformationElementIDRates, fakeApRates), |
| 82 | Dot11Info(layers.Dot11InformationElementIDDSSet, []byte{byte(conf.Channel & 0xff)}), |
| 83 | } |
| 84 | for _, v := range extendDot11Info { |
| 85 | stack = append(stack, v) |
| 86 | } |
| 87 | if conf.Encryption { |
| 88 | stack = append(stack, &layers.Dot11InformationElement{ |
| 89 | ID: layers.Dot11InformationElementIDRSNInfo, |
| 90 | Length: uint8(len(fakeApWpaRSN) & 0xff), |
| 91 | Info: fakeApWpaRSN, |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | return Serialize(stack...) |
| 96 | } |
| 97 | |
| 98 | func NewDot11ProbeRequest(staMac net.HardwareAddr, seq uint16, ssid string, channel int) (error, []byte) { |
| 99 | stack := []gopacket.SerializableLayer{ |
| 100 | &layers.RadioTap{}, |
| 101 | &layers.Dot11{ |
| 102 | Address1: network.BroadcastHw, |
| 103 | Address2: staMac, |
| 104 | Address3: network.BroadcastHw, |
| 105 | Type: layers.Dot11TypeMgmtProbeReq, |
| 106 | SequenceNumber: seq, |
| 107 | }, |
| 108 | &layers.Dot11InformationElement{ |
| 109 | ID: layers.Dot11InformationElementIDSSID, |
| 110 | Length: uint8(len(ssid) & 0xff), |
| 111 | Info: []byte(ssid), |
| 112 | }, |
| 113 | Dot11Info(layers.Dot11InformationElementIDRates, []byte{0x82, 0x84, 0x8b, 0x96}), |
| 114 | Dot11Info(layers.Dot11InformationElementIDESRates, []byte{0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c}), |
| 115 | Dot11Info(layers.Dot11InformationElementIDDSSet, []byte{byte(channel & 0xff)}), |
| 116 | Dot11Info(layers.Dot11InformationElementIDHTCapabilities, []byte{0x2d, 0x40, 0x1b, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), |
| 117 | Dot11Info(layers.Dot11InformationElementIDExtCapability, []byte{0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x40}), |
| 118 | Dot11Info(0xff /* HE Capabilities */, []byte{0x23, 0x01, 0x08, 0x08, 0x18, 0x00, 0x80, 0x20, 0x30, 0x02, 0x00, 0x0d, 0x00, 0x9f, 0x08, 0x00, 0x00, 0x00, 0xfd, 0xff, 0xfd, 0xff, 0x39, 0x1c, 0xc7, 0x71, 0x1c, 0x07}), |
| 119 | } |
| 120 | |
| 121 | return Serialize(stack...) |
| 122 | } |
| 123 | |
| 124 | func NewDot11Deauth(a1 net.HardwareAddr, a2 net.HardwareAddr, a3 net.HardwareAddr, seq uint16) (error, []byte) { |
| 125 | return Serialize( |
| 126 | &layers.RadioTap{}, |
| 127 | &layers.Dot11{ |
| 128 | Address1: a1, |
| 129 | Address2: a2, |
| 130 | Address3: a3, |
| 131 | Type: layers.Dot11TypeMgmtDeauthentication, |
| 132 | SequenceNumber: seq, |
| 133 | }, |
| 134 | &layers.Dot11MgmtDeauthentication{ |
| 135 | Reason: layers.Dot11ReasonClass2FromNonAuth, |
| 136 | }, |
| 137 | ) |
| 138 | } |
| 139 | |
| 140 | func NewDot11Auth(sta net.HardwareAddr, apBSSID net.HardwareAddr, seq uint16) (error, []byte) { |
| 141 | return Serialize( |
| 142 | &layers.RadioTap{}, |
| 143 | &layers.Dot11{ |
| 144 | Address1: apBSSID, |
| 145 | Address2: sta, |
| 146 | Address3: apBSSID, |
| 147 | Type: layers.Dot11TypeMgmtAuthentication, |
| 148 | SequenceNumber: seq, |
| 149 | FragmentNumber: 0, |
| 150 | DurationID: durationID, |
| 151 | }, |
| 152 | &layers.Dot11MgmtAuthentication{ |
| 153 | Algorithm: layers.Dot11AlgorithmOpen, |
| 154 | Sequence: 1, |
| 155 | Status: layers.Dot11StatusSuccess, |
| 156 | }, |
| 157 | ) |
| 158 | } |
| 159 | |
| 160 | func NewDot11AssociationRequest(sta net.HardwareAddr, apBSSID net.HardwareAddr, apESSID string, seq uint16) (error, []byte) { |
| 161 | return Serialize( |
| 162 | &layers.RadioTap{}, |
| 163 | &layers.Dot11{ |
| 164 | Address1: apBSSID, |
| 165 | Address2: sta, |
| 166 | Address3: apBSSID, |
| 167 | Type: layers.Dot11TypeMgmtAssociationReq, |
| 168 | SequenceNumber: seq, |
| 169 | FragmentNumber: 0, |
| 170 | DurationID: durationID, |
| 171 | }, |
| 172 | // as seen on wireshark ... |
| 173 | &layers.Dot11MgmtAssociationReq{ |
| 174 | CapabilityInfo: capabilityInfo, |
| 175 | ListenInterval: listenInterval, |
| 176 | }, |
| 177 | Dot11Info(layers.Dot11InformationElementIDSSID, []byte(apESSID)), |
| 178 | Dot11Info(layers.Dot11InformationElementIDRates, assocRates), |
| 179 | Dot11Info(layers.Dot11InformationElementIDESRates, assocESRates), |
| 180 | Dot11Info(layers.Dot11InformationElementIDRSNInfo, assocRSNInfo), |
| 181 | Dot11Info(layers.Dot11InformationElementIDHTCapabilities, assocCapabilities), |
| 182 | &layers.Dot11InformationElement{ |
| 183 | ID: layers.Dot11InformationElementIDVendor, |
| 184 | Length: 7, |
| 185 | OUI: []byte{0, 0x50, 0xf2, 0x02}, |
| 186 | Info: []byte{0, 0x01, 0}, |
| 187 | }, |
| 188 | ) |
| 189 | } |
| 190 | |
| 191 | func Dot11Parse(packet gopacket.Packet) (ok bool, radiotap *layers.RadioTap, dot11 *layers.Dot11) { |
| 192 | ok = false |
| 193 | radiotap = nil |
| 194 | dot11 = nil |
| 195 | |
| 196 | radiotapLayer := packet.Layer(layers.LayerTypeRadioTap) |
| 197 | if radiotapLayer == nil { |
| 198 | return |
| 199 | } |
| 200 | radiotap, ok = radiotapLayer.(*layers.RadioTap) |
| 201 | if !ok || radiotap == nil { |
| 202 | return |
| 203 | } |
| 204 | |
| 205 | dot11Layer := packet.Layer(layers.LayerTypeDot11) |
| 206 | if dot11Layer == nil { |
| 207 | ok = false |
| 208 | return |
| 209 | } |
| 210 | |
| 211 | dot11, ok = dot11Layer.(*layers.Dot11) |
| 212 | return |
| 213 | } |
| 214 | |
| 215 | func Dot11ParseIDSSID(packet gopacket.Packet) (bool, string) { |
| 216 | for _, layer := range packet.Layers() { |
| 217 | if layer.LayerType() == layers.LayerTypeDot11InformationElement { |
| 218 | dot11info, ok := layer.(*layers.Dot11InformationElement) |
| 219 | if ok && dot11info.ID == layers.Dot11InformationElementIDSSID { |
| 220 | if len(dot11info.Info) == 0 { |
| 221 | return true, "<hidden>" |
| 222 | } |
| 223 | return true, string(dot11info.Info) |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return false, "" |
| 229 | } |
| 230 | |
| 231 | func Dot11ParseEncryption(packet gopacket.Packet, dot11 *layers.Dot11) (bool, string, string, string) { |
| 232 | var i uint16 |
| 233 | enc := "" |
| 234 | cipher := "" |
| 235 | auth := "" |
| 236 | found := false |
| 237 | |
| 238 | if dot11.Flags.WEP() { |
| 239 | found = true |
| 240 | enc = "WEP" |
| 241 | } |
| 242 | |
| 243 | for _, layer := range packet.Layers() { |
| 244 | if layer.LayerType() == layers.LayerTypeDot11InformationElement { |
| 245 | info, ok := layer.(*layers.Dot11InformationElement) |
| 246 | if ok { |
| 247 | found = true |
| 248 | if info.ID == layers.Dot11InformationElementIDRSNInfo { |
| 249 | enc = "WPA2" |
| 250 | rsn, err := Dot11InformationElementRSNInfoDecode(info.Info) |
| 251 | if err == nil { |
| 252 | for i = 0; i < rsn.Pairwise.Count; i++ { |
| 253 | cipher = rsn.Pairwise.Suites[i].Type.String() |
| 254 | } |
| 255 | for i = 0; i < rsn.AuthKey.Count; i++ { |
| 256 | auth = rsn.AuthKey.Suites[i].Type.String() |
| 257 | } |
| 258 | } |
| 259 | } else if enc == "" && info.ID == layers.Dot11InformationElementIDVendor && info.Length >= 8 && bytes.Equal(info.OUI, wpaSignatureBytes) && bytes.HasPrefix(info.Info, []byte{1, 0}) { |
| 260 | enc = "WPA" |
| 261 | vendor, err := Dot11InformationElementVendorInfoDecode(info.Info) |
| 262 | if err == nil { |
| 263 | for i = 0; i < vendor.Unicast.Count; i++ { |
| 264 | cipher = vendor.Unicast.Suites[i].Type.String() |
| 265 | } |
| 266 | for i = 0; i < vendor.AuthKey.Count; i++ { |
| 267 | auth = vendor.AuthKey.Suites[i].Type.String() |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if found && enc == "" { |
| 276 | enc = "OPEN" |
| 277 | } |
| 278 | |
| 279 | return found, enc, cipher, auth |
| 280 | |
| 281 | } |
| 282 | |
| 283 | func Dot11IsDataFor(dot11 *layers.Dot11, station net.HardwareAddr) bool { |
| 284 | // only check data packets of connected stations |
| 285 | if dot11.Type.MainType() != layers.Dot11TypeData { |
| 286 | return false |
| 287 | } |
| 288 | // packet going to this specific BSSID? |
| 289 | return bytes.Equal(dot11.Address1, station) |
| 290 | } |
| 291 | |
| 292 | func Dot11ParseDSSet(packet gopacket.Packet) (bool, int) { |
| 293 | channel := 0 |
| 294 | found := false |
| 295 | for _, layer := range packet.Layers() { |
| 296 | info, ok := layer.(*layers.Dot11InformationElement) |
| 297 | if ok { |
| 298 | if info.ID == layers.Dot11InformationElementIDDSSet { |
| 299 | channel, _ = Dot11InformationElementIDDSSetDecode(info.Info) |
| 300 | found = true |
| 301 | break |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return found, channel |
| 307 | } |
| 308 | |
| 309 | func Dot11ParseEAPOL(packet gopacket.Packet, dot11 *layers.Dot11) (ok bool, key *layers.EAPOLKey, apMac net.HardwareAddr, staMac net.HardwareAddr) { |
| 310 | ok = false |
| 311 | // ref. https://wlan1nde.wordpress.com/2014/10/27/4-way-handshake/ |
| 312 | if keyLayer := packet.Layer(layers.LayerTypeEAPOLKey); keyLayer != nil { |
| 313 | if key = keyLayer.(*layers.EAPOLKey); key.KeyType == layers.EAPOLKeyTypePairwise { |
| 314 | ok = true |
| 315 | if dot11.Flags.FromDS() { |
| 316 | staMac = dot11.Address1 |
| 317 | apMac = dot11.Address2 |
| 318 | } else if dot11.Flags.ToDS() { |
| 319 | staMac = dot11.Address2 |
| 320 | apMac = dot11.Address1 |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | return |
| 325 | } |