234 lines
6.0 KiB
Go
234 lines
6.0 KiB
Go
package sources
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
<<<<<<< HEAD
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
=======
|
|
"io"
|
|
"net/http"
|
|
"regexp"
|
|
>>>>>>> worktree-agent-adad8c10
|
|
"time"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
"github.com/salvacybersec/keyhunter/pkg/providers"
|
|
"github.com/salvacybersec/keyhunter/pkg/recon"
|
|
)
|
|
|
|
<<<<<<< HEAD
|
|
// TravisCISource searches public Travis CI build logs for leaked API keys.
|
|
// It queries the Travis CI API v3 /builds endpoint for builds matching
|
|
// provider keywords. No authentication required for public repositories.
|
|
=======
|
|
// TravisCISource scrapes public Travis CI build logs for leaked API keys.
|
|
// Travis CI exposes build logs publicly by default for open-source projects.
|
|
// Developers frequently print environment variables or use secrets insecurely
|
|
// in CI scripts, causing API keys to appear in build output.
|
|
>>>>>>> worktree-agent-adad8c10
|
|
type TravisCISource struct {
|
|
BaseURL string
|
|
Registry *providers.Registry
|
|
Limiters *recon.LimiterRegistry
|
|
Client *Client
|
|
}
|
|
|
|
var _ recon.ReconSource = (*TravisCISource)(nil)
|
|
|
|
<<<<<<< HEAD
|
|
func (s *TravisCISource) Name() string { return "travisci" }
|
|
func (s *TravisCISource) RateLimit() rate.Limit { return rate.Every(3 * time.Second) }
|
|
func (s *TravisCISource) Burst() int { return 1 }
|
|
func (s *TravisCISource) RespectsRobots() bool { return true }
|
|
func (s *TravisCISource) Enabled(_ recon.Config) bool { return true }
|
|
|
|
=======
|
|
func (s *TravisCISource) Name() string { return "travisci" }
|
|
func (s *TravisCISource) RateLimit() rate.Limit { return rate.Every(3 * time.Second) }
|
|
func (s *TravisCISource) Burst() int { return 2 }
|
|
func (s *TravisCISource) RespectsRobots() bool { return false }
|
|
func (s *TravisCISource) Enabled(_ recon.Config) bool { return true }
|
|
|
|
// travisBuildResponse represents the Travis CI API builds response.
|
|
>>>>>>> worktree-agent-adad8c10
|
|
type travisBuildResponse struct {
|
|
Builds []travisBuild `json:"builds"`
|
|
}
|
|
|
|
type travisBuild struct {
|
|
<<<<<<< HEAD
|
|
ID int `json:"id"`
|
|
State string `json:"state"`
|
|
Repository travisRepository `json:"repository"`
|
|
}
|
|
|
|
type travisRepository struct {
|
|
Slug string `json:"slug"`
|
|
}
|
|
=======
|
|
ID int `json:"id"`
|
|
State string `json:"state"`
|
|
}
|
|
|
|
// ciLogKeyPattern matches API key patterns commonly leaked in CI logs.
|
|
var ciLogKeyPattern = regexp.MustCompile(`(?i)(api[_-]?key|secret[_-]?key|token|password|credential|auth[_-]?token)['":\s]*[=:]\s*['"]?([a-zA-Z0-9_\-]{16,})['"]?`)
|
|
>>>>>>> worktree-agent-adad8c10
|
|
|
|
func (s *TravisCISource) Sweep(ctx context.Context, _ string, out chan<- recon.Finding) error {
|
|
base := s.BaseURL
|
|
if base == "" {
|
|
base = "https://api.travis-ci.org"
|
|
}
|
|
client := s.Client
|
|
if client == nil {
|
|
client = NewClient()
|
|
}
|
|
|
|
queries := BuildQueries(s.Registry, "travisci")
|
|
<<<<<<< HEAD
|
|
kwIndex := travisKeywordIndex(s.Registry)
|
|
=======
|
|
if len(queries) == 0 {
|
|
return nil
|
|
}
|
|
>>>>>>> worktree-agent-adad8c10
|
|
|
|
for _, q := range queries {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
<<<<<<< HEAD
|
|
=======
|
|
|
|
>>>>>>> worktree-agent-adad8c10
|
|
if s.Limiters != nil {
|
|
if err := s.Limiters.Wait(ctx, s.Name(), s.RateLimit(), s.Burst(), false); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
<<<<<<< HEAD
|
|
endpoint := fmt.Sprintf("%s/builds?limit=20&sort_by=finished_at:desc&state=passed&event_type=push",
|
|
base)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("travisci: build request: %w", err)
|
|
=======
|
|
// Search for builds related to the query keyword.
|
|
searchURL := fmt.Sprintf("%s/builds?search=%s&limit=5", base, q)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchURL, nil)
|
|
if err != nil {
|
|
continue
|
|
>>>>>>> worktree-agent-adad8c10
|
|
}
|
|
req.Header.Set("Travis-API-Version", "3")
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
resp, err := client.Do(ctx, req)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
<<<<<<< HEAD
|
|
var result travisBuildResponse
|
|
decErr := json.NewDecoder(resp.Body).Decode(&result)
|
|
_ = resp.Body.Close()
|
|
if decErr != nil {
|
|
continue
|
|
}
|
|
|
|
provName := kwIndex[strings.ToLower(q)]
|
|
for _, build := range result.Builds {
|
|
source := fmt.Sprintf("https://app.travis-ci.com/%s/builds/%d",
|
|
url.PathEscape(build.Repository.Slug), build.ID)
|
|
f := recon.Finding{
|
|
ProviderName: provName,
|
|
Confidence: "low",
|
|
Source: source,
|
|
SourceType: "recon:travisci",
|
|
DetectedAt: time.Now(),
|
|
}
|
|
select {
|
|
case out <- f:
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
=======
|
|
var builds travisBuildResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&builds); err != nil {
|
|
_ = resp.Body.Close()
|
|
continue
|
|
}
|
|
_ = resp.Body.Close()
|
|
|
|
for _, b := range builds.Builds {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if s.Limiters != nil {
|
|
if err := s.Limiters.Wait(ctx, s.Name(), s.RateLimit(), s.Burst(), false); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Fetch the build log.
|
|
logURL := fmt.Sprintf("%s/builds/%d/log", base, b.ID)
|
|
logReq, err := http.NewRequestWithContext(ctx, http.MethodGet, logURL, nil)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
logReq.Header.Set("Travis-API-Version", "3")
|
|
logReq.Header.Set("Accept", "text/plain")
|
|
|
|
logResp, err := client.Do(ctx, logReq)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
body, err := io.ReadAll(io.LimitReader(logResp.Body, 256*1024))
|
|
_ = logResp.Body.Close()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
if ciLogKeyPattern.Match(body) {
|
|
out <- recon.Finding{
|
|
ProviderName: q,
|
|
Source: logURL,
|
|
SourceType: "recon:travisci",
|
|
Confidence: "medium",
|
|
DetectedAt: time.Now(),
|
|
}
|
|
>>>>>>> worktree-agent-adad8c10
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
<<<<<<< HEAD
|
|
|
|
func travisKeywordIndex(reg *providers.Registry) map[string]string {
|
|
m := make(map[string]string)
|
|
if reg == nil {
|
|
return m
|
|
}
|
|
for _, p := range reg.List() {
|
|
for _, k := range p.Keywords {
|
|
kl := strings.ToLower(strings.TrimSpace(k))
|
|
if kl != "" {
|
|
if _, exists := m[kl]; !exists {
|
|
m[kl] = p.Name
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
=======
|
|
>>>>>>> worktree-agent-adad8c10
|