Skip to content

Find, verify, and analyze leaked credentials

Add filesystem scan to CLI (#77)

Bill Rich8d1db04c3632
diff --git a/main.go b/main.go
index 549045f57..7716f99ea 100644
--- a/main.go
+++ b/main.go
@@ -58,8 +58,9 @@ func main() {
// gitlabScanTarget := gitlabScan.Arg("target", "GitLab target. Can be a repository, user or organization.").Required().String()
// gitlabScanToken := gitlabScan.Flag("token", "GitLab token.").String()
- filesystemScan := cli.Command("filesystem", "Coming soon. Find credentials in a filesystem.")
- // filesystemScanPath := filesystemScan.Arg("path", "Path to scan.").Required().String()
+ filesystemScan := cli.Command("filesystem", "Find credentials in a filesystem.")
+ filesystemDirectories := filesystemScan.Flag("directory", "Path to directory to scan. You can repeat this flag.").Required().Strings()
+ // TODO: Add more filesystem scan options. Currently only supports scanning a list of directories.
// filesystemScanRecursive := filesystemScan.Flag("recursive", "Scan recursively.").Short('r').Bool()
// filesystemScanIncludePaths := filesystemScan.Flag("include_paths", "Path to file with newline separated regexes for files to include in scan.").Short('i').String()
// filesystemScanExcludePaths := filesystemScan.Flag("exclude_paths", "Path to file with newline separated regexes for files to exclude in scan.").Short('x').String()
@@ -132,7 +133,10 @@ func main() {
case gitlabScan.FullCommand():
log.Fatal("GitLab not implemented. Coming soon.")
case filesystemScan.FullCommand():
- log.Fatal("Filesystem not implemented. Coming soon.")
+ err := e.ScanFileSystem(ctx, *filesystemDirectories)
+ if err != nil {
+ logrus.WithError(err).Fatal("Failed to scan filesystem")
+ }
case s3Scan.FullCommand():
log.Fatal("S3 not implemented. Coming soon.")
}
diff --git a/pkg/engine/filesystem.go b/pkg/engine/filesystem.go
new file mode 100644
index 000000000..fd0a7b707
--- /dev/null
+++ b/pkg/engine/filesystem.go
@@ -0,0 +1,38 @@
+package engine
+
+import (
+ "context"
+ "github.com/go-errors/errors"
+ "github.com/sirupsen/logrus"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/sources/filesystem"
+ "google.golang.org/protobuf/proto"
+ "google.golang.org/protobuf/types/known/anypb"
+ "runtime"
+)
+
+func (e *Engine) ScanFileSystem(ctx context.Context, directories []string) error {
+ connection := &sourcespb.Filesystem{
+ Directories: directories,
+ }
+ var conn anypb.Any
+ err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
+ if err != nil {
+ logrus.WithError(err).Error("failed to marshal filesystem connection")
+ return err
+ }
+
+ fileSystemSource := filesystem.Source{}
+ err = fileSystemSource.Init(ctx, "local", 0, int64(sourcespb.SourceType_SOURCE_TYPE_FILESYSTEM), true, &conn, runtime.NumCPU())
+ if err != nil {
+ return errors.WrapPrefix(err, "could not init filesystem source", 0)
+ }
+ go func() {
+ err := fileSystemSource.Chunks(ctx, e.ChunksChan())
+ if err != nil {
+ logrus.WithError(err).Error("error scanning filesystem")
+ }
+ close(e.ChunksChan())
+ }()
+ return nil
+}