Debian Codegraph @

modules/wifi/wifi_recon_handshakes.go

raw symbols SCIP
1 package wifi
2
3 import (
4 "bytes"
5 "fmt"
6 "github.com/bettercap/bettercap/network"
7 "net"
8 "path"
9
10 "github.com/bettercap/bettercap/packets"
11
12 "github.com/google/gopacket"
13 "github.com/google/gopacket/layers"
14 )
15
16 func allZeros(s []byte) bool {
17 for _, v := range s {
18 if v != 0 {
19 return false
20 }
21 }
22 return true
23 }
24
25 func (mod *WiFiModule) discoverHandshakes(radiotap *layers.RadioTap, dot11 *layers.Dot11, packet gopacket.Packet) {
26 isEAPOL := false
27
28 if ok, key, apMac, staMac := packets.Dot11ParseEAPOL(packet, dot11); ok {
29 isEAPOL = true
30
31 // first, locate the AP in our list by its BSSID
32 ap, found := mod.Session.WiFi.Get(apMac.String())
33 if !found {
34 mod.Warning("could not find AP with BSSID %s", apMac.String())
35 return
36 }
37
38 // locate the client station, if its BSSID is ours, it means we sent
39 // an association request via wifi.assoc because we're trying to capture
40 // the PMKID from the first EAPOL sent by the AP.
41 // (Reference about PMKID https://hashcat.net/forum/thread-7717.html)
42 // In this case, we need to add ourselves as a client station of the AP
43 // in order to have a consistent association of AP, client and handshakes.
44 staIsUs := bytes.Equal(staMac, mod.iface.HW)
45 station, found := ap.Get(staMac.String())
46 staAdded := false
47 if !found {
48 station, staAdded = ap.AddClientIfNew(staMac.String(), ap.Frequency, ap.RSSI)
49 }
50
51 rawPMKID := []byte(nil)
52 if !key.Install && key.KeyACK && !key.KeyMIC {
53 // [1] (ACK) AP is sending ANonce to the client
54 rawPMKID = station.Handshake.AddAndGetPMKID(packet)
55 PMKID := "without PMKID"
56 if rawPMKID != nil {
57 PMKID = "with PMKID"
58 }
59
60 mod.Debug("got frame 1/4 of the %s <-> %s handshake (%s) (anonce:%x)",
61 apMac,
62 staMac,
63 PMKID,
64 key.Nonce)
65
66 //add the ap's station's beacon packet to be saved as part of the handshake cap file
67 //https://github.com/ZerBea/hcxtools/issues/92
68 //https://github.com/bettercap/bettercap/issues/592
69
70 if ap.Station.Handshake.Beacon != nil {
71 mod.Debug("adding beacon frame to handshake for %s", apMac)
72 station.Handshake.AddFrame(1, ap.Station.Handshake.Beacon)
73 }
74
75 } else if !key.Install && !key.KeyACK && key.KeyMIC && !allZeros(key.Nonce) {
76 // [2] (MIC) client is sending SNonce+MIC to the API
77 station.Handshake.AddFrame(1, packet)
78
79 mod.Debug("got frame 2/4 of the %s <-> %s handshake (snonce:%x mic:%x)",
80 apMac,
81 staMac,
82 key.Nonce,
83 key.MIC)
84 } else if key.Install && key.KeyACK && key.KeyMIC {
85 // [3]: (INSTALL+ACK+MIC) AP informs the client that the PTK is installed
86 station.Handshake.AddFrame(2, packet)
87
88 mod.Debug("got frame 3/4 of the %s <-> %s handshake (mic:%x)",
89 apMac,
90 staMac,
91 key.MIC)
92 }
93
94 // if we have unsaved packets as part of the handshake, save them.
95 numUnsaved := station.Handshake.NumUnsaved()
96 shakesFileName := mod.shakesFile
97 if mod.shakesAggregate == false {
98 shakesFileName = path.Join(shakesFileName, fmt.Sprintf("%s.pcap", ap.PathFriendlyName()))
99 }
100 doSave := numUnsaved > 0
101 if doSave && shakesFileName != "" {
102 mod.Debug("(aggregate %v) saving handshake frames to %s", mod.shakesAggregate, shakesFileName)
103 if err := mod.Session.WiFi.SaveHandshakesTo(shakesFileName, mod.handle.LinkType()); err != nil {
104 mod.Error("error while saving handshake frames to %s: %s", shakesFileName, err)
105 }
106 }
107
108 validPMKID := rawPMKID != nil
109 validHalfHandshake := !staIsUs && station.Handshake.Half()
110 validFullHandshake := station.Handshake.Complete()
111 // if we have unsaved packets AND
112 // if we captured a PMKID OR
113 // if we captured am half handshake which is not ours OR
114 // if we captured a full handshake
115 if doSave && (validPMKID || validHalfHandshake || validFullHandshake) {
116 mod.Session.Events.Add("wifi.client.handshake", HandshakeEvent{
117 File: shakesFileName,
118 NewPackets: numUnsaved,
119 AP: apMac.String(),
120 Station: staMac.String(),
121 PMKID: rawPMKID,
122 Half: station.Handshake.Half(),
123 Full: station.Handshake.Complete(),
124 })
125 // make sure the info that we have key material for this AP
126 // is persisted even after stations are pruned due to inactivity
127 ap.WithKeyMaterial(true)
128 }
129 // if we added ourselves as a client station but we didn't get any
130 // PMKID, just remove it from the list of clients of this AP.
131 if staAdded || (staIsUs && rawPMKID == nil) {
132 ap.RemoveClient(staMac.String())
133 }
134 }
135
136 // quick and dirty heuristic, see thread here https://github.com/bettercap/bettercap/issues/810#issuecomment-805145392
137 if isEAPOL || (dot11.Type.MainType() != layers.Dot11TypeData && dot11.Type.MainType() != layers.Dot11TypeCtrl) {
138 target := (* network.Station)(nil)
139 targetAP := (* network.AccessPoint)(nil)
140
141 // collect target bssids
142 bssids := make([]net.HardwareAddr, 0)
143 for _, addr := range []net.HardwareAddr{dot11.Address1, dot11.Address2, dot11.Address3, dot11.Address4} {
144 if bytes.Equal(addr, network.BroadcastHw) == false {
145 bssids = append(bssids, addr)
146 }
147 }
148
149 // for each AP
150 mod.Session.WiFi.EachAccessPoint(func(mac string, ap *network.AccessPoint) {
151 // only check APs we captured handshakes of
152 if target == nil && ap.HasKeyMaterial() {
153 // search client station
154 ap.EachClient(func(mac string, station *network.Station) {
155 // any valid key material for this station?
156 if station.Handshake.Any() {
157 // check if target
158 for _, a := range bssids {
159 if bytes.Equal(a, station.HW) {
160 target = station
161 targetAP = ap
162 break
163 }
164 }
165 }
166 })
167 }
168 })
169
170 if target != nil {
171 mod.Debug("saving extra %s frame (%d bytes) for %s",
172 dot11.Type.String(),
173 len(packet.Data()),
174 target.String())
175
176 target.Handshake.AddExtra(packet)
177
178 shakesFileName := mod.shakesFile
179 if mod.shakesAggregate == false {
180 shakesFileName = path.Join(shakesFileName, fmt.Sprintf("%s.pcap", targetAP.PathFriendlyName()))
181 }
182 if shakesFileName != "" {
183 mod.Debug("(aggregate %v) saving handshake frames to %s", mod.shakesAggregate, shakesFileName)
184 if err := mod.Session.WiFi.SaveHandshakesTo(shakesFileName, mod.handle.LinkType()); err != nil {
185 mod.Error("error while saving handshake frames to %s: %s", shakesFileName, err)
186 }
187 }
188 }
189 }
190
191 }