Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make --config a global option on rita command #631

Merged
merged 6 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var (

// below are some prebuilt flags that get used often in various commands

// configFlag allows users to specify an alternate config file to use
configFlag = cli.StringFlag{
// ConfigFlag allows users to specify an alternate config file to use
ConfigFlag = cli.StringFlag{
Name: "config, c",
Usage: "Use a given `CONFIG_FILE` when running this command",
Value: "",
Expand Down Expand Up @@ -130,13 +130,34 @@ var (
}
)

// SetConfigFilePath reads config file path from cli context and stores it in app metadata
// to make it available to all subcommands. The root command and all subcommands use this.
// If --config flag is supplied for both root command and a subcommand, the value of the
// subcommand is used.
func SetConfigFilePath(c *cli.Context) error {
if configFilePath := c.String("config"); configFilePath != "" {
c.App.Metadata["config"] = configFilePath
}
return nil
}

// getConfigFilePath returns config file path from app metadata
func getConfigFilePath(c *cli.Context) string {
switch cfg := c.App.Metadata["config"].(type) {
case string:
return cfg
default:
return ""
}
}

// bootstrapCommands simply adds a given command to the allCommands array
func bootstrapCommands(commands ...cli.Command) {
for _, command := range commands {
command.Before = func(c *cli.Context) error {
//Get access to the logger
configFile := c.String("config")
res := resources.InitResources(configFile)
SetConfigFilePath(c)
res := resources.InitResources(getConfigFilePath(c))
//Display args in logs
fields := log.Fields{
"Arguments": c.Args(),
Expand Down
4 changes: 2 additions & 2 deletions commands/delete-database.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func init() {
Usage: "Delete imported database(s)",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
forceFlag,
configFlag,
allFlag,
matchFlag,
regexFlag,
Expand All @@ -35,7 +35,7 @@ func init() {

//deleteDatabase deletes a target database
func deleteDatabase(c *cli.Context) error {
res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))

// Different command flags
tgt := c.Args().Get(0)
Expand Down
6 changes: 3 additions & 3 deletions commands/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func init() {
"Logs directly in <import directory> will be imported into a database" +
" named <database name>.",
Flags: []cli.Flag{
ConfigFlag,
threadFlag,
configFlag,
deleteFlag,
rollingFlag,
totalChunksFlag,
Expand All @@ -32,7 +32,7 @@ func init() {
Action: func(c *cli.Context) error {
importer := NewImporter(c)
err := importer.run()
fmt.Println(updateCheck(c.String("config")))
fmt.Println(updateCheck(getConfigFilePath(c)))
return err
},
}
Expand All @@ -59,7 +59,7 @@ type (
//NewImporter ....
func NewImporter(c *cli.Context) *Importer {
return &Importer{
configFile: c.String("config"),
configFile: getConfigFilePath(c),
args: c.Args(),
deleteOldData: c.Bool("delete"),
userRolling: c.Bool("rolling"),
Expand Down
4 changes: 2 additions & 2 deletions commands/reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func init() {
UsageText: "rita html-report [command-options] [database]\n\n" +
"If no database is specified, a report will be created for every database.",
Flags: []cli.Flag{
configFlag,
ConfigFlag,
netNamesFlag,
noBrowserFlag,
},
Action: func(c *cli.Context) error {
res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
databaseName := c.Args().Get(0)
var databases []string
if databaseName != "" {
Expand Down
4 changes: 2 additions & 2 deletions commands/show-beacons-fqdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func init() {
Usage: "Print hosts which show signs of C2 software (FQDN Analysis)",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
configFlag,
delimFlag,
netNamesFlag,
},
Expand All @@ -33,7 +33,7 @@ func showBeaconsFQDN(c *cli.Context) error {
if db == "" {
return cli.NewExitError("Specify a database", -1)
}
res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

data, err := beaconfqdn.Results(res, 0)
Expand Down
4 changes: 2 additions & 2 deletions commands/show-beacons.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func init() {
Usage: "Print hosts which show signs of C2 software",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
configFlag,
delimFlag,
netNamesFlag,
},
Expand All @@ -33,7 +33,7 @@ func showBeacons(c *cli.Context) error {
if db == "" {
return cli.NewExitError("Specify a database", -1)
}
res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

data, err := beacon.Results(res, 0)
Expand Down
4 changes: 2 additions & 2 deletions commands/show-bl-hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func init() {
Name: "show-bl-hostnames",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
configFlag,
limitFlag,
noLimitFlag,
delimFlag,
Expand All @@ -40,7 +40,7 @@ func printBLHostnames(c *cli.Context) error {
return cli.NewExitError("Specify a database", -1)
}

res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

data, err := blacklist.HostnameResults(res, "conn_count", c.Int("limit"), c.Bool("no-limit"))
Expand Down
8 changes: 4 additions & 4 deletions commands/show-bl-ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func init() {
Name: "show-bl-source-ips",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
blConnFlag,
blSortFlag,
configFlag,
limitFlag,
noLimitFlag,
delimFlag,
Expand All @@ -35,10 +35,10 @@ func init() {
Name: "show-bl-dest-ips",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
blConnFlag,
blSortFlag,
configFlag,
limitFlag,
noLimitFlag,
delimFlag,
Expand Down Expand Up @@ -71,7 +71,7 @@ func printBLSourceIPs(c *cli.Context) error {
if err != nil {
return err
}
res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

data, err := blacklist.SrcIPResults(res, sort, c.Int("limit"), c.Bool("no-limit"))
Expand Down Expand Up @@ -105,7 +105,7 @@ func printBLDestIPs(c *cli.Context) error {
return err
}

res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

data, err := blacklist.DstIPResults(res, sort, c.Int("limit"), c.Bool("no-limit"))
Expand Down
4 changes: 2 additions & 2 deletions commands/show-databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func init() {
Aliases: []string{"show-databases"},
Usage: "Print the databases currently stored",
Flags: []cli.Flag{
configFlag,
ConfigFlag,
},
Action: func(c *cli.Context) error {
res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))

if res != nil {
for _, name := range res.MetaDB.GetDatabases() {
Expand Down
4 changes: 2 additions & 2 deletions commands/show-explodedDns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func init() {
Usage: "Print dns analysis. Exposes covert dns channels",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
configFlag,
limitFlag,
noLimitFlag,
delimFlag,
Expand All @@ -31,7 +31,7 @@ func init() {
return cli.NewExitError("Specify a database", -1)
}

res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

data, err := explodeddns.Results(res, c.Int("limit"), c.Bool("no-limit"))
Expand Down
4 changes: 2 additions & 2 deletions commands/show-long-connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func init() {
Usage: "Print long connections and relevant information",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
configFlag,
limitFlag,
noLimitFlag,
delimFlag,
Expand All @@ -32,7 +32,7 @@ func init() {
return cli.NewExitError("Specify a database", -1)
}

res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

thresh := 60 // 1 minute
Expand Down
4 changes: 2 additions & 2 deletions commands/show-strobes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func init() {
Usage: "Print strobe information",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
cli.BoolFlag{
Name: "connection-count, l",
Usage: "Sort the strobes by largest connection count.",
},
configFlag,
limitFlag,
noLimitFlag,
delimFlag,
Expand All @@ -35,7 +35,7 @@ func init() {
return cli.NewExitError("Specify a database", -1)
}

res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

sortDirection := -1
Expand Down
4 changes: 2 additions & 2 deletions commands/show-user-agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func init() {
Usage: "Print user agent information",
ArgsUsage: "<database>",
Flags: []cli.Flag{
ConfigFlag,
humanFlag,
cli.BoolFlag{
Name: "least-used, l",
Usage: "Sort the user agents from least used to most used.",
},
configFlag,
limitFlag,
noLimitFlag,
delimFlag,
Expand All @@ -34,7 +34,7 @@ func init() {
return cli.NewExitError("Specify a database", -1)
}

res := resources.InitResources(c.String("config"))
res := resources.InitResources(getConfigFilePath(c))
res.DB.SelectDB(db)

sortDirection := 1
Expand Down
13 changes: 4 additions & 9 deletions commands/test-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ import (

func init() {
command := cli.Command{
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "specify a config file to be used",
Value: "",
},
},
Flags: []cli.Flag{ConfigFlag},
Name: "test-config",
Usage: "Check the configuration file for validity",
Before: SetConfigFilePath,
Action: testConfiguration,
}

Expand All @@ -31,7 +26,7 @@ func init() {
// testConfiguration prints out the result of parsing the config file
func testConfiguration(c *cli.Context) error {
// First, print out the config as it was parsed
conf, err := config.LoadConfig(c.String("config"))
conf, err := config.LoadConfig(getConfigFilePath(c))
if err != nil {
fmt.Fprintf(os.Stdout, "Failed to config: %s\n", err.Error())
os.Exit(-1)
Expand All @@ -51,7 +46,7 @@ func testConfiguration(c *cli.Context) error {
fmt.Fprintf(os.Stdout, "\n%s\n", string(tableConfig))

// Then test initializing external resources like db connection and file handles
resources.InitResources(c.String("config"))
resources.InitResources(getConfigFilePath(c))

return nil
}
2 changes: 1 addition & 1 deletion commands/update-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var versions = []string{"Major", "Minor", "Patch"}
func GetVersionPrinter() func(*cli.Context) {
return func(c *cli.Context) {
fmt.Printf("%s version %s\n", c.App.Name, c.App.Version)
fmt.Println(updateCheck(c.String("config")))
fmt.Println(updateCheck(getConfigFilePath(c)))
}
}

Expand Down
2 changes: 2 additions & 0 deletions rita.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
app := cli.NewApp()
app.Name = "rita"
app.Usage = "Look for evil needles in big haystacks."
app.Flags = []cli.Flag{commands.ConfigFlag}

cli.VersionPrinter = commands.GetVersionPrinter()

Expand All @@ -24,6 +25,7 @@ func main() {

// Define commands used with this application
app.Commands = commands.Commands()
app.Before = commands.SetConfigFilePath

runtime.GOMAXPROCS(runtime.NumCPU())
app.Run(os.Args)
Expand Down