-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OC-103] - Add Gemini detector (#800)
* Add Gemini detector. * Add regex and test code for Gemini detector. * Remove else. * Add commentary. * Address comments. * Use regular else. * Make nice and complicated. * use regular detection pattern. * Add detector to default detectors.
- Loading branch information
Showing
3 changed files
with
276 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package gemini | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/hmac" | ||
"crypto/sha512" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"encoding/json" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"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{} | ||
|
||
const ( | ||
baseURL = "https://api.gemini.com" | ||
accountDetail = "/v1/account" | ||
account = "primary" | ||
) | ||
|
||
// Ensure the Scanner satisfies the interface at compile time. | ||
var _ detectors.Detector = (*Scanner)(nil) | ||
|
||
var ( | ||
client = common.SaneHttpClient() | ||
|
||
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives. | ||
keyPat = regexp.MustCompile(`\b((?:master-|account-)[0-9A-Za-z]{20})\b`) | ||
secretPat = regexp.MustCompile(`[A-Za-z0-9]{27,28}`) | ||
) | ||
|
||
// 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{"master-", "account-"} | ||
} | ||
|
||
// FromData will find and optionally verify Gemini secrets in a given set of bytes. | ||
func (s Scanner) FromData(_ context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | ||
dataStr := string(data) | ||
|
||
idMatches := keyPat.FindAllStringSubmatch(dataStr, -1) | ||
secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1) | ||
|
||
for _, match := range idMatches { | ||
if len(match) != 2 { | ||
continue | ||
} | ||
resMatch := strings.TrimSpace(match[1]) | ||
|
||
for _, secretMatch := range secretMatches { | ||
resSecretMatch := strings.TrimSpace(secretMatch[0]) | ||
|
||
s1 := detectors.Result{ | ||
DetectorType: detectorspb.DetectorType_Gemini, | ||
Raw: []byte(resMatch), | ||
RawV2: []byte(resMatch + resSecretMatch), | ||
} | ||
|
||
if verify { | ||
req, err := constructRequest(resSecretMatch, resMatch) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
res, err := client.Do(req) | ||
if err == nil { | ||
defer res.Body.Close() | ||
if res.StatusCode >= 200 && res.StatusCode < 300 { | ||
s1.Verified = true | ||
} else { | ||
if detectors.IsKnownFalsePositive(resSecretMatch, detectors.DefaultFalsePositives, true) { | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
results = append(results, s1) | ||
} | ||
} | ||
|
||
return detectors.CleanResults(results), nil | ||
} | ||
|
||
func constructRequest(secret, keyID string) (*http.Request, error) { | ||
req, err := http.NewRequest("POST", baseURL+accountDetail, &bytes.Buffer{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
params := map[string]interface{}{ | ||
"request": accountDetail, | ||
"nonce": time.Now().UnixNano(), | ||
} | ||
|
||
acct := strings.Split(keyID, "-") | ||
// Not entirely sure how to handle master account keys where one of the accounts is named "primary". | ||
if len(acct) > 1 && acct[0] == "master" { | ||
params["account"] = account | ||
} | ||
|
||
reqStr, err := json.Marshal(¶ms) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
payload := base64.StdEncoding.EncodeToString(reqStr) | ||
signature := constructSignature(payload, secret) | ||
|
||
req.Header = http.Header{ | ||
"Content-Type": {"text/plain"}, | ||
"Content-Length": {"0"}, | ||
"X-GEMINI-APIKEY": {keyID}, | ||
"X-GEMINI-PAYLOAD": {payload}, | ||
"X-GEMINI-SIGNATURE": {signature}, | ||
"Cache-Control": {"no-cache"}, | ||
} | ||
return req, err | ||
} | ||
|
||
func constructSignature(payload string, resSecretMatch string) string { | ||
h := hmac.New(sha512.New384, []byte(resSecretMatch)) | ||
h.Write([]byte(payload)) | ||
signature := hex.EncodeToString(h.Sum(nil)) | ||
return signature | ||
} |
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,140 @@ | ||
//go:build detectors | ||
// +build detectors | ||
|
||
package gemini | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
func TestGemini_FromChunk(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors2") | ||
if err != nil { | ||
t.Fatalf("could not get test secrets from GCP: %s", err) | ||
} | ||
secretMaster := testSecrets.MustGetField("GEMINI") | ||
keyMaster := testSecrets.MustGetField("GEMINI_KEY") | ||
secretAccount := testSecrets.MustGetField("GEMINI_ACCOUNT") | ||
keyAccount := testSecrets.MustGetField("GEMINI_KEY_ACCOUNT") | ||
inactiveSecret := testSecrets.MustGetField("GEMINI_INACTIVE") | ||
|
||
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; master", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a gemini %s and secretMaster %s within", keyMaster, secretMaster)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_Gemini, | ||
Verified: true, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "found, verified; account", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a gemini %s and secretAccount %s within", keyAccount, secretAccount)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_Gemini, | ||
Verified: true, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "found, unverified", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a gemini secretMaster %s and secretMaster %s within but not valid", keyMaster, inactiveSecret)), // the secretMaster would satisfy the regex but not pass validation | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_Gemini, | ||
Verified: false, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "not found", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte("You cannot find the secretMaster 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("Gemini.FromData() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
for i := range got { | ||
if len(got[i].Raw) == 0 { | ||
t.Fatalf("no raw secretMaster present: \n %+v", got[i]) | ||
} | ||
got[i].Raw = nil | ||
} | ||
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "RawV2") | ||
if diff := cmp.Diff(tt.want, got, ignoreOpts); diff != "" { | ||
t.Errorf("Gemini.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