|
| 1 | +// Copyright (c) 2025 Valentin Lobstein (Chocapikk) <balgogan@protonmail.com> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 | +// this software and associated documentation files (the "Software"), to deal in |
| 5 | +// the Software without restriction, including without limitation the rights to |
| 6 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 7 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 8 | +// subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in all |
| 11 | +// copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + |
| 20 | +package cmd |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + |
| 25 | + "github.com/Chocapikk/wpprobe/internal/scanner" |
| 26 | + "github.com/Chocapikk/wpprobe/internal/search" |
| 27 | + "github.com/Chocapikk/wpprobe/internal/wordfence" |
| 28 | + "github.com/charmbracelet/lipgloss/tree" |
| 29 | + "github.com/spf13/cobra" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + flagCVE string |
| 34 | + flagPlugin string |
| 35 | + flagTitle string |
| 36 | + flagSeverity string |
| 37 | + flagAuth string |
| 38 | + showDetails bool |
| 39 | +) |
| 40 | + |
| 41 | +var searchCmd = &cobra.Command{ |
| 42 | + Use: "search", |
| 43 | + Short: "Search vulnerabilities by various filters", |
| 44 | + RunE: runSearch, |
| 45 | +} |
| 46 | + |
| 47 | +func init() { |
| 48 | + rootCmd.AddCommand(searchCmd) |
| 49 | + searchCmd.Flags().StringVar(&flagCVE, "cve", "", "Filter by CVE ID substring") |
| 50 | + searchCmd.Flags().StringVar(&flagPlugin, "plugin", "", "Filter by plugin slug substring") |
| 51 | + searchCmd.Flags().StringVar(&flagTitle, "title", "", "Filter by keyword in title") |
| 52 | + searchCmd.Flags(). |
| 53 | + StringVar(&flagSeverity, "severity", "", "Filter by severity (critical, high, medium, low)") |
| 54 | + searchCmd.Flags(). |
| 55 | + StringVar(&flagAuth, "auth", "", "Filter by auth type (Unauth, Auth, Privileged)") |
| 56 | + searchCmd.Flags(). |
| 57 | + BoolVarP(&showDetails, "details", "d", false, "Show detailed CVE entries per plugin") |
| 58 | +} |
| 59 | + |
| 60 | +func runSearch(cmd *cobra.Command, args []string) error { |
| 61 | + if !search.AnyFilterSet(flagCVE, flagPlugin, flagTitle, flagSeverity, flagAuth) { |
| 62 | + return fmt.Errorf( |
| 63 | + "please specify at least one filter: --cve, --plugin, --title, --severity, or --auth", |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + vulns, err := wordfence.LoadVulnerabilities("wordfence_vulnerabilities.json") |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + filtered := search.FilterAll(vulns, flagCVE, flagPlugin, flagTitle, flagSeverity, flagAuth) |
| 73 | + if len(filtered) == 0 { |
| 74 | + fmt.Println(scanner.UnknownStyle.Render("No vulnerabilities match filters.")) |
| 75 | + return nil |
| 76 | + } |
| 77 | + byPlugin := search.GroupByPlugin(filtered) |
| 78 | + |
| 79 | + root := tree.Root( |
| 80 | + scanner.TitleStyle.Render(fmt.Sprintf("🔍 %d vulnerabilities found", len(filtered))), |
| 81 | + ) |
| 82 | + search.BuildTree(root, byPlugin, showDetails) |
| 83 | + |
| 84 | + fmt.Println(scanner.SeparatorStyle.Render(root.String())) |
| 85 | + return nil |
| 86 | +} |
0 commit comments