-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoMap.go
283 lines (257 loc) · 7.88 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// ______ __ __
// / \ | \ / \
// | $$$$$$\ ______ | $$\ / $$ ______ ______
// | $$ __\$$ / \ | $$$\ / $$$ | \ / \
// | $$| \| $$$$$$\| $$$$\ $$$$ \$$$$$$\| $$$$$$\
// | $$ \$$$$| $$ | $$| $$\$$ $$ $$ / $$| $$ | $$
// | $$__| $$| $$__/ $$| $$ \$$$| $$| $$$$$$$| $$__/ $$
// \$$ $$ \$$ $$| $$ \$ | $$ \$$ $$| $$ $$
// \$$$$$$ \$$$$$$ \$$ \$$ \$$$$$$$| $$$$$$$
// | $$
// | $$
// \$$
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"sort"
"strconv"
"strings"
"time"
"github.com/adedayo/cidr"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func connect(ip string, port int, reply chan string) bool {
host := ip + ":" + strconv.Itoa(port)
conn, err := net.DialTimeout("tcp", host, 5*time.Second)
if err != nil {
reply <- "|--port " + strconv.Itoa(port) + "---closed"
return false
}
defer conn.Close()
reply <- "|--port " + strconv.Itoa(port) + "---open"
return true
}
func SYN(ip string, port int, reply chan string) bool {
dstAddress, err := net.LookupIP(ip)
if err != nil {
log.Fatal(err)
}
dstIP := dstAddress[0].To4()
var dstPort layers.TCPPort
dstPort = layers.TCPPort(port)
srcIP, sport := localIPPort(dstIP)
srcPort := layers.TCPPort(sport)
// IP header, only used with checksum
ipHeader := &layers.IPv4{
SrcIP: srcIP,
DstIP: dstIP,
Protocol: layers.IPProtocolTCP, // layers.I(nternet)P(rotocol)ProtocolT(ransport)C(ontrol)P(rotocol)
}
// TCP header
tcp := &layers.TCP{
SrcPort: srcPort,
DstPort: dstPort,
Seq: 1105024978,
SYN: true,
Window: 14600,
}
tcp.SetNetworkLayerForChecksum(ipHeader)
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
if err := gopacket.SerializeLayers(buf, opts, tcp); err != nil {
log.Fatal(err)
}
conn, err := net.ListenPacket("ip4:tcp", "0.0.0.0")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
if _, err := conn.WriteTo(buf.Bytes(), &net.IPAddr{IP: dstIP}); err != nil {
log.Fatal(err)
}
if err := conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
log.Fatal(err)
}
for {
b := make([]byte, 4096)
n, addr, err := conn.ReadFrom(b)
if err != nil {
log.Println("error reading packet: ", err)
return false
} else if addr.String() == dstIP.String() {
packet := gopacket.NewPacket(b[:n], layers.LayerTypeTCP, gopacket.Default)
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
tcp, _ := tcpLayer.(*layers.TCP)
if tcp.DstPort == srcPort {
if tcp.SYN && tcp.ACK {
reply <- "|--port " + strconv.Itoa(port) + "---open"
} else {
reply <- "|--port " + strconv.Itoa(port) + "---closed"
}
return true
}
}
}
}
}
func localIPPort(dstIP net.IP) (net.IP, int) {
serverAddress, err := net.ResolveUDPAddr("udp", dstIP.String()+":12345")
if err != nil {
log.Fatal(err)
}
// use this to find out what the source IP should be when crafting SYN packet
if con, err := net.DialUDP("udp", nil, serverAddress); err == nil {
if udpAddress, ok := con.LocalAddr().(*net.UDPAddr); ok {
return udpAddress.IP, udpAddress.Port
}
}
return nil, -1
}
func main() {
// declare vars
var ip string
var port = "default"
var syn bool
result := make(map[string]string)
scannedPorts := make(map[int]string)
fmt.Println(" ______ __ __ ")
fmt.Println(" / \\ | \\ / \\ ")
fmt.Println("| $$$$$$\\ ______ | $$\\ / $$ ______ ______ ")
fmt.Println("| $$ __\\$$ / \\ | $$$\\ / $$$ | \\ / \\ ")
fmt.Println("| $$| \\| $$$$$$\\| $$$$\\ $$$$ \\$$$$$$\\| $$$$$$\\")
fmt.Println("| $$ \\$$$$| $$ | $$| $$\\$$ $$ $$ / $$| $$ | $$")
fmt.Println("| $$__| $$| $$__/ $$| $$ \\$$$| $$| $$$$$$$| $$__/ $$")
fmt.Println(" \\$$ $$ \\$$ $$| $$ \\$ | $$ \\$$ $$| $$ $$")
fmt.Println(" \\$$$$$$ \\$$$$$$ \\$$ \\$$ \\$$$$$$$| $$$$$$$ ")
fmt.Println(" | $$ ")
fmt.Println(" | $$ ")
fmt.Println(" \\$$")
fmt.Println("\nStarting GoMap...")
flag.StringVar(&ip, "i", "127.0.0.1", "Specify ip(s) you wish to scan (comma delimited without spaces, CiDR notation accepted). Default is localhost.")
flag.StringVar(&port, "p", "default", "Specify ports you wish to scan (comma delimited with or without ranges and no spaces). Default is top 1000 most common TCP ports (Warning, this may take a while to run so go do something else).")
flag.BoolVar(&syn, "s", false, "Scan using TCP SYN mode.")
flag.Parse()
if port == "default" {
if content, err := ioutil.ReadFile("tcp.txt"); err == nil {
port = string(content)
}
}
// put the chosen ports into a map so they can easily be used later.
ports := strings.Split(port, ",")
for p := 0; p < len(ports); p++ {
if strings.Contains(ports[p], "-") {
temporary0, err0 := strconv.Atoi(strings.Split(ports[p], "-")[0])
temporary1, err1 := strconv.Atoi(strings.Split(ports[p], "-")[1])
if err0 == nil && err1 == nil {
for i := 0; i < temporary1-temporary0+1; i++ {
ports = append(ports, strconv.Itoa(temporary0+i))
}
}
}
}
// turn ranges into individual values
var portList = make(map[int]int)
for p := 0; p < len(ports); p++ {
if temp, err := strconv.Atoi(ports[p]); err == nil && !strings.Contains(ports[p], "-") {
portList[p] = temp
}
}
// do the same for ips, but take into account CiDR input
var ips []string
if strings.Contains(ip, "/") {
ips = cidr.Expand(ip)
} else {
ips = strings.Split(ip, ",")
}
var ipList = make(map[int]string)
for i := 0; i < len(ips); i++ {
ipList[i] = ips[i]
}
if syn == false {
reply := make(chan string)
for ipIndex := 0; ipIndex < len(ipList); ipIndex++ {
for i := range portList {
go connect(ipList[ipIndex], portList[i], reply)
scannedPorts[portList[i]] += <-reply + "\n"
}
// sort scannedPorts
temp := ""
sorted := make([]int, 0, len(scannedPorts))
for k := range scannedPorts {
sorted = append(sorted, k)
}
sort.Ints(sorted)
multi := 0
for _, k := range sorted {
if strings.Contains(scannedPorts[k], "open") {
temp += scannedPorts[k]
} else { // get rid of long lists of closed ports for ease of reading
if strings.Contains(scannedPorts[k-1], "closed") && strings.Contains(scannedPorts[k+1], "closed") {
if multi >= 1 {
temp += ""
} else {
temp += "|............closed\n"
multi += 1
}
} else {
temp += scannedPorts[k]
multi = 0
}
}
}
result[ipList[ipIndex]] = temp
for j := range scannedPorts {
delete(scannedPorts, j)
}
}
} else {
reply := make(chan string)
for ipIndex := 0; ipIndex < len(ipList); ipIndex++ {
for i := range portList {
go SYN(ipList[ipIndex], portList[i], reply)
scannedPorts[portList[i]] += <-reply + "\n"
}
// sort scannedPorts
temp := ""
sorted := make([]int, 0, len(scannedPorts))
for k := range scannedPorts {
sorted = append(sorted, k)
}
sort.Ints(sorted)
multi := 0
for _, k := range sorted {
if strings.Contains(scannedPorts[k], "open") {
temp += scannedPorts[k]
} else { // get rid of long lists of closed ports for ease of reading
if strings.Contains(scannedPorts[k-1], "closed") && strings.Contains(scannedPorts[k+1], "closed") {
if multi >= 1 {
temp += ""
} else {
temp += "|............closed\n"
multi += 1
}
} else {
temp += scannedPorts[k]
multi = 0
}
}
}
result[ipList[ipIndex]] = temp
for j := range scannedPorts {
delete(scannedPorts, j)
}
}
}
for scanned := range result {
fmt.Println("\nHost " + scanned + ":")
fmt.Println(result[scanned])
}
}