Skip to content

Commit

Permalink
get rid of session
Browse files Browse the repository at this point in the history
- remove session and reimplement everything inside
- use logrus
- create opts and config a part of scanner
- optimise the parameter passed
- convert scanner functions to method
TODO
- Decouple functions
  • Loading branch information
ibreakthecloud committed Feb 15, 2023
1 parent 470f3fb commit 6c9f83b
Show file tree
Hide file tree
Showing 22 changed files with 477 additions and 510 deletions.
7 changes: 7 additions & 0 deletions constants/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package constants

const (
PLUGIN_NAME = "MalwareScanner"
TempDirSuffix = "YaraHunter"
ExtractedImageFilesDir = "ExtractedFiles"
)
2 changes: 1 addition & 1 deletion core/sys/fileLinux.go → constants/filelinux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sys
package constants

const (
/* statfs(2) */
Expand Down
6 changes: 6 additions & 0 deletions constants/yararule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package constants

const (
Filescan = 0
Procscan = 1
)
100 changes: 0 additions & 100 deletions core/log.go

This file was deleted.

69 changes: 35 additions & 34 deletions core/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"strings"
"syscall"

"github.com/deepfence/YaraHunter/core/sys"
"github.com/deepfence/YaRadare/core/sys"
"github.com/deepfence/YaraHunter/constants"
"github.com/deepfence/YaraHunter/pkg/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

Expand All @@ -18,13 +21,12 @@ type MatchFile struct {
}

// IsSkippableFile Checks if the path is excluded
func IsSkippableContainerRuntimeDir(fs afero.Fs, path string, baseDir string) bool {
hostMountPath := *session.Options.HostMountPath
func IsSkippableContainerRuntimeDir(fs afero.Fs, excludedContainerPaths []string, path string, baseDir, hostMountPath string) bool {
if hostMountPath != "" {
baseDir = hostMountPath
}

for _, skippablePathIndicator := range session.Config.ExcludedContainerPaths {
for _, skippablePathIndicator := range excludedContainerPaths {
if strings.HasPrefix(path, skippablePathIndicator) || strings.HasPrefix(path, filepath.Join(baseDir, skippablePathIndicator)) {
return true
}
Expand All @@ -49,44 +51,43 @@ func IsSkippableContainerRuntimeDir(fs afero.Fs, path string, baseDir string) bo
switch uint32(buf.Type) {
case
// pseudo filesystems
sys.BDEVFS_MAGIC,
sys.BINFMTFS_MAGIC,
sys.CGROUP_SUPER_MAGIC,
sys.DEBUGFS_MAGIC,
sys.EFIVARFS_MAGIC,
sys.FUTEXFS_SUPER_MAGIC,
sys.HUGETLBFS_MAGIC,
sys.PIPEFS_MAGIC,
sys.PROC_SUPER_MAGIC,
sys.SELINUX_MAGIC,
sys.SMACK_MAGIC,
sys.SYSFS_MAGIC,
constants.BDEVFS_MAGIC,
constants.BINFMTFS_MAGIC,
constants.CGROUP_SUPER_MAGIC,
constants.DEBUGFS_MAGIC,
constants.EFIVARFS_MAGIC,
constants.FUTEXFS_SUPER_MAGIC,
constants.HUGETLBFS_MAGIC,
constants.PIPEFS_MAGIC,
constants.PROC_SUPER_MAGIC,
constants.SELINUX_MAGIC,
constants.SMACK_MAGIC,
constants.SYSFS_MAGIC,
// network filesystems
sys.AFS_FS_MAGIC,
sys.OPENAFS_FS_MAGIC,
sys.CEPH_SUPER_MAGIC,
sys.CIFS_MAGIC_NUMBER,
sys.CODA_SUPER_MAGIC,
sys.NCP_SUPER_MAGIC,
sys.NFS_SUPER_MAGIC,
sys.OCFS2_SUPER_MAGIC,
sys.SMB_SUPER_MAGIC,
sys.V9FS_MAGIC,
sys.VMBLOCK_SUPER_MAGIC,
sys.XENFS_SUPER_MAGIC:
constants.AFS_FS_MAGIC,
constants.OPENAFS_FS_MAGIC,
constants.CEPH_SUPER_MAGIC,
constants.CIFS_MAGIC_NUMBER,
constants.CODA_SUPER_MAGIC,
constants.NCP_SUPER_MAGIC,
constants.NFS_SUPER_MAGIC,
constants.OCFS2_SUPER_MAGIC,
constants.SMB_SUPER_MAGIC,
constants.V9FS_MAGIC,
constants.VMBLOCK_SUPER_MAGIC,
constants.XENFS_SUPER_MAGIC:
return true
}
return false
}

// IsSkippableFile Checks if the path is excluded
func IsSkippableDir(fs afero.Fs, path string, baseDir string) bool {
hostMountPath := *session.Options.HostMountPath
func IsSkippableDir(fs afero.Fs, config config.Config, path, baseDir, hostMountPath string) bool {
if hostMountPath != "" {
baseDir = hostMountPath
}

for _, skippablePathIndicator := range session.Config.ExcludedPaths {
for _, skippablePathIndicator := range config.ExcludedPaths {
if strings.HasPrefix(path, skippablePathIndicator) || strings.HasPrefix(path, filepath.Join(baseDir, skippablePathIndicator)) {
return true
}
Expand Down Expand Up @@ -142,9 +143,9 @@ func IsSkippableDir(fs afero.Fs, path string, baseDir string) bool {
}

// IsSkippableFileExtension Checks if the file extension is excluded
func IsSkippableFileExtension(path string) bool {
func IsSkippableFileExtension(excludedExtensions []string, path string) bool {
extension := strings.ToLower(filepath.Ext(path))
for _, skippableExt := range session.Config.ExcludedExtensions {
for _, skippableExt := range excludedExtensions {
if extension == skippableExt {
return true
}
Expand All @@ -158,7 +159,7 @@ func UpdateDirsPermissionsRW(dir string) {
if f.IsDir() {
err := os.Chmod(path, 0700)
if err != nil {
GetSession().Log.Error("Failed to change dir %s permission: %s", path, err)
log.Errorf("Error updating permissions for dir %s: %s", path, err)
}
}
return nil
Expand Down
88 changes: 0 additions & 88 deletions core/session.go

This file was deleted.

Loading

0 comments on commit 6c9f83b

Please sign in to comment.