-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcpscan.go
168 lines (140 loc) · 2.99 KB
/
tcpscan.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
package main
import (
"flag"
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
"sync"
)
var TIMEOUT *time.Duration
var VERBOSE *bool
var mtx sync.Mutex
func end(msg string) {
fmt.Println(msg)
os.Exit(1)
}
func check(err error, msg string) {
if err != nil {
fmt.Println(msg)
os.Exit(1)
}
}
func wait() {
var i int
fmt.Println("Press enter to stop")
fmt.Scanf("%d", &i)
}
func checkPort(hostport string) (bool, string) {
conn, err := net.DialTimeout("tcp", hostport, *TIMEOUT)
if err != nil {
//panic(err)
return false, ""
}
defer conn.Close()
fmt.Fprintf(conn, "TEST\n\n")
buff := make([]byte, 1024)
conn.Read(buff)
banner := string(buff)
if buff[0] != 0 {
return true, banner
}
return false, banner
}
func expandPorts(port *string) *[]string {
ports := &[]string{}
for _, p := range strings.Split(*port, ",") {
if strings.Contains(p, "-") {
spl := strings.Split(p, "-")
start, _ := strconv.Atoi(spl[0])
end, _ := strconv.Atoi(spl[1])
for i := start; i <= end; i++ {
*ports = append(*ports, strconv.Itoa(i))
}
} else {
*ports = append(*ports, p)
}
}
return ports
}
func expandHosts(host *string) *[]string {
hosts := &[]string{}
if !strings.Contains(*host, "-") {
*hosts = append(*hosts, *host)
return hosts
}
spl := strings.Split(*host, "-")
sIP := ip2octet(spl[0])
eIP := ip2octet(spl[1])
for {
ip := octet2ip(sIP)
*hosts = append(*hosts, ip)
if equalOctets(sIP, eIP) {
break
}
sIP = incOctet(sIP)
}
return hosts
}
func scan(c <-chan string) {
for hostport := range c {
if hostport == "EOF" {
time.Sleep(*TIMEOUT)
os.Exit(1)
}
isOpen, banner := checkPort(hostport)
if isOpen {
fmt.Printf("%s Open [%s]\n", hostport, banner)
} /*else {
fmt.Printf("%s Closed [%s]\n", hostport, banner)
}*/
}
}
func main() {
port := flag.String("p", "", "ports ex: -p 80,81 ex: -p 0-80")
fullMode := flag.Bool("full", false, "scan all the 65535 ports")
lowMode := flag.Bool("low", false, "scan the 1024 lower ports")
host := flag.String("h", "127.0.0.1", "hosts ex: -h 192.168.1.16-192.168.1.17")
gor := flag.Int("go",5,"goroutines")
TIMEOUT = flag.Duration("t", 4*time.Second, "timeout in seconds")
flag.Parse()
if len(*host) <= 0 ||
(len(*port) <= 0 && !*fullMode && !*lowMode) {
fmt.Println("try -h")
return
}
fmt.Println("buffering ...")
ports := expandPorts(port)
hosts := expandHosts(host)
fmt.Println("scanning ...")
if *lowMode {
for i := 0; i < 1024; i++ {
port := strconv.Itoa(i)
*ports = append(*ports, port)
}
} else if *fullMode {
for i := 0; i < 65535; i++ {
port := strconv.Itoa(i)
*ports = append(*ports, port)
}
}
c := make(chan string, 1)
// feed the channel
go func (hosts []string, ports []string, c chan string, gor int) {
for _, h := range hosts {
for _, p := range ports {
c <- fmt.Sprintf("%s:%s",h,p)
}
}
c <- "EOF"
close(c)
}(*hosts,*ports,c,*gor)
// launch goroutines
for i:=0; i<*gor; i++ {
go scan(c)
}
fmt.Println("waiting ...")
wait()
}