forked from michenriksen/aquatone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
269 lines (230 loc) · 7.23 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"time"
"github.com/ayusheek/aquatone/agents"
"github.com/ayusheek/aquatone/core"
"github.com/ayusheek/aquatone/parsers"
"github.com/google/uuid"
)
var (
sess *core.Session
err error
)
func isURL(s string) bool {
u, err := url.ParseRequestURI(s)
if err != nil {
return false
}
if u.Scheme == "" {
return false
}
return true
}
func hasSupportedScheme(s string) bool {
u, err := url.ParseRequestURI(s)
if err != nil {
return false
}
if u.Scheme == "http" || u.Scheme == "https" {
return true
}
return false
}
func main() {
if sess, err = core.NewSession(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if *sess.Options.Version {
sess.Out.Info("%s v%s", core.Name, core.Version)
os.Exit(0)
}
fi, err := os.Stat(*sess.Options.OutDir)
if os.IsNotExist(err) {
sess.Out.Fatal("Output destination %s does not exist\n", *sess.Options.OutDir)
os.Exit(1)
}
if !fi.IsDir() {
sess.Out.Fatal("Output destination must be a directory\n")
os.Exit(1)
}
sess.Out.Important("%s v%s started at %s\n\n", core.Name, core.Version, sess.Stats.StartedAt.Format(time.RFC3339))
if *sess.Options.SessionPath != "" {
jsonSession, err := ioutil.ReadFile(*sess.Options.SessionPath)
if err != nil {
sess.Out.Fatal("Unable to read session file at %s: %s\n", *sess.Options.SessionPath, err)
os.Exit(1)
}
var parsedSession core.Session
if err := json.Unmarshal(jsonSession, &parsedSession); err != nil {
sess.Out.Fatal("Unable to parse session file at %s: %s\n", *sess.Options.SessionPath, err)
os.Exit(1)
}
sess.Out.Important("Loaded Aquatone session at %s\n", *sess.Options.SessionPath)
sess.Out.Important("Generating HTML report...")
var template []byte
if *sess.Options.TemplatePath != "" {
template, err = ioutil.ReadFile(*sess.Options.TemplatePath)
} else {
template, err = sess.Asset("static/report_template.html")
}
if err != nil {
sess.Out.Fatal("Can't read report template file\n")
os.Exit(1)
}
report := core.NewReport(&parsedSession, string(template))
f, err := os.OpenFile(sess.GetFilePath("aquatone_report.html"), os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
sess.Out.Fatal("Error during report generation: %s\n", err)
os.Exit(1)
}
err = report.Render(f)
if err != nil {
sess.Out.Fatal("Error during report generation: %s\n", err)
os.Exit(1)
}
sess.Out.Important(" done\n\n")
sess.Out.Important("Wrote HTML report to: %s\n\n", sess.GetFilePath("aquatone_report.html"))
os.Exit(0)
}
agents.NewTCPPortScanner().Register(sess)
agents.NewURLPublisher().Register(sess)
agents.NewURLRequester().Register(sess)
agents.NewURLHostnameResolver().Register(sess)
agents.NewURLPageTitleExtractor().Register(sess)
agents.NewURLScreenshotter().Register(sess)
agents.NewURLTechnologyFingerprinter().Register(sess)
agents.NewURLTakeoverDetector().Register(sess)
reader := bufio.NewReader(os.Stdin)
var targets []string
if *sess.Options.Nmap {
parser := parsers.NewNmapParser()
targets, err = parser.Parse(reader)
if err != nil {
sess.Out.Fatal("Unable to parse input as Nmap/Masscan XML: %s\n", err)
os.Exit(1)
}
} else {
parser := parsers.NewRegexParser()
targets, err = parser.Parse(reader)
if err != nil {
sess.Out.Fatal("Unable to parse input.\n")
os.Exit(1)
}
}
if len(targets) == 0 {
sess.Out.Fatal("No targets found in input.\n")
os.Exit(1)
}
sess.Out.Important("Targets : %d\n", len(targets))
sess.Out.Important("Threads : %d\n", *sess.Options.Threads)
sess.Out.Important("Ports : %s\n", strings.Trim(strings.Replace(fmt.Sprint(sess.Ports), " ", ", ", -1), "[]"))
sess.Out.Important("Output dir : %s\n\n", *sess.Options.OutDir)
sess.EventBus.Publish(core.SessionStart)
for _, target := range targets {
if isURL(target) {
if hasSupportedScheme(target) {
sess.EventBus.Publish(core.URL, target)
}
} else {
sess.EventBus.Publish(core.Host, target)
}
}
time.Sleep(1 * time.Second)
sess.EventBus.WaitAsync()
sess.WaitGroup.Wait()
sess.EventBus.Publish(core.SessionEnd)
time.Sleep(1 * time.Second)
sess.EventBus.WaitAsync()
sess.WaitGroup.Wait()
sess.Out.Important("Calculating page structures...")
f, _ := os.OpenFile(sess.GetFilePath("aquatone_urls.txt"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
for _, page := range sess.Pages {
filename := sess.GetFilePath(fmt.Sprintf("html/%s.html", page.BaseFilename()))
body, err := os.Open(filename)
if err != nil {
continue
}
structure, _ := core.GetPageStructure(body)
page.PageStructure = structure
f.WriteString(page.URL + "\n")
}
f.Close()
sess.Out.Important(" done\n")
sess.Out.Important("Clustering similar pages...")
for _, page := range sess.Pages {
foundCluster := false
for clusterUUID, cluster := range sess.PageSimilarityClusters {
addToCluster := true
for _, pageURL := range cluster {
page2 := sess.GetPage(pageURL)
if page2 != nil && core.GetSimilarity(page.PageStructure, page2.PageStructure) < 0.80 {
addToCluster = false
break
}
}
if addToCluster {
foundCluster = true
sess.PageSimilarityClusters[clusterUUID] = append(sess.PageSimilarityClusters[clusterUUID], page.URL)
break
}
}
if !foundCluster {
newClusterUUID := uuid.New().String()
sess.PageSimilarityClusters[newClusterUUID] = []string{page.URL}
}
}
sess.Out.Important(" done\n")
sess.Out.Important("Generating HTML report...")
var template []byte
if *sess.Options.TemplatePath != "" {
template, err = ioutil.ReadFile(*sess.Options.TemplatePath)
} else {
template, err = sess.Asset("static/report_template.html")
}
if err != nil {
sess.Out.Fatal("Can't read report template file\n")
os.Exit(1)
}
report := core.NewReport(sess, string(template))
f, err = os.OpenFile(sess.GetFilePath("aquatone_report.html"), os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
sess.Out.Fatal("Error during report generation: %s\n", err)
os.Exit(1)
}
err = report.Render(f)
if err != nil {
sess.Out.Fatal("Error during report generation: %s\n", err)
os.Exit(1)
}
sess.Out.Important(" done\n\n")
sess.End()
sess.Out.Important("Writing session file...")
err = sess.SaveToFile("aquatone_session.json")
if err != nil {
sess.Out.Error("Failed!\n")
sess.Out.Debug("Error: %v\n", err)
}
sess.Out.Important("Time:\n")
sess.Out.Info(" - Started at : %v\n", sess.Stats.StartedAt.Format(time.RFC3339))
sess.Out.Info(" - Finished at : %v\n", sess.Stats.FinishedAt.Format(time.RFC3339))
sess.Out.Info(" - Duration : %v\n\n", sess.Stats.Duration().Round(time.Second))
sess.Out.Important("Requests:\n")
sess.Out.Info(" - Successful : %v\n", sess.Stats.RequestSuccessful)
sess.Out.Info(" - Failed : %v\n\n", sess.Stats.RequestFailed)
sess.Out.Info(" - 2xx : %v\n", sess.Stats.ResponseCode2xx)
sess.Out.Info(" - 3xx : %v\n", sess.Stats.ResponseCode3xx)
sess.Out.Info(" - 4xx : %v\n", sess.Stats.ResponseCode4xx)
sess.Out.Info(" - 5xx : %v\n\n", sess.Stats.ResponseCode5xx)
sess.Out.Important("Screenshots:\n")
sess.Out.Info(" - Successful : %v\n", sess.Stats.ScreenshotSuccessful)
sess.Out.Info(" - Failed : %v\n\n", sess.Stats.ScreenshotFailed)
sess.Out.Important("Wrote HTML report to: %s\n\n", sess.GetFilePath("aquatone_report.html"))
}