-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsSigScraper.go
216 lines (180 loc) · 4.74 KB
/
JsSigScraper.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
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/BurntSushi/toml"
"golang.org/x/net/html"
)
type JavaScriptSignature struct {
Name string
Regex string
}
type Configuration struct {
Signatures []JavaScriptSignature
}
var (
defaultUserAgent = "Mozilla/5.0"
)
func main() {
urlPtr := flag.String("url", "", "URL to scan")
configFilePtr := flag.String("config", "", "Path to configuration file")
keepJS := flag.Bool("keepjs", false, "Keep JavaScript files")
userAgent := flag.String("useragent", defaultUserAgent, "User agent to use")
flag.Parse()
if *urlPtr == "" || *configFilePtr == "" {
fmt.Println("Usage: go run JsSigScraper.go -url <URL> -config <config-file> [-keepjs] [-useragent <user-agent>]")
return
}
config, err := readConfig(*configFilePtr)
if err != nil {
log.Fatalf("Error reading configuration: %v", err)
}
htmlContent, err := fetchHTML(*urlPtr, *userAgent)
if err != nil {
log.Fatalf("Error fetching HTML: %v", err)
}
javascriptURLs, err := extractJSScriptURLs(htmlContent, *urlPtr)
if err != nil {
log.Fatalf("Error extracting JavaScript URLs: %v", err)
}
var wg sync.WaitGroup
// Download JavaScript files and check for JavaScript signatures
for _, jsURL := range javascriptURLs {
wg.Add(1)
go func(jsURL string) {
defer wg.Done()
content, err := fetchJavascript(jsURL, *userAgent)
if err != nil {
log.Printf("Error downloading JavaScript from %s: %v\n", jsURL, err)
return
}
for _, signature := range config.Signatures {
if matched, _ := regexp.MatchString(signature.Regex, content); matched {
fmt.Println(signature.Name)
break
}
}
// Save the JavaScript content to a file if -keepjs is specified
if *keepJS {
if err := saveJavascript(jsURL, content); err != nil {
log.Printf("Error saving JavaScript from %s: %v\n", jsURL, err)
}
}
}(jsURL)
}
// Wait for all goroutines to finish
wg.Wait()
}
func readConfig(configFile string) (Configuration, error) {
var config Configuration
if _, err := toml.DecodeFile(configFile, &config); err != nil {
return Configuration{}, err
}
return config, nil
}
func fetchHTML(url, userAgent string) (string, error) {
client := &http.Client{Timeout: 10 * time.Second}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
request.Header.Set("User-Agent", userAgent)
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP request failed with status code: %d", response.StatusCode)
}
htmlBytes, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(htmlBytes), nil
}
// absolute monstrosity
func extractJSScriptURLs(htmlString, baseURL string) ([]string, error) {
var jsURLs []string
base, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
tokenizer := html.NewTokenizer(strings.NewReader(htmlString))
for {
tokenType := tokenizer.Next()
switch tokenType {
case html.ErrorToken:
return jsURLs, nil // End of the document
case html.StartTagToken, html.SelfClosingTagToken:
token := tokenizer.Token()
if token.Data == "script" {
for _, attr := range token.Attr {
if attr.Key == "src" {
scriptURL, err := url.Parse(attr.Val)
if err != nil {
return nil, err
}
absoluteURL := base.ResolveReference(scriptURL).String()
jsURLs = append(jsURLs, absoluteURL)
}
}
}
}
}
}
func fetchJavascript(jsURL, userAgent string) (string, error) {
client := &http.Client{Timeout: 10 * time.Second}
request, err := http.NewRequest("GET", jsURL, nil)
if err != nil {
return "", err
}
request.Header.Set("User-Agent", userAgent)
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP request failed with status code: %d", response.StatusCode)
}
jsBytes, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(jsBytes), nil
}
func saveJavascript(jsURL, content string) error {
domain, filename, err := getFilename(jsURL)
if err != nil {
return err
}
// Create folder for domain if it doesn't exist
if err := os.MkdirAll(domain, 0755); err != nil {
return err
}
filename = filepath.Join(domain, filename)
if err := os.WriteFile(filename, []byte(content), 0644); err != nil {
return err
}
return nil
}
func getFilename(jsURL string) (string, string, error) {
url, err := url.Parse(jsURL)
if err != nil {
return "", "", err
}
domain := url.Hostname()
filename := filepath.Base(url.Path)
return domain, filename, nil
}