| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "encoding/hex" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type wpsAttrType int |
| 12 | |
| 13 | const ( |
| 14 | wpsHex wpsAttrType = 0 |
| 15 | wpsStr wpsAttrType = 1 |
| 16 | ) |
| 17 | |
| 18 | type wpsAttr struct { |
| 19 | Name string |
| 20 | Type wpsAttrType |
| 21 | Func func([]byte, *map[string]string) string |
| 22 | Desc map[string]string |
| 23 | } |
| 24 | |
| 25 | type wpsDevType struct { |
| 26 | Category string |
| 27 | Subcats map[uint16]string |
| 28 | } |
| 29 | |
| 30 | var ( |
| 31 | wfaExtensionBytes = []byte{0x00, 0x37, 0x2a} |
| 32 | wpsVersion2ID = uint8(0x00) |
| 33 | wpsVersionDesc = map[string]string{ |
| 34 | "10": "1.0", |
| 35 | "11": "1.1", |
| 36 | "20": "2.0", |
| 37 | } |
| 38 | |
| 39 | wpsDeviceTypes = map[uint16]wpsDevType{ |
| 40 | 0x0001: {"Computer", map[uint16]string{ |
| 41 | 0x0001: "PC", |
| 42 | 0x0002: "Server", |
| 43 | 0x0003: "Media Center", |
| 44 | }}, |
| 45 | 0x0002: {"Input Device", map[uint16]string{}}, |
| 46 | 0x0003: {"Printers, Scanners, Faxes and Copiers", map[uint16]string{ |
| 47 | 0x0001: "Printer", |
| 48 | 0x0002: "Scanner", |
| 49 | }}, |
| 50 | 0x0004: {"Camera", map[uint16]string{ |
| 51 | 0x0001: "Digital Still Camera", |
| 52 | }}, |
| 53 | 0x0005: {"Storage", map[uint16]string{ |
| 54 | 0x0001: "NAS", |
| 55 | }}, |
| 56 | 0x0006: {"Network Infra", map[uint16]string{ |
| 57 | 0x0001: "AP", |
| 58 | 0x0002: "Router", |
| 59 | 0x0003: "Switch", |
| 60 | }}, |
| 61 | |
| 62 | 0x0007: {"Display", map[uint16]string{ |
| 63 | 0x0001: "TV", |
| 64 | 0x0002: "Electronic Picture Frame", |
| 65 | 0x0003: "Projector", |
| 66 | }}, |
| 67 | |
| 68 | 0x0008: {"Multimedia Device", map[uint16]string{ |
| 69 | 0x0001: "DAR", |
| 70 | 0x0002: "PVR", |
| 71 | 0x0003: "MCX", |
| 72 | }}, |
| 73 | |
| 74 | 0x0009: {"Gaming Device", map[uint16]string{ |
| 75 | 0x0001: "XBox", |
| 76 | 0x0002: "XBox360", |
| 77 | 0x0003: "Playstation", |
| 78 | }}, |
| 79 | 0x000F: {"Telephone", map[uint16]string{ |
| 80 | 0x0001: "Windows Mobile", |
| 81 | }}, |
| 82 | } |
| 83 | |
| 84 | wpsAttributes = map[uint16]wpsAttr{ |
| 85 | 0x104A: {Name: "Version", Desc: wpsVersionDesc}, |
| 86 | 0x1044: {Name: "State", Desc: map[string]string{ |
| 87 | "01": "Not Configured", |
| 88 | "02": "Configured", |
| 89 | }}, |
| 90 | 0x1012: {Name: "Device Password ID", Desc: map[string]string{ |
| 91 | "0000": "Pin", |
| 92 | "0004": "PushButton", |
| 93 | }}, |
| 94 | 0x103B: {Name: "Response Type", Desc: map[string]string{ |
| 95 | "00": "Enrollee Info", |
| 96 | "01": "Enrollee", |
| 97 | "02": "Registrar", |
| 98 | "03": "AP", |
| 99 | }}, |
| 100 | |
| 101 | 0x1054: {Name: "Primary Device Type", Func: dot11ParseWPSDeviceType}, |
| 102 | 0x1049: {Name: "Vendor Extension", Func: dot11ParseWPSVendorExtension}, |
| 103 | 0x1053: {Name: "Selected Registrar Config Methods", Func: dot11ParseWPSConfigMethods}, |
| 104 | 0x1008: {Name: "Config Methods", Func: dot11ParseWPSConfigMethods}, |
| 105 | 0x103C: {Name: "RF Bands", Func: dott11ParseWPSBands}, |
| 106 | |
| 107 | 0x1057: {Name: "AP Setup Locked"}, |
| 108 | 0x1041: {Name: "Selected Registrar"}, |
| 109 | 0x1047: {Name: "UUID-E"}, |
| 110 | 0x1021: {Name: "Manufacturer", Type: wpsStr}, |
| 111 | 0x1023: {Name: "Model Name", Type: wpsStr}, |
| 112 | 0x1024: {Name: "Model Number", Type: wpsStr}, |
| 113 | 0x1042: {Name: "Serial Number", Type: wpsStr}, |
| 114 | 0x1011: {Name: "Device Name", Type: wpsStr}, |
| 115 | 0x1045: {Name: "SSID", Type: wpsStr}, |
| 116 | 0x102D: {Name: "OS Version", Type: wpsStr}, |
| 117 | } |
| 118 | |
| 119 | wpsConfigs = map[uint16]string{ |
| 120 | 0x0001: "USB", |
| 121 | 0x0002: "Ethernet", |
| 122 | 0x0004: "Label", |
| 123 | 0x0008: "Display", |
| 124 | 0x0010: "External NFC", |
| 125 | 0x0020: "Internal NFC", |
| 126 | 0x0040: "NFC Interface", |
| 127 | 0x0080: "Push Button", |
| 128 | 0x0100: "Keypad", |
| 129 | } |
| 130 | |
| 131 | wpsBands = map[uint8]string{ |
| 132 | 0x01: "2.4Ghz", |
| 133 | 0x02: "5.0Ghz", |
| 134 | } |
| 135 | ) |
| 136 | |
| 137 | func dott11ParseWPSBands(data []byte, info *map[string]string) string { |
| 138 | if len(data) == 1 { |
| 139 | mask := uint8(data[0]) |
| 140 | bands := []string{} |
| 141 | |
| 142 | for bit, band := range wpsBands { |
| 143 | if mask&bit != 0 { |
| 144 | bands = append(bands, band) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if len(bands) > 0 { |
| 149 | return strings.Join(bands, ", ") |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return hex.EncodeToString(data) |
| 154 | } |
| 155 | |
| 156 | func dot11ParseWPSConfigMethods(data []byte, info *map[string]string) string { |
| 157 | if len(data) == 2 { |
| 158 | mask := binary.BigEndian.Uint16(data) |
| 159 | configs := []string{} |
| 160 | |
| 161 | for bit, conf := range wpsConfigs { |
| 162 | if mask&bit != 0 { |
| 163 | configs = append(configs, conf) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if len(configs) > 0 { |
| 168 | return strings.Join(configs, ", ") |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return hex.EncodeToString(data) |
| 173 | } |
| 174 | |
| 175 | func dot11ParseWPSVendorExtension(data []byte, info *map[string]string) string { |
| 176 | if len(data) > 3 && bytes.Equal(data[0:3], wfaExtensionBytes) { |
| 177 | size := len(data) |
| 178 | for offset := 3; offset < size; { |
| 179 | idByte := uint8(data[offset]) |
| 180 | if next := offset + 1; next < size { |
| 181 | sizeByte := uint8(data[next]) |
| 182 | if idByte == wpsVersion2ID { |
| 183 | if next = offset + 2; next < size { |
| 184 | verByte := fmt.Sprintf("%x", data[next]) |
| 185 | (*info)["Version"] = wpsVersionDesc[verByte] |
| 186 | if next = offset + 3; next < size { |
| 187 | data = data[next:] |
| 188 | } |
| 189 | break |
| 190 | } |
| 191 | } |
| 192 | offset += int(sizeByte) + 2 |
| 193 | } else { |
| 194 | break |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | return hex.EncodeToString(data) |
| 199 | } |
| 200 | |
| 201 | func dot11ParseWPSDeviceType(data []byte, info *map[string]string) string { |
| 202 | if len(data) == 8 { |
| 203 | catId := binary.BigEndian.Uint16(data[0:2]) |
| 204 | oui := data[2:6] |
| 205 | subCatId := binary.BigEndian.Uint16(data[6:8]) |
| 206 | if cat, found := wpsDeviceTypes[catId]; found { |
| 207 | if sub, found := cat.Subcats[subCatId]; found { |
| 208 | return fmt.Sprintf("%s (oui:%x)", sub, oui) |
| 209 | } |
| 210 | return fmt.Sprintf("%s (oui:%x)", cat.Category, oui) |
| 211 | } |
| 212 | return fmt.Sprintf("cat:%x sub:%x oui:%x %x", catId, subCatId, oui, data) |
| 213 | } |
| 214 | return hex.EncodeToString(data) |
| 215 | } |