|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + awesomesearchqueries "github.com/projectdiscovery/awesome-search-queries" |
| 9 | +) |
| 10 | + |
| 11 | +type CPEInfo struct { |
| 12 | + Product string `json:"product,omitempty"` |
| 13 | + Vendor string `json:"vendor,omitempty"` |
| 14 | + CPE string `json:"cpe,omitempty"` |
| 15 | +} |
| 16 | + |
| 17 | +type CPEDetector struct { |
| 18 | + titlePatterns map[string][]CPEInfo |
| 19 | + bodyPatterns map[string][]CPEInfo |
| 20 | + faviconPatterns map[string][]CPEInfo |
| 21 | +} |
| 22 | + |
| 23 | +type rawQuery struct { |
| 24 | + Name string `json:"name"` |
| 25 | + Vendor json.RawMessage `json:"vendor"` |
| 26 | + Type string `json:"type"` |
| 27 | + Engines []rawEngine `json:"engines"` |
| 28 | +} |
| 29 | + |
| 30 | +type rawEngine struct { |
| 31 | + Platform string `json:"platform"` |
| 32 | + Queries []string `json:"queries"` |
| 33 | +} |
| 34 | + |
| 35 | +func NewCPEDetector() (*CPEDetector, error) { |
| 36 | + data, err := awesomesearchqueries.GetQueries() |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to load queries: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + var queries []rawQuery |
| 42 | + if err := json.Unmarshal(data, &queries); err != nil { |
| 43 | + return nil, fmt.Errorf("failed to parse queries: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + detector := &CPEDetector{ |
| 47 | + titlePatterns: make(map[string][]CPEInfo), |
| 48 | + bodyPatterns: make(map[string][]CPEInfo), |
| 49 | + faviconPatterns: make(map[string][]CPEInfo), |
| 50 | + } |
| 51 | + |
| 52 | + for _, q := range queries { |
| 53 | + vendor := parseVendor(q.Vendor) |
| 54 | + info := CPEInfo{ |
| 55 | + Product: q.Name, |
| 56 | + Vendor: vendor, |
| 57 | + CPE: generateCPE(vendor, q.Name), |
| 58 | + } |
| 59 | + |
| 60 | + for _, engine := range q.Engines { |
| 61 | + for _, query := range engine.Queries { |
| 62 | + detector.extractPattern(query, info) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return detector, nil |
| 68 | +} |
| 69 | + |
| 70 | +func parseVendor(raw json.RawMessage) string { |
| 71 | + var vendorStr string |
| 72 | + if err := json.Unmarshal(raw, &vendorStr); err == nil { |
| 73 | + return vendorStr |
| 74 | + } |
| 75 | + |
| 76 | + var vendorSlice []string |
| 77 | + if err := json.Unmarshal(raw, &vendorSlice); err == nil && len(vendorSlice) > 0 { |
| 78 | + return vendorSlice[0] |
| 79 | + } |
| 80 | + |
| 81 | + return "" |
| 82 | +} |
| 83 | + |
| 84 | +func generateCPE(vendor, product string) string { |
| 85 | + if vendor == "" || product == "" { |
| 86 | + return "" |
| 87 | + } |
| 88 | + return fmt.Sprintf("cpe:2.3:a:%s:%s:*:*:*:*:*:*:*:*", |
| 89 | + strings.ToLower(strings.ReplaceAll(vendor, " ", "_")), |
| 90 | + strings.ToLower(strings.ReplaceAll(product, " ", "_"))) |
| 91 | +} |
| 92 | + |
| 93 | +func (d *CPEDetector) extractPattern(query string, info CPEInfo) { |
| 94 | + query = strings.TrimSpace(query) |
| 95 | + |
| 96 | + titlePrefixes := []string{ |
| 97 | + "http.title:", |
| 98 | + "title=", |
| 99 | + "title==", |
| 100 | + "intitle:", |
| 101 | + "title:", |
| 102 | + "title='", |
| 103 | + `title="`, |
| 104 | + } |
| 105 | + |
| 106 | + for _, prefix := range titlePrefixes { |
| 107 | + if strings.HasPrefix(strings.ToLower(query), strings.ToLower(prefix)) { |
| 108 | + pattern := extractQuotedValue(strings.TrimPrefix(query, prefix)) |
| 109 | + pattern = strings.TrimPrefix(pattern, prefix[:len(prefix)-1]) |
| 110 | + if pattern != "" { |
| 111 | + pattern = strings.ToLower(pattern) |
| 112 | + d.titlePatterns[pattern] = appendUnique(d.titlePatterns[pattern], info) |
| 113 | + } |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + bodyPrefixes := []string{ |
| 119 | + "http.html:", |
| 120 | + "body=", |
| 121 | + "body==", |
| 122 | + "intext:", |
| 123 | + } |
| 124 | + |
| 125 | + for _, prefix := range bodyPrefixes { |
| 126 | + if strings.HasPrefix(strings.ToLower(query), strings.ToLower(prefix)) { |
| 127 | + pattern := extractQuotedValue(strings.TrimPrefix(query, prefix)) |
| 128 | + if pattern != "" { |
| 129 | + pattern = strings.ToLower(pattern) |
| 130 | + d.bodyPatterns[pattern] = appendUnique(d.bodyPatterns[pattern], info) |
| 131 | + } |
| 132 | + return |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + faviconPrefixes := []string{ |
| 137 | + "http.favicon.hash:", |
| 138 | + "icon_hash=", |
| 139 | + "icon_hash==", |
| 140 | + } |
| 141 | + |
| 142 | + for _, prefix := range faviconPrefixes { |
| 143 | + if strings.HasPrefix(strings.ToLower(query), strings.ToLower(prefix)) { |
| 144 | + pattern := extractQuotedValue(strings.TrimPrefix(query, prefix)) |
| 145 | + if pattern != "" { |
| 146 | + d.faviconPatterns[pattern] = appendUnique(d.faviconPatterns[pattern], info) |
| 147 | + } |
| 148 | + return |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func extractQuotedValue(s string) string { |
| 154 | + s = strings.TrimSpace(s) |
| 155 | + |
| 156 | + if len(s) >= 2 { |
| 157 | + if (s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '\'' && s[len(s)-1] == '\'') { |
| 158 | + s = s[1 : len(s)-1] |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if idx := strings.Index(s, "\" ||"); idx > 0 { |
| 163 | + s = s[:idx] |
| 164 | + } |
| 165 | + if idx := strings.Index(s, "' ||"); idx > 0 { |
| 166 | + s = s[:idx] |
| 167 | + } |
| 168 | + |
| 169 | + return strings.TrimSpace(s) |
| 170 | +} |
| 171 | + |
| 172 | +func appendUnique(slice []CPEInfo, info CPEInfo) []CPEInfo { |
| 173 | + for _, existing := range slice { |
| 174 | + if existing.Product == info.Product && existing.Vendor == info.Vendor { |
| 175 | + return slice |
| 176 | + } |
| 177 | + } |
| 178 | + return append(slice, info) |
| 179 | +} |
| 180 | + |
| 181 | +func (d *CPEDetector) Detect(title, body, faviconHash string) []CPEInfo { |
| 182 | + seen := make(map[string]bool) |
| 183 | + var results []CPEInfo |
| 184 | + |
| 185 | + titleLower := strings.ToLower(title) |
| 186 | + bodyLower := strings.ToLower(body) |
| 187 | + |
| 188 | + for pattern, infos := range d.titlePatterns { |
| 189 | + if strings.Contains(titleLower, pattern) { |
| 190 | + for _, info := range infos { |
| 191 | + key := info.Product + "|" + info.Vendor |
| 192 | + if !seen[key] { |
| 193 | + seen[key] = true |
| 194 | + results = append(results, info) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + for pattern, infos := range d.bodyPatterns { |
| 201 | + if strings.Contains(bodyLower, pattern) { |
| 202 | + for _, info := range infos { |
| 203 | + key := info.Product + "|" + info.Vendor |
| 204 | + if !seen[key] { |
| 205 | + seen[key] = true |
| 206 | + results = append(results, info) |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + if faviconHash != "" { |
| 213 | + if infos, ok := d.faviconPatterns[faviconHash]; ok { |
| 214 | + for _, info := range infos { |
| 215 | + key := info.Product + "|" + info.Vendor |
| 216 | + if !seen[key] { |
| 217 | + seen[key] = true |
| 218 | + results = append(results, info) |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + return results |
| 225 | +} |
0 commit comments