Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do local URI verification, while attempting to defuse SSRF (#879)
* simplify monogo pattern * do URI verification locally, while attempting to defuse SSRF * test SSRF defuse * simplify err check logic per linter recommendation * split up detectors * address comments * remove unused var
- Loading branch information
1 parent
fe1e475
commit a7fc122
Showing
13 changed files
with
550 additions
and
83 deletions.
There are no files selected for viewing
This file contains 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 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,98 @@ | ||
package ftp | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/jlaffaye/ftp" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
type Scanner struct{} | ||
|
||
// Ensure the Scanner satisfies the interface at compile time. | ||
var _ detectors.Detector = (*Scanner)(nil) | ||
|
||
var ( | ||
keyPat = regexp.MustCompile(`\bftp://[\S]{3,50}:([\S]{3,50})@[-.%\w\/:]+\b`) | ||
) | ||
|
||
// Keywords are used for efficiently pre-filtering chunks. | ||
// Use identifiers in the secret preferably, or the provider name. | ||
func (s Scanner) Keywords() []string { | ||
return []string{"ftp://"} | ||
} | ||
|
||
// FromData will find and optionally verify URI secrets in a given set of bytes. | ||
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | ||
dataStr := string(data) | ||
|
||
matches := keyPat.FindAllStringSubmatch(dataStr, -1) | ||
|
||
for _, match := range matches { | ||
urlMatch := match[0] | ||
password := match[1] | ||
|
||
// Skip findings where the password only has "*" characters, this is a redacted password | ||
if strings.Trim(password, "*") == "" { | ||
continue | ||
} | ||
|
||
parsedURL, err := url.Parse(urlMatch) | ||
if err != nil { | ||
continue | ||
} | ||
if _, ok := parsedURL.User.Password(); !ok { | ||
continue | ||
} | ||
|
||
redact := strings.TrimSpace(strings.Replace(urlMatch, password, strings.Repeat("*", len(password)), -1)) | ||
|
||
s := detectors.Result{ | ||
DetectorType: detectorspb.DetectorType_FTP, | ||
Raw: []byte(urlMatch), | ||
Redacted: redact, | ||
} | ||
|
||
if verify { | ||
s.Verified = verifyFTP(ctx, parsedURL) | ||
} | ||
|
||
if !s.Verified { | ||
// Skip unverified findings where the password starts with a `$` - it's almost certainly a variable. | ||
if strings.HasPrefix(password, "$") { | ||
continue | ||
} | ||
} | ||
|
||
if !s.Verified && detectors.IsKnownFalsePositive(string(s.Raw), detectors.DefaultFalsePositives, false) { | ||
continue | ||
} | ||
|
||
results = append(results, s) | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
func verifyFTP(ctx context.Context, u *url.URL) bool { | ||
host := u.Host | ||
if !strings.Contains(host, ":") { | ||
host = host + ":21" | ||
} | ||
|
||
c, err := ftp.Dial(host, ftp.DialWithTimeout(5*time.Second)) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
password, _ := u.User.Password() | ||
err = c.Login(u.User.Username(), password) | ||
|
||
return err == nil | ||
} |
This file contains 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,109 @@ | ||
//go:build detectors | ||
// +build detectors | ||
|
||
package ftp | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/kylelemons/godebug/pretty" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
func TestURI_FromChunk(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
data []byte | ||
verify bool | ||
} | ||
tests := []struct { | ||
name string | ||
s Scanner | ||
args args | ||
want []detectors.Result | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "bad scheme", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte("file://user:pass@foo.com:123/wh/at/ever"), | ||
verify: true, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "verified FTP", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
// https://dlptest.com/ftp-test/ | ||
data: []byte("ftp://dlpuser:rNrKYTX9g7z3RgJRmxWuGHbeu@ftp.dlptest.com"), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_FTP, | ||
Verified: true, | ||
Redacted: "ftp://dlpuser:*************************@ftp.dlptest.com", | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "unverified FTP", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
// https://dlptest.com/ftp-test/ | ||
data: []byte("ftp://dlpuser:invalid@ftp.dlptest.com"), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_FTP, | ||
Verified: false, | ||
Redacted: "ftp://dlpuser:*******@ftp.dlptest.com", | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := Scanner{} | ||
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("URI.FromData() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
// if os.Getenv("FORCE_PASS_DIFF") == "true" { | ||
// return | ||
// } | ||
for i := range got { | ||
got[i].Raw = nil | ||
} | ||
if diff := pretty.Compare(got, tt.want); diff != "" { | ||
t.Errorf("URI.FromData() %s diff: (-got +want)\n%s", tt.name, diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkFromData(benchmark *testing.B) { | ||
ctx := context.Background() | ||
s := Scanner{} | ||
for name, data := range detectors.MustGetBenchmarkData() { | ||
benchmark.Run(name, func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
_, err := s.FromData(ctx, false, data) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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 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,96 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/go-redis/redis" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
type Scanner struct{} | ||
|
||
// Ensure the Scanner satisfies the interface at compile time. | ||
var _ detectors.Detector = (*Scanner)(nil) | ||
|
||
var ( | ||
keyPat = regexp.MustCompile(`\bredis://[\S]{3,50}:([\S]{3,50})@[-.%\w\/:]+\b`) | ||
) | ||
|
||
// Keywords are used for efficiently pre-filtering chunks. | ||
// Use identifiers in the secret preferably, or the provider name. | ||
func (s Scanner) Keywords() []string { | ||
return []string{"redis"} | ||
} | ||
|
||
// FromData will find and optionally verify URI secrets in a given set of bytes. | ||
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | ||
dataStr := string(data) | ||
|
||
matches := keyPat.FindAllStringSubmatch(dataStr, -1) | ||
|
||
for _, match := range matches { | ||
urlMatch := match[0] | ||
password := match[1] | ||
|
||
// Skip findings where the password only has "*" characters, this is a redacted password | ||
if strings.Trim(password, "*") == "" { | ||
continue | ||
} | ||
|
||
parsedURL, err := url.Parse(urlMatch) | ||
if err != nil { | ||
continue | ||
} | ||
if _, ok := parsedURL.User.Password(); !ok { | ||
continue | ||
} | ||
|
||
redact := strings.TrimSpace(strings.Replace(urlMatch, password, strings.Repeat("*", len(password)), -1)) | ||
|
||
s := detectors.Result{ | ||
DetectorType: detectorspb.DetectorType_Redis, | ||
Raw: []byte(urlMatch), | ||
Redacted: redact, | ||
} | ||
|
||
if verify { | ||
s.Verified = verifyRedis(ctx, parsedURL) | ||
} | ||
|
||
if !s.Verified { | ||
// Skip unverified findings where the password starts with a `$` - it's almost certainly a variable. | ||
if strings.HasPrefix(password, "$") { | ||
continue | ||
} | ||
} | ||
|
||
if !s.Verified && detectors.IsKnownFalsePositive(string(s.Raw), detectors.DefaultFalsePositives, false) { | ||
continue | ||
} | ||
|
||
results = append(results, s) | ||
} | ||
|
||
return results, nil | ||
} | ||
|
||
func verifyRedis(ctx context.Context, u *url.URL) bool { | ||
opt, err := redis.ParseURL(u.String()) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
client := redis.NewClient(opt) | ||
|
||
status, err := client.Ping().Result() | ||
if err == nil && status == "PONG" { | ||
return true | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.