-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
166 lines (128 loc) · 3.44 KB
/
main.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
package main
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
)
func parseIPRange(cidr string) ([]string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
var ips []string
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
}
// remove network address and broadcast address
return ips[1 : len(ips)-1], nil
}
// Function to increment the IP for the for loop in the CIDR range parser
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func createPortRange(portRange []string) ([]int, error) {
if len(portRange) < 2 {
port, err := strconv.Atoi(portRange[0])
if err != nil {
fmt.Println(err)
}
return []int{port}, nil
}
// Convert string cli input to integers for building lists
range_start, err := strconv.Atoi(portRange[0])
if err != nil {
fmt.Println(err)
}
range_end, err := strconv.Atoi(portRange[1])
if err != nil {
fmt.Println(err)
}
var portList []int
for port := range_start; port <= range_end; port++ {
portList = append(portList, port)
}
return portList, nil
}
func convertPortListToString(portList []int) ([]string, error) {
var stringPortList []string
for _, port := range portList {
stringPortList = append(stringPortList, strconv.Itoa(port))
}
return stringPortList, nil
}
func doScan(target string, portList []int, wg *sync.WaitGroup) {
defer wg.Done()
var portOutPut []int
var pwg sync.WaitGroup
for _, port := range portList {
connStatus := make(chan net.Conn)
connErr := make(chan error)
pwg.Add(1)
// Spawn another goroutine per port for super fast scanning! (More ports == eats more memory of course)
go doTCPConnection(target, port, &pwg, connStatus, connErr)
conn,err := <-connStatus, <-connErr
close(connStatus)
close(connErr)
if err != nil {
fmt.Println(err)
}
// Convert string ports Ints back to strings to handles connection and printing status
if conn == nil {
fmt.Println("Could not connect to " + target + " on port " + strconv.Itoa(port))
} else {
fmt.Println("Connected to " + target + " on port " + strconv.Itoa(port))
portOutPut = append(portOutPut, port)
}
}
pwg.Wait()
stringPortOutput, _ := convertPortListToString(portOutPut)
if portOutPut != nil {
fmt.Println("Host: " + target + " Ports: " + strings.Join(stringPortOutput, "/TCP, ") + "/TCP")
}
portOutPut = nil
}
func doTCPConnection(target string, port int, pwg *sync.WaitGroup, connStatus chan net.Conn, connErr chan error) {
defer pwg.Done()
// Convert string ports Ints back to strings to handles connection and printing status
conn, err := net.DialTimeout("tcp", target+":"+strconv.Itoa(port), time.Duration(1)*time.Second)
connStatus <- conn
connErr <- err
}
func main() {
// Get IP/CIDR range from args
if len(os.Args) < 3 {
fmt.Println("Usage portscanner <IP or IP range> <port or port range>")
fmt.Println("Example: portscanner 192.168.2.3 1-1024")
os.Exit(0)
}
ip := string(os.Args[1])
port := string(os.Args[2])
portRange := strings.Split(port, "-")
portList, err := createPortRange(portRange)
if err != nil {
fmt.Println(err)
}
ips, err := parseIPRange(ip)
if err != nil {
fmt.Println(err)
}
if len(ips) == 0 {
ips = append(ips, ip)
}
// Spawn Goroutine per target and let the scans run async
var wg sync.WaitGroup
for _, target := range ips {
wg.Add(1)
go doScan(target, portList, &wg)
}
wg.Wait()
}