Skip to content

Find, verify, and analyze leaked credentials

added bitfinex detector

Jonee Ryan Ty9c921f1a3a98
diff --git a/pkg/detectors/bitfinex/bitfinex.go b/pkg/detectors/bitfinex/bitfinex.go
new file mode 100644
index 000000000..43dbe6301
--- /dev/null
+++ b/pkg/detectors/bitfinex/bitfinex.go
@@ -0,0 +1,102 @@
+package bitfinex
+
+import (
+ "context"
+ "flag"
+ "regexp"
+ "strings"
+
+ "github.com/bitfinexcom/bitfinex-api-go/v2/rest"
+ "github.com/trufflesecurity/trufflehog/pkg/detectors"
+
+ "github.com/trufflesecurity/trufflehog/pkg/common"
+ "github.com/trufflesecurity/trufflehog/pkg/pb/detectorspb"
+)
+
+type Scanner struct{}
+
+// Ensure the Scanner satisfies the interface at compile time
+var _ detectors.Detector = (*Scanner)(nil)
+
+var (
+ client = common.SaneHttpClient()
+
+ // related resource https://medium.com/@Bitfinex/api-development-update-april-65fe52f84124
+ apiKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"bitfinex"}) + `\b([A-Za-z0-9_-]{43})\b`)
+ apiSecretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"bitfinex"}) + `\b([A-Za-z0-9_-]{43})\b`)
+)
+
+var (
+ orderid = flag.String("id", "", "lookup trades for an order ID")
+ api = flag.String("api", "https://api-pub.bitfinex.com/v2/", "v2 REST API URL")
+)
+
+// 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{"bitfinex"}
+}
+
+// FromData will find and optionally verify Bitfinex 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)
+
+ apiKeyMatches := apiKeyPat.FindAllStringSubmatch(dataStr, -1)
+ apiSecretMatches := apiSecretPat.FindAllStringSubmatch(dataStr, -1)
+
+ for _, apiKeyMatch := range apiKeyMatches {
+ if len(apiKeyMatch) != 2 {
+ continue
+ }
+ apiKeyRes := strings.TrimSpace(apiKeyMatch[1])
+
+ s1 := detectors.Result{
+ DetectorType: detectorspb.DetectorType_Bitfinex,
+ Raw: []byte(apiKeyRes),
+ }
+
+ for _, apiSecretMatch := range apiSecretMatches {
+ if len(apiSecretMatch) != 2 {
+ continue
+ }
+ apiSecretRes := strings.TrimSpace(apiSecretMatch[1])
+
+ if apiKeyRes == apiSecretRes {
+ continue
+ }
+
+ if verify {
+ // thankfully official golang examples exist but you just need to dig their many repos https://github.com/bitfinexcom/bitfinex-api-go/blob/master/examples/v2/rest-orders/main.go
+ key := apiKeyRes
+ secret := apiSecretRes
+ c := rest.NewClientWithURL(*api).Credentials(key, secret)
+
+ isValid := true // assume valid
+ _, err = c.Orders.AllHistory()
+ if err != nil {
+ if strings.HasPrefix(err.Error(), "POST https://") { // eg POST https://api-pub.bitfinex.com/v2/auth/r/orders/hist: 500 apikey: digest invalid (10100)
+ isValid = false
+ } else {
+ if detectors.IsKnownFalsePositive(apiKeyRes, detectors.DefaultFalsePositives, true) {
+ continue
+ }
+ }
+ }
+
+ s1.Verified = isValid
+ // If there is a valid one, we need to stop iterating now and return the valid result
+ if isValid == true {
+ break
+ }
+ }
+ }
+
+ // By appending resutls in the outer loop we can reduce false positives if there are multiple
+ // combinations of secrets and IDs found.
+ if len(apiSecretMatches) > 0 {
+ results = append(results, s1)
+ }
+ }
+
+ return detectors.CleanResults(results), nil
+}
diff --git a/pkg/detectors/bitfinex/bitfinex_test.go b/pkg/detectors/bitfinex/bitfinex_test.go
new file mode 100644
index 000000000..13d53af61
--- /dev/null
+++ b/pkg/detectors/bitfinex/bitfinex_test.go
@@ -0,0 +1,123 @@
+package bitfinex
+
+import (
+ "context"
+ "fmt"
+ "testing"
+ "time"
+
+ "github.com/kylelemons/godebug/pretty"
+ "github.com/trufflesecurity/trufflehog/pkg/detectors"
+
+ "github.com/trufflesecurity/trufflehog/pkg/common"
+ "github.com/trufflesecurity/trufflehog/pkg/pb/detectorspb"
+)
+
+func TestBitfinex_FromChunk(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
+ defer cancel()
+ testSecrets, err := common.GetScannersTestSecretsVersion(ctx, 3)
+ if err != nil {
+ t.Fatalf("could not get test secrets from GCP: %s", err)
+ }
+ apiKey := testSecrets.MustGetField("BITFINEX_API_KEY")
+ inactiveApiKey := testSecrets.MustGetField("BITFINEX_API_KEY_INACTIVE")
+ apiSecret := testSecrets.MustGetField("BITFINEX_API_SECRET")
+
+ type args struct {
+ ctx context.Context
+ data []byte
+ verify bool
+ }
+ tests := []struct {
+ name string
+ s Scanner
+ args args
+ want []detectors.Result
+ wantErr bool
+ }{
+ {
+ name: "found, verified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a bitfinex api key %s within bitfinex api secret %s", apiKey, apiSecret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ // will try to scan (apiKey, apiSecret) which will verify then (apiSecret, apiKey) which will not since both parameters have equal length
+ {
+ DetectorType: detectorspb.DetectorType_Bitfinex,
+ Verified: true,
+ },
+ {
+ DetectorType: detectorspb.DetectorType_Bitfinex,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "found, unverified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a bitfinex api key %s within bitfinex api secret %s", inactiveApiKey, apiSecret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Bitfinex,
+ Verified: false,
+ },
+ {
+ DetectorType: detectorspb.DetectorType_Bitfinex,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "not found",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte("You cannot find the secret within"),
+ verify: true,
+ },
+ want: nil,
+ 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("Bitfinex.FromData() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ for i := range got {
+ if len(got[i].Raw) == 0 {
+ t.Fatalf("no raw secret present: \n %+v", got[i])
+ }
+ got[i].Raw = nil
+ }
+ if diff := pretty.Compare(got, tt.want); diff != "" {
+ t.Errorf("Bitfinex.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++ {
+ s.FromData(ctx, false, data)
+ }
+ })
+ }
+}