Debian Codegraph @

pkg/scraper/scraper.go

raw symbols SCIP
1 package scraper
2
3 import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "path/filepath"
8 "regexp"
9 "strings"
10
11 "github.com/ffuf/ffuf/v2/pkg/ffuf"
12
13 "github.com/PuerkitoBio/goquery"
14 )
15
16 type ScraperRule struct {
17 Name string `json:"name"`
18 Rule string `json:"rule"`
19 Target string `json:"target"`
20 compiledRule *regexp.Regexp
21 Type string `json:"type"`
22 OnlyMatched bool `json:"onlymatched"`
23 Action []string `json:"action"`
24 }
25
26 type ScraperGroup struct {
27 Rules []*ScraperRule `json:"rules"`
28 Name string `json:"groupname"`
29 Active bool `json:"active"`
30 }
31
32 type Scraper struct {
33 Rules []*ScraperRule
34 }
35
36 func readGroupFromFile(filename string) (ScraperGroup, error) {
37 data, err := os.ReadFile(filename)
38 if err != nil {
39 return ScraperGroup{Rules: make([]*ScraperRule, 0)}, err
40 }
41 sc := ScraperGroup{}
42 err = json.Unmarshal([]byte(data), &sc)
43 return sc, err
44 }
45
46 func FromDir(dirname string, activestr string) (ffuf.Scraper, ffuf.Multierror) {
47 scr := Scraper{Rules: make([]*ScraperRule, 0)}
48 errs := ffuf.NewMultierror()
49 activegrps := parseActiveGroups(activestr)
50 all_files, err := os.ReadDir(ffuf.SCRAPERDIR)
51 if err != nil {
52 errs.Add(err)
53 return &scr, errs
54 }
55 for _, filename := range all_files {
56 if filename.Type().IsRegular() && strings.HasSuffix(filename.Name(), ".json") {
57 sg, err := readGroupFromFile(filepath.Join(dirname, filename.Name()))
58 if err != nil {
59 cerr := fmt.Errorf("%s : %s", filepath.Join(dirname, filename.Name()), err)
60 errs.Add(cerr)
61 continue
62 }
63 if (sg.Active && isActive("all", activegrps)) || isActive(sg.Name, activegrps) {
64 for _, r := range sg.Rules {
65 err = r.init()
66 if err != nil {
67 cerr := fmt.Errorf("%s : %s", filepath.Join(dirname, filename.Name()), err)
68 errs.Add(cerr)
69 continue
70 }
71 scr.Rules = append(scr.Rules, r)
72 }
73 }
74 }
75 }
76 return &scr, errs
77 }
78
79 // FromFile initializes a scraper instance and reads rules from a file
80 func (s *Scraper) AppendFromFile(path string) error {
81 sg, err := readGroupFromFile(path)
82 if err != nil {
83 return err
84 }
85
86 for _, r := range sg.Rules {
87 err = r.init()
88 if err != nil {
89 continue
90 }
91 s.Rules = append(s.Rules, r)
92 }
93
94 return err
95 }
96
97 func (s *Scraper) Execute(resp *ffuf.Response, matched bool) []ffuf.ScraperResult {
98 res := make([]ffuf.ScraperResult, 0)
99 for _, rule := range s.Rules {
100 if !matched && rule.OnlyMatched {
101 // pass this rule as there was no match
102 continue
103 }
104 sourceData := ""
105 if rule.Target == "body" {
106 sourceData = string(resp.Data)
107 } else if rule.Target == "headers" {
108 sourceData = headerString(resp.Headers)
109 } else {
110 sourceData = headerString(resp.Headers) + string(resp.Data)
111 }
112 val := rule.Check(sourceData)
113 if len(val) > 0 {
114 res = append(res, ffuf.ScraperResult{
115 Name: rule.Name,
116 Type: rule.Type,
117 Action: rule.Action,
118 Results: val,
119 })
120 }
121 }
122 return res
123 }
124
125 // init initializes the scraper rule, and returns an error in case there's an error in the syntax
126 func (r *ScraperRule) init() error {
127 var err error
128 if r.Type == "regexp" {
129 r.compiledRule, err = regexp.Compile(r.Rule)
130 if err != nil {
131 return err
132 }
133 }
134 return err
135 }
136
137 func (r *ScraperRule) Check(data string) []string {
138 if r.Type == "regexp" {
139 return r.checkRegexp(data)
140 } else if r.Type == "query" {
141 return r.checkQuery(data)
142 }
143 return []string{}
144 }
145
146 func (r *ScraperRule) checkQuery(data string) []string {
147 val := make([]string, 0)
148 doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
149 if err != nil {
150 return []string{}
151 }
152 doc.Find(r.Rule).Each(func(i int, sel *goquery.Selection) {
153 val = append(val, sel.Text())
154 })
155 return val
156 }
157
158 func (r *ScraperRule) checkRegexp(data string) []string {
159 val := make([]string, 0)
160 if r.compiledRule != nil {
161 res := r.compiledRule.FindAllStringSubmatch(data, -1)
162 for _, grp := range res {
163 val = append(val, grp...)
164 }
165 return val
166 }
167 return []string{}
168 }