Skip to content

Commit

Permalink
Merge branch 'bump-log4shell-cli-version' of github.com:lunasec-io/lu…
Browse files Browse the repository at this point in the history
…nasec into bump-log4shell-cli-version
  • Loading branch information
factoidforrest committed Dec 16, 2021
2 parents 5b506a1 + 427e491 commit 4372467
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
20 changes: 14 additions & 6 deletions tools/log4shell/scan/scan.go
Expand Up @@ -79,6 +79,14 @@ func (s *Log4jDirectoryScanner) Scan(
return
}

if info.Mode() & os.ModeSymlink != 0 {
// overwrite path and info with the resolved symlink file values
path, info, err = util.ResolveSymlinkFilePathAndInfo(path)
if err != nil {
return
}
}

fileExt := util.FileExt(path)
switch fileExt {
case constants.JarFileExt, constants.WarFileExt:
Expand All @@ -105,7 +113,7 @@ func (s *Log4jDirectoryScanner) scanLocatedArchive(
log.Warn().
Str("path", path).
Err(err).
Msg("unable to open archive")
Msg("unable to open located archive")
return
}
defer file.Close()
Expand All @@ -130,7 +138,7 @@ func (s *Log4jDirectoryScanner) scanArchiveForVulnerableFiles(
log.Warn().
Str("path", path).
Err(err).
Msg("unable to open archive")
Msg("unable to open archive for scanning")
return
}

Expand Down Expand Up @@ -169,7 +177,7 @@ func (s *Log4jDirectoryScanner) scanFile(
}
return
}
return s.scanArchive(path, file)
return s.scanEmbeddedArchive(path, file)
}
return
}
Expand All @@ -191,7 +199,7 @@ func (s *Log4jDirectoryScanner) scanArchiveFile(
return s.processArchiveFile(reader, path, file.Name)
}

func (s *Log4jDirectoryScanner) scanArchive(
func (s *Log4jDirectoryScanner) scanEmbeddedArchive(
path string,
file *zip.File,
) (findings []types.Finding) {
Expand All @@ -201,7 +209,7 @@ func (s *Log4jDirectoryScanner) scanArchive(
Str("classFile", file.Name).
Str("path", path).
Err(err).
Msg("unable to open archive")
Msg("unable to open embedded archive")
return
}
defer reader.Close()
Expand All @@ -212,7 +220,7 @@ func (s *Log4jDirectoryScanner) scanArchive(
Str("classFile", file.Name).
Str("path", path).
Err(err).
Msg("unable to read archive")
Msg("unable to read embedded archive")
return
}

Expand Down
31 changes: 29 additions & 2 deletions tools/log4shell/util/fs.go
Expand Up @@ -15,7 +15,8 @@
package util

import (
"log"
"github.com/rs/zerolog/log"
"os"
"path/filepath"
"strings"
)
Expand All @@ -27,7 +28,11 @@ func FileExt(path string) string {
func searchDir(searchDir string, callback filepath.WalkFunc) {
err := filepath.Walk(searchDir, callback)
if err != nil {
log.Fatal(err)
log.Error().
Err(err).
Str("searchDir", searchDir).
Msg("Unable to walk directory")
panic(err)
}
}

Expand All @@ -37,3 +42,25 @@ func SearchDirs(searchDirs []string, callback filepath.WalkFunc) {
searchDir(dir, callback)
}
}

func ResolveSymlinkFilePathAndInfo(symlinkPath string) (path string, info os.FileInfo, err error) {
path, err = filepath.EvalSymlinks(symlinkPath)
if err != nil {
log.Warn().
Str("path", path).
Err(err).
Msg("unable to read symlink to file")
return
}

// use file info of the resolved file
info, err = os.Lstat(path)
if err != nil {
log.Warn().
Str("path", path).
Err(err).
Msg("unable to read file info of symlink file")
return
}
return
}

0 comments on commit 4372467

Please sign in to comment.