-
Notifications
You must be signed in to change notification settings - Fork 40
Malicious code scanner #614
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34c51b0
Malicious code scanner
barv-jfrog c0d1edd
Malicious code scanner
barv-jfrog ee68831
Malicious code scanner
barv-jfrog 3fae385
Malicious code scanner
barv-jfrog 91e8aed
Malicious code scanner
barv-jfrog 469cbcb
Merge branch 'dev' into mal-code-scanner
barv-jfrog 1dd80e0
Merge branch 'dev' into mal-code-scanner
barv-jfrog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package maliciousscan | ||
|
|
||
| import ( | ||
| "github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
| ) | ||
|
|
||
| func GetDescription() string { | ||
| return "[Beta] Scan malicious models (pickle files, etc.) located in the working directory." | ||
| } | ||
|
|
||
| func GetArguments() []components.Argument { | ||
| return []components.Argument{} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,281 @@ | ||
| package maliciousscan | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/jfrog/jfrog-cli-core/v2/common/format" | ||
| "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
| "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" | ||
| "github.com/jfrog/jfrog-cli-security/jas" | ||
| "github.com/jfrog/jfrog-cli-security/jas/maliciouscode" | ||
| "github.com/jfrog/jfrog-cli-security/utils" | ||
| "github.com/jfrog/jfrog-cli-security/utils/jasutils" | ||
| "github.com/jfrog/jfrog-cli-security/utils/results" | ||
| "github.com/jfrog/jfrog-cli-security/utils/results/output" | ||
| "github.com/jfrog/jfrog-cli-security/utils/severityutils" | ||
| "github.com/jfrog/jfrog-cli-security/utils/xray" | ||
| ioUtils "github.com/jfrog/jfrog-client-go/utils/io" | ||
| "github.com/jfrog/jfrog-client-go/utils/io/fileutils" | ||
| "github.com/jfrog/jfrog-client-go/utils/log" | ||
| ) | ||
|
|
||
| type MaliciousScanCommand struct { | ||
| serverDetails *config.ServerDetails | ||
| workingDirs []string | ||
| threads int | ||
| outputFormat format.OutputFormat | ||
| minSeverityFilter severityutils.Severity | ||
| progress ioUtils.ProgressMgr | ||
| customAnalyzerManagerPath string | ||
| project string | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) SetProgress(progress ioUtils.ProgressMgr) { | ||
| cmd.progress = progress | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) SetThreads(threads int) *MaliciousScanCommand { | ||
| cmd.threads = threads | ||
| return cmd | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) SetServerDetails(server *config.ServerDetails) *MaliciousScanCommand { | ||
| cmd.serverDetails = server | ||
| return cmd | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) SetWorkingDirs(workingDirs []string) *MaliciousScanCommand { | ||
| cmd.workingDirs = workingDirs | ||
| return cmd | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) SetOutputFormat(format format.OutputFormat) *MaliciousScanCommand { | ||
| cmd.outputFormat = format | ||
| return cmd | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) SetMinSeverityFilter(minSeverity severityutils.Severity) *MaliciousScanCommand { | ||
| cmd.minSeverityFilter = minSeverity | ||
| return cmd | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) SetCustomAnalyzerManagerPath(path string) *MaliciousScanCommand { | ||
| cmd.customAnalyzerManagerPath = path | ||
| return cmd | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) SetProject(project string) *MaliciousScanCommand { | ||
| cmd.project = project | ||
| return cmd | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) ServerDetails() (*config.ServerDetails, error) { | ||
| return cmd.serverDetails, nil | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) CommandName() string { | ||
| return "malicious_scan" | ||
| } | ||
|
|
||
| func NewMaliciousScanCommand() *MaliciousScanCommand { | ||
| return &MaliciousScanCommand{} | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) Run() (err error) { | ||
attiasas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| xrayVersion, entitledForJas, workingDirs, err := cmd.validateAndPrepare() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cmdResults := cmd.initializeCommandResults(xrayVersion, entitledForJas) | ||
| populateScanTargets(cmdResults, workingDirs) | ||
|
|
||
| scanner, err := cmd.createJasScanner() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err = cmd.runMaliciousScans(cmdResults, scanner); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return cmd.outputResults(cmdResults) | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) validateAndPrepare() (xrayVersion string, entitledForJas bool, workingDirs []string, err error) { | ||
| xrayManager, xrayVersion, err := xray.CreateXrayServiceManagerAndGetVersion(cmd.serverDetails, xray.WithScopedProjectKey(cmd.project)) | ||
| if err != nil { | ||
| return "", false, nil, err | ||
| } | ||
|
|
||
| entitledForJas, err = jas.IsEntitledForJas(xrayManager, xrayVersion) | ||
| if err != nil { | ||
| return "", false, nil, err | ||
| } | ||
| if !entitledForJas { | ||
| return "", false, nil, errors.New("JAS (Advanced Security) feature is not entitled") | ||
| } | ||
|
|
||
| log.Info("JFrog Xray version is:", xrayVersion) | ||
|
|
||
| workingDirs, err = coreutils.GetFullPathsWorkingDirs(cmd.workingDirs) | ||
| if err != nil { | ||
| return "", false, nil, err | ||
| } | ||
| logScanPaths(workingDirs) | ||
|
|
||
| return xrayVersion, entitledForJas, workingDirs, nil | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) initializeCommandResults(xrayVersion string, entitledForJas bool) *results.SecurityCommandResults { | ||
| cmdResults := results.NewCommandResults(utils.SourceCode) | ||
| cmdResults.SetXrayVersion(xrayVersion) | ||
| cmdResults.SetEntitledForJas(entitledForJas) | ||
| cmdResults.SetResultsContext(results.ResultContext{ | ||
| IncludeVulnerabilities: true, | ||
| }) | ||
| return cmdResults | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) createJasScanner() (*jas.JasScanner, error) { | ||
| scannerOptions := []jas.JasScannerOption{ | ||
| jas.WithEnvVars( | ||
| false, | ||
| jas.NotDiffScanEnvValue, | ||
| jas.GetAnalyzerManagerXscEnvVars( | ||
| "", | ||
| "", | ||
| cmd.project, | ||
| nil, | ||
| ), | ||
| ), | ||
| jas.WithMinSeverity(cmd.minSeverityFilter), | ||
| } | ||
|
|
||
| scanner, err := jas.NewJasScanner(cmd.serverDetails, scannerOptions...) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create JAS scanner: %w", err) | ||
| } | ||
| if scanner == nil { | ||
| return nil, errors.New("JAS scanner was not created") | ||
| } | ||
|
|
||
| if err = cmd.setAnalyzerManagerPath(scanner); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| log.Debug(fmt.Sprintf("Using analyzer manager executable at: %s", scanner.AnalyzerManager.AnalyzerManagerFullPath)) | ||
| return scanner, nil | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) setAnalyzerManagerPath(scanner *jas.JasScanner) error { | ||
| if cmd.customAnalyzerManagerPath == "" { | ||
| if err := jas.DownloadAnalyzerManagerIfNeeded(0); err != nil { | ||
| return fmt.Errorf("failed to download analyzer manager: %s", err.Error()) | ||
| } | ||
| var err error | ||
| if scanner.AnalyzerManager.AnalyzerManagerFullPath, err = jas.GetAnalyzerManagerExecutable(); err != nil { | ||
| return fmt.Errorf("failed to set analyzer manager executable path: %s", err.Error()) | ||
| } | ||
| } else { | ||
| scanner.AnalyzerManager.AnalyzerManagerFullPath = cmd.customAnalyzerManagerPath | ||
| log.Debug("using custom analyzer manager binary path") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) runMaliciousScans(cmdResults *results.SecurityCommandResults, scanner *jas.JasScanner) error { | ||
| jasScanProducerConsumer := utils.NewSecurityParallelRunner(cmd.threads) | ||
| jasScanProducerConsumer.JasWg.Add(1) | ||
attiasas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| createMaliciousScansTask := func(threadId int) (generalError error) { | ||
| defer func() { | ||
| jasScanProducerConsumer.JasWg.Done() | ||
| }() | ||
| for _, targetResult := range cmdResults.Targets { | ||
| vulnerabilitiesResults, err := maliciouscode.RunMaliciousScan( | ||
| scanner, | ||
| maliciouscode.MaliciousScannerType, | ||
| targetResult.Target, | ||
| len(cmdResults.Targets), | ||
| threadId, | ||
| ) | ||
| jasScanProducerConsumer.ResultsMu.Lock() | ||
| // Malicious code scans only return vulnerabilities, not violations | ||
| targetResult.AddJasScanResults(jasutils.MaliciousCode, vulnerabilitiesResults, nil, jas.GetAnalyzerManagerExitCode(err)) | ||
| jasScanProducerConsumer.ResultsMu.Unlock() | ||
| if err = jas.ParseAnalyzerManagerError(jasutils.MaliciousCode, err); err != nil { | ||
| _ = targetResult.AddTargetError(fmt.Errorf("failed to run malicious scan: %w", err), false) | ||
| } | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if _, addTaskErr := jasScanProducerConsumer.Runner.AddTaskWithError(createMaliciousScansTask, func(taskErr error) { | ||
| cmdResults.AddGeneralError(fmt.Errorf("failed while adding malicious scan tasks: %s", taskErr.Error()), false) | ||
| }); addTaskErr != nil { | ||
| return fmt.Errorf("failed to create malicious scan task: %w", addTaskErr) | ||
| } | ||
|
|
||
| jasScanProducerConsumer.Start() | ||
| return nil | ||
| } | ||
|
|
||
| func (cmd *MaliciousScanCommand) outputResults(cmdResults *results.SecurityCommandResults) error { | ||
| if err := output.NewResultsWriter(cmdResults). | ||
| SetOutputFormat(cmd.outputFormat). | ||
| SetPlatformUrl(cmd.serverDetails.Url). | ||
| SetPrintExtendedTable(false). | ||
| SetIsMultipleRootProject(cmdResults.HasMultipleTargets()). | ||
| SetSubScansPerformed([]utils.SubScanType{utils.MaliciousCodeScan}). | ||
| PrintScanResults(); err != nil { | ||
| return errors.Join(err, cmdResults.GetErrors()) | ||
| } | ||
|
|
||
| if err := cmdResults.GetErrors(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| log.Info("Malicious scan completed successfully.") | ||
| return nil | ||
| } | ||
|
|
||
| func logScanPaths(workingDirs []string) { | ||
| if len(workingDirs) == 0 { | ||
| return | ||
| } | ||
| if len(workingDirs) == 1 { | ||
| log.Debug("Scanning path:", workingDirs[0]) | ||
| return | ||
| } | ||
| log.Debug("Scanning paths:", strings.Join(workingDirs, ", ")) | ||
| } | ||
|
|
||
| func populateScanTargets(cmdResults *results.SecurityCommandResults, workingDirs []string) { | ||
| for _, requestedDirectory := range workingDirs { | ||
| if !fileutils.IsPathExists(requestedDirectory, false) { | ||
| log.Warn("The working directory", requestedDirectory, "doesn't exist. Skipping scan...") | ||
| continue | ||
| } | ||
| cmdResults.NewScanResults(results.ScanTarget{Target: requestedDirectory, Name: filepath.Base(requestedDirectory)}) | ||
| } | ||
|
|
||
| if len(cmdResults.Targets) == 0 { | ||
| log.Warn("No scan targets were detected.") | ||
| return | ||
| } | ||
|
|
||
| logScanTargetsInfo(cmdResults) | ||
| } | ||
|
|
||
| func logScanTargetsInfo(cmdResults *results.SecurityCommandResults) { | ||
| if len(cmdResults.Targets) == 0 { | ||
| return | ||
| } | ||
| log.Info("Scanning", len(cmdResults.Targets), "target(s)...") | ||
| for _, targetResult := range cmdResults.Targets { | ||
| log.Info("Scanning target:", targetResult.Target) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should add a similar flag with different description if command is not hidden
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok