-
Notifications
You must be signed in to change notification settings - Fork 19
/
gomap.go
211 lines (183 loc) · 4.41 KB
/
gomap.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
package gomap
import (
"bytes"
"encoding/json"
"fmt"
"net"
)
// IPScanResult contains the results of a scan on a single ip
type IPScanResult struct {
Hostname string
IP []net.IP
Results []portResult
}
// JsonRange contains a slice of of JsonIP results
type JsonRange struct {
results []JsonIP
}
// JsonIP contains the results for a single JSON entry
type JsonIP struct {
IP string
Hostname string
Active bool
Ports []string
}
type portResult struct {
Port int
State bool
Service string
}
type tcpHeader struct {
SrcPort uint16
DstPort uint16
SeqNum uint32
AckNum uint32
Flags uint16
Window uint16
ChkSum uint16
UrgentPointer uint16
}
type tcpOption struct {
Kind uint8
Length uint8
Data []byte
}
// RangeScanResult contains multiple IPScanResults
type RangeScanResult []*IPScanResult
// ScanIP scans a single IP for open ports
func ScanIP(hostname string, proto string, fastscan bool, stealth bool) (*IPScanResult, error) {
laddr, err := getLocalIP()
if err != nil {
return nil, err
}
if stealth {
if canSocketBind(laddr) == false {
return nil, fmt.Errorf("socket: operation not permitted")
}
}
return scanIPPorts(hostname, laddr, proto, fastscan, stealth)
}
// ScanRange scans every address on a CIDR for open ports
func ScanRange(proto string, fastscan bool, stealth bool) (RangeScanResult, error) {
laddr, err := getLocalIP()
if err != nil {
return nil, err
}
if stealth {
if canSocketBind(laddr) == false {
return nil, fmt.Errorf("socket: operation not permitted")
}
}
return scanIPRange(laddr, proto, fastscan, stealth)
}
// String with the results of a single scanned IP
func (results *IPScanResult) String() string {
b := bytes.NewBuffer(nil)
ip := results.IP[len(results.IP)-1]
fmt.Fprintf(b, "\nHost: %s (%s)\n", results.Hostname, ip)
active := false
for _, r := range results.Results {
if r.State {
active = true
break
}
}
if active {
fmt.Fprintf(b, "\t| %s %s\n", "Port", "Service")
fmt.Fprintf(b, "\t| %s %s\n", "----", "-------")
for _, v := range results.Results {
if v.State {
fmt.Fprintf(b, "\t|---- %d %s\n", v.Port, v.Service)
}
}
} else if results.Hostname != "Unknown" {
fmt.Fprintf(b, "\t|---- %s\n", "No Open Ports Found")
}
return b.String()
}
// String with the results of multiple scanned IP's
func (results RangeScanResult) String() string {
b := bytes.NewBuffer(nil)
for _, r := range results {
ip := r.IP[len(r.IP)-1]
fmt.Fprintf(b, "\nHost: %s (%s)\n", r.Hostname, ip)
active := false
for _, r := range r.Results {
if r.State {
active = true
break
}
}
if active {
fmt.Fprintf(b, "\t| %s %s\n", "Port", "Service")
fmt.Fprintf(b, "\t| %s %s\n", "----", "-------")
for _, v := range r.Results {
if v.State {
fmt.Fprintf(b, "\t|---- %d %s\n", v.Port, v.Service)
}
}
} else if r.Hostname != "Unknown" {
fmt.Fprintf(b, "\t|---- %s\n", "No Open Ports Found")
}
}
return b.String()
}
// Contains a marshaled struct containing the results for a ip scan
func (results *IPScanResult) Json() (string, error) {
var ipdata JsonIP
fmt.Println(results.IP)
ipdata.IP = fmt.Sprintf("%s", results.IP[len(results.IP)-1])
ipdata.Hostname = results.Hostname
active := false
for _, r := range results.Results {
if r.State {
active = true
break
}
}
ipdata.Active = active
if active {
for _, v := range results.Results {
if v.State {
entry := fmt.Sprintf("%d: %s", v.Port, v.Service)
ipdata.Ports = append(ipdata.Ports, entry)
}
}
}
j, err := json.MarshalIndent(ipdata, "", " ")
if err != nil {
return "", err
}
return string(j), nil
}
// Contains a marshaled struct containing the results for a range scan
func (results RangeScanResult) Json() (string, error) {
var data JsonRange
for _, r := range results {
var ipdata JsonIP
ipdata.IP = fmt.Sprintf("%s", r.IP[len(r.IP)-1])
ipdata.Hostname = r.Hostname
active := false
for _, r := range r.Results {
if r.State {
active = true
break
}
}
ipdata.Active = active
if active {
for _, v := range r.Results {
if v.State {
entry := fmt.Sprintf("%d: %s", v.Port, v.Service)
ipdata.Ports = append(ipdata.Ports, entry)
}
}
}
data.results = append(data.results, ipdata)
}
j, err := json.MarshalIndent(data.results, "", " ")
if err != nil {
return "", err
}
return string(j), nil
}