-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathportscanner.go
242 lines (194 loc) · 4.43 KB
/
portscanner.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
package portscanner
import (
"fmt"
"net"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
)
// IPv4 is the type used for IP addresses.
type IPv4 [4]int
/*struct IPPortList{
ip IPv4
ports []string
}*/
// Check checks if an error is nil or a significant error.
func Check(err error) {
if err != nil {
panic(err)
}
}
// ToInt converts a string to integer, as strconv.Atoi does, but without
// returning errors.
func ToInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}
// ToString converts an IP from IPv4 type to string.
func (ip *IPv4) ToString() string {
ipStringed := strconv.Itoa(ip[0])
for i := 1; i < 4; i++ {
strI := strconv.Itoa(ip[i])
ipStringed += "." + strI
}
return ipStringed
}
// IsValid checks an IP address as valid or not.
func (ip *IPv4) IsValid() bool {
for i, oct := range ip {
if i == 0 || i == 3 {
if oct < 1 || oct > 254 {
return false
}
} else {
if oct < 0 || oct > 255 {
return false
}
}
}
return true
}
// PlusPlus increments an IPv4 value.
func (ip *IPv4) PlusPlus() *IPv4 {
if ip[3] < 254 {
ip[3] = ip[3] + 1
} else {
if ip[2] < 255 {
ip[2] = ip[2] + 1
ip[3] = 1
} else {
if ip[1] < 255 {
ip[1] = ip[1] + 1
ip[2] = 1
ip[3] = 1
} else {
if ip[0] < 255 {
ip[0] = ip[0] + 1
ip[1] = 1
ip[2] = 1
ip[3] = 1
}
}
}
}
return ip
}
// ToIPv4 converts an string to a IPv4.
func ToIPv4(ip string) IPv4 {
var newIP IPv4
ipS := strings.Split(ip, ".")
for i, v := range ipS {
newIP[i], _ = strconv.Atoi(v)
}
return newIP
}
// ParseIPSequence gets a sequence of IP addresses correspondent from an
// "init-end" entry.
func ParseIPSequence(ipSequence string) []IPv4 {
var arrayIps []IPv4
series, _ := regexp.Compile("([0-9]+)")
// For sequence ips, using '-'
lSeries := series.FindAllStringSubmatch(ipSequence, -1)
for i := ToInt(lSeries[3][0]); i <= ToInt(lSeries[4][0]); i++ {
arrayIps = append(arrayIps, IPv4{
ToInt(lSeries[0][0]),
ToInt(lSeries[1][0]),
ToInt(lSeries[2][0]),
i})
}
return arrayIps
}
// ParsePortList gets a port list from its port entry in arguments.
func ParsePortList(rawPorts string) []string {
var ports []string
individuals, _ := regexp.Compile("([0-9]+)[,]*")
series, _ := regexp.Compile("([0-9]+)[-]([0-9]+)")
// For individual ports, separated by ','
lIndividuals := individuals.FindAllStringSubmatch(rawPorts, -1)
// For sequence ports, using '-'
lSeries := series.FindAllStringSubmatch(rawPorts, -1)
if len(lSeries) > 0 {
for _, s := range lSeries {
init, _ := strconv.Atoi(s[1])
end, _ := strconv.Atoi(s[2])
for i := init + 1; i < end; i++ {
ports = append(ports, strconv.Itoa(i))
}
}
}
for _, port := range lIndividuals {
ports = append(ports, port[1])
}
sort.Strings(ports)
return ports
}
// GetAllIPsClassC returns a slice of IPv4 with all IP addresses
// from a Class C.
//func GetAllIPsClassC(ip IPv4) []IPv4 {}
// PresentResults presents all results in console.
func PresentResults(ip IPv4, ports []string) int {
fmt.Println(" \n>" + ip.ToString())
fmt.Println(" Port: Description:")
for _, port := range ports {
fmt.Println(" " + port + "\t" + portShortList[port])
}
return 0
}
// PortScanner scans IP:port pairs looking for open ports on IP addresses.
func PortScanner(ip IPv4, portList []string) []string {
var open []string
for _, port := range portList {
conn, err := net.DialTimeout("tcp",
ip.ToString()+":"+port,
100*time.Millisecond)
if err == nil {
conn.Close()
open = append(open, port)
}
}
return open
}
// IPScanner scans all IP addresses in ipList for every port in portList.
func IPScanner(ipstr []string, portStr []string, printResults bool) map[IPv4][]string {
m := make(map[IPv4][]string)
var ipList []IPv4
var portList []string
var wg sync.WaitGroup
if len(portStr) == 1 {
portList = ParsePortList(portStr[0])
} else {
portList = portStr
}
if len(ipstr) == 0 {
ipList = append(ipList, IPv4{127, 0, 0, 1})
} else {
for _, i := range ipstr {
if strings.Contains(i, "-") {
ipList = append(ipList, ParseIPSequence(i)...)
} else {
ip := ToIPv4(i)
if ip.IsValid() {
ipList = append(ipList, ip)
}
}
}
}
for _, ip := range ipList {
wg.Add(1)
go func(ip IPv4) {
defer wg.Done()
result := PortScanner(ip, portList)
if len(result) > 0 {
m[ip] = result
if printResults {
PresentResults(ip, result)
}
}
}(ip)
}
wg.Wait()
return m
}