diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..551c545 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +all: + go run log4jscanner.go + +build: + go build -o log4jscanner + diff --git a/jar/backup.go b/jar/backup.go new file mode 100644 index 0000000..fc27497 --- /dev/null +++ b/jar/backup.go @@ -0,0 +1,52 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package jar + +import ( + "bufio" + "fmt" + "io" + "log" + "os" +) + +func CreateDirectoryifNotExist(dir string) { + if _, err := os.Stat(dir); os.IsNotExist(err) { + err = os.Mkdir(dir, 0755) + if err != nil { + log.Fatal(err) + } + } +} + +func Backup(dstFileName string, srcFileName string) (written int64, err error) { + srcFile, err := os.Open(srcFileName) + if err != nil { + fmt.Printf("open file error = %v\n", err) + } + defer srcFile.Close() + + reader := bufio.NewReader(srcFile) + + dstFile, err := os.OpenFile(dstFileName, os.O_WRONLY|os.O_CREATE, 0666) + if err != nil { + fmt.Printf("open file error = %v\n", err) + } + + writer := bufio.NewWriter(dstFile) + + defer dstFile.Close() + return io.Copy(writer, reader) +} diff --git a/log4jscanner.go b/log4jscanner.go index fdad047..0f979d3 100644 --- a/log4jscanner.go +++ b/log4jscanner.go @@ -40,6 +40,7 @@ Flags: -f, --force Don't skip network and userland filesystems. (smb,nfs,afs,fuse) -w, --rewrite Rewrite vulnerable JARs as they are detected. -v, --verbose Print verbose logs to stderr. + -b, --backup Suffix to use to backup a file when rewriting (.bak) `) } @@ -74,8 +75,10 @@ func main() { flag.BoolVar(&w, "w", false, "") flag.BoolVar(&verbose, "verbose", false, "") flag.BoolVar(&v, "v", false, "") + flag.BoolVar(&force, "force", false, "") flag.BoolVar(&f, "f", false, "") + flag.Func("s", "", appendSkip) flag.Func("skip", "", appendSkip) flag.Usage = usage @@ -94,10 +97,12 @@ func main() { if w { rewrite = w } + log.SetFlags(log.LstdFlags | log.Lshortfile) logf := func(format string, v ...interface{}) { if verbose { log.Printf(format, v...) + } } seen := 0 @@ -140,6 +145,7 @@ func main() { }, } + for _, dir := range dirs { logf("Scanning %s", dir) if err := walker.Walk(dir); err != nil {