-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
169 lines (134 loc) · 4.35 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
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
package main
import (
"GoScan/core/ProbeParser"
"GoScan/core/ProbeParser/Types"
"GoScan/core/portscan"
"GoScan/core/helpers"
"encoding/json"
"flag"
"github.com/malfunkt/iprange"
"strings"
"strconv"
"log"
"fmt"
"os"
"bufio"
"sort"
)
// commandline params for future use
var hostsInput = ""
var portsInput = ""
var outFileInput = ""
var excludeFields = ""
var includeFields = ""
var excludeFieldsList []string
var includeFieldsList []string
var outFileHanle *os.File
var outputWriter *bufio.Writer
var printCSV = false
var allMatches = false
var timeoutInput int
var ports []int
var hostLists []string
// initializing the scanner. parsing the command arguments and created output file if required
func init() {
flag.StringVar(&hostsInput, "h", "", "Host to be scanned, supports four formats:\n192.168.1.1\n192.168.1.1-10\n192.168.1.*\n192.168.1.0/24.")
flag.StringVar(&portsInput, "p", "80-99,7000-9000,9001-9999,4430,1433,1521,3306,5000,5432,6379,21,22,100-500,873,4440,6082,3389,5560,5900-5909,1080,1900,10809,50030,50050,50070", "Customize port list, separate with ',' example: 21,22,80-99,8000-8080 ...")
flag.IntVar(&timeoutInput, "t", 2, "Setting scaner connection timeouts,Maxtime 30 Second.")
flag.StringVar(&outFileInput, "w", "", "Output the scanning information to file.\n[CSV mode only]")
flag.StringVar(&excludeFields, "ex", "", "Exclude the following field from the output.\n[CSV mode only]\n-e Banner,RawBanner,Hostname ")
flag.StringVar(&includeFields, "inc", "", "Include only the following field from the output.\n[CSV mode only]\nOverrides exclude filer\n-i IP,Name,Port ")
flag.BoolVar(&printCSV, "csv", false, "Output as CSV\n[BOOL] default false")
flag.BoolVar(&allMatches, "all", false, "scan for all mathces")
flag.Parse()
// creating output file if so required
if outFileInput != "" {
outFileHanle, err := os.Create(outFileInput)
helpers.Check(err)
outputWriter = bufio.NewWriter(outFileHanle)
}
// parsing hosts input
hostlist, err := iprange.ParseList(hostsInput)
if err == nil {
hostsList := hostlist.Expand()
for _, host := range hostsList {
host := host.String()
hostLists = append(hostLists, host)
}
} else {
flag.Usage()
os.Exit(1)
}
// parsing the imported ports
ports = parsePort(portsInput)
// parsing exclude list
excludeFieldsList = strings.Split(excludeFields, ",")
includeFieldsList = strings.Split(includeFields, ",")
}
// printing to screen or writing to file. (determinated by commandline param)
func print(data string) {
if outFileInput == "" {
fmt.Println(data)
} else {
_, err := fmt.Fprintf(outputWriter, data + "\n")
helpers.Check(err)
outputWriter.Flush()
}
}
// parsing the ports list input
func parsePort(ports string) []int {
var scanPorts []int
slices := strings.Split(ports, ",")
for _, port := range slices {
port = strings.Trim(port, " ")
upper := port
if strings.Contains(port, "-") {
ranges := strings.Split(port, "-")
if len(ranges) < 2 {
continue
}
sort.Strings(ranges)
port = ranges[0]
upper = ranges[1]
}
start, _ := strconv.Atoi(port)
end, _ := strconv.Atoi(upper)
for i := start; i <= end; i++ {
scanPorts = append(scanPorts, i)
}
}
return scanPorts
}
func main() {
defer outFileHanle.Close()
headerPrinted := false
log.Printf("Found %d Hosts. \n", len(hostLists))
log.Printf("Scanning %d ports \n", len(ports))
// loading nmap probes from the file
vscan := Types.VScan{}
vscan.ParseServiceProbes()
// scanning open ports
// returns <hosts, addresses>, hosts are not relevant for the next steps. just addresses
// no probes - just TCP-CONNCET scan
addresses := portscan.TCPportScan(hostLists, ports, "tcp", timeoutInput)
// now probe all the live hosts
for host, openPorts := range addresses {
// scan target with nmap probes and custom modules
results := ProbeParser.ScanTarget(&vscan, host, openPorts, allMatches)
for _, results := range results {
for _, result := range results {
// showing the output as CSV or JSON
if printCSV {
if !headerPrinted {
print(strings.Join(Types.GetHeaders(&result, includeFieldsList, excludeFieldsList), ","))
headerPrinted = true
}
print(strings.Join(Types.GetValues(&result, includeFieldsList, excludeFieldsList), ","))
} else {
s, _ := json.MarshalIndent(result, "", "\t")
log.Println(string(s))
}
}
}
}
}