-
Notifications
You must be signed in to change notification settings - Fork 100
/
main.go
executable file
·364 lines (296 loc) · 9.47 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
package main
import (
"bufio"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"io"
"log"
"net/http"
"net/textproto"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
)
// sourceMap represents a sourceMap. We only really care about the sources and
// sourcesContent arrays.
type sourceMap struct {
Version int `json:"version"`
Sources []string `json:"sources"`
SourcesContent []string `json:"sourcesContent"`
}
// command line args
type config struct {
outdir string // output directory
url string // sourcemap url
jsurl string // javascript url
proxy string // upstream proxy server
insecure bool // skip tls verification
headers headerList // additional user-supplied http headers
}
type headerList []string
func (i *headerList) String() string {
return ""
}
func (i *headerList) Set(value string) error {
*i = append(*i, value)
return nil
}
// getSourceMap retrieves a sourcemap from a URL or a local file and returns
// its sourceMap.
func getSourceMap(source string, headers []string, insecureTLS bool, proxyURL url.URL) (m sourceMap, err error) {
var body []byte
var client http.Client
log.Printf("[+] Retrieving Sourcemap from %.1024s...\n", source)
u, err := url.ParseRequestURI(source)
if err != nil {
// If it's a file, read it.
body, err = os.ReadFile(source)
if err != nil {
log.Fatalln(err)
}
} else {
if u.Scheme == "http" || u.Scheme == "https" {
// If it's a URL, get it.
req, err := http.NewRequest("GET", u.String(), nil)
tr := &http.Transport{}
if err != nil {
log.Fatalln(err)
}
if insecureTLS {
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
if proxyURL != (url.URL{}) {
tr.Proxy = http.ProxyURL(&proxyURL)
}
client = http.Client{
Transport: tr,
}
if len(headers) > 0 {
headerString := strings.Join(headers, "\r\n") + "\r\n\r\n" // squish all the headers together with CRLFs
log.Printf("[+] Setting the following headers: \n%s", headerString)
r := bufio.NewReader(strings.NewReader(headerString))
tpReader := textproto.NewReader(r)
mimeHeader, err := tpReader.ReadMIMEHeader()
if err != nil {
log.Fatalln(err)
}
req.Header = http.Header(mimeHeader)
}
res, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
body, err = io.ReadAll(res.Body)
defer res.Body.Close()
if res.StatusCode != 200 && len(body) > 0 {
log.Printf("[!] WARNING - non-200 status code: %d - Confirm this URL contains valid source map manually!", res.StatusCode)
log.Printf("[!] WARNING - sourceMap URL request return != 200 - however, body length > 0 so continuing... ")
}
if err != nil {
log.Fatalln(err)
}
} else if u.Scheme == "data" {
urlchunks := strings.Split(u.Opaque, ",")
if len(urlchunks) < 2 {
log.Fatalf("[!] Could not parse data URI - expected atleast 2 chunks but got %d\n", len(urlchunks))
}
data, err := base64.StdEncoding.DecodeString(urlchunks[1])
if err != nil {
log.Fatal("[!] Error base64 decoding", err)
}
body = []byte(data)
} else {
// If it's a file, read it.
body, err = os.ReadFile(source)
if err != nil {
log.Fatalln(err)
}
}
}
// Unmarshall the body into the struct.
log.Printf("[+] Read %d bytes, parsing JSON.\n", len(body))
err = json.Unmarshal(body, &m)
if err != nil {
log.Printf("[!] Error parsing JSON - confirm %s is a valid JS sourcemap", source)
}
return
}
// getSourceMapFromJS queries a JavaScript URL, parses its headers and content and looks for sourcemaps
// follows the rules outlined in https://tc39.es/source-map-spec/#linking-generated-code
func getSourceMapFromJS(jsurl string, headers []string, insecureTLS bool, proxyURL url.URL) (m sourceMap, err error) {
var client http.Client
log.Printf("[+] Retrieving JavaScript from URL: %s.\n", jsurl)
// perform the request
u, err := url.ParseRequestURI(jsurl)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest("GET", u.String(), nil)
tr := &http.Transport{}
if err != nil {
log.Fatalln(err)
}
if insecureTLS {
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
if proxyURL != (url.URL{}) {
tr.Proxy = http.ProxyURL(&proxyURL)
}
client = http.Client{
Transport: tr,
}
if len(headers) > 0 {
headerString := strings.Join(headers, "\r\n") + "\r\n\r\n" // squish all the headers together with CRLFs
log.Printf("[+] Setting the following headers: \n%s", headerString)
r := bufio.NewReader(strings.NewReader(headerString))
tpReader := textproto.NewReader(r)
mimeHeader, err := tpReader.ReadMIMEHeader()
if err != nil {
log.Fatalln(err)
}
req.Header = http.Header(mimeHeader)
}
res, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
if res.StatusCode != 200 {
log.Fatalf("[!] non-200 status code: %d", res.StatusCode)
}
var sourceMap string
// check for SourceMap and X-SourceMap (deprecated) headers
if sourceMap = res.Header.Get("SourceMap"); sourceMap == "" {
sourceMap = res.Header.Get("X-SourceMap")
}
if sourceMap != "" {
log.Printf("[.] Found SourceMap URI in response headers: %.1024s...", sourceMap)
} else {
// parse the javascript
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
// JS file can have multiple source maps in it, but only the last line is valid https://sourcemaps.info/spec.html#h.lmz475t4mvbx
re := regexp.MustCompile(`\/\/[@#] sourceMappingURL=(.*)`)
match := re.FindAllSubmatch(body, -1)
if len(match) != 0 {
// only the sourcemap at the end of the file should be valid
sourceMap = string(match[len(match)-1][1])
log.Printf("[.] Found SourceMap in JavaScript body: %.1024s...", sourceMap)
}
}
// this introduces a forced request bug if the JS file we're parsing is
// malicious and forces us to make a request out to something dodgy - take care
if sourceMap != "" {
var sourceMapURL *url.URL
// handle absolute/relative rules
sourceMapURL, err = url.ParseRequestURI(sourceMap)
if err != nil {
// relative url...
sourceMapURL, err = u.Parse(sourceMap)
if err != nil {
log.Fatal(err)
}
}
return getSourceMap(sourceMapURL.String(), headers, insecureTLS, proxyURL)
}
err = errors.New("[!] No sourcemap URL found")
return
}
// writeFile writes content to file at path p.
func writeFile(p string, content string) error {
p = filepath.Clean(p)
if _, err := os.Stat(filepath.Dir(p)); os.IsNotExist(err) {
// Using MkdirAll here is tricky, because even if we fail, we might have
// created some of the parent directories.
err = os.MkdirAll(filepath.Dir(p), 0700)
if err != nil {
return err
}
}
log.Printf("[+] Writing %d bytes to %s.\n", len(content), p)
return os.WriteFile(p, []byte(content), 0600)
}
// cleanWindows replaces the illegal characters from a path with `-`.
func cleanWindows(p string) string {
m1 := regexp.MustCompile(`[?%*|:"<>]`)
return m1.ReplaceAllString(p, "")
}
func main() {
var proxyURL url.URL
var conf config
var err error
flag.StringVar(&conf.outdir, "output", "", "Source file output directory - REQUIRED")
flag.StringVar(&conf.url, "url", "", "URL or path to the Sourcemap file - cannot be used with jsurl")
flag.StringVar(&conf.jsurl, "jsurl", "", "URL to JavaScript file - cannot be used with url")
flag.StringVar(&conf.proxy, "proxy", "", "Proxy URL")
help := flag.Bool("help", false, "Show help")
flag.BoolVar(&conf.insecure, "insecure", false, "Ignore invalid TLS certificates")
flag.Var(&conf.headers, "header", "A header to send with the request, similar to curl's -H. Can be set multiple times, EG: \"./sourcemapper --header \"Cookie: session=bar\" --header \"Authorization: blerp\"")
flag.Parse()
if *help || (conf.url == "" && conf.jsurl == "") || conf.outdir == "" {
flag.Usage()
return
}
if conf.jsurl != "" && conf.url != "" {
log.Println("[!] Both -jsurl and -url supplied")
flag.Usage()
return
}
if conf.proxy != "" {
p, err := url.Parse(conf.proxy)
if err != nil {
log.Fatal(err)
}
proxyURL = *p
}
var sm sourceMap
// these need to just take the conf object
if conf.url != "" {
if sm, err = getSourceMap(conf.url, conf.headers, conf.insecure, proxyURL); err != nil {
log.Fatal(err)
}
} else if conf.jsurl != "" {
if sm, err = getSourceMapFromJS(conf.jsurl, conf.headers, conf.insecure, proxyURL); err != nil {
log.Fatal(err)
}
}
// everything below needs to go into its own function
log.Printf("[+] Retrieved Sourcemap with version %d, containing %d entries.\n", sm.Version, len(sm.Sources))
if len(sm.Sources) == 0 {
log.Fatal("No sources found.")
}
if len(sm.SourcesContent) == 0 {
log.Fatal("No source content found.")
}
if sm.Version != 3 {
log.Println("[!] Sourcemap is not version 3. This is untested!")
}
if _, err := os.Stat(conf.outdir); os.IsNotExist(err) {
err = os.Mkdir(conf.outdir, 0700)
if err != nil {
log.Fatal(err)
}
}
for i, sourcePath := range sm.Sources {
sourcePath = "/" + sourcePath // path.Clean will ignore a leading '..', must be a '/..'
// If on windows, clean the sourcepath.
if runtime.GOOS == "windows" {
sourcePath = cleanWindows(sourcePath)
}
// Use filepath.Join. https://parsiya.net/blog/2019-03-09-path.join-considered-harmful/
scriptPath, scriptData := filepath.Join(conf.outdir, filepath.Clean(sourcePath)), sm.SourcesContent[i]
err := writeFile(scriptPath, scriptData)
if err != nil {
log.Printf("Error writing %s file: %s", scriptPath, err)
}
}
log.Println("[+] Done")
}