forked from schollz/find3-cli-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
399 lines (366 loc) · 8.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"os/user"
"runtime"
"strconv"
"strings"
"time"
log "github.com/cihub/seelog"
"github.com/montanaflynn/stats"
"github.com/schollz/find3/server/main/src/models"
"github.com/urfave/cli"
)
var (
wifiInterface string
version string
commit string
date string
server string
family, device, location string
scanSeconds int
minimumThreshold int
doBluetooth bool
doWifi bool
doReverse bool
doDebug bool
doGPS bool
doSetPromiscuous bool
doNotModifyPromiscuity bool
doIgnoreRandomizedMacs bool
doAllPackets bool
runForever bool
allFrequencies bool
allFrequenciesDelay int
currentChannel string
)
func main() {
defer log.Flush()
app := cli.NewApp()
app.Name = "find3-cli-scanner"
app.Version = version
app.Usage = "this command line scanner works with FIND3\n\t\tto capture bluetooth and WiFi signals from devices"
app.Authors = []cli.Author{
{
Name: "Zack Scholl",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "bluetooth",
Usage: "scan bluetooth",
},
cli.BoolFlag{
Name: "wifi",
Usage: "scan wifi",
},
cli.StringFlag{
Name: "server",
Value: "https://cloud.internalpositioning.com",
Usage: "FIND3 server for submitting fingerprints",
},
cli.StringFlag{
Name: "interface,i",
Value: "wlan0",
Usage: "wifi interface for scanning",
},
cli.StringFlag{
Name: "family,f",
Value: "",
Usage: "family name",
},
cli.StringFlag{
Name: "device,d",
Value: "",
Usage: "device name",
},
cli.StringFlag{
Name: "location,l",
Value: "",
Usage: "location name (automatically toggles learning)",
},
cli.BoolFlag{
Name: "gps",
Usage: "enable gps collection (using wifi)",
},
cli.BoolFlag{
Name: "passive",
Usage: "enable passive scanning",
},
cli.BoolFlag{
Name: "debug",
Usage: "enable debug mode",
},
cli.BoolFlag{
Name: "monitor-mode",
Usage: "enable monitor mode (turn promiscuous mode on)",
},
cli.BoolFlag{
Name: "disable-monitor-mode",
Usage: "disable monitor mode (turn promiscuous mode off)",
},
cli.BoolFlag{
Name: "no-modify",
Usage: "disable changing wifi promiscuity mode",
},
cli.BoolFlag{
Name: "no-randomized-macs",
Usage: "ignore randomized MAC addresses",
},
cli.BoolFlag{
Name: "all-frequencies",
Usage: "scan all frequencies",
},
cli.IntFlag{
Name: "all-frequencies-delay",
Value: 1000,
Usage: "scan all frequencies with specified delay in milliseconds",
},
cli.BoolFlag{
Name: "all-packets",
Usage: "process all packets (not only broadcast)",
},
cli.BoolFlag{
Name: "forever",
Usage: "run until Ctl+C signal",
},
cli.IntFlag{
Name: "min-rssi",
Value: -100,
Usage: "minimum RSSI to use",
},
cli.IntFlag{
Name: "scantime,s",
Value: 40,
Usage: "number of seconds to scan",
},
}
app.Action = func(c *cli.Context) (err error) {
// set variables
server = c.GlobalString("server")
family = strings.ToLower(c.GlobalString("family"))
device = c.GlobalString("device")
wifiInterface = c.GlobalString("interface")
location = c.GlobalString("location")
doBluetooth = c.GlobalBool("bluetooth")
doWifi = c.GlobalBool("wifi")
doReverse = c.GlobalBool("passive")
doDebug = c.GlobalBool("debug")
doGPS = c.GlobalBool("gps")
doSetPromiscuous = c.GlobalBool("monitor-mode")
doNotModifyPromiscuity = c.GlobalBool("no-modify")
doIgnoreRandomizedMacs = c.GlobalBool("no-randomized-macs")
doAllPackets = c.GlobalBool("all-packets")
runForever = c.GlobalBool("forever")
scanSeconds = c.GlobalInt("scantime")
minimumThreshold = c.GlobalInt("min-rssi")
allFrequencies = c.GlobalBool("all-frequencies")
allFrequenciesDelay = c.GlobalInt("all-frequencies-delay")
if doDebug {
setLogLevel("debug")
} else {
setLogLevel("info")
}
// make sure is sudo
if runtime.GOOS == "linux" && os.Getenv("SUDO_USER") == "" {
user, usererr := user.Current()
if usererr == nil && user.Name != "root" {
err = errors.New("need to run with sudo")
return
}
}
// ensure backwards compatibility
if !doBluetooth && !doWifi {
doWifi = true
}
if doSetPromiscuous {
PromiscuousMode(true)
return
}
if device == "" {
return errors.New("device cannot be blank (set with -d)")
} else if family == "" {
return errors.New("family cannot be blank (set with -f)")
}
if allFrequencies {
go HopChannels(time.Duration(allFrequenciesDelay) * time.Millisecond)
}
for {
if doWifi {
log.Infof("scanning with %s", wifiInterface)
}
if doBluetooth {
log.Infof("scanning bluetooth")
}
if doBluetooth || doReverse {
log.Infof("scanning for %d seconds", scanSeconds)
}
if !doReverse {
err = basicCapture()
} else {
log.Info("working in passive mode")
err = reverseCapture()
}
if !runForever {
break
} else if err != nil {
log.Warn(err)
}
}
return
}
err := app.Run(os.Args)
if err != nil {
log.Error(err)
}
}
func reverseCapture() (err error) {
c := make(chan map[string]map[string]interface{})
if doBluetooth {
go scanBluetooth(c)
}
payload := models.SensorData{}
payload.Family = family
payload.Device = device
payload.Timestamp = time.Now().UnixNano() / int64(time.Millisecond)
payload.Sensors = make(map[string]map[string]interface{})
if doWifi {
if !doNotModifyPromiscuity {
PromiscuousMode(true)
time.Sleep(1 * time.Second)
}
payload, err = ReverseScan(time.Duration(scanSeconds) * time.Second)
if err != nil {
return
}
if !doNotModifyPromiscuity {
PromiscuousMode(false)
time.Sleep(1 * time.Second)
}
}
if doBluetooth {
data := <-c
log.Debugf("bluetooth data:%+v", data)
for sensor := range data {
payload.Sensors[sensor] = make(map[string]interface{})
for device := range data[sensor] {
payload.Sensors[sensor][device] = data[sensor][device]
}
}
}
bSensors, _ := json.MarshalIndent(payload, "", " ")
log.Debug(string(bSensors))
err = postData(payload, "/passive")
return
}
func basicCapture() (err error) {
payload := models.SensorData{}
payload.Timestamp = time.Now().UnixNano() / int64(time.Millisecond)
payload.Family = family
payload.Device = device
payload.Location = location
payload.Sensors = make(map[string]map[string]interface{})
// collect sensors asynchronously
c := make(chan map[string]map[string]interface{})
numSensors := 0
if doWifi {
go scanWifi(c)
numSensors++
}
if doBluetooth {
go scanBluetooth(c)
numSensors++
}
for i := 0; i < numSensors; i++ {
data := <-c
for sensor := range data {
payload.Sensors[sensor] = make(map[string]interface{})
for device := range data[sensor] {
payload.Sensors[sensor][device] = data[sensor][device]
}
}
}
if len(payload.Sensors) == 0 {
err = errors.New("collected no data")
return
}
if _, ok := payload.Sensors["wifi"]; ok && doGPS {
acquired := 0.0
for device := range payload.Sensors["wifi"] {
lat, lon := func() (lat, lon float64) {
type MacData struct {
Ready bool `json:"ready"`
MacAddress string `json:"mac"`
Exists bool `json:"exists"`
Latitude float64 `json:"lat,omitempty"`
Longitude float64 `json:"lon,omitempty"`
Error string `json:"err,omitempty"`
}
var md MacData
resp, err := http.Get("https://mac2gps.schollz.com/" + device)
if err != nil {
return
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&md)
if err != nil {
return
}
lat = md.Latitude
lon = md.Longitude
if md.Ready && md.Exists {
log.Debugf("found GPS: %+v", md)
}
return
}()
if lat != 0 {
acquired++
}
payload.GPS.Latitude += lat
payload.GPS.Longitude += lon
}
if acquired > 0 {
payload.GPS.Latitude = payload.GPS.Latitude / acquired
payload.GPS.Longitude = payload.GPS.Longitude / acquired
}
}
bPayload, err := json.MarshalIndent(payload, "", " ")
if err != nil {
return
}
log.Debug(string(bPayload))
err = postData(payload, "/data")
return
}
// this doesn't work, just playing
func bluetoothTimeOfFlight() {
t := time.Now()
s, _ := RunCommand(60*time.Second, "l2ping -c 300 -f 0C:3E:9F:28:22:6A")
milliseconds := make([]float64, 300)
i := 0
for _, line := range strings.Split(s, "\n") {
if !strings.Contains(line, "ms") {
continue
}
lineSplit := strings.Fields(line)
msString := strings.TrimRight(lineSplit[len(lineSplit)-1], "ms")
ms, err := strconv.ParseFloat(msString, 64)
if err != nil {
log.Error(err)
}
milliseconds[i] = ms
i++
}
milliseconds = milliseconds[:i]
median, err := stats.Median(milliseconds)
if err != nil {
log.Error(err)
}
fmt.Println(median)
fmt.Println(time.Since(t) / 300)
}