mirror of
https://github.com/trufflesecurity/trufflehog.git
synced 2024-11-15 01:17:34 +00:00
ce1ce29b90
* optimize maching detetors * update method name * updates * update naming * updates * update comment * updates * remove testcase * update default match len to 512 * update * update test * add support for multpart cred provider * add ability to scan entire chunk * encapsulate matches logic within FindDetectorMatches * use []byte directly * nil chunk data * use []byte * set hidden flag to true * remove * [refactor] - multi part detectors (#2906) * Detectors beginning w/ a * Detectors beginning w/ b * Detectors beginning w/ c * Detectors beginning w/ d * Detectors beginning w/ e * Detectors beginning w/ f * Detectors beginning w/ f&g * fix * Detectors beginning w/ i-l * Detectors beginning w/ m-p * Detectors beginning w/ r-s * Detectors beginning w/ t * Detectors beginning w/ u-z * revert alconst * remaining fixes * lint * [feat] - Add Support for `compareDetectionStrategies` Mode (#2918) * Detector comparison mode * remove else * return error if results dont match * update default hidden flag to not scan entire chunks * fix tests * enhance encapsulation by including methods on DetectorMatch to handle merging and extracting * remove space * fix * update detector * updates * remove else * run comparison concurrently
110 lines
3 KiB
Go
110 lines
3 KiB
Go
package sqlserver
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
|
|
regexp "github.com/wasilibs/go-re2"
|
|
|
|
mssql "github.com/microsoft/go-mssqldb"
|
|
"github.com/microsoft/go-mssqldb/msdsn"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
|
)
|
|
|
|
type Scanner struct{}
|
|
|
|
// Ensure the Scanner satisfies the interface at compile time.
|
|
var _ detectors.Detector = (*Scanner)(nil)
|
|
|
|
var (
|
|
// SQLServer connection string is a semicolon delimited set of case-insensitive parameters which may go in any order.
|
|
pattern = regexp.MustCompile("(?:\n|`|'|\"| )?((?:[A-Za-z0-9_ ]+=[^;$'`\"$]+;?){3,})(?:'|`|\"|\r\n|\n)?")
|
|
)
|
|
|
|
// 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{"sql", "database", "Data Source", "Server=", "Network address="}
|
|
}
|
|
|
|
// FromData will find and optionally verify SpotifyKey secrets in a given set of bytes.
|
|
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
|
|
matches := pattern.FindAllStringSubmatch(string(data), -1)
|
|
for _, match := range matches {
|
|
paramsUnsafe, err := msdsn.Parse(match[1])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
if paramsUnsafe.Password == "" {
|
|
continue
|
|
}
|
|
|
|
s1 := detectors.Result{
|
|
DetectorType: detectorspb.DetectorType_SQLServer,
|
|
Raw: []byte(paramsUnsafe.Password),
|
|
RawV2: []byte(paramsUnsafe.URL().String()),
|
|
Redacted: detectors.RedactURL(*paramsUnsafe.URL()),
|
|
}
|
|
|
|
if verify {
|
|
isVerified, err := ping(paramsUnsafe)
|
|
|
|
s1.Verified = isVerified
|
|
|
|
if mssqlErr, isMssqlErr := err.(mssql.Error); isMssqlErr && mssqlErr.Number == 18456 {
|
|
// Login failed
|
|
// Number taken from https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors?view=sql-server-ver16
|
|
// Nothing to do; determinate failure to verify
|
|
} else {
|
|
s1.SetVerificationError(err, paramsUnsafe.Password)
|
|
}
|
|
}
|
|
|
|
results = append(results, s1)
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
var ping = func(config msdsn.Config) (bool, error) {
|
|
cleanConfig := msdsn.Config{}
|
|
cleanConfig.Host = config.Host
|
|
cleanConfig.Port = config.Port
|
|
cleanConfig.User = config.User
|
|
cleanConfig.Password = config.Password
|
|
cleanConfig.Database = config.Database
|
|
cleanConfig.DisableRetry = true
|
|
cleanConfig.Encryption = config.Encryption
|
|
cleanConfig.TLSConfig = config.TLSConfig
|
|
cleanConfig.Instance = config.Instance
|
|
cleanConfig.DialTimeout = time.Second * 3
|
|
cleanConfig.ConnTimeout = time.Second * 3
|
|
|
|
url := cleanConfig.URL()
|
|
query := url.Query()
|
|
url.RawQuery = query.Encode()
|
|
|
|
conn, err := sql.Open("mssql", url.String())
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer func() {
|
|
_ = conn.Close()
|
|
}()
|
|
|
|
err = conn.Ping()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func (s Scanner) Type() detectorspb.DetectorType {
|
|
return detectorspb.DetectorType_SQLServer
|
|
}
|