package sources import ( "context" "encoding/json" "fmt" <<<<<<< HEAD "net/http" "strings" ======= "io" "net/http" >>>>>>> worktree-agent-adad8c10 "time" "golang.org/x/time/rate" "github.com/salvacybersec/keyhunter/pkg/providers" "github.com/salvacybersec/keyhunter/pkg/recon" ) <<<<<<< HEAD // 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. ======= // JenkinsSource scrapes publicly accessible Jenkins build consoles for leaked // API keys. Many Jenkins instances are exposed to the internet without // authentication, and build console output frequently contains printed // environment variables or secrets passed via command-line arguments. >>>>>>> worktree-agent-adad8c10 type JenkinsSource struct { BaseURL string Registry *providers.Registry Limiters *recon.LimiterRegistry Client *Client } var _ recon.ReconSource = (*JenkinsSource)(nil) <<<<<<< HEAD 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 } ======= 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 2 } func (s *JenkinsSource) RespectsRobots() bool { return true } func (s *JenkinsSource) Enabled(_ recon.Config) bool { return true } // jenkinsJobsResponse represents the Jenkins API jobs listing. >>>>>>> worktree-agent-adad8c10 type jenkinsJobsResponse struct { Jobs []jenkinsJob `json:"jobs"` } type jenkinsJob struct { <<<<<<< HEAD Name string `json:"name"` URL string `json:"url"` LastBuild *jenkinsBuild `json:"lastBuild"` } type jenkinsBuild struct { Number int `json:"number"` URL string `json:"url"` ======= Name string `json:"name"` URL string `json:"url"` Color string `json:"color"` >>>>>>> worktree-agent-adad8c10 } func (s *JenkinsSource) Sweep(ctx context.Context, _ string, out chan<- recon.Finding) error { base := s.BaseURL if base == "" { <<<<<<< HEAD base = "https://jenkins.example.com" ======= return nil // No default; Jenkins instances are discovered via dorking >>>>>>> worktree-agent-adad8c10 } client := s.Client if client == nil { client = NewClient() } queries := BuildQueries(s.Registry, "jenkins") <<<<<<< HEAD kwIndex := jenkinsKeywordIndex(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/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) ======= // List jobs from the Jenkins API. jobsURL := fmt.Sprintf("%s/api/json?tree=jobs[name,url,color]", base) req, err := http.NewRequestWithContext(ctx, http.MethodGet, jobsURL, nil) if err != nil { continue >>>>>>> worktree-agent-adad8c10 } req.Header.Set("Accept", "application/json") resp, err := client.Do(ctx, req) if err != nil { continue } <<<<<<< HEAD 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() ======= var jobs jenkinsJobsResponse if err := json.NewDecoder(resp.Body).Decode(&jobs); err != nil { _ = resp.Body.Close() continue } _ = resp.Body.Close() for _, job := range jobs.Jobs { 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 last build console output. consoleURL := fmt.Sprintf("%s/job/%s/lastBuild/consoleText", base, job.Name) consoleReq, err := http.NewRequestWithContext(ctx, http.MethodGet, consoleURL, nil) if err != nil { continue } consoleResp, err := client.Do(ctx, consoleReq) if err != nil { continue } body, err := io.ReadAll(io.LimitReader(consoleResp.Body, 256*1024)) _ = consoleResp.Body.Close() if err != nil { continue } if ciLogKeyPattern.Match(body) { out <- recon.Finding{ ProviderName: q, Source: consoleURL, SourceType: "recon:jenkins", Confidence: "medium", DetectedAt: time.Now(), } >>>>>>> worktree-agent-adad8c10 } } } return nil } <<<<<<< HEAD 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 } ======= >>>>>>> worktree-agent-adad8c10