-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
115 lines (102 loc) · 3.25 KB
/
scanner.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
package main
import (
"flag"
"fmt"
"lilypad-scanner/addr"
"lilypad-scanner/log"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
func main() {
cidrStr := flag.String("cidr", "0.0.0.205|8", "specify the cidr range, only reversed format is supported(0.0.0.255|8)")
resume := flag.Uint("resume", 0, "resume pointer, last current value minus total worker count")
workers := flag.Int("workers", 8000, "worker count")
verbose := flag.Bool("verbose", false, "chatty mode")
flag.Parse()
fmt.Printf("Launched with cidr_rev: %s, resume: %d. workers: %d, verbose: %t\n", *cidrStr, *resume, *workers, *verbose)
cidr := addr.NewCIDR4Reversed(*cidrStr)
iter := cidr.Iterator().Resume(uint32(*resume)).
RegisterSkip(addr.NewCIDR4("0.0.0.0/8")).
RegisterSkip(addr.NewCIDR4("10.0.0.0/8")).
RegisterSkip(addr.NewCIDR4("100.64.0.0/10")).
RegisterSkip(addr.NewCIDR4("127.0.0.0/8")).
RegisterSkip(addr.NewCIDR4("169.254.0.0/16")).
RegisterSkip(addr.NewCIDR4("172.16.0.0/12")).
RegisterSkip(addr.NewCIDR4("192.0.0.0/24")).
RegisterSkip(addr.NewCIDR4("192.0.2.0/24")).
RegisterSkip(addr.NewCIDR4("192.88.99.0/24")).
RegisterSkip(addr.NewCIDR4("192.168.0.0/16")).
RegisterSkip(addr.NewCIDR4("198.18.0.0/15")).
RegisterSkip(addr.NewCIDR4("224.0.0.0/4")).
RegisterSkip(addr.NewCIDR4("240.0.0.0/4"))
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
results := make(chan ScanResult, 10)
responded := 0
matched := 0
go resultConsumer(results, &responded, &matched)
go mainOutput(iter, &responded, &matched)
go keepProgress(iter)
block := make(chan bool)
wg := sync.WaitGroup{}
for i := 0; i < *workers; i++ {
go func(id int) {
wg.Add(1)
<-block
worker(id, iter, results, *verbose)
wg.Done()
}(i)
}
close(block)
wg.Wait()
time.Sleep(time.Second * 10)
}
func keepProgress(iter *addr.CIDR4RevIterator) {
tick := time.NewTicker(time.Second * 10)
for {
f, err := os.OpenFile("progress.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC|os.O_SYNC, 0777)
if err != nil {
panic("failed to open progress.log: " + err.Error())
}
current := iter.Counter()
_, err = f.WriteString(fmt.Sprint(current))
if err != nil {
panic("failed to write progress.log: " + err.Error())
}
_ = f.Close()
<-tick.C
}
}
func resultConsumer(c chan ScanResult, responded *int, matched *int) {
for {
result := <-c
*responded++
log.Info().Logf("Server %s responded with: %s", result.ip, strconv.QuoteToASCII(result.response))
if strings.Contains(result.response, "client") {
log.Info().Log("THIS MIGHT BE A MATCH")
*matched++
}
}
}
func mainOutput(iter *addr.CIDR4RevIterator, responded *int, matched *int) {
tick := time.NewTicker(time.Second * 2)
t := time.Now()
for {
clear()
fmt.Println("Skyfalls' Low Performance Single-threaded Server Scanner(TM)")
fmt.Printf("Total ips: %d\n", iter.CountTotal())
fmt.Printf("Current: %d - %s\n", iter.Counter(), iter.Current())
fmt.Printf("Responded: %d Matched: %d\n", *responded, *matched)
progress := float64(iter.Counter()) / float64(iter.CountTotal())
elapsed := time.Now().Sub(t)
fmt.Printf("Progress: %.2f%% | ETA: %s\n", progress*100, time.Duration(float64(elapsed.Nanoseconds())/progress*(1-progress)))
fmt.Printf("Time Elapsed: %s", elapsed)
_ = <-tick.C
}
}
func clear() {
fmt.Print("\033[H\033[2J")
}