2020-07-16 13:59:09 +00:00
|
|
|
package main
|
|
|
|
|
2020-09-01 12:32:45 +00:00
|
|
|
import (
|
2020-11-30 09:37:17 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-01 12:32:45 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-03-10 12:55:25 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-10-07 09:05:35 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-09-01 12:32:45 +00:00
|
|
|
)
|
|
|
|
|
2022-03-10 12:55:25 +00:00
|
|
|
func printHelp(cmd *cobra.Command) {
|
2023-11-28 22:51:51 +00:00
|
|
|
if err := cmd.Help(); err != nil {
|
2022-04-13 13:44:23 +00:00
|
|
|
log.Fatalf("unable to print help(): %s", err)
|
2022-03-10 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *string) error {
|
|
|
|
/*if a range is provided, change the scope*/
|
|
|
|
if *ipRange != "" {
|
|
|
|
_, _, err := net.ParseCIDR(*ipRange)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s isn't a valid range", *ipRange)
|
|
|
|
}
|
|
|
|
}
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
if *ip != "" {
|
|
|
|
ipRepr := net.ParseIP(*ip)
|
|
|
|
if ipRepr == nil {
|
|
|
|
return fmt.Errorf("%s isn't a valid ip", *ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:36:21 +00:00
|
|
|
// avoid confusion on scope (ip vs Ip and range vs Range)
|
2022-04-19 10:07:35 +00:00
|
|
|
switch strings.ToLower(*scope) {
|
|
|
|
case "ip":
|
|
|
|
*scope = types.Ip
|
|
|
|
case "range":
|
|
|
|
*scope = types.Range
|
|
|
|
case "country":
|
|
|
|
*scope = types.Country
|
|
|
|
case "as":
|
|
|
|
*scope = types.AS
|
|
|
|
}
|
2024-02-01 21:36:21 +00:00
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 15:50:02 +00:00
|
|
|
func removeFromSlice(val string, slice []string) []string {
|
|
|
|
var i int
|
|
|
|
var value string
|
|
|
|
|
|
|
|
valueFound := false
|
|
|
|
|
|
|
|
// get the index
|
|
|
|
for i, value = range slice {
|
|
|
|
if value == val {
|
|
|
|
valueFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if valueFound {
|
|
|
|
slice[i] = slice[len(slice)-1]
|
|
|
|
slice[len(slice)-1] = ""
|
|
|
|
slice = slice[:len(slice)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return slice
|
|
|
|
}
|