-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
315 lines (262 loc) · 9.36 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/akamensky/argparse"
"golang.org/x/net/websocket"
)
// DebugData is JSON structure returned by Chromium
type DebugData struct {
Description string `json:"description"`
DevtoolsFrontendURL string `json:"devtoolsFrontendUrl"`
FaviconURL string `json:"faviconUrl"`
ID string `json:"id"`
Title string `json:"title"`
PageType string `json:"type"`
URL string `json:"url"`
WebSocketDebuggerURL string `json:"webSocketDebuggerUrl"`
}
// WebsocketResponseRoot is the raw response from Chromium websocket
type WebsocketResponseRoot struct {
ID int `json:"id"`
Result WebsocketResponseNested `json:"result"`
}
// WebsocketResponseNested is the object within the the raw response from Chromium websocket
type WebsocketResponseNested struct {
Cookies []Cookie `json:"cookies"`
}
// Cookie is JSON structure returned by Chromium websocket
type Cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Domain string `json:"domain"`
Path string `json:"path"`
Expires float64 `json:"expires"`
Size int `json:"size"`
HTTPOnly bool `json:"httpOnly"`
Secure bool `json:"secure"`
Session bool `json:"session"`
SameSite string `json:"sameSite"`
Priority string `json:"priority"`
}
// LightCookie is a JSON structure for the cookie with only the name, value, domain, path, and (modified) expires fields
type LightCookie struct {
Name string `json:"name"`
Value string `json:"value"`
Domain string `json:"domain"`
Path string `json:"path"`
Expires float64 `json:"expirationDate"`
}
// GetDebugData interacts with Chromium debug port to obtain the JSON response of open tabs/installed extensions
func GetDebugData(debugPort string) []DebugData {
// Create debugURL from user input
var debugURL = "http://localhost:" + debugPort + "/json"
// Make GET request
resp, err := http.Get(debugURL)
if err != nil {
log.Fatalln(err)
}
// Read GET response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
// Unmarshal JSON response
var debugList []DebugData
err = json.Unmarshal(body, &debugList)
if err != nil {
log.Fatalln(err)
}
return debugList
}
// PrintDebugData takes the JSON response from Chromium and prints open tabs and installed extensions
func PrintDebugData(debugList []DebugData, grep string) {
// Check length of grep to see if filtering was requested
var grepFlag = false
if len(grep) > 0 {
grepFlag = true
}
for _, value := range debugList {
if grepFlag {
if strings.Contains(value.Title, grep) || strings.Contains(value.URL, grep) {
fmt.Printf("Title: %s\n", value.Title)
fmt.Printf("Type: %s\n", value.PageType)
fmt.Printf("URL: %s\n", value.URL)
fmt.Printf("WebSocket Debugger URL: %s\n\n", value.WebSocketDebuggerURL)
}
} else {
fmt.Printf("Title: %s\n", value.Title)
fmt.Printf("Type: %s\n", value.PageType)
fmt.Printf("URL: %s\n", value.URL)
fmt.Printf("WebSocket Debugger URL: %s\n\n", value.WebSocketDebuggerURL)
}
}
}
// DumpCookies interacts with the webSocketDebuggerUrl to obtain Chromium cookies
func DumpCookies(debugList []DebugData, format string, grep string) {
// Check length of grep to see if filtering was requested
var grepFlag = false
if len(grep) > 0 {
grepFlag = true
}
// Obtain WebSocketDebuggerURL from DebugData list
var websocketURL = debugList[0].WebSocketDebuggerURL
// Connect to websocket
ws, err := websocket.Dial(websocketURL, "", "http://localhost/")
if err != nil {
log.Fatal(err)
}
// Send message to websocket
var message = "{\"id\": 1, \"method\":\"Network.getAllCookies\"}"
websocket.Message.Send(ws, message)
// Get cookies from websocket
var rawResponse []byte
websocket.Message.Receive(ws, &rawResponse)
// Unmarshal JSON response
var websocketResponseRoot WebsocketResponseRoot
err = json.Unmarshal(rawResponse, &websocketResponseRoot)
if err != nil {
log.Fatalln(err)
}
// Print cookies in raw format as returned by Chromium websocket
if format == "raw" {
fmt.Printf("%s\n", rawResponse)
os.Exit(0)
}
// Print cookies in JSON format with modified expiration date
// Additionally, only include name, value, domain, path, and expirationDate
if format == "modified" {
lightCookieList := []LightCookie{}
for _, value := range websocketResponseRoot.Result.Cookies {
if grepFlag {
if strings.Contains(value.Name, grep) || strings.Contains(value.Domain, grep) {
// Turns Cookie into LightCookie with modified expires field
var lightCookie LightCookie
lightCookie.Name = value.Name
lightCookie.Value = value.Value
lightCookie.Domain = value.Domain
lightCookie.Path = value.Path
lightCookie.Expires = (float64)(time.Now().Unix() + (10 * 365 * 24 * 60 * 60))
lightCookieList = append(lightCookieList, lightCookie)
}
} else {
// Turns Cookie into LightCookie with modified expires field
var lightCookie LightCookie
lightCookie.Name = value.Name
lightCookie.Value = value.Value
lightCookie.Domain = value.Domain
lightCookie.Path = value.Path
lightCookie.Expires = (float64)(time.Now().Unix() + (10 * 365 * 24 * 60 * 60))
lightCookieList = append(lightCookieList, lightCookie)
}
}
lightCookieJSON, err := json.Marshal(lightCookieList)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s\n", lightCookieJSON)
os.Exit(0)
}
// Default to printing cookies in human format
for _, value := range websocketResponseRoot.Result.Cookies {
if grepFlag {
if strings.Contains(value.Name, grep) || strings.Contains(value.Domain, grep) {
fmt.Printf("name: %s\n", value.Name)
fmt.Printf("value: %s\n", value.Value)
fmt.Printf("domain: %s\n", value.Domain)
fmt.Printf("path: %s\n", value.Path)
fmt.Printf("expires: %f\n", value.Expires)
fmt.Printf("size: %d\n", value.Size)
fmt.Printf("httpOnly: %t\n", value.HTTPOnly)
fmt.Printf("secure: %t\n", value.Secure)
fmt.Printf("session: %t\n", value.Session)
fmt.Printf("sameSite: %s\n", value.SameSite)
fmt.Printf("priority: %s\n\n", value.Priority)
}
} else {
fmt.Printf("name: %s\n", value.Name)
fmt.Printf("value: %s\n", value.Value)
fmt.Printf("domain: %s\n", value.Domain)
fmt.Printf("path: %s\n", value.Path)
fmt.Printf("expires: %f\n", value.Expires)
fmt.Printf("size: %d\n", value.Size)
fmt.Printf("httpOnly: %t\n", value.HTTPOnly)
fmt.Printf("secure: %t\n", value.Secure)
fmt.Printf("session: %t\n", value.Session)
fmt.Printf("sameSite: %s\n", value.SameSite)
fmt.Printf("priority: %s\n\n", value.Priority)
}
}
}
func ClearCookies(debugList []DebugData){
var websocketURL = debugList[0].WebSocketDebuggerURL
// Connect to websocket
ws, err := websocket.Dial(websocketURL, "", "http://localhost/")
if err != nil {
log.Fatal(err)
}
// Send message to websocket
var message = "{\"id\": 1, \"method\": \"Network.clearBrowserCookies\"}"
websocket.Message.Send(ws, message)
}
func LoadCookies(debugList []DebugData, load string){
// Read cookies
content, err := ioutil.ReadFile(load)
if err != nil {
log.Fatal(err)
}
var websocketURL = debugList[0].WebSocketDebuggerURL
// Connect to websocket
ws, err := websocket.Dial(websocketURL, "", "http://localhost/")
if err != nil {
log.Fatal(err)
}
// Send message to websocket
var message = fmt.Sprintf("{\"id\": 1, \"method\":\"Network.setCookies\", \"params\":{\"cookies\":%s}}", content)
websocket.Message.Send(ws, message)
}
func main() {
// Create new parser object
parser := argparse.NewParser("WhiteChocolateMacademia", "Interact with Chromium-based browsers' debug port to view open tabs, installed extensions, and cookies (https://github.com/slyd0g/WhiteChocolateMacademiaNut)")
// Create arguments
var debugPort *string = parser.String("p", "port", &argparse.Options{Required: true, Help: "{REQUIRED} - Debug port"})
var dump *string = parser.String("d", "dump", &argparse.Options{Required: false, Help: "{ pages || cookies } - Dump open tabs/extensions or cookies"})
var format *string = parser.String("f", "format", &argparse.Options{Required: false, Help: "{ raw || human || modified } - Format when dumping cookies"})
var grep *string = parser.String("g", "grep", &argparse.Options{Required: false, Help: "Narrow scope of dumping to specific name/domain"})
var load *string = parser.String("l", "load", &argparse.Options{Required: false, Help: "File name for cookies to load into browser"})
var clear *string = parser.String("c", "clear", &argparse.Options{Required: false, Help: "Clear cookies before loading new cookies"})
// Parse arguments
err := parser.Parse(os.Args)
if err != nil {
// In case of error print error and print usage
// This can also be done by passing -h or --help flags
fmt.Printf("%s\n", parser.Usage(err))
os.Exit(1)
}
if *dump != "" {
// Enumerate open tabs and installed extensions
if *dump == "pages" {
debugList := GetDebugData(*debugPort)
PrintDebugData(debugList, *grep)
}
// Dump cookies
if *dump == "cookies" {
debugList := GetDebugData(*debugPort)
DumpCookies(debugList, *format, *grep)
}
}
if *clear != "" {
debugList := GetDebugData(*debugPort)
ClearCookies(debugList)
}
if *load != "" {
debugList := GetDebugData(*debugPort)
LoadCookies(debugList, *load)
}
}