-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
root.go
181 lines (150 loc) · 4.5 KB
/
root.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
package cmd
import (
"context"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"time"
"github.com/liamg/furious/scan"
"github.com/liamg/furious/version"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var debug bool
var timeoutMS int = 2000
var parallelism int = 500
var portSelection string
var scanType = "stealth"
var hideUnavailableHosts bool
var versionRequested bool
func init() {
rootCmd.PersistentFlags().BoolVarP(&hideUnavailableHosts, "up-only", "u", hideUnavailableHosts, "Omit output for hosts which are not up")
rootCmd.PersistentFlags().BoolVarP(&versionRequested, "version", "", versionRequested, "Output version information and exit")
rootCmd.PersistentFlags().StringVarP(&scanType, "scan-type", "s", scanType, "Scan type. Must be one of stealth, connect")
rootCmd.PersistentFlags().BoolVarP(&debug, "verbose", "v", debug, "Enable verbose logging")
rootCmd.PersistentFlags().IntVarP(&timeoutMS, "timeout-ms", "t", timeoutMS, "Scan timeout in MS")
rootCmd.PersistentFlags().IntVarP(¶llelism, "workers", "w", parallelism, "Parallel routines to scan on")
rootCmd.PersistentFlags().StringVarP(&portSelection, "ports", "p", portSelection, "Port to scan. Comma separated, can sue hyphens e.g. 22,80,443,8080-8090")
}
func createScanner(ti *scan.TargetIterator, scanTypeStr string, timeout time.Duration, routines int) (scan.Scanner, error) {
switch strings.ToLower(scanTypeStr) {
case "stealth", "syn", "fast":
if os.Geteuid() > 0 {
return nil, fmt.Errorf("Access Denied: You must be a priviliged user to run this type of scan.")
}
return scan.NewSynScanner(ti, timeout, routines), nil
case "connect":
return scan.NewConnectScanner(ti, timeout, routines), nil
case "device":
return scan.NewDeviceScanner(ti, timeout), nil
}
return nil, fmt.Errorf("Unknown scan type '%s'", scanTypeStr)
}
var rootCmd = &cobra.Command{
Use: "furious",
Short: "Furious is a IP/port scanner",
Long: `An IP/port scanner for identifying hosts/services remotely.`,
Run: func(cmd *cobra.Command, args []string) {
if versionRequested {
v := version.Version
if v == "" {
v = "development version"
}
fmt.Printf("furious %s\n", v)
return
}
if debug {
log.SetLevel(log.DebugLevel)
}
if len(args) == 0 {
fmt.Println("Please specify a target")
os.Exit(1)
}
ports, err := getPorts(portSelection)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("Scan cancelled. Requesting stop...")
cancel()
}()
startTime := time.Now()
fmt.Printf("\nStarting scan at %s\n\n", startTime.String())
for _, target := range args {
targetIterator := scan.NewTargetIterator(target)
// creating scanner
scanner, err := createScanner(targetIterator, scanType, time.Millisecond*time.Duration(timeoutMS), parallelism)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
log.Debugf("Starting scanner...")
if err := scanner.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
}
log.Debugf("Scanning target %s...", target)
results, err := scanner.Scan(ctx, ports)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, result := range results {
if !hideUnavailableHosts || result.IsHostUp() {
scanner.OutputResult(result)
}
}
}
fmt.Printf("Scan complete in %s.\n", time.Since(startTime).String())
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func getPorts(selection string) ([]int, error) {
if selection == "" {
return scan.DefaultPorts, nil
}
ports := []int{}
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
}