-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from cerberauth/discover-openapi
feat: scan for discoverable openapi files
- Loading branch information
Showing
13 changed files
with
254 additions
and
58 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
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
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
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
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
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
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,11 @@ | ||
package scan | ||
|
||
import "github.com/cerberauth/vulnapi/scan/discover" | ||
|
||
func (s *Scan) WithServerSignatureScan() *Scan { | ||
return s.AddScanHandler(discover.ServerSignatureScanHandler) | ||
} | ||
|
||
func (s *Scan) WithAllDiscoverScans() *Scan { | ||
return s.WithServerSignatureScan() | ||
} |
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,83 @@ | ||
package discover | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/cerberauth/vulnapi/internal/auth" | ||
"github.com/cerberauth/vulnapi/internal/request" | ||
"github.com/cerberauth/vulnapi/internal/scan" | ||
"github.com/cerberauth/vulnapi/report" | ||
) | ||
|
||
const ( | ||
DiscoverableOpenAPISeverityLevel = 1 | ||
DiscoverableOpenAPIVulnerabilityName = "Discoverable OpenAPI" | ||
DiscoverableOpenAPIVulnerabilityDescription = "An OpenAPI file is exposed without protection. This can lead to information disclosure and security issues" | ||
) | ||
|
||
var possibleOpenAPIPaths = []string{ | ||
"/openapi", | ||
"/swagger.json", | ||
"/swagger.yaml", | ||
"/openapi.json", | ||
"/openapi.yaml", | ||
"/api-docs", | ||
"/api-docs.json", | ||
"/api-docs.yaml", | ||
"/api-docs.yml", | ||
"/v2/api-docs", | ||
"/v3/api-docs", | ||
".well-known/openapi.json", | ||
".well-known/openapi.yaml", | ||
} | ||
|
||
func extractBase(inputURL string) (*url.URL, error) { | ||
parsedURL, err := url.Parse(inputURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
baseURL := fmt.Sprintf("%s://%s%s", parsedURL.Scheme, parsedURL.Host, parsedURL.Path) | ||
base, err := url.Parse(baseURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return base, nil | ||
} | ||
|
||
func DiscoverableOpenAPIScanHandler(operation *request.Operation, securityScheme auth.SecurityScheme) (*report.ScanReport, error) { | ||
r := report.NewScanReport() | ||
|
||
securityScheme.SetAttackValue(securityScheme.GetValidValue()) | ||
|
||
base, err := extractBase(operation.Url) | ||
if err != nil { | ||
return r, err | ||
} | ||
|
||
for _, path := range possibleOpenAPIPaths { | ||
newOperation := operation.Clone() | ||
newOperation.Url = base.ResolveReference(&url.URL{Path: path}).String() | ||
|
||
attempt, err := scan.ScanURL(newOperation, &securityScheme) | ||
r.AddScanAttempt(attempt).End() | ||
if err != nil { | ||
return r, err | ||
} | ||
|
||
if attempt.Response.StatusCode < 300 { | ||
r.AddVulnerabilityReport(&report.VulnerabilityReport{ | ||
SeverityLevel: DiscoverableOpenAPISeverityLevel, | ||
Name: DiscoverableOpenAPIVulnerabilityName, | ||
Description: DiscoverableOpenAPIVulnerabilityDescription, | ||
Operation: newOperation, | ||
}) | ||
|
||
return r, nil | ||
} | ||
} | ||
|
||
return r, nil | ||
} |
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,60 @@ | ||
package discover_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/cerberauth/vulnapi/internal/auth" | ||
"github.com/cerberauth/vulnapi/internal/request" | ||
"github.com/cerberauth/vulnapi/report" | ||
"github.com/cerberauth/vulnapi/scan/discover" | ||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDiscoverableScannerWithNoDiscoverableOpenAPI(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
securityScheme := auth.NewNoAuthSecurityScheme() | ||
operation := request.NewOperation("http://localhost:8080/", "GET", nil, nil, nil) | ||
|
||
httpmock.RegisterResponder(operation.Method, operation.Url, httpmock.NewBytesResponder(204, nil).HeaderAdd(http.Header{"Server": []string{"Apache/2.4.29 (Ubuntu)"}})) | ||
httpmock.RegisterNoResponder(func(req *http.Request) (*http.Response, error) { | ||
return httpmock.NewStringResponse(404, "Not Found"), nil | ||
}) | ||
|
||
report, err := discover.DiscoverableOpenAPIScanHandler(operation, securityScheme) | ||
|
||
require.NoError(t, err) | ||
assert.Greater(t, httpmock.GetTotalCallCount(), 10) | ||
assert.False(t, report.HasVulnerabilityReport()) | ||
} | ||
|
||
func TestDiscoverableScannerWithOneDiscoverableOpenAPI(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
securityScheme := auth.NewNoAuthSecurityScheme() | ||
operation := request.NewOperation("http://localhost:8080/openapi.yaml", "GET", nil, nil, nil) | ||
httpmock.RegisterResponder(operation.Method, operation.Url, httpmock.NewBytesResponder(204, nil).HeaderAdd(http.Header{"Server": []string{"Apache/2.4.29 (Ubuntu)"}})) | ||
httpmock.RegisterNoResponder(func(req *http.Request) (*http.Response, error) { | ||
return httpmock.NewStringResponse(404, "Not Found"), nil | ||
}) | ||
|
||
expectedReport := report.VulnerabilityReport{ | ||
SeverityLevel: discover.DiscoverableOpenAPISeverityLevel, | ||
Name: discover.DiscoverableOpenAPIVulnerabilityName, | ||
Description: discover.DiscoverableOpenAPIVulnerabilityDescription, | ||
Operation: operation, | ||
} | ||
|
||
report, err := discover.DiscoverableOpenAPIScanHandler(operation, securityScheme) | ||
|
||
require.NoError(t, err) | ||
assert.Greater(t, httpmock.GetTotalCallCount(), 0) | ||
assert.True(t, report.HasVulnerabilityReport()) | ||
assert.Equal(t, report.GetVulnerabilityReports()[0].Name, expectedReport.Name) | ||
assert.Equal(t, report.GetVulnerabilityReports()[0].Operation.Url, expectedReport.Operation.Url) | ||
} |
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
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
Oops, something went wrong.