-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
132 lines (104 loc) · 2.89 KB
/
client.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
package main
import (
"flag"
"fmt"
"log"
"net"
"strconv"
"strings"
"sync"
"time"
)
func ClientSubCommand() *clientCommand {
gc := &clientCommand{
fs: flag.NewFlagSet("client", flag.ContinueOnError),
}
gc.fs.IntVar(&gc.timeout, "timeout", 3, "Time before a connection is seen as invalid (in seconds)")
gc.fs.IntVar(&gc.maxThreads, "threads", 500, "Number of threads")
gc.fs.BoolVar(&gc.verbose, "verbose", false, "Display more output")
gc.fs.StringVar(&gc.portRange, "range", "1-1024", "Range of TCP ports to check, e.g 1-1024")
gc.fs.StringVar(&gc.address, "address", "", "Address of egressbuster server")
return gc
}
type clientCommand struct {
fs *flag.FlagSet
timeout int
maxThreads int
verbose bool
portRange string
address string
lowPort int
highPort int
}
func (g *clientCommand) PrintUsage() {
g.fs.Usage()
}
func (g *clientCommand) Name() string {
return g.fs.Name()
}
func (g *clientCommand) Init(args []string) error {
err := g.fs.Parse(args)
if err != nil {
return err
}
portRange := strings.Split(g.portRange, "-")
if len(portRange) != 2 {
log.Fatal("Invalid port range specified, should be in the format of 1-65536")
}
g.lowPort, err = strconv.Atoi(portRange[0])
if err != nil {
log.Fatal("Could not parse low port: ", err)
}
g.highPort, err = strconv.Atoi(portRange[1])
if err != nil {
log.Fatal("Could not parse high port: ", err)
}
if g.lowPort > g.highPort {
log.Fatal("Lowport is bigger than high port, invalid")
}
if g.highPort > 65536 {
log.Println("Highport is greater than max number of ports, limiting to TCP 65536")
g.highPort = 65536
}
_, _, err = net.ParseCIDR(g.address)
if err != nil {
if net.ParseIP(g.address) == nil {
log.Fatal("Could not parse Ip address or range from: ", g.address)
}
}
return nil
}
func (g *clientCommand) Run() error {
limit := make(chan bool, g.maxThreads)
log.Printf("[i] Sending packets to egress listener (%s)...\n", g.address)
log.Printf("[i] Starting at: %d/tcp, ending at: %d/tcp\n", g.lowPort, g.highPort)
var wg sync.WaitGroup
for currentPort := g.lowPort; currentPort <= g.highPort; currentPort++ {
limit <- true
wg.Add(1)
go func(currentPort int) {
defer func() {
<-limit
wg.Done()
}()
if g.verbose || currentPort%1000 == 0 {
log.Println("[i] Trying: TCP ", currentPort)
}
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", g.address, currentPort), time.Duration(g.timeout)*time.Second)
if err != nil {
return
}
defer conn.Close()
msg := fmt.Sprintf("egressor:%d", currentPort)
_, err = conn.Write([]byte(msg))
if err != nil {
log.Printf("[+] Initial connection worked on port %d/tcp however sending message failed, %s\n", currentPort, err.Error())
return
}
log.Printf("[*] Connection made to %s on port: %d/tcp\n", g.address, currentPort)
}(currentPort)
}
wg.Wait()
log.Println("Finished!")
return nil
}