| 1 | package network |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | |
| 6 | "github.com/google/gopacket" |
| 7 | "github.com/google/gopacket/layers" |
| 8 | ) |
| 9 | |
| 10 | type Handshake struct { |
| 11 | sync.RWMutex |
| 12 | |
| 13 | Beacon gopacket.Packet |
| 14 | Challenges []gopacket.Packet |
| 15 | Responses []gopacket.Packet |
| 16 | Confirmations []gopacket.Packet |
| 17 | hasPMKID bool |
| 18 | unsaved []gopacket.Packet |
| 19 | } |
| 20 | |
| 21 | func NewHandshake() *Handshake { |
| 22 | return &Handshake{ |
| 23 | Challenges: make([]gopacket.Packet, 0), |
| 24 | Responses: make([]gopacket.Packet, 0), |
| 25 | Confirmations: make([]gopacket.Packet, 0), |
| 26 | unsaved: make([]gopacket.Packet, 0), |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func (h *Handshake) SetBeacon(pkt gopacket.Packet) { |
| 31 | h.Lock() |
| 32 | defer h.Unlock() |
| 33 | |
| 34 | if h.Beacon == nil { |
| 35 | h.Beacon = pkt |
| 36 | h.unsaved = append(h.unsaved, pkt) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func (h *Handshake) AddAndGetPMKID(pkt gopacket.Packet) []byte { |
| 41 | h.AddFrame(0, pkt) |
| 42 | |
| 43 | prevWasKey := false |
| 44 | for _, layer := range pkt.Layers() { |
| 45 | if layer.LayerType() == layers.LayerTypeEAPOLKey { |
| 46 | prevWasKey = true |
| 47 | continue |
| 48 | } |
| 49 | |
| 50 | if prevWasKey && layer.LayerType() == layers.LayerTypeDot11InformationElement { |
| 51 | info := layer.(*layers.Dot11InformationElement) |
| 52 | if info.ID == layers.Dot11InformationElementIDVendor && info.Length == 20 { |
| 53 | h.Lock() |
| 54 | defer h.Unlock() |
| 55 | h.hasPMKID = true |
| 56 | return info.Info |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | prevWasKey = false |
| 61 | } |
| 62 | |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | func (h *Handshake) AddFrame(n int, pkt gopacket.Packet) { |
| 67 | h.Lock() |
| 68 | defer h.Unlock() |
| 69 | |
| 70 | switch n { |
| 71 | case 0: |
| 72 | h.Challenges = append(h.Challenges, pkt) |
| 73 | case 1: |
| 74 | h.Responses = append(h.Responses, pkt) |
| 75 | case 2: |
| 76 | h.Confirmations = append(h.Confirmations, pkt) |
| 77 | } |
| 78 | |
| 79 | h.unsaved = append(h.unsaved, pkt) |
| 80 | } |
| 81 | |
| 82 | func (h *Handshake) AddExtra(pkt gopacket.Packet) { |
| 83 | h.Lock() |
| 84 | defer h.Unlock() |
| 85 | h.unsaved = append(h.unsaved, pkt) |
| 86 | } |
| 87 | |
| 88 | func (h *Handshake) Complete() bool { |
| 89 | h.RLock() |
| 90 | defer h.RUnlock() |
| 91 | |
| 92 | nChal := len(h.Challenges) |
| 93 | nResp := len(h.Responses) |
| 94 | nConf := len(h.Confirmations) |
| 95 | |
| 96 | return nChal > 0 && nResp > 0 && nConf > 0 |
| 97 | } |
| 98 | |
| 99 | func (h *Handshake) Half() bool { |
| 100 | h.RLock() |
| 101 | defer h.RUnlock() |
| 102 | |
| 103 | /* |
| 104 | * You can use every combination of the handshake to crack the net: |
| 105 | * M1/M2 |
| 106 | * M2/M3 |
| 107 | * M3/M4 |
| 108 | * M1/M4 (if M4 snonce is not zero) |
| 109 | * We only have M1 (the challenge), M2 (the response) and M3 (the confirmation) |
| 110 | */ |
| 111 | nChal := len(h.Challenges) |
| 112 | nResp := len(h.Responses) |
| 113 | nConf := len(h.Confirmations) |
| 114 | |
| 115 | return (nChal > 0 && nResp > 0) || (nResp > 0 && nConf > 0) |
| 116 | } |
| 117 | |
| 118 | func (h *Handshake) HasPMKID() bool { |
| 119 | h.RLock() |
| 120 | defer h.RUnlock() |
| 121 | return h.hasPMKID |
| 122 | } |
| 123 | |
| 124 | func (h *Handshake) Any() bool { |
| 125 | return h.HasPMKID() || h.Half() || h.Complete() |
| 126 | } |
| 127 | |
| 128 | func (h *Handshake) NumUnsaved() int { |
| 129 | h.RLock() |
| 130 | defer h.RUnlock() |
| 131 | return len(h.unsaved) |
| 132 | } |
| 133 | |
| 134 | func (h *Handshake) EachUnsavedPacket(cb func(gopacket.Packet)) { |
| 135 | h.Lock() |
| 136 | defer h.Unlock() |
| 137 | |
| 138 | for _, pkt := range h.unsaved { |
| 139 | cb(pkt) |
| 140 | } |
| 141 | h.unsaved = make([]gopacket.Packet, 0) |
| 142 | } |