-
Notifications
You must be signed in to change notification settings - Fork 0
/
jasn.go
172 lines (145 loc) · 3.04 KB
/
jasn.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
package main
import (
"errors"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"github.com/malfunkt/iprange"
"github.com/urfave/cli/v2"
)
type scanParams struct {
ipRange string
ipList []net.IP
portRange string
portList []int
timeout int
concurrency int
}
func main() {
app := &cli.App{
Name: "jasn port scanner",
Version: "v0.1",
Usage: "scan the port in given ip range, example -i 192.0.2.0/24 -p 22,80-139",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "iplist",
Aliases: []string{"i"},
Usage: "ip list",
Value: "",
},
&cli.StringFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "port list",
Value: "",
},
&cli.IntFlag{
Name: "timeout",
Aliases: []string{"t"},
Usage: "timeout",
Value: 3,
},
&cli.IntFlag{
Name: "concurrency",
Aliases: []string{"c"},
Usage: "concurrency",
Value: 1000,
},
},
Action: doScan,
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func doScan(ctx *cli.Context) error {
sp, err := parseArgs(ctx)
if err != nil {
return err
}
sp.portList, err = getPorts(sp.portRange)
if err != nil {
return err
}
sp.ipList, err = getIpList(sp.ipRange)
if err != nil {
return err
}
log.Println("Begin Scan")
log.Println(strings.Repeat("*", 100))
beginScan(sp)
log.Println(strings.Repeat("*", 100))
log.Println("End Scan")
return nil
}
func parseArgs(ctx *cli.Context) (scanParams, error) {
sp := scanParams{
portRange: "22,23,53,80-139",
timeout: 3,
concurrency: 1000,
}
if ctx.IsSet("iplist") {
sp.ipRange = ctx.String("iplist")
} else {
return sp, errors.New("iplist is required")
}
if ctx.IsSet("port") {
sp.portRange = ctx.String("port")
}
if ctx.IsSet("timeout") {
sp.timeout = ctx.Int("timeout")
}
if ctx.IsSet("concurrency") {
sp.concurrency = ctx.Int("concurrency")
}
return sp, nil
}
func getPorts(selection string) ([]int, error) {
ports := []int{}
if selection == "" {
return ports, nil
}
ranges := strings.Split(selection, ",")
for _, r := range ranges {
r = strings.TrimSpace(r)
if strings.Contains(r, "-") {
parts := strings.Split(r, "-")
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid port selection segment: '%s'", r)
}
p1, err := strconv.Atoi(parts[0])
if err != nil {
return nil, fmt.Errorf("Invalid port number: '%s'", parts[0])
}
p2, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("Invalid port number: '%s'", parts[1])
}
if p1 > p2 {
return nil, fmt.Errorf("Invalid port range: %d-%d", p1, p2)
}
for i := p1; i <= p2; i++ {
ports = append(ports, i)
}
} else {
if port, err := strconv.Atoi(r); err != nil {
return nil, fmt.Errorf("Invalid port number: '%s'", r)
} else {
ports = append(ports, port)
}
}
}
return ports, nil
}
func getIpList(ips string) ([]net.IP, error) {
addressList, err := iprange.ParseList(ips)
if err != nil {
return nil, err
}
list := addressList.Expand()
return list, err
}