Improve Bitfinex Detector - Implemented API-Based Secret Validation (#4368)
* updated secret validation method * simplified the code * updated go modules * simplified the code
This commit is contained in:
@@ -30,14 +30,12 @@ require (
|
||||
github.com/aws/smithy-go v1.22.5
|
||||
github.com/aymanbagabas/go-osc52 v1.2.1
|
||||
github.com/bill-rich/go-syslog v0.0.0-20220413021637-49edb52a574c
|
||||
github.com/bitfinexcom/bitfinex-api-go v0.0.0-20210608095005-9e0b26f200fb
|
||||
github.com/bradleyfalzon/ghinstallation/v2 v2.14.0
|
||||
github.com/brianvoe/gofakeit/v7 v7.3.0
|
||||
github.com/charmbracelet/bubbles v0.18.0
|
||||
github.com/charmbracelet/bubbletea v1.3.6
|
||||
github.com/charmbracelet/glamour v0.7.0
|
||||
github.com/charmbracelet/lipgloss v1.1.0
|
||||
github.com/coinbase/waas-client-library-go v1.0.8
|
||||
github.com/couchbase/gocb/v2 v2.10.1
|
||||
github.com/crewjam/rfc5424 v0.1.0
|
||||
github.com/csnewman/dextk v0.3.0
|
||||
@@ -130,11 +128,9 @@ require (
|
||||
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
|
||||
cloud.google.com/go/compute/metadata v0.7.0 // indirect
|
||||
cloud.google.com/go/iam v1.5.2 // indirect
|
||||
cloud.google.com/go/longrunning v0.6.7 // indirect
|
||||
cloud.google.com/go/monitoring v1.24.2 // indirect
|
||||
dario.cat/mergo v1.0.0 // indirect
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect
|
||||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 // indirect
|
||||
@@ -214,7 +210,6 @@ require (
|
||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/go-github/v66 v66.0.0 // indirect
|
||||
github.com/google/go-github/v69 v69.0.0 // indirect
|
||||
github.com/google/go-querystring v1.1.0 // indirect
|
||||
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect
|
||||
@@ -299,7 +294,6 @@ require (
|
||||
github.com/yuin/goldmark-emoji v1.0.2 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.3 // indirect
|
||||
github.com/zeebo/errs v1.4.0 // indirect
|
||||
go.einride.tech/aip v0.60.0 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
||||
go.opentelemetry.io/contrib/detectors/gcp v1.36.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.61.0 // indirect
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
package bitfinex
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"flag"
|
||||
"crypto/hmac"
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
regexp "github.com/wasilibs/go-re2"
|
||||
|
||||
"github.com/bitfinexcom/bitfinex-api-go/v2/rest"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
||||
)
|
||||
|
||||
type Scanner struct {
|
||||
client *http.Client
|
||||
detectors.DefaultMultiPartCredentialProvider
|
||||
}
|
||||
|
||||
@@ -22,23 +28,35 @@ type Scanner struct {
|
||||
var _ detectors.Detector = (*Scanner)(nil)
|
||||
|
||||
var (
|
||||
client = common.SaneHttpClient()
|
||||
defaultClient = 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 (
|
||||
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"}
|
||||
}
|
||||
|
||||
func (s Scanner) Type() detectorspb.DetectorType {
|
||||
return detectorspb.DetectorType_Bitfinex
|
||||
}
|
||||
|
||||
func (s Scanner) Description() string {
|
||||
return "Bitfinex is a cryptocurrency exchange offering various trading options. Bitfinex API keys can be used to access and manage trading accounts."
|
||||
}
|
||||
|
||||
func (s Scanner) getClient() *http.Client {
|
||||
if s.client != nil {
|
||||
return s.client
|
||||
}
|
||||
|
||||
return defaultClient
|
||||
}
|
||||
|
||||
// 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)
|
||||
@@ -66,7 +84,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
|
||||
}
|
||||
|
||||
if verify {
|
||||
isVerified, verificationErr := verifyBitfinex(apiKey, apiSecret)
|
||||
isVerified, verificationErr := verifyBitfinex(ctx, s.getClient(), apiKey, apiSecret)
|
||||
s1.Verified = isVerified
|
||||
s1.SetVerificationError(verificationErr)
|
||||
}
|
||||
@@ -78,26 +96,63 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s Scanner) Type() detectorspb.DetectorType {
|
||||
return detectorspb.DetectorType_Bitfinex
|
||||
}
|
||||
|
||||
func (s Scanner) Description() string {
|
||||
return "Bitfinex is a cryptocurrency exchange offering various trading options. Bitfinex API keys can be used to access and manage trading accounts."
|
||||
}
|
||||
|
||||
// docs: https://docs.bitfinex.com/docs/introduction
|
||||
func verifyBitfinex(key, secret string) (bool, error) {
|
||||
// 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
|
||||
http.DefaultClient = client
|
||||
c := rest.NewClientWithURL(*api).Credentials(key, secret)
|
||||
|
||||
_, err := c.Orders.AllHistory()
|
||||
func verifyBitfinex(ctx context.Context, client *http.Client, apiKey, apiSecret string) (bool, error) {
|
||||
baseURL := "https://api.bitfinex.com"
|
||||
requestPath := "/v2/auth/r/wallets"
|
||||
signaturePath := "/api" + requestPath
|
||||
nonce := fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Microsecond))
|
||||
body := "{}"
|
||||
signaturePayload := signaturePath + nonce + body
|
||||
signature, err := sign(signaturePayload, apiSecret)
|
||||
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)
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, baseURL+requestPath, bytes.NewBuffer([]byte(body)))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("bfx-apikey", apiKey)
|
||||
req.Header.Set("bfx-signature", signature)
|
||||
req.Header.Set("bfx-nonce", nonce)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
return true, nil
|
||||
case http.StatusInternalServerError:
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if strings.Contains(string(body), "apikey: digest invalid") || strings.Contains(string(body), "apikey: invalid") {
|
||||
return false, nil
|
||||
} else {
|
||||
return false, fmt.Errorf("failed to verify Bitfinex API key, error: %s", string(body))
|
||||
}
|
||||
default:
|
||||
return false, fmt.Errorf("unexpected HTTP response status %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func sign(msg, apiSecret string) (string, error) {
|
||||
sig := hmac.New(sha512.New384, []byte(apiSecret))
|
||||
_, err := sig.Write([]byte(msg))
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
return hex.EncodeToString(sig.Sum(nil)), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user