-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.go
226 lines (189 loc) · 4.49 KB
/
scanner.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
package main
import (
"flag"
"fmt"
"net"
"os"
"os/exec"
"os/signal"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/tatsushid/go-fastping"
)
type response struct {
addr *net.IPAddr
rtt time.Duration
}
func main() {
textPtr := flag.String("IP", "127.0.0.1", "IP Address/Domain name. (Required)")
flag.Parse()
if *textPtr == "" {
flag.PrintDefaults()
os.Exit(1)
}
FastScan(*textPtr)
}
//FastScan takes IP as a String
func FastScan(ip string) {
var ipList []string
if strings.Contains(ip, "-") {
ipList = append(ipList, ParseIPSequence(ip)...)
} else {
ipList = []string{ip}
}
for _, i := range ipList {
if osChek(i) {
fmt.Println("scanning started", i)
scanresult := `----------------------------
SCAN RESULTS
----------------------------`
fmt.Println(scanresult)
target := ip
start := time.Now()
activeThreads := 0
doneChannel := make(chan bool)
fmt.Println("Port", "\tStatus", "\tService\t\t")
fmt.Println("----", "\t------", "\t------\t\t")
val := 0
for port := 0; port <= 65535; port++ {
go scanTCPConnection(target, port, doneChannel, &val)
time.Sleep(time.Millisecond)
activeThreads++
}
// Wait for all threads to finish
for activeThreads > 0 {
<-doneChannel
activeThreads--
}
fmt.Println("\n---------------------------")
fmt.Println(val, "Ports are Open")
fmt.Println("Time Took: ", time.Since(start))
} else {
fmt.Println("Not able to Reach the Domain/IP", i)
//os.Exit(1)
}
}
}
func scanTCPConnection(ip string, port int, doneChannel chan bool, val *int) {
_, err := net.DialTimeout("tcp", ip+":"+strconv.Itoa(port),
time.Second*1)
if err == nil {
fmt.Println(port, "\tOpen\t", portShortList[ToString(port)])
*val = *val + 1
}
doneChannel <- true
}
//Ping the Domain and return Bool Value (For Windows)
func resolveIP(host string) bool {
hostname := host
if len(hostname) == 0 {
flag.Usage()
os.Exit(1)
}
p := fastping.NewPinger()
netProto := "ip4:icmp"
if strings.Index(hostname, ":") != -1 {
netProto = "ip6:ipv6-icmp"
}
ra, err := net.ResolveIPAddr(netProto, hostname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
results := make(map[string]*response)
results[ra.String()] = nil
p.AddIPAddr(ra)
onRecv, onIdle := make(chan *response), make(chan bool)
p.OnRecv = func(addr *net.IPAddr, t time.Duration) {
onRecv <- &response{addr: addr, rtt: t}
}
p.OnIdle = func() {
onIdle <- true
}
p.MaxRTT = time.Second
p.RunLoop()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
loop:
for {
select {
case <-c:
fmt.Println("get interrupted")
break loop
case res := <-onRecv:
if _, ok := results[res.addr.String()]; ok {
results[res.addr.String()] = res
}
case <-onIdle:
for _, r := range results {
if r == nil {
return false
}
return true
}
case <-p.Done():
if err = p.Err(); err != nil {
fmt.Println("Ping failed:", err)
}
break loop
}
}
signal.Stop(c)
p.Stop()
return false
}
//ParseIPSequence is Ip scanning
func ParseIPSequence(ipSequence string) []string {
var arrayIps []string
series, _ := regexp.Compile("([0-9]+)")
// For sequence ips, using '-'
lSeries := series.FindAllStringSubmatch(ipSequence, -1)
for i := ToInt(lSeries[3][0]); i <= ToInt(lSeries[4][0]); i++ {
arrayIps = append(arrayIps,
lSeries[0][0]+"."+
lSeries[1][0]+"."+
lSeries[2][0]+"."+ToString(i))
}
//fmt.Println(lSeries[3][0])
// fmt.Println(lSeries[4][0])
return arrayIps
}
//ToInt is to convert into integer
func ToInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}
//ToString is to convert into String
func ToString(s int) string {
i := strconv.Itoa(s)
return i
}
func osChek(ip string) bool {
switch os := runtime.GOOS; os {
case "linux":
return pingIP(ip)
case "darwin":
return pingIP(ip)
default:
fmt.Println("Not supported for the OS")
return resolveIP(ip)
}
}
func pingIP(ip string) bool {
Command := fmt.Sprintf("ping -c 1 " + ip + " > /dev/null && echo true || echo false")
output, err := exec.Command("/bin/sh", "-c", Command).Output()
test := string(output)
if strings.Contains(test, "true") {
return true
}
if err != nil {
fmt.Println("found error")
return false
}
return false
}