-
Notifications
You must be signed in to change notification settings - Fork 1
/
divinity.go
354 lines (341 loc) · 7.66 KB
/
divinity.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
/*
##############################################################################
# Copyright (C) 2020 Hakdefnet International <https://hakdefnet.org>
#
# Authors:
# phx <https://github.com/phx>
# 1D10T <https://github.com/HDN-1D10T>
#
# This program free software licensed under Creative Commons BY-NC-ND 4.0.
# You can redistribute it and/or modify it under the terms of the
# Attribution-NonCommerical-NoDerivatives 4.0 International License,
# as published by Creative Commons.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See CC Attribution-NonCommerical-NoDerivatives 4.0 International License
# for more details.
#
# You should have received a copy of the CC BY-NC-ND 4.0 License along with
# this program. If not, see <https://creativecommons.org/licenses/>.
##############################################################################
*/
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"regexp"
"runtime"
"strconv"
"sync"
"github.com/HDN-1D10T/divinity/src/util"
"github.com/HDN-1D10T/divinity/src/config"
"github.com/HDN-1D10T/divinity/src/masscan"
"github.com/HDN-1D10T/divinity/src/shodan"
"github.com/HDN-1D10T/divinity/src/tcp"
)
// Configuration imported from src/config
type Configuration struct{ config.Options }
func makeRange(min, max int) []int {
r := make([]int, max-min+1)
for i := range r {
r[i] = min + i
}
return r
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func getIPsFromCIDR(cidr string) ([]string, error) {
var ips []string
allIPs := regexp.MustCompile(`/0$`)
if allIPs.MatchString(cidr) {
err := "You can't pass a /0. Give me something I can handle."
log.Fatal(err)
}
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
log.Fatal(err)
}
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
}
single := regexp.MustCompile(`/32$`)
if single.MatchString(cidr) {
return ips, nil
}
// remove network address and broadcast address
return ips[1 : len(ips)-1], nil
}
func mScan(cidr string) {
m := masscan.New()
m.SetPorts("0-65535")
m.SetRanges(cidr)
m.SetRate("2000")
m.SetExclude("127.0.0.1")
err := m.Run()
if err != nil {
fmt.Println("scanner failed", err)
return
}
results, err := m.Parse()
if err != nil {
fmt.Println("Scan Results:", err)
return
}
for _, result := range results {
fmt.Println(result)
}
}
func cidrFromStdin() (ips []string) {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
var ranges []string
for scanner.Scan() {
if scanner.Text() != "" {
ranges = append(ranges, scanner.Text())
}
}
for _, rangx := range ranges {
ipList, _ := getIPsFromCIDR(rangx)
for _, ip := range ipList {
ips = append(ips, ip)
}
}
return ips
}
func cidrFromList(list string) (ips []string) {
file, err := os.Open(list)
if err != nil {
util.PanicErr(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var ranges []string
for scanner.Scan() {
if scanner.Text() != "" {
ranges = append(ranges, scanner.Text())
}
}
for _, rangx := range ranges {
ipList, _ := getIPsFromCIDR(rangx)
for _, ip := range ipList {
ips = append(ips, ip)
}
}
return ips
}
func processList(ips []string) {
runtime.GOMAXPROCS(100)
var wg = sync.WaitGroup{}
conf := Configuration{
config.ParseConfiguration(),
}
listIPs := *conf.ListIPs
cidr := *conf.Cidr
masscan := *conf.Masscan
protocol := *conf.Protocol
scan := *conf.Scan
outputFile := *conf.OutputFile
chIPs := make(chan string, len(ips))
chDone := make(chan bool)
go func() {
for _, ip := range ips {
chIPs <- ip
}
}()
if listIPs {
go func() {
for i := 0; i <= len(ips)-1; i++ {
ip := <-chIPs
if len(outputFile) > 0 {
util.FileWrite(ip + "\n")
}
fmt.Println(ip)
}
chDone <- true
}()
<-chDone
return
}
if *conf.Routes {
asns := ips
routes := tcp.GetAllRoutes(asns)
for _, route := range routes {
if len(outputFile) > 0 {
util.FileWrite(route + "\n")
}
fmt.Println(route)
}
return
}
// -scanfast && -port
if *conf.ScanFast && len(*conf.Port) > 0 {
tcp.ScanMulti(ips, *conf.Port)
return
}
// -scan
if scan {
// Scan with Masscan
if masscan {
mScan(cidr)
return
}
for _, host := range ips {
// Scan with native scanner
tcp.Scan(host)
}
return
}
if protocol == "tcp" {
tcp.Handler(ips)
}
if protocol == "http" || protocol == "https" {
for _, host := range ips {
wg.Add(1)
go tcp.DoHTTPLogin(host, &wg)
}
wg.Wait()
}
return
}
func main() {
runtime.GOMAXPROCS(100)
var wg = sync.WaitGroup{}
conf := Configuration{
config.ParseConfiguration(),
}
asn := *conf.ASN
cidr := *conf.Cidr
list := *conf.List
ipsOnly := *conf.IPOnly
passive := *conf.Passive
shodanSearch := *conf.SearchTerm
// Get CIDR range from ASN
if len(*conf.ASN) > 1 && *conf.Routes {
asnMatch := regexp.MustCompile(`^[Aa][Ss][0-9]+$`)
if !asnMatch.MatchString(asn) {
asn = "AS" + asn
}
var asns []string
asns = append(asns, asn)
tcp.GetAllRoutes(asns)
return
}
// Process list from CIDR range
if len(cidr) == 1 || cidr == "stdin" {
ips := cidrFromStdin()
processList(ips)
return
}
if len(cidr) > 5 && len(cidr) < 19 {
ips, _ := getIPsFromCIDR(cidr)
processList(ips)
return
}
// Process list from stdin
if len(list) == 1 || list == "stdin" {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
var ips []string
for scanner.Scan() {
if scanner.Text() != "" {
ips = append(ips, scanner.Text())
}
}
processList(ips)
return
}
// Process list from file
if len(list) > 1 {
if cidr == "list" {
ips := cidrFromList(list)
processList(ips)
return
}
file, err := os.Open(list)
if err != nil {
util.PanicErr(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var ips []string
for scanner.Scan() {
if scanner.Text() != "" {
ips = append(ips, scanner.Text())
}
}
processList(ips)
return
}
// Process list from Shodan in passive mode
if passive {
apiKey := os.Getenv("SHODAN_API_KEY")
s := shodan.New(apiKey)
info, err := s.APIInfo()
util.PanicErr(err)
// Get Shodan IP Results
if !ipsOnly {
fmt.Printf(
"Query Credits:\t%d\nScan Credits:\t%d\n\n",
info.QueryCredits,
info.ScanCredits)
}
pageRange := makeRange(1, *conf.Pages)
for _, num := range pageRange {
pageStr := strconv.Itoa(num)
query := shodanSearch + "&page=" + pageStr
hostSearch, err := s.HostSearch(query)
util.PanicErr(err)
// Run config from command line arguments:
if ipsOnly {
for _, host := range hostSearch.Matches {
msg := host.IPString
util.LogWrite(msg)
}
} else {
for _, host := range hostSearch.Matches {
msg := host.IPString + "\t\t" + host.Location.CountryName
util.LogWrite(msg)
}
}
}
return
}
// Shodan active mode
if len(shodanSearch) > 0 {
apiKey := os.Getenv("SHODAN_API_KEY")
s := shodan.New(apiKey)
info, err := s.APIInfo()
util.PanicErr(err)
fmt.Printf(
"Query Credits:\t%d\nScan Credits:\t%d\n\n",
info.QueryCredits,
info.ScanCredits)
pageRange := makeRange(1, *conf.Pages)
for _, num := range pageRange {
pageStr := strconv.Itoa(num)
query := shodanSearch + "&page=" + pageStr
hostSearch, err := s.HostSearch(query)
util.PanicErr(err)
// wg.Add(len(hostSearch.Matches))
for _, host := range hostSearch.Matches {
wg.Add(1)
go tcp.DoHTTPLogin(host.IPString, &wg)
}
wg.Wait()
}
}
return
}