|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/ismailtsdln/ApkSentinel/internal/analyzer" |
| 9 | + "github.com/ismailtsdln/ApkSentinel/internal/decompiler" |
| 10 | + "github.com/ismailtsdln/ApkSentinel/internal/report" |
| 11 | + "github.com/ismailtsdln/ApkSentinel/internal/scanner" |
| 12 | + "github.com/ismailtsdln/ApkSentinel/internal/utils" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + inputPath string |
| 18 | + outputDir string |
| 19 | + outputFormat string |
| 20 | + customPatterns string |
| 21 | + jadxPath string |
| 22 | + verbose bool |
| 23 | +) |
| 24 | + |
| 25 | +var rootCmd = &cobra.Command{ |
| 26 | + Use: "apk-sentinel", |
| 27 | + Short: "ApkSentinel is a high-performance APK static analysis tool", |
| 28 | + Long: `ApkSentinel is a high-performance APK static analysis tool designed to |
| 29 | +detect hard-coded secrets, API keys, and sensitive URLs in Android applications.`, |
| 30 | + Run: func(cmd *cobra.Command, args []string) { |
| 31 | + utils.PrintBanner() |
| 32 | + |
| 33 | + if inputPath == "" { |
| 34 | + utils.Error("Input APK path is required.") |
| 35 | + cmd.Help() |
| 36 | + os.Exit(1) |
| 37 | + } |
| 38 | + |
| 39 | + // 1. Decompile |
| 40 | + decomp := decompiler.NewDecompiler(jadxPath, filepath.Join(outputDir, "decompiled")) |
| 41 | + decompiledDir, err := decomp.Decompile(inputPath) |
| 42 | + if err != nil { |
| 43 | + utils.Error("Error during decompilation: %v", err) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + |
| 47 | + // 2. Scan |
| 48 | + patternFile := customPatterns |
| 49 | + if patternFile == "" { |
| 50 | + // fallback to default patterns |
| 51 | + patternFile = "internal/patterns/default_patterns.json" |
| 52 | + } |
| 53 | + |
| 54 | + s, err := scanner.NewScanner(patternFile) |
| 55 | + if err != nil { |
| 56 | + utils.Error("Error initializing scanner: %v", err) |
| 57 | + os.Exit(1) |
| 58 | + } |
| 59 | + |
| 60 | + utils.Info("Scanning decompiled source code...") |
| 61 | + results, err := s.ScanDirectory(decompiledDir) |
| 62 | + if err != nil { |
| 63 | + utils.Error("Error during scan: %v", err) |
| 64 | + os.Exit(1) |
| 65 | + } |
| 66 | + |
| 67 | + // 3. Manifest Analysis |
| 68 | + manifestPath := filepath.Join(decompiledDir, "resources", "AndroidManifest.xml") |
| 69 | + // Sometimes jadx puts it in the root depending on version/config |
| 70 | + if _, err := os.Stat(manifestPath); os.IsNotExist(err) { |
| 71 | + manifestPath = filepath.Join(decompiledDir, "AndroidManifest.xml") |
| 72 | + } |
| 73 | + |
| 74 | + var findings []analyzer.SecurityFinding |
| 75 | + if _, err := os.Stat(manifestPath); err == nil { |
| 76 | + utils.Info("Analyzing AndroidManifest.xml...") |
| 77 | + findings, _ = analyzer.AnalyzeManifest(manifestPath) |
| 78 | + } |
| 79 | + |
| 80 | + // 4. Report |
| 81 | + r := report.Report{ |
| 82 | + APKPath: inputPath, |
| 83 | + Results: results, |
| 84 | + Findings: findings, |
| 85 | + } |
| 86 | + |
| 87 | + if outputFormat == "json" || outputFormat == "both" { |
| 88 | + if err := report.SaveJSON(r, outputDir); err != nil { |
| 89 | + utils.Error("Error saving JSON report: %v", err) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if outputFormat == "html" || outputFormat == "both" { |
| 94 | + if err := report.SaveHTML(r, outputDir); err != nil { |
| 95 | + utils.Error("Error saving HTML report: %v", err) |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | +} |
| 100 | + |
| 101 | +func Execute() { |
| 102 | + if err := rootCmd.Execute(); err != nil { |
| 103 | + fmt.Println(err) |
| 104 | + os.Exit(1) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func init() { |
| 109 | + rootCmd.PersistentFlags().StringVarP(&inputPath, "input", "i", "", "Input APK path (required)") |
| 110 | + rootCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "./report", "Output directory") |
| 111 | + rootCmd.PersistentFlags().StringVarP(&outputFormat, "format", "f", "json", "Output format (json|html)") |
| 112 | + rootCmd.PersistentFlags().StringVarP(&customPatterns, "pattern", "p", "", "Custom pattern file (JSON)") |
| 113 | + rootCmd.PersistentFlags().StringVar(&jadxPath, "jadx-path", "jadx", "Path to jadx executable") |
| 114 | + rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Show verbose output") |
| 115 | +} |
| 116 | + |
| 117 | +func main() { |
| 118 | + Execute() |
| 119 | +} |
0 commit comments