Skip to content

[Feat] Detector implementation for Azure API Management Direct Management Key #3938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

abmussani
Copy link
Contributor

Description:

This PR implements a new Detector for Azure Direct management API key. This detector looks for direct manage url and keys (Primary or Secondary), generates a access token with expiry of 5 seconds (to avoid leakage) and verify the credentials based on generated values.

Test Output:

$ go test -tags=detectors ./pkg/detectors/azuredirectmanagementkey/
ok  	github.com/trufflesecurity/trufflehog/v3/pkg/detectors/azuredirectmanagementkey	6.881s

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@abmussani abmussani requested review from a team as code owners February 26, 2025 15:54
s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_AzureDirectManagementKey,
Raw: []byte(baseUrl),
RawV2: []byte(baseUrl + primaryKey),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we start making RawV2 structured in new detectors? #3634 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgmz I am also not in favor of using RawV2 as structured secret. As @ahrav mentioned that it is tightly coupled with Enterprise and (in future) if we need to restructure it, due to any reason, We wont be able to do it.

My suggestion is to have another Map for this purpose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... it is tightly coupled with Enterprise and (in future) if we need to restructure it, due to any reason, We wont be able to do it.

That is the case regardless. Making it structured at least makes it parseable and human-readable, unlike doing a simple concatenation.

var (
defaultClient = common.SaneHttpClient()
urlPat = regexp.MustCompile(`https://([a-z0-9][a-z0-9-]{0,48}[a-z0-9])\.management\.azure-api\.net`) // https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.APIM.Name/
primaryKeyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"azure"}) + `\b([a-zA-Z0-9+\/-]{86,88}\b={0,2})`) // Base64-encoded primary key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why - at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rgmz Initially I copied that regex from another detector, Azure Storage. After your comment, I generated 20 times a key and confidently say that all of them ended with [a-zA-Z0-9]==. Updated the regex.

@abmussani abmussani force-pushed the azuredirectmanagementapikey-detector-impl branch from eb37f00 to a76aa3d Compare February 28, 2025 10:52
@rgmz rgmz mentioned this pull request Feb 28, 2025
2 tasks
var (
defaultClient = common.SaneHttpClient()
urlPat = regexp.MustCompile(`https://([a-z0-9][a-z0-9-]{0,48}[a-z0-9])\.management\.azure-api\.net`) // https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.APIM.Name/
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"azure", ".management.azure-api.net"}) + `\b([a-zA-Z0-9+\/]{83,85}[a-zA-Z0-9]==)`) // pattern for both Primary and secondary key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\b isn't valid when beside / or +.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has bitten me so many times... 😭

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the regex but AzureContainerRegistry Detector has the similar issue. right ?

@abmussani abmussani changed the title [Feature] Detector implementation for Azure API Management Direct Management Key [Feat] Detector implementation for Azure API Management Direct Management Key Mar 5, 2025
@abmussani abmussani force-pushed the azuredirectmanagementapikey-detector-impl branch from b132a61 to 0a16736 Compare April 9, 2025 10:39
@abmussani abmussani force-pushed the azuredirectmanagementapikey-detector-impl branch from ca0e501 to 9345878 Compare April 21, 2025 10:53
Copy link
Contributor

@kashifkhan0771 kashifkhan0771 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one comment related to False Positive Implementation. Rest LGTM!

return "Azure API Management provides a direct management REST API for performing operations on selected entities, such as users, groups, products, and subscriptions."
}

func (s Scanner) IsFalsePositive(_ detectors.Result) (bool, string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to skip some specific keyword can we implement it as this

@zricethezav
Copy link
Collaborator

@abmussani gotta fix conflicts here too

@abmussani abmussani merged commit 18c009f into trufflesecurity:main May 8, 2025
13 checks passed
abmussani added a commit to abmussani/trufflehog that referenced this pull request May 8, 2025
* main: (40 commits)
  [Feat] Detector implementation for Azure API Management Direct Management Key (trufflesecurity#3938)
  Fastly Analyzer (trufflesecurity#4082)
  Postman Code Uses Consistent Casing for Id Var Names (trufflesecurity#4124)
  Normalize UID to Uid in Postman Code (trufflesecurity#4125)
  postman_client.IDNameUUID becomes IdNameUid (trufflesecurity#4123)
  Fixed Kontent Detector (trufflesecurity#4122)
  [Fix] Added Prefix In Dockerhub Detector Regex (trufflesecurity#4084)
  Issue - 3697 - GitHub analyzer panic (trufflesecurity#4113)
  fix(test/snowflake): Snowflake flaky pattern test (trufflesecurity#4083)
  Netlify Analyzer (trufflesecurity#4106)
  Add xAI detector (trufflesecurity#4117)
  added tunnel authtoken detection in ngrok detector (trufflesecurity#4115)
  Postman increase http client timeout (trufflesecurity#4086)
  feat: bing subscription key support (trufflesecurity#4092)
  Improve precommit hook setup and support both global and repo pre-commit hooks, frameworkless and with pre-commit or husky (trufflesecurity#4098)
  Make a first pass at some structural introduction docs (trufflesecurity#4076)
  Update action.yml (trufflesecurity#4108)
  Refactor Netlify Detector (trufflesecurity#4102)
  chore: added github action custom detector name (trufflesecurity#3417)
  Fixed PubnubsubsciptionKey detector verification (trufflesecurity#4107)
  ...

# Conflicts:
#	pkg/engine/defaults/defaults.go
#	pkg/pb/detectorspb/detectors.pb.go
#	proto/detectors.proto
abmussani added a commit to abmussani/trufflehog that referenced this pull request May 16, 2025
* main:
  [Feat] Added Mux API Analyzer (trufflesecurity#4128)
  fixed name of netlify analyzer in cli output (trufflesecurity#4140)
  fix(discordwebhook): Update Discord webhook detector to support 19-digit IDs (trufflesecurity#4133)
  [Feat] Added New AccuWeather Detector Version (trufflesecurity#4114)
  [Feat] Added Ngrok API Key Analyzer (trufflesecurity#4110)
  Improved JDBC Detector Regex (trufflesecurity#4109)
  [Feat] Detector implementation for Azure Configuration Connection String Key (trufflesecurity#3939)
  test(sources/s3): fix missing region error (trufflesecurity#4131)
  feat(sources/s3): migrate to AWS SDK v2 (trufflesecurity#4069)
  Update PreCommit.md (trufflesecurity#4112)
  Exclusion of FalsePositive GH's usernames in PrivateKeyDetector (trufflesecurity#4046)
  Monday App Analyzer (trufflesecurity#4120)
  [Feat] Detector implementation for Azure API Management Direct Management Key (trufflesecurity#3938)
  Fastly Analyzer (trufflesecurity#4082)
  Postman Code Uses Consistent Casing for Id Var Names (trufflesecurity#4124)
  Normalize UID to Uid in Postman Code (trufflesecurity#4125)
  postman_client.IDNameUUID becomes IdNameUid (trufflesecurity#4123)
  Fixed Kontent Detector (trufflesecurity#4122)

# Conflicts:
#	pkg/analyzer/analyzers/analyzers.go
#	pkg/analyzer/cli.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants