-
Notifications
You must be signed in to change notification settings - Fork 1
/
overlord.go
234 lines (214 loc) · 6.29 KB
/
overlord.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
package main
import (
"fmt"
"net"
"net/url"
"os"
"time"
"overlord/models"
"overlord/scanners/port-scanner"
. "libs/color"
"github.com/asdine/storm"
"github.com/gin-gonic/gin"
"github.com/gocolly/colly"
//"github.com/gocolly/colly/debug"
"github.com/gocolly/colly/proxy"
"gopkg.in/mgo.v2/bson"
)
///// App ///////////////////////////////////////////////
type Application struct {
Name string
Version models.Version
Database *storm.DB
Collector colly.Collector
Config models.Config
}
func (self Application) SetCollectorLimits(parallelism int, delay time.Duration) {
self.Collector.Limit(&colly.LimitRule{
Parallelism: parallelism,
RandomDelay: delay * time.Second,
})
}
func (self Application) SetCollectorProxies(socksProxies string) (err error) {
proxies, err := proxy.RoundRobinProxySwitcher(socksProxies)
if err == nil {
self.Collector.SetProxyFunc(proxies)
}
return err
}
///// Main ////////////////////////////////////////////
func (self Application) PrintBanner() {
fmt.Println(Bold(Magenta(fmt.Sprintf("%v: Network Detector (v%v)", self.Name, self.Version.String()))))
fmt.Println(Gray("=================================="))
}
func LoadJSONConfig(path string) (config app.Config) {
fileData, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("Error: ", err)
return nil
}
json.Unmarshal(fileData, &config)
return config
}
func main() {
config := LoadJSONConfig("./config.json")
if _, err := os.Stat("~/.local/share/overlord"); err != nil {
if os.IsNotExist(err) {
os.Mkdir("~/.local/share/overlord", 700)
}
}
db, _ := storm.Open("servers.db")
app := Application{
Name: "Overlord",
Version: models.Version{0, 1, 0},
Database: db,
Config: models.Config{},
Collector: *colly.NewCollector(
//colly.Debugger(&debug.LogDebugger{}),
colly.IgnoreRobotsTxt(),
colly.MaxDepth(15),
colly.CacheDir("./cache/"),
//colly.DisallowedDomains("facebook.com", "twitter.com"),
colly.UserAgent("Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"),
//colly.UserAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"),
colly.Async(false),
//colly.URLFilters(
// regexp.MustCompile("(.+|^facebook|^twitter)$"),
//),
),
}
app.PrintBanner()
defer app.Database.Close()
app.InitWebUI()
app.SetCollectorLimits(2, 4)
///////////////////////////////////////////////////////////////
// On HTML Load ///////////////////////////////////////////////
app.Collector.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
e.Request.Visit(link)
})
///////////////////////////////////////////////////////////////
// On Request /////////////////////////////////////////////////
fmt.Println("Starting Server Scanning...")
app.Collector.OnRequest(func(r *colly.Request) {
if len(r.URL.String()) > 0 {
// Parse URL
u, _ := url.Parse(r.URL.String())
// Parse Addresses
addresses, _ := net.LookupIP(u.Host)
var stringAddresses []string
for _, address := range addresses {
stringAddresses = append(stringAddresses, address.String())
}
// Parse Headers
headersString := fmt.Sprintf("%s", r.Headers)
var s models.Server
err := db.One("Host", u.Host, &s)
if err != nil {
for _, ip := range addresses {
ps := portscanner.NewPortScanner(ip.String(), 2*time.Second, 5)
fmt.Printf("scanning port %d-%d...\n", 1, 65536)
openTCPPorts := ps.GetOpenedPort(1, 65536)
var services []models.Service
for i := 0; i < len(openTCPPorts); i++ {
port := openTCPPorts[i]
fmt.Print(" ", port, " [open]")
fmt.Println(" --> ", ps.DescribePort(port))
services = append(services, models.Service{
Description: ps.DescribePort(port),
Port: port,
})
}
fmt.Println("Listing open ports found with port-scanner lib:", openTCPPorts)
fmt.Println("Listing open services found with port-scanner lib:", services)
err := app.Database.Save(&models.Server{
ID: bson.NewObjectId(),
IPAddress: ip,
Host: u.Host,
OpenTCPPorts: openTCPPorts,
Services: services,
CreatedAt: time.Now(),
})
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println("Server successfully saved.")
}
}
err := app.Database.Save(&models.Page{
ID: bson.NewObjectId(),
Path: u.Path,
CreatedAt: time.Now(),
URL: r.URL.String(),
Scheme: u.Scheme,
Body: "",
RawQuery: u.RawQuery,
Headers: headersString[5:(len(headersString) - 1)],
})
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println("Page successfully saved.")
}
err = app.Database.Save(&models.Domain{
ID: bson.NewObjectId(),
CreatedAt: time.Now(),
Host: u.Host,
IPAddresses: addresses,
Headers: headersString[5:(len(headersString) - 1)],
})
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println("Domain successfully saved.")
}
}
}
})
//app.Collector.OnResponse(func(r *colly.Response) {
// fmt.Println(r.Ctx.Get("url"))
//})
//// Set error handler
//app.Collector.OnError(func(r *colly.Response, err error) {
// fmt.Println("Request URL:", r.Request.URL, "failed with response:", r, "\nError:", err)
//})
// Seed
app.Collector.Visit(app.Config.SeedURL)
}
func (app *Application) InitWebUI() {
// TODO: Provide a web API that serves results of crawling
router := gin.Default()
//gin.SetMode(gin.ReleaseMode)
v1 := router.Group("/api/v1")
v1.GET("/pages", app.Pages)
v1.GET("/domains", app.Domains)
v1.GET("/servers", app.Servers)
go router.Run(":8080")
}
func (app *Application) Servers(c *gin.Context) {
var servers []models.Server
err := app.Database.All(&servers)
if err != nil {
c.JSON(200, gin.H{"error": err})
} else {
c.IndentedJSON(200, servers)
}
}
func (app *Application) Domains(c *gin.Context) {
var domains []models.Domain
err := app.Database.All(&domains)
if err != nil {
c.JSON(200, gin.H{"error": err})
} else {
c.IndentedJSON(200, domains)
}
}
func (app *Application) Pages(c *gin.Context) {
var pages []models.Page
err := app.Database.All(&pages)
if err != nil {
c.JSON(200, gin.H{"error": err})
} else {
c.IndentedJSON(200, pages)
}
}