-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
127 lines (106 loc) · 2.92 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/c2h5oh/datasize"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/vlad-s/hcpxread/helpers"
"github.com/vlad-s/hcpxread/menu"
"github.com/vlad-s/hcpxread/structs"
)
var (
capture = flag.String("capture", "", "The HCCAPX `file` to read")
debug = flag.Bool("debug", false, "Show additional, debugging info")
)
const BANNER = ` _ _
| |__ ___ _ ____ ___ __ ___ __ _ __| |
| '_ \ / __| '_ \ \/ / '__/ _ \/ _` + "` |/ _`" + ` |
| | | | (__| |_) > <| | | __/ (_| | (_| |
|_| |_|\___| .__/_/\_\_| \___|\__,_|\__,_|
|_|
`
var (
log = helpers.Logger
Instances structs.HccapxInstances
)
func init() {
flag.Parse()
log.SetLevel(logrus.DebugLevel)
if *capture == "" {
fmt.Println(BANNER)
flag.Usage()
os.Exit(1)
}
helpers.SetDebugging(*debug)
}
func main() {
helpers.ClearScreen()
stat, err := os.Stat(*capture)
if err != nil {
log.WithError(err).Fatal("Error stating the file")
}
content, err := ioutil.ReadFile(*capture)
if err != nil {
log.WithError(err).Fatal("Error reading the file")
}
if len(content) < 393 {
if helpers.Debug() {
log.WithField("size", len(content)).Debug("File too small")
}
log.WithField("bytes", len(content)).Fatal("File too small for a single HCPX structure")
}
fileSize := datasize.ByteSize(stat.Size()).HumanReadable()
log.WithFields(logrus.Fields{"name": stat.Name(), "size": fileSize}).Info("Opened file for reading")
fileHeader := content[0:4]
correctHeader := bytes.Equal(fileHeader, structs.HcpxHeader)
if !correctHeader {
log.WithField("header", string(fileHeader)).Fatal("Wrong file header")
}
log.Info("Searching for HCPX headers...")
indexes := helpers.SearchHeaders(content)
log.WithField("indexes", len(indexes)).Info("Finished searching for headers")
for _, i := range indexes {
j := i + 393
h := helpers.ParseHccapx(content[i:j])
Instances = append(Instances, h)
}
log.Infof("Summary: %d networks, %d WPA/%d WPA2, %d unique APs",
len(Instances), Instances.WPANum(), Instances.WPA2Num(), Instances.UniqueAPs())
var choice int
reader := bufio.NewReader(os.Stdin)
for {
helpers.PrintInstances(Instances)
helpers.PrintCommands()
fmt.Printf("\nchoice > ")
usertext, err := reader.ReadString('\n')
if err != nil {
helpers.ClearScreen()
log.Error(errors.Wrap(err, "Error input"))
continue
}
usertext = strings.Replace(usertext, "\n", "", -1)
usertext = strings.Replace(usertext, "\r", "", -1)
choice, err = strconv.Atoi(usertext)
if err != nil {
helpers.ClearScreen()
log.Error(errors.Wrap(err, "Error converting to number"))
continue
}
exported, err := menu.ParseChoice(choice, Instances)
if err != nil {
helpers.ClearScreen()
log.Error(err)
}
if exported {
helpers.ClearScreen()
log.Info("File successfully exported")
}
}
}