|
| 1 | +/** |
| 2 | +2 * @Author: shaochuyu |
| 3 | +3 * @Date: 8/4/24 |
| 4 | +4 */ |
| 5 | + |
| 6 | +package js |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + _ "embed" |
| 11 | + "fmt" |
| 12 | + "gopkg.in/yaml.v2" |
| 13 | + "os" |
| 14 | + "regexp" |
| 15 | + "strings" |
| 16 | + "time" |
| 17 | + "wscan/core/http" |
| 18 | + "wscan/core/model" |
| 19 | + "wscan/core/plugins/base" |
| 20 | + logger "wscan/core/utils/log" |
| 21 | +) |
| 22 | + |
| 23 | +//go:embed "sensitive_content_check.yml" |
| 24 | +var ruleYaml []byte |
| 25 | + |
| 26 | +// Rule defines the structure of a single regex rule |
| 27 | +type Rule struct { |
| 28 | + Title string `yaml:"title"` |
| 29 | + Pattern string `yaml:"regex"` |
| 30 | + Compiled *regexp.Regexp `yaml:"-"` |
| 31 | +} |
| 32 | + |
| 33 | +// RuleSet holds a slice of rules |
| 34 | +type RuleSet struct { |
| 35 | + Rules []Rule `yaml:"rules"` |
| 36 | +} |
| 37 | + |
| 38 | +func LoadRulesFromFile(filename string) ([]Rule, error) { |
| 39 | + data, err := os.ReadFile(filename) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return LoadRulesWithRaw(data) |
| 44 | +} |
| 45 | + |
| 46 | +func LoadRulesWithRaw(raw []byte) ([]Rule, error) { |
| 47 | + var ruleSet RuleSet |
| 48 | + err := yaml.Unmarshal(raw, &ruleSet) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + // Compile regexes |
| 54 | + for i := range ruleSet.Rules { |
| 55 | + re, err := regexp.Compile(ruleSet.Rules[i].Pattern) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + ruleSet.Rules[i].Compiled = re // Store compiled regex |
| 60 | + } |
| 61 | + |
| 62 | + return ruleSet.Rules, nil |
| 63 | +} |
| 64 | + |
| 65 | +// ScanText checks for sensitive information in the given text |
| 66 | +func ScanText(text string, rules []Rule) { |
| 67 | + for _, rule := range rules { |
| 68 | + if rule.Compiled == nil { |
| 69 | + logger.Printf("No compiled regex for rule %s", rule.Title) |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + matches := rule.Compiled.FindAllString(text, -1) |
| 74 | + if matches != nil { |
| 75 | + for _, match := range matches { |
| 76 | + // Check if the match should be ignored based on the content |
| 77 | + ignored := []string{"function", "encodeURIComponent", "XMLHttpRequest"} |
| 78 | + shouldIgnore := false |
| 79 | + for _, ignore := range ignored { |
| 80 | + if strings.Contains(match, ignore) { |
| 81 | + shouldIgnore = true |
| 82 | + break |
| 83 | + } |
| 84 | + } |
| 85 | + if shouldIgnore { |
| 86 | + continue |
| 87 | + } |
| 88 | + fmt.Printf("Found sensitive information:\nType: %s\nMatch: %s\n", rule.Title, match) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +type SensitiveContentCheck struct { |
| 95 | + rules []Rule |
| 96 | +} |
| 97 | + |
| 98 | +func (p *SensitiveContentCheck) Finger() *base.Finger { |
| 99 | + return &base.Finger{ |
| 100 | + CheckAction: func(ctx context.Context, ab *base.Apollo) error { |
| 101 | + flow := ab.GetTargetFlow() |
| 102 | + logger.Infof("Start js/sensitive-content-check, %s", flow.Request.URL()) |
| 103 | + for _, rule := range p.rules { |
| 104 | + if rule.Compiled == nil { |
| 105 | + logger.Printf("No compiled regex for rule %s", rule.Title) |
| 106 | + continue |
| 107 | + } |
| 108 | + matches := rule.Compiled.FindAllString(flow.Response.Text, -1) |
| 109 | + if matches != nil { |
| 110 | + fp := &model.Vuln{ |
| 111 | + Payload: rule.Pattern, |
| 112 | + Param: nil, |
| 113 | + Flow: []*http.Flow{{Request: flow.Request, Response: flow.Response}}, |
| 114 | + Binding: &model.VulnBinding{Plugin: "js/sensitive-content-check", Category: "js/sensitive-content-check", ID: "js/sensitive-content-check"}, |
| 115 | + Extra: map[string]interface{}{ |
| 116 | + "title": rule.Title, |
| 117 | + "matches": matches, |
| 118 | + }, |
| 119 | + CreateTime: time.Now().Unix(), |
| 120 | + } |
| 121 | + fp.SetTargetURL(flow.Request.URL()) |
| 122 | + ab.OutputVuln(fp) |
| 123 | + |
| 124 | + } |
| 125 | + } |
| 126 | + return nil |
| 127 | + }, |
| 128 | + Channel: "javascript", |
| 129 | + Binding: &model.VulnBinding{ID: "js/sensitive-content-check", Plugin: "js/sensitive-content-check", Category: "js/sensitive-content-check"}, |
| 130 | + } |
| 131 | +} |
0 commit comments