| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "fmt" |
| 6 | ) |
| 7 | |
| 8 | type Dot11CipherType uint8 |
| 9 | |
| 10 | const ( |
| 11 | Dot11CipherWep Dot11CipherType = 1 |
| 12 | Dot11CipherTkip Dot11CipherType = 2 |
| 13 | Dot11CipherWrap Dot11CipherType = 3 |
| 14 | Dot11CipherCcmp Dot11CipherType = 4 |
| 15 | Dot11CipherWep104 Dot11CipherType = 5 |
| 16 | ) |
| 17 | |
| 18 | func (a Dot11CipherType) String() string { |
| 19 | switch a { |
| 20 | case Dot11CipherWep: |
| 21 | return "WEP" |
| 22 | case Dot11CipherTkip: |
| 23 | return "TKIP" |
| 24 | case Dot11CipherWrap: |
| 25 | return "WRAP" |
| 26 | case Dot11CipherCcmp: |
| 27 | return "CCMP" |
| 28 | case Dot11CipherWep104: |
| 29 | return "WEP104" |
| 30 | default: |
| 31 | return "UNK" |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | type Dot11AuthType uint8 |
| 36 | |
| 37 | const ( |
| 38 | Dot11AuthMgt Dot11AuthType = 1 |
| 39 | Dot11AuthPsk Dot11AuthType = 2 |
| 40 | ) |
| 41 | |
| 42 | func (a Dot11AuthType) String() string { |
| 43 | switch a { |
| 44 | case Dot11AuthMgt: |
| 45 | return "MGT" |
| 46 | case Dot11AuthPsk: |
| 47 | return "PSK" |
| 48 | default: |
| 49 | return "UNK" |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | type CipherSuite struct { |
| 54 | OUI []byte // 3 bytes |
| 55 | Type Dot11CipherType |
| 56 | } |
| 57 | |
| 58 | type AuthSuite struct { |
| 59 | OUI []byte // 3 bytes |
| 60 | Type Dot11AuthType |
| 61 | } |
| 62 | |
| 63 | type CipherSuiteSelector struct { |
| 64 | Count uint16 |
| 65 | Suites []CipherSuite |
| 66 | } |
| 67 | |
| 68 | type AuthSuiteSelector struct { |
| 69 | Count uint16 |
| 70 | Suites []AuthSuite |
| 71 | } |
| 72 | |
| 73 | type RSNInfo struct { |
| 74 | Version uint16 |
| 75 | Group CipherSuite |
| 76 | Pairwise CipherSuiteSelector |
| 77 | AuthKey AuthSuiteSelector |
| 78 | } |
| 79 | |
| 80 | type VendorInfo struct { |
| 81 | WPAVersion uint16 |
| 82 | Multicast CipherSuite |
| 83 | Unicast CipherSuiteSelector |
| 84 | AuthKey AuthSuiteSelector |
| 85 | } |
| 86 | |
| 87 | func canParse(what string, buf []byte, need int) error { |
| 88 | available := len(buf) |
| 89 | if need > available { |
| 90 | return fmt.Errorf("Malformed 802.11 packet, could not parse %s: needed %d bytes but only %d are available.", what, need, available) |
| 91 | } |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | func parsePairwiseSuite(buf []byte) (suite CipherSuite, err error) { |
| 96 | if err = canParse("RSN.Pairwise.Suite", buf, 4); err == nil { |
| 97 | suite.OUI = buf[0:3] |
| 98 | suite.Type = Dot11CipherType(buf[3]) |
| 99 | } |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | func parseAuthkeySuite(buf []byte) (suite AuthSuite, err error) { |
| 104 | if err = canParse("RSN.AuthKey.Suite", buf, 4); err == nil { |
| 105 | suite.OUI = buf[0:3] |
| 106 | suite.Type = Dot11AuthType(buf[3]) |
| 107 | } |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | func Dot11InformationElementVendorInfoDecode(buf []byte) (v VendorInfo, err error) { |
| 112 | if err = canParse("Vendor", buf, 8); err == nil { |
| 113 | v.WPAVersion = binary.LittleEndian.Uint16(buf[0:2]) |
| 114 | v.Multicast.OUI = buf[2:5] |
| 115 | v.Multicast.Type = Dot11CipherType(buf[5]) |
| 116 | v.Unicast.Count = binary.LittleEndian.Uint16(buf[6:8]) |
| 117 | buf = buf[8:] |
| 118 | } else { |
| 119 | v.Unicast.Count = 0 |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | // check what we're left with |
| 124 | if err = canParse("Vendor.Unicast.Suites", buf, int(v.Unicast.Count)*4); err == nil { |
| 125 | for i := uint16(0); i < v.Unicast.Count; i++ { |
| 126 | if suite, err := parsePairwiseSuite(buf); err == nil { |
| 127 | v.Unicast.Suites = append(v.Unicast.Suites, suite) |
| 128 | buf = buf[4:] |
| 129 | } else { |
| 130 | return v, err |
| 131 | } |
| 132 | } |
| 133 | } else { |
| 134 | v.Unicast.Count = 0 |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | if err = canParse("Vendor.AuthKey.Count", buf, 2); err == nil { |
| 139 | v.AuthKey.Count = binary.LittleEndian.Uint16(buf[0:2]) |
| 140 | buf = buf[2:] |
| 141 | } else { |
| 142 | v.AuthKey.Count = 0 |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | // just like before, check if we have enough data |
| 147 | if err = canParse("Vendor.AuthKey.Suites", buf, int(v.AuthKey.Count)*4); err == nil { |
| 148 | for i := uint16(0); i < v.AuthKey.Count; i++ { |
| 149 | if suite, err := parseAuthkeySuite(buf); err == nil { |
| 150 | v.AuthKey.Suites = append(v.AuthKey.Suites, suite) |
| 151 | buf = buf[4:] |
| 152 | } else { |
| 153 | return v, err |
| 154 | } |
| 155 | } |
| 156 | } else { |
| 157 | v.AuthKey.Count = 0 |
| 158 | } |
| 159 | |
| 160 | return |
| 161 | } |
| 162 | |
| 163 | func Dot11InformationElementRSNInfoDecode(buf []byte) (rsn RSNInfo, err error) { |
| 164 | if err = canParse("RSN", buf, 8); err == nil { |
| 165 | rsn.Version = binary.LittleEndian.Uint16(buf[0:2]) |
| 166 | rsn.Group.OUI = buf[2:5] |
| 167 | rsn.Group.Type = Dot11CipherType(buf[5]) |
| 168 | rsn.Pairwise.Count = binary.LittleEndian.Uint16(buf[6:8]) |
| 169 | buf = buf[8:] |
| 170 | } else { |
| 171 | rsn.Pairwise.Count = 0 |
| 172 | return |
| 173 | } |
| 174 | |
| 175 | // check what we're left with |
| 176 | if err = canParse("RSN.Pairwise.Suites", buf, int(rsn.Pairwise.Count)*4); err == nil { |
| 177 | for i := uint16(0); i < rsn.Pairwise.Count; i++ { |
| 178 | if suite, err := parsePairwiseSuite(buf); err == nil { |
| 179 | rsn.Pairwise.Suites = append(rsn.Pairwise.Suites, suite) |
| 180 | buf = buf[4:] |
| 181 | } else { |
| 182 | return rsn, err |
| 183 | } |
| 184 | } |
| 185 | } else { |
| 186 | rsn.Pairwise.Count = 0 |
| 187 | return |
| 188 | } |
| 189 | |
| 190 | if err = canParse("RSN.AuthKey.Count", buf, 2); err == nil { |
| 191 | rsn.AuthKey.Count = binary.LittleEndian.Uint16(buf[0:2]) |
| 192 | buf = buf[2:] |
| 193 | } else { |
| 194 | rsn.AuthKey.Count = 0 |
| 195 | return |
| 196 | } |
| 197 | |
| 198 | // just like before, check if we have enough data |
| 199 | if err = canParse("RSN.AuthKey.Suites", buf, int(rsn.AuthKey.Count)*4); err == nil { |
| 200 | for i := uint16(0); i < rsn.AuthKey.Count; i++ { |
| 201 | if suite, err := parseAuthkeySuite(buf); err == nil { |
| 202 | rsn.AuthKey.Suites = append(rsn.AuthKey.Suites, suite) |
| 203 | buf = buf[4:] |
| 204 | } else { |
| 205 | return rsn, err |
| 206 | } |
| 207 | } |
| 208 | } else { |
| 209 | rsn.AuthKey.Count = 0 |
| 210 | } |
| 211 | |
| 212 | return |
| 213 | } |
| 214 | |
| 215 | func Dot11InformationElementIDDSSetDecode(buf []byte) (channel int, err error) { |
| 216 | if err = canParse("DSSet.channel", buf, 1); err == nil { |
| 217 | channel = int(buf[0]) |
| 218 | } |
| 219 | |
| 220 | return |
| 221 | } |