-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathshow-bl-ip.go
252 lines (222 loc) · 7.21 KB
/
show-bl-ip.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package commands
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/activecm/rita-legacy/pkg/blacklist"
"github.com/activecm/rita-legacy/resources"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)
func init() {
blSourceIPs := cli.Command{
Name: "show-bl-source-ips",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
blConnFlag,
blSortFlag,
limitFlag,
noLimitFlag,
delimFlag,
netNamesFlag,
},
Usage: "Print blacklisted IPs which initiated connections",
Action: printBLSourceIPs,
}
blDestIPs := cli.Command{
Name: "show-bl-dest-ips",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
blConnFlag,
blSortFlag,
limitFlag,
noLimitFlag,
delimFlag,
netNamesFlag,
},
Usage: "Print blacklisted IPs which received connections",
Action: printBLDestIPs,
}
bootstrapCommands(blSourceIPs, blDestIPs)
}
func parseBLArgs(c *cli.Context) (string, string, bool, bool, bool, error) {
db := c.Args().Get(0)
sort := c.String("sort")
connected := c.Bool("connected")
human := c.Bool("human-readable")
showNetNames := c.Bool("network-names")
var err error
if db == "" {
err = cli.NewExitError("Specify a database", -1)
} else if sort != "conn_count" && sort != "total_bytes" {
err = cli.NewExitError("Invalid option passed to sort flag", -1)
}
return db, sort, connected, human, showNetNames, err
}
func printBLSourceIPs(c *cli.Context) error {
db, sort, connected, human, showNetNames, err := parseBLArgs(c)
if err != nil {
return err
}
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)
data, err := blacklist.SrcIPResults(res, sort, 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 human {
err = showBLIPsHuman(data, connected, showNetNames, true)
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
} else {
err = showBLIPs(data, connected, showNetNames, true, c.String("delimiter"))
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
}
return nil
}
func printBLDestIPs(c *cli.Context) error {
db, sort, connected, human, showNetNames, err := parseBLArgs(c)
if err != nil {
return err
}
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)
data, err := blacklist.DstIPResults(res, sort, 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 human {
err = showBLIPsHuman(data, connected, showNetNames, false)
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
} else {
err = showBLIPs(data, connected, showNetNames, false, c.String("delimiter"))
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
}
return nil
}
func showBLIPs(ips []blacklist.IPResult, connectedHosts, showNetNames, source bool, delim string) error {
var headerFields []string
if !showNetNames && !connectedHosts {
headerFields = []string{"IP", "Connections", "Unique Connections", "Total Bytes"}
} else if showNetNames && !connectedHosts {
headerFields = []string{"IP", "Network", "Connections", "Unique Connections", "Total Bytes"}
} else if !showNetNames && connectedHosts && source {
headerFields = []string{"IP", "Connections", "Unique Connections", "Total Bytes", "Destinations"}
} else if !showNetNames && connectedHosts && !source {
headerFields = []string{"IP", "Connections", "Unique Connections", "Total Bytes", "Sources"}
} else if showNetNames && connectedHosts && source {
headerFields = []string{"IP", "Network", "Connections", "Unique Connections", "Total Bytes", "Destinations"}
} else if showNetNames && connectedHosts && !source {
headerFields = []string{"IP", "Network", "Connections", "Unique Connections", "Total Bytes", "Sources"}
}
// Print the headerFields and analytic values, separated by a delimiter
fmt.Println(strings.Join(headerFields, delim))
for _, entry := range ips {
var serialized []string
if showNetNames {
serialized = []string{entry.Host.IP, entry.Host.NetworkName}
} else {
serialized = []string{entry.Host.IP}
}
serialized = append(serialized,
strconv.Itoa(entry.Connections),
strconv.Itoa(entry.UniqueConnections),
strconv.Itoa(entry.TotalBytes),
)
if connectedHosts {
var connectedHostsIPs []string
if showNetNames {
for _, connectedUniqIP := range entry.Peers {
escapedNetName := strings.ReplaceAll(connectedUniqIP.NetworkName, " ", "_")
escapedNetName = strings.ReplaceAll(escapedNetName, ":", "_")
connectedIPStr := escapedNetName + ":" + connectedUniqIP.IP
connectedHostsIPs = append(connectedHostsIPs, connectedIPStr)
}
} else {
for _, connectedUniqIP := range entry.Peers {
connectedHostsIPs = append(connectedHostsIPs, connectedUniqIP.IP)
}
}
sort.Strings(connectedHostsIPs)
serialized = append(serialized, strings.Join(connectedHostsIPs, " "))
}
fmt.Println(
strings.Join(
serialized,
delim,
),
)
}
return nil
}
func showBLIPsHuman(ips []blacklist.IPResult, connectedHosts, showNetNames, source bool) error {
table := tablewriter.NewWriter(os.Stdout)
var headerFields []string
if !showNetNames && !connectedHosts {
headerFields = []string{"IP", "Connections", "Unique Connections", "Total Bytes"}
} else if showNetNames && !connectedHosts {
headerFields = []string{"IP", "Network", "Connections", "Unique Connections", "Total Bytes"}
} else if !showNetNames && connectedHosts && source {
headerFields = []string{"IP", "Connections", "Unique Connections", "Total Bytes", "Destinations"}
} else if !showNetNames && connectedHosts && !source {
headerFields = []string{"IP", "Connections", "Unique Connections", "Total Bytes", "Sources"}
} else if showNetNames && connectedHosts && source {
headerFields = []string{"IP", "Network", "Connections", "Unique Connections", "Total Bytes", "Destinations"}
} else if showNetNames && connectedHosts && !source {
headerFields = []string{"IP", "Network", "Connections", "Unique Connections", "Total Bytes", "Sources"}
}
table.SetHeader(headerFields)
for _, entry := range ips {
var serialized []string
if showNetNames {
serialized = []string{entry.Host.IP, entry.Host.NetworkName}
} else {
serialized = []string{entry.Host.IP}
}
serialized = append(serialized,
strconv.Itoa(entry.Connections),
strconv.Itoa(entry.UniqueConnections),
strconv.Itoa(entry.TotalBytes),
)
if connectedHosts {
var connectedHostsIPs []string
for _, connectedUniqIP := range entry.Peers {
var connectedIPStr string
if showNetNames {
escapedNetName := strings.ReplaceAll(connectedUniqIP.NetworkName, " ", "_")
escapedNetName = strings.ReplaceAll(escapedNetName, ":", "_")
connectedIPStr = escapedNetName + ":" + connectedUniqIP.IP
} else {
connectedIPStr = connectedUniqIP.IP
}
connectedHostsIPs = append(connectedHostsIPs, connectedIPStr)
}
sort.Strings(connectedHostsIPs)
serialized = append(serialized, strings.Join(connectedHostsIPs, " "))
}
table.Append(serialized)
}
table.Render()
return nil
}