| 1 | + | package cmd |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "fmt" |
| 5 | + | "os" |
| 6 | + | "strings" |
| 7 | + | "sync" |
| 8 | + | |
| 9 | + | "github.com/j3ssie/metabigor/core" |
| 10 | + | "github.com/j3ssie/metabigor/modules" |
| 11 | + | "github.com/spf13/cobra" |
| 12 | + | ) |
| 13 | + | |
| 14 | + | func init() { |
| 15 | + | var cveCmd = &cobra.Command{ |
| 16 | + | Use: "cve", |
| 17 | + | Short: "CVE or Advisory Search", |
| 18 | + | Long: fmt.Sprintf(`Metabigor - Intelligence Tool but without API key - %v by %v`, core.VERSION, core.AUTHOR), |
| 19 | + | RunE: runCVE, |
| 20 | + | } |
| 21 | + | |
| 22 | + | cveCmd.Flags().StringP("source", "s", "all", "Search Engine Select") |
| 23 | + | cveCmd.Flags().StringSliceP("query", "q", []string{}, "Query to search (Multiple -q flags are accepted)") |
| 24 | + | RootCmd.AddCommand(cveCmd) |
| 25 | + | } |
| 26 | + | |
| 27 | + | func runCVE(cmd *cobra.Command, _ []string) error { |
| 28 | + | options.Search.Source, _ = cmd.Flags().GetString("source") |
| 29 | + | options.Search.Source = strings.ToLower(options.Search.Source) |
| 30 | + | queries, _ := cmd.Flags().GetStringSlice("query") |
| 31 | + | |
| 32 | + | var inputs []string |
| 33 | + | if options.Input != "-" && options.Input != "" { |
| 34 | + | if strings.Contains(options.Input, "\n") { |
| 35 | + | inputs = strings.Split(options.Input, "\n") |
| 36 | + | } else { |
| 37 | + | inputs = append(inputs, options.Input) |
| 38 | + | } |
| 39 | + | } |
| 40 | + | if len(queries) > 0 { |
| 41 | + | inputs = append(inputs, queries...) |
| 42 | + | } |
| 43 | + | if len(inputs) == 0 { |
| 44 | + | core.ErrorF("No input found") |
| 45 | + | os.Exit(1) |
| 46 | + | } |
| 47 | + | |
| 48 | + | |
| 49 | + | var wg sync.WaitGroup |
| 50 | + | jobs := make(chan string) |
| 51 | + | |
| 52 | + | for i := 0; i < options.Concurrency; i++ { |
| 53 | + | wg.Add(1) |
| 54 | + | go func() { |
| 55 | + | defer wg.Done() |
| 56 | + | // do real stuff here |
| 57 | + | for job := range jobs { |
| 58 | + | searchResult := runCVESingle(job, options) |
| 59 | + | StoreData(searchResult, options) |
| 60 | + | } |
| 61 | + | }() |
| 62 | + | } |
| 63 | + | |
| 64 | + | for _, input := range inputs { |
| 65 | + | jobs <- input |
| 66 | + | } |
| 67 | + | |
| 68 | + | close(jobs) |
| 69 | + | wg.Wait() |
| 70 | + | |
| 71 | + | if !core.FileExists(options.Output) { |
| 72 | + | core.ErrorF("No data found") |
| 73 | + | } |
| 74 | + | core.DebugF("Unique Output: %v", options.Output) |
| 75 | + | core.Unique(options.Output) |
| 76 | + | return nil |
| 77 | + | } |
| 78 | + | |
| 79 | + | func runCVESingle(input string, options core.Options) []string { |
| 80 | + | var data []string |
| 81 | + | core.BannerF(fmt.Sprintf("Search on %v for: ", options.Search.Source), input) |
| 82 | + | if options.Search.Source == "all" { |
| 83 | + | options.Search.Source = "vulners" |
| 84 | + | } |
| 85 | + | options.Search.Query = input |
| 86 | + | |
| 87 | + | // select source |
| 88 | + | if strings.Contains(options.Search.Source, "vulner") { |
| 89 | + | data = append(data, modules.Vulners(options)...) |
| 90 | + | } |
| 91 | + | return data |
| 92 | + | } |
| 93 | + | |