package sources import ( "context" "encoding/json" "fmt" "net/http" "strings" "time" "golang.org/x/time/rate" "github.com/salvacybersec/keyhunter/pkg/providers" "github.com/salvacybersec/keyhunter/pkg/recon" ) // JenkinsSource searches publicly accessible Jenkins instances for build // console output containing leaked API keys. It queries the Jenkins JSON API // at /api/json to enumerate jobs and their latest builds. Credentialless -- // targets open Jenkins instances discovered via dorking or IoT scanners. type JenkinsSource struct { BaseURL string Registry *providers.Registry Limiters *recon.LimiterRegistry Client *Client } var _ recon.ReconSource = (*JenkinsSource)(nil) func (s *JenkinsSource) Name() string { return "jenkins" } func (s *JenkinsSource) RateLimit() rate.Limit { return rate.Every(3 * time.Second) } func (s *JenkinsSource) Burst() int { return 1 } func (s *JenkinsSource) RespectsRobots() bool { return true } func (s *JenkinsSource) Enabled(_ recon.Config) bool { return true } type jenkinsJobsResponse struct { Jobs []jenkinsJob `json:"jobs"` } type jenkinsJob struct { Name string `json:"name"` URL string `json:"url"` LastBuild *jenkinsBuild `json:"lastBuild"` } type jenkinsBuild struct { Number int `json:"number"` URL string `json:"url"` } func (s *JenkinsSource) Sweep(ctx context.Context, _ string, out chan<- recon.Finding) error { base := s.BaseURL if base == "" { base = "https://jenkins.example.com" } client := s.Client if client == nil { client = NewClient() } queries := BuildQueries(s.Registry, "jenkins") kwIndex := jenkinsKeywordIndex(s.Registry) for _, q := range queries { 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 } } endpoint := fmt.Sprintf("%s/api/json?tree=jobs[name,url,lastBuild[number,url]]", base) req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) if err != nil { return fmt.Errorf("jenkins: build request: %w", err) } req.Header.Set("Accept", "application/json") resp, err := client.Do(ctx, req) if err != nil { continue } var result jenkinsJobsResponse decErr := json.NewDecoder(resp.Body).Decode(&result) _ = resp.Body.Close() if decErr != nil { continue } provName := kwIndex[strings.ToLower(q)] for _, job := range result.Jobs { if job.LastBuild == nil { continue } source := job.LastBuild.URL if source == "" { source = fmt.Sprintf("%s/job/%s/%d/console", base, job.Name, job.LastBuild.Number) } f := recon.Finding{ ProviderName: provName, Confidence: "low", Source: source, SourceType: "recon:jenkins", DetectedAt: time.Now(), } select { case out <- f: case <-ctx.Done(): return ctx.Err() } } } return nil } func jenkinsKeywordIndex(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 }