/
portscan.go
executable file
·156 lines (138 loc) · 3.37 KB
/
portscan.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
package portscan
import (
"encoding/json"
"log"
"strconv"
"strings"
"time"
"pkg/utils/structs"
"sync"
"pkg/profiles"
)
var (
mu sync.Mutex
scanResultChannel = make(chan host)
)
type PortScanParams struct {
Hosts []string `json:"hosts"` // Can also be a cidr
Ports string `json:"ports"`
}
func doScan(hostList []string, portListStrs []string, job *structs.Job) []CIDR {
// Variable declarations
timeout := time.Duration(500) * time.Millisecond
var portList []PortRange
// populate the portList
for i := 0; i < len(portListStrs); i++ {
if strings.Contains(portListStrs[i], "-") && len(portListStrs) == 1 {
// They want all the ports
allPorts := PortRange{1, 65535}
var newList []PortRange
newList = append(newList, allPorts)
portList = newList
break
}
var tmpRange PortRange
if strings.Contains(portListStrs[i], "-") {
parts := strings.Split(portListStrs[i], "-")
start, err := strconv.Atoi(parts[0])
if err == nil {
end, err := strconv.Atoi(parts[1])
if err == nil {
tmpRange = PortRange{
Start: start,
End: end,
}
portList = append(portList, tmpRange)
}
}
} else {
intPort, err := strconv.Atoi(portListStrs[i])
if err == nil {
tmpRange = PortRange{
Start: intPort,
End: intPort,
}
portList = append(portList, tmpRange)
}
}
}
// var cidrs []*CIDR
var results []CIDR
// Scan the hosts
go job.MonitorStop()
for i := 0; i < len(hostList); i++ {
newCidr, err := NewCIDR(hostList[i])
if err != nil {
continue
} else {
// Iterate through every host in hostCidr
newCidr.ScanHosts(portList, timeout, job)
results = append(results, *newCidr)
// cidrs = append(cidrs, newCidr)
}
}
return results
}
func Run(task structs.Task) {
msg := structs.Response{}
msg.TaskID = task.TaskID
params := PortScanParams{}
err := json.Unmarshal([]byte(task.Params), ¶ms)
if err != nil {
msg.UserOutput = err.Error()
msg.Completed = true
msg.Status = "error"
resp, _ := json.Marshal(msg)
mu.Lock()
profiles.TaskResponses = append(profiles.TaskResponses, resp)
mu.Unlock()
return
}
if len(params.Hosts) == 0 {
msg.UserOutput = "No hosts given to scan"
msg.Completed = true
msg.Status = "error"
resp, _ := json.Marshal(msg)
mu.Lock()
profiles.TaskResponses = append(profiles.TaskResponses, resp)
mu.Unlock()
return
}
if len(params.Ports) == 0 {
msg.UserOutput = "No ports given to scan"
msg.Completed = true
msg.Status = "error"
resp, _ := json.Marshal(msg)
mu.Lock()
profiles.TaskResponses = append(profiles.TaskResponses, resp)
mu.Unlock()
return
}
portStrings := strings.Split(params.Ports, ",")
log.Println("Beginning portscan...")
results := doScan(params.Hosts, portStrings, task.Job)
if task.Job.Monitoring {
go task.Job.SendKill()
}
// log.Println("Finished!")
data, err := json.MarshalIndent(results, "", " ")
// // fmt.Println("Data:", string(data))
if err != nil {
msg.UserOutput = err.Error()
msg.Completed = true
msg.Status = "error"
resp, _ := json.Marshal(msg)
mu.Lock()
profiles.TaskResponses = append(profiles.TaskResponses, resp)
mu.Unlock()
return
}
// fmt.Println("Sending on up the data:\n", string(data))
msg.UserOutput = string(data)
msg.Completed = true
resp, _ := json.Marshal(msg)
mu.Lock()
profiles.TaskResponses = append(profiles.TaskResponses, resp)
mu.Unlock()
return
}