-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathshow-long-connections.go
169 lines (148 loc) · 4.33 KB
/
show-long-connections.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
package commands
import (
"fmt"
"os"
"strings"
"time"
"github.com/activecm/rita-legacy/pkg/uconn"
"github.com/activecm/rita-legacy/resources"
"github.com/activecm/rita-legacy/util"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)
func init() {
command := cli.Command{
Name: "show-long-connections",
Usage: "Print long connections and relevant information",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
limitFlag,
noLimitFlag,
delimFlag,
netNamesFlag,
},
Action: func(c *cli.Context) error {
db := c.Args().Get(0)
if db == "" {
return cli.NewExitError("Specify a database", -1)
}
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)
thresh := 60 // 1 minute
data, err := uconn.LongConnResults(res, thresh, c.Int("limit"), c.Bool("no-limit"))
if err != nil {
res.Log.Error(err)
return cli.NewExitError(err, -1)
}
if !(len(data) > 0) {
return cli.NewExitError("No results were found for "+db, -1)
}
if c.Bool("human-readable") {
err := showConnsHuman(data, c.Bool("network-names"))
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
return nil
}
err = showConns(data, c.String("delimiter"), c.Bool("network-names"))
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
return nil
},
}
bootstrapCommands(command)
}
func showConns(connResults []uconn.LongConnResult, delim string, showNetNames bool) error {
var headerFields []string
if showNetNames {
headerFields = []string{"Source Network", "Destination Network", "Source IP", "Destination IP", "Port:Protocol:Service", "Total Duration", "Longest Duration", "Connections", "Total Bytes", "State"}
} else {
headerFields = []string{"Source IP", "Destination IP", "Port:Protocol:Service", "Total Duration", "Longest Duration", "Connections", "Total Bytes", "State"}
}
// Print the headers and analytic values, separated by a delimiter
fmt.Println(strings.Join(headerFields, delim))
for _, result := range connResults {
var row []string
// Convert the true/false open/closed state to a nice string
state := "closed"
if result.Open {
state = "open"
}
if showNetNames {
row = []string{
result.SrcNetworkName,
result.DstNetworkName,
result.SrcIP,
result.DstIP,
strings.Join(result.Tuples, " "),
f(result.TotalDuration),
f(result.MaxDuration),
i(result.ConnectionCount),
i(result.TotalBytes),
state,
}
} else {
row = []string{
result.SrcIP,
result.DstIP,
strings.Join(result.Tuples, " "),
f(result.TotalDuration),
f(result.MaxDuration),
i(result.ConnectionCount),
i(result.TotalBytes),
state,
}
}
fmt.Println(strings.Join(row, delim))
}
return nil
}
func showConnsHuman(connResults []uconn.LongConnResult, showNetNames bool) error {
table := tablewriter.NewWriter(os.Stdout)
var headerFields []string
if showNetNames {
headerFields = []string{"Source Network", "Destination Network", "Source IP", "Destination IP", "Port:Protocol:Service", "Total Duration", "Longest Duration", "Connections", "Total Bytes", "State"}
} else {
headerFields = []string{"Source IP", "Destination IP", "Port:Protocol:Service", "Total Duration", "Longest Duration", "Connections", "Total Bytes", "State"}
}
table.SetHeader(headerFields)
for _, result := range connResults {
var row []string
// Convert the true/false open/closed state to a nice string
state := "closed"
if result.Open {
state = "open"
}
if showNetNames {
row = []string{
result.SrcNetworkName,
result.DstNetworkName,
result.SrcIP,
result.DstIP,
strings.Join(result.Tuples, " "),
util.FormatDuration(time.Duration(int(result.TotalDuration * float64(time.Second)))),
util.FormatDuration(time.Duration(int(result.MaxDuration * float64(time.Second)))),
i(result.ConnectionCount),
i(result.TotalBytes),
state,
}
} else {
row = []string{
result.SrcIP,
result.DstIP,
strings.Join(result.Tuples, " "),
util.FormatDuration(time.Duration(int(result.TotalDuration * float64(time.Second)))),
util.FormatDuration(time.Duration(int(result.MaxDuration * float64(time.Second)))),
i(result.ConnectionCount),
i(result.TotalBytes),
state,
}
}
table.Append(row)
}
table.Render()
return nil
}