-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsybilhunter.go
483 lines (402 loc) · 15.7 KB
/
sybilhunter.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// sybilhunter hunts for sybils in the Tor anonymity network.
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"os/user"
"path"
"path/filepath"
"strings"
"sync"
"time"
tor "git.torproject.org/user/phw/zoossh.git"
)
const (
toolName = "sybilhunter"
version = "2016.01.a"
timeLayout = "2006-01-02_15:04:05"
configFile = ".sybilhunterrc"
longCSVFormat = "long"
wideCSVFormat = "wide"
)
// Files for manual analysis are written to this directory.
var outputDir string
// CmdLineParams stores command line arguments.
type CmdLineParams struct {
Threshold float64
BwFraction float64
Neighbours int
WindowSize int
Uptime bool
Contrib bool
Churn bool
PrintFiles bool
PrintSome bool
Fingerprints bool
Matrix bool
ShowVersion bool
Visualise bool
Cumulative bool
NoFamily bool
DescriptorDir string
ArchiveData string
InputData string
OutputDir string
StartDate time.Time
EndDate time.Time
StartDateStr string
EndDateStr string
ReferenceRelay string
LogFile string
SearchAlg string
CSVFormat string
Filter *tor.ObjectFilter
FilterFpr string
FilterAddr string
FilterNickname string
// Callbacks holds a slice of analysis functions that are called for parsed
// data objects.
Callbacks []AnalysisCallback
}
// AnalysisCallback is a callback function that analyses the given object set.
type AnalysisCallback func(chan tor.ObjectSet, *CmdLineParams, *sync.WaitGroup)
// ParseFlagSet parses the given arguments and returns a CmdLineParams struct.
// If the given CmdLineParams struct is not nil, its content is overwritten
// with the given arguments.
func ParseFlagSet(arguments []string, params *CmdLineParams) *CmdLineParams {
if params == nil {
params = new(CmdLineParams)
params.BwFraction = -1
params.Neighbours = -1
params.WindowSize = 1
params.SearchAlg = "linear"
params.CSVFormat = longCSVFormat
params.Filter = tor.NewObjectFilter()
}
flags := flag.NewFlagSet(toolName, flag.ExitOnError)
flags.Float64Var(¶ms.Threshold, "threshold", params.Threshold, "Analysis-specific threshold.")
flags.Float64Var(¶ms.BwFraction, "bwfraction", params.BwFraction, "Print which relays amount to the given total bandwidth fraction.")
flags.IntVar(¶ms.Neighbours, "neighbours", params.Neighbours, "Find n nearest neighbours.")
flags.IntVar(¶ms.WindowSize, "windowsize", params.WindowSize, "Window size for moving average (default is 1).")
flags.BoolVar(¶ms.Uptime, "uptime", params.Uptime, "Create relay uptime visualisation. Use -input for output file name.")
flags.BoolVar(¶ms.Contrib, "contrib", params.Contrib, "Determine the bandwidth contribution of relays in the given IP address blocks.")
flags.BoolVar(¶ms.Churn, "churn", params.Churn, "Determine churn rate of given set of consensuses. Requires -threshold parameter.")
flags.BoolVar(¶ms.PrintFiles, "print", params.PrintFiles, "Print the content of all files in the given file or directory.")
flags.BoolVar(¶ms.PrintSome, "printsome", params.PrintSome, "Print the content of all files in the given file or directory that contain the given fingerprints. Requires -input parameter.")
flags.BoolVar(¶ms.Fingerprints, "fingerprints", params.Fingerprints, "Analyse relay fingerprints in the given file or directory.")
flags.BoolVar(¶ms.Matrix, "matrix", params.Matrix, "Calculate O(n^2) similarity matrix for all objects in the given file or directory.")
flags.BoolVar(¶ms.ShowVersion, "version", params.ShowVersion, "Show version and exit.")
flags.BoolVar(¶ms.Visualise, "visualise", params.Visualise, "Write DOT code to stdout, that can then be turned into a diagram using Graphviz.")
flags.BoolVar(¶ms.Cumulative, "cumulative", params.Cumulative, "Accumulate all files in a directory rather than process them independently.")
flags.BoolVar(¶ms.NoFamily, "nofamily", params.NoFamily, "Don't interpret MyFamily relationships as Sybils.")
flags.StringVar(¶ms.DescriptorDir, "descdir", params.DescriptorDir, "Path to directory containing router descriptors.")
flags.StringVar(¶ms.ArchiveData, "data", params.ArchiveData, "File or directory to analyse. It must contain network statuses or relay descriptors.")
flags.StringVar(¶ms.InputData, "input", params.InputData, "File or directory to analyse. It must contain network statuses or relay descriptors.")
flags.StringVar(¶ms.OutputDir, "output", params.OutputDir, "Directory where analysis results are written to.")
flags.StringVar(¶ms.ReferenceRelay, "referencerelay", params.ReferenceRelay, "Relay that's used as reference for nearest neighbour search.")
flags.StringVar(¶ms.StartDateStr, "startdate", params.StartDateStr, "Start date for analyzed data in format YYYY-MM-DD.")
flags.StringVar(¶ms.EndDateStr, "enddate", params.EndDateStr, "End date for analyzed data in format YYYY-MM-DD.")
flags.StringVar(¶ms.FilterFpr, "filter-fpr", params.FilterFpr, "Filter router statuses and descriptors by fingerprint. Use ',' as delimiter when multiple fingerprints are given.")
flags.StringVar(¶ms.FilterAddr, "filter-addr", params.FilterAddr, "Filter router statuses and descriptors by IP address. Use ',' as delimiter when multiple addresses are given.")
flags.StringVar(¶ms.FilterNickname, "filter-nickname", params.FilterNickname, "Filter router statuses and descriptors by nickname. Use ',' as delimiter when multiple nicknames are given.")
flags.StringVar(¶ms.LogFile, "logfile", params.LogFile, "Log file to write log messages to.")
flags.StringVar(¶ms.SearchAlg, "search", params.SearchAlg, "Search algorithm to use. Must be 'vptree' or 'linear'. Default is 'linear'.")
flags.StringVar(¶ms.CSVFormat, "csvformat", params.CSVFormat, "Must be either 'long' or 'wide'. Default is 'long'.")
err := flags.Parse(arguments)
if err != nil {
log.Fatalf("Aborting because couldn't parse arguments: %s\n", err)
}
return params
}
// parseDate extracts and returns the date that is in the given date string.
func parseDate(dateString string) time.Time {
date, err := time.Parse("2006-01-02", dateString)
if err != nil {
log.Fatalf("Given date \"%s\" invalid. We expect the format YYYY-MM-DD.\n", dateString)
}
return date
}
// ParseConfig parses the configuration file ~/.sybilhunterrc. Every line in
// the file is interpreted as command line argument.
func ParseConfig() *CmdLineParams {
user, err := user.Current()
if err != nil {
log.Printf("%s. Not reading config file.\n", err)
return nil
}
file, err := os.Open(path.Join(user.HomeDir, configFile))
if err != nil {
log.Printf("%s. Not reading config file.\n", err)
return nil
}
defer file.Close()
log.Println("Attempting to parse configuration file.")
// Turn config file content into format that we can run flag.Parse() on.
scanner := bufio.NewScanner(file)
var arguments []string = make([]string, 0)
for scanner.Scan() {
line := scanner.Text()
words := strings.Split(line, " ")
for _, word := range words {
arguments = append(arguments, word)
}
}
log.Printf("Configuration arguments: %s\n", arguments)
return ParseFlagSet(arguments, nil)
}
// setNonPrimitiveParams turns primitive data types (e.g., the string date
// variables) into their appropriate data type (e.g., time.Time for the date
// variables). This function is just a workaround to deal with the flag
// module's limitations.
func setNonPrimitiveParams(params *CmdLineParams) {
if params.StartDateStr != "" {
params.StartDate = parseDate(params.StartDateStr)
}
if params.EndDateStr != "" {
params.EndDate = parseDate(params.EndDateStr)
} else {
params.EndDate = time.Now()
}
if params.FilterFpr != "" {
fprs := strings.Split(params.FilterFpr, ",")
for _, fpr := range fprs {
params.Filter.AddFingerprint(tor.Fingerprint(fpr))
}
}
if params.FilterAddr != "" {
addrs := strings.Split(params.FilterAddr, ",")
for _, addr := range addrs {
params.Filter.AddIPAddr(net.ParseIP(addr))
}
}
if params.FilterNickname != "" {
nicks := strings.Split(params.FilterNickname, ",")
for _, nick := range nicks {
params.Filter.AddNickname(nick)
}
}
log.Printf("Object filter is empty: %t", params.Filter.IsEmpty())
}
func main() {
var threshold float64
log.Printf("Command line arguments: %s\n", os.Args[1:])
// Read config file first.
params := ParseConfig()
// Let command line arguments overwrite arguments in config file.
params = ParseFlagSet(os.Args[1:], params)
setNonPrimitiveParams(params)
if params.ShowVersion {
_, execName := path.Split(os.Args[0])
fmt.Printf("%s v%s\n", execName, version)
os.Exit(0)
}
if params.LogFile != "" {
fd, err := os.OpenFile(params.LogFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Fatal(err)
}
defer fd.Close()
log.SetOutput(fd)
log.Printf("Using log file %q.\n", params.LogFile)
}
if params.ArchiveData == "" {
log.Fatalln("No file or directory given. Please use the -data switch.")
}
if params.Matrix {
if threshold == 0 {
log.Println("You might want to use -threshold to only consider similarities above or equal to the given threshold.")
}
params.Callbacks = append(params.Callbacks, SimilarityMatrix)
}
if params.Fingerprints {
params.Callbacks = append(params.Callbacks, AnalyseFingerprints)
}
if params.PrintFiles {
params.Callbacks = append(params.Callbacks, PrettyPrint)
}
if params.PrintSome {
if params.InputData == "" {
log.Fatalln("Need a file containing newline-separated relay fingerprints. Use -input switch.")
}
params.Callbacks = append(params.Callbacks, PrintSome)
}
if params.Neighbours != -1 {
if params.Neighbours < 1 {
log.Fatalf("Number of neighbours should be > 0, but %d given.\n", params.Neighbours)
}
if params.ReferenceRelay == "" {
log.Fatalln("No reference relay given. Please use the -referencerelay switch.")
}
params.Callbacks = append(params.Callbacks, FindNearestNeighbours)
}
if params.Churn {
log.Printf("Using '%s' CSV format. Use -csvformat if you don't like that.", params.CSVFormat)
params.Callbacks = append(params.Callbacks, AnalyseChurn)
}
if params.Contrib {
if params.InputData == "" {
log.Fatalln("Need a file containing IP address blocks, one per line. Use -input switch.")
}
params.Callbacks = append(params.Callbacks, BandwidthContribution)
}
if params.Uptime {
if params.InputData == "" {
log.Println("You didn't use -input to specify the file name to write to. Using default.")
params.InputData = "/tmp/uptime-visualisation.jpg"
}
params.Callbacks = append(params.Callbacks, AnalyseUptimes)
}
if params.BwFraction != -1 {
if params.BwFraction < 0 || params.BwFraction > 1 {
log.Fatalf("Bandwidth fraction must be in [0,1], but %.3f was given.\n", params.BwFraction)
}
params.Callbacks = append(params.Callbacks, FindFastRelays)
}
if params.CSVFormat != longCSVFormat && params.CSVFormat != wideCSVFormat {
log.Fatalf("Parameter 'csvformat' must be either '%s' or '%s', but is '%s'.", longCSVFormat, wideCSVFormat, params.CSVFormat)
}
if len(params.Callbacks) == 0 {
log.Fatalln("No command given. Please use -print, -printsome, -fingerprint, -matrix, -neighbours, -bwfraction, or -churn.")
}
if err := ParseFiles(params); err != nil {
log.Fatal(err)
}
}
// fileInRange tries to extract the time that's part of consensus file names,
// e.g., 2015-07-31-15-00-00-consensus. If the time is in the given date
// range, it returns true, otherwise false. By trusting that the file name
// contains a time stamp (and all consensus files from CollecTor do), we can
// discard irrelevant files significantly faster than by parsing their content.
func fileInRange(fileName string, startDate, endDate time.Time) bool {
date, err := time.Parse("2006-01-02-15-04-05-consensus", path.Base(fileName))
if err != nil {
// Parse the file if we are unable to extract the timestamp.
return true
}
return date.After(startDate) && date.Before(endDate)
}
// GatherObjects returns a callback function that gathers data objects from a
// file, directory, or tarball. If the given object set pointer is not nil, it
// is used to accumulate objects. If the given channels are not nil,
// GatherObjects sends the gathered data objects over the channels instead of
// accumulating them.
func GatherObjects(objs *tor.ObjectSet, channels []chan tor.ObjectSet, params *CmdLineParams) func(string, os.FileInfo, io.Reader) error {
return func(path string, info os.FileInfo, r io.Reader) error {
if info.IsDir() {
return nil
}
if !fileInRange(path, params.StartDate, params.EndDate) {
log.Printf("File %s not in desired date range.\n", path)
return nil
}
objects, err := tor.ParseUnknown(r)
if err != nil {
log.Println(err)
return nil
}
if channels != nil {
// Processing independently.
for _, channel := range channels {
if objects != nil {
channel <- objects
}
}
} else {
// Processing cumulatively.
if *objs == nil {
*objs = objects
} else {
(*objs).Merge(objects)
}
}
return nil
}
}
// Walk over the entries of path, open each one, and pass it to callback.
func walkPath(path string, callback func(string, os.FileInfo, io.Reader) error) error {
// callback expects an already opened file.
walkFn := func(path string, info os.FileInfo, err error) error {
fd, err := os.Open(path)
if err != nil {
return err
}
defer fd.Close()
return callback(path, info, fd)
}
return filepath.Walk(path, walkFn)
}
// Walk over the entries of the named tar.xz file and pass them to callback.
//
// Unlike filepath.Walk, this function does not visit directory entries in
// lexicographic order, rather the order they appear in the tar file.
func walkTarXZFile(path string, callback func(string, os.FileInfo, io.Reader) error) error {
fd, t, err := openTarXZFile(path)
if err != nil {
return err
}
defer fd.Close()
for {
header, err := t.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
err = callback(header.Name, header.FileInfo(), t)
if err != nil {
return err
}
}
return nil
}
// Walk over the entries of the given path. The treatment depends on the given
// path: if it ends in ".tar.xz" it is passed to walkTarXZFile; otherwise it is
// passed to walkPath.
func walkArchiveData(path string, callback func(string, os.FileInfo, io.Reader) error) error {
if strings.HasSuffix(path, ".tar.xz") {
return walkTarXZFile(path, callback)
} else {
return walkPath(path, callback)
}
}
// ParseFiles parses the given directory or files and passes the parsed data to
// the given analysis functions. ParseFiles then waits for all these functions
// to finish processing.
func ParseFiles(params *CmdLineParams) error {
var objs tor.ObjectSet
var channels []chan tor.ObjectSet
var group sync.WaitGroup
group.Add(len(params.Callbacks))
// Create a channel for and invoke all callback functions.
for _, analysisFunc := range params.Callbacks {
channel := make(chan tor.ObjectSet)
channels = append(channels, channel)
go analysisFunc(channel, params, &group)
}
if params.Cumulative {
log.Printf("Processing \"%s\" cumulatively.\n", params.ArchiveData)
walkArchiveData(params.ArchiveData, GatherObjects(&objs, nil, params))
if objs == nil {
return errors.New("Gathered object set empty. Are we parsing the right files?")
}
// Send accumulated object set to all callback functions.
for _, channel := range channels {
channel <- objs
}
} else {
log.Printf("Processing \"%s\" independently.\n", params.ArchiveData)
walkArchiveData(params.ArchiveData, GatherObjects(nil, channels, params))
}
// Close processing channels and wait for goroutines to finish.
for _, channel := range channels {
close(channel)
}
group.Wait()
return nil
}