|
| 1 | +package maliciousscan |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/jfrog/jfrog-cli-core/v2/common/format" |
| 11 | + "github.com/jfrog/jfrog-cli-core/v2/utils/config" |
| 12 | + "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" |
| 13 | + "github.com/jfrog/jfrog-cli-security/jas" |
| 14 | + "github.com/jfrog/jfrog-cli-security/jas/runner" |
| 15 | + "github.com/jfrog/jfrog-cli-security/utils" |
| 16 | + "github.com/jfrog/jfrog-cli-security/utils/results" |
| 17 | + "github.com/jfrog/jfrog-cli-security/utils/results/output" |
| 18 | + "github.com/jfrog/jfrog-cli-security/utils/severityutils" |
| 19 | + "github.com/jfrog/jfrog-cli-security/utils/xray" |
| 20 | + ioUtils "github.com/jfrog/jfrog-client-go/utils/io" |
| 21 | + "github.com/jfrog/jfrog-client-go/utils/io/fileutils" |
| 22 | + "github.com/jfrog/jfrog-client-go/utils/log" |
| 23 | +) |
| 24 | + |
| 25 | +type MaliciousScanCommand struct { |
| 26 | + serverDetails *config.ServerDetails |
| 27 | + workingDirs []string |
| 28 | + threads int |
| 29 | + outputFormat format.OutputFormat |
| 30 | + minSeverityFilter severityutils.Severity |
| 31 | + progress ioUtils.ProgressMgr |
| 32 | + customAnalyzerManagerPath string |
| 33 | +} |
| 34 | + |
| 35 | +func (cmd *MaliciousScanCommand) SetProgress(progress ioUtils.ProgressMgr) { |
| 36 | + cmd.progress = progress |
| 37 | +} |
| 38 | + |
| 39 | +func (cmd *MaliciousScanCommand) SetThreads(threads int) *MaliciousScanCommand { |
| 40 | + cmd.threads = threads |
| 41 | + return cmd |
| 42 | +} |
| 43 | + |
| 44 | +func (cmd *MaliciousScanCommand) SetServerDetails(server *config.ServerDetails) *MaliciousScanCommand { |
| 45 | + cmd.serverDetails = server |
| 46 | + return cmd |
| 47 | +} |
| 48 | + |
| 49 | +func (cmd *MaliciousScanCommand) SetWorkingDirs(workingDirs []string) *MaliciousScanCommand { |
| 50 | + cmd.workingDirs = workingDirs |
| 51 | + return cmd |
| 52 | +} |
| 53 | + |
| 54 | +func (cmd *MaliciousScanCommand) SetOutputFormat(format format.OutputFormat) *MaliciousScanCommand { |
| 55 | + cmd.outputFormat = format |
| 56 | + return cmd |
| 57 | +} |
| 58 | + |
| 59 | +func (cmd *MaliciousScanCommand) SetMinSeverityFilter(minSeverity severityutils.Severity) *MaliciousScanCommand { |
| 60 | + cmd.minSeverityFilter = minSeverity |
| 61 | + return cmd |
| 62 | +} |
| 63 | + |
| 64 | +func (cmd *MaliciousScanCommand) SetCustomAnalyzerManagerPath(path string) *MaliciousScanCommand { |
| 65 | + cmd.customAnalyzerManagerPath = path |
| 66 | + return cmd |
| 67 | +} |
| 68 | + |
| 69 | +func (cmd *MaliciousScanCommand) ServerDetails() (*config.ServerDetails, error) { |
| 70 | + return cmd.serverDetails, nil |
| 71 | +} |
| 72 | + |
| 73 | +func (cmd *MaliciousScanCommand) CommandName() string { |
| 74 | + return "malicious_scan" |
| 75 | +} |
| 76 | + |
| 77 | +func NewMaliciousScanCommand() *MaliciousScanCommand { |
| 78 | + return &MaliciousScanCommand{} |
| 79 | +} |
| 80 | + |
| 81 | +func (cmd *MaliciousScanCommand) Run() (err error) { |
| 82 | + defer func() { |
| 83 | + if err != nil { |
| 84 | + var e *exec.ExitError |
| 85 | + if errors.As(err, &e) { |
| 86 | + if e.ExitCode() != coreutils.ExitCodeVulnerableBuild.Code { |
| 87 | + err = errors.New("Malicious scan command failed. " + err.Error()) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + }() |
| 92 | + |
| 93 | + xrayManager, xrayVersion, err := xray.CreateXrayServiceManagerAndGetVersion(cmd.serverDetails) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + entitledForJas, err := jas.IsEntitledForJas(xrayManager, xrayVersion) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + if !entitledForJas { |
| 103 | + return errors.New("JAS (Advanced Security) feature is not entitled") |
| 104 | + } |
| 105 | + |
| 106 | + log.Info("JFrog Xray version is:", xrayVersion) |
| 107 | + |
| 108 | + if err = jas.DownloadAnalyzerManagerIfNeeded(0); err != nil { |
| 109 | + return fmt.Errorf("failed to download Analyzer Manager: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + workingDirs, err := coreutils.GetFullPathsWorkingDirs(cmd.workingDirs) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + logScanPaths(workingDirs) |
| 117 | + |
| 118 | + cmdResults := results.NewCommandResults(utils.SourceCode) |
| 119 | + cmdResults.SetXrayVersion(xrayVersion) |
| 120 | + cmdResults.SetEntitledForJas(entitledForJas) |
| 121 | + cmdResults.SetResultsContext(results.ResultContext{ |
| 122 | + IncludeVulnerabilities: true, |
| 123 | + }) |
| 124 | + |
| 125 | + populateScanTargets(cmdResults, workingDirs) |
| 126 | + |
| 127 | + scannerOptions := []jas.JasScannerOption{ |
| 128 | + jas.WithEnvVars( |
| 129 | + false, // validateSecrets not relevant for malicious scan |
| 130 | + jas.NotDiffScanEnvValue, |
| 131 | + jas.GetAnalyzerManagerXscEnvVars( |
| 132 | + "", // msi |
| 133 | + "", // gitRepoUrl |
| 134 | + "", // projectKey |
| 135 | + nil, // watches |
| 136 | + ), |
| 137 | + ), |
| 138 | + jas.WithMinSeverity(cmd.minSeverityFilter), |
| 139 | + } |
| 140 | + |
| 141 | + scanner, err := jas.NewJasScanner(cmd.serverDetails, scannerOptions...) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("failed to create JAS scanner: %w", err) |
| 144 | + } |
| 145 | + if scanner == nil { |
| 146 | + return errors.New("JAS scanner was not created") |
| 147 | + } |
| 148 | + |
| 149 | + if cmd.customAnalyzerManagerPath != "" { |
| 150 | + scanner.AnalyzerManager.AnalyzerManagerFullPath = cmd.customAnalyzerManagerPath |
| 151 | + } else { |
| 152 | + if scanner.AnalyzerManager.AnalyzerManagerFullPath, err = jas.GetAnalyzerManagerExecutable(); err != nil { |
| 153 | + return fmt.Errorf("failed to set analyzer manager executable path: %w", err) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + log.Debug(fmt.Sprintf("Using analyzer manager executable at: %s", scanner.AnalyzerManager.AnalyzerManagerFullPath)) |
| 158 | + |
| 159 | + jasScanProducerConsumer := utils.NewSecurityParallelRunner(cmd.threads) |
| 160 | + |
| 161 | + serverDetails, err := cmd.ServerDetails() |
| 162 | + if err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + |
| 166 | + jasScanProducerConsumer.JasWg.Add(1) |
| 167 | + createMaliciousScansTask := func(threadId int) (generalError error) { |
| 168 | + defer func() { |
| 169 | + jasScanProducerConsumer.JasWg.Done() |
| 170 | + }() |
| 171 | + for _, targetResult := range cmdResults.Targets { |
| 172 | + if targetResult.AppsConfigModule == nil { |
| 173 | + _ = targetResult.AddTargetError(fmt.Errorf("can't find module for path %s", targetResult.Target), false) |
| 174 | + continue |
| 175 | + } |
| 176 | + appsConfigModule := *targetResult.AppsConfigModule |
| 177 | + jasParams := runner.JasRunnerParams{ |
| 178 | + Runner: &jasScanProducerConsumer, |
| 179 | + ServerDetails: serverDetails, |
| 180 | + Scanner: scanner, |
| 181 | + Module: appsConfigModule, |
| 182 | + ScansToPerform: []utils.SubScanType{utils.MaliciousCodeScan}, |
| 183 | + ScanResults: targetResult, |
| 184 | + TargetCount: len(cmdResults.Targets), |
| 185 | + } |
| 186 | + |
| 187 | + if generalError = runner.AddJasScannersTasks(jasParams); generalError != nil { |
| 188 | + _ = targetResult.AddTargetError(fmt.Errorf("failed to add malicious scan task: %w", generalError), false) |
| 189 | + generalError = nil |
| 190 | + } |
| 191 | + } |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + if _, addTaskErr := jasScanProducerConsumer.Runner.AddTaskWithError(createMaliciousScansTask, func(taskErr error) { |
| 196 | + cmdResults.AddGeneralError(fmt.Errorf("failed while adding JAS scan tasks: %s", taskErr.Error()), false) |
| 197 | + }); addTaskErr != nil { |
| 198 | + return fmt.Errorf("failed to create JAS task: %w", addTaskErr) |
| 199 | + } |
| 200 | + |
| 201 | + jasScanProducerConsumer.Start() |
| 202 | + |
| 203 | + if cmd.progress != nil { |
| 204 | + if err = cmd.progress.Quit(); err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + if err = output.NewResultsWriter(cmdResults). |
| 210 | + SetOutputFormat(cmd.outputFormat). |
| 211 | + SetPlatformUrl(cmd.serverDetails.Url). |
| 212 | + SetPrintExtendedTable(false). |
| 213 | + SetIsMultipleRootProject(cmdResults.HasMultipleTargets()). |
| 214 | + SetSubScansPerformed([]utils.SubScanType{utils.MaliciousCodeScan}). |
| 215 | + PrintScanResults(); err != nil { |
| 216 | + return errors.Join(err, cmdResults.GetErrors()) |
| 217 | + } |
| 218 | + |
| 219 | + if err = cmdResults.GetErrors(); err != nil { |
| 220 | + return err |
| 221 | + } |
| 222 | + |
| 223 | + log.Info("Malicious scan completed successfully.") |
| 224 | + return nil |
| 225 | +} |
| 226 | + |
| 227 | +func logScanPaths(workingDirs []string) { |
| 228 | + if len(workingDirs) == 0 { |
| 229 | + return |
| 230 | + } |
| 231 | + if len(workingDirs) == 1 { |
| 232 | + log.Info("Scanning path:", workingDirs[0]) |
| 233 | + return |
| 234 | + } |
| 235 | + log.Info("Scanning paths:", strings.Join(workingDirs, ", ")) |
| 236 | +} |
| 237 | + |
| 238 | +func populateScanTargets(cmdResults *results.SecurityCommandResults, workingDirs []string) { |
| 239 | + for _, requestedDirectory := range workingDirs { |
| 240 | + if !fileutils.IsPathExists(requestedDirectory, false) { |
| 241 | + log.Warn("The working directory", requestedDirectory, "doesn't exist. Skipping scan...") |
| 242 | + continue |
| 243 | + } |
| 244 | + cmdResults.NewScanResults(results.ScanTarget{Target: requestedDirectory, Name: filepath.Base(requestedDirectory)}) |
| 245 | + } |
| 246 | + |
| 247 | + if len(workingDirs) == 0 { |
| 248 | + currentDir, err := coreutils.GetWorkingDirectory() |
| 249 | + if err != nil { |
| 250 | + cmdResults.AddGeneralError(fmt.Errorf("failed to get current working directory: %w", err), false) |
| 251 | + return |
| 252 | + } |
| 253 | + cmdResults.NewScanResults(results.ScanTarget{Target: currentDir, Name: filepath.Base(currentDir)}) |
| 254 | + } |
| 255 | + |
| 256 | + if len(cmdResults.Targets) == 0 { |
| 257 | + log.Warn("No scan targets were detected. Proceeding with empty scan...") |
| 258 | + return |
| 259 | + } |
| 260 | + |
| 261 | + jfrogAppsConfig, err := jas.CreateJFrogAppsConfig(cmdResults.GetTargetsPaths()) |
| 262 | + if err != nil { |
| 263 | + cmdResults.AddGeneralError(fmt.Errorf("failed to create JFrogAppsConfig: %w", err), false) |
| 264 | + return |
| 265 | + } |
| 266 | + |
| 267 | + for _, targetResult := range cmdResults.Targets { |
| 268 | + targetResult.AppsConfigModule = jas.GetModule(targetResult.Target, jfrogAppsConfig) |
| 269 | + } |
| 270 | + |
| 271 | + logScanTargetsInfo(cmdResults) |
| 272 | +} |
| 273 | + |
| 274 | +func logScanTargetsInfo(cmdResults *results.SecurityCommandResults) { |
| 275 | + if len(cmdResults.Targets) == 0 { |
| 276 | + return |
| 277 | + } |
| 278 | + log.Info("Scanning", len(cmdResults.Targets), "target(s)...") |
| 279 | + for _, targetResult := range cmdResults.Targets { |
| 280 | + log.Info("Scanning target:", targetResult.Target) |
| 281 | + } |
| 282 | +} |
0 commit comments