| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "encoding/hex" |
| 7 | "fmt" |
| 8 | "net" |
| 9 | |
| 10 | "github.com/google/gopacket" |
| 11 | "github.com/google/gopacket/layers" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | wpsSignatureBytes = []byte{0x00, 0x50, 0xf2, 0x04} |
| 16 | ) |
| 17 | |
| 18 | func wpsUint16At(data []byte, size int, offset *int) (bool, uint16) { |
| 19 | if *offset <= size-2 { |
| 20 | off := *offset |
| 21 | *offset += 2 |
| 22 | return true, binary.BigEndian.Uint16(data[off:]) |
| 23 | } |
| 24 | // fmt.Printf("uint16At( data(%d), off=%d )\n", size, *offset) |
| 25 | return false, 0 |
| 26 | } |
| 27 | |
| 28 | func wpsDataAt(data []byte, size int, offset *int, num int) (bool, []byte) { |
| 29 | max := size - num |
| 30 | if *offset <= max { |
| 31 | off := *offset |
| 32 | *offset += num |
| 33 | return true, data[off : off+num] |
| 34 | } |
| 35 | // fmt.Printf("dataAt( data(%d), off=%d, num=%d )\n", size, *offset, num) |
| 36 | return false, nil |
| 37 | } |
| 38 | |
| 39 | func dot11ParseWPSTag(id uint16, size uint16, data []byte, info *map[string]string) { |
| 40 | name := "" |
| 41 | val := "" |
| 42 | |
| 43 | if attr, found := wpsAttributes[id]; found { |
| 44 | name = attr.Name |
| 45 | if attr.Type == wpsStr { |
| 46 | val = string(data) |
| 47 | } else { |
| 48 | val = hex.EncodeToString(data) |
| 49 | } |
| 50 | |
| 51 | if attr.Desc != nil { |
| 52 | if desc, found := attr.Desc[val]; found { |
| 53 | val = desc |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if attr.Func != nil { |
| 58 | val = attr.Func(data, info) |
| 59 | } |
| 60 | } else { |
| 61 | name = fmt.Sprintf("0x%X", id) |
| 62 | val = hex.EncodeToString(data) |
| 63 | } |
| 64 | |
| 65 | if val != "" { |
| 66 | (*info)[name] = val |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func dot11ParseWPSData(data []byte) (ok bool, info map[string]string) { |
| 71 | info = map[string]string{} |
| 72 | size := len(data) |
| 73 | |
| 74 | for offset := 0; offset < size; { |
| 75 | ok := false |
| 76 | tagId := uint16(0) |
| 77 | tagLen := uint16(0) |
| 78 | tagData := []byte(nil) |
| 79 | |
| 80 | if ok, tagId = wpsUint16At(data, size, &offset); !ok { |
| 81 | break |
| 82 | } else if ok, tagLen = wpsUint16At(data, size, &offset); !ok { |
| 83 | break |
| 84 | } else if ok, tagData = wpsDataAt(data, size, &offset, int(tagLen)); !ok { |
| 85 | break |
| 86 | } else { |
| 87 | dot11ParseWPSTag(tagId, tagLen, tagData, &info) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return true, info |
| 92 | } |
| 93 | |
| 94 | func Dot11ParseWPS(packet gopacket.Packet, dot11 *layers.Dot11) (ok bool, bssid net.HardwareAddr, info map[string]string) { |
| 95 | ok = false |
| 96 | for _, layer := range packet.Layers() { |
| 97 | if layer.LayerType() == layers.LayerTypeDot11InformationElement { |
| 98 | if dot11info, infoOk := layer.(*layers.Dot11InformationElement); infoOk && dot11info.ID == layers.Dot11InformationElementIDVendor { |
| 99 | if bytes.Equal(dot11info.OUI, wpsSignatureBytes) { |
| 100 | bssid = dot11.Address3 |
| 101 | ok, info = dot11ParseWPSData(dot11info.Info) |
| 102 | return |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return |
| 108 | } |